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
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
* @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
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
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
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) {
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
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
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) {
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')
method: 'fetch',
name,
store: 'Rolodex',
- password: utf8.toBytes('').buffer
+ password: utf8.toBuffer('')
})
const addresses = response[name]
return addresses
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) {
method: 'fetch',
name: address,
store: 'Rolodex',
- password: utf8.toBytes('').buffer
+ password: utf8.toBuffer('')
})
const name = response[address]
return name
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) {