From: Chris Duncan Date: Sat, 26 Jul 2025 05:38:44 +0000 (-0700) Subject: Convenient method to reduce explicit buffer access. X-Git-Tag: v0.10.5~50^2~20 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=91d1f637416d0d776c53d434f5b1a415d6480442;p=libnemo.git Convenient method to reduce explicit buffer access. --- diff --git a/src/lib/account.ts b/src/lib/account.ts index 9357288..56a2b8a 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -194,7 +194,7 @@ export class Account { async export (password: Key, format?: 'hex'): Promise { if (typeof password === 'string') password = utf8.toBytes(password) try { - const privateKey = await this.#export(password) + const privateKey = new Uint8Array(await this.#export(password)) return format === 'hex' ? bytes.toHex(privateKey) : privateKey @@ -251,8 +251,8 @@ export class Account { try { const { signature } = await NanoNaClWorker.request({ method: 'detached', - privateKey: (await this.#export(password)).buffer, - msg: hex.toBytes(block.hash).buffer + privateKey: await this.#export(password), + msg: hex.toBuffer(block.hash) }) block.signature = bytes.toHex(new Uint8Array(signature)) return block.signature @@ -317,7 +317,7 @@ export class Account { * @param {Key} password Used previously to lock the Account * @returns Private key bytes as a Uint8Array */ - async #export (password: Key): Promise> { + async #export (password: Key): Promise { if (typeof password === 'string') password = utf8.toBytes(password) if (password == null || !(password instanceof Uint8Array)) { throw new Error('Password must be string or bytes') @@ -329,7 +329,7 @@ export class Account { store: 'Account', password: password.buffer }) - return new Uint8Array(response[this.publicKey]) + return response[this.publicKey] } catch (err) { throw new Error(`Failed to export private key for Account ${this.address}`, { cause: err }) } finally { @@ -368,7 +368,7 @@ export class Account { try { const result = await NanoNaClWorker.request({ method: 'convert', - privateKey: new Uint8Array(privateKey).buffer + privateKey: privateKey.buffer.slice() }) const publicKey = new Uint8Array(result.publicKey) privateAccounts[bytes.toHex(publicKey)] = privateKey.buffer diff --git a/src/lib/block.ts b/src/lib/block.ts index 71a99be..d969a65 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -190,9 +190,9 @@ abstract class Block { try { const { isVerified } = await NanoNaClWorker.request({ 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) { diff --git a/src/lib/convert.ts b/src/lib/convert.ts index e863dc7..aa6440d 100644 --- a/src/lib/convert.ts +++ b/src/lib/convert.ts @@ -265,6 +265,17 @@ export class hex { return [...hex].map(c => dec.toBin(parseInt(c, 16), 4)).join('') } + /** + * Convert a hexadecimal string to an ArrayBuffer. + * + * @param {string} hex - Hexadecimal number string to convert + * @param {number} [padding=0] - Minimum length of the resulting array padded as necessary with starting 0x00 bytes + * @returns {ArrayBuffer} Buffer representation of the input value + */ + static toBuffer (hex: string, padding: number = 0): ArrayBuffer { + return this.toBytes(hex, padding).buffer + } + /** * Convert a hexadecimal string to a Uint8Array of bytes. * @@ -296,6 +307,16 @@ export class obj { export class utf8 { static #encoder: TextEncoder = new TextEncoder() + /** + * Convert a UTF-8 text string to an ArrayBuffer. + * + * @param {string} utf8 - String to convert + * @returns {ArrayBuffer} Buffer representation of the input string + */ + static toBuffer (utf8: string): ArrayBuffer { + return this.toBytes(utf8).buffer + } + /** * Convert a UTF-8 text string to a Uint8Array of bytes. * diff --git a/src/lib/rolodex.ts b/src/lib/rolodex.ts index 0668001..e3efbd5 100644 --- a/src/lib/rolodex.ts +++ b/src/lib/rolodex.ts @@ -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(data) return result } catch (err) { @@ -86,8 +86,8 @@ export class Rolodex { const { result: isUpdated } = await SafeWorker.request({ 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 diff --git a/src/lib/tools.ts b/src/lib/tools.ts index d8935d7..f8596e5 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -180,8 +180,8 @@ export async function verify (key: Key, signature: string, ...input: string[]): const { isVerified } = await NanoNaClWorker.request({ 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) {