Java Code Examples for org.web3j.crypto.Sign#SignatureData
The following examples show how to use
org.web3j.crypto.Sign#SignatureData .
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: PrivateTransactionEncoder.java From web3j with Apache License 2.0 | 6 votes |
public static List<RlpType> asRlpValues( final RawPrivateTransaction privateTransaction, final Sign.SignatureData signatureData) { final List<RlpType> result = new ArrayList<>( TransactionEncoder.asRlpValues( privateTransaction.asRawTransaction(), signatureData)); result.add(privateTransaction.getPrivateFrom().asRlp()); privateTransaction .getPrivateFor() .ifPresent(privateFor -> result.add(Base64String.unwrapListToRlp(privateFor))); privateTransaction.getPrivacyGroupId().map(Base64String::asRlp).ifPresent(result::add); result.add(RlpString.create(privateTransaction.getRestriction().getRestriction())); return result; }
Example 2
Source File: SignedRawPrivateTransaction.java From web3j with Apache License 2.0 | 6 votes |
public SignedRawPrivateTransaction( final BigInteger nonce, final BigInteger gasPrice, final BigInteger gasLimit, final String to, final String data, final Sign.SignatureData signatureData, final Base64String privateFrom, final List<Base64String> privateFor, final Base64String privacyGroupId, final Restriction restriction) { super( nonce, gasPrice, gasLimit, to, data, privateFrom, privateFor, privacyGroupId, restriction); this.signatureData = signatureData; }
Example 3
Source File: CryptoFunctions.java From alpha-wallet-android with MIT License | 6 votes |
public static Sign.SignatureData sigFromByteArray(byte[] sig) { if (sig.length < 64 || sig.length > 65) return null; byte subv = sig[64]; if (subv < 27) subv += 27; byte[] subrRev = Arrays.copyOfRange(sig, 0, 32); byte[] subsRev = Arrays.copyOfRange(sig, 32, 64); BigInteger r = new BigInteger(1, subrRev); BigInteger s = new BigInteger(1, subsRev); return new Sign.SignatureData(subv, subrRev, subsRev); }
Example 4
Source File: DappBrowserFragment.java From alpha-wallet-android with MIT License | 6 votes |
public void testRecoverAddressFromSignature(String message, String sig) { String prefix = PERSONAL_MESSAGE_PREFIX + message.length(); byte[] msgHash = (prefix + message).getBytes(); byte[] signatureBytes = Numeric.hexStringToByteArray(sig); Sign.SignatureData sd = sigFromByteArray(signatureBytes); String addressRecovered; try { BigInteger recoveredKey = Sign.signedMessageToKey(msgHash, sd); addressRecovered = "0x" + Keys.getAddress(recoveredKey); System.out.println("Recovered: " + addressRecovered); } catch (SignatureException e) { e.printStackTrace(); } }
Example 5
Source File: CryptoFunctions.java From alpha-wallet-android with MIT License | 5 votes |
public static Sign.SignatureData sigFromByteArray(byte[] sig) { if (sig.length < 64 || sig.length > 65) return null; byte subv = sig[64]; if (subv < 27) subv += 27; byte[] subrRev = Arrays.copyOfRange(sig, 0, 32); byte[] subsRev = Arrays.copyOfRange(sig, 32, 64); BigInteger r = new BigInteger(1, subrRev); BigInteger s = new BigInteger(1, subsRev); return new Sign.SignatureData(subv, subrRev, subsRev); }
Example 6
Source File: CryptoFunctions.java From alpha-wallet-android with MIT License | 5 votes |
@Override public BigInteger signedMessageToKey(byte[] data, byte[] signature) throws SignatureException { Sign.SignatureData sigData = sigFromByteArray(signature); if (sigData == null) return BigInteger.ZERO; else return Sign.signedMessageToKey(data, sigData); }
Example 7
Source File: CryptoFunctions.java From alpha-wallet-android with MIT License | 5 votes |
@Override public BigInteger signedMessageToKey(byte[] data, byte[] signature) throws SignatureException { Sign.SignatureData sigData = sigFromByteArray(signature); if (sigData == null) return BigInteger.ZERO; else return Sign.signedMessageToKey(data, sigData); }
Example 8
Source File: UniversalLinkTypeTest.java From alpha-wallet-android with MIT License | 5 votes |
private byte[] getSignature(byte[] message) throws SalesOrderMalformed { Sign.SignatureData sigData = Sign.signMessage(message, testKey); byte[] sig = new byte[65]; try { System.arraycopy(sigData.getR(), 0, sig, 0, 32); System.arraycopy(sigData.getS(), 0, sig, 32, 32); System.arraycopy(sigData.getV(), 0, sig, 64, 1); } catch (IndexOutOfBoundsException e) { throw new SalesOrderMalformed("Signature shorter than expected 256"); } return sig; }
Example 9
Source File: UniversalLinkTest.java From alpha-wallet-android with MIT License | 5 votes |
private boolean verifySignature(byte[] message, byte[] signature) { boolean pass = false; try { Sign.SignatureData sig = sigFromByteArray(signature); String address = ecRecoverAddress(message, sig); if (Numeric.cleanHexPrefix(address).equalsIgnoreCase(Numeric.cleanHexPrefix(OWNER_ADDR))) { pass = true; } } catch (Exception e) { e.printStackTrace(); } return pass; }
Example 10
Source File: UniversalLinkTest.java From alpha-wallet-android with MIT License | 5 votes |
private byte[] getSignature(byte[] message) throws SalesOrderMalformed { Sign.SignatureData sigData = Sign.signMessage(message, testKey); byte[] sig = new byte[65]; try { System.arraycopy(sigData.getR(), 0, sig, 0, 32); System.arraycopy(sigData.getS(), 0, sig, 32, 32); System.arraycopy(sigData.getV(), 0, sig, 64, 1); } catch (IndexOutOfBoundsException e) { throw new SalesOrderMalformed("Signature shorter than expected 256"); } return sig; }
Example 11
Source File: AlphaWalletService.java From alpha-wallet-android with MIT License | 5 votes |
private void addSignature(Map<String, String> args, byte[] sig) { Sign.SignatureData sigData = sigFromByteArray(sig); if (sigData != null) { args.put("r", Numeric.toHexString(sigData.getR())); args.put("s", Numeric.toHexString(sigData.getS())); args.put("v", Numeric.toHexString(sigData.getV())); } }
Example 12
Source File: PrivateTransactionEncoder.java From web3j with Apache License 2.0 | 5 votes |
public static byte[] signMessage( final RawPrivateTransaction rawTransaction, final Credentials credentials) { final byte[] encodedTransaction = encode(rawTransaction); final Sign.SignatureData signatureData = Sign.signMessage(encodedTransaction, credentials.getEcKeyPair()); return encode(rawTransaction, signatureData); }
Example 13
Source File: CryptoFunctions.java From alpha-wallet-android with MIT License | 5 votes |
@Override public BigInteger signedMessageToKey(byte[] data, byte[] signature) throws SignatureException { Sign.SignatureData sigData = sigFromByteArray(signature); if (sigData == null) return BigInteger.ZERO; return Sign.signedMessageToKey(data, sigData); }
Example 14
Source File: SignedRawPrivateTransaction.java From web3j with Apache License 2.0 | 4 votes |
public Sign.SignatureData getSignatureData() { return signatureData; }
Example 15
Source File: EdconLinkGenerator.java From alpha-wallet-android with MIT License | 4 votes |
private byte[] signMagicLink(byte[] signData) { ECKeyPair ecKeyPair = ECKeyPair.create(privateKey); Sign.SignatureData signatureData = Sign.signMessage(signData, ecKeyPair); return bytesFromSignature(signatureData); }
Example 16
Source File: PrivateTransactionEncoder.java From web3j with Apache License 2.0 | 4 votes |
public static byte[] encode(final RawPrivateTransaction rawTransaction, final long chainId) { final Sign.SignatureData signatureData = new Sign.SignatureData(longToBytes(chainId), new byte[] {}, new byte[] {}); return encode(rawTransaction, signatureData); }
Example 17
Source File: SpawnableLinkGenerator.java From alpha-wallet-android with MIT License | 4 votes |
private byte[] signMagicLink(byte[] signData) { ECKeyPair ecKeyPair = ECKeyPair.create(privateKey); Sign.SignatureData signatureData = Sign.signMessage(signData, ecKeyPair); return bytesFromSignature(signatureData); }
Example 18
Source File: PrivateTransactionDecoderTest.java From web3j with Apache License 2.0 | 4 votes |
@Test public void testDecodingSigned() throws Exception { final BigInteger nonce = BigInteger.ZERO; final BigInteger gasPrice = BigInteger.ONE; final BigInteger gasLimit = BigInteger.TEN; final String to = "0x0add5355"; final RawPrivateTransaction rawTransaction = RawPrivateTransaction.createTransaction( nonce, gasPrice, gasLimit, to, "", MOCK_ENCLAVE_KEY, MOCK_PRIVATE_FOR, RESTRICTED); final String privateKey = "8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63"; final Credentials credentials = Credentials.create(privateKey); final byte[] encodedMessage = PrivateTransactionEncoder.signMessage(rawTransaction, credentials); final String hexMessage = Numeric.toHexString(encodedMessage); final RawPrivateTransaction result = PrivateTransactionDecoder.decode(hexMessage); assertNotNull(result); assertEquals(nonce, result.getNonce()); assertEquals(gasPrice, result.getGasPrice()); assertEquals(gasLimit, result.getGasLimit()); assertEquals(to, result.getTo()); assertEquals("", result.getData()); assertEquals(MOCK_ENCLAVE_KEY, result.getPrivateFrom()); assertEquals(MOCK_PRIVATE_FOR, result.getPrivateFor().get()); assertEquals(RESTRICTED, result.getRestriction()); assertTrue(result instanceof SignedRawPrivateTransaction); final SignedRawPrivateTransaction signedResult = (SignedRawPrivateTransaction) result; assertNotNull(signedResult.getSignatureData()); Sign.SignatureData signatureData = signedResult.getSignatureData(); final byte[] encodedTransaction = PrivateTransactionEncoder.encode(rawTransaction); final BigInteger key = Sign.signedMessageToKey(encodedTransaction, signatureData); assertEquals(key, credentials.getEcKeyPair().getPublicKey()); assertEquals(credentials.getAddress(), signedResult.getFrom()); signedResult.verify(credentials.getAddress()); assertNull(signedResult.getChainId()); }
Example 19
Source File: BesuPrivacyQuickstartIntegrationTest.java From web3j with Apache License 2.0 | 4 votes |
@Test public void simplePrivateTransactions() throws Exception { // Build new privacy group using the create API final Base64String privacyGroupId = nodeBob.privCreatePrivacyGroup( Arrays.asList( ENCLAVE_KEY_ALICE, ENCLAVE_KEY_BOB, ENCLAVE_KEY_CHARLIE), "AliceBobCharlie", "AliceBobCharlie group") .send() .getPrivacyGroupId(); final BigInteger nonce = nodeCharlie .privGetTransactionCount(ALICE.getAddress(), privacyGroupId) .send() .getTransactionCount(); final RawPrivateTransaction rawPrivateTransaction = RawPrivateTransaction.createContractTransaction( nonce, ZERO_GAS_PROVIDER.getGasPrice(), ZERO_GAS_PROVIDER.getGasLimit(), HUMAN_STANDARD_TOKEN_BINARY, ENCLAVE_KEY_ALICE, privacyGroupId, RESTRICTED); final String signedTransactionData = Numeric.toHexString( PrivateTransactionEncoder.signMessage(rawPrivateTransaction, 2018, ALICE)); final String transactionHash = nodeAlice.eeaSendRawTransaction(signedTransactionData).send().getTransactionHash(); final PollingPrivateTransactionReceiptProcessor receiptProcessor = new PollingPrivateTransactionReceiptProcessor(nodeAlice, 1 * 1000, 120); final PrivateTransactionReceipt receipt = receiptProcessor.waitForTransactionReceipt(transactionHash); assertEquals(receipt.getFrom(), (ALICE.getAddress())); assertEquals(receipt.getLogs().size(), (0)); assertNull(receipt.getTo()); assertNotNull(receipt.getContractAddress()); assertNotNull(receipt.getStatus()); assertNull(receipt.getRevertReason()); final PrivateTransactionWithPrivacyGroup privateTransaction = (PrivateTransactionWithPrivacyGroup) nodeAlice .privGetPrivateTransaction(transactionHash) .send() .getPrivateTransaction() .get(); assertEquals(privateTransaction.getFrom(), (ALICE.getAddress())); assertEquals(privateTransaction.getGas(), (ZERO_GAS_PROVIDER.getGasLimit())); assertEquals(privateTransaction.getGasPrice(), (ZERO_GAS_PROVIDER.getGasPrice())); assertEquals(privateTransaction.getNonce(), (nonce)); final byte[] encodedTransaction = PrivateTransactionEncoder.encode(rawPrivateTransaction, 2018); final Sign.SignatureData signatureData = Sign.signMessage(encodedTransaction, ALICE.getEcKeyPair()); final Sign.SignatureData eip155SignatureData = TransactionEncoder.createEip155SignatureData(signatureData, 2018); assertEquals( Numeric.toBytesPadded(BigInteger.valueOf(privateTransaction.getV()), 2), (eip155SignatureData.getV())); assertEquals( Numeric.hexStringToByteArray(privateTransaction.getR()), (eip155SignatureData.getR())); assertEquals( Numeric.hexStringToByteArray(privateTransaction.getS()), (eip155SignatureData.getS())); assertEquals(privateTransaction.getPrivateFrom(), (ENCLAVE_KEY_ALICE)); assertEquals(privateTransaction.getPrivacyGroupId(), (privacyGroupId)); assertEquals(privateTransaction.getRestriction(), (RESTRICTED)); assertNull(privateTransaction.getTo()); }
Example 20
Source File: PrivateTransactionEncoder.java From web3j with Apache License 2.0 | 4 votes |
private static byte[] encode( final RawPrivateTransaction rawTransaction, final Sign.SignatureData signatureData) { final List<RlpType> values = asRlpValues(rawTransaction, signatureData); final RlpList rlpList = new RlpList(values); return RlpEncoder.encode(rlpList); }