import { Address , JsonKey , PrivateKey } from './crypto' ; import { deserializeFromJson } from './crypto/PrivateKeyFactory' ; import { ScryptParams } from './scrypt' ; import { Transaction } from './transaction/transaction' ; import { signTransaction } from './transaction/transactionBuilder' ; import { ab2hexstring , generateRandomArray , randomBytes } from './utils' ; /**
 * Control data of identity
 */ export class ControlData { static fromJson ( json ) { const privateKey = deserializeFromJson ( json as JsonKey ) ; const cd = new ControlData ( json . id , privateKey , new Address ( json . address ) , json . salt ) ; cd . publicKey = json . publicKey ; cd . hash = json . hash ; return cd ; } /**
     * Id of control data
     */ id : string ; /**
     * Encrypted private key
     */ encryptedKey : PrivateKey ; /**
     * Address of control data
     */ address : Address ; /**
     * Salt of control data
     */ salt : string ; /**
     * hash type
     */ hash : string = 'sha256' ; /**
     * The public key
     */ publicKey : string ; constructor ( id , encryptedKey , address , salt ) { this . id = id ; this . encryptedKey = encryptedKey ; this . address = address ; this . salt = salt ; } toJson ( ) { return { id : this . id , ... this . encryptedKey . serializeJson ( ) , address : this . address . toBase58 ( ) , salt : this . salt , [ 'enc-alg' ] : 'aes-256-gcm' , hash : this . hash , publicKey : this . publicKey } ; } } export class Identity { param label param encryptedPrivateKey param password param address param saltBase64 param params static importIdentity ( label , encryptedPrivateKey , password , address , saltBase64 , params ? ) { const identity = new Identity ( ) ; const salt = Buffer . from ( saltBase64 , 'base64' ) . toString ( 'hex' ) ; const privateKey = encryptedPrivateKey . decrypt ( password , address , salt , params ) ; if ( ! label ) { label = ab2hexstring ( generateRandomArray ( 4 ) ) ; } const publicKey = privateKey . getPublicKey ( ) ; identity . ontid = Address . generateOntid ( publicKey ) ; identity . label = label ; identity . lock = false ; identity . isDefault = false ; const control = new ControlData ( '1' , encryptedPrivateKey , Address . fromOntid ( identity . ontid ) , saltBase64 ) ; control . publicKey = publicKey . serializeHex ( ) ; identity . controls . push ( control ) ; return identity ; } param privateKey param keyphrase param label param params static create ( privateKey , keyphrase , label , params ? ) { const identity = new Identity ( ) ; identity . ontid = '' ; identity . label = label ; identity . lock = false ; identity . isDefault = false ; const publicKey = privateKey . getPublicKey ( ) ; identity . ontid = Address . generateOntid ( publicKey ) ; const address = Address . fromOntid ( identity . ontid ) ; const salt = randomBytes ( 16 ) ; const encryptedPrivateKey = privateKey . encrypt ( keyphrase , address , salt , params ) ; const saltBase64 = Buffer . from ( salt , 'hex' ) . toString ( 'base64' ) ; const control = new ControlData ( '1' , encryptedPrivateKey , address , saltBase64 ) ; control . publicKey = publicKey . serializeHex ( ) ; identity . controls . push ( control ) ; return identity ; } static parseJson ( json ) { return Identity . parseJsonObj ( JSON . parse ( json ) ) ; } param obj static parseJsonObj ( obj ) { const id = new Identity ( ) ; id . ontid = obj . ontid ; id . label = obj . label ; id . lock = obj . lock ; id . isDefault = obj . isDefault ; id . controls = ( obj . controls as any [ ] ) . map ( ( c ) => ControlData . fromJson ( c ) ) ; id . extra = obj . extra ; return id ; } ontid : string ; label : string ; lock : boolean ; isDefault : boolean ; controls : ControlData [ ] = [ ] ; extra : null ; addControl ( control ) { for ( const c of this . controls ) { if ( c . address . toBase58 ( ) === control . address . toBase58 ( ) ) { return ; } } control . id = ( this . controls . length + 1 ) . toString ( ) ; this . controls . push ( control ) ; } toJson ( ) { return JSON . stringify ( this . toJsonObj ( ) ) ; } /**
     * Serializes to JSON object.
     *
     * Returned object will not be stringified.
     *
     */ toJsonObj ( ) { const obj = { ontid : this . ontid , label : this . label , lock : this . lock , isDefault : this . isDefault , controls : this . controls . map ( ( c ) => c . toJson ( ) ) , extra : this . extra } ; return obj ; } exportPrivateKey ( password , params ? ) { const encryptedKey = this . controls [ 0 ] . encryptedKey ; const address = this . controls [ 0 ] . address ; const salt = this . controls [ 0 ] . salt ; return encryptedKey . decrypt ( password , address , salt , params ) ; } signTransaction ( password , tx , params ? ) { const pri = this . exportPrivateKey ( password , params ) ; signTransaction ( tx , pri , pri . algorithm . defaultSchema ) ; return tx ; } }