org.web3j.utils.Numeric.toBytesPadded()方法的使用及代码示例

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

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

Numeric.toBytesPadded介绍

暂无

代码示例

代码示例来源: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: web3j/web3j

@Test
public void testToBytesPadded() {
  assertThat(Numeric.toBytesPadded(BigInteger.TEN, 1),
      is(new byte[] { 0xa }));
  assertThat(Numeric.toBytesPadded(BigInteger.TEN, 8),
      is(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0xa }));
  assertThat(Numeric.toBytesPadded(BigInteger.valueOf(Integer.MAX_VALUE), 4),
      is(new byte[] { 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff }));
}

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

@Test(expected = RuntimeException.class)
public void testToBytesPaddedInvalid() {
  Numeric.toBytesPadded(BigInteger.valueOf(Long.MAX_VALUE), 7);
}

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

public static SignatureData signMessage(byte[] message, ECKeyPair keyPair, boolean needToHash) {
  BigInteger publicKey = keyPair.getPublicKey();
  byte[] messageHash;
  if (needToHash) {
    messageHash = Hash.sha3(message);
  } else {
    messageHash = message;
  }
  ECDSASignature sig = keyPair.sign(messageHash);
  // Now we have to work backwards to figure out the recId needed to recover the signature.
  int recId = -1;
  for (int i = 0; i < 4; i++) {
    BigInteger k = recoverFromSignature(i, sig, messageHash);
    if (k != null && k.equals(publicKey)) {
      recId = i;
      break;
    }
  }
  if (recId == -1) {
    throw new RuntimeException(
        "Could not construct a recoverable key. Are your credentials valid?");
  }
  int headerByte = recId + 27;
  // 1 header + 32 bytes for R + 32 bytes for S
  byte v = (byte) headerByte;
  byte[] r = Numeric.toBytesPadded(sig.r, 32);
  byte[] s = Numeric.toBytesPadded(sig.s, 32);
  return new SignatureData(v, r, s);
}

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

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

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

public static WalletFile create(String password, ECKeyPair ecKeyPair, int n, int p)
    throws CipherException {
  byte[] salt = generateRandomBytes(32);
  byte[] derivedKey = generateDerivedScryptKey(
      password.getBytes(UTF_8), salt, n, R, p, DKLEN);
  byte[] encryptKey = Arrays.copyOfRange(derivedKey, 0, 16);
  byte[] iv = generateRandomBytes(16);
  byte[] privateKeyBytes =
      Numeric.toBytesPadded(ecKeyPair.getPrivateKey(), Keys.PRIVATE_KEY_SIZE);
  byte[] cipherText = performCipherOperation(
        Cipher.ENCRYPT_MODE, iv, encryptKey, privateKeyBytes);
  byte[] mac = generateMac(derivedKey, cipherText);
  return createWalletFile(ecKeyPair, cipherText, iv, salt, mac, n, p);
}

代码示例来源: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

@Test
public void testGetAddressZeroPadded() {
  byte[] address = Keys.getAddress(
      Numeric.toBytesPadded(BigInteger.valueOf(0x1234), Keys.PUBLIC_KEY_SIZE));
  String expected = Numeric.toHexStringNoPrefix(address);
  String value = "1234";
  assertThat(Keys.getAddress("0x"
          + Strings.zeros(Keys.PUBLIC_KEY_LENGTH_IN_HEX - value.length()) + value),
      equalTo(expected));
}

代码示例来源: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: org.web3j/crypto

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: io.daonomic.scalether/util

public static SignatureData signMessage(byte[] message, BigInteger publicKey, BigInteger privateKey) {
  byte[] messageHash = Hash.sha3(message);
  ECDSASignature sig = sign(messageHash, privateKey);
  // Now we have to work backwards to figure out the recId needed to recover the signature.
  int recId = -1;
  for (int i = 0; i < 4; i++) {
    BigInteger k = recoverFromSignature(i, sig, messageHash);
    if (k != null && k.equals(publicKey)) {
      recId = i;
      break;
    }
  }
  if (recId == -1) {
    throw new RuntimeException(
        "Could not construct a recoverable key. This should never happen.");
  }
  int headerByte = recId + 27;
  // 1 header + 32 bytes for R + 32 bytes for S
  byte v = (byte) headerByte;
  byte[] r = Numeric.toBytesPadded(sig.r, 32);
  byte[] s = Numeric.toBytesPadded(sig.s, 32);
  return new SignatureData(v, r, s);
}

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

public static SignatureData signMessage(byte[] message, ECKeyPair keyPair, boolean needToHash) {
  BigInteger publicKey = keyPair.getPublicKey();
  byte[] messageHash;
  if (needToHash) {
    messageHash = Hash.sha3(message);
  } else {
    messageHash = message;
  }
  ECDSASignature sig = keyPair.sign(messageHash);
  // Now we have to work backwards to figure out the recId needed to recover the signature.
  int recId = -1;
  for (int i = 0; i < 4; i++) {
    BigInteger k = recoverFromSignature(i, sig, messageHash);
    if (k != null && k.equals(publicKey)) {
      recId = i;
      break;
    }
  }
  if (recId == -1) {
    throw new RuntimeException(
        "Could not construct a recoverable key. Are your credentials valid?");
  }
  int headerByte = recId + 27;
  // 1 header + 32 bytes for R + 32 bytes for S
  byte v = (byte) headerByte;
  byte[] r = Numeric.toBytesPadded(sig.r, 32);
  byte[] s = Numeric.toBytesPadded(sig.s, 32);
  return new SignatureData(v, r, s);
}

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

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

代码示例来源: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: 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);
  }
}

相关文章