@@ -120,6 +120,67 @@ describe("TKHQ", () => {
120120 expect ( encodedKey ) . toEqual ( keySol ) ;
121121 } ) ;
122122
123+ it ( "encodes bitcoin WIF private key correctly" , async ( ) => {
124+ const keyWif = "L1sF5SF3CnCN9gA7vh7MAtbiVu9igdr3C1BYPKZduw4yaezdeCTV" ;
125+ const keyWifBytes = TKHQ . base58Decode ( keyWif ) . subarray ( 0 , - 4 ) ; // remove 4 byte checksum
126+ expect ( keyWifBytes . length ) . toEqual ( 34 ) ; // 1 byte version + 32 byte privkey + 1 byte compressed flag
127+ const keyPrivBytes = keyWifBytes . subarray ( 1 , 33 ) ;
128+ const encodedKey = await TKHQ . encodeKey (
129+ keyPrivBytes ,
130+ "BITCOIN_MAINNET_WIF"
131+ ) ;
132+ expect ( encodedKey ) . toEqual ( keyWif ) ;
133+ } ) ;
134+
135+ // Bitcoin WIF export negative tests
136+ it ( "rejects bitcoin WIF encoding with wrong private key length" , async ( ) => {
137+ const tooShortKey = new Uint8Array ( 31 ) . fill ( 0x42 ) ;
138+ await expect (
139+ TKHQ . encodeKey ( tooShortKey , "BITCOIN_MAINNET_WIF" )
140+ ) . rejects . toThrow ( "invalid private key length. Expected 32 bytes. Got 31." ) ;
141+
142+ const tooLongKey = new Uint8Array ( 33 ) . fill ( 0x42 ) ;
143+ await expect (
144+ TKHQ . encodeKey ( tooLongKey , "BITCOIN_MAINNET_WIF" )
145+ ) . rejects . toThrow ( "invalid private key length. Expected 32 bytes. Got 33." ) ;
146+ } ) ;
147+
148+ // Encodes Testnet WIF correctly
149+ it ( "encodes bitcoin Testnet WIF private key correctly" , async ( ) => {
150+ const keyWif = "cTVYYVnHVzNepyxZhB7fzRuneC6RVDJETJdGAoDc4RrirgghQxyR" ;
151+ const keyWifBytes = TKHQ . base58Decode ( keyWif ) . subarray ( 0 , - 4 ) ; // remove 4 byte checksum
152+ expect ( keyWifBytes . length ) . toEqual ( 34 ) ; // 1 byte version + 32 byte privkey + 1 byte compressed flag
153+ const keyPrivBytes = keyWifBytes . subarray ( 1 , 33 ) ;
154+ const encodedKey = await TKHQ . encodeKey (
155+ keyPrivBytes ,
156+ "BITCOIN_TESTNET_WIF"
157+ ) ;
158+ expect ( encodedKey ) . toEqual ( keyWif ) ;
159+ } ) ;
160+
161+ it ( "encodes sui bech32 private key correctly" , async ( ) => {
162+ const keySui =
163+ "suiprivkey1qpj5xd9396rxsu7h45tzccalhuf95e4pygls3ps9txszn9ywpwsnznaeq0l" ;
164+ const { words } = TKHQ . decodeBech32 ( keySui ) ;
165+ const keySuiBytes = TKHQ . bech32FromWords ( words ) . subarray ( 1 ) ; // remove 1 byte scheme flag
166+ expect ( keySuiBytes . length ) . toEqual ( 32 ) ;
167+ const encodedKey = await TKHQ . encodeKey ( keySuiBytes , "SUI_BECH32" ) ;
168+ expect ( encodedKey ) . toEqual ( keySui ) ;
169+ } ) ;
170+
171+ // SUI Bech32 export negative tests
172+ it ( "rejects sui bech32 encoding with wrong private key length" , async ( ) => {
173+ const tooShortKey = new Uint8Array ( 31 ) . fill ( 0x42 ) ;
174+ await expect ( TKHQ . encodeKey ( tooShortKey , "SUI_BECH32" ) ) . rejects . toThrow (
175+ "invalid private key length. Expected 32 bytes. Got 31."
176+ ) ;
177+
178+ const tooLongKey = new Uint8Array ( 33 ) . fill ( 0x42 ) ;
179+ await expect ( TKHQ . encodeKey ( tooLongKey , "SUI_BECH32" ) ) . rejects . toThrow (
180+ "invalid private key length. Expected 32 bytes. Got 33."
181+ ) ;
182+ } ) ;
183+
123184 it ( "encodes wallet with only mnemonic correctly" , async ( ) => {
124185 const mnemonic =
125186 "suffer surround soup duck goose patrol add unveil appear eye neglect hurry alpha project tomorrow embody hen wish twenty join notable amused burden treat" ;
0 commit comments