@@ -407,7 +407,22 @@ public static Protocol.Transaction signTransaction(ECKey ecKey,
407
407
return null ;
408
408
}
409
409
transaction = TransactionUtils .setTimestamp (transaction );
410
- return TransactionUtils .sign (transaction , ecKey );
410
+ //return TransactionUtils.sign(transaction, ecKey);
411
+ String mainGateWay = "TYYrjz9W9ii98zMEF7KoL24KhGRXqWpjEJ" ;
412
+ return TransactionUtils
413
+ .sign (transaction , ecKey , WalletClient .decodeFromBase58Check (mainGateWay ), false );
414
+
415
+ }
416
+
417
+ public static Protocol .Transaction signTransaction (ECKey ecKey ,
418
+ Protocol .Transaction transaction , byte [] mainGateWay , boolean isSideChain ) {
419
+ Wallet .setAddressPreFixByte (CommonConstant .ADD_PRE_FIX_BYTE_MAINNET );
420
+ if (ecKey == null || ecKey .getPrivKey () == null ) {
421
+ //logger.warn("Warning: Can't sign,there is no private key !!");
422
+ return null ;
423
+ }
424
+ transaction = TransactionUtils .setTimestamp (transaction );
425
+ return TransactionUtils .sign (transaction , ecKey , mainGateWay , isSideChain );
411
426
}
412
427
413
428
/**
@@ -1808,7 +1823,142 @@ public static byte[] deployContract(String contractName, String abiString, Strin
1808
1823
System .out .println ("Transaction is empty" );
1809
1824
return null ;
1810
1825
}
1811
- transaction = signTransaction (ecKey , transaction );
1826
+ //transaction = signTransaction(ecKey, transaction);
1827
+ String mainGatewayAddress = "TYYrjz9W9ii98zMEF7KoL24KhGRXqWpjEJ" ;
1828
+ transaction = signTransaction (ecKey , transaction ,
1829
+ WalletClient .decodeFromBase58Check (mainGatewayAddress ), false );
1830
+ System .out .println (
1831
+ "txid = " + ByteArray .toHexString (Sha256Hash .hash (transaction .getRawData ().toByteArray ())));
1832
+ contractAddress = generateContractAddress (transaction , owner );
1833
+ System .out .println (
1834
+ "Your smart contract address will be: " + WalletClient .encode58Check (contractAddress ));
1835
+
1836
+ int i = 10 ;
1837
+ GrpcAPI .Return response = blockingStubFull .broadcastTransaction (transaction );
1838
+ while (response .getResult () == false && response .getCode () == response_code .SERVER_BUSY
1839
+ && i > 0 ) {
1840
+ i --;
1841
+ response = blockingStubFull .broadcastTransaction (transaction );
1842
+ logger .info ("repeate times = " + (11 - i ));
1843
+ try {
1844
+ Thread .sleep (300 );
1845
+ } catch (InterruptedException e ) {
1846
+ e .printStackTrace ();
1847
+ }
1848
+ }
1849
+ if (response .getResult () == false ) {
1850
+ logger .info ("Code = " + response .getCode ());
1851
+ logger .info ("Message = " + response .getMessage ().toStringUtf8 ());
1852
+ return null ;
1853
+ } else {
1854
+ //logger.info("brodacast succesfully");
1855
+ return contractAddress ;
1856
+ }
1857
+ }
1858
+
1859
+ /**
1860
+ * constructor.
1861
+ */
1862
+
1863
+ public static byte [] deploySideContract (String contractName , String abiString , String code ,
1864
+ String data , Long feeLimit , long value ,
1865
+ long consumeUserResourcePercent , long originEnergyLimit , String tokenId , long tokenValue ,
1866
+ String libraryAddress , String priKey , byte [] ownerAddress , String mainGatewayAddress ,
1867
+ WalletGrpc .WalletBlockingStub blockingStubFull ) {
1868
+ Wallet .setAddressPreFixByte (CommonConstant .ADD_PRE_FIX_BYTE_MAINNET );
1869
+ ECKey temKey = null ;
1870
+ try {
1871
+ BigInteger priK = new BigInteger (priKey , 16 );
1872
+ temKey = ECKey .fromPrivate (priK );
1873
+ } catch (Exception ex ) {
1874
+ ex .printStackTrace ();
1875
+ }
1876
+ final ECKey ecKey = temKey ;
1877
+
1878
+ byte [] owner = ownerAddress ;
1879
+ SmartContract .ABI abi = jsonStr2Abi (abiString );
1880
+ if (abi == null ) {
1881
+ logger .error ("abi is null" );
1882
+ return null ;
1883
+ }
1884
+ //byte[] codeBytes = Hex.decode(code);
1885
+ SmartContract .Builder builder = SmartContract .newBuilder ();
1886
+ builder .setName (contractName );
1887
+ builder .setOriginAddress (ByteString .copyFrom (owner ));
1888
+ builder .setAbi (abi );
1889
+ builder .setConsumeUserResourcePercent (consumeUserResourcePercent );
1890
+ builder .setOriginEnergyLimit (originEnergyLimit );
1891
+
1892
+ if (value != 0 ) {
1893
+
1894
+ builder .setCallValue (value );
1895
+ }
1896
+
1897
+ byte [] byteCode ;
1898
+ if (null != libraryAddress ) {
1899
+ byteCode = replaceLibraryAddress (code , libraryAddress );
1900
+ } else {
1901
+ byteCode = Hex .decode (code );
1902
+ }
1903
+ builder .setBytecode (ByteString .copyFrom (byteCode ));
1904
+
1905
+ Builder contractBuilder = CreateSmartContract .newBuilder ();
1906
+ contractBuilder .setOwnerAddress (ByteString .copyFrom (owner ));
1907
+ contractBuilder .setCallTokenValue (tokenValue );
1908
+ contractBuilder .setTokenId (Long .parseLong (tokenId ));
1909
+ CreateSmartContract contractDeployContract = contractBuilder
1910
+ .setNewContract (builder .build ()).build ();
1911
+
1912
+ TransactionExtention transactionExtention = blockingStubFull
1913
+ .deployContract (contractDeployContract );
1914
+ if (transactionExtention == null || !transactionExtention .getResult ().getResult ()) {
1915
+ System .out .println ("RPC create trx failed!" );
1916
+ if (transactionExtention != null ) {
1917
+ System .out .println ("Code = " + transactionExtention .getResult ().getCode ());
1918
+ System .out
1919
+ .println ("Message = " + transactionExtention .getResult ().getMessage ().toStringUtf8 ());
1920
+ }
1921
+ return null ;
1922
+ }
1923
+
1924
+ final TransactionExtention .Builder texBuilder = TransactionExtention .newBuilder ();
1925
+ Transaction .Builder transBuilder = Transaction .newBuilder ();
1926
+ Transaction .raw .Builder rawBuilder = transactionExtention .getTransaction ().getRawData ()
1927
+ .toBuilder ();
1928
+ rawBuilder .setFeeLimit (feeLimit );
1929
+ transBuilder .setRawData (rawBuilder );
1930
+ for (int i = 0 ; i < transactionExtention .getTransaction ().getSignatureCount (); i ++) {
1931
+ ByteString s = transactionExtention .getTransaction ().getSignature (i );
1932
+ transBuilder .setSignature (i , s );
1933
+ }
1934
+ for (int i = 0 ; i < transactionExtention .getTransaction ().getRetCount (); i ++) {
1935
+ Result r = transactionExtention .getTransaction ().getRet (i );
1936
+ transBuilder .setRet (i , r );
1937
+ }
1938
+ texBuilder .setTransaction (transBuilder );
1939
+ texBuilder .setResult (transactionExtention .getResult ());
1940
+ texBuilder .setTxid (transactionExtention .getTxid ());
1941
+ transactionExtention = texBuilder .build ();
1942
+
1943
+ byte [] contractAddress = generateContractAddress (transactionExtention .getTransaction (), owner );
1944
+ System .out .println (
1945
+ "Your smart contract address will be: " + WalletClient .encode58Check (contractAddress ));
1946
+ if (transactionExtention == null ) {
1947
+ return null ;
1948
+ }
1949
+ Return ret = transactionExtention .getResult ();
1950
+ if (!ret .getResult ()) {
1951
+ System .out .println ("Code = " + ret .getCode ());
1952
+ System .out .println ("Message = " + ret .getMessage ().toStringUtf8 ());
1953
+ return null ;
1954
+ }
1955
+ Transaction transaction = transactionExtention .getTransaction ();
1956
+ if (transaction == null || transaction .getRawData ().getContractCount () == 0 ) {
1957
+ System .out .println ("Transaction is empty" );
1958
+ return null ;
1959
+ }
1960
+ transaction = signTransaction (ecKey , transaction ,
1961
+ WalletClient .decodeFromBase58Check (mainGatewayAddress ), false );
1812
1962
System .out .println (
1813
1963
"txid = " + ByteArray .toHexString (Sha256Hash .hash (transaction .getRawData ().toByteArray ())));
1814
1964
contractAddress = generateContractAddress (transaction , owner );
0 commit comments