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

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

本文整理了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

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

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

/**
 * 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);
}

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

public static ECKeyPair deserialize(byte[] input) {
    if (input.length != PRIVATE_KEY_SIZE + PUBLIC_KEY_SIZE) {
      throw new RuntimeException("Invalid input key size");
    }

    BigInteger privateKey = Numeric.toBigInt(input, 0, PRIVATE_KEY_SIZE);
    BigInteger publicKey = Numeric.toBigInt(input, PRIVATE_KEY_SIZE, PUBLIC_KEY_SIZE);

    return new ECKeyPair(privateKey, publicKey);
  }
}

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

public static BigInteger toBigInt(String hexValue) {
  String cleanValue = cleanHexPrefix(hexValue);
  return toBigIntNoPrefix(cleanValue);
}

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

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();
  }
}

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

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);
  }
}

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

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

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

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

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

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

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

protected RawTransaction(BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, String to,
            BigInteger value, String data) {
  this.nonce = nonce;
  this.gasPrice = gasPrice;
  this.gasLimit = gasLimit;
  this.to = to;
  this.value = value;
  if (data != null) {
    this.data = Numeric.cleanHexPrefix(data);
  }
}

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

private BigInteger convert(String value) {
  if (value != null) {
    return Numeric.decodeQuantity(value);
  } else {
    return null;
  }
}

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

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

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

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));
  }
}

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

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

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

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;
}

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

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);
  }
}

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

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);
}

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

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

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

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

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

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

相关文章