Search in sources :

Example 1 with ECDSASignature

use of org.ethereum.crypto.signature.ECDSASignature in project rskj by rsksmart.

the class EthModuleWalletEnabled method sign.

private String sign(String data, ECKey ecKey) {
    byte[] dataHash = TypeConverter.stringHexToByteArray(data);
    // 0x19 = 25, length should be an ascii decimals, message - original
    String prefix = (char) 25 + "Ethereum Signed Message:\n" + dataHash.length;
    byte[] messageHash = HashUtil.keccak256(ByteUtil.merge(prefix.getBytes(StandardCharsets.UTF_8), dataHash));
    ECDSASignature signature = ECDSASignature.fromSignature(ecKey.sign(messageHash));
    return TypeConverter.toJsonHex(ByteUtil.merge(BigIntegers.asUnsignedByteArray(32, signature.getR()), BigIntegers.asUnsignedByteArray(32, signature.getS()), new byte[] { signature.getV() }));
}
Also used : ECDSASignature(org.ethereum.crypto.signature.ECDSASignature)

Example 2 with ECDSASignature

use of org.ethereum.crypto.signature.ECDSASignature in project rskj by rsksmart.

the class TransactionTest method test1.

@Test
public /* sign transaction  https://tools.ietf.org/html/rfc6979 */
void test1() throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException, IOException {
    // python taken exact data
    String txRLPRawData = "a9e880872386f26fc1000085e8d4a510008203e89413978aee95f38490e9769c39b2773ed763d9cd5f80";
    // String txRLPRawData = "f82804881bc16d674ec8000094cd2a3d9f938e13cd947ec05abc7fe734df8dd8268609184e72a0006480";
    byte[] cowPrivKey = Hex.decode("c85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aaf4");
    ECKey key = ECKey.fromPrivate(cowPrivKey);
    byte[] data = Hex.decode(txRLPRawData);
    // step 1: serialize + RLP encode
    // step 2: hash = sha3(step1)
    byte[] txHash = HashUtil.keccak256(data);
    ECDSASignature signature = ECDSASignature.fromSignature(key.doSign(txHash));
    System.out.println(signature);
}
Also used : ECDSASignature(org.ethereum.crypto.signature.ECDSASignature) ECKey(org.ethereum.crypto.ECKey) Test(org.junit.Test)

Example 3 with ECDSASignature

use of org.ethereum.crypto.signature.ECDSASignature in project rskj by rsksmart.

the class Web3ImplTest method eth_sign_testSignatureGenerationToBeAlways32BytesLength.

@Test
public void eth_sign_testSignatureGenerationToBeAlways32BytesLength() {
    PowerMockito.mockStatic(ECDSASignature.class);
    when(ECDSASignature.fromSignature(any())).thenReturn(new ECDSASignature(new BigInteger("90799205472826917840242505107457993089603477280876640922171931138596850540969"), new BigInteger("12449423892652054473462673837036123325448979032544381124854758290795038162079"))).thenReturn(new ECDSASignature(new BigInteger("1"), new BigInteger("1")));
    Web3Impl web3 = createWeb3();
    String addr1 = web3.personal_newAccountWithSeed("sampleSeed1");
    byte[] hash = Keccak256Helper.keccak256("this is the data to hash".getBytes());
    String signature = web3.eth_sign(addr1, "0x" + ByteUtil.toHexString(hash));
    Assert.assertThat(signature, is("0x0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100"));
}
Also used : ECDSASignature(org.ethereum.crypto.signature.ECDSASignature) BigInteger(java.math.BigInteger) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with ECDSASignature

use of org.ethereum.crypto.signature.ECDSASignature in project rskj by rsksmart.

the class PeerDiscoveryMessage method encode.

public PeerDiscoveryMessage encode(byte[] type, byte[] data, ECKey privKey) {
    /* [1] Calc sha3 - prepare for sig */
    byte[] payload = new byte[type.length + data.length];
    payload[0] = type[0];
    System.arraycopy(data, 0, payload, 1, data.length);
    byte[] forSig = HashUtil.keccak256(payload);
    /* [2] Crate signature*/
    ECDSASignature ecdsaSignature = ECDSASignature.fromSignature(privKey.sign(forSig));
    ecdsaSignature.setV((byte) (ecdsaSignature.getV() - 27));
    byte[] sigBytes = merge(BigIntegers.asUnsignedByteArray(32, ecdsaSignature.getR()), BigIntegers.asUnsignedByteArray(32, ecdsaSignature.getS()), new byte[] { ecdsaSignature.getV() });
    // [3] calculate MDC
    byte[] forSha = merge(sigBytes, type, data);
    // wrap all the data in to the packet
    this.mdc = HashUtil.keccak256(forSha);
    this.signature = sigBytes;
    this.type = type;
    this.data = data;
    this.wire = merge(this.mdc, this.signature, this.type, this.data);
    return this;
}
Also used : ECDSASignature(org.ethereum.crypto.signature.ECDSASignature)

Example 5 with ECDSASignature

use of org.ethereum.crypto.signature.ECDSASignature in project rskj by rsksmart.

the class ECKeyTest method testSValue.

@Test
public void testSValue() throws Exception {
    // Check that we never generate an S value that is larger than half the curve order. This avoids a malleability
    // issue that can allow someone to change a transaction [hash] without invalidating the signature.
    final int ITERATIONS = 10;
    ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(ITERATIONS));
    List<ListenableFuture<ECDSASignature>> sigFutures = Lists.newArrayList();
    final ECKey key = new ECKey();
    for (byte i = 0; i < ITERATIONS; i++) {
        final byte[] hash = HashUtil.keccak256(new byte[] { i });
        sigFutures.add(executor.submit(new Callable<ECDSASignature>() {

            @Override
            public ECDSASignature call() throws Exception {
                return ECDSASignature.fromSignature(key.doSign(hash));
            }
        }));
    }
    List<ECDSASignature> sigs = Futures.allAsList(sigFutures).get();
    for (ECDSASignature signature : sigs) {
        assertTrue(signature.getS().compareTo(ECKey.HALF_CURVE_ORDER) <= 0);
    }
    final ECDSASignature duplicate = new ECDSASignature(sigs.get(0).getR(), sigs.get(0).getS());
    assertEquals(sigs.get(0), duplicate);
    assertEquals(sigs.get(0).hashCode(), duplicate.hashCode());
}
Also used : ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ECDSASignature(org.ethereum.crypto.signature.ECDSASignature) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Aggregations

ECDSASignature (org.ethereum.crypto.signature.ECDSASignature)8 Test (org.junit.Test)4 BigInteger (java.math.BigInteger)2 ECKey (org.ethereum.crypto.ECKey)2 BootstrapImportException (co.rsk.db.importer.BootstrapImportException)1 BootstrapDataEntry (co.rsk.db.importer.provider.index.data.BootstrapDataEntry)1 BootstrapDataSignature (co.rsk.db.importer.provider.index.data.BootstrapDataSignature)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)1 Map (java.util.Map)1 Callable (java.util.concurrent.Callable)1 RLPElement (org.ethereum.util.RLPElement)1 RLPList (org.ethereum.util.RLPList)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1