2424import org .tron .common .utils .FileUtil ;
2525import org .tron .common .utils .Sha256Hash ;
2626import org .tron .common .zksnark .Librustzcash ;
27+ import org .tron .common .zksnark .LibrustzcashParam ;
2728import org .tron .common .zksnark .LibrustzcashParam .BindingSigParams ;
2829import org .tron .common .zksnark .LibrustzcashParam .InitZksnarkParams ;
2930import org .tron .common .zksnark .LibrustzcashParam .OutputProofParams ;
4142import org .tron .core .config .DefaultConfig ;
4243import org .tron .core .config .args .Args ;
4344import org .tron .core .db .Manager ;
44- import org .tron .core .exception .BadItemException ;
45- import org .tron .core .exception .ContractExeException ;
46- import org .tron .core .exception .ContractValidateException ;
47- import org .tron .core .exception .PermissionException ;
48- import org .tron .core .exception .SignatureFormatException ;
49- import org .tron .core .exception .ZksnarkException ;
45+ import org .tron .core .exception .*;
5046import org .tron .core .zen .ZenTransactionBuilder ;
5147import org .tron .core .zen .ZenTransactionBuilder .ReceiveDescriptionInfo ;
5248import org .tron .core .zen .ZenTransactionBuilder .SpendDescriptionInfo ;
6258import org .tron .core .zen .note .NoteEncryption ;
6359import org .tron .core .zen .note .Note .NotePlaintextEncryptionResult ;
6460import org .tron .core .zen .note .OutgoingPlaintext ;
61+ import org .tron .protos .Contract ;
6562import org .tron .protos .Contract .PedersenHash ;
6663import org .tron .protos .Contract .ShieldedTransferContract ;
6764import org .tron .protos .Contract .SpendDescription ;
65+ import org .tron .protos .Protocol ;
6866import org .tron .protos .Protocol .AccountType ;
6967import org .tron .protos .Protocol .Transaction ;
7068import org .tron .protos .Protocol .Transaction .Contract .ContractType ;
@@ -84,17 +82,10 @@ public class ShieldedReceiveTest {
8482
8583 private static Wallet wallet ;
8684
87- public enum TestColumn {CV , ZKPOOF , D_CM , PKD_CM , VALUE_CM , R_CM }
88-
89- ;
90-
91- public enum TestSignMissingColumn {FROM_ADDRESS , FROM_AMOUNT , SPEND_DESCRITPION , RECEIVE_DESCRIPTION , TO_ADDRESS , TO_AMOUNT }
92-
93- ;
94-
95- public enum TestReceiveMissingColumn {CV , CM , EPK , C_ENC , C_OUT , ZKPROOF }
96-
97- ;
85+ public enum TestColumn {CV , ZKPOOF , D_CM , PKD_CM , VALUE_CM , R_CM };
86+ public enum TestSignMissingColumn {FROM_ADDRESS , FROM_AMOUNT , SPEND_DESCRITPION ,
87+ RECEIVE_DESCRIPTION , TO_ADDRESS , TO_AMOUNT };
88+ public enum TestReceiveMissingColumn {CV , CM , EPK , C_ENC , C_OUT , ZKPROOF };
9889
9990 static {
10091 Args .setParam (new String []{"--output-directory" , dbPath }, "config-localtest.conf" );
@@ -1962,4 +1953,164 @@ public TransactionCapsule buildShieldedTransactionWithoutSpendAuthSig(PaymentAdd
19621953 builder .getContractBuilder ().setBindingSignature (ByteString .copyFrom (bindingSig ));
19631954 return transactionCapsule ;
19641955 }
1956+
1957+ @ Test
1958+ public void testMemoTooLong ()
1959+ throws ContractValidateException , TooBigTransactionException , TooBigTransactionResultException ,
1960+ TaposException , TransactionExpirationException , ReceiptCheckErrException ,
1961+ DupTransactionException , VMIllegalException , ValidateSignatureException , BadItemException ,
1962+ ContractExeException , AccountResourceInsufficientException , InvalidProtocolBufferException , ZksnarkException {
1963+ Pointer ctx = Librustzcash .librustzcashSaplingProvingCtxInit ();
1964+
1965+ librustzcashInitZksnarkParams ();
1966+ dbManager .getDynamicPropertiesStore ().saveAllowZksnarkTransaction (1 );
1967+ dbManager .getDynamicPropertiesStore ().saveTotalShieldedPoolValue (4010 * 1000000l );
1968+ ZenTransactionBuilder builder = new ZenTransactionBuilder (wallet );
1969+
1970+ // generate spend proof
1971+ SpendingKey sk = SpendingKey
1972+ .decode ("ff2c06269315333a9207f817d2eca0ac555ca8f90196976324c7756504e7c9ee" );
1973+ ExpandedSpendingKey expsk = sk .expandedSpendingKey ();
1974+ PaymentAddress address = sk .defaultAddress ();
1975+ Note note = new Note (address , 4010 * 1000000 );
1976+ IncrementalMerkleVoucherContainer voucher = createSimpleMerkleVoucherContainer (note .cm ());
1977+ byte [] anchor = voucher .root ().getContent ().toByteArray ();
1978+ dbManager .getMerkleContainer ()
1979+ .putMerkleTreeIntoStore (anchor , voucher .getVoucherCapsule ().getTree ());
1980+ builder .addSpend (expsk , note , anchor , voucher );
1981+
1982+ // generate output proof
1983+ SpendingKey spendingKey = SpendingKey .random ();
1984+ FullViewingKey fullViewingKey = spendingKey .fullViewingKey ();
1985+ IncomingViewingKey incomingViewingKey = fullViewingKey .inViewingKey ();
1986+ PaymentAddress paymentAddress = incomingViewingKey .address (new DiversifierT ()).get ();
1987+ byte [] memo = org .tron .keystore .Wallet .generateRandomBytes (1024 );
1988+ builder .addOutput (fullViewingKey .getOvk (), paymentAddress , 4000 * 1000000 , memo );
1989+
1990+ TransactionCapsule transactionCap = builder .build ();
1991+
1992+ boolean ok = dbManager .pushTransaction (transactionCap );
1993+ Assert .assertTrue (ok );
1994+
1995+ // add here
1996+ byte [] ivk = fullViewingKey .inViewingKey ().getValue ();
1997+ Protocol .Transaction t = transactionCap .getInstance ();
1998+
1999+ for (org .tron .protos .Protocol .Transaction .Contract c : t .getRawData ().getContractList ()) {
2000+ if (c .getType () != ContractType .ShieldedTransferContract ) {
2001+ continue ;
2002+ }
2003+ Contract .ShieldedTransferContract stContract = c .getParameter ()
2004+ .unpack (Contract .ShieldedTransferContract .class );
2005+ Contract .ReceiveDescription receiveDescription = stContract .getReceiveDescription (0 );
2006+
2007+ Optional <Note > ret1 = Note .decrypt (
2008+ receiveDescription .getCEnc ().toByteArray (),//ciphertext
2009+ ivk ,
2010+ receiveDescription .getEpk ().toByteArray (),//epk
2011+ receiveDescription .getNoteCommitment ().toByteArray () //cm
2012+ );
2013+
2014+ if (ret1 .isPresent ()) {
2015+ Note noteText = ret1 .get ();
2016+ byte [] pk_d = new byte [32 ];
2017+ if (!Librustzcash .librustzcashIvkToPkd (
2018+ new LibrustzcashParam .IvkToPkdParams (incomingViewingKey .getValue (), noteText .d .getData (), pk_d ))) {
2019+ Librustzcash .librustzcashSaplingProvingCtxFree (ctx );
2020+ return ;
2021+ }
2022+ Assert .assertArrayEquals (paymentAddress .getPkD (), pk_d );
2023+ Assert .assertEquals ( 4000 * 1000000 ,noteText .value );
2024+
2025+ byte [] resultMemo = new byte [512 ];
2026+ System .arraycopy (memo ,0 ,resultMemo ,0 ,512 );
2027+ Assert .assertArrayEquals (resultMemo , noteText .memo );
2028+ } else {
2029+ Assert .assertFalse (true );
2030+ }
2031+ }
2032+ // end here
2033+ Librustzcash .librustzcashSaplingProvingCtxFree (ctx );
2034+ Assert .assertTrue (ok );
2035+ }
2036+
2037+ @ Test
2038+ public void testMemoNotEnough ()
2039+ throws ContractValidateException , TooBigTransactionException , TooBigTransactionResultException ,
2040+ TaposException , TransactionExpirationException , ReceiptCheckErrException ,
2041+ DupTransactionException , VMIllegalException , ValidateSignatureException , BadItemException ,
2042+ ContractExeException , AccountResourceInsufficientException , InvalidProtocolBufferException , ZksnarkException {
2043+ Pointer ctx = Librustzcash .librustzcashSaplingProvingCtxInit ();
2044+
2045+ librustzcashInitZksnarkParams ();
2046+ dbManager .getDynamicPropertiesStore ().saveAllowZksnarkTransaction (1 );
2047+ dbManager .getDynamicPropertiesStore ().saveTotalShieldedPoolValue (4010 * 1000000l );
2048+ ZenTransactionBuilder builder = new ZenTransactionBuilder (wallet );
2049+
2050+ // generate spend proof
2051+ SpendingKey sk = SpendingKey
2052+ .decode ("ff2c06269315333a9207f817d2eca0ac555ca8f90196976324c7756504e7c9ee" );
2053+ ExpandedSpendingKey expsk = sk .expandedSpendingKey ();
2054+ PaymentAddress address = sk .defaultAddress ();
2055+ Note note = new Note (address , 4010 * 1000000 );
2056+ IncrementalMerkleVoucherContainer voucher = createSimpleMerkleVoucherContainer (note .cm ());
2057+ byte [] anchor = voucher .root ().getContent ().toByteArray ();
2058+ dbManager .getMerkleContainer ()
2059+ .putMerkleTreeIntoStore (anchor , voucher .getVoucherCapsule ().getTree ());
2060+ builder .addSpend (expsk , note , anchor , voucher );
2061+
2062+ // generate output proof
2063+ SpendingKey spendingKey = SpendingKey .random ();
2064+ FullViewingKey fullViewingKey = spendingKey .fullViewingKey ();
2065+ IncomingViewingKey incomingViewingKey = fullViewingKey .inViewingKey ();
2066+ PaymentAddress paymentAddress = incomingViewingKey .address (new DiversifierT ()).get ();
2067+ byte [] memo = org .tron .keystore .Wallet .generateRandomBytes (128 );
2068+ builder .addOutput (fullViewingKey .getOvk (), paymentAddress , 4000 * 1000000 , memo );
2069+
2070+ TransactionCapsule transactionCap = builder .build ();
2071+
2072+ boolean ok = dbManager .pushTransaction (transactionCap );
2073+ Assert .assertTrue (ok );
2074+
2075+ // add here
2076+ byte [] ivk = fullViewingKey .inViewingKey ().getValue ();
2077+ Protocol .Transaction t = transactionCap .getInstance ();
2078+
2079+ for (org .tron .protos .Protocol .Transaction .Contract c : t .getRawData ().getContractList ()) {
2080+ if (c .getType () != ContractType .ShieldedTransferContract ) {
2081+ continue ;
2082+ }
2083+ Contract .ShieldedTransferContract stContract = c .getParameter ()
2084+ .unpack (Contract .ShieldedTransferContract .class );
2085+ Contract .ReceiveDescription receiveDescription = stContract .getReceiveDescription (0 );
2086+
2087+ Optional <Note > ret1 = Note .decrypt (
2088+ receiveDescription .getCEnc ().toByteArray (),//ciphertext
2089+ ivk ,
2090+ receiveDescription .getEpk ().toByteArray (),//epk
2091+ receiveDescription .getNoteCommitment ().toByteArray () //cm
2092+ );
2093+
2094+ if (ret1 .isPresent ()) {
2095+ Note noteText = ret1 .get ();
2096+ byte [] pk_d = new byte [32 ];
2097+ if (!Librustzcash .librustzcashIvkToPkd (
2098+ new LibrustzcashParam .IvkToPkdParams (incomingViewingKey .getValue (), noteText .d .getData (), pk_d ))) {
2099+ Librustzcash .librustzcashSaplingProvingCtxFree (ctx );
2100+ return ;
2101+ }
2102+ Assert .assertArrayEquals (paymentAddress .getPkD (), pk_d );
2103+ Assert .assertEquals ( 4000 * 1000000 ,noteText .value );
2104+
2105+ byte [] resultMemo = new byte [512 ];
2106+ System .arraycopy (memo ,0 ,resultMemo ,0 ,128 );
2107+ Assert .assertArrayEquals (resultMemo , noteText .memo );
2108+ } else {
2109+ Assert .assertFalse (true );
2110+ }
2111+ }
2112+ // end here
2113+ Librustzcash .librustzcashSaplingProvingCtxFree (ctx );
2114+ Assert .assertTrue (ok );
2115+ }
19652116}
0 commit comments