}\r
}\r
\r
- /**\r
- * USING THIS METHOD IS DISCOURAGED. This library works in its entirety without\r
- * exposing the private keys of accounts.\r
- *\r
- * Retrieves and decrypts the private key of the Account. The same password\r
- * used to lock it must be used to unlock it. If derived from a wallet, the\r
- * password for the account is the wallet seed.\r
- *\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
- /**\r
- * USING THIS METHOD IS DISCOURAGED. This library works in its entirety without\r
- * exposing the private keys of accounts.\r
- *\r
- * Retrieves and decrypts the private key of the Account. The same password\r
- * used to lock it must be used to unlock it. If derived from a wallet, the\r
- * password for the account is the wallet seed.\r
- *\r
- * @param {Key} password Used previously to lock the Account\r
- * @returns Private key bytes as a hexadecimal string\r
- */\r
- async export (password: Key, format: 'hex'): Promise<string>\r
- async export (password: Key, format?: 'hex'): Promise<Key> {\r
- if (typeof password === 'string') password = utf8.toBytes(password)\r
- try {\r
- const privateKey = new Uint8Array(await this.#export(password))\r
- return format === 'hex'\r
- ? bytes.toHex(privateKey)\r
- : privateKey\r
- } catch (err) {\r
- console.log(err)\r
- throw new Error('Failed to export Account private key')\r
- } finally {\r
- bytes.erase(password)\r
- }\r
- }\r
-\r
/**\r
* Refreshes the account from its current state on the network.\r
*\r
* @param {Key} password - Required to decrypt the private key for signing\r
* @returns {Promise<string>} Hexadecimal-formatted 64-byte signature\r
*/\r
- async sign (block: ChangeBlock | ReceiveBlock | SendBlock, password: Key): Promise<string> {\r
- if (typeof password === 'string') password = utf8.toBytes(password)\r
+ async sign (block: ChangeBlock | ReceiveBlock | SendBlock, password: string): Promise<string> {\r
try {\r
const { signature } = await NanoNaClWorker.request<ArrayBuffer>({\r
method: 'detached',\r
- privateKey: await this.#export(password),\r
+ privateKey: await this.#getPrivateKey(password),\r
msg: hex.toBuffer(block.hash)\r
})\r
block.signature = bytes.toHex(new Uint8Array(signature))\r
return block.signature\r
} catch (err) {\r
throw new Error(`Failed to sign block`, { cause: err })\r
- } finally {\r
- bytes.erase(password)\r
}\r
}\r
\r
* Retrieves and decrypts the private key of the Account. The same password\r
* used to lock it must be used to unlock it.\r
*\r
- * @param {Key} password Used previously to lock the Account\r
- * @returns Private key bytes as a Uint8Array\r
+ * @param {string} password Used previously to lock the Account\r
+ * @returns {Promise<ArrayBuffer>} Promise for buffer of private key\r
*/\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
- }\r
+ async #getPrivateKey (password: string): Promise<ArrayBuffer> {\r
try {\r
const response = await SafeWorker.request<ArrayBuffer>({\r
method: 'fetch',\r
names: this.publicKey,\r
store: 'Account',\r
- password: password.buffer\r
+ password: utf8.toBuffer(password)\r
})\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
- bytes.erase(password)\r
}\r
}\r
\r