static async fromEntropy (passkey: string | Uint8Array, entropy: string, salt: string = ''): Promise<Bip44Wallet> {\r
let wallet: Bip44Wallet\r
try {\r
- const id = await Entropy.create(16)\r
+ const id = await Entropy.create()\r
const e = await Entropy.import(entropy)\r
const m = await Bip39Mnemonic.fromEntropy(e.hex)\r
const s = await m.toBip39Seed(salt)\r
static async fromMnemonic (passkey: string | Uint8Array, mnemonic: string, salt: string = ''): Promise<Bip44Wallet> {\r
let wallet: Bip44Wallet\r
try {\r
- const id = await Entropy.create(16)\r
+ const id = await Entropy.create()\r
const m = await Bip39Mnemonic.fromPhrase(mnemonic)\r
const s = await m.toBip39Seed(salt)\r
Bip44Wallet.#isInternal = true\r
if (!/^[0-9a-fA-F]+$/i.test(seed)) {\r
throw new Error('Seed contains invalid hexadecimal characters.')\r
}\r
- const id = await Entropy.create(16)\r
+ const id = await Entropy.create()\r
Bip44Wallet.#isInternal = true\r
const wallet = new this(id, seed)\r
try {\r
if (!/^[0-9a-fA-F]+$/i.test(seed)) {\r
throw new Error('Seed contains invalid hexadecimal characters.')\r
}\r
- const id = await Entropy.create(16)\r
+ const id = await Entropy.create()\r
const s = seed\r
const m = await Bip39Mnemonic.fromEntropy(seed)\r
Blake2bWallet.#isInternal = true\r
static async fromMnemonic (passkey: string | Uint8Array, mnemonic: string): Promise<Blake2bWallet> {\r
let wallet: Blake2bWallet\r
try {\r
- const id = await Entropy.create(16)\r
+ const id = await Entropy.create()\r
const m = await Bip39Mnemonic.fromPhrase(mnemonic)\r
const s = await m.toBlake2bSeed()\r
Blake2bWallet.#isInternal = true\r
}\r
\r
/**\r
- * Locks the wallet with a password that will be needed to unlock it later.\r
+ * Locks the wallet and all currently derived accounts with a password that\r
+ * will be needed to unlock it later.\r
*\r
* @param {(string|Uint8Array)} password Used to lock the wallet\r
* @returns True if successfully locked\r
data.seed = this.#s\r
}\r
const response = (await Wallet.#poolSafe.assign({\r
- method: 'put',\r
+ method: 'set',\r
name: this.id,\r
password,\r
data\r
}))[0]\r
- const success = response.result\r
+ const success = response?.result\r
if (!success) {\r
throw null\r
}\r
} finally {\r
password.fill(0)\r
}\r
- this.#locked = true\r
this.#m = null\r
this.#s = null\r
+ this.#locked = true\r
return true\r
}\r
\r
name: this.id,\r
password\r
}))[0]\r
- const { id, mnemonic, seed } = response.result\r
- if (id !== this.id) {\r
+ const { id, mnemonic, seed } = response?.result\r
+ if (id == null || id !== this.id) {\r
throw null\r
}\r
if (mnemonic != null) {\r
promises.push(account.unlock(password))\r
}\r
await Promise.all(promises)\r
- this.#locked = false\r
} catch (err) {\r
throw new Error('Failed to unlock wallet')\r
} finally {\r
password.fill(0)\r
}\r
+ this.#locked = false\r
return true\r
}\r
\r
let result
try {
const passwordBytes = obj.toBytes(password ?? [])
- switch (d.method) {
- case 'put': {
- result = await this.put(name, passwordBytes, data)
- break
- }
- case 'overwrite': {
- result = await this.overwrite(name, passwordBytes, data)
+ switch (method) {
+ case 'set': {
+ result = await this.set(name, passwordBytes, data)
break
}
case 'get': {
/**
* Encrypts data with a password byte array and stores it in the Safe.
*/
- static async put (name: string, password: Uint8Array, data: any): Promise<boolean> {
+ static async set (name: string, password: Uint8Array, data: any): Promise<boolean> {
if (await this.#exists(name)) {
password.fill(0)
throw new Error(this.ERR_MSG)
}
- return this.overwrite(name, password, data)
- }
-
- /**
- * Encrypts data with a password byte array and stores it in the Safe.
- */
- static async overwrite (name: string, password: Uint8Array, data: any): Promise<boolean> {
let passkey: CryptoKey
try {
passkey = await globalThis.crypto.subtle.importKey('raw', password, 'PBKDF2', false, ['deriveBits', 'deriveKey'])
encrypted: bytes.toHex(new Uint8Array(encrypted)),
iv: iv.hex
}
- return await this.#put(record, name)
+ return await this.#add(record, name)
} catch (err) {
throw new Error(this.ERR_MSG)
}
})
}
- static async #put (record: SafeRecord, name: string): Promise<boolean> {
- const result = await this.#transact<typeof name>('readwrite', db => db.put(record, name))
+ static async #add (record: SafeRecord, name: string): Promise<boolean> {
+ const result = await this.#transact<typeof name>('readwrite', db => db.add(record, name))
return await this.#exists(result)
}