import { connect } from "react-redux" ; import { accounts , hdpathPreview , hwkey , IState , screen , settings } from "@emeraldwallet/store" ; import * as React from "react" ; import { Dispatch } from "react" ; import { isLedger , isPk , isPkJson , isPkRaw , isSeedCreate , isSeedSelected , Result } from "./flow/types" ; import CreateWalletWizard from "./CreateWalletWizard" ; import * as vault from "@emeraldpay/emerald-vault-core" ; import { Pages } from "@emeraldwallet/store/lib/screen" ; import { BlockchainCode , blockchainCodeToId , Blockchains , IBlockchain } from "@emeraldwallet/core" ; import { AddEntry , IdSeedReference , LedgerSeedReference , SeedDefinition , SeedEntry , SeedReference , Uuid } from "@emeraldpay/emerald-vault-core" ; type Props = { seeds : vault . SeedDescription [ ] ; blockchains : IBlockchain [ ] ; } ; type Actions = { onCreate : ( value : Result ) => Promise < string > ; onError : ( err : any ) => void ; onCancel : ( ) => void ; mnemonicGenerator ? : ( ) => Promise < string > ; onSaveSeed : ( seed : SeedDefinition ) => Promise < string > ; } ; see CreateWalletWizard const Component = ( ( props ) => { return < CreateWalletWizard { ... props } / > ; } ) ; type OwnProps = { } ; function entriesForBlockchains ( seedRef , account , blockchains , addresses ) { const entries = [ ] ; blockchains . forEach ( ( blockchain ) => { const key = { hdPath : Blockchains [ blockchain ] . params . hdPath . forAccount ( account ) . toString ( ) , seed : seedRef , address : addresses [ blockchain ] } ; entries . push ( { type : "hd-path" , blockchain : blockchainCodeToId ( blockchain ) , key } ) ; } ) ; return entries ; } export default connect ( ( state , ownProps ) : Props => { return { seeds : accounts . selectors . getSeeds ( state ) , blockchains : settings . selectors . currentChains ( state ) } ; } , ( dispatch , ownProps ) : Actions => { return { onError : screen . actions . catchError ( dispatch ) , onCreate : ( value ) => { return new Promise ( ( resolve , reject ) => { const handler = ( walletId ? , err ? ) => { if ( err || typeof walletId == 'undefined' ) { reject ( err ) ; } else { dispatch ( accounts . actions . subscribeWalletBalance ( walletId ) ) ; resolve ( walletId ) ; } } ; const opts = { label : value . options . label } ; const entries = [ ] ; const type = value . type ; if ( isSeedSelected ( type ) || isSeedCreate ( type ) ) { const seed = value . seed ; if ( typeof seed == "object" && seed . type == "id" && seed . password && typeof value . seedAccount == 'number' ) { const account = value . seedAccount ; entriesForBlockchains ( value . seed ! , account , value . blockchains , value . addresses || { } ) . forEach ( ( e ) => entries . push ( e ) ) ; } else { console . warn ( "Account number is not set" ) ; } } else if ( isPk ( type ) ) { if ( isPkJson ( type ) ) { entries . push ( { type : "ethereum-json" , key : type . json , blockchain : blockchainCodeToId ( value . blockchains [ 0 ] ) } ) ; } else if ( isPkRaw ( type ) ) { entries . push ( { type : "raw-pk-hex" , key : type . pk , password : type . password , blockchain : blockchainCodeToId ( value . blockchains [ 0 ] ) } ) ; } } else if ( isLedger ( type ) ) { if ( typeof value . seedAccount == 'number' ) { const account = value . seedAccount ; const seedRef = { type : "ledger" } ; entriesForBlockchains ( seedRef , account , value . blockchains , value . addresses || { } ) . forEach ( ( e ) => entries . push ( e ) ) ; } else { console . warn ( "Account number is not set" ) ; } } dispatch ( accounts . actions . createWallet ( opts , entries , handler ) ) ; dispatch ( hwkey . actions . setWatch ( false ) ) ; dispatch ( hdpathPreview . actions . clean ( ) ) ; } ) ; } , mnemonicGenerator : ( ) => { return new Promise ( ( resolve ) => dispatch ( accounts . actions . generateMnemonic ( resolve ) ) ) ; } , onSaveSeed : ( seed ) => { return new Promise < string > ( ( resolve ) => { const id = dispatch ( accounts . actions . createSeed ( seed , resolve ) ) ; dispatch ( accounts . actions . loadSeedsAction ( ) ) ; return id ; } ) ; } , onCancel : ( ) => { dispatch ( screen . actions . gotoScreen ( Pages . HOME ) ) ; dispatch ( hwkey . actions . setWatch ( false ) ) ; dispatch ( hdpathPreview . actions . clean ( ) ) ; } } ; } ) ( ( Component ) ) ;