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); }
public static byte[] encode(RawTransaction rawTransaction) { return encode(rawTransaction, null); }
private static byte[] encode(RawTransaction rawTransaction, Sign.SignatureData signatureData) { List<RlpType> values = asRlpValues(rawTransaction, signatureData); RlpList rlpList = new RlpList(values); return RlpEncoder.encode(rlpList); }
public static byte[] signMessage( RawTransaction rawTransaction, byte chainId, Credentials credentials) { byte[] encodedTransaction = encode(rawTransaction, chainId); Sign.SignatureData signatureData = Sign.signMessage( encodedTransaction, credentials.getEcKeyPair()); Sign.SignatureData eip155SignatureData = createEip155SignatureData(signatureData, chainId); return encode(rawTransaction, eip155SignatureData); }
RawTransaction rawTransaction = RawTransaction.createEtherTransaction( nonce, gasPrice, gasLimit, to, value); byte[] signedMessage = TransactionEncoder.signMessage( rawTransaction, SampleKeys.CREDENTIALS); String hexMessage = Numeric.toHexString(signedMessage); assertNotNull(signedResult.getSignatureData()); Sign.SignatureData signatureData = signedResult.getSignatureData(); byte[] encodedTransaction = TransactionEncoder.encode(rawTransaction); BigInteger key = Sign.signedMessageToKey(encodedTransaction, signatureData); assertEquals(key, SampleKeys.PUBLIC_KEY);
public static byte[] signMessage( RawTransaction rawTransaction, byte chainId, Credentials credentials) { byte[] encodedTransaction = encode(rawTransaction, chainId); Sign.SignatureData signatureData = Sign.signMessage( encodedTransaction, credentials.getEcKeyPair()); Sign.SignatureData eip155SignatureData = createEip155SignatureData(signatureData, chainId); return encode(rawTransaction, eip155SignatureData); }
/** * 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); }
public static byte[] encode(RawTransaction rawTransaction, byte chainId) { Sign.SignatureData signatureData = new Sign.SignatureData( chainId, new byte[] {}, new byte[] {}); return encode(rawTransaction, signatureData); }
@Test public void testContractAsRlpValues() { List<RlpType> rlpStrings = TransactionEncoder.asRlpValues( createContractTransaction(), null); assertThat(rlpStrings.size(), is(6)); assertThat(rlpStrings.get(3), is(RlpString.create(""))); }
/** * 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 testEtherTransactionAsRlpValues() { List<RlpType> rlpStrings = TransactionEncoder.asRlpValues(createEtherTransaction(), new Sign.SignatureData((byte) 0, new byte[32], new byte[32])); assertThat(rlpStrings.size(), is(9)); assertThat(rlpStrings.get(3), equalTo(RlpString.create(new BigInteger("add5355", 16)))); }
@Test public void testSignMessage() { byte[] signedMessage = TransactionEncoder.signMessage( createEtherTransaction(), SampleKeys.CREDENTIALS); String hexMessage = Numeric.toHexString(signedMessage); assertThat(hexMessage, is("0xf85580010a840add5355887fffffffffffffff80" + "1c" + "a046360b50498ddf5566551ce1ce69c46c565f1f478bb0ee680caf31fbc08ab727" + "a01b2f1432de16d110407d544f519fc91b84c8e16d3b6ec899592d486a94974cd0")); }
public String getFrom() throws SignatureException { Integer chainId = getChainId(); byte[] encodedTransaction; if (null == chainId) { encodedTransaction = TransactionEncoder.encode(this); } else { encodedTransaction = TransactionEncoder.encode(this, chainId.byteValue()); } byte v = signatureData.getV(); byte[] r = signatureData.getR(); byte[] s = signatureData.getS(); Sign.SignatureData signatureDataV = new Sign.SignatureData(getRealV(v), r, s); BigInteger key = Sign.signedMessageToKey(encodedTransaction, signatureDataV); return "0x" + Keys.getAddress(key); }
private static byte[] encode(RawTransaction rawTransaction, Sign.SignatureData signatureData) { List<RlpType> values = asRlpValues(rawTransaction, signatureData); RlpList rlpList = new RlpList(values); return RlpEncoder.encode(rlpList); }
@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 testEip155Encode() { assertThat(TransactionEncoder.encode(createEip155RawTransaction(), (byte) 1), is(Numeric.hexStringToByteArray( "0xec098504a817c800825208943535353535353535353535353535353535353535880de0" + "b6b3a764000080018080"))); }
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 testSignTransaction() throws Exception { boolean accountUnlocked = unlockAccount(); assertTrue(accountUnlocked); RawTransaction rawTransaction = createTransaction(); byte[] encoded = TransactionEncoder.encode(rawTransaction); byte[] hashed = Hash.sha3(encoded); EthSign ethSign = web3j.ethSign(ALICE.getAddress(), Numeric.toHexString(hashed)) .sendAsync().get(); String signature = ethSign.getSignature(); assertNotNull(signature); assertFalse(signature.isEmpty()); }
@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)); }