import { expect } from "chai" ; import { Bech32 } from "../lib/Bech32" ; describe ( "Bech32" , ( ) => { describe ( ".wordsToBuffer()" , ( ) => { describe ( "without common denominator" , ( ) => { const words = [ 0 , 1 , 2 , 3 ] ; it ( "pad=true, includes overflowed words" , ( ) => { const result = Bech32 . wordsToBuffer ( words , true ) ; expect ( result . toString ( "hex" ) ) . to . equal ( "004430" ) ; } ) ; it ( "pad=false, throws" , ( ) => { expect ( ( ) => Bech32 . wordsToBuffer ( words , false ) ) . to . throw ( "Invalid bech32 encoding" ) ; } ) ; } ) ; describe ( "with common denominator" , ( ) => { const words = [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ; it ( "pad=true" , ( ) => { const result = Bech32 . wordsToBuffer ( words , true ) ; expect ( result . toString ( "hex" ) ) . to . equal ( "00443214c7" ) ; } ) ; it ( "pad=false" , ( ) => { const result = Bech32 . wordsToBuffer ( words , false ) ; expect ( result . toString ( "hex" ) ) . to . equal ( "00443214c7" ) ; } ) ; } ) ; } ) ; describe ( ".bufferToWords()" , ( ) => { describe ( "without common denominator" , ( ) => { const buffer = Buffer . from ( "004430" , "hex" ) ; it ( "pad=true, includes additional zero word" , ( ) => { const result = Bech32 . bufferToWords ( buffer , true ) ; expect ( result ) . to . deep . equal ( [ 0 , 1 , 2 , 3 , 0 ] ) ; } ) ; it ( "pad=false, excludes additional zero word" , ( ) => { const result = Bech32 . bufferToWords ( buffer , false ) ; expect ( result ) . to . deep . equal ( [ 0 , 1 , 2 , 3 ] ) ; } ) ; } ) ; } ) ; describe ( ".sizeOfNum()" , ( ) => { const tests = [ [ 0 , 1 ] , [ 1 , 1 ] , [ 31 , 1 ] , [ 32 , 2 ] , [ 1023 , 2 ] , [ 1024 , 3 ] , [ 524288 , 4 ] , [ 16777216 , 5 ] , [ 536870912 , 6 ] , ] ; for ( const [ input , output ] of tests ) { it ( `${ input } => ${ output }` , ( ) => { expect ( Bech32 . sizeofNum ( input ) ) . to . equal ( output ) ; } ) ; } } ) ; describe ( ".sizeOfBits()" , ( ) => { const tests = [ [ 1 , 1 ] , [ 5 , 1 ] , [ 6 , 2 ] , [ 11 , 3 ] , [ 16 , 4 ] , ] ; for ( const [ input , output ] of tests ) { it ( `${ input } => ${ output }` , ( ) => { expect ( Bech32 . sizeofBits ( input ) ) . to . equal ( output ) ; } ) ; } } ) ; describe ( ".sizeOfBytes" , ( ) => { const tests = [ [ 1 , 2 ] , [ 2 , 4 ] , [ 3 , 5 ] , [ 4 , 7 ] , [ 5 , 8 ] , ] ; for ( const [ input , output ] of tests ) { it ( `${ input } => ${ output }` , ( ) => { expect ( Bech32 . sizeofBytes ( input ) ) . to . equal ( output ) ; } ) ; } } ) ; describe ( "BIP173 Vectors" , ( ) => { const valid = [ "A12UEL5L" , "a12uel5l" , "an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs" , "abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw" , "11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j" , "split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w" , "?1ezyfcl" , ] ; for ( const test of valid ) { it ( "valid " + test , ( ) => { const decoded = Bech32 . decode ( test ) ; const encoded = Bech32 . encode ( decoded . hrp , decoded . words ) ; expect ( encoded ) . to . equal ( test . toLowerCase ( ) ) ; } ) ; } const invalid = [ "\x201nwldj5" , "\x7f1axkwrx" , "\x801eym55h" , "pzry9x0s0muk" , "1pzry9x0s0muk" , "x1b4n0q5v" , "li1dgmt3" , "de1lg7wt" + 0xff , "A1G7SGD8" , "10a06t8" , "1qzzfhee" , "bc1Atw" , ] ; for ( const test of invalid ) { it ( "invalid " + test , ( ) => { expect ( ( ) => Bech32 . decode ( test ) ) . to . throw ( ) ; } ) ; } } ) ; describe ( "BIP350 Vectors" , ( ) => { const valid = [ "A1LQFN3A" , "a1lqfn3a" , "an83characterlonghumanreadablepartthatcontainsthetheexcludedcharactersbioandnumber11sg7hg6" , "abcdef1l7aum6echk45nj3s0wdvt2fg8x9yrzpqzd3ryx" , "11llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllludsr8" , "split1checkupstagehandshakeupstreamerranterredcaperredlc445v" , "?1v759aa" , ] ; for ( const test of valid ) { it ( "valid " + test , ( ) => { const decoded = Bech32 . decode ( test ) ; const encoded = Bech32 . encode ( decoded . hrp , decoded . words , decoded . version ) ; expect ( encoded ) . to . equal ( test . toLowerCase ( ) ) ; } ) ; } const invalid = [ "\x201xj0phk" , "\x7f1g6xzxy" , "\x801vctc34" , "qyrz8wqd2c9m" , "1qyrz8wqd2c9m" , "y1b0jsk6g" , "lt1igcx5c0" , "in1muywd" , "mm1crxm3i" , "au1s5cgom" , "M1VUXWEZ" , "16plkw9" , "1p2gdwpf" , ] ; for ( const test of invalid ) { it ( "invalid " + test , ( ) => { expect ( ( ) => Bech32 . decode ( test ) ) . to . throw ( ) ; } ) ; } } ) ; } ) ;