@@ -17,6 +17,8 @@ import { Prover } from "./prover";
1717const height = 1234209 ;
1818const externalAddress = "9gN8gmyaDBuWPZLn8zj9uZxnLUj4TE9rtedtLGNjf6cUhTmoTwc" ;
1919
20+ const toBytes = ( input : string | undefined | null ) => hex . decode ( input ?? "" ) ;
21+
2022describe ( "Transaction signing" , ( ) => {
2123 it ( "Should sign a transaction with a single secret and a single input" , async ( ) => {
2224 // generate keys
@@ -41,7 +43,7 @@ describe("Transaction signing", () => {
4143 const signedTx = prover . signTransaction ( unsignedTx , [ rootKey ] ) ;
4244
4345 // verify
44- const proof = hex . decode ( signedTx . inputs [ 0 ] . spendingProof . proofBytes ) ;
46+ const proof = toBytes ( signedTx . inputs [ 0 ] . spendingProof ? .proofBytes ) ;
4547
4648 // verify using own verifier
4749 expect ( prover . verify ( unsignedTx , proof , rootKey ) ) . to . be . true ;
@@ -79,7 +81,7 @@ describe("Transaction signing", () => {
7981 const signedTx = prover . signTransaction ( unsignedTx , [ rootKey , child1 ] ) ;
8082
8183 // verify
82- const proof = hex . decode ( signedTx . inputs [ 0 ] . spendingProof . proofBytes ) ;
84+ const proof = toBytes ( signedTx . inputs [ 0 ] . spendingProof ? .proofBytes ) ;
8385
8486 // verify using own verifier
8587 expect ( prover . verify ( unsignedTx , proof , child1 ) ) . to . be . true ;
@@ -119,7 +121,7 @@ describe("Transaction signing", () => {
119121 ] ) ;
120122
121123 // verify
122- const proof = hex . decode ( signedTx . inputs [ 0 ] . spendingProof . proofBytes ) ;
124+ const proof = toBytes ( signedTx . inputs [ 0 ] . spendingProof ? .proofBytes ) ;
123125
124126 // verify using own verifier
125127 expect ( prover . verify ( unsignedTx , proof , rootKey ) ) . to . be . true ;
@@ -161,8 +163,8 @@ describe("Transaction signing", () => {
161163
162164 // verify
163165 const txBytes = unsignedTx . toBytes ( ) ;
164- const proof0 = hex . decode ( signedTx . inputs [ 0 ] . spendingProof . proofBytes ) ;
165- const proof1 = hex . decode ( signedTx . inputs [ 1 ] . spendingProof . proofBytes ) ;
166+ const proof0 = toBytes ( signedTx . inputs [ 0 ] . spendingProof ? .proofBytes ) ;
167+ const proof1 = toBytes ( signedTx . inputs [ 1 ] . spendingProof ? .proofBytes ) ;
166168
167169 // verify using own verifier
168170 expect ( prover . verify ( txBytes , proof0 , rootKey ) ) . to . be . true ;
@@ -208,9 +210,9 @@ describe("Transaction signing", () => {
208210
209211 // verify
210212 const txBytes = unsignedTx . toBytes ( ) ;
211- const proof0 = hex . decode ( signedTx . inputs [ 0 ] . spendingProof . proofBytes ) ;
212- const proof1 = hex . decode ( signedTx . inputs [ 1 ] . spendingProof . proofBytes ) ;
213- const proof2 = hex . decode ( signedTx . inputs [ 2 ] . spendingProof . proofBytes ) ;
213+ const proof0 = toBytes ( signedTx . inputs [ 0 ] . spendingProof ? .proofBytes ) ;
214+ const proof1 = toBytes ( signedTx . inputs [ 1 ] . spendingProof ? .proofBytes ) ;
215+ const proof2 = toBytes ( signedTx . inputs [ 2 ] . spendingProof ? .proofBytes ) ;
214216
215217 expect ( prover . verify ( txBytes , proof0 , rootKey ) ) . to . be . false ; // inverted by the mapper
216218 expect ( prover . verify ( txBytes , proof0 , child2 ) ) . to . be . true ; // inverted by the mapper
@@ -250,8 +252,8 @@ describe("Transaction signing", () => {
250252
251253 // verify
252254 const txBytes = unsignedTx . toBytes ( ) ;
253- const proof0 = hex . decode ( signedTx . inputs [ 0 ] . spendingProof . proofBytes ) ;
254- const proof1 = hex . decode ( signedTx . inputs [ 1 ] . spendingProof . proofBytes ) ;
255+ const proof0 = toBytes ( signedTx . inputs [ 0 ] . spendingProof ? .proofBytes ) ;
256+ const proof1 = toBytes ( signedTx . inputs [ 1 ] . spendingProof ? .proofBytes ) ;
255257
256258 expect ( prover . verify ( txBytes , proof0 , rootKey ) ) . to . be . false ; // inverted by the mapper
257259 expect ( prover . verify ( txBytes , proof0 , child1 ) ) . to . be . true ; // inverted by the mapper
@@ -319,14 +321,14 @@ describe("Transaction signing", () => {
319321
320322 // verify all inputs using own verifier
321323 for ( const input of signedTx . inputs ) {
322- const proof = hex . decode ( input . spendingProof . proofBytes ) ;
324+ const proof = toBytes ( input . spendingProof ? .proofBytes ) ;
323325 expect ( prover . verify ( txBytes , proof , rootKey ) ) . to . be . true ;
324326 }
325327
326328 // verify all inputs using sigma-rust for comparison
327329 const addr = Address . from_public_key ( rootKey . publicKey ) ;
328330 for ( const input of signedTx . inputs ) {
329- const proof = hex . decode ( input . spendingProof . proofBytes ) ;
331+ const proof = toBytes ( input . spendingProof ? .proofBytes ) ;
330332 expect ( verify_signature ( addr , txBytes , proof ) ) . to . be . true ;
331333 }
332334 } ) ;
@@ -380,42 +382,42 @@ describe("Transaction proof verification", () => {
380382
381383 it ( "Should verify from bytes" , ( ) => {
382384 const prover = new Prover ( ) ;
383- const proof = signedTx . inputs [ 0 ] . spendingProof . proofBytes ;
385+ const proof = signedTx . inputs [ 0 ] . spendingProof ? .proofBytes ?? "" ;
384386
385387 expect ( prover . verify ( unsignedTx . toBytes ( ) , proof , rootKey ) ) . to . be . true ;
386388 } ) ;
387389
388390 it ( "Should verify from hex" , ( ) => {
389391 const prover = new Prover ( ) ;
390- const proof = signedTx . inputs [ 0 ] . spendingProof . proofBytes ;
392+ const proof = signedTx . inputs [ 0 ] . spendingProof ? .proofBytes ?? "" ;
391393
392394 expect ( prover . verify ( hex . encode ( unsignedTx . toBytes ( ) ) , proof , rootKey ) ) . to . be . true ;
393395 } ) ;
394396
395397 it ( "Should verify from SignedTransaction" , ( ) => {
396398 const prover = new Prover ( ) ;
397- const proof = signedTx . inputs [ 0 ] . spendingProof . proofBytes ;
399+ const proof = signedTx . inputs [ 0 ] . spendingProof ? .proofBytes ?? "" ;
398400
399401 expect ( prover . verify ( signedTx , proof , rootKey ) ) . to . be . true ;
400402 } ) ;
401403
402404 it ( "Should verify from ErgoUnsignedTransaction" , ( ) => {
403405 const prover = new Prover ( ) ;
404- const proof = signedTx . inputs [ 0 ] . spendingProof . proofBytes ;
406+ const proof = signedTx . inputs [ 0 ] . spendingProof ? .proofBytes ?? "" ;
405407
406408 expect ( prover . verify ( unsignedTx , proof , rootKey ) ) . to . be . true ;
407409 } ) ;
408410
409411 it ( "Should verify from EIP12UnsignedTransaction" , ( ) => {
410412 const prover = new Prover ( ) ;
411- const proof = signedTx . inputs [ 0 ] . spendingProof . proofBytes ;
413+ const proof = signedTx . inputs [ 0 ] . spendingProof ? .proofBytes ?? "" ;
412414
413415 expect ( prover . verify ( unsignedTx . toEIP12Object ( ) , proof , rootKey ) ) . to . be . true ;
414416 } ) ;
415417
416418 it ( "Should verify from PlainObject" , ( ) => {
417419 const prover = new Prover ( ) ;
418- const proof = signedTx . inputs [ 0 ] . spendingProof . proofBytes ;
420+ const proof = signedTx . inputs [ 0 ] . spendingProof ? .proofBytes ?? "" ;
419421
420422 expect ( prover . verify ( unsignedTx . toPlainObject ( ) , proof , rootKey ) ) . to . be . true ;
421423 } ) ;
0 commit comments