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
}
}
-/**
-* 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)