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); } }
public static byte[] nameHashAsBytes(String ensName) { return Numeric.hexStringToByteArray(nameHash(ensName)); }
static <T extends NumericType> T decodeNumeric(String input, Class<T> type) { try { byte[] inputByteArray = Numeric.hexStringToByteArray(input); int typeLengthAsBytes = getTypeLengthInBytes(type); byte[] resultByteArray = new byte[typeLengthAsBytes + 1]; if (Int.class.isAssignableFrom(type) || Fixed.class.isAssignableFrom(type)) { resultByteArray[0] = inputByteArray[0]; // take MSB as sign bit } int valueOffset = Type.MAX_BYTE_LENGTH - typeLengthAsBytes; System.arraycopy(inputByteArray, valueOffset, resultByteArray, 1, typeLengthAsBytes); BigInteger numericValue = new BigInteger(resultByteArray); return type.getConstructor(BigInteger.class).newInstance(numericValue); } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new UnsupportedOperationException( "Unable to create instance of " + type.getName(), e); } }
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); }
public static String generateContractAddress(String address, BigInteger nonce) { byte[] result = generateContractAddress(Numeric.hexStringToByteArray(address), nonce); return Numeric.toHexString(result); } }
/** * 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); }
@Test public void testHexStringToByteArray() { assertThat(Numeric.hexStringToByteArray(""), is(new byte[] { })); assertThat(Numeric.hexStringToByteArray("0"), is(new byte[] { 0 })); assertThat(Numeric.hexStringToByteArray("1"), is(new byte[] { 0x1 })); assertThat(Numeric.hexStringToByteArray(HEX_RANGE_STRING), is(HEX_RANGE_ARRAY)); assertThat(Numeric.hexStringToByteArray("0x123"), is(new byte[] { 0x1, 0x23 })); }
@Test public void testSignMessage() { Sign.SignatureData signatureData = Sign.signPrefixedMessage(TEST_MESSAGE, SampleKeys.KEY_PAIR); Sign.SignatureData expected = new Sign.SignatureData( (byte) 28, Numeric.hexStringToByteArray( "0x0464eee9e2fe1a10ffe48c78b80de1ed8dcf996f3f60955cb2e03cb21903d930"), Numeric.hexStringToByteArray( "0x06624da478b3f862582e85b31c6a21c6cae2eee2bd50f55c93c4faad9d9c8d7f") ); assertThat(signatureData, is(expected)); }
result.add(RlpString.create(Numeric.hexStringToByteArray(to))); } else { result.add(RlpString.create("")); byte[] data = Numeric.hexStringToByteArray(rawTransaction.getData()); result.add(RlpString.create(data));
@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))); }
byte[] mac = Numeric.hexStringToByteArray(crypto.getMac()); byte[] iv = Numeric.hexStringToByteArray(crypto.getCipherparams().getIv()); byte[] cipherText = Numeric.hexStringToByteArray(crypto.getCiphertext()); int p = scryptKdfParams.getP(); int r = scryptKdfParams.getR(); byte[] salt = Numeric.hexStringToByteArray(scryptKdfParams.getSalt()); derivedKey = generateDerivedScryptKey(password.getBytes(UTF_8), salt, n, r, p, dklen); } else if (kdfParams instanceof WalletFile.Aes128CtrKdfParams) { int c = aes128CtrKdfParams.getC(); String prf = aes128CtrKdfParams.getPrf(); byte[] salt = Numeric.hexStringToByteArray(aes128CtrKdfParams.getSalt());
@Test public void testDecodeIndexedBytes32Value() { String rawInput = "0x1234567890123456789012345678901234567890123456789012345678901234"; byte[] rawInputBytes = Numeric.hexStringToByteArray(rawInput); assertThat(FunctionReturnDecoder.decodeIndexedValue( rawInput, new TypeReference<Bytes32>(){}), equalTo(new Bytes32(rawInputBytes))); }
@Test public void testEip155Encode() { assertThat(TransactionEncoder.encode(createEip155RawTransaction(), (byte) 1), is(Numeric.hexStringToByteArray( "0xec098504a817c800825208943535353535353535353535353535353535353535880de0" + "b6b3a764000080018080"))); }
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); } }
@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"))); }
byte[] msgHash = Hash.sha3((prefix + message).getBytes()); byte[] signatureBytes = Numeric.hexStringToByteArray(signature); byte v = signatureBytes[64]; if (v < 27) {
@Test public void testDecodeIndexedDynamicBytesValue() { DynamicBytes bytes = new DynamicBytes(new byte[]{ 1, 2, 3, 4, 5}); String encoded = TypeEncoder.encodeDynamicBytes(bytes); String hash = Hash.sha3(encoded); assertThat(FunctionReturnDecoder.decodeIndexedValue( hash, new TypeReference<DynamicBytes>() {}), equalTo(new Bytes32(Numeric.hexStringToByteArray(hash)))); }
@Test public void testDecodeIndexedStringValue() { Utf8String string = new Utf8String("some text"); String encoded = TypeEncoder.encodeString(string); String hash = Hash.sha3(encoded); assertThat(FunctionReturnDecoder.decodeIndexedValue( hash, new TypeReference<Utf8String>() {}), equalTo(new Bytes32(Numeric.hexStringToByteArray(hash)))); }
@Test public void testDecodeIndexedDynamicArrayValue() { DynamicArray<Uint256> array = new DynamicArray<>(new Uint256(BigInteger.TEN)); String encoded = TypeEncoder.encodeDynamicArray(array); String hash = Hash.sha3(encoded); assertThat(FunctionReturnDecoder.decodeIndexedValue( hash, new TypeReference<DynamicArray>() {}), equalTo(new Bytes32(Numeric.hexStringToByteArray(hash)))); } }
private void testGenerated(String seed, String expectedPriv, String expectedPub, int[] path) { Bip32ECKeyPair pair = Bip32ECKeyPair.generateKeyPair(Numeric.hexStringToByteArray(seed)); assertNotNull(pair); pair = Bip32ECKeyPair.deriveKeyPair(pair, path); assertNotNull(pair); assertEquals(expectedPriv, Base58.encode(addChecksum(serializePrivate(pair)))); assertEquals(expectedPub, Base58.encode(addChecksum(serializePublic(pair)))); }