From f47d5de3922a88ae385e586dcc3947f05c2ae803 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Tue, 4 Jul 2023 08:50:49 +0200 Subject: [PATCH] minor refactor of bip322 implementation --- .../sparrowwallet/drongo/crypto/Bip322.java | 10 ++-- .../drongo/crypto/Bip322Test.java | 56 ++----------------- 2 files changed, 10 insertions(+), 56 deletions(-) diff --git a/src/main/java/com/sparrowwallet/drongo/crypto/Bip322.java b/src/main/java/com/sparrowwallet/drongo/crypto/Bip322.java index 1919df7..3b40c56 100644 --- a/src/main/java/com/sparrowwallet/drongo/crypto/Bip322.java +++ b/src/main/java/com/sparrowwallet/drongo/crypto/Bip322.java @@ -16,9 +16,12 @@ import java.util.*; import static com.sparrowwallet.drongo.protocol.ScriptType.P2TR; public class Bip322 { - public static String signMessageBip322(ScriptType scriptType, Address address, String message, PSBTInputSigner psbtInputSigner) { + public static String signMessageBip322(ScriptType scriptType, String message, ECKey privKey) { checkScriptType(scriptType); + ECKey pubKey = ECKey.fromPublicOnly(privKey); + Address address = scriptType.getAddress(pubKey); + Transaction toSpend = getBip322ToSpend(address, message); Transaction toSign = getBip322ToSign(toSpend); @@ -28,9 +31,8 @@ public class Bip322 { PSBTInput psbtInput = psbt.getPsbtInputs().get(0); psbtInput.setWitnessUtxo(utxoOutput); psbtInput.setSigHash(SigHash.ALL); - psbtInput.sign(psbtInputSigner); + psbtInput.sign(scriptType.getOutputKey(privKey)); - ECKey pubKey = psbtInputSigner.getPubKey(); TransactionSignature signature = psbtInput.isTaproot() ? psbtInput.getTapKeyPathSignature() : psbtInput.getPartialSignature(pubKey); Transaction finalizeTransaction = new Transaction(); @@ -53,7 +55,7 @@ public class Bip322 { TransactionSignature signature; ECKey pubKey; - if(address.getScriptType() == ScriptType.P2WPKH) { + if(scriptType == ScriptType.P2WPKH) { signature = witness.getSignatures().get(0); pubKey = ECKey.fromPublicOnly(witness.getPushes().get(1)); diff --git a/src/test/java/com/sparrowwallet/drongo/crypto/Bip322Test.java b/src/test/java/com/sparrowwallet/drongo/crypto/Bip322Test.java index 9f0d4ae..cd6295a 100644 --- a/src/test/java/com/sparrowwallet/drongo/crypto/Bip322Test.java +++ b/src/test/java/com/sparrowwallet/drongo/crypto/Bip322Test.java @@ -4,10 +4,6 @@ import com.sparrowwallet.drongo.Utils; import com.sparrowwallet.drongo.address.Address; import com.sparrowwallet.drongo.address.InvalidAddressException; import com.sparrowwallet.drongo.protocol.ScriptType; -import com.sparrowwallet.drongo.protocol.Sha256Hash; -import com.sparrowwallet.drongo.protocol.SigHash; -import com.sparrowwallet.drongo.protocol.TransactionSignature; -import com.sparrowwallet.drongo.psbt.PSBTInputSigner; import org.junit.Assert; import org.junit.Test; @@ -29,32 +25,10 @@ public class Bip322Test { Address address = ScriptType.P2WPKH.getAddress(privKey); Assert.assertEquals("bc1q9vza2e8x573nczrlzms0wvx3gsqjx7vavgkx0l", address.toString()); - String signature = Bip322.signMessageBip322(ScriptType.P2WPKH, address, "", new PSBTInputSigner() { - @Override - public TransactionSignature sign(Sha256Hash hash, SigHash sigHash, TransactionSignature.Type signatureType) { - return privKey.sign(hash, sigHash, signatureType); - } - - @Override - public ECKey getPubKey() { - return ECKey.fromPublicOnly(privKey); - } - }); - + String signature = Bip322.signMessageBip322(ScriptType.P2WPKH, "", privKey); Assert.assertEquals("AkcwRAIgM2gBAQqvZX15ZiysmKmQpDrG83avLIT492QBzLnQIxYCIBaTpOaD20qRlEylyxFSeEA2ba9YOixpX8z46TSDtS40ASECx/EgAxlkQpQ9hYjgGu6EBCPMVPwVIVJqO4XCsMvViHI=", signature); - String signature2 = Bip322.signMessageBip322(ScriptType.P2WPKH, address, "Hello World", new PSBTInputSigner() { - @Override - public TransactionSignature sign(Sha256Hash hash, SigHash sigHash, TransactionSignature.Type signatureType) { - return privKey.sign(hash, sigHash, signatureType); - } - - @Override - public ECKey getPubKey() { - return ECKey.fromPublicOnly(privKey); - } - }); - + String signature2 = Bip322.signMessageBip322(ScriptType.P2WPKH, "Hello World", privKey); Assert.assertEquals("AkcwRAIgZRfIY3p7/DoVTty6YZbWS71bc5Vct9p9Fia83eRmw2QCICK/ENGfwLtptFluMGs2KsqoNSk89pO7F29zJLUx9a/sASECx/EgAxlkQpQ9hYjgGu6EBCPMVPwVIVJqO4XCsMvViHI=", signature2); } @@ -89,18 +63,7 @@ public class Bip322Test { Address address = ScriptType.P2TR.getAddress(privKey); Assert.assertEquals("bc1ppv609nr0vr25u07u95waq5lucwfm6tde4nydujnu8npg4q75mr5sxq8lt3", address.toString()); - String signature = Bip322.signMessageBip322(ScriptType.P2TR, address, "Hello World", new PSBTInputSigner() { - @Override - public TransactionSignature sign(Sha256Hash hash, SigHash sigHash, TransactionSignature.Type signatureType) { - return address.getScriptType().getOutputKey(privKey).sign(hash, sigHash, signatureType); - } - - @Override - public ECKey getPubKey() { - return ECKey.fromPublicOnly(privKey); - } - }); - + String signature = Bip322.signMessageBip322(ScriptType.P2TR, "Hello World", privKey); Assert.assertEquals("AUHd69PrJQEv+oKTfZ8l+WROBHuy9HKrbFCJu7U1iK2iiEy1vMU5EfMtjc+VSHM7aU0SDbak5IUZRVno2P5mjSafAQ==", signature); } @@ -122,18 +85,7 @@ public class Bip322Test { Address address = ScriptType.P2SH_P2WPKH.getAddress(privKey); Assert.assertEquals("37qyp7jQAzqb2rCBpMvVtLDuuzKAUCVnJb", address.toString()); - String signature = Bip322.signMessageBip322(ScriptType.P2SH_P2WPKH, address, "Hello World", new PSBTInputSigner() { - @Override - public TransactionSignature sign(Sha256Hash hash, SigHash sigHash, TransactionSignature.Type signatureType) { - return address.getScriptType().getOutputKey(privKey).sign(hash, sigHash, signatureType); - } - - @Override - public ECKey getPubKey() { - return ECKey.fromPublicOnly(privKey); - } - }); - + String signature = Bip322.signMessageBip322(ScriptType.P2SH_P2WPKH, "Hello World", privKey); Assert.assertEquals("AkcwRAIgHx821fcP3D4R6RsXHF8kXza4d/SqpKGaGu++AEQjJz0CIH9cN5XGDkgkqqF9OMTbYvhgI7Yp9NoHXEgLstjqDOqDASECx/EgAxlkQpQ9hYjgGu6EBCPMVPwVIVJqO4XCsMvViHI=", signature); }