Java Code Examples for org.web3j.utils.Numeric#toBytesPadded()
The following examples show how to use
org.web3j.utils.Numeric#toBytesPadded() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransactionDecoder.java From alpha-wallet-android with MIT License | 6 votes |
public Sign.SignatureData getSignatureData(TransactionInput data) { Sign.SignatureData sigData = null; if (data.functionData.hasSig && data.sigData != null && data.sigData.size() == 3) { BigInteger vBi = new BigInteger(data.sigData.get(0), 16); BigInteger rBi = new BigInteger(data.sigData.get(1), 16); BigInteger sBi = new BigInteger(data.sigData.get(2), 16); byte v = (byte) vBi.intValue(); byte[] r = Numeric.toBytesPadded(rBi, 32); byte[] s = Numeric.toBytesPadded(sBi, 32); sigData = new Sign.SignatureData(v, r, s); } return sigData; }
Example 2
Source File: Keys.java From client-sdk-java with Apache License 2.0 | 5 votes |
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; }
Example 3
Source File: TransactionDecoder.java From client-sdk-java with Apache License 2.0 | 5 votes |
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 || (values.getValues().size() == 8 && ((RlpString) values.getValues().get(7)).getBytes().length == 10) || (values.getValues().size() == 9 && ((RlpString) values.getValues().get(8)).getBytes().length == 10)) { // the 8th or 9nth element is the hex // representation of "restricted" for private transactions return RawTransaction.createTransaction(nonce, gasPrice, gasLimit, to, value, data); } else { final byte[] v = ((RlpString) values.getValues().get(6)).getBytes(); final byte[] r = Numeric.toBytesPadded( Numeric.toBigInt(((RlpString) values.getValues().get(7)).getBytes()), 32); final byte[] s = Numeric.toBytesPadded( Numeric.toBigInt(((RlpString) values.getValues().get(8)).getBytes()), 32); final Sign.SignatureData signatureData = new Sign.SignatureData(v, r, s); return new SignedRawTransaction( nonce, gasPrice, gasLimit, to, value, data, signatureData); } }
Example 4
Source File: Sign.java From client-sdk-java with Apache License 2.0 | 5 votes |
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 = new byte[] {(byte) headerByte}; byte[] r = Numeric.toBytesPadded(sig.r, 32); byte[] s = Numeric.toBytesPadded(sig.s, 32); return new SignatureData(v, r, s); }
Example 5
Source File: Keys.java From etherscan-explorer with GNU General Public License v3.0 | 5 votes |
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; }
Example 6
Source File: Sign.java From etherscan-explorer with GNU General Public License v3.0 | 5 votes |
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. 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); }
Example 7
Source File: Keys.java From web3j with Apache License 2.0 | 5 votes |
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; }
Example 8
Source File: TransactionDecoder.java From web3j with Apache License 2.0 | 5 votes |
public static RawTransaction decode(final String hexTransaction) { final byte[] transaction = Numeric.hexStringToByteArray(hexTransaction); final RlpList rlpList = RlpDecoder.decode(transaction); final RlpList values = (RlpList) rlpList.getValues().get(0); final BigInteger nonce = ((RlpString) values.getValues().get(0)).asPositiveBigInteger(); final BigInteger gasPrice = ((RlpString) values.getValues().get(1)).asPositiveBigInteger(); final BigInteger gasLimit = ((RlpString) values.getValues().get(2)).asPositiveBigInteger(); final String to = ((RlpString) values.getValues().get(3)).asString(); final BigInteger value = ((RlpString) values.getValues().get(4)).asPositiveBigInteger(); final String data = ((RlpString) values.getValues().get(5)).asString(); if (values.getValues().size() == 6 || (values.getValues().size() == 8 && ((RlpString) values.getValues().get(7)).getBytes().length == 10) || (values.getValues().size() == 9 && ((RlpString) values.getValues().get(8)).getBytes().length == 10)) { // the 8th or 9nth element is the hex // representation of "restricted" for private transactions return RawTransaction.createTransaction(nonce, gasPrice, gasLimit, to, value, data); } else { final byte[] v = ((RlpString) values.getValues().get(6)).getBytes(); final byte[] r = Numeric.toBytesPadded( Numeric.toBigInt(((RlpString) values.getValues().get(7)).getBytes()), 32); final byte[] s = Numeric.toBytesPadded( Numeric.toBigInt(((RlpString) values.getValues().get(8)).getBytes()), 32); final Sign.SignatureData signatureData = new Sign.SignatureData(v, r, s); return new SignedRawTransaction( nonce, gasPrice, gasLimit, to, value, data, signatureData); } }
Example 9
Source File: Sign.java From web3j with Apache License 2.0 | 5 votes |
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 = new byte[] {(byte) headerByte}; byte[] r = Numeric.toBytesPadded(sig.r, 32); byte[] s = Numeric.toBytesPadded(sig.s, 32); return new SignatureData(v, r, s); }
Example 10
Source File: Wallet.java From client-sdk-java with Apache License 2.0 | 4 votes |
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); }
Example 11
Source File: Wallet.java From etherscan-explorer with GNU General Public License v3.0 | 4 votes |
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); }
Example 12
Source File: Wallet.java From web3j with Apache License 2.0 | 4 votes |
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); }