55import com .google .common .base .Charsets ;
66import com .google .common .collect .Lists ;
77import com .google .common .io .Files ;
8+ import com .google .protobuf .Any ;
89import com .google .protobuf .ByteString ;
910import com .google .protobuf .InvalidProtocolBufferException ;
1011import java .io .File ;
@@ -1415,16 +1416,8 @@ public SpendDescriptionCapsule generateSpendProof(SpendDescriptionInfo spend, lo
14151416 addZeroValueOutputNote (builder );
14161417 builder .setTransparentOutput (ByteArray .fromHexString (TO_ADDRESS ), 10_000_000L );
14171418
1418- TransactionCapsule transactionCap1 = builder .build ();
1419- try {
1420- executeTx (transactionCap1 );
1421- Assert .fail ();
1422- } catch (ContractValidateException e ) {
1423- if (!e .getMessage ().equals ("librustzcashSaplingFinalCheck error" )) {
1424- throw e ;
1425- }
1426- System .out .println ("Done" );
1427- }
1419+ ZksnarkException error = Assert .assertThrows (ZksnarkException .class , builder ::build );
1420+ Assert .assertEquals ("Binding signature generation failed" , error .getMessage ());
14281421
14291422 // 0L + 20_000_000L = 0L + 10_000_000L + 10_000_000L
14301423 }
@@ -1459,7 +1452,11 @@ public void TestGeneratesProofWithDiffCtx() throws Exception {
14591452 public SpendDescriptionCapsule generateSpendProof (SpendDescriptionInfo spend , long ctx )
14601453 throws ZksnarkException {
14611454 long fakeCtx = JLibrustzcash .librustzcashSaplingProvingCtxInit ();
1462- return super .generateSpendProof (spend , fakeCtx );
1455+ try {
1456+ return super .generateSpendProof (spend , fakeCtx );
1457+ } finally {
1458+ JLibrustzcash .librustzcashSaplingProvingCtxFree (fakeCtx );
1459+ }
14631460 }
14641461 };
14651462
@@ -1474,16 +1471,8 @@ public SpendDescriptionCapsule generateSpendProof(SpendDescriptionInfo spend, lo
14741471 addZeroValueOutputNote (builder );
14751472 builder .setTransparentOutput (ByteArray .fromHexString (TO_ADDRESS ), 10_000_000L );
14761473
1477- TransactionCapsule transactionCap1 = builder .build ();
1478- try {
1479- executeTx (transactionCap1 );
1480- Assert .fail ();
1481- } catch (ContractValidateException e ) {
1482- if (!e .getMessage ().equals ("librustzcashSaplingFinalCheck error" )) {
1483- throw e ;
1484- }
1485- System .out .println ("Done" );
1486- }
1474+ ZksnarkException error = Assert .assertThrows (ZksnarkException .class , builder ::build );
1475+ Assert .assertEquals ("Binding signature generation failed" , error .getMessage ());
14871476 // 0L + 20_000_000L = 0L + 10_000_000L + 10_000_000L
14881477 }
14891478 }
@@ -1664,6 +1653,28 @@ public void TestDefaultBuilder() throws Exception {
16641653 executeTx (transactionCapsule );
16651654 }
16661655
1656+ @ Test
1657+ public void testInvalidBindingSignatureIsRejectedByVerifier () throws Exception {
1658+ dbManager .getDynamicPropertiesStore ().saveAllowShieldedTransaction (1 );
1659+ dbManager .getDynamicPropertiesStore ().saveTotalShieldedPoolValue (1000 * 1000000L );
1660+
1661+ TransactionCapsule valid = generateDefaultBuilder (new ZenTransactionBuilder (wallet ));
1662+ Protocol .Transaction .Contract contract = valid .getInstance ().getRawData ().getContract (0 );
1663+ ShieldedTransferContract invalidSignature = contract .getParameter ()
1664+ .unpack (ShieldedTransferContract .class ).toBuilder ()
1665+ .setBindingSignature (ByteString .copyFrom (new byte [64 ])).build ();
1666+ TransactionCapsule invalid = new TransactionCapsule (valid .getInstance ().toBuilder ()
1667+ .setRawData (valid .getInstance ().getRawData ().toBuilder ()
1668+ .setContract (0 , contract .toBuilder ().setParameter (Any .pack (invalidSignature ))))
1669+ .build ());
1670+
1671+ ContractValidateException error = Assert .assertThrows (ContractValidateException .class ,
1672+ () -> executeTx (invalid ));
1673+ Assert .assertEquals ("librustzcashSaplingFinalCheck error" , error .getMessage ());
1674+ // The same transaction with its original signature must still pass validation and execution.
1675+ executeTx (valid );
1676+ }
1677+
16671678 @ Test
16681679 public void TestWrongSpendRk () throws Exception {
16691680 dbManager .getDynamicPropertiesStore ().saveAllowShieldedTransaction (1 );
0 commit comments