Skip to content

Commit e178b57

Browse files
committed
cleanup
1 parent 9f15c20 commit e178b57

File tree

4 files changed

+36
-62
lines changed

4 files changed

+36
-62
lines changed

packages/snap/src/index.test.tsx

Lines changed: 0 additions & 50 deletions
This file was deleted.

packages/snap/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let wasmModule: InitOutput;
99
*
1010
* invoked the snap.
1111
* @param args.request - A validated JSON-RPC request object.
12-
* @returns The result of `snap_dialog`.
12+
* @returns The ViewingKey
1313
* @throws If the request method is not valid for this snap.
1414
*/
1515
export const onRpcRequest: ({

packages/snap/src/rpc/getViewingKey.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ export async function getViewingKey() {
1010

1111
// Define the account index and network
1212
const accountIndex = 0;
13-
const network = 'main' as Network;
13+
const network = 'test' as Network;
1414

1515
// Generate the UnifiedSpendingKey and obtain the Viewing Key
1616
const viewingKey = await generateViewingKey(seed, accountIndex, network);
1717

1818
console.log('viewingKey', viewingKey);
19-
// Return the Viewing Key to the dApp
2019
return viewingKey.encode(network);
2120
} catch (error) {
2221
console.error('Error generating Viewing Key:', error);
Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
1-
function hexStringToUint8Array(hex: string): Uint8Array {
2-
if (hex.length % 2 !== 0) {
3-
throw new Error('Invalid hex string: length must be even');
1+
/**
2+
* Converts a hexadecimal string to a Uint8Array.
3+
* @param hexString - The hexadecimal string to convert.
4+
* @returns A Uint8Array representing the binary data of the hex string.
5+
* @throws Will throw an error if the input string contains non-hex characters or has an odd length.
6+
*/
7+
function hexStringToUint8Array(hexString: string): Uint8Array {
8+
// Remove any leading "0x" or "0X" if present
9+
if (hexString.startsWith('0x') || hexString.startsWith('0X')) {
10+
hexString = hexString.slice(2);
11+
}
12+
13+
if (!/^[0-9a-fA-F]*$/.test(hexString)) {
14+
throw new Error('Hex string contains invalid characters');
15+
}
16+
17+
if (hexString.length % 2 !== 0) {
18+
throw new Error('Hex string must have an even length');
19+
}
20+
21+
const byteArray = new Uint8Array(hexString.length / 2);
22+
23+
for (let i = 0; i < byteArray.length; i++) {
24+
const byte = hexString.slice(i * 2, i * 2 + 2);
25+
const byteValue = parseInt(byte, 16);
26+
27+
if (isNaN(byteValue)) {
28+
throw new Error(`Invalid hex byte: "${byte}"`);
429
}
5-
const uint8Array = new Uint8Array(hex.length / 2);
6-
for (let i = 0; i < uint8Array.length; i++) {
7-
uint8Array[i] = parseInt(hex.substr(i * 2, 2), 16);
8-
}
9-
return uint8Array;
10-
}
30+
31+
byteArray[i] = byteValue;
32+
}
33+
34+
return byteArray;
35+
}

0 commit comments

Comments
 (0)