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

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

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

Numeric.toBytesPadded介绍

暂无

代码示例

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

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

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

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

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

  1. public static SignatureData signMessage(byte[] message, ECKeyPair keyPair, boolean needToHash) {
  2. BigInteger publicKey = keyPair.getPublicKey();
  3. byte[] messageHash;
  4. if (needToHash) {
  5. messageHash = Hash.sha3(message);
  6. } else {
  7. messageHash = message;
  8. }
  9. ECDSASignature sig = keyPair.sign(messageHash);
  10. // Now we have to work backwards to figure out the recId needed to recover the signature.
  11. int recId = -1;
  12. for (int i = 0; i < 4; i++) {
  13. BigInteger k = recoverFromSignature(i, sig, messageHash);
  14. if (k != null && k.equals(publicKey)) {
  15. recId = i;
  16. break;
  17. }
  18. }
  19. if (recId == -1) {
  20. throw new RuntimeException(
  21. "Could not construct a recoverable key. Are your credentials valid?");
  22. }
  23. int headerByte = recId + 27;
  24. // 1 header + 32 bytes for R + 32 bytes for S
  25. byte v = (byte) headerByte;
  26. byte[] r = Numeric.toBytesPadded(sig.r, 32);
  27. byte[] s = Numeric.toBytesPadded(sig.s, 32);
  28. return new SignatureData(v, r, s);
  29. }

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

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

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

  1. public static WalletFile create(String password, ECKeyPair ecKeyPair, int n, int p)
  2. throws CipherException {
  3. byte[] salt = generateRandomBytes(32);
  4. byte[] derivedKey = generateDerivedScryptKey(
  5. password.getBytes(UTF_8), salt, n, R, p, DKLEN);
  6. byte[] encryptKey = Arrays.copyOfRange(derivedKey, 0, 16);
  7. byte[] iv = generateRandomBytes(16);
  8. byte[] privateKeyBytes =
  9. Numeric.toBytesPadded(ecKeyPair.getPrivateKey(), Keys.PRIVATE_KEY_SIZE);
  10. byte[] cipherText = performCipherOperation(
  11. Cipher.ENCRYPT_MODE, iv, encryptKey, privateKeyBytes);
  12. byte[] mac = generateMac(derivedKey, cipherText);
  13. return createWalletFile(ecKeyPair, cipherText, iv, salt, mac, n, p);
  14. }

代码示例来源: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. @Test
  2. public void testGetAddressZeroPadded() {
  3. byte[] address = Keys.getAddress(
  4. Numeric.toBytesPadded(BigInteger.valueOf(0x1234), Keys.PUBLIC_KEY_SIZE));
  5. String expected = Numeric.toHexStringNoPrefix(address);
  6. String value = "1234";
  7. assertThat(Keys.getAddress("0x"
  8. + Strings.zeros(Keys.PUBLIC_KEY_LENGTH_IN_HEX - value.length()) + value),
  9. equalTo(expected));
  10. }

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

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

  1. public static SignatureData signMessage(byte[] message, BigInteger publicKey, BigInteger privateKey) {
  2. byte[] messageHash = Hash.sha3(message);
  3. ECDSASignature sig = sign(messageHash, privateKey);
  4. // Now we have to work backwards to figure out the recId needed to recover the signature.
  5. int recId = -1;
  6. for (int i = 0; i < 4; i++) {
  7. BigInteger k = recoverFromSignature(i, sig, messageHash);
  8. if (k != null && k.equals(publicKey)) {
  9. recId = i;
  10. break;
  11. }
  12. }
  13. if (recId == -1) {
  14. throw new RuntimeException(
  15. "Could not construct a recoverable key. This should never happen.");
  16. }
  17. int headerByte = recId + 27;
  18. // 1 header + 32 bytes for R + 32 bytes for S
  19. byte v = (byte) headerByte;
  20. byte[] r = Numeric.toBytesPadded(sig.r, 32);
  21. byte[] s = Numeric.toBytesPadded(sig.s, 32);
  22. return new SignatureData(v, r, s);
  23. }

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

  1. public static SignatureData signMessage(byte[] message, ECKeyPair keyPair, boolean needToHash) {
  2. BigInteger publicKey = keyPair.getPublicKey();
  3. byte[] messageHash;
  4. if (needToHash) {
  5. messageHash = Hash.sha3(message);
  6. } else {
  7. messageHash = message;
  8. }
  9. ECDSASignature sig = keyPair.sign(messageHash);
  10. // Now we have to work backwards to figure out the recId needed to recover the signature.
  11. int recId = -1;
  12. for (int i = 0; i < 4; i++) {
  13. BigInteger k = recoverFromSignature(i, sig, messageHash);
  14. if (k != null && k.equals(publicKey)) {
  15. recId = i;
  16. break;
  17. }
  18. }
  19. if (recId == -1) {
  20. throw new RuntimeException(
  21. "Could not construct a recoverable key. Are your credentials valid?");
  22. }
  23. int headerByte = recId + 27;
  24. // 1 header + 32 bytes for R + 32 bytes for S
  25. byte v = (byte) headerByte;
  26. byte[] r = Numeric.toBytesPadded(sig.r, 32);
  27. byte[] s = Numeric.toBytesPadded(sig.s, 32);
  28. return new SignatureData(v, r, s);
  29. }

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

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

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

相关文章