import { cryptography } from '../../src' ; const { getAddressFromPublicKey , getLisk32AddressFromPublicKey , getPrivateAndPublicKeyFromPassphrase , getKeys , getAddressAndPublicKeyFromPassphrase , getAddressFromPassphrase , getAddressFromPrivateKey , validateLisk32Address , getAddressFromLisk32Address , getLisk32AddressFromAddress , } = cryptography ; describe ( 'keys' , ( ) => { const defaultPassphrase = 'secret' ; const defaultPrivateKey = Buffer . from ( '2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b5d036a858ce89f844491762eb89e2bfbd50a4a0a0da658e4b2628b25b117ae09' , 'hex' ) ; const defaultPublicKey = Buffer . from ( '5d036a858ce89f844491762eb89e2bfbd50a4a0a0da658e4b2628b25b117ae09' , 'hex' ) ; const defaultAddress = Buffer . from ( '3a971fd02b4a07fc20aad1936d3cb1d263b96e0f' , 'hex' ) ; const defaultAddressAndPublicKey = { publicKey : defaultPublicKey , address : defaultAddress , } ; describe ( '#getPrivateAndPublicKeyFromPassphrase' , ( ) => { let keyPair ; beforeEach ( ( ) => { keyPair = getPrivateAndPublicKeyFromPassphrase ( defaultPassphrase ) ; } ) ; it ( 'should generate the correct publicKey from a passphrase' , ( ) => { expect ( keyPair ) . toHaveProperty ( 'publicKey' , defaultPublicKey ) ; } ) ; it ( 'should generate the correct privateKey from a passphrase' , ( ) => { expect ( keyPair ) . toHaveProperty ( 'privateKey' , defaultPrivateKey ) ; } ) ; } ) ; describe ( '#getKeys' , ( ) => { let keyPair ; beforeEach ( ( ) => { keyPair = getKeys ( defaultPassphrase ) ; } ) ; it ( 'should generate the correct publicKey from a passphrase' , ( ) => { expect ( keyPair ) . toHaveProperty ( 'publicKey' , defaultPublicKey ) ; } ) ; it ( 'should generate the correct privateKey from a passphrase' , ( ) => { expect ( keyPair ) . toHaveProperty ( 'privateKey' , defaultPrivateKey ) ; } ) ; } ) ; describe ( '#getAddressAndPublicKeyFromPassphrase' , ( ) => { it ( 'should create correct address and publicKey' , ( ) => { expect ( getAddressAndPublicKeyFromPassphrase ( defaultPassphrase ) ) . toEqual ( defaultAddressAndPublicKey ) ; } ) ; } ) ; describe ( '#getAddressFromPassphrase' , ( ) => { it ( 'should create correct address' , ( ) => { expect ( getAddressFromPassphrase ( defaultPassphrase ) ) . toEqual ( defaultAddress ) ; } ) ; } ) ; describe ( '#getAddressFromPrivateKey' , ( ) => { it ( 'should create correct address' , ( ) => { expect ( getAddressFromPrivateKey ( defaultPrivateKey . slice ( 0 , 64 ) ) ) . toEqual ( defaultAddress ) ; } ) ; } ) ; describe ( '#getAddressFromPublicKey' , ( ) => { it ( 'should generate address from publicKey' , ( ) => { const address = getAddressFromPublicKey ( defaultPublicKey ) ; expect ( address ) . toEqual ( defaultAddress ) ; } ) ; } ) ; describe ( '#getLisk32AddressFromPublicKey' , ( ) => { const publicKey = Buffer . from ( '0eb0a6d7b862dc35c856c02c47fde3b4f60f2f3571a888b9a8ca7540c6793243' , 'hex' ) ; const expectedLisk32Address = 'lsk24cd35u4jdq8szo3pnsqe5dsxwrnazyqqqg5eu' ; it ( 'should generate base32 address from publicKey' , ( ) => { const address = getLisk32AddressFromPublicKey ( publicKey , 'lsk' ) ; expect ( address ) . toBe ( expectedLisk32Address ) ; } ) ; } ) ; describe ( '#validateLisk32Address' , ( ) => { describe ( 'Given valid addresses' , ( ) => { const addresses = [ 'lsk24cd35u4jdq8szo3pnsqe5dsxwrnazyqqqg5eu' , 'lskoaknq582o6fw7sp82bm2hnj7pzp47mpmbmux2g' , 'lskqf5xbhu874yqg89k449zk2fctj46fona9bafgr' , 'lskamc9kfzenupkgexyxsf4qz9fv8mo9432of9p5j' , 'lsk6xevdsz3dpqfsx2u6mg3jx9zk8xqdozvn7x5ur' , ] ; it ( 'should return true' , ( ) => { return addresses . forEach ( address => { return expect ( validateLisk32Address ( address ) ) . toBeTruthy ( ) ; } ) ; } ) ; } ) ; describe ( 'Given an address that is too short' , ( ) => { const address = 'lsk1' ; it ( 'should throw an error' , ( ) => { return expect ( validateLisk32Address . bind ( null , address ) ) . toThrow ( 'Address length does not match requirements. Expected 41 characters.' ) ; } ) ; } ) ; describe ( 'Given an address that is too long' , ( ) => { const address = 'lskoaknq582o6fw7sp82bm2hnj7pzp47mpmbmux2ga' ; it ( 'should throw an error' , ( ) => { return expect ( validateLisk32Address . bind ( null , address ) ) . toThrow ( 'Address length does not match requirements. Expected 41 characters.' ) ; } ) ; } ) ; describe ( 'Given an address that is not prefixed with `lsk`' , ( ) => { const address = 'LSK24cd35u4jdq8szo3pnsqe5dsxwrnazyqqqg5eu' ; it ( 'should throw an error' , ( ) => { return expect ( validateLisk32Address . bind ( null , address ) ) . toThrow ( 'Invalid address prefix. Actual prefix: LSK, Expected prefix: lsk' ) ; } ) ; } ) ; describe ( 'Given an address containing non-base32 characters' , ( ) => { const address = 'lsk1aknq582o6fw7sp82bm2hnj7pzp47mpmbmux2g' ; it ( 'should throw an error' , ( ) => { return expect ( validateLisk32Address . bind ( null , address ) ) . toThrow ( "Invalid character found in address. Only allow characters: 'abcdefghjkmnopqrstuvwxyz23456789'." ) ; } ) ; } ) ; describe ( 'Given an address with invalid checksum' , ( ) => { const address = 'lskoaknq582o6fw7sp82bm2hnj7pzp47mpmbmuxgg' ; it ( 'should throw an error' , ( ) => { return expect ( validateLisk32Address . bind ( null , address ) ) . toThrow ( 'Invalid checksum for address.' ) ; } ) ; } ) ; } ) ; describe ( 'getAddressFromLisk32Address' , ( ) => { const account = { passphrase : 'boil typical oyster traffic ethics timber envelope undo lecture poverty space keep' , privateKey : 'b02cd384aae38021bc025522aca7a015b64ff1ac2e47426b5bc749951273fdbc81d0db4fba38f3ce334e6c0f3192dd62366f43daa46a94bfc55ebf6205ea2453' , publicKey : '81d0db4fba38f3ce334e6c0f3192dd62366f43daa46a94bfc55ebf6205ea2453' , binaryAddress : '4762070a641cf689f765d43ad792e1970e6bb863' , address : 'lsk3hyz7vtpcts3thsmduh98pwxrjnbw7ccoxchxu' , } ; it ( 'should throw error for invalid address' , ( ) => { return expect ( getAddressFromLisk32Address . bind ( null , 'invalid' ) ) . toThrow ( ) ; } ) ; it ( 'should return an address given a base32 address' , ( ) => { expect ( getAddressFromLisk32Address ( account . address ) . toString ( 'hex' ) ) . toBe ( account . binaryAddress ) ; } ) ; } ) ; describe ( 'getLisk32AddressFromAddress' , ( ) => { const account = { passphrase : 'boil typical oyster traffic ethics timber envelope undo lecture poverty space keep' , privateKey : 'b02cd384aae38021bc025522aca7a015b64ff1ac2e47426b5bc749951273fdbc81d0db4fba38f3ce334e6c0f3192dd62366f43daa46a94bfc55ebf6205ea2453' , publicKey : '81d0db4fba38f3ce334e6c0f3192dd62366f43daa46a94bfc55ebf6205ea2453' , binaryAddress : '4762070a641cf689f765d43ad792e1970e6bb863' , address : 'lsk3hyz7vtpcts3thsmduh98pwxrjnbw7ccoxchxu' , } ; it ( 'should return base32 address given an address' , ( ) => { expect ( getLisk32AddressFromAddress ( Buffer . from ( account . binaryAddress , 'hex' ) ) ) . toBe ( account . address ) ; } ) ; } ) ; } ) ;