Java Code Examples for org.web3j.utils.Numeric#hexStringToByteArray()
The following examples show how to use
org.web3j.utils.Numeric#hexStringToByteArray() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TokenscriptFunction.java From alpha-wallet-android with MIT License | 6 votes |
private String checkBytesString(String responseValue) throws Exception { String name = ""; if (responseValue.length() > 0) { //try raw bytes byte[] data = Numeric.hexStringToByteArray(responseValue); //check leading bytes for non-zero if (data[0] != 0) { //truncate zeros int index = data.length - 1; while (data[index] == 0 && index > 0) index--; if (index != (data.length - 1)) { data = Arrays.copyOfRange(data, 0, index + 1); } name = new String(data, "UTF-8"); } } return name; }
Example 2
Source File: TokenscriptFunction.java From alpha-wallet-android with MIT License | 6 votes |
private String checkBytesString(String responseValue) throws Exception { String name = ""; if (responseValue.length() > 0) { //try raw bytes byte[] data = Numeric.hexStringToByteArray(responseValue); //check leading bytes for non-zero if (data[0] != 0) { //truncate zeros int index = data.length - 1; while (data[index] == 0 && index > 0) index--; if (index != (data.length - 1)) { data = Arrays.copyOfRange(data, 0, index + 1); } name = new String(data, "UTF-8"); } } return name; }
Example 3
Source File: TokenscriptFunction.java From alpha-wallet-android with MIT License | 6 votes |
private String checkBytesString(String responseValue) throws Exception { String name = ""; if (responseValue.length() > 0) { //try raw bytes byte[] data = Numeric.hexStringToByteArray(responseValue); //check leading bytes for non-zero if (data[0] != 0) { //truncate zeros int index = data.length - 1; while (data[index] == 0 && index > 0) index--; if (index != (data.length - 1)) { data = Arrays.copyOfRange(data, 0, index + 1); } name = new String(data, "UTF-8"); } } return name; }
Example 4
Source File: WasmFunctionEncoder.java From client-sdk-java with Apache License 2.0 | 6 votes |
public static String encodeConstructor(String code, List<?> inputParameters) { List<Object> parameters = new ArrayList<>(); // parameters.add(DEPLOY_METHOD_NAME); parameters.add(fnvOne64Hash(DEPLOY_METHOD_NAME)); for (Object o : inputParameters) { if (!o.equals(Void.class)) { parameters.add(o); } } byte[] parameterData = RLPCodec.encode(parameters); byte[] codeBinary = Numeric.hexStringToByteArray(code); Object[] objs = new Object[] { codeBinary, parameterData }; byte[] data = RLPCodec.encode(objs); byte[] result = new byte[MAGIC_NUM.length + data.length]; System.arraycopy(MAGIC_NUM, 0, result, 0, MAGIC_NUM.length); System.arraycopy(data, 0, result, MAGIC_NUM.length, data.length); return Numeric.toHexStringNoPrefix(result); }
Example 5
Source File: TypeDecoder.java From client-sdk-java with Apache License 2.0 | 6 votes |
static <T extends Bytes> T decodeBytes(String input, int offset, Class<T> type) { try { String simpleName = type.getSimpleName(); String[] splitName = simpleName.split(Bytes.class.getSimpleName()); int length = Integer.parseInt(splitName[1]); int hexStringLength = length << 1; byte[] bytes = Numeric.hexStringToByteArray( input.substring(offset, offset + hexStringLength)); return type.getConstructor(byte[].class).newInstance(bytes); } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new UnsupportedOperationException( "Unable to create instance of " + type.getName(), e); } }
Example 6
Source File: TransactionDecoder.java From web3j with Apache License 2.0 | 5 votes |
public static RawTransaction decode(final String hexTransaction) { final byte[] transaction = Numeric.hexStringToByteArray(hexTransaction); final RlpList rlpList = RlpDecoder.decode(transaction); final RlpList values = (RlpList) rlpList.getValues().get(0); final BigInteger nonce = ((RlpString) values.getValues().get(0)).asPositiveBigInteger(); final BigInteger gasPrice = ((RlpString) values.getValues().get(1)).asPositiveBigInteger(); final BigInteger gasLimit = ((RlpString) values.getValues().get(2)).asPositiveBigInteger(); final String to = ((RlpString) values.getValues().get(3)).asString(); final BigInteger value = ((RlpString) values.getValues().get(4)).asPositiveBigInteger(); final String data = ((RlpString) values.getValues().get(5)).asString(); if (values.getValues().size() == 6 || (values.getValues().size() == 8 && ((RlpString) values.getValues().get(7)).getBytes().length == 10) || (values.getValues().size() == 9 && ((RlpString) values.getValues().get(8)).getBytes().length == 10)) { // the 8th or 9nth element is the hex // representation of "restricted" for private transactions return RawTransaction.createTransaction(nonce, gasPrice, gasLimit, to, value, data); } else { final byte[] v = ((RlpString) values.getValues().get(6)).getBytes(); final byte[] r = Numeric.toBytesPadded( Numeric.toBigInt(((RlpString) values.getValues().get(7)).getBytes()), 32); final byte[] s = Numeric.toBytesPadded( Numeric.toBigInt(((RlpString) values.getValues().get(8)).getBytes()), 32); final Sign.SignatureData signatureData = new Sign.SignatureData(v, r, s); return new SignedRawTransaction( nonce, gasPrice, gasLimit, to, value, data, signatureData); } }
Example 7
Source File: TypeDecoder.java From client-sdk-java with Apache License 2.0 | 5 votes |
static DynamicBytes decodeDynamicBytes(String input, int offset) { int encodedLength = decodeUintAsInt(input, offset); int hexStringEncodedLength = encodedLength << 1; int valueOffset = offset + MAX_BYTE_LENGTH_FOR_HEX_STRING; String data = input.substring(valueOffset, valueOffset + hexStringEncodedLength); byte[] bytes = Numeric.hexStringToByteArray(data); return new DynamicBytes(bytes); }
Example 8
Source File: FunctionReturnDecoderTest.java From client-sdk-java with Apache License 2.0 | 5 votes |
@Test public void testDecodeIndexedBytes32Value() { String rawInput = "0x1234567890123456789012345678901234567890123456789012345678901234"; byte[] rawInputBytes = Numeric.hexStringToByteArray(rawInput); assertThat(FunctionReturnDecoder.decodeIndexedValue( rawInput, new TypeReference<Bytes32>(){}), equalTo(new Bytes32(rawInputBytes))); }
Example 9
Source File: FunctionReturnDecoderTest.java From client-sdk-java with Apache License 2.0 | 5 votes |
@Test public void testDecodeIndexedBytes16Value() { String rawInput = "0x1234567890123456789012345678901200000000000000000000000000000000"; byte[] rawInputBytes = Numeric.hexStringToByteArray(rawInput.substring(0, 34)); assertThat(FunctionReturnDecoder.decodeIndexedValue( rawInput, new TypeReference<Bytes16>(){}), equalTo(new Bytes16(rawInputBytes))); }
Example 10
Source File: TransactionEncoder.java From client-sdk-java with Apache License 2.0 | 5 votes |
static List<RlpType> asRlpValues( RawTransaction rawTransaction, Sign.SignatureData signatureData) { List<RlpType> result = new ArrayList<>(); result.add(RlpString.create(rawTransaction.getNonce())); result.add(RlpString.create(rawTransaction.getGasPrice())); result.add(RlpString.create(rawTransaction.getGasLimit())); // an empty to address (contract creation) should not be encoded as a numeric 0 value String to = rawTransaction.getTo(); if (to != null && to.length() > 0) { // addresses that start with zeros should be encoded with the zeros included, not // as numeric values result.add(RlpString.create(Numeric.hexStringToByteArray(to))); } else { result.add(RlpString.create("")); } result.add(RlpString.create(rawTransaction.getValue())); // value field will already be hex encoded, so we need to convert into binary first byte[] data = Numeric.hexStringToByteArray(rawTransaction.getData()); result.add(RlpString.create(data)); if (signatureData != null) { result.add(RlpString.create(Bytes.trimLeadingZeroes(signatureData.getV()))); result.add(RlpString.create(Bytes.trimLeadingZeroes(signatureData.getR()))); result.add(RlpString.create(Bytes.trimLeadingZeroes(signatureData.getS()))); } return result; }
Example 11
Source File: SignTest.java From etherscan-explorer with GNU General Public License v3.0 | 5 votes |
@Test public void testSignMessage() { Sign.SignatureData signatureData = Sign.signMessage(TEST_MESSAGE, SampleKeys.KEY_PAIR); Sign.SignatureData expected = new Sign.SignatureData( (byte) 27, Numeric.hexStringToByteArray( "0x9631f6d21dec448a213585a4a41a28ef3d4337548aa34734478b563036163786"), Numeric.hexStringToByteArray( "0x2ff816ee6bbb82719e983ecd8a33a4b45d32a4b58377ef1381163d75eedc900b") ); assertThat(signatureData, is(expected)); }
Example 12
Source File: EstimateGasUtil.java From client-sdk-java with Apache License 2.0 | 5 votes |
/** * 获取data中的gas消耗 * * @param rlpData * @return */ private static BigInteger getDataGasLimit(String rlpData) { byte[] bytes = Numeric.hexStringToByteArray(rlpData); int nonZeroSize = 0; int zeroSize = 0; for (byte b : bytes) { if (b != 0) { nonZeroSize++; } else { zeroSize++; } } return BigInteger.valueOf(nonZeroSize).multiply(BASE_NON_ZERO_GAS_LIMIT).add(BigInteger.valueOf(zeroSize).multiply(BASE_ZERO_GAS_LIMIT)); }
Example 13
Source File: TokenRepository.java From trust-wallet-android-source with GNU General Public License v3.0 | 5 votes |
public static byte[] createTokenTransferData(String to, BigInteger tokenAmount) { List<Type> params = Arrays.<Type>asList(new Address(to), new Uint256(tokenAmount)); List<TypeReference<?>> returnTypes = Arrays.<TypeReference<?>>asList(new TypeReference<Bool>() { }); Function function = new Function("transfer", params, returnTypes); String encodedFunction = FunctionEncoder.encode(function); return Numeric.hexStringToByteArray(Numeric.cleanHexPrefix(encodedFunction)); }
Example 14
Source File: TokenRepository.java From alpha-wallet-android with MIT License | 4 votes |
public static byte[] createTicketTransferData(String to, List<BigInteger> tokenIndices, Token token) { Function function = token.getTransferFunction(to, tokenIndices); String encodedFunction = FunctionEncoder.encode(function); return Numeric.hexStringToByteArray(Numeric.cleanHexPrefix(encodedFunction)); }
Example 15
Source File: StructuredDataEncoder.java From web3j with Apache License 2.0 | 4 votes |
public byte[] typeHash(String primaryType) { return Numeric.hexStringToByteArray(sha3String(encodeType(primaryType))); }
Example 16
Source File: ECRecoverTest.java From etherscan-explorer with GNU General Public License v3.0 | 4 votes |
@Test public void testRecoverAddressFromSignature() { //CHECKSTYLE:OFF String signature = "0x2c6401216c9031b9a6fb8cbfccab4fcec6c951cdf40e2320108d1856eb532250576865fbcd452bcdc4c57321b619ed7a9cfd38bd973c3e1e0243ac2777fe9d5b1b"; //CHECKSTYLE:ON String address = "0x31b26e43651e9371c88af3d36c14cfd938baf4fd"; String message = "v0G9u7huK4mJb2K1"; String prefix = PERSONAL_MESSAGE_PREFIX + message.length(); byte[] msgHash = Hash.sha3((prefix + message).getBytes()); byte[] signatureBytes = Numeric.hexStringToByteArray(signature); byte v = signatureBytes[64]; if (v < 27) { v += 27; } SignatureData sd = new SignatureData( v, (byte[]) Arrays.copyOfRange(signatureBytes, 0, 32), (byte[]) Arrays.copyOfRange(signatureBytes, 32, 64)); String addressRecovered = null; boolean match = false; // Iterate for each possible key to recover for (int i = 0; i < 4; i++) { BigInteger publicKey = Sign.recoverFromSignature( (byte) i, new ECDSASignature(new BigInteger(1, sd.getR()), new BigInteger(1, sd.getS())), msgHash); if (publicKey != null) { addressRecovered = "0x" + Keys.getAddress(publicKey); if (addressRecovered.equals(address)) { match = true; break; } } } assertThat(addressRecovered, is(address)); assertTrue(match); }
Example 17
Source File: ECRecoverTest.java From client-sdk-java with Apache License 2.0 | 4 votes |
@Test public void testRecoverAddressFromSignature() { //CHECKSTYLE:OFF String signature = "0x2c6401216c9031b9a6fb8cbfccab4fcec6c951cdf40e2320108d1856eb532250576865fbcd452bcdc4c57321b619ed7a9cfd38bd973c3e1e0243ac2777fe9d5b1b"; //CHECKSTYLE:ON String address = "0x31b26e43651e9371c88af3d36c14cfd938baf4fd"; String message = "v0G9u7huK4mJb2K1"; String prefix = PERSONAL_MESSAGE_PREFIX + message.length(); byte[] msgHash = Hash.sha3((prefix + message).getBytes()); byte[] signatureBytes = Numeric.hexStringToByteArray(signature); byte v = signatureBytes[64]; if (v < 27) { v += 27; } SignatureData sd = new SignatureData( v, (byte[]) Arrays.copyOfRange(signatureBytes, 0, 32), (byte[]) Arrays.copyOfRange(signatureBytes, 32, 64)); String addressRecovered = null; boolean match = false; // Iterate for each possible key to recover for (int i = 0; i < 4; i++) { BigInteger publicKey = Sign.recoverFromSignature( (byte) i, new ECDSASignature(new BigInteger(1, sd.getR()), new BigInteger(1, sd.getS())), msgHash); if (publicKey != null) { addressRecovered = "0x" + Keys.getAddress(publicKey); if (addressRecovered.equals(address)) { match = true; break; } } } assertThat(addressRecovered, is(address)); assertTrue(match); }
Example 18
Source File: ECRecoverTest.java From web3j with Apache License 2.0 | 4 votes |
@Test public void testRecoverAddressFromSignature() { String signature = "0x2c6401216c9031b9a6fb8cbfccab4fcec6c951cdf40e2320108d1856eb532250576865fbcd452bcdc4c57321b619ed7a9cfd38bd973c3e1e0243ac2777fe9d5b1b"; String address = "0x31b26e43651e9371c88af3d36c14cfd938baf4fd"; String message = "v0G9u7huK4mJb2K1"; String prefix = PERSONAL_MESSAGE_PREFIX + message.length(); byte[] msgHash = Hash.sha3((prefix + message).getBytes()); byte[] signatureBytes = Numeric.hexStringToByteArray(signature); byte v = signatureBytes[64]; if (v < 27) { v += 27; } SignatureData sd = new SignatureData( v, (byte[]) Arrays.copyOfRange(signatureBytes, 0, 32), (byte[]) Arrays.copyOfRange(signatureBytes, 32, 64)); String addressRecovered = null; boolean match = false; // Iterate for each possible key to recover for (int i = 0; i < 4; i++) { BigInteger publicKey = Sign.recoverFromSignature( (byte) i, new ECDSASignature( new BigInteger(1, sd.getR()), new BigInteger(1, sd.getS())), msgHash); if (publicKey != null) { addressRecovered = "0x" + Keys.getAddress(publicKey); if (addressRecovered.equals(address)) { match = true; break; } } } assertEquals(addressRecovered, (address)); assertTrue(match); }
Example 19
Source File: NameHash.java From web3j with Apache License 2.0 | 4 votes |
public static byte[] nameHashAsBytes(String ensName) { return Numeric.hexStringToByteArray(nameHash(ensName)); }
Example 20
Source File: Hash.java From etherscan-explorer with GNU General Public License v3.0 | 2 votes |
/** * Keccak-256 hash function. * * @param hexInput hex encoded input data with optional 0x prefix * @return hash value as hex encoded string */ public static String sha3(String hexInput) { byte[] bytes = Numeric.hexStringToByteArray(hexInput); byte[] result = sha3(bytes); return Numeric.toHexString(result); }