From: Chris Duncan Date: Thu, 16 Oct 2025 03:12:09 +0000 (-0700) Subject: Add account to database after loading. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=eedf94882fe2f34a92d62333add5959b22876503;p=libnemo.git Add account to database after loading. --- diff --git a/src/lib/account/load.ts b/src/lib/account/load.ts index 1835f4d..e9ec958 100644 --- a/src/lib/account/load.ts +++ b/src/lib/account/load.ts @@ -3,25 +3,24 @@ import { Account, KeyPair } from '../account' import { ACCOUNT_KEY_BYTE_LENGTH, ACCOUNT_KEY_HEX_LENGTH } from '../constants' -import { hex } from '../convert' +import { bytes, hex } from '../convert' import { NanoNaCl } from '../crypto' +import { Database } from '../database' import { Address } from './address' export function _load (input: string | Uint8Array | KeyPair, type?: 'private'): { address: Address, publicKey: Uint8Array, index?: number } { - if (isKeyPair(input) && type === 'private') { - return fromPrivate(input) - } else { - return fromPublic(input) + const account = (isKeyPair(input) && type === 'private') + ? fromPrivate(input) + : fromPublic(input) + const data: { address: string, publicKey: string, index?: number } = { + address: account.address.toString(), + publicKey: bytes.toHex(account.publicKey) } + if (account.index != null) data.index = account.index + Database.add({ [account.address.toString()]: data }, Account.DB_NAME) + return account } -/** -* Instantiates an Account object from its private key which is used to derive -* the corresponding public key and then discarded. -* -* @param {KeyPair} keypairs - Indexes and keys of the accounts -* @returns {Promise} Promise for new Account objects -*/ function fromPrivate (keypair: KeyPair) { try { let { index, privateKey } = keypair @@ -45,13 +44,6 @@ function fromPrivate (keypair: KeyPair) { } } -/** -* Instantiates Account objects from public data, each specifying either its -* public key or its Nano address. -* -* @param {(string | Uint8Array|KeyPair)[]} input - Public keys or addresses of the accounts -* @returns {Account[]} The instantiated Account objects -*/ function fromPublic (input: string | Uint8Array | KeyPair | unknown) { try { const keypair = isKeyPair(input)