Skip to content

Commit

Permalink
rollback ecsda classes
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsobries committed Nov 18, 2024
1 parent cbf378a commit ea3efbc
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/main/java/org/arkecosystem/crypto/signature/ECDSASigner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.arkecosystem.crypto.signature;

import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.Sha256Hash;

public class ECDSASigner implements Signer {
@Override
public byte[] sign(byte[] message, ECKey privateKey) {
return privateKey.sign(Sha256Hash.wrap(message)).encodeToDER();
}
}
15 changes: 15 additions & 0 deletions src/main/java/org/arkecosystem/crypto/signature/ECDSAVerifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.arkecosystem.crypto.signature;

import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.SignatureDecodeException;

public class ECDSAVerifier implements Verifier {
@Override
public boolean verify(byte[] hash, ECKey key, byte[] signature) {
try {
return ECKey.verify(hash, signature, key.getPubKey());
} catch (SignatureDecodeException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.Map;
import org.arkecosystem.crypto.encoding.Hex;
import org.arkecosystem.crypto.identities.PrivateKey;
import org.arkecosystem.crypto.signature.ECDSAVerifier;
import org.arkecosystem.crypto.signature.ECDSASigner;
import org.arkecosystem.crypto.signature.SchnorrSigner;
import org.arkecosystem.crypto.signature.SchnorrVerifier;
import org.arkecosystem.crypto.signature.Signer;
Expand Down Expand Up @@ -196,10 +198,10 @@ public List<Object> decodePayload(HashMap<String, Object> data) {
}

private Signer signer() {
return new SchnorrSigner();
return new ECDSASigner();
}

private Verifier verifier() {
return new SchnorrVerifier();
return new ECDSAVerifier();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.arkecosystem.crypto.signature;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.arkecosystem.crypto.Schnorr;
import org.arkecosystem.crypto.identities.PrivateKey;
import org.bitcoinj.core.ECKey;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class ECDSASignerTest {

private Signer signer;

@BeforeEach
void setUp() {
signer = new ECDSASigner();
}

@Test
void sign() {
String message = "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89";
ECKey privateKey = PrivateKey.fromPassphrase("some passphrase");
String signature =
"304402207856D22D9C1E146492117B61D83F0A8D2E046C5A75F471172689A0A0C26907C6022035A1368F38EC63BC9B7BEEC7BCE49BD2C1AE28179EE83EBE7FD9C17824E2514B";

assertEquals(
signature,
Schnorr.bytesToHex(signer.sign(Schnorr.hexStringToByteArray(message), privateKey)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.arkecosystem.crypto.signature;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.arkecosystem.crypto.Schnorr;
import org.arkecosystem.crypto.identities.PrivateKey;
import org.bitcoinj.core.ECKey;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class ECDSAVerifierTest {

private Verifier verifier;

@BeforeEach
void setUp() {
verifier = new ECDSAVerifier();
}

@Test
void verify() {
byte[] message =
Schnorr.hexStringToByteArray(
"243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89");
ECKey privateKey = PrivateKey.fromPassphrase("some passphrase");
String signature =
"304402207856D22D9C1E146492117B61D83F0A8D2E046C5A75F471172689A0A0C26907C6022035A1368F38EC63BC9B7BEEC7BCE49BD2C1AE28179EE83EBE7FD9C17824E2514B";

assertTrue(verifier.verify(message, privateKey, Schnorr.hexStringToByteArray(signature)));
}

@Test
void verifyInvalid() {
byte[] message =
Schnorr.hexStringToByteArray(
"243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89");
ECKey privateKey = PrivateKey.fromPassphrase("some passphrase");
String signature =
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001";

assertFalse(verifier.verify(message, privateKey, Schnorr.hexStringToByteArray(signature)));
}
}

0 comments on commit ea3efbc

Please sign in to comment.