2020import org .tron .common .utils .ByteUtil ;
2121import org .tron .core .config .args .Args ;
2222import org .tron .core .exception .ContractValidateException ;
23+ import org .tron .core .exception .ZksnarkException ;
2324import org .tron .core .zen .note .NoteEncryption .Encryption ;
2425import org .tron .protos .contract .ShieldContract ;
2526
@@ -39,7 +40,8 @@ private static BytesMessage triggerInput(GrpcAPI.ShieldedTRC20Parameters paramet
3940 .setAmount (amount )
4041 .setShieldedTRC20Parameters (parameters );
4142 for (int i = 0 ; i < signatureCount ; i ++) {
42- request .addSpendAuthoritySignature (BytesMessage .getDefaultInstance ());
43+ request .addSpendAuthoritySignature (BytesMessage .newBuilder ()
44+ .setValue (ByteString .copyFrom (new byte [64 ])));
4345 }
4446
4547 CommonParameter commonParameter = mock (Args .class );
@@ -53,9 +55,24 @@ private static BytesMessage triggerInput(GrpcAPI.ShieldedTRC20Parameters paramet
5355 private static GrpcAPI .ShieldedTRC20Parameters withReceives (
5456 GrpcAPI .ShieldedTRC20Parameters .Builder parameters , int receives ) {
5557 for (int i = 0 ; i < receives ; i ++) {
56- parameters .addReceiveDescription (ShieldContract .ReceiveDescription .getDefaultInstance ());
58+ parameters .addReceiveDescription (ShieldContract .ReceiveDescription .newBuilder ()
59+ .setNoteCommitment (ByteString .copyFrom (new byte [32 ]))
60+ .setValueCommitment (ByteString .copyFrom (new byte [32 ]))
61+ .setEpk (ByteString .copyFrom (new byte [32 ]))
62+ .setZkproof (ByteString .copyFrom (new byte [192 ]))
63+ .setCEnc (ByteString .copyFrom (new byte [580 ]))
64+ .setCOut (ByteString .copyFrom (new byte [80 ])));
5765 }
58- return parameters .build ();
66+ return parameters .setBindingSignature (ByteString .copyFrom (new byte [64 ])).build ();
67+ }
68+
69+ private static ShieldContract .SpendDescription .Builder spend () {
70+ return ShieldContract .SpendDescription .newBuilder ()
71+ .setNullifier (ByteString .copyFrom (new byte [32 ]))
72+ .setAnchor (ByteString .copyFrom (new byte [32 ]))
73+ .setValueCommitment (ByteString .copyFrom (new byte [32 ]))
74+ .setRk (ByteString .copyFrom (new byte [32 ]))
75+ .setZkproof (ByteString .copyFrom (new byte [192 ]));
5976 }
6077
6178 /**
@@ -76,7 +93,7 @@ private static GrpcAPI.ShieldedTRC20Parameters burn(int receives, String amount)
7693 return withReceives (GrpcAPI .ShieldedTRC20Parameters .newBuilder ()
7794 .setParameterType ("burn" )
7895 .setTriggerContractInput (ByteArray .toHexString (record ))
79- .addSpendDescription (ShieldContract . SpendDescription . newBuilder ()
96+ .addSpendDescription (spend ()
8097 .setNullifier (ByteString .copyFrom (nullifier ))), receives );
8198 }
8299
@@ -85,6 +102,7 @@ public void transferDescriptionCardinalityIsReportedAsValidationFailure() {
85102 GrpcAPI .ShieldedTRC20Parameters .Builder parameters =
86103 GrpcAPI .ShieldedTRC20Parameters .newBuilder ()
87104 .setParameterType ("transfer" )
105+ .setBindingSignature (ByteString .copyFrom (new byte [64 ]))
88106 .addSpendDescription (ShieldContract .SpendDescription .getDefaultInstance ());
89107 for (int i = 0 ; i < 3 ; i ++) {
90108 parameters .addReceiveDescription (ShieldContract .ReceiveDescription .getDefaultInstance ());
@@ -96,6 +114,89 @@ public void transferDescriptionCardinalityIsReportedAsValidationFailure() {
96114 exception .getMessage ());
97115 }
98116
117+ @ Test
118+ public void invalidBindingSignatureLengthsAreReportedAsValidationFailures () {
119+ GrpcAPI .ShieldedTRC20Parameters [] parameters = {
120+ withReceives (GrpcAPI .ShieldedTRC20Parameters .newBuilder ().setParameterType ("mint" ), 1 ),
121+ withReceives (GrpcAPI .ShieldedTRC20Parameters .newBuilder ().setParameterType ("transfer" )
122+ .addSpendDescription (spend ()), 1 ),
123+ burn (0 , "1" )
124+ };
125+ for (GrpcAPI .ShieldedTRC20Parameters parameter : parameters ) {
126+ for (int length : new int []{0 , 63 , 65 }) {
127+ GrpcAPI .ShieldedTRC20Parameters invalid = parameter .toBuilder ()
128+ .setBindingSignature (ByteString .copyFrom (new byte [length ])).build ();
129+ ContractValidateException exception = assertThrows (ContractValidateException .class ,
130+ () -> triggerInput (invalid , "1" , invalid .getSpendDescriptionCount ()));
131+ assertEquals ("invalid shielded TRC-20 trigger input: binding_signature must be 64 bytes" ,
132+ exception .getMessage ());
133+ }
134+ }
135+ }
136+
137+ @ Test
138+ public void signatureCountValidationRetainsWalletError () {
139+ GrpcAPI .ShieldedTRC20Parameters [] parameters = {
140+ withReceives (GrpcAPI .ShieldedTRC20Parameters .newBuilder ().setParameterType ("transfer" )
141+ .addSpendDescription (spend ()), 1 ),
142+ burn (0 , "1" )
143+ };
144+ for (GrpcAPI .ShieldedTRC20Parameters parameter : parameters ) {
145+ for (int count : new int []{0 , 2 }) {
146+ ZksnarkException exception = assertThrows (ZksnarkException .class ,
147+ () -> triggerInput (parameter , "1" , count ));
148+ assertEquals ("the number of spendDescription and spendAuthoritySignature is not equal" ,
149+ exception .getMessage ());
150+ }
151+ }
152+ }
153+
154+ @ Test
155+ public void burnInputValidationRetainsWalletErrors () {
156+ GrpcAPI .ShieldedTRC20Parameters valid = burn (0 , "1" );
157+ for (int count : new int []{0 , 2 }) {
158+ GrpcAPI .ShieldedTRC20Parameters .Builder parameters = valid .toBuilder ()
159+ .clearSpendDescription ();
160+ for (int i = 0 ; i < count ; i ++) {
161+ parameters .addSpendDescription (valid .getSpendDescription (0 ));
162+ }
163+ ZksnarkException exception = assertThrows (ZksnarkException .class ,
164+ () -> triggerInput (parameters .build (), "1" , count ));
165+ assertEquals ("burn trigger input requires exactly one spendDescription for nf-bound nonce" ,
166+ exception .getMessage ());
167+ }
168+ for (int length : new int []{0 , 31 , 33 }) {
169+ GrpcAPI .ShieldedTRC20Parameters .Builder parameters = valid .toBuilder ();
170+ parameters .getSpendDescriptionBuilder (0 ).setNullifier (ByteString .copyFrom (new byte [length ]));
171+ ZksnarkException exception = assertThrows (ZksnarkException .class ,
172+ () -> triggerInput (parameters .build (), "1" , 1 ));
173+ assertEquals ("burn trigger input requires 32-byte spendDescription.nullifier" ,
174+ exception .getMessage ());
175+ }
176+ }
177+
178+ @ Test
179+ public void malformedDescriptionsAreReportedAsValidationFailures () {
180+ GrpcAPI .ShieldedTRC20Parameters valid = withReceives (
181+ GrpcAPI .ShieldedTRC20Parameters .newBuilder ().setParameterType ("transfer" )
182+ .addSpendDescription (spend ()), 1 );
183+ GrpcAPI .ShieldedTRC20Parameters .Builder invalidSpend = valid .toBuilder ();
184+ invalidSpend .getSpendDescriptionBuilder (0 ).setZkproof (ByteString .copyFrom (new byte [191 ]));
185+ ContractValidateException spendError = assertThrows (ContractValidateException .class ,
186+ () -> triggerInput (invalidSpend .build (), "0" , 1 ));
187+ assertEquals ("invalid shielded TRC-20 trigger input: "
188+ + "spend_description.zkproof must be 192 bytes" ,
189+ spendError .getMessage ());
190+
191+ GrpcAPI .ShieldedTRC20Parameters .Builder invalidReceive = valid .toBuilder ();
192+ invalidReceive .getReceiveDescriptionBuilder (0 ).setCEnc (ByteString .copyFrom (new byte [579 ]));
193+ ContractValidateException receiveError = assertThrows (ContractValidateException .class ,
194+ () -> triggerInput (invalidReceive .build (), "0" , 1 ));
195+ assertEquals ("invalid shielded TRC-20 trigger input: "
196+ + "receive_description.c_enc must be 580 bytes" ,
197+ receiveError .getMessage ());
198+ }
199+
99200 @ Test
100201 public void mintRequiresExactlyOneReceiveDescription () throws Exception {
101202 for (int receives : new int []{0 , 2 }) {
0 commit comments