mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-19 13:45:48 +00:00
ScriptPattern: For all matcher methods, take Script as an argument rather than List<ScriptChunk>.
This commit is contained in:
parent
caf9e52940
commit
fc4a29e3cb
@ -233,7 +233,7 @@ public class Script {
|
|||||||
* useful more exotic types of transaction, but today most payments are to addresses.
|
* useful more exotic types of transaction, but today most payments are to addresses.
|
||||||
*/
|
*/
|
||||||
public boolean isSentToRawPubKey() {
|
public boolean isSentToRawPubKey() {
|
||||||
return ScriptPattern.isPayToPubKey(chunks);
|
return ScriptPattern.isPayToPubKey(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -243,7 +243,7 @@ public class Script {
|
|||||||
* way to make payments due to the short and recognizable base58 form addresses come in.
|
* way to make payments due to the short and recognizable base58 form addresses come in.
|
||||||
*/
|
*/
|
||||||
public boolean isSentToAddress() {
|
public boolean isSentToAddress() {
|
||||||
return ScriptPattern.isPayToPubKeyHash(chunks);
|
return ScriptPattern.isPayToPubKeyHash(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -680,18 +680,18 @@ public class Script {
|
|||||||
* Bitcoin system).</p>
|
* Bitcoin system).</p>
|
||||||
*/
|
*/
|
||||||
public boolean isPayToScriptHash() {
|
public boolean isPayToScriptHash() {
|
||||||
return ScriptPattern.isPayToScriptHash(chunks);
|
return ScriptPattern.isPayToScriptHash(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether this script matches the format used for multisig outputs: [n] [keys...] [m] CHECKMULTISIG
|
* Returns whether this script matches the format used for multisig outputs: [n] [keys...] [m] CHECKMULTISIG
|
||||||
*/
|
*/
|
||||||
public boolean isSentToMultiSig() {
|
public boolean isSentToMultiSig() {
|
||||||
return ScriptPattern.isSentToMultisig(chunks);
|
return ScriptPattern.isSentToMultisig(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSentToCLTVPaymentChannel() {
|
public boolean isSentToCLTVPaymentChannel() {
|
||||||
return ScriptPattern.isSentToCltvPaymentChannel(chunks);
|
return ScriptPattern.isSentToCltvPaymentChannel(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean equalsRange(byte[] a, int start, byte[] b) {
|
private static boolean equalsRange(byte[] a, int start, byte[] b) {
|
||||||
@ -804,7 +804,7 @@ public class Script {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isOpReturn() {
|
public boolean isOpReturn() {
|
||||||
return ScriptPattern.isOpReturn(chunks);
|
return ScriptPattern.isOpReturn(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2017 John L. Jegutanis
|
* Copyright 2017 John L. Jegutanis
|
||||||
|
* Copyright 2018 Andreas Schildbach
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -27,7 +28,8 @@ import static org.bitcoinj.script.ScriptOpCodes.*;
|
|||||||
* This is a Script pattern matcher with some typical script patterns
|
* This is a Script pattern matcher with some typical script patterns
|
||||||
*/
|
*/
|
||||||
public class ScriptPattern {
|
public class ScriptPattern {
|
||||||
public static boolean isPayToPubKeyHash(List<ScriptChunk> chunks) {
|
public static boolean isPayToPubKeyHash(Script script) {
|
||||||
|
List<ScriptChunk> chunks = script.chunks;
|
||||||
return chunks.size() == 5 &&
|
return chunks.size() == 5 &&
|
||||||
chunks.get(0).equalsOpCode(OP_DUP) &&
|
chunks.get(0).equalsOpCode(OP_DUP) &&
|
||||||
chunks.get(1).equalsOpCode(OP_HASH160) &&
|
chunks.get(1).equalsOpCode(OP_HASH160) &&
|
||||||
@ -37,7 +39,8 @@ public class ScriptPattern {
|
|||||||
chunks.get(4).equalsOpCode(OP_CHECKSIG);
|
chunks.get(4).equalsOpCode(OP_CHECKSIG);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isPayToScriptHash(List<ScriptChunk> chunks) {
|
public static boolean isPayToScriptHash(Script script) {
|
||||||
|
List<ScriptChunk> chunks = script.chunks;
|
||||||
// We check for the effective serialized form because BIP16 defines a P2SH output using an exact byte
|
// We check for the effective serialized form because BIP16 defines a P2SH output using an exact byte
|
||||||
// template, not the logical program structure. Thus you can have two programs that look identical when
|
// template, not the logical program structure. Thus you can have two programs that look identical when
|
||||||
// printed out but one is a P2SH script and the other isn't! :(
|
// printed out but one is a P2SH script and the other isn't! :(
|
||||||
@ -51,7 +54,8 @@ public class ScriptPattern {
|
|||||||
chunks.get(2).equalsOpCode(OP_EQUAL);
|
chunks.get(2).equalsOpCode(OP_EQUAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isPayToPubKey(List<ScriptChunk> chunks) {
|
public static boolean isPayToPubKey(Script script) {
|
||||||
|
List<ScriptChunk> chunks = script.chunks;
|
||||||
return chunks.size() == 2 &&
|
return chunks.size() == 2 &&
|
||||||
chunks.get(1).equalsOpCode(OP_CHECKSIG) &&
|
chunks.get(1).equalsOpCode(OP_CHECKSIG) &&
|
||||||
!chunks.get(0).isOpCode() &&
|
!chunks.get(0).isOpCode() &&
|
||||||
@ -59,7 +63,8 @@ public class ScriptPattern {
|
|||||||
chunks.get(0).data.length > 1;
|
chunks.get(0).data.length > 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isSentToMultisig(List<ScriptChunk> chunks) {
|
public static boolean isSentToMultisig(Script script) {
|
||||||
|
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);
|
||||||
// Must end in OP_CHECKMULTISIG[VERIFY].
|
// Must end in OP_CHECKMULTISIG[VERIFY].
|
||||||
@ -82,7 +87,8 @@ public class ScriptPattern {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isSentToCltvPaymentChannel(List<ScriptChunk> chunks) {
|
public static boolean isSentToCltvPaymentChannel(Script script) {
|
||||||
|
List<ScriptChunk> chunks = script.chunks;
|
||||||
if (chunks.size() != 10) return false;
|
if (chunks.size() != 10) return false;
|
||||||
// Check that opcodes match the pre-determined format.
|
// Check that opcodes match the pre-determined format.
|
||||||
if (!chunks.get(0).equalsOpCode(OP_IF)) return false;
|
if (!chunks.get(0).equalsOpCode(OP_IF)) return false;
|
||||||
@ -98,7 +104,8 @@ public class ScriptPattern {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isOpReturn(List<ScriptChunk> chunks) {
|
public static boolean isOpReturn(Script script) {
|
||||||
|
List<ScriptChunk> chunks = script.chunks;
|
||||||
return chunks.size() > 0 && chunks.get(0).equalsOpCode(ScriptOpCodes.OP_RETURN);
|
return chunks.size() > 0 && chunks.get(0).equalsOpCode(ScriptOpCodes.OP_RETURN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2017 John L. Jegutanis
|
* Copyright 2017 John L. Jegutanis
|
||||||
|
* Copyright 2018 Andreas Schildbach
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -32,22 +33,22 @@ public class ScriptPatternTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testCommonScripts() {
|
public void testCommonScripts() {
|
||||||
assertTrue(ScriptPattern.isPayToPubKeyHash(
|
assertTrue(ScriptPattern.isPayToPubKeyHash(
|
||||||
ScriptBuilder.createOutputScript(keys.get(0).toAddress(MainNetParams.get())).getChunks()
|
ScriptBuilder.createOutputScript(keys.get(0).toAddress(MainNetParams.get()))
|
||||||
));
|
));
|
||||||
assertTrue(ScriptPattern.isPayToScriptHash(
|
assertTrue(ScriptPattern.isPayToScriptHash(
|
||||||
ScriptBuilder.createP2SHOutputScript(2, keys).getChunks()
|
ScriptBuilder.createP2SHOutputScript(2, keys)
|
||||||
));
|
));
|
||||||
assertTrue(ScriptPattern.isSentToMultisig(
|
assertTrue(ScriptPattern.isSentToMultisig(
|
||||||
ScriptBuilder.createMultiSigOutputScript(2, keys).getChunks()
|
ScriptBuilder.createMultiSigOutputScript(2, keys)
|
||||||
));
|
));
|
||||||
assertTrue(ScriptPattern.isPayToPubKey(
|
assertTrue(ScriptPattern.isPayToPubKey(
|
||||||
ScriptBuilder.createOutputScript(keys.get(0)).getChunks()
|
ScriptBuilder.createOutputScript(keys.get(0))
|
||||||
));
|
));
|
||||||
assertTrue(ScriptPattern.isSentToCltvPaymentChannel(
|
assertTrue(ScriptPattern.isSentToCltvPaymentChannel(
|
||||||
ScriptBuilder.createCLTVPaymentChannelOutput(BigInteger.ONE, keys.get(0), keys.get(1)).getChunks()
|
ScriptBuilder.createCLTVPaymentChannelOutput(BigInteger.ONE, keys.get(0), keys.get(1))
|
||||||
));
|
));
|
||||||
assertTrue(ScriptPattern.isOpReturn(
|
assertTrue(ScriptPattern.isOpReturn(
|
||||||
ScriptBuilder.createOpReturnScript(new byte[10]).getChunks()
|
ScriptBuilder.createOpReturnScript(new byte[10])
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user