Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 24 additions & 33 deletions src/test/java/stest/tron/wallet/common/client/utils/AbiUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,33 @@
import org.spongycastle.util.encoders.Hex;
import org.tron.common.crypto.Hash;
import org.tron.core.Wallet;
//import org.tron.walletserver.WalletClient;

public class AbiUtil {

static Pattern paramTypeBytes = Pattern.compile("^bytes([0-9]*)$");
static Pattern paramTypeNumber = Pattern.compile("^(u?int)([0-9]*)$");
static Pattern paramTypeArray = Pattern.compile("^(.*)\\[([0-9]*)\\]$");
//

static abstract class Coder {
boolean dynamic = false;
String name;
String type;

// DataWord[] encode
abstract byte[] encode(String value);

abstract byte[] decode();

}

class Paramater {
String type;
}

public static String[] getTypes(String methodSign) {
int start = methodSign.indexOf('(') + 1;
int end = methodSign.indexOf(')');

String typeSring = methodSign.subSequence(start,end).toString();
String typeString = methodSign.subSequence(start,end).toString();

return typeSring.split(",");
return typeString.split(",");
}

public static String geMethodId(String methodSign) {
Expand All @@ -58,20 +55,15 @@ public static Coder getParamCoder(String type) {
return new CoderDynamicBytes();
}

boolean match = false;

if (type.matches("^bytes([0-9]*)$")) {
if (paramTypeBytes.matcher(type).find()) {
return new CoderFixedBytes();
}


if (type.matches("^(u?int)([0-9]*)$")) {
if (paramTypeNumber.matcher(type).find()) {
return new CoderNumber();
}


Pattern r = Pattern.compile("^(.*)\\[([0-9]*)]$");
Matcher m = r.matcher(type);
Matcher m = paramTypeArray.matcher(type);
if (m.find()) {
String arrayType = m.group(1);
int length = -1;
Expand All @@ -87,7 +79,7 @@ static class CoderArray extends Coder {
private String elementType;
private int length;

public CoderArray(String arrayType, int length) {
CoderArray(String arrayType, int length) {
this.elementType = arrayType;
this.length = length;
if (length == -1) {
Expand All @@ -101,13 +93,13 @@ byte[] encode(String arrayValues) {

Coder coder = getParamCoder(elementType);


List<Object> strings = null;
try {
ObjectMapper mapper = new ObjectMapper();
strings = mapper.readValue(arrayValues, List.class);
} catch (IOException e) {
e.printStackTrace();
return null;
}

List<Coder> coders = new ArrayList<>();
Expand All @@ -122,16 +114,9 @@ byte[] encode(String arrayValues) {
}
}


if (this.length == -1) {
System.out.println("array encoded");
System.out.println(Hex.toHexString(concat(new DataWord(strings.size()).getData(),
pack(coders, strings))));
System.out.println("fdsfsdf");
return concat(new DataWord(strings.size()).getData(), pack(coders, strings));
} else {
System.out.println(Hex.toHexString(pack(coders, strings)));

return pack(coders, strings);
}
}
Expand Down Expand Up @@ -327,21 +312,27 @@ public static String parseMethod(String methodSign, String params) {
return parseMethod(methodSign, params, false);
}

public static String parseMethod(String methodSign, String params, boolean isHex) {
public static String parseMethod(String methodSign, String input, boolean isHex) {
byte[] selector = new byte[4];
System.arraycopy(Hash.sha3(methodSign.getBytes()), 0, selector,0, 4);
System.out.println(methodSign + ":" + Hex.toHexString(selector));
if (params.length() == 0) {
if (input.length() == 0) {
return Hex.toHexString(selector);
}
if (isHex) {
return Hex.toHexString(selector) + params;
return Hex.toHexString(selector) + input;
}
byte[] encodedParms = encodeInput(methodSign, input);

return Hex.toHexString(selector) + Hex.toHexString(encodedParms);
}

public static byte[] encodeInput(String methodSign, String input) {
ObjectMapper mapper = new ObjectMapper();
params = "[" + params + "]";
List<Object> strings = null;
input = "[" + input + "]";
List<Object> items = null;
try {
strings = mapper.readValue(params, List.class);
items = mapper.readValue(input, List.class);
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -352,12 +343,11 @@ public static String parseMethod(String methodSign, String params, boolean isHex
coders.add(c);
}

byte[] encodedParms = pack(coders, strings);

return Hex.toHexString(selector) + Hex.toHexString(encodedParms);
return pack(coders, items);
}

public static void main(String[] args) {
// String method = "test(address,string,int)";
String method = "test(string,int2,string)";
String params = "asdf,3123,adf";

Expand All @@ -378,6 +368,7 @@ public static void main(String[] args) {
String bytesValue2 = "123123123";

System.out.println(parseMethod(byteMethod1, bytesValue1));

}

public static byte[] concat(byte[]... bytesArray) {
Expand Down
135 changes: 126 additions & 9 deletions src/test/java/stest/tron/wallet/common/client/utils/PublicMethed.java
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ public static byte[] deployContract(String contractName, String abiString, Strin
logger.info("Message = " + response.getMessage().toStringUtf8());
return null;
} else {
logger.info("brodacast succesfully");
//logger.info("brodacast succesfully");
return contractAddress;
}
}
Expand Down Expand Up @@ -1101,9 +1101,7 @@ public static String deployContractAndGetTransactionInfoById(String contractName
texBuilder.setTxid(transactionExtention.getTxid());
transactionExtention = texBuilder.build();

byte[] contractAddress = generateContractAddress(transactionExtention.getTransaction(), owner);
System.out.println(
"Your smart contract address will be: " + WalletClient.encode58Check(contractAddress));

if (transactionExtention == null) {
return null;
}
Expand All @@ -1121,7 +1119,7 @@ public static String deployContractAndGetTransactionInfoById(String contractName
transaction = signTransaction(ecKey, transaction);
System.out.println(
"txid = " + ByteArray.toHexString(Sha256Hash.hash(transaction.getRawData().toByteArray())));
contractAddress = generateContractAddress(transaction, owner);
byte[] contractAddress = generateContractAddress(transaction, owner);
System.out.println(
"Your smart contract address will be: " + WalletClient.encode58Check(contractAddress));
int i = 10;
Expand All @@ -1142,7 +1140,7 @@ public static String deployContractAndGetTransactionInfoById(String contractName
logger.info("Message = " + response.getMessage().toStringUtf8());
return null;
} else {
logger.info("brodacast succesfully");
//logger.info("brodacast succesfully");
return ByteArray.toHexString(Sha256Hash.hash(transaction.getRawData().toByteArray()));
}
}
Expand Down Expand Up @@ -1298,9 +1296,9 @@ public static SmartContract getContract(byte[] address, WalletGrpc
ByteString byteString = ByteString.copyFrom(address);
BytesMessage bytesMessage = BytesMessage.newBuilder().setValue(byteString).build();
Integer i = 0;
while (blockingStubFull.getContract(bytesMessage).getName().isEmpty() && i++ < 7) {
while (blockingStubFull.getContract(bytesMessage).getName().isEmpty() && i++ < 4) {
try {
Thread.sleep(3000);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -1490,7 +1488,7 @@ public static String triggerContract(byte[] contractAddress, String method, Stri
logger.info("Message = " + response.getMessage().toStringUtf8());
return null;
} else {
logger.info("brodacast succesfully");
//logger.info("brodacast succesfully");
return ByteArray.toHexString(Sha256Hash.hash(transaction.getRawData().toByteArray()));
}
}
Expand Down Expand Up @@ -1762,4 +1760,123 @@ public static boolean exchangeTransaction(long exchangeId, byte[] tokenId, long
}


public static String deployContractWithConstantParame(String contractName, String abiString,
String code, String constructorStr, String argsStr,String data, Long feeLimit, long value,
long consumeUserResourcePercent, byte[] libraryAddress, String priKey, byte[] ownerAddress,
WalletGrpc.WalletBlockingStub blockingStubFull) {
Wallet.setAddressPreFixByte(CommonConstant.ADD_PRE_FIX_BYTE_MAINNET);
ECKey temKey = null;
try {
BigInteger priK = new BigInteger(priKey, 16);
temKey = ECKey.fromPrivate(priK);
} catch (Exception ex) {
ex.printStackTrace();
}
final ECKey ecKey = temKey;

SmartContract.ABI abi = jsonStr2Abi(abiString);
if (abi == null) {
logger.error("abi is null");
return null;
}

code += Hex.toHexString(AbiUtil.encodeInput(constructorStr, argsStr));
byte[] owner = ownerAddress;
SmartContract.Builder builder = SmartContract.newBuilder();
builder.setName(contractName);
builder.setOriginAddress(ByteString.copyFrom(owner));
builder.setAbi(abi);
builder.setConsumeUserResourcePercent(consumeUserResourcePercent);

if (value != 0) {

builder.setCallValue(value);
}

byte[] byteCode;
if (null != libraryAddress) {
byteCode = replaceLibraryAddress(code, libraryAddress);
} else {
byteCode = Hex.decode(code);
}
builder.setBytecode(ByteString.copyFrom(byteCode));

CreateSmartContract contractDeployContract = CreateSmartContract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(owner)).setNewContract(builder.build()).build();

TransactionExtention transactionExtention = blockingStubFull
.deployContract(contractDeployContract);
if (transactionExtention == null || !transactionExtention.getResult().getResult()) {
System.out.println("RPC create trx failed!");
if (transactionExtention != null) {
System.out.println("Code = " + transactionExtention.getResult().getCode());
System.out
.println("Message = " + transactionExtention.getResult().getMessage().toStringUtf8());
}
return null;
}

final TransactionExtention.Builder texBuilder = TransactionExtention.newBuilder();
Transaction.Builder transBuilder = Transaction.newBuilder();
Transaction.raw.Builder rawBuilder = transactionExtention.getTransaction().getRawData()
.toBuilder();
rawBuilder.setFeeLimit(feeLimit);
transBuilder.setRawData(rawBuilder);
for (int i = 0; i < transactionExtention.getTransaction().getSignatureCount(); i++) {
ByteString s = transactionExtention.getTransaction().getSignature(i);
transBuilder.setSignature(i, s);
}
for (int i = 0; i < transactionExtention.getTransaction().getRetCount(); i++) {
Result r = transactionExtention.getTransaction().getRet(i);
transBuilder.setRet(i, r);
}
texBuilder.setTransaction(transBuilder);
texBuilder.setResult(transactionExtention.getResult());
texBuilder.setTxid(transactionExtention.getTxid());
transactionExtention = texBuilder.build();

if (transactionExtention == null) {
return null;
}
Return ret = transactionExtention.getResult();
if (!ret.getResult()) {
System.out.println("Code = " + ret.getCode());
System.out.println("Message = " + ret.getMessage().toStringUtf8());
return null;
}
Transaction transaction = transactionExtention.getTransaction();
if (transaction == null || transaction.getRawData().getContractCount() == 0) {
System.out.println("Transaction is empty");
return null;
}
transaction = signTransaction(ecKey, transaction);
System.out.println(
"txid = " + ByteArray.toHexString(Sha256Hash.hash(transaction.getRawData().toByteArray())));
byte[] contractAddress = generateContractAddress(transaction, owner);
System.out.println(
"Your smart contract address will be: " + WalletClient.encode58Check(contractAddress));
int i = 10;
GrpcAPI.Return response = blockingStubFull.broadcastTransaction(transaction);
while (response.getResult() == false && response.getCode() == response_code.SERVER_BUSY
&& i > 0) {
i--;
response = blockingStubFull.broadcastTransaction(transaction);
logger.info("repeate times = " + (11 - i));
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (response.getResult() == false) {
logger.info("Code = " + response.getCode());
logger.info("Message = " + response.getMessage().toStringUtf8());
return null;
} else {
//logger.info("brodacast succesfully");
return ByteArray.toHexString(Sha256Hash.hash(transaction.getRawData().toByteArray()));
}
}


}
Loading