import * as bip39 from 'bip39' ; import { ONT_BIP44_PATH } from './consts' ; import { Address , PrivateKey } from './crypto' ; import { deserializeFromJson } from './crypto/PrivateKeyFactory' ; import { ERROR_CODE } from './error' ; import { ScryptParams } from './scrypt' ; import { Transaction } from './transaction/transaction' ; import { signTransaction } from './transaction/transactionBuilder' ; import { ab2hexstring , generateRandomArray , randomBytes } from './utils' ; const { HDKey } = require ( '@ont-dev/hdkey-secp256r1' ) ; export class Account { param label param encryptedPrivateKey param password param address param saltBase64 param params static importAccount ( label , encryptedPrivateKey , password , address , saltBase64 , params ? ) { const account = new Account ( ) ; const salt = Buffer . from ( saltBase64 , 'base64' ) . toString ( 'hex' ) ; const privateKey = encryptedPrivateKey . decrypt ( password , address , salt , params ) ; if ( ! label ) { label = ab2hexstring ( generateRandomArray ( 4 ) ) ; } account . label = label ; account . lock = false ; account . isDefault = false ; account . salt = saltBase64 ; account . encryptedKey = encryptedPrivateKey ; const publicKey = privateKey . getPublicKey ( ) ; account . publicKey = publicKey . key ; account . address = Address . fromPubKey ( publicKey ) ; return account ; } param label param mnemonic param password param params static importWithMnemonic ( label , mnemonic , password , params ? ) { mnemonic = mnemonic . trim ( ) ; if ( ! bip39 . validateMnemonic ( mnemonic ) ) { throw ERROR_CODE . INVALID_PARAMS ; } const seed = bip39 . mnemonicToSeedHex ( mnemonic ) ; const hdkey = HDKey . fromMasterSeed ( Buffer . from ( seed , 'hex' ) ) ; const pri = hdkey . derive ( ONT_BIP44_PATH ) ; const key = Buffer . from ( pri . privateKey ) . toString ( 'hex' ) ; const privateKey = new PrivateKey ( key ) ; const account = Account . create ( privateKey , password , label , params ) ; return account ; } param privateKey param password param label param params static create ( privateKey , password , label ? , params ? ) { const account = new Account ( ) ; if ( ! label ) { label = ab2hexstring ( generateRandomArray ( 4 ) ) ; } account . label = label ; account . lock = false ; account . isDefault = false ; const salt = randomBytes ( 16 ) ; const publicKey = privateKey . getPublicKey ( ) ; const address = Address . fromPubKey ( publicKey ) ; account . publicKey = publicKey . serializeHex ( ) ; account . address = address ; account . encryptedKey = privateKey . encrypt ( password , address , salt , params ) ; account . salt = Buffer . from ( salt , 'hex' ) . toString ( 'base64' ) ; return account ; } static parseJson ( json ) { return Account . parseJsonObj ( JSON . parse ( json ) ) ; } param obj static parseJsonObj ( obj ) { const account = new Account ( ) ; account . address = new Address ( obj . address ) ; account . label = obj . label ; account . lock = obj . lock ; account . isDefault = obj . isDefault ; account . publicKey = obj . publicKey ; account . hash = obj . hash ; account . salt = obj . salt ; account . encryptedKey = deserializeFromJson ( { algorithm : obj . algorithm , parameters : obj . parameters , key : obj . key , external : obj . external } ) ; account . extra = obj . extra ; return account ; } address : Address ; label : string ; lock : boolean ; encryptedKey : PrivateKey ; extra : null ; 'enc-alg' : string = 'aes-256-gcm' ; hash : string = 'sha256' ; salt : string ; publicKey : string ; isDefault : boolean ; toJson ( ) { return JSON . stringify ( this . toJsonObj ( ) ) ; } /**
     * Serializes to JSON object.
     *
     * Returned object will not be stringified.
     *
     */ toJsonObj ( ) { const obj = { 'address' : this . address . toBase58 ( ) , 'label' : this . label , 'lock' : this . lock , ... this . encryptedKey . serializeJson ( ) , 'enc-alg' : this [ 'enc-alg' ] , 'hash' : this . hash , 'salt' : this . salt , 'isDefault' : this . isDefault , 'publicKey' : this . publicKey , 'signatureScheme' : this . encryptedKey . algorithm . defaultSchema . label } ; return obj ; } exportPrivateKey ( password , params ? ) { return this . encryptedKey . decrypt ( password , this . address , this . salt , params ) ; } signTransaction ( password , tx , params ? ) { const pri = this . exportPrivateKey ( password , params ) ; signTransaction ( tx , pri , pri . algorithm . defaultSchema ) ; return tx ; } }