static import (keypairs: KeyPair[]): Account[]\r
/**\r
* Instantiates an Account object from its private key which is used to derive\r
- * a public key and then discarded. The public key is saved in the database.\r
+ * a public key and then discarded.\r
*\r
- * @param {KeyPair} keypair - Index and keys of the account\r
- * @param {Key} password - Used to encrypt the private key\r
+ * @param {KeyPair} keypair - Index and key of the account\r
+ * @param {string} type - Indicates a private key\r
* @returns {Promise<Account>} Promise for a new Account object\r
*/\r
- static async import (keypair: KeyPair, password: Key): Promise<Account>\r
+ static async import (keypair: KeyPair, type: 'private'): Promise<Account>\r
/**\r
* Instantiates Account objects from their private keys which are used to\r
- * derive public keys and then discarded. The public keys are saved in the\r
- * database.\r
+ * derive public keys and then discarded.\r
*\r
* @param {KeyPair[]} keypairs - Indexes and keys of the accounts\r
- * @param {Key} password - Used to encrypt the private keys\r
+ * @param {string} type - Indicates private keys\r
* @returns {Promise<Account[]>} Promise for array of new Account objects\r
*/\r
- static async import (keypairs: KeyPair[], password: Key): Promise<Account[]>\r
- static import (input: Key | KeyPair | (Key | KeyPair)[], password?: Key): Account | Account[] | Promise<Account | Account[]> {\r
+ static async import (keypairs: KeyPair[], type: 'private'): Promise<Account[]>\r
+ static import (input: Key | KeyPair | (Key | KeyPair)[], type?: 'private'): Account | Account[] | Promise<Account | Account[]> {\r
const isInputArray = Array.isArray(input)\r
const inputs = isInputArray ? input : [input]\r
- if (this.#isKeyPairs(inputs) && password != null) {\r
+ if (this.#isKeyPairs(inputs) && type === 'private') {\r
return new Promise((resolve, reject): void => {\r
- this.#fromPrivate(inputs, password)\r
+ this.#fromPrivate(inputs)\r
.then(r => resolve(isInputArray ? r : r[0]))\r
.catch(e => reject(e))\r
})\r
this.#weight = BigInt(weight)\r
}\r
\r
- /**\r
- * Signs a block using the private key of the account. The signature is\r
- * appended to the signature field of the block before being returned.\r
- *\r
- * @param {(ChangeBlock|ReceiveBlock|SendBlock)} block - The block data to be hashed and signed\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: string): Promise<string> {\r
- // try {\r
- // const signature = await NanoNaCl.detached(hex.toBytes(block.hash), new Uint8Array(await this.#getPrivateKey(password)))\r
- // block.signature = bytes.toHex(signature)\r
- // return block.signature\r
- // } catch (err) {\r
- // throw new Error(`Failed to sign block`, { cause: err })\r
- // }\r
- // }\r
-\r
/**\r
* Validates a Nano address with 'nano' and 'xrb' prefixes\r
* Derived from https://github.com/alecrios/nano-address-validator\r
}\r
\r
/**\r
- * Instantiates an Account object from its private key which is then encrypted\r
- * and stored in IndexedDB. The corresponding public key will automatically be\r
- * derived and saved.\r
+ * Instantiates an Account object from its private key which is used to derive\r
+ * the corresponding public key and then discarded.\r
*\r
* @param {KeyPair} keypairs - Indexes and keys of the accounts\r
- * @param {number} [index] - Account number used when deriving the key\r
* @returns {Account} A new Account object\r
*/\r
- static async #fromPrivate (keypairs: KeyPair[], password: Key): Promise<Account[]> {\r
- if (typeof password === 'string') password = utf8.toBytes(password)\r
- if (password == null || !(password instanceof Uint8Array)) {\r
- throw new Error('Invalid password when importing Account')\r
- }\r
+ static async #fromPrivate (keypairs: KeyPair[]): Promise<Account[]> {\r
+ try {\r
+ const accounts: Account[] = []\r
+ for (let keypair of keypairs) {\r
+ let { index, privateKey } = keypair\r
+ if (index == null) {\r
+ throw new RangeError('Index missing for Account')\r
+ }\r
+ this.#validateKey(privateKey)\r
+ if (typeof privateKey === 'string') privateKey = hex.toBytes(privateKey)\r
\r
- const accounts: Account[] = []\r
- const privateAccounts: NamedData = {\r
- method: 'store',\r
- store: 'Account',\r
- password: password.buffer\r
- }\r
- for (let keypair of keypairs) {\r
- let { index, privateKey } = keypair\r
- if (index == null) {\r
- throw new RangeError('Index missing for Account')\r
- }\r
- this.#validateKey(privateKey)\r
- if (typeof privateKey === 'string') privateKey = hex.toBytes(privateKey)\r
- try {\r
const publicKey = await NanoNaCl.convert(privateKey)\r
- privateAccounts[bytes.toHex(publicKey)] = privateKey.buffer\r
const address = this.#keyToAddress(publicKey)\r
this.#isInternal = true\r
accounts.push(new this(address, publicKey, index))\r
- } catch (err) {\r
- throw new Error(`Failed to derive public key from private key`, { cause: err })\r
}\r
- }\r
-\r
- try {\r
- // const { result } = await SafeWorker.request(privateAccounts)\r
- // if (!result) {\r
- // throw null\r
- // }\r
return accounts\r
} catch (err) {\r
- throw new Error(`Failed to lock Accounts`, { cause: err })\r
- } finally {\r
- bytes.erase(password)\r
+ throw new Error('Failed to import Accounts from private keys', { cause: err })\r
}\r
}\r
\r