org.web3j.utils.Numeric类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(234)

本文整理了Java中org.web3j.utils.Numeric类的一些代码示例,展示了Numeric类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Numeric类的具体详情如下:
包路径:org.web3j.utils.Numeric
类名称:Numeric

Numeric介绍

[英]Message codec functions.

Implementation as per https://github.com/ethereum/wiki/wiki/JSON-RPC#hex-value-encoding
[中]消息编解码器功能。
按照https://github.com/ethereum/wiki/wiki/JSON-RPC#hex-值编码

代码示例

代码示例来源:origin: web3j/web3j

  1. public static String toHexStringNoPrefix(byte[] input) {
  2. return toHexString(input, 0, input.length, false);
  3. }

代码示例来源:origin: web3j/web3j

  1. /**
  2. * Keccak-256 hash function.
  3. *
  4. * @param hexInput hex encoded input data with optional 0x prefix
  5. * @return hash value as hex encoded string
  6. */
  7. public static String sha3(String hexInput) {
  8. byte[] bytes = Numeric.hexStringToByteArray(hexInput);
  9. byte[] result = sha3(bytes);
  10. return Numeric.toHexString(result);
  11. }

代码示例来源:origin: web3j/web3j

  1. public static ECKeyPair deserialize(byte[] input) {
  2. if (input.length != PRIVATE_KEY_SIZE + PUBLIC_KEY_SIZE) {
  3. throw new RuntimeException("Invalid input key size");
  4. }
  5. BigInteger privateKey = Numeric.toBigInt(input, 0, PRIVATE_KEY_SIZE);
  6. BigInteger publicKey = Numeric.toBigInt(input, PRIVATE_KEY_SIZE, PUBLIC_KEY_SIZE);
  7. return new ECKeyPair(privateKey, publicKey);
  8. }
  9. }

代码示例来源:origin: web3j/web3j

  1. public static BigInteger toBigInt(String hexValue) {
  2. String cleanValue = cleanHexPrefix(hexValue);
  3. return toBigIntNoPrefix(cleanValue);
  4. }

代码示例来源:origin: ethjava/web3j-sample

  1. private static void decodeMessage(String signedData) {
  2. //样例 https://ropsten.etherscan.io/tx/0xfd8acd10d72127f29f0a01d8bcaf0165665b5598781fe01ca4bceaa6ab9f2cb0
  3. try {
  4. System.out.println(signedData);
  5. System.out.println("解密 start " + System.currentTimeMillis());
  6. RlpList rlpList = RlpDecoder.decode(Numeric.hexStringToByteArray(signedData));
  7. List<RlpType> values = ((RlpList) rlpList.getValues().get(0)).getValues();
  8. BigInteger nonce = Numeric.toBigInt(((RlpString) values.get(0)).getBytes());
  9. BigInteger gasPrice = Numeric.toBigInt(((RlpString) values.get(1)).getBytes());
  10. BigInteger gasLimit = Numeric.toBigInt(((RlpString) values.get(2)).getBytes());
  11. String to = Numeric.toHexString(((RlpString) values.get(3)).getBytes());
  12. BigInteger value = Numeric.toBigInt(((RlpString) values.get(4)).getBytes());
  13. String data = Numeric.toHexString(((RlpString) values.get(5)).getBytes());
  14. RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, to, value, data);
  15. RlpString v = (RlpString) values.get(6);
  16. RlpString r = (RlpString) values.get(7);
  17. RlpString s = (RlpString) values.get(8);
  18. Sign.SignatureData signatureData = new Sign.SignatureData(
  19. v.getBytes()[0],
  20. Numeric.toBytesPadded(Numeric.toBigInt(r.getBytes()), 32),
  21. Numeric.toBytesPadded(Numeric.toBigInt(s.getBytes()), 32));
  22. BigInteger pubKey = Sign.signedMessageToKey(TransactionEncoder.encode(rawTransaction), signatureData);
  23. System.out.println("publicKey " + pubKey.toString(16));
  24. String address = Numeric.prependHexPrefix(Keys.getAddress(pubKey));
  25. System.out.println("address " + address);
  26. System.out.println("解密 end " + System.currentTimeMillis());
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. }

代码示例来源:origin: web3j/web3j

  1. public static RawTransaction decode(String hexTransaction) {
  2. byte[] transaction = Numeric.hexStringToByteArray(hexTransaction);
  3. RlpList rlpList = RlpDecoder.decode(transaction);
  4. RlpList values = (RlpList) rlpList.getValues().get(0);
  5. BigInteger nonce = ((RlpString) values.getValues().get(0)).asPositiveBigInteger();
  6. BigInteger gasPrice = ((RlpString) values.getValues().get(1)).asPositiveBigInteger();
  7. BigInteger gasLimit = ((RlpString) values.getValues().get(2)).asPositiveBigInteger();
  8. String to = ((RlpString) values.getValues().get(3)).asString();
  9. BigInteger value = ((RlpString) values.getValues().get(4)).asPositiveBigInteger();
  10. String data = ((RlpString) values.getValues().get(5)).asString();
  11. if (values.getValues().size() > 6) {
  12. byte v = ((RlpString) values.getValues().get(6)).getBytes()[0];
  13. byte[] r = Numeric.toBytesPadded(
  14. Numeric.toBigInt(((RlpString) values.getValues().get(7)).getBytes()), 32);
  15. byte[] s = Numeric.toBytesPadded(
  16. Numeric.toBigInt(((RlpString) values.getValues().get(8)).getBytes()), 32);
  17. Sign.SignatureData signatureData = new Sign.SignatureData(v, r, s);
  18. return new SignedRawTransaction(nonce, gasPrice, gasLimit,
  19. to, value, data, signatureData);
  20. } else {
  21. return RawTransaction.createTransaction(nonce,
  22. gasPrice, gasLimit, to, value, data);
  23. }
  24. }

代码示例来源:origin: web3j/web3j

  1. public static byte[] nameHashAsBytes(String ensName) {
  2. return Numeric.hexStringToByteArray(nameHash(ensName));
  3. }

代码示例来源:origin: web3j/web3j

  1. Numeric.toHexStringNoPrefix(
  2. Numeric.toBytesPadded(
  3. new BigInteger(Long.toString(offset)), MAX_BYTE_LENGTH

代码示例来源:origin: web3j/web3j

  1. static String encodeBool(Bool value) {
  2. byte[] rawValue = new byte[MAX_BYTE_LENGTH];
  3. if (value.getValue()) {
  4. rawValue[rawValue.length - 1] = 1;
  5. }
  6. return Numeric.toHexStringNoPrefix(rawValue);
  7. }

代码示例来源:origin: web3j/web3j

  1. protected RawTransaction(BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, String to,
  2. BigInteger value, String data) {
  3. this.nonce = nonce;
  4. this.gasPrice = gasPrice;
  5. this.gasLimit = gasLimit;
  6. this.to = to;
  7. this.value = value;
  8. if (data != null) {
  9. this.data = Numeric.cleanHexPrefix(data);
  10. }
  11. }

代码示例来源:origin: web3j/web3j

  1. private BigInteger convert(String value) {
  2. if (value != null) {
  3. return Numeric.decodeQuantity(value);
  4. } else {
  5. return null;
  6. }
  7. }

代码示例来源:origin: web3j/web3j

  1. public static byte[] serialize(ECKeyPair ecKeyPair) {
  2. byte[] privateKey = Numeric.toBytesPadded(ecKeyPair.getPrivateKey(), PRIVATE_KEY_SIZE);
  3. byte[] publicKey = Numeric.toBytesPadded(ecKeyPair.getPublicKey(), PUBLIC_KEY_SIZE);
  4. byte[] result = Arrays.copyOf(privateKey, PRIVATE_KEY_SIZE + PUBLIC_KEY_SIZE);
  5. System.arraycopy(publicKey, 0, result, PRIVATE_KEY_SIZE, PUBLIC_KEY_SIZE);
  6. return result;
  7. }

代码示例来源:origin: TrustWallet/trust-wallet-android-source

  1. public static byte[] createTokenTransferData(String to, BigInteger tokenAmount) {
  2. List<Type> params = Arrays.<Type>asList(new Address(to), new Uint256(tokenAmount));
  3. List<TypeReference<?>> returnTypes = Arrays.<TypeReference<?>>asList(new TypeReference<Bool>() {
  4. });
  5. Function function = new Function("transfer", params, returnTypes);
  6. String encodedFunction = FunctionEncoder.encode(function);
  7. return Numeric.hexStringToByteArray(Numeric.cleanHexPrefix(encodedFunction));
  8. }
  9. }

代码示例来源:origin: web3j/web3j

  1. private void assertCorrectEntropy(String expected, String mnemonic, int size) {
  2. Assert.assertEquals(expected, Numeric.toHexStringNoPrefixZeroPadded(
  3. Numeric.toBigInt(MnemonicUtils.generateEntropy(mnemonic)), size));
  4. }
  5. }

代码示例来源:origin: web3j/web3j

  1. public Transaction(String from, BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit,
  2. String to, BigInteger value, String data) {
  3. this.from = from;
  4. this.to = to;
  5. this.gas = gasLimit;
  6. this.gasPrice = gasPrice;
  7. this.value = value;
  8. if (data != null) {
  9. this.data = Numeric.prependHexPrefix(data);
  10. }
  11. this.nonce = nonce;
  12. }

代码示例来源:origin: org.web3j/crypto

  1. public static RawTransaction decode(String hexTransaction) {
  2. byte[] transaction = Numeric.hexStringToByteArray(hexTransaction);
  3. RlpList rlpList = RlpDecoder.decode(transaction);
  4. RlpList values = (RlpList) rlpList.getValues().get(0);
  5. BigInteger nonce = ((RlpString) values.getValues().get(0)).asPositiveBigInteger();
  6. BigInteger gasPrice = ((RlpString) values.getValues().get(1)).asPositiveBigInteger();
  7. BigInteger gasLimit = ((RlpString) values.getValues().get(2)).asPositiveBigInteger();
  8. String to = ((RlpString) values.getValues().get(3)).asString();
  9. BigInteger value = ((RlpString) values.getValues().get(4)).asPositiveBigInteger();
  10. String data = ((RlpString) values.getValues().get(5)).asString();
  11. if (values.getValues().size() > 6) {
  12. byte v = ((RlpString) values.getValues().get(6)).getBytes()[0];
  13. byte[] r = Numeric.toBytesPadded(
  14. Numeric.toBigInt(((RlpString) values.getValues().get(7)).getBytes()), 32);
  15. byte[] s = Numeric.toBytesPadded(
  16. Numeric.toBigInt(((RlpString) values.getValues().get(8)).getBytes()), 32);
  17. Sign.SignatureData signatureData = new Sign.SignatureData(v, r, s);
  18. return new SignedRawTransaction(nonce, gasPrice, gasLimit,
  19. to, value, data, signatureData);
  20. } else {
  21. return RawTransaction.createTransaction(nonce,
  22. gasPrice, gasLimit, to, value, data);
  23. }
  24. }

代码示例来源:origin: web3j/web3j

  1. static DynamicBytes decodeDynamicBytes(String input, int offset) {
  2. int encodedLength = decodeUintAsInt(input, offset);
  3. int hexStringEncodedLength = encodedLength << 1;
  4. int valueOffset = offset + MAX_BYTE_LENGTH_FOR_HEX_STRING;
  5. String data = input.substring(valueOffset,
  6. valueOffset + hexStringEncodedLength);
  7. byte[] bytes = Numeric.hexStringToByteArray(data);
  8. return new DynamicBytes(bytes);
  9. }

代码示例来源:origin: web3j/web3j

  1. @Test
  2. public void testGetAddressSmallPublicKey() {
  3. byte[] address = Keys.getAddress(
  4. Numeric.toBytesPadded(BigInteger.valueOf(0x1234), Keys.PUBLIC_KEY_SIZE));
  5. String expected = Numeric.toHexStringNoPrefix(address);
  6. assertThat(Keys.getAddress("0x1234"), equalTo(expected));
  7. }

代码示例来源:origin: web3j/web3j

  1. public static String toHexStringWithPrefixSafe(BigInteger value) {
  2. String result = toHexStringNoPrefix(value);
  3. if (result.length() < 2) {
  4. result = Strings.zeros(1) + result;
  5. }
  6. return HEX_PREFIX + result;
  7. }

代码示例来源:origin: web3j/web3j

  1. public static boolean isValidPrivateKey(String privateKey) {
  2. String cleanPrivateKey = Numeric.cleanHexPrefix(privateKey);
  3. return cleanPrivateKey.length() == PRIVATE_KEY_LENGTH_IN_HEX;
  4. }

相关文章