}
if (typeof value === 'string') {
if (RegExp(`^[A-F0-9]{${ACCOUNT_KEY_HEX_LENGTH}}$`, 'i').test(value)) {
- this.#address = Address.fromPublicKey(hex.toBuffer(value))
+ this.#address = (this.constructor as typeof Address).fromPublicKey(hex.toBuffer(value))
} else if (RegExp(`^(${PREFIX}|${PREFIX_LEGACY})[13][${ALPHABET}]{59}$`).test(value)) {
this.#address = value
} else {
}
} else if (value instanceof ArrayBuffer || value instanceof Uint8Array) {
const v = new Uint8Array(value)
- this.#address = Address.fromPublicKey(v)
+ this.#address = (this.constructor as typeof Address).fromPublicKey(v)
} else {
throw new TypeError('Invalid address input', { cause: value })
}
id: name,
addresses
}
- const results = await Database.put(data, Rolodex.DB_NAME)
+ const results = await Database.put(data, this.DB_NAME)
if (results.length !== Object.keys(data).length) {
throw new Error('Unexpected results from adding address', { cause: results })
}
addresses
}
}
- const isUpdated = await Database.put(data, Rolodex.DB_NAME)
+ const isUpdated = await Database.put(data, this.DB_NAME)
if (!isUpdated) {
throw new Error('failed to remove address from existing name')
}
- const isDeleted = await Database.delete(address, Rolodex.DB_NAME)
+ const isDeleted = await Database.delete(address, this.DB_NAME)
return isDeleted
}
static async deleteName (name: string): Promise<boolean> {
const records = await this.getAddresses(name)
records.push(name)
- return await Database.delete(records, Rolodex.DB_NAME)
+ return await Database.delete(records, this.DB_NAME)
}
/**
*/
static async getAddresses (name: string): Promise<string[]> {
try {
- const records = await Database.get<NamedData<string[]>>(name, Rolodex.DB_NAME)
+ const records = await Database.get<NamedData<string[]>>(name, this.DB_NAME)
const record = records[name]
return record?.addresses
? record.addresses.sort()
*/
static async getAllNames (): Promise<string[]> {
try {
- const records = await Database.getAll<NamedData>(Rolodex.DB_NAME)
+ const records = await Database.getAll<NamedData>(this.DB_NAME)
return Object.keys(records).filter(v => v.slice(0, 5) !== 'nano_')
} catch (err) {
console.error(err)
*/
static async getName (address: string): Promise<string | null> {
try {
- const records = await Database.get<NamedData<string>>(address, Rolodex.DB_NAME)
+ const records = await Database.get<NamedData<string>>(address, this.DB_NAME)
const record = records[address]
return record?.name ?? null
} catch (err) {
* three types of wallets are supported: BIP-44, BLAKE2b, and Ledger.\r
*/\r
export class Wallet {\r
+ static #isInternal: boolean = false\r
+\r
+ /**\r
+ * @returns {boolean}\r
+ */\r
+ static get isInternal (): boolean { return this.#isInternal }\r
/**\r
* @returns {'Wallet'}\r
*/\r
*/\r
static async create (type: 'BIP-44' | 'BLAKE2b', password: string, mnemonicSalt?: string): Promise<Wallet>\r
static async create (type: WalletType, password?: string, mnemonicSalt?: string): Promise<Wallet> {\r
- Wallet.#isInternal = true\r
+ this.#isInternal = true\r
const self = new this(type)\r
+ this.#isInternal = false\r
{ ({ mnemonic: self.#mnemonic, seed: self.#seed } = await _create(self, self.#vault, password, mnemonicSalt)) }\r
return self\r
}\r
*/\r
static async load (type: 'BIP-44' | 'BLAKE2b', password: string, mnemonicPhrase: string, mnemonicSalt?: string): Promise<Wallet>\r
static async load (type: WalletType, password: string, secret: string, mnemonicSalt?: string): Promise<Wallet> {\r
- Wallet.#isInternal = true\r
+ this.#isInternal = true\r
const self = new this(type)\r
+ this.#isInternal = false\r
await _load(self, self.#vault, password, secret, mnemonicSalt)\r
return self\r
}\r
static async restore (id?: string): Promise<Wallet | Wallet[]> {\r
const backups = await _restore(id)\r
const wallets = backups.map(backup => {\r
- Wallet.#isInternal = true\r
- return new this(backup.type, backup.id)\r
+ this.#isInternal = true\r
+ const self = new this(backup.type, backup.id)\r
+ this.#isInternal = false\r
+ return self\r
})\r
return typeof id === 'string' ? wallets[0] : wallets\r
}\r
\r
constructor (type: WalletType, id?: string)\r
constructor (type: unknown, id?: string) {\r
- if (!Wallet.#isInternal) {\r
+ if (!(this.constructor as typeof Wallet).isInternal) {\r
throw new Error(`Wallet cannot be instantiated directly. Use 'await Wallet.create()' instead.`)\r
}\r
if (type !== 'BIP-44' && type !== 'BLAKE2b' && type !== 'Ledger') {\r
throw new TypeError('Invalid wallet type', { cause: type })\r
}\r
- Wallet.#isInternal = false\r
this.#accounts = new Map<number, Account>()\r
this.#id = id ?? crypto.randomUUID()\r
this.#type = type\r
return await _verify(this.type, this.#vault, secret)\r
}\r
\r
- static #isInternal: boolean = false\r
#accounts: Map<number, Account>\r
#id: string\r
#mnemonic?: ArrayBuffer\r