public String sign(RawTransaction rawTransaction) { byte[] signedMessage; if (chainId > ChainId.NONE) { signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials); } else { signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); } return Numeric.toHexString(signedMessage); }
/** * Utility method to provide the transaction hash for a given transaction. * * @param rawTransaction we wish to send * @param credentials of the sender * @return encoded transaction hash */ public static byte[] generateTransactionHash( RawTransaction rawTransaction, Credentials credentials) { byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); return Hash.sha3(signedMessage); }
/** * Utility method to provide the transaction hash for a given transaction. * * @param rawTransaction we wish to send * @param chainId of the intended chain * @param credentials of the sender * @return encoded transaction hash */ public static byte[] generateTransactionHash( RawTransaction rawTransaction, byte chainId, Credentials credentials) { byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials); return Hash.sha3(signedMessage); }
@Test public void testSignMessage() { byte[] signedMessage = TransactionEncoder.signMessage( createEtherTransaction(), SampleKeys.CREDENTIALS); String hexMessage = Numeric.toHexString(signedMessage); assertThat(hexMessage, is("0xf85580010a840add5355887fffffffffffffff80" + "1c" + "a046360b50498ddf5566551ce1ce69c46c565f1f478bb0ee680caf31fbc08ab727" + "a01b2f1432de16d110407d544f519fc91b84c8e16d3b6ec899592d486a94974cd0")); }
@Test public void testEip155Transaction() { // https://github.com/ethereum/EIPs/issues/155 Credentials credentials = Credentials.create( "0x4646464646464646464646464646464646464646464646464646464646464646"); assertThat(TransactionEncoder.signMessage( createEip155RawTransaction(), (byte) 1, credentials), is(Numeric.hexStringToByteArray( "0xf86c098504a817c800825208943535353535353535353535353535353535353535880" + "de0b6b3a76400008025a028ef61340bd939bc2195fe537567866003e1a15d" + "3c71ff63e1590620aa636276a067cbe9d8997f761aecb703304b3800ccf55" + "5c9f3dc64214b297fb1966a3b6d83"))); }
@Test public void testTransferEther() throws Exception { BigInteger nonce = getNonce(ALICE.getAddress()); RawTransaction rawTransaction = createEtherTransaction( nonce, BOB.getAddress()); byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, ALICE); String hexValue = Numeric.toHexString(signedMessage); EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get(); String transactionHash = ethSendTransaction.getTransactionHash(); assertFalse(transactionHash.isEmpty()); TransactionReceipt transactionReceipt = waitForTransactionReceipt(transactionHash); assertThat(transactionReceipt.getTransactionHash(), is(transactionHash)); }
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(); }
@Test public void testDeploySmartContract() throws Exception { BigInteger nonce = getNonce(ALICE.getAddress()); RawTransaction rawTransaction = createSmartContractTransaction(nonce); byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, ALICE); String hexValue = Numeric.toHexString(signedMessage); EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get(); String transactionHash = ethSendTransaction.getTransactionHash(); assertFalse(transactionHash.isEmpty()); TransactionReceipt transactionReceipt = waitForTransactionReceipt(transactionHash); assertThat(transactionReceipt.getTransactionHash(), is(transactionHash)); assertFalse("Contract execution ran out of gas", rawTransaction.getGasLimit().equals(transactionReceipt.getGasUsed())); }
private String sendCreateContractTransaction( Credentials credentials, BigInteger initialSupply) throws Exception { BigInteger nonce = getNonce(credentials.getAddress()); String encodedConstructor = FunctionEncoder.encodeConstructor( Arrays.asList( new Uint256(initialSupply), new Utf8String("web3j tokens"), new Uint8(BigInteger.TEN), new Utf8String("w3j$"))); RawTransaction rawTransaction = RawTransaction.createContractTransaction( nonce, GAS_PRICE, GAS_LIMIT, BigInteger.ZERO, getHumanStandardTokenBinary() + encodedConstructor); byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); String hexValue = Numeric.toHexString(signedMessage); EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue) .sendAsync().get(); return transactionResponse.getTransactionHash(); }
@Test public void testDecodingSignedChainId() throws Exception { BigInteger nonce = BigInteger.ZERO; BigInteger gasPrice = BigInteger.ONE; BigInteger gasLimit = BigInteger.TEN; String to = "0x0add5355"; BigInteger value = BigInteger.valueOf(Long.MAX_VALUE); Integer chainId = 1; RawTransaction rawTransaction = RawTransaction.createEtherTransaction( nonce, gasPrice, gasLimit, to, value); byte[] signedMessage = TransactionEncoder.signMessage( rawTransaction, chainId.byteValue(), SampleKeys.CREDENTIALS); String hexMessage = Numeric.toHexString(signedMessage); RawTransaction result = TransactionDecoder.decode(hexMessage); assertNotNull(result); assertEquals(nonce, result.getNonce()); assertEquals(gasPrice, result.getGasPrice()); assertEquals(gasLimit, result.getGasLimit()); assertEquals(to, result.getTo()); assertEquals(value, result.getValue()); assertEquals("", result.getData()); assertTrue(result instanceof SignedRawTransaction); SignedRawTransaction signedResult = (SignedRawTransaction) result; assertEquals(SampleKeys.ADDRESS, signedResult.getFrom()); signedResult.verify(SampleKeys.ADDRESS); assertEquals(chainId, signedResult.getChainId()); }
RawTransaction rawTransaction = RawTransaction.createEtherTransaction( nonce, gasPrice, gasLimit, to, value); byte[] signedMessage = TransactionEncoder.signMessage( rawTransaction, SampleKeys.CREDENTIALS); String hexMessage = Numeric.toHexString(signedMessage);
/** * Utility method to provide the transaction hash for a given transaction. * * @param rawTransaction we wish to send * @param credentials of the sender * @return encoded transaction hash */ public static byte[] generateTransactionHash( RawTransaction rawTransaction, Credentials credentials) { byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); return Hash.sha3(signedMessage); }
/** * Utility method to provide the transaction hash for a given transaction. * * @param rawTransaction we wish to send * @param chainId of the intended chain * @param credentials of the sender * @return encoded transaction hash */ public static byte[] generateTransactionHash( RawTransaction rawTransaction, byte chainId, Credentials credentials) { byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials); return Hash.sha3(signedMessage); }
public String createOfflineTx(String toAddress, BigInteger gasPrice, BigInteger gasLimit, BigInteger amount, BigInteger nonce) { RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, toAddress, amount); byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); String hexValue = Numeric.toHexString(signedMessage); return hexValue; }
/** * @param transaction * @return Signed transaction bytes */ public byte[] signTransaction(RawTransaction transaction, ECKey accountKey) { Credentials credentials = Credentials.create(accountKey.getPrivateKeyAsHex()); return TransactionEncoder.signMessage(transaction, credentials); } }
/** * 签名交易 */ 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 signData(RawTransaction rawTransaction, WalletFile walletfile, String password) throws Exception { Credentials credentials = Credentials.create(Wallet.decrypt(password, walletfile)); byte[] signMessage = TransactionEncoder.signMessage(rawTransaction, ChainId.ROPSTEN, credentials); return Numeric.toHexString(signMessage); }
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 String signedTransactionData() { final RawTransaction transaction = RawTransaction.createEtherTransaction( nonce.orElse(nonce.orElseGet(sender::getNextNonce)), MINIMUM_GAS_PRICE, TRANSFER_GAS_COST, recipient.getAddress(), Convert.toWei(amount, unit).toBigIntegerExact()); return toHexString(TransactionEncoder.signMessage(transaction, sender.web3jCredentials())); } }
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(); } }