]> git.codecow.com Git - libnemo.git/commitdiff
Convenient method to reduce explicit buffer access.
authorChris Duncan <chris@zoso.dev>
Sat, 26 Jul 2025 05:38:44 +0000 (22:38 -0700)
committerChris Duncan <chris@zoso.dev>
Sat, 26 Jul 2025 05:38:44 +0000 (22:38 -0700)
src/lib/account.ts
src/lib/block.ts
src/lib/convert.ts
src/lib/rolodex.ts
src/lib/tools.ts

index 935728854d1db17da59f461266e8e47db54816d5..56a2b8acaa45a493f65329d3dd5b8f8c0b61d38e 100644 (file)
@@ -194,7 +194,7 @@ export class Account {
        async export (password: Key, format?: 'hex'): Promise<Key> {\r
                if (typeof password === 'string') password = utf8.toBytes(password)\r
                try {\r
-                       const privateKey = await this.#export(password)\r
+                       const privateKey = new Uint8Array(await this.#export(password))\r
                        return format === 'hex'\r
                                ? bytes.toHex(privateKey)\r
                                : privateKey\r
@@ -251,8 +251,8 @@ export class Account {
                try {\r
                        const { signature } = await NanoNaClWorker.request<ArrayBuffer>({\r
                                method: 'detached',\r
-                               privateKey: (await this.#export(password)).buffer,\r
-                               msg: hex.toBytes(block.hash).buffer\r
+                               privateKey: await this.#export(password),\r
+                               msg: hex.toBuffer(block.hash)\r
                        })\r
                        block.signature = bytes.toHex(new Uint8Array(signature))\r
                        return block.signature\r
@@ -317,7 +317,7 @@ export class Account {
        * @param {Key} password Used previously to lock the Account\r
        * @returns Private key bytes as a Uint8Array\r
        */\r
-       async #export (password: Key): Promise<Uint8Array<ArrayBuffer>> {\r
+       async #export (password: Key): Promise<ArrayBuffer> {\r
                if (typeof password === 'string') password = utf8.toBytes(password)\r
                if (password == null || !(password instanceof Uint8Array)) {\r
                        throw new Error('Password must be string or bytes')\r
@@ -329,7 +329,7 @@ export class Account {
                                store: 'Account',\r
                                password: password.buffer\r
                        })\r
-                       return new Uint8Array(response[this.publicKey])\r
+                       return response[this.publicKey]\r
                } catch (err) {\r
                        throw new Error(`Failed to export private key for Account ${this.address}`, { cause: err })\r
                } finally {\r
@@ -368,7 +368,7 @@ export class Account {
                        try {\r
                                const result = await NanoNaClWorker.request<ArrayBuffer>({\r
                                        method: 'convert',\r
-                                       privateKey: new Uint8Array(privateKey).buffer\r
+                                       privateKey: privateKey.buffer.slice()\r
                                })\r
                                const publicKey = new Uint8Array(result.publicKey)\r
                                privateAccounts[bytes.toHex(publicKey)] = privateKey.buffer\r
index 71a99be615fe3af1dce8e37066964bf83a5be493..d969a65d5bedc4bf836fd857a9a9a64cae3962ab 100644 (file)
@@ -190,9 +190,9 @@ abstract class Block {
                try {
                        const { isVerified } = await NanoNaClWorker.request<boolean>({
                                method: 'verify',
-                               msg: hex.toBytes(this.hash).buffer,
-                               signature: hex.toBytes(this.signature ?? '').buffer,
-                               publicKey: hex.toBytes(key).buffer
+                               msg: hex.toBuffer(this.hash),
+                               signature: hex.toBuffer(this.signature ?? ''),
+                               publicKey: hex.toBuffer(key)
                        })
                        return isVerified
                } catch (err) {
index e863dc73c01f512d030b783500cca0d9b62fc3a6..aa6440d1cebff4893cdb418af6bfafc09b1c6d9b 100644 (file)
@@ -265,6 +265,17 @@ export class hex {
                return [...hex].map(c => dec.toBin(parseInt(c, 16), 4)).join('')\r
        }\r
 \r
+       /**\r
+       * Convert a hexadecimal string to an ArrayBuffer.\r
+       *\r
+       * @param {string} hex - Hexadecimal number string to convert\r
+       * @param {number} [padding=0] - Minimum length of the resulting array padded as necessary with starting 0x00 bytes\r
+       * @returns {ArrayBuffer} Buffer representation of the input value\r
+       */\r
+       static toBuffer (hex: string, padding: number = 0): ArrayBuffer {\r
+               return this.toBytes(hex, padding).buffer\r
+       }\r
+\r
        /**\r
        * Convert a hexadecimal string to a Uint8Array of bytes.\r
        *\r
@@ -296,6 +307,16 @@ export class obj {
 export class utf8 {\r
        static #encoder: TextEncoder = new TextEncoder()\r
 \r
+       /**\r
+       * Convert a UTF-8 text string to an ArrayBuffer.\r
+       *\r
+       * @param {string} utf8 - String to convert\r
+       * @returns {ArrayBuffer} Buffer representation of the input string\r
+       */\r
+       static toBuffer (utf8: string): ArrayBuffer {\r
+               return this.toBytes(utf8).buffer\r
+       }\r
+\r
        /**\r
        * Convert a UTF-8 text string to a Uint8Array of bytes.\r
        *\r
index 0668001c0d60eab993d608c003b7e79c412b739f..e3efbd5791c254c934d03d30c9ac443214488b48 100644 (file)
@@ -54,16 +54,16 @@ export class Rolodex {
                        const data: NamedData = {
                                method: 'store',
                                store: 'Rolodex',
-                               password: utf8.toBytes('').buffer,
-                               [address]: utf8.toBytes(name).buffer
+                               password: utf8.toBuffer(''),
+                               [address]: utf8.toBuffer(name)
                        }
                        if (existingName != null) {
                                const filteredAddresses = (await this.getAddresses(existingName)).filter(a => a !== address).sort()
-                               data[existingName] = utf8.toBytes(JSON.stringify(filteredAddresses)).buffer
+                               data[existingName] = utf8.toBuffer(JSON.stringify(filteredAddresses))
                        }
                        const existingAddresses = await this.getAddresses(name)
                        existingAddresses.push(account.address)
-                       data[name] = utf8.toBytes(JSON.stringify(existingAddresses)).buffer
+                       data[name] = utf8.toBuffer(JSON.stringify(existingAddresses))
                        const { result } = await SafeWorker.request<boolean>(data)
                        return result
                } catch (err) {
@@ -86,8 +86,8 @@ export class Rolodex {
                const { result: isUpdated } = await SafeWorker.request<boolean>({
                        method: 'store',
                        store: 'Rolodex',
-                       password: utf8.toBytes('').buffer,
-                       [name]: utf8.toBytes(JSON.stringify(addresses)).buffer
+                       password: utf8.toBuffer(''),
+                       [name]: utf8.toBuffer(JSON.stringify(addresses))
                })
                if (!isUpdated) {
                        throw new Error('failed to remove address from existing name')
@@ -132,7 +132,7 @@ export class Rolodex {
                                method: 'fetch',
                                name,
                                store: 'Rolodex',
-                               password: utf8.toBytes('').buffer
+                               password: utf8.toBuffer('')
                        })
                        const addresses = response[name]
                        return addresses
@@ -155,7 +155,7 @@ export class Rolodex {
                                method: 'export',
                                name: '',
                                store: 'Rolodex',
-                               password: utf8.toBytes('').buffer
+                               password: utf8.toBuffer('')
                        })
                        return Object.keys(response).filter(v => v.slice(0, 5) !== 'nano_')
                } catch (err) {
@@ -176,7 +176,7 @@ export class Rolodex {
                                method: 'fetch',
                                name: address,
                                store: 'Rolodex',
-                               password: utf8.toBytes('').buffer
+                               password: utf8.toBuffer('')
                        })
                        const name = response[address]
                        return name
index d8935d75f3d9d59f0f0e9dfbe680f135cc79a437..f8596e59a0fa718b804b50cfcab87c222d9371d1 100644 (file)
@@ -180,8 +180,8 @@ export async function verify (key: Key, signature: string, ...input: string[]):
                const { isVerified } = await NanoNaClWorker.request<boolean>({
                        method: 'verify',
                        msg: hash(input).buffer,
-                       signature: hex.toBytes(signature).buffer,
-                       publicKey: new Uint8Array(key).buffer
+                       signature: hex.toBuffer(signature),
+                       publicKey: key.buffer.slice()
                })
                return isVerified
        } catch (err) {