org.web3j.crypto.Sign.SignatureData Java Examples

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: TransactionSerializer.java    From ethsigner with Apache License 2.0 6 votes vote down vote up
public String serialize(final Transaction transaction) {
  final byte[] bytesToSign = transaction.rlpEncode(chainId);

  final Signature signature = signer.sign(bytesToSign);

  final SignatureData web3jSignature =
      new SignatureData(
          signature.getV().toByteArray(),
          signature.getR().toByteArray(),
          signature.getS().toByteArray());

  final SignatureData eip155Signature =
      TransactionEncoder.createEip155SignatureData(web3jSignature, chainId);

  final byte[] serializedBytes = transaction.rlpEncode(eip155Signature);
  return Numeric.toHexString(serializedBytes);
}
 
Example #2
Source File: EthTransactionTest.java    From ethsigner with Apache License 2.0 6 votes vote down vote up
@Test
public void rlpEncodesTransaction() {
  final SignatureData signatureData =
      new SignatureData(new byte[] {1}, new byte[] {2}, new byte[] {3});
  final byte[] rlpEncodedBytes = ethTransaction.rlpEncode(signatureData);
  final String rlpString = Numeric.toHexString(rlpEncodedBytes);

  final SignedRawTransaction decodedTransaction =
      (SignedRawTransaction) TransactionDecoder.decode(rlpString);
  assertThat(decodedTransaction.getTo()).isEqualTo("0xd46e8dd67c5d32be8058bb8eb970870f07244567");
  assertThat(decodedTransaction.getGasLimit()).isEqualTo(Numeric.decodeQuantity("0x76c0"));
  assertThat(decodedTransaction.getGasPrice()).isEqualTo(Numeric.decodeQuantity("0x9184e72a000"));
  assertThat(decodedTransaction.getNonce())
      .isEqualTo(
          Numeric.decodeQuantity(
              "0xe04d296d2460cfb8472af2c5fd05b5a214109c25688d3704aed5484f9a7792f2"));
  assertThat(decodedTransaction.getValue()).isEqualTo(Numeric.decodeQuantity("0x0"));
  assertThat(decodedTransaction.getData())
      .isEqualTo(
          "d46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675");

  final SignatureData decodedSignatureData = decodedTransaction.getSignatureData();
  assertThat(trimLeadingZeroes(decodedSignatureData.getV())).isEqualTo(new byte[] {1});
  assertThat(trimLeadingZeroes(decodedSignatureData.getR())).isEqualTo(new byte[] {2});
  assertThat(trimLeadingZeroes(decodedSignatureData.getS())).isEqualTo(new byte[] {3});
}
 
Example #3
Source File: PrivateTransaction.java    From ethsigner with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] rlpEncode(final SignatureData signatureData) {
  final RawPrivateTransaction rawTransaction = createTransaction();
  final List<RlpType> values =
      PrivateTransactionEncoder.asRlpValues(rawTransaction, signatureData);
  final RlpList rlpList = new RlpList(values);
  return RlpEncoder.encode(rlpList);
}
 
Example #4
Source File: EthTransaction.java    From ethsigner with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] rlpEncode(final SignatureData signatureData) {
  final RawTransaction rawTransaction = createTransaction();
  final List<RlpType> values = TransactionEncoder.asRlpValues(rawTransaction, signatureData);
  final RlpList rlpList = new RlpList(values);
  return RlpEncoder.encode(rlpList);
}
 
Example #5
Source File: EeaPrivateTransactionTest.java    From ethsigner with Apache License 2.0 5 votes vote down vote up
@Test
public void rlpEncodesTransaction() {
  final SignatureData signatureData =
      new SignatureData(new byte[] {1}, new byte[] {2}, new byte[] {3});
  final byte[] rlpEncodedBytes = privateTransaction.rlpEncode(signatureData);
  final String rlpString = Numeric.toHexString(rlpEncodedBytes);

  final SignedRawPrivateTransaction decodedTransaction =
      (SignedRawPrivateTransaction) PrivateTransactionDecoder.decode(rlpString);
  assertThat(decodedTransaction.getTo()).isEqualTo("0xd46e8dd67c5d32be8058bb8eb970870f07244567");
  assertThat(decodedTransaction.getGasLimit()).isEqualTo(decodeQuantity("0x76c0"));
  assertThat(decodedTransaction.getGasPrice()).isEqualTo(decodeQuantity("0x9184e72a000"));
  assertThat(decodedTransaction.getNonce()).isEqualTo(decodeQuantity("0x1"));
  assertThat(decodedTransaction.getValue()).isEqualTo(decodeQuantity("0x0"));
  assertThat(decodedTransaction.getData())
      .isEqualTo(
          "d46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675");
  assertThat(decodedTransaction.getRestriction()).isEqualTo(Restriction.RESTRICTED);

  final Base64String expectedDecodedPrivateFrom = params.privateFrom();
  final Base64String expectedDecodedPrivateFor = params.privateFor().get().get(0);

  assertThat(decodedTransaction.getPrivateFrom()).isEqualTo(expectedDecodedPrivateFrom);
  assertThat(decodedTransaction.getPrivateFor().get().get(0))
      .isEqualTo(expectedDecodedPrivateFor);

  final SignatureData decodedSignatureData = decodedTransaction.getSignatureData();
  assertThat(trimLeadingZeroes(decodedSignatureData.getV())).isEqualTo(new byte[] {1});
  assertThat(trimLeadingZeroes(decodedSignatureData.getR())).isEqualTo(new byte[] {2});
  assertThat(trimLeadingZeroes(decodedSignatureData.getS())).isEqualTo(new byte[] {3});
}
 
Example #6
Source File: Transaction.java    From ethsigner with Apache License 2.0 4 votes vote down vote up
default byte[] rlpEncode(final long chainId) {
  final SignatureData signatureData =
      new SignatureData(longToBytes(chainId), new byte[] {}, new byte[] {});
  return rlpEncode(signatureData);
}
 
Example #7
Source File: ECRecoverTest.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testRecoverAddressFromSignature() {
    //CHECKSTYLE:OFF
    String signature = "0x2c6401216c9031b9a6fb8cbfccab4fcec6c951cdf40e2320108d1856eb532250576865fbcd452bcdc4c57321b619ed7a9cfd38bd973c3e1e0243ac2777fe9d5b1b";
    //CHECKSTYLE:ON
    String address = "0x31b26e43651e9371c88af3d36c14cfd938baf4fd";
    String message = "v0G9u7huK4mJb2K1";
            
    String prefix = PERSONAL_MESSAGE_PREFIX + message.length();
    byte[] msgHash = Hash.sha3((prefix + message).getBytes());

    byte[] signatureBytes = Numeric.hexStringToByteArray(signature);
    byte v = signatureBytes[64];
    if (v < 27) { 
        v += 27; 
    }
       
    SignatureData sd = new SignatureData(
            v, 
            (byte[]) Arrays.copyOfRange(signatureBytes, 0, 32), 
            (byte[]) Arrays.copyOfRange(signatureBytes, 32, 64));

    String addressRecovered = null;
    boolean match = false;
    
    // Iterate for each possible key to recover
    for (int i = 0; i < 4; i++) {
        BigInteger publicKey = Sign.recoverFromSignature(
                (byte) i, 
                new ECDSASignature(new BigInteger(1, sd.getR()), new BigInteger(1, sd.getS())), 
                msgHash);
           
        if (publicKey != null) {
            addressRecovered = "0x" + Keys.getAddress(publicKey); 
            
            if (addressRecovered.equals(address)) {
                match = true;
                break;
            }
        }
    }
    
    assertThat(addressRecovered, is(address));
    assertTrue(match);
}
 
Example #8
Source File: ECRecoverTest.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testRecoverAddressFromSignature() {
    //CHECKSTYLE:OFF
    String signature = "0x2c6401216c9031b9a6fb8cbfccab4fcec6c951cdf40e2320108d1856eb532250576865fbcd452bcdc4c57321b619ed7a9cfd38bd973c3e1e0243ac2777fe9d5b1b";
    //CHECKSTYLE:ON
    String address = "0x31b26e43651e9371c88af3d36c14cfd938baf4fd";
    String message = "v0G9u7huK4mJb2K1";
            
    String prefix = PERSONAL_MESSAGE_PREFIX + message.length();
    byte[] msgHash = Hash.sha3((prefix + message).getBytes());

    byte[] signatureBytes = Numeric.hexStringToByteArray(signature);
    byte v = signatureBytes[64];
    if (v < 27) { 
        v += 27; 
    }
       
    SignatureData sd = new SignatureData(
            v, 
            (byte[]) Arrays.copyOfRange(signatureBytes, 0, 32), 
            (byte[]) Arrays.copyOfRange(signatureBytes, 32, 64));

    String addressRecovered = null;
    boolean match = false;
    
    // Iterate for each possible key to recover
    for (int i = 0; i < 4; i++) {
        BigInteger publicKey = Sign.recoverFromSignature(
                (byte) i, 
                new ECDSASignature(new BigInteger(1, sd.getR()), new BigInteger(1, sd.getS())), 
                msgHash);
           
        if (publicKey != null) {
            addressRecovered = "0x" + Keys.getAddress(publicKey); 
            
            if (addressRecovered.equals(address)) {
                match = true;
                break;
            }
        }
    }
    
    assertThat(addressRecovered, is(address));
    assertTrue(match);
}
 
Example #9
Source File: ECRecoverTest.java    From web3j with Apache License 2.0 4 votes vote down vote up
@Test
public void testRecoverAddressFromSignature() {

    String signature =
            "0x2c6401216c9031b9a6fb8cbfccab4fcec6c951cdf40e2320108d1856eb532250576865fbcd452bcdc4c57321b619ed7a9cfd38bd973c3e1e0243ac2777fe9d5b1b";

    String address = "0x31b26e43651e9371c88af3d36c14cfd938baf4fd";
    String message = "v0G9u7huK4mJb2K1";

    String prefix = PERSONAL_MESSAGE_PREFIX + message.length();
    byte[] msgHash = Hash.sha3((prefix + message).getBytes());

    byte[] signatureBytes = Numeric.hexStringToByteArray(signature);
    byte v = signatureBytes[64];
    if (v < 27) {
        v += 27;
    }

    SignatureData sd =
            new SignatureData(
                    v,
                    (byte[]) Arrays.copyOfRange(signatureBytes, 0, 32),
                    (byte[]) Arrays.copyOfRange(signatureBytes, 32, 64));

    String addressRecovered = null;
    boolean match = false;

    // Iterate for each possible key to recover
    for (int i = 0; i < 4; i++) {
        BigInteger publicKey =
                Sign.recoverFromSignature(
                        (byte) i,
                        new ECDSASignature(
                                new BigInteger(1, sd.getR()), new BigInteger(1, sd.getS())),
                        msgHash);

        if (publicKey != null) {
            addressRecovered = "0x" + Keys.getAddress(publicKey);

            if (addressRecovered.equals(address)) {
                match = true;
                break;
            }
        }
    }

    assertEquals(addressRecovered, (address));
    assertTrue(match);
}
 
Example #10
Source File: Transaction.java    From ethsigner with Apache License 2.0 votes vote down vote up
byte[] rlpEncode(SignatureData signatureData);