Skip to content

Commit 3938504

Browse files
Merge pull request #6174 from 317787106/hotfix/verify_payment
feat(api): Optimize error handling of shielded transaction api
2 parents 068eb3a + a6b2142 commit 3938504

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

framework/src/main/java/org/tron/core/Wallet.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3478,6 +3478,9 @@ private void checkShieldedTRC20NoteValue(
34783478
if (spendNote.getNote().getValue() < 0) {
34793479
throw new ContractValidateException("The value in SpendNoteTRC20 must >= 0");
34803480
}
3481+
if (StringUtils.isEmpty(spendNote.getNote().getPaymentAddress())) {
3482+
throw new ContractValidateException("Payment Address in SpendNote should not be empty");
3483+
}
34813484
}
34823485
}
34833486

@@ -3486,6 +3489,9 @@ private void checkShieldedTRC20NoteValue(
34863489
if (receiveNote.getNote().getValue() < 0) {
34873490
throw new ContractValidateException("The value in ReceiveNote must >= 0");
34883491
}
3492+
if (StringUtils.isEmpty(receiveNote.getNote().getPaymentAddress())) {
3493+
throw new ContractValidateException("Payment Address in ReceiveNote should not be empty");
3494+
}
34893495
}
34903496
}
34913497
}

framework/src/main/java/org/tron/core/zen/address/KeyIo.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.ByteArrayOutputStream;
2020
import java.util.ArrayList;
2121
import java.util.List;
22+
import org.apache.commons.lang3.StringUtils;
2223
import org.tron.common.utils.Bech32;
2324
import org.tron.common.utils.Bech32.Bech32Data;
2425

@@ -29,6 +30,9 @@ public class KeyIo {
2930
private static String SAPLING_PAYMENT_ADDRESS = "ztron";
3031

3132
public static PaymentAddress decodePaymentAddress(String str) {
33+
if (StringUtils.isEmpty(str)) {
34+
return null;
35+
}
3236
byte[] data;
3337
Bech32Data bech = Bech32.decode(str);
3438
if (bech.hrp.equals(SAPLING_PAYMENT_ADDRESS)

framework/src/test/java/org/tron/core/ShieldWalletTest.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.tron.core.capsule.TransactionCapsule;
2020
import org.tron.core.config.args.Args;
2121
import org.tron.core.exception.ContractExeException;
22+
import org.tron.core.exception.ContractValidateException;
2223
import org.tron.core.services.http.JsonFormat;
2324
import org.tron.core.services.http.JsonFormat.ParseException;
2425

@@ -337,6 +338,82 @@ public void testCreateShieldedContractParameters() throws ContractExeException {
337338
}
338339
}
339340

341+
@Test
342+
public void testCreateShieldedContractParameters2() throws ContractExeException {
343+
librustzcashInitZksnarkParams();
344+
Args.getInstance().setFullNodeAllowShieldedTransactionArgs(true);
345+
Wallet wallet1 = spy(new Wallet());
346+
347+
doReturn(BigInteger.valueOf(1).toByteArray())
348+
.when(wallet1).getShieldedContractScalingFactor(
349+
ByteArray.fromHexString("4144007979359ECAC395BBD3CEF8060D3DF2DC3F01"));
350+
String parameter = new String(ByteArray.fromHexString(
351+
"7b0a202020202261736b223a2263323531336539653330383439343933326264383265306365353336"
352+
+ "363264313734323164393062373261383437316130613132623835353261333336653032222c0a202"
353+
+ "02020226e736b223a2234633662663364643461303634336432306236323866376534353938306335"
354+
+ "653138376630376135316436663365383661616631616239313663303765623064222c0a202020202"
355+
+ "26f766b223a2231376135386439613530353864613665343263613132636432383964306136616131"
356+
+ "363962393236633138653139626361353138623864366638363734653433222c0a202020202266726"
357+
+ "f6d5f616d6f756e74223a22313030222c0a2020202022736869656c6465645f726563656976657322"
358+
+ "3a5b0a20202020202020207b0a202020202020202020202020226e6f7465223a7b0a2020202020202"
359+
+ "02020202020202020202276616c7565223a3130302c0a202020202020202020202020202020202270"
360+
+ "61796d656e745f61646472657373223a22222c0a202020202020202020202020202020202272636d2"
361+
+ "23a223136623666356534303434346162376565616231316165363631336332376633353131373937"
362+
+ "3165666138376237313536306235383133383239633933393064220a2020202020202020202020207"
363+
+ "d0a20202020202020207d0a202020205d2c0a2020202022736869656c6465645f54524332305f636f"
364+
+ "6e74726163745f61646472657373223a2234313434303037393739333539454341433339354242443"
365+
+ "3434546383036304433444632444333463031220a7d"));
366+
367+
PrivateShieldedTRC20Parameters.Builder builder = PrivateShieldedTRC20Parameters.newBuilder();
368+
try {
369+
JsonFormat.merge(parameter, builder, false);
370+
} catch (ParseException e) {
371+
Assert.fail();
372+
}
373+
374+
try {
375+
wallet1.createShieldedContractParameters(builder.build());
376+
Assert.fail();
377+
} catch (Exception e) {
378+
Assert.assertTrue(e instanceof ContractValidateException);
379+
Assert.assertEquals("Payment Address in ReceiveNote should not be empty",
380+
e.getMessage());
381+
}
382+
383+
String parameter2 = new String(ByteArray.fromHexString(
384+
"7b0a202020202261736b223a2263323531336539653330383439343933326264383265306365353336"
385+
+ "363264313734323164393062373261383437316130613132623835353261333336653032222c0a202"
386+
+ "02020226e736b223a2234633662663364643461303634336432306236323866376534353938306335"
387+
+ "653138376630376135316436663365383661616631616239313663303765623064222c0a202020202"
388+
+ "26f766b223a2231376135386439613530353864613665343263613132636432383964306136616131"
389+
+ "363962393236633138653139626361353138623864366638363734653433222c0a202020202266726"
390+
+ "f6d5f616d6f756e74223a22313030222c0a2020202022736869656c6465645f7370656e6473223a5b"
391+
+ "0a20202020202020207b0a202020202020202020202020226e6f7465223a7b0a20202020202020202"
392+
+ "0202020202020202276616c7565223a3130302c0a2020202020202020202020202020202022706179"
393+
+ "6d656e745f61646472657373223a22222c0a202020202020202020202020202020202272636d223a2"
394+
+ "231366236663565343034343461623765656162313161653636313363323766333531313739373165"
395+
+ "666138376237313536306235383133383239633933393064220a2020202020202020202020207d0a2"
396+
+ "0202020202020207d0a202020205d2c0a2020202022736869656c6465645f54524332305f636f6e74"
397+
+ "726163745f61646472657373223a22343134343030373937393335394543414333393542424433434"
398+
+ "546383036304433444632444333463031220a7d"));
399+
400+
builder = PrivateShieldedTRC20Parameters.newBuilder();
401+
try {
402+
JsonFormat.merge(parameter2, builder, false);
403+
} catch (ParseException e) {
404+
Assert.fail();
405+
}
406+
407+
try {
408+
wallet1.createShieldedContractParameters(builder.build());
409+
Assert.fail();
410+
} catch (Exception e) {
411+
Assert.assertTrue(e instanceof ContractValidateException);
412+
Assert.assertEquals("Payment Address in SpendNote should not be empty",
413+
e.getMessage());
414+
}
415+
}
416+
340417
@Test
341418
public void testCreateShieldedContractParametersWithoutAsk() throws ContractExeException {
342419
librustzcashInitZksnarkParams();

framework/src/test/java/org/tron/core/ShieldedTRC20BuilderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2269,7 +2269,7 @@ public void testscanShieldedTRC20NotesByOvk() throws Exception {
22692269
}
22702270
}
22712271

2272-
@Test(expected = IllegalArgumentException.class)
2272+
@Test(expected = ZksnarkException.class)
22732273
public void isShieldedTRC20ContractNoteSpent() throws Exception {
22742274
int statNum = 9200;
22752275
int endNum = 9240;

0 commit comments

Comments
 (0)