public Transaction(String from, BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, String to, BigInteger value, String data) { this.from = from; this.to = to; this.gas = gasLimit; this.gasPrice = gasPrice; this.value = value; if (data != null) { this.data = Numeric.prependHexPrefix(data); } this.nonce = nonce; }
public static Credentials create(ECKeyPair ecKeyPair) { String address = Numeric.prependHexPrefix(Keys.getAddress(ecKeyPair)); return new Credentials(ecKeyPair, address); }
@Test public void testPrependHexPrefix() { assertThat(Numeric.prependHexPrefix(""), is("0x")); assertThat(Numeric.prependHexPrefix("0x0123456789abcdef"), is("0x0123456789abcdef")); assertThat(Numeric.prependHexPrefix("0x"), is("0x")); assertThat(Numeric.prependHexPrefix("0123456789abcdef"), is("0x0123456789abcdef")); }
@SuppressWarnings("unchecked") private void prepareEthGetCode(String binary) throws IOException { EthGetCode ethGetCode = new EthGetCode(); ethGetCode.setResult(Numeric.prependHexPrefix(binary)); Request<?, EthGetCode> ethGetCodeRequest = mock(Request.class); when(ethGetCodeRequest.send()) .thenReturn(ethGetCode); when(web3j.ethGetCode(ADDRESS, DefaultBlockParameterName.LATEST)) .thenReturn((Request) ethGetCodeRequest); }
@Test public void testIsValidPrivateKey() { assertTrue(isValidPrivateKey(SampleKeys.PRIVATE_KEY_STRING)); assertTrue(isValidPrivateKey(Numeric.prependHexPrefix(SampleKeys.PRIVATE_KEY_STRING))); assertFalse(isValidPrivateKey("")); assertFalse(isValidPrivateKey(SampleKeys.PRIVATE_KEY_STRING + "a")); assertFalse(isValidPrivateKey(SampleKeys.PRIVATE_KEY_STRING.substring(1))); }
public static Credentials create(ECKeyPair ecKeyPair) { String address = Numeric.prependHexPrefix(Keys.getAddress(ecKeyPair)); return new Credentials(ecKeyPair, address); }
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(); } }