public static RawTransaction createTransaction( BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, String to, BigInteger value, String data) { return new RawTransaction(nonce, gasPrice, gasLimit, to, value, data); }
@Override public EthSendTransaction sendTransaction( BigInteger gasPrice, BigInteger gasLimit, String to, String data, BigInteger value) throws IOException { BigInteger nonce = getNonce(); RawTransaction rawTransaction = RawTransaction.createTransaction( nonce, gasPrice, gasLimit, to, value, data); return signAndSend(rawTransaction); }
public static RawTransaction decode(String hexTransaction) { byte[] transaction = Numeric.hexStringToByteArray(hexTransaction); RlpList rlpList = RlpDecoder.decode(transaction); RlpList values = (RlpList) rlpList.getValues().get(0); BigInteger nonce = ((RlpString) values.getValues().get(0)).asPositiveBigInteger(); BigInteger gasPrice = ((RlpString) values.getValues().get(1)).asPositiveBigInteger(); BigInteger gasLimit = ((RlpString) values.getValues().get(2)).asPositiveBigInteger(); String to = ((RlpString) values.getValues().get(3)).asString(); BigInteger value = ((RlpString) values.getValues().get(4)).asPositiveBigInteger(); String data = ((RlpString) values.getValues().get(5)).asString(); if (values.getValues().size() > 6) { byte v = ((RlpString) values.getValues().get(6)).getBytes()[0]; byte[] r = Numeric.toBytesPadded( Numeric.toBigInt(((RlpString) values.getValues().get(7)).getBytes()), 32); byte[] s = Numeric.toBytesPadded( Numeric.toBigInt(((RlpString) values.getValues().get(8)).getBytes()), 32); Sign.SignatureData signatureData = new Sign.SignatureData(v, r, s); return new SignedRawTransaction(nonce, gasPrice, gasLimit, to, value, data, signatureData); } else { return RawTransaction.createTransaction(nonce, gasPrice, gasLimit, to, value, data); } }
private String execute( Credentials credentials, Function function, String contractAddress) throws Exception { BigInteger nonce = getNonce(credentials.getAddress()); String encodedFunction = FunctionEncoder.encode(function); RawTransaction rawTransaction = RawTransaction.createTransaction( nonce, GAS_PRICE, GAS_LIMIT, contractAddress, encodedFunction); byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); String hexValue = Numeric.toHexString(signedMessage); EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue) .sendAsync().get(); return transactionResponse.getTransactionHash(); }
public static RawTransaction createTransaction( BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, String to, BigInteger value, String data) { return new RawTransaction(nonce, gasPrice, gasLimit, to, value, data); }
private void sign(String toAddress, String message, DltTransactionRequest dltTransaction) { RawTransaction rawTransaction = RawTransaction.createTransaction( this.nonce = null != dltTransaction.getSequence() ? BigInteger.valueOf(dltTransaction.getSequence()) : this.nonce, Optional.ofNullable(dltTransaction.getFee()).orElse(EthGasStation.getInstance().calculate(FEE_POLICY.NORMAL)), Optional.ofNullable(dltTransaction.getFeeLimit()).orElse(this.gasLimit), toAddress, dltTransaction.getAmount(), message ); byte transactionSignedBytes[] = TransactionEncoder.signMessage(rawTransaction, Credentials.create(this.ecKeyPair)); dltTransaction.setSignedTransaction(Numeric.toHexString(transactionSignedBytes)); this.nonce = this.nonce.add(BigInteger.ONE); }
/** * 签名交易 */ public static String signTransaction(BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, String to, BigInteger value, String data, byte chainId, String privateKey) throws IOException { byte[] signedMessage; RawTransaction rawTransaction = RawTransaction.createTransaction( nonce, gasPrice, gasLimit, to, value, data); if (privateKey.startsWith("0x")) { privateKey = privateKey.substring(2); } ECKeyPair ecKeyPair = ECKeyPair.create(new BigInteger(privateKey, 16)); Credentials credentials = Credentials.create(ecKeyPair); if (chainId > ChainId.NONE) { signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials); } else { signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); } String hexValue = Numeric.toHexString(signedMessage); return hexValue; }
public String signContractTransaction(String contractAddress, String to, BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, BigDecimal amount, BigDecimal decimal, WalletFile walletfile, String password) throws Exception { BigDecimal realValue = amount.multiply(decimal); Function function = new Function("transfer", Arrays.asList(new Address(to), new Uint256(realValue.toBigInteger())), Collections.emptyList()); String data = FunctionEncoder.encode(function); RawTransaction rawTransaction = RawTransaction.createTransaction( nonce, gasPrice, gasLimit, contractAddress, data); return signData(rawTransaction, walletfile, password); }
private static void decodeMessage(String signedData) { //样例 https://ropsten.etherscan.io/tx/0xfd8acd10d72127f29f0a01d8bcaf0165665b5598781fe01ca4bceaa6ab9f2cb0 try { System.out.println(signedData); System.out.println("解密 start " + System.currentTimeMillis()); RlpList rlpList = RlpDecoder.decode(Numeric.hexStringToByteArray(signedData)); List<RlpType> values = ((RlpList) rlpList.getValues().get(0)).getValues(); BigInteger nonce = Numeric.toBigInt(((RlpString) values.get(0)).getBytes()); BigInteger gasPrice = Numeric.toBigInt(((RlpString) values.get(1)).getBytes()); BigInteger gasLimit = Numeric.toBigInt(((RlpString) values.get(2)).getBytes()); String to = Numeric.toHexString(((RlpString) values.get(3)).getBytes()); BigInteger value = Numeric.toBigInt(((RlpString) values.get(4)).getBytes()); String data = Numeric.toHexString(((RlpString) values.get(5)).getBytes()); RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, to, value, data); RlpString v = (RlpString) values.get(6); RlpString r = (RlpString) values.get(7); RlpString s = (RlpString) values.get(8); Sign.SignatureData signatureData = new Sign.SignatureData( v.getBytes()[0], Numeric.toBytesPadded(Numeric.toBigInt(r.getBytes()), 32), Numeric.toBytesPadded(Numeric.toBigInt(s.getBytes()), 32)); BigInteger pubKey = Sign.signedMessageToKey(TransactionEncoder.encode(rawTransaction), signatureData); System.out.println("publicKey " + pubKey.toString(16)); String address = Numeric.prependHexPrefix(Keys.getAddress(pubKey)); System.out.println("address " + address); System.out.println("解密 end " + System.currentTimeMillis()); } catch (Exception e) { e.printStackTrace(); } }
public static RawTransaction decode(String hexTransaction) { byte[] transaction = Numeric.hexStringToByteArray(hexTransaction); RlpList rlpList = RlpDecoder.decode(transaction); RlpList values = (RlpList) rlpList.getValues().get(0); BigInteger nonce = ((RlpString) values.getValues().get(0)).asPositiveBigInteger(); BigInteger gasPrice = ((RlpString) values.getValues().get(1)).asPositiveBigInteger(); BigInteger gasLimit = ((RlpString) values.getValues().get(2)).asPositiveBigInteger(); String to = ((RlpString) values.getValues().get(3)).asString(); BigInteger value = ((RlpString) values.getValues().get(4)).asPositiveBigInteger(); String data = ((RlpString) values.getValues().get(5)).asString(); if (values.getValues().size() > 6) { byte v = ((RlpString) values.getValues().get(6)).getBytes()[0]; byte[] r = Numeric.toBytesPadded( Numeric.toBigInt(((RlpString) values.getValues().get(7)).getBytes()), 32); byte[] s = Numeric.toBytesPadded( Numeric.toBigInt(((RlpString) values.getValues().get(8)).getBytes()), 32); Sign.SignatureData signatureData = new Sign.SignatureData(v, r, s); return new SignedRawTransaction(nonce, gasPrice, gasLimit, to, value, data, signatureData); } else { return RawTransaction.createTransaction(nonce, gasPrice, gasLimit, to, value, data); } }
private String execute( Credentials credentials, Function function, String contractAddress) throws Exception { BigInteger nonce = mWeb3j.ethGetTransactionCount(mAddress, DefaultBlockParameterName.LATEST).send().getTransactionCount(); BigInteger gasPrice = mWeb3j.ethGasPrice().send().getGasPrice(); BigInteger gasLimit = new BigInteger("200000"); String encodedFunction = FunctionEncoder.encode(function); RawTransaction rawTransaction = RawTransaction.createTransaction( nonce, gasPrice, gasLimit, contractAddress, encodedFunction); byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); String hexValue = Numeric.toHexString(signedMessage); EthSendTransaction transactionResponse = mWeb3j.ethSendRawTransaction(hexValue) .sendAsync().get(); return transactionResponse.getTransactionHash(); } }