]> git.codecow.com Git - libnemo.git/commitdiff
Add account to database after loading. next/account-db
authorChris Duncan <chris@zoso.dev>
Thu, 16 Oct 2025 03:12:09 +0000 (20:12 -0700)
committerChris Duncan <chris@zoso.dev>
Thu, 16 Oct 2025 03:12:09 +0000 (20:12 -0700)
src/lib/account/load.ts

index 1835f4d644cda7f7d794098348b92162e2f5ffe0..e9ec958923c44274ec5902bda0ab74fe4a6564ac 100644 (file)
@@ -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<ArrayBuffer> | KeyPair, type?: 'private'): { address: Address, publicKey: Uint8Array<ArrayBuffer>, 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<Account[]>} 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<ArrayBuffer>|KeyPair)[]} input - Public keys or addresses of the accounts
-* @returns {Account[]} The instantiated Account objects
-*/
 function fromPublic (input: string | Uint8Array<ArrayBuffer> | KeyPair | unknown) {
        try {
                const keypair = isKeyPair(input)