mirror of
https://github.com/sparrowwallet/drongo.git
synced 2024-11-04 11:06:44 +00:00
minor refactor of bip322 implementation
This commit is contained in:
parent
e965a9ddd7
commit
f47d5de392
2 changed files with 10 additions and 56 deletions
|
@ -16,9 +16,12 @@ import java.util.*;
|
||||||
import static com.sparrowwallet.drongo.protocol.ScriptType.P2TR;
|
import static com.sparrowwallet.drongo.protocol.ScriptType.P2TR;
|
||||||
|
|
||||||
public class Bip322 {
|
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);
|
checkScriptType(scriptType);
|
||||||
|
|
||||||
|
ECKey pubKey = ECKey.fromPublicOnly(privKey);
|
||||||
|
Address address = scriptType.getAddress(pubKey);
|
||||||
|
|
||||||
Transaction toSpend = getBip322ToSpend(address, message);
|
Transaction toSpend = getBip322ToSpend(address, message);
|
||||||
Transaction toSign = getBip322ToSign(toSpend);
|
Transaction toSign = getBip322ToSign(toSpend);
|
||||||
|
|
||||||
|
@ -28,9 +31,8 @@ public class Bip322 {
|
||||||
PSBTInput psbtInput = psbt.getPsbtInputs().get(0);
|
PSBTInput psbtInput = psbt.getPsbtInputs().get(0);
|
||||||
psbtInput.setWitnessUtxo(utxoOutput);
|
psbtInput.setWitnessUtxo(utxoOutput);
|
||||||
psbtInput.setSigHash(SigHash.ALL);
|
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);
|
TransactionSignature signature = psbtInput.isTaproot() ? psbtInput.getTapKeyPathSignature() : psbtInput.getPartialSignature(pubKey);
|
||||||
|
|
||||||
Transaction finalizeTransaction = new Transaction();
|
Transaction finalizeTransaction = new Transaction();
|
||||||
|
@ -53,7 +55,7 @@ public class Bip322 {
|
||||||
TransactionSignature signature;
|
TransactionSignature signature;
|
||||||
ECKey pubKey;
|
ECKey pubKey;
|
||||||
|
|
||||||
if(address.getScriptType() == ScriptType.P2WPKH) {
|
if(scriptType == ScriptType.P2WPKH) {
|
||||||
signature = witness.getSignatures().get(0);
|
signature = witness.getSignatures().get(0);
|
||||||
pubKey = ECKey.fromPublicOnly(witness.getPushes().get(1));
|
pubKey = ECKey.fromPublicOnly(witness.getPushes().get(1));
|
||||||
|
|
||||||
|
|
|
@ -4,10 +4,6 @@ import com.sparrowwallet.drongo.Utils;
|
||||||
import com.sparrowwallet.drongo.address.Address;
|
import com.sparrowwallet.drongo.address.Address;
|
||||||
import com.sparrowwallet.drongo.address.InvalidAddressException;
|
import com.sparrowwallet.drongo.address.InvalidAddressException;
|
||||||
import com.sparrowwallet.drongo.protocol.ScriptType;
|
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.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -29,32 +25,10 @@ public class Bip322Test {
|
||||||
Address address = ScriptType.P2WPKH.getAddress(privKey);
|
Address address = ScriptType.P2WPKH.getAddress(privKey);
|
||||||
Assert.assertEquals("bc1q9vza2e8x573nczrlzms0wvx3gsqjx7vavgkx0l", address.toString());
|
Assert.assertEquals("bc1q9vza2e8x573nczrlzms0wvx3gsqjx7vavgkx0l", address.toString());
|
||||||
|
|
||||||
String signature = Bip322.signMessageBip322(ScriptType.P2WPKH, address, "", new PSBTInputSigner() {
|
String signature = Bip322.signMessageBip322(ScriptType.P2WPKH, "", privKey);
|
||||||
@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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Assert.assertEquals("AkcwRAIgM2gBAQqvZX15ZiysmKmQpDrG83avLIT492QBzLnQIxYCIBaTpOaD20qRlEylyxFSeEA2ba9YOixpX8z46TSDtS40ASECx/EgAxlkQpQ9hYjgGu6EBCPMVPwVIVJqO4XCsMvViHI=", signature);
|
Assert.assertEquals("AkcwRAIgM2gBAQqvZX15ZiysmKmQpDrG83avLIT492QBzLnQIxYCIBaTpOaD20qRlEylyxFSeEA2ba9YOixpX8z46TSDtS40ASECx/EgAxlkQpQ9hYjgGu6EBCPMVPwVIVJqO4XCsMvViHI=", signature);
|
||||||
|
|
||||||
String signature2 = Bip322.signMessageBip322(ScriptType.P2WPKH, address, "Hello World", new PSBTInputSigner() {
|
String signature2 = Bip322.signMessageBip322(ScriptType.P2WPKH, "Hello World", privKey);
|
||||||
@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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Assert.assertEquals("AkcwRAIgZRfIY3p7/DoVTty6YZbWS71bc5Vct9p9Fia83eRmw2QCICK/ENGfwLtptFluMGs2KsqoNSk89pO7F29zJLUx9a/sASECx/EgAxlkQpQ9hYjgGu6EBCPMVPwVIVJqO4XCsMvViHI=", signature2);
|
Assert.assertEquals("AkcwRAIgZRfIY3p7/DoVTty6YZbWS71bc5Vct9p9Fia83eRmw2QCICK/ENGfwLtptFluMGs2KsqoNSk89pO7F29zJLUx9a/sASECx/EgAxlkQpQ9hYjgGu6EBCPMVPwVIVJqO4XCsMvViHI=", signature2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,18 +63,7 @@ public class Bip322Test {
|
||||||
Address address = ScriptType.P2TR.getAddress(privKey);
|
Address address = ScriptType.P2TR.getAddress(privKey);
|
||||||
Assert.assertEquals("bc1ppv609nr0vr25u07u95waq5lucwfm6tde4nydujnu8npg4q75mr5sxq8lt3", address.toString());
|
Assert.assertEquals("bc1ppv609nr0vr25u07u95waq5lucwfm6tde4nydujnu8npg4q75mr5sxq8lt3", address.toString());
|
||||||
|
|
||||||
String signature = Bip322.signMessageBip322(ScriptType.P2TR, address, "Hello World", new PSBTInputSigner() {
|
String signature = Bip322.signMessageBip322(ScriptType.P2TR, "Hello World", privKey);
|
||||||
@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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Assert.assertEquals("AUHd69PrJQEv+oKTfZ8l+WROBHuy9HKrbFCJu7U1iK2iiEy1vMU5EfMtjc+VSHM7aU0SDbak5IUZRVno2P5mjSafAQ==", signature);
|
Assert.assertEquals("AUHd69PrJQEv+oKTfZ8l+WROBHuy9HKrbFCJu7U1iK2iiEy1vMU5EfMtjc+VSHM7aU0SDbak5IUZRVno2P5mjSafAQ==", signature);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,18 +85,7 @@ public class Bip322Test {
|
||||||
Address address = ScriptType.P2SH_P2WPKH.getAddress(privKey);
|
Address address = ScriptType.P2SH_P2WPKH.getAddress(privKey);
|
||||||
Assert.assertEquals("37qyp7jQAzqb2rCBpMvVtLDuuzKAUCVnJb", address.toString());
|
Assert.assertEquals("37qyp7jQAzqb2rCBpMvVtLDuuzKAUCVnJb", address.toString());
|
||||||
|
|
||||||
String signature = Bip322.signMessageBip322(ScriptType.P2SH_P2WPKH, address, "Hello World", new PSBTInputSigner() {
|
String signature = Bip322.signMessageBip322(ScriptType.P2SH_P2WPKH, "Hello World", privKey);
|
||||||
@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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Assert.assertEquals("AkcwRAIgHx821fcP3D4R6RsXHF8kXza4d/SqpKGaGu++AEQjJz0CIH9cN5XGDkgkqqF9OMTbYvhgI7Yp9NoHXEgLstjqDOqDASECx/EgAxlkQpQ9hYjgGu6EBCPMVPwVIVJqO4XCsMvViHI=", signature);
|
Assert.assertEquals("AkcwRAIgHx821fcP3D4R6RsXHF8kXza4d/SqpKGaGu++AEQjJz0CIH9cN5XGDkgkqqF9OMTbYvhgI7Yp9NoHXEgLstjqDOqDASECx/EgAxlkQpQ9hYjgGu6EBCPMVPwVIVJqO4XCsMvViHI=", signature);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue