detect p2wsh addresses properly

This commit is contained in:
Craig Raw 2019-09-06 18:11:57 +02:00
parent 3406ec2f15
commit d3d5fd4a80
3 changed files with 27 additions and 9 deletions

View file

@ -105,7 +105,7 @@ public class Script {
* Returns true if this script has the required form to contain a destination address
*/
public boolean containsToAddress() {
return ScriptPattern.isP2PK(this) || ScriptPattern.isP2PKH(this) || ScriptPattern.isP2SH(this) || ScriptPattern.isP2WH(this) || ScriptPattern.isSentToMultisig(this);
return ScriptPattern.isP2PK(this) || ScriptPattern.isP2PKH(this) || ScriptPattern.isP2SH(this) || ScriptPattern.isP2WPKH(this) || ScriptPattern.isP2WSH(this) || ScriptPattern.isSentToMultisig(this);
}
/**
@ -118,7 +118,7 @@ public class Script {
return ScriptPattern.extractHashFromP2PKH(this);
else if (ScriptPattern.isP2SH(this))
return ScriptPattern.extractHashFromP2SH(this);
else if (ScriptPattern.isP2WH(this))
else if (ScriptPattern.isP2WPKH(this) || ScriptPattern.isP2WSH(this))
return ScriptPattern.extractHashFromP2WH(this);
else
throw new ProtocolException("Script not in the standard scriptPubKey form");
@ -134,8 +134,10 @@ public class Script {
return new Address[] { new P2PKHAddress( ScriptPattern.extractHashFromP2PKH(this)) };
else if (ScriptPattern.isP2SH(this))
return new Address[] { new P2SHAddress(ScriptPattern.extractHashFromP2SH(this)) };
else if (ScriptPattern.isP2WH(this))
else if (ScriptPattern.isP2WPKH(this))
return new Address[] { new P2WPKHAddress(ScriptPattern.extractHashFromP2WH(this)) };
else if (ScriptPattern.isP2WSH(this))
return new Address[] { new P2WSHAddress(ScriptPattern.extractHashFromP2WH(this)) };
else if (ScriptPattern.isSentToMultisig(this))
return ScriptPattern.extractMultisigAddresses(this);
else

View file

@ -156,10 +156,9 @@ public class ScriptPattern {
}
/**
* Returns true if this script is of the form {@code OP_0 <hash>}. This can either be a P2WPKH or P2WSH scriptPubKey. These
* two script types were introduced with segwit.
* Returns true if this script is of the form {@code OP_0 <hash[20]>}. This is a P2WPKH scriptPubKey.
*/
public static boolean isP2WH(Script script) {
public static boolean isP2WPKH(Script script) {
List<ScriptChunk> chunks = script.chunks;
if (chunks.size() != 2)
return false;
@ -168,7 +167,24 @@ public class ScriptPattern {
byte[] chunk1data = chunks.get(1).data;
if (chunk1data == null)
return false;
if (chunk1data.length != 20 && chunk1data.length != 32)
if (chunk1data.length != 20)
return false;
return true;
}
/**
* Returns true if this script is of the form {@code OP_0 <hash[32]>}. This is a P2WSH scriptPubKey.
*/
public static boolean isP2WSH(Script script) {
List<ScriptChunk> chunks = script.chunks;
if (chunks.size() != 2)
return false;
if (!chunks.get(0).equalsOpCode(OP_0))
return false;
byte[] chunk1data = chunks.get(1).data;
if (chunk1data == null)
return false;
if (chunk1data.length != 32)
return false;
return true;
}
@ -176,7 +192,7 @@ public class ScriptPattern {
/**
* Extract the pubkey hash from a P2WPKH or the script hash from a P2WSH scriptPubKey. It's important that the
* script is in the correct form, so you will want to guard calls to this method with
* {@link #isP2WH(Script)}.
* {@link #isP2WPKH(Script)} or {@link #isP2WSH(Script)}.
*/
public static byte[] extractHashFromP2WH(Script script) {
return script.chunks.get(1).data;

View file

@ -178,7 +178,7 @@ public class Transaction extends TransactionPart {
}
public static final void main(String[] args) {
String hex = "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704f4a3051c0152ffffffff0100f2052a0100000043410485e646ba748709356dfbb75465563fc7954d547fd5bd9c69f66e991c61e533fc876b74e0e638a5ba22ea94111fa4795a4a184b8cdf24e88b24787d1a24061795ac00000000";
String hex = "020000000001017811567adbc80d903030ae30fc28d5cd7c395a6a74ccab96734cf5da5bd67f1a0100000000feffffff0227030000000000002200206a4c4d9be3de0e40f601d11cebd86b6d8763caa9d91f8e5e8de5f5fc8657d46da00f000000000000220020e9eaae21539323a2627701dd2c234e3499e0faf563d73fd5fcd4d263192924a604004730440220385a8b9b998abfc9319b710c44b78727b189d7029fc6e4b6c4013a3ff2976a7b02207ab7ca6aedd8d86de6d08835d8b3e4481c778043675f59f72241e7d608aa80820147304402201f62ed94f41b77ee5eb490e127ead10bd4c2144a2eacc8d61865d86fec437ed2022037488b5b96390911ded8ba086b419c335c037dc4cb004202313635741d3691b001475221022a0d4dd0d1a7182cd45de3f460737988c17653428dcb32d9c2ab35a584c716882103171d9b824205cd5db6e9353676a292ca954b24d8310a36fc983469ba3fb507a252ae8d0b0900";
byte[] transactionBytes = Utils.hexToBytes(hex);
Transaction transaction = new Transaction(transactionBytes);