import BigNumber from 'bignumber.js' ; import AddressGenerator from './lib/address-generator' ; import AddressImporter , { Account , Wallet } from './lib/address-importer' ; import BlockSigner , { BlockData , ReceiveBlock , RepresentativeBlock , SendBlock , SignedBlock } from './lib/block-signer' ; import NanoAddress from './lib/nano-address' ; import NanoConverter from './lib/nano-converter' ; import Signer from './lib/signer' ; import Convert from './lib/util/convert' ; const nanoAddress = new NanoAddress ( ) ; const generator = new AddressGenerator ( ) ; const importer = new AddressImporter ( ) ; const signer = new Signer ( ) ; const wallet = { param { string } entropy param { string } seedPassword returns { Wallet } generate : ( entropy ? , seedPassword ? ) : Wallet => { return generator . generateWallet ( entropy , seedPassword ) ; } , param { string } seed returns { Wallet } generateLegacy : ( seed ? ) : Wallet => { return generator . generateLegacyWallet ( seed ) ; } , param { string } mnemonic param { string } seedPassword throws returns { Wallet } fromMnemonic : ( mnemonic , seedPassword ? ) : Wallet => { return importer . fromMnemonic ( mnemonic , seedPassword ) ; } , param { string } mnemonic throws returns { Wallet } fromLegacyMnemonic : ( mnemonic ) : Wallet => { return importer . fromLegacyMnemonic ( mnemonic ) ; } , param { string } seed returns { Wallet } fromSeed : ( seed ) : Wallet => { return importer . fromSeed ( seed ) ; } , param { string } seed returns { Wallet } fromLegacySeed : ( seed ) : Wallet => { return importer . fromLegacySeed ( seed ) ; } , param { string } seed param { number } from param { number } to returns { Account [ ] } accounts : ( seed , from , to ) : Account [ ] => { return importer . fromSeed ( seed , from , to ) . accounts ; } , param { string } seed param { number } from param { number } to returns { Account [ ] } legacyAccounts : ( seed , from , to ) : Account [ ] => { return importer . fromLegacySeed ( seed , from , to ) . accounts ; } , } ; const blockSigner = new BlockSigner ( ) ; const block = { param { SendBlock } data param { string } privateKey returns { SignedBlock } send : ( data , privateKey ) : SignedBlock => { return blockSigner . send ( data , privateKey ) ; } , param { ReceiveBlock } data param { string } privateKey returns { SignedBlock } receive : ( data , privateKey ) : SignedBlock => { return blockSigner . receive ( data , privateKey ) ; } , param { RepresentativeBlock } data param { string } privateKey returns { SignedBlock } representative : ( data , privateKey ) : SignedBlock => { const block = { ... data , fromAddress : data . address , amountRaw : '0' , toAddress : 'nano_1111111111111111111111111111111111111111111111111111hifc8npp' , } ; return blockSigner . send ( block , privateKey ) ; } , } ; const tools = { param { string | BigNumber } input param { string } inputUnit param { string } outputUnit returns { string } convert : ( input , inputUnit , outputUnit ) : string => { return NanoConverter . convert ( input , inputUnit , outputUnit ) ; } , param { string } privateKey param { ... string } input returns { string } sign : ( privateKey , ... input ) : string => { const data = input . map ( Convert . stringToHex ) ; return signer . sign ( privateKey , ... data ) ; } , param { string } publicKey param { string } signature param { ... string } input returns { boolean } verify : ( publicKey , signature , ... input ) : boolean => { const data = input . map ( Convert . stringToHex ) ; return signer . verify ( publicKey , signature , ... data ) ; } , param { string } publicKey param { BlockData } block returns { boolean } verifyBlock : ( publicKey , block ) : boolean => { const preamble = 0x6 . toString ( ) . padStart ( 64 , '0' ) ; return signer . verify ( publicKey , block . signature , preamble , nanoAddress . nanoAddressToHexString ( block . account ) , block . previous , nanoAddress . nanoAddressToHexString ( block . representative ) , Convert . dec2hex ( block . balance , 16 ) . toUpperCase ( ) , block . link ) ; } , param { string } input returns { boolean } validateAddress : ( input ) : boolean => { return nanoAddress . validateNanoAddress ( input ) ; } , param { string } input returns { boolean } validateMnemonic : ( input ) : boolean => { return importer . validateMnemonic ( input ) ; } , param { string } input returns { string } addressToPublicKey : ( input ) : string => { const cleaned = input . replace ( 'nano_' , '' ) . replace ( 'xrb_' , '' ) ; const publicKeyBytes = nanoAddress . decodeNanoBase32 ( cleaned ) ; return Convert . ab2hex ( publicKeyBytes ) . slice ( 0 , 64 ) ; } , param { string } input returns { string } publicKeyToAddress : ( input ) : string => { return nanoAddress . deriveAddress ( input ) ; } , param { string | string [ ] } input returns { string } blake2b : ( input ) : string => { if ( Array . isArray ( input ) ) { return Convert . ab2hex ( signer . generateHash ( input . map ( Convert . stringToHex ) ) ) ; } else { return Convert . ab2hex ( signer . generateHash ( [ Convert . stringToHex ( input ) ] ) ) ; } } , } ; export { wallet , block , tools , } ;