88import org .junit .Assert ;
99import org .spongycastle .util .encoders .Hex ;
1010import org .tron .api .GrpcAPI ;
11+ import org .tron .api .GrpcAPI .PrivateShieldedTRC20Parameters ;
1112import org .tron .api .GrpcAPI .Return ;
13+ import org .tron .api .GrpcAPI .SpendNoteTRC20 ;
1214import org .tron .api .GrpcAPI .TransactionExtention ;
1315import org .tron .common .crypto .ECKey ;
1416import org .tron .common .crypto .Sha256Sm3Hash ;
3840@ Slf4j
3941public class ShieldedTRC20Demo {
4042
41- private static String fullnode = "34.220.77.106:50051" ;
42- private static String soliditynode = "52.15.93.92:50061" ;
4343 private static String trc20ContractAddress = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t" ;
4444 private static String shieldedTRC20ContractAddress = "TQEuSEVRk1GtfExm5q9T8a1w84GvgQJ13V" ;
4545 private static String privateKey =
46- "f4cc9a4bcd6a2167e734d0e3de349f93c3a0210fcf6c2390f0e20bc43832a13b " ;
47- private static String pubAddress = "TCuT4T3xhrSuScmhbVuWf3ZAvRnMXvoqC6 " ;
46+ "2c8893287a87ac9f4b70af14fbae75e5c898e3b6645e5fed311f5fe60b2dff2f " ;
47+ private static String pubAddress = "TXmiKi5UZ6Pqe22aW5R8LEcNGGpgh2BNMH " ;
4848 private static String spendingKey = "004f74ce2bde08f0c936f2929b94cb2ca49111db95001576f99d04c3e671daf6" ;
49- private static GrpcClient grpcClient = new GrpcClient ( fullnode , soliditynode );
49+ private static GrpcClient grpcClient = WalletApi . init ( );
5050 private static BigInteger scalingFactorBi = getScalingFactorBi ();
5151
5252 public static void main (String [] args ) throws ZksnarkException , InterruptedException {
@@ -60,9 +60,8 @@ private static String mintDemo() throws ZksnarkException {
6060
6161 SpendingKey sk = new SpendingKey (ByteArray .fromHexString (spendingKey ));
6262 ExpandedSpendingKey expsk = sk .expandedSpendingKey ();
63-
6463 byte [] ovk = expsk .getOvk ();
65- long fromAmount = 10 ;
64+ long fromAmount = 1 ;
6665 FullViewingKey fullViewingKey = sk .fullViewingKey ();
6766 IncomingViewingKey incomingViewingKey = fullViewingKey .inViewingKey ();
6867 PaymentAddress paymentAddress = incomingViewingKey .address (new DiversifierT ().random ()).get ();
@@ -73,7 +72,7 @@ private static String mintDemo() throws ZksnarkException {
7372 GrpcAPI .ReceiveNote .Builder revNoteBuilder = GrpcAPI .ReceiveNote .newBuilder ();
7473 long revValue = fromAmount ;
7574 byte [] memo = new byte [512 ];
76- byte [] rcm = grpcClient .getRcm ().getValue ().toByteArray ();
75+ byte [] rcm = WalletApi .getRcm (). get ().getValue ().toByteArray ();
7776 GrpcAPI .Note revNote = getNote (revValue , KeyIo .encodePaymentAddress (paymentAddress ), rcm , memo );
7877 revNoteBuilder .setNote (revNote );
7978
@@ -85,7 +84,7 @@ private static String mintDemo() throws ZksnarkException {
8584 paramBuilder .addShieldedReceives (revNoteBuilder .build ());
8685 paramBuilder .setShieldedTRC20ContractAddress (ByteString .copyFrom (contractAddress ));
8786
88- GrpcAPI .ShieldedTRC20Parameters trc20MintParams = grpcClient
87+ GrpcAPI .ShieldedTRC20Parameters trc20MintParams = WalletApi
8988 .createShieldedContractParameters (paramBuilder .build ());
9089 byte [] callerAddress = WalletApi .decodeFromBase58Check (pubAddress );
9190 return triggerMint (contractAddress , callerAddress , privateKey ,
@@ -97,56 +96,52 @@ public static void transferDemo() throws ZksnarkException, InterruptedException
9796 .decodeFromBase58Check (shieldedTRC20ContractAddress );
9897 byte [] callerAddress = WalletApi .decodeFromBase58Check (pubAddress );
9998 SpendingKey sk = new SpendingKey (ByteArray .fromHexString (spendingKey ));
100- setAllowance (10 );
99+ setAllowance (2 );
101100 GrpcAPI .PrivateShieldedTRC20Parameters mintPrivateParam1 = mintParams (
102- privateKey , 10 , shieldedTRC20ContractAddress );
103- GrpcAPI .ShieldedTRC20Parameters mintParam1 = grpcClient .createShieldedContractParameters (
101+ privateKey , 2 , shieldedTRC20ContractAddress );
102+ GrpcAPI .ShieldedTRC20Parameters mintParam1 = WalletApi .createShieldedContractParameters (
104103 mintPrivateParam1 );
105104 String mintInput = mintParam1 .getTriggerContractInput ();
106105 String txid = triggerMint (contractAddress , callerAddress , privateKey , mintInput );
107106
108- // SpendNoteTRC20 1
109- logger .info ("mint txid: " + txid );
110- Optional <TransactionInfo > infoById = grpcClient .getTransactionInfoById (txid );
111- while (infoById .get ().getLogList ().size () < 2 ) {
112- logger .info ("Can not get transaction info, please wait...." );
113- Thread .sleep (5000 );
114- infoById = grpcClient .getTransactionInfoById (txid );
115- }
116- byte [] txData = infoById .get ().getLog (1 ).getData ().toByteArray ();
117- long pos = bytes32Tolong (ByteArray .subArray (txData , 0 , 32 ));
118- byte [] contractResult = triggerGetPath (contractAddress , pos );
119- byte [] path = ByteArray .subArray (contractResult , 32 , 1056 );
120- byte [] root = ByteArray .subArray (contractResult , 0 , 32 );
121- GrpcAPI .SpendNoteTRC20 .Builder note1Builder = GrpcAPI .SpendNoteTRC20 .newBuilder ();
122- note1Builder .setAlpha (ByteString .copyFrom (grpcClient .getRcm ().getValue ().toByteArray ()));
123- note1Builder .setPos (pos );
124- note1Builder .setPath (ByteString .copyFrom (path ));
125- note1Builder .setRoot (ByteString .copyFrom (root ));
126- note1Builder .setNote (mintPrivateParam1 .getShieldedReceives (0 ).getNote ());
107+ Optional <TransactionInfo > infoById = waitToGetTransactionInfo (txid );
127108 GrpcAPI .PrivateShieldedTRC20Parameters .Builder privateTRC20Builder = GrpcAPI
128109 .PrivateShieldedTRC20Parameters .newBuilder ();
129- privateTRC20Builder .addShieldedSpends (note1Builder .build ());
110+ privateTRC20Builder
111+ .addShieldedSpends (getSpendNote (infoById .get (), mintPrivateParam1 , contractAddress ));
130112
131113 //ReceiveNote 1
132114 GrpcAPI .ReceiveNote .Builder revNoteBuilder = GrpcAPI .ReceiveNote .newBuilder ();
133115 FullViewingKey fullViewingKey = sk .fullViewingKey ();
134116 IncomingViewingKey incomingViewingKey = fullViewingKey .inViewingKey ();
135117 PaymentAddress paymentAddress = incomingViewingKey .address (new DiversifierT ().random ()).get ();
136- long revValue = 10 ;
118+ long revValue = 1 ;
137119 byte [] memo = new byte [512 ];
138- byte [] rcm = grpcClient .getRcm ().getValue ().toByteArray ();
120+ byte [] rcm = WalletApi .getRcm (). get ().getValue ().toByteArray ();
139121 String paymentAddressStr = KeyIo .encodePaymentAddress (paymentAddress );
140122 GrpcAPI .Note revNote = getNote (revValue , paymentAddressStr , rcm , memo );
141123 revNoteBuilder .setNote (revNote );
142124 privateTRC20Builder .addShieldedReceives (revNoteBuilder .build ());
143125
126+ //ReceiveNote 2
127+ GrpcAPI .ReceiveNote .Builder revNoteBuilder2 = GrpcAPI .ReceiveNote .newBuilder ();
128+ PaymentAddress paymentAddress2 = incomingViewingKey .address (new DiversifierT ().random ()).get ();
129+ String paymentAddressStr2 = KeyIo .encodePaymentAddress (paymentAddress2 );
130+ long revValue2 = 1 ;
131+ byte [] memo2 = new byte [512 ];
132+ byte [] rcm2 = WalletApi .getRcm ().get ().getValue ().toByteArray ();
133+
134+ GrpcAPI .Note revNote2 = getNote (revValue2 , paymentAddressStr2 , rcm2 , memo2 );
135+ revNoteBuilder2 .setNote (revNote2 );
136+ privateTRC20Builder .addShieldedReceives (revNoteBuilder2 .build ());
137+
138+
144139 ExpandedSpendingKey expsk = sk .expandedSpendingKey ();
145140 privateTRC20Builder .setAsk (ByteString .copyFrom (expsk .getAsk ()));
146141 privateTRC20Builder .setNsk (ByteString .copyFrom (expsk .getNsk ()));
147142 privateTRC20Builder .setOvk (ByteString .copyFrom (expsk .getOvk ()));
148143 privateTRC20Builder .setShieldedTRC20ContractAddress (ByteString .copyFrom (contractAddress ));
149- GrpcAPI .ShieldedTRC20Parameters transferParam = grpcClient
144+ GrpcAPI .ShieldedTRC20Parameters transferParam = WalletApi
150145 .createShieldedContractParameters (privateTRC20Builder .build ());
151146 triggerTransfer (contractAddress , callerAddress , privateKey ,
152147 transferParam .getTriggerContractInput ());
@@ -158,45 +153,30 @@ public static void burnDemo() throws ZksnarkException, InterruptedException {
158153 byte [] callerAddress = WalletApi .decodeFromBase58Check (pubAddress );
159154 SpendingKey sk = new SpendingKey (ByteArray .fromHexString (spendingKey ));
160155 GrpcAPI .PrivateShieldedTRC20Parameters mintPrivateParam1 = mintParams (
161- privateKey , 10 , shieldedTRC20ContractAddress );
162- setAllowance (10 );
163- GrpcAPI .ShieldedTRC20Parameters mintParam1 = grpcClient .createShieldedContractParameters (
156+ privateKey , 1 , shieldedTRC20ContractAddress );
157+ setAllowance (1 );
158+ GrpcAPI .ShieldedTRC20Parameters mintParam1 = WalletApi .createShieldedContractParameters (
164159 mintPrivateParam1 );
165- long value = 10 ;
166160 String mintInput1 = mintParam1 .getTriggerContractInput ();
167161 String txid = triggerMint (contractAddress , callerAddress , privateKey , mintInput1 );
168162
169163 // SpendNoteTRC20 1
170- Optional <TransactionInfo > infoById = grpcClient .getTransactionInfoById (txid );
171- while (infoById .get ().getLogList ().size () < 2 ) {
172- logger .info ("Can not get transaction info, please wait...." );
173- Thread .sleep (5000 );
174- infoById = grpcClient .getTransactionInfoById (txid );
175- }
176- byte [] tx1Data = infoById .get ().getLog (1 ).getData ().toByteArray ();
177- long pos = bytes32Tolong (ByteArray .subArray (tx1Data , 0 , 32 ));
178- byte [] contractResult = triggerGetPath (contractAddress , pos );
179- byte [] path = ByteArray .subArray (contractResult , 32 , 1056 );
180- byte [] root = ByteArray .subArray (contractResult , 0 , 32 );
181- GrpcAPI .SpendNoteTRC20 .Builder note1Builder = GrpcAPI .SpendNoteTRC20 .newBuilder ();
182- note1Builder .setAlpha (ByteString .copyFrom (grpcClient .getRcm ().getValue ().toByteArray ()));
183- note1Builder .setPos (pos );
184- note1Builder .setPath (ByteString .copyFrom (path ));
185- note1Builder .setRoot (ByteString .copyFrom (root ));
186- note1Builder .setNote (mintPrivateParam1 .getShieldedReceives (0 ).getNote ());
164+ Optional <TransactionInfo > infoById = waitToGetTransactionInfo (txid );
187165 GrpcAPI .PrivateShieldedTRC20Parameters .Builder privateTRC20Builder = GrpcAPI
188166 .PrivateShieldedTRC20Parameters .newBuilder ();
189- privateTRC20Builder .addShieldedSpends (note1Builder .build ());
167+ privateTRC20Builder
168+ .addShieldedSpends (getSpendNote (infoById .get (), mintPrivateParam1 , contractAddress ));
190169
191170 ExpandedSpendingKey expsk = sk .expandedSpendingKey ();
192171 privateTRC20Builder .setAsk (ByteString .copyFrom (expsk .getAsk ()));
193172 privateTRC20Builder .setNsk (ByteString .copyFrom (expsk .getNsk ()));
194173 privateTRC20Builder .setOvk (ByteString .copyFrom (expsk .getOvk ()));
195- BigInteger toAmount = BigInteger .valueOf (10 ).multiply (scalingFactorBi );
174+ BigInteger toAmount = BigInteger .valueOf (1 ).multiply (scalingFactorBi );
196175 privateTRC20Builder .setToAmount (toAmount .toString ());
197176 privateTRC20Builder .setTransparentToAddress (ByteString .copyFrom (callerAddress ));
198177 privateTRC20Builder .setShieldedTRC20ContractAddress (ByteString .copyFrom (contractAddress ));
199- GrpcAPI .ShieldedTRC20Parameters burnParam = grpcClient .createShieldedContractParameters (privateTRC20Builder .build ());
178+ GrpcAPI .ShieldedTRC20Parameters burnParam = WalletApi
179+ .createShieldedContractParameters (privateTRC20Builder .build ());
200180
201181 triggerBurn (contractAddress , callerAddress , privateKey ,
202182 burnParam .getTriggerContractInput ());
@@ -236,7 +216,7 @@ private static String triggerTransfer(
236216 "0" ,
237217 0 ,
238218 callerAddress , privateKey );
239- Optional <TransactionInfo > infoById = grpcClient .getTransactionInfoById (txid );
219+ Optional <TransactionInfo > infoById = WalletApi .getTransactionInfoById (txid );
240220 return txid ;
241221 }
242222
@@ -341,7 +321,7 @@ private static String triggerContract(byte[] contractAddress, String method, Str
341321 String txid = ByteArray .toHexString (Sha256Sm3Hash .hash (
342322 transaction .getRawData ().toByteArray ()));
343323 System .out .println ("trigger txid = " + txid );
344- grpcClient .broadcastTransaction (transaction );
324+ WalletApi .broadcastTransaction (transaction );
345325 return txid ;
346326 }
347327
@@ -430,7 +410,7 @@ private static GrpcAPI.PrivateShieldedTRC20Parameters mintParams(String privKey,
430410 IncomingViewingKey incomingViewingKey = fullViewingKey .inViewingKey ();
431411 PaymentAddress paymentAddress = incomingViewingKey .address (new DiversifierT ().random ()).get ();
432412 byte [] memo = new byte [512 ];
433- byte [] rcm = grpcClient .getRcm ().getValue ().toByteArray ();
413+ byte [] rcm = WalletApi .getRcm (). get ().getValue ().toByteArray ();
434414 String paymentAddressStr = KeyIo .encodePaymentAddress (paymentAddress );
435415 GrpcAPI .Note revNote = getNote (value , paymentAddressStr , rcm , memo );
436416 revNoteBuilder .setNote (revNote );
@@ -465,13 +445,41 @@ private static byte[] triggerGetPath(byte[] contractAddress, long pos) {
465445 return result ;
466446 }
467447
448+ private static Optional <TransactionInfo > waitToGetTransactionInfo (String txid )
449+ throws InterruptedException {
450+ logger .info ("mint txid: " + txid );
451+ Optional <TransactionInfo > infoById = WalletApi .getTransactionInfoById (txid );
452+ while (infoById .get ().getLogList ().size () < 2 ) {
453+ logger .info ("Can not get transaction info, please wait...." );
454+ Thread .sleep (5000 );
455+ return WalletApi .getTransactionInfoById (txid );
456+ }
457+ return null ;
458+ }
459+
460+ private static SpendNoteTRC20 getSpendNote (TransactionInfo txInfo ,
461+ PrivateShieldedTRC20Parameters mintPrivateParam1 , byte [] contractAddress ) {
462+ byte [] tx1Data = txInfo .getLog (1 ).getData ().toByteArray ();
463+ long pos = bytes32ToLong (ByteArray .subArray (tx1Data , 0 , 32 ));
464+ byte [] contractResult = triggerGetPath (contractAddress , pos );
465+ byte [] path = ByteArray .subArray (contractResult , 32 , 1056 );
466+ byte [] root = ByteArray .subArray (contractResult , 0 , 32 );
467+ GrpcAPI .SpendNoteTRC20 .Builder noteBuilder = GrpcAPI .SpendNoteTRC20 .newBuilder ();
468+ noteBuilder .setAlpha (ByteString .copyFrom (WalletApi .getRcm ().get ().getValue ().toByteArray ()));
469+ noteBuilder .setPos (pos );
470+ noteBuilder .setPath (ByteString .copyFrom (path ));
471+ noteBuilder .setRoot (ByteString .copyFrom (root ));
472+ noteBuilder .setNote (mintPrivateParam1 .getShieldedReceives (0 ).getNote ());
473+ return noteBuilder .build ();
474+ }
475+
468476 private static byte [] longTo32Bytes (long value ) {
469477 byte [] longBytes = ByteArray .fromLong (value );
470478 byte [] zeroBytes = new byte [24 ];
471479 return ByteUtil .merge (zeroBytes , longBytes );
472480 }
473481
474- private static long bytes32Tolong (byte [] value ) {
482+ private static long bytes32ToLong (byte [] value ) {
475483 return ByteArray .toLong (value );
476484 }
477485}
0 commit comments