mirror of
https://github.com/sparrowwallet/drongo.git
synced 2024-12-26 10:06:45 +00:00
update to refactor signature verification
This commit is contained in:
parent
fd94e885b8
commit
a1696ec2e8
4 changed files with 20 additions and 20 deletions
|
@ -110,7 +110,7 @@ public class Script {
|
||||||
* Returns true if this script has the required form to contain a destination address
|
* Returns true if this script has the required form to contain a destination address
|
||||||
*/
|
*/
|
||||||
public boolean containsToAddress() {
|
public boolean containsToAddress() {
|
||||||
return ScriptPattern.isP2PK(this) || ScriptPattern.isP2PKH(this) || ScriptPattern.isP2SH(this) || ScriptPattern.isP2WPKH(this) || ScriptPattern.isP2WSH(this) || ScriptPattern.isSentToMultisig(this);
|
return ScriptPattern.isP2PK(this) || ScriptPattern.isP2PKH(this) || ScriptPattern.isP2SH(this) || ScriptPattern.isP2WPKH(this) || ScriptPattern.isP2WSH(this) || ScriptPattern.isMultisig(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -143,7 +143,7 @@ public class Script {
|
||||||
return new Address[] { new P2WPKHAddress(ScriptPattern.extractHashFromP2WH(this)) };
|
return new Address[] { new P2WPKHAddress(ScriptPattern.extractHashFromP2WH(this)) };
|
||||||
else if (ScriptPattern.isP2WSH(this))
|
else if (ScriptPattern.isP2WSH(this))
|
||||||
return new Address[] { new P2WSHAddress(ScriptPattern.extractHashFromP2WH(this)) };
|
return new Address[] { new P2WSHAddress(ScriptPattern.extractHashFromP2WH(this)) };
|
||||||
else if (ScriptPattern.isSentToMultisig(this))
|
else if (ScriptPattern.isMultisig(this))
|
||||||
return ScriptPattern.extractMultisigAddresses(this);
|
return ScriptPattern.extractMultisigAddresses(this);
|
||||||
else
|
else
|
||||||
throw new NonStandardScriptException("Cannot find addresses in non standard script: " + toString());
|
throw new NonStandardScriptException("Cannot find addresses in non standard script: " + toString());
|
||||||
|
@ -154,7 +154,7 @@ public class Script {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ScriptPattern.isSentToMultisig(this)) {
|
if(ScriptPattern.isMultisig(this)) {
|
||||||
return ScriptPattern.extractMultisigThreshold(this);
|
return ScriptPattern.extractMultisigThreshold(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -110,7 +110,7 @@ public class ScriptPattern {
|
||||||
* Returns whether this script matches the format used for multisig outputs:
|
* Returns whether this script matches the format used for multisig outputs:
|
||||||
* {@code [n] [keys...] [m] CHECKMULTISIG}
|
* {@code [n] [keys...] [m] CHECKMULTISIG}
|
||||||
*/
|
*/
|
||||||
public static boolean isSentToMultisig(Script script) {
|
public static boolean isMultisig(Script script) {
|
||||||
List<ScriptChunk> chunks = script.chunks;
|
List<ScriptChunk> chunks = script.chunks;
|
||||||
if (chunks.size() < 4) return false;
|
if (chunks.size() < 4) return false;
|
||||||
ScriptChunk chunk = chunks.get(chunks.size() - 1);
|
ScriptChunk chunk = chunks.get(chunks.size() - 1);
|
||||||
|
|
|
@ -251,8 +251,8 @@ public class PSBT {
|
||||||
PSBTInput input = new PSBTInput(inputEntries, transaction, inputIndex);
|
PSBTInput input = new PSBTInput(inputEntries, transaction, inputIndex);
|
||||||
|
|
||||||
boolean verified = input.verifySignatures();
|
boolean verified = input.verifySignatures();
|
||||||
if(verified) {
|
if(!verified && input.getPartialSignatures().size() > 0) {
|
||||||
log.debug("Verified signatures on input #" + inputIndex);
|
throw new IllegalStateException("Unverifiable partial signatures provided");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.psbtInputs.add(input);
|
this.psbtInputs.add(input);
|
||||||
|
|
|
@ -240,7 +240,7 @@ public class PSBTInput {
|
||||||
|
|
||||||
public boolean isSigned() throws NonStandardScriptException {
|
public boolean isSigned() throws NonStandardScriptException {
|
||||||
//All partial sigs are already verified
|
//All partial sigs are already verified
|
||||||
int reqSigs = getConnectedScript().getNumRequiredSignatures();
|
int reqSigs = getSigningScript().getNumRequiredSignatures();
|
||||||
int sigs = getPartialSignatures().size();
|
int sigs = getPartialSignatures().size();
|
||||||
return sigs == reqSigs;
|
return sigs == reqSigs;
|
||||||
}
|
}
|
||||||
|
@ -253,9 +253,9 @@ public class PSBTInput {
|
||||||
}
|
}
|
||||||
|
|
||||||
if(getNonWitnessUtxo() != null || getWitnessUtxo() != null) {
|
if(getNonWitnessUtxo() != null || getWitnessUtxo() != null) {
|
||||||
Script connectedScript = getConnectedScript();
|
Script signingScript = getSigningScript();
|
||||||
if(connectedScript != null) {
|
if(signingScript != null) {
|
||||||
Sha256Hash hash = getHashForSignature(connectedScript, localSigHash);
|
Sha256Hash hash = getHashForSignature(signingScript, localSigHash);
|
||||||
|
|
||||||
for(ECKey sigPublicKey : getPartialSignatures().keySet()) {
|
for(ECKey sigPublicKey : getPartialSignatures().keySet()) {
|
||||||
TransactionSignature signature = getPartialSignature(sigPublicKey);
|
TransactionSignature signature = getPartialSignature(sigPublicKey);
|
||||||
|
@ -271,30 +271,30 @@ public class PSBTInput {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Script getConnectedScript() {
|
public Script getSigningScript() {
|
||||||
int vout = (int)transaction.getInputs().get(index).getOutpoint().getIndex();
|
int vout = (int)transaction.getInputs().get(index).getOutpoint().getIndex();
|
||||||
Script connectedScript = getNonWitnessUtxo() != null ? getNonWitnessUtxo().getOutputs().get(vout).getScript() : getWitnessUtxo().getScript();
|
Script signingScript = getNonWitnessUtxo() != null ? getNonWitnessUtxo().getOutputs().get(vout).getScript() : getWitnessUtxo().getScript();
|
||||||
|
|
||||||
if(ScriptPattern.isP2SH(connectedScript)) {
|
if(ScriptPattern.isP2SH(signingScript)) {
|
||||||
if(getRedeemScript() == null) {
|
if(getRedeemScript() == null) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
connectedScript = getRedeemScript();
|
signingScript = getRedeemScript();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ScriptPattern.isP2WPKH(connectedScript)) {
|
if(ScriptPattern.isP2WPKH(signingScript)) {
|
||||||
Address address = new P2PKHAddress(connectedScript.getPubKeyHash());
|
Address address = new P2PKHAddress(signingScript.getPubKeyHash());
|
||||||
connectedScript = address.getOutputScript();
|
signingScript = address.getOutputScript();
|
||||||
} else if(ScriptPattern.isP2WSH(connectedScript)) {
|
} else if(ScriptPattern.isP2WSH(signingScript)) {
|
||||||
if(getWitnessScript() == null) {
|
if(getWitnessScript() == null) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
connectedScript = getWitnessScript();
|
signingScript = getWitnessScript();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return connectedScript;
|
return signingScript;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Sha256Hash getHashForSignature(Script connectedScript, Transaction.SigHash localSigHash) {
|
private Sha256Hash getHashForSignature(Script connectedScript, Transaction.SigHash localSigHash) {
|
||||||
|
|
Loading…
Reference in a new issue