commit ef0497853f5e7957eb68be62de08e4408990bcb1
parent 419cf7db0340ad131c83208efeb76dab3b3c3d9c
Author: Jared Tobin <jared@jtobin.io>
Date: Mon, 20 Jan 2025 11:45:07 +0400
lib: rename OP_PUSHNUM_N to OP_N
Shorter, and commonly used. Also renames OP_PUSHNUM_NEG1 to OP_1NEGATE.
Diffstat:
2 files changed, 32 insertions(+), 18 deletions(-)
diff --git a/lib/Bitcoin/Prim/Script.hs b/lib/Bitcoin/Prim/Script.hs
@@ -85,10 +85,20 @@ hilo b =
-- script and term representation ---------------------------------------------
-- | A Script program, represented as a 'ByteArray'.
+--
+-- >>> from_base16 "0014b472a266d0bd89c13706a4132ccfb16f7c3b9fcb"
+-- Just (Script
+-- [ 0x00, 0x14, 0xb4, 0x72, 0xa2, 0x66, 0xd0, 0xbd, 0x89, 0xc1, 0x37
+-- , 0x06, 0xa4, 0x13, 0x2c, 0xcf, 0xb1, 0x6f, 0x7c, 0x3b, 0x9f, 0xcb])
newtype Script = Script BA.ByteArray
deriving (Eq, Show)
-- | Terms of the Script language (either opcodes or bytes).
+--
+-- >>> OPCODE OP_RETURN
+-- OP_RETURN
+-- >>> BYTE 0x00
+-- 0x00
data Term =
OPCODE {-# UNPACK #-} !Opcode
| BYTE {-# UNPACK #-} !Word8
@@ -103,6 +113,10 @@ instance Show Term where
-- script conversions ---------------------------------------------------------
-- | Convert a 'Script' to a base16-encoded ByteString.
+--
+-- >>> let script = to_script [OPCODE OP_1, OPCODE OP_2, OPCODE OP_ADD]
+-- >>> to_base16 script
+-- "515293"
to_base16 :: Script -> BS.ByteString
to_base16 (Script ba) = B16.encode (ba_to_bs ba)
{-# INLINE to_base16 #-}
@@ -255,24 +269,24 @@ data Opcode =
| OP_PUSHDATA1
| OP_PUSHDATA2
| OP_PUSHDATA4
- | OP_PUSHNUM_NEG1
+ | OP_1NEGATE
| OP_RESERVED
- | OP_PUSHNUM_1
- | OP_PUSHNUM_2
- | OP_PUSHNUM_3
- | OP_PUSHNUM_4
- | OP_PUSHNUM_5
- | OP_PUSHNUM_6
- | OP_PUSHNUM_7
- | OP_PUSHNUM_8
- | OP_PUSHNUM_9
- | OP_PUSHNUM_10
- | OP_PUSHNUM_11
- | OP_PUSHNUM_12
- | OP_PUSHNUM_13
- | OP_PUSHNUM_14
- | OP_PUSHNUM_15
- | OP_PUSHNUM_16
+ | OP_1
+ | OP_2
+ | OP_3
+ | OP_4
+ | OP_5
+ | OP_6
+ | OP_7
+ | OP_8
+ | OP_9
+ | OP_10
+ | OP_11
+ | OP_12
+ | OP_13
+ | OP_14
+ | OP_15
+ | OP_16
| OP_NOP
| OP_VER
| OP_IF
diff --git a/test/Main.hs b/test/Main.hs
@@ -125,7 +125,7 @@ p2sh :: BS.ByteString
p2sh = "5221038282263212c609d9ea2a6e3e172de238d8c39cabe56f3f9e451d2c4c7739ba8721031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f2102b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a873753ae"
p2sh_terms :: [Term]
-p2sh_terms = [OPCODE OP_PUSHNUM_2,OPCODE OP_PUSHBYTES_33,BYTE 0x03,BYTE 0x82,BYTE 0x82,BYTE 0x26,BYTE 0x32,BYTE 0x12,BYTE 0xc6,BYTE 0x09,BYTE 0xd9,BYTE 0xea,BYTE 0x2a,BYTE 0x6e,BYTE 0x3e,BYTE 0x17,BYTE 0x2d,BYTE 0xe2,BYTE 0x38,BYTE 0xd8,BYTE 0xc3,BYTE 0x9c,BYTE 0xab,BYTE 0xe5,BYTE 0x6f,BYTE 0x3f,BYTE 0x9e,BYTE 0x45,BYTE 0x1d,BYTE 0x2c,BYTE 0x4c,BYTE 0x77,BYTE 0x39,BYTE 0xba,BYTE 0x87,OPCODE OP_PUSHBYTES_33,BYTE 0x03,BYTE 0x1b,BYTE 0x84,BYTE 0xc5,BYTE 0x56,BYTE 0x7b,BYTE 0x12,BYTE 0x64,BYTE 0x40,BYTE 0x99,BYTE 0x5d,BYTE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,BYTE 0x40,BYTE 0x99,BYTE 0x5d,BYTE 0x3e,BYTE 0xd5,BYTE 0xaa,BYTE 0xba,BYTE 0x05,BYTE 0x65,BYTE 0xd7,BYTE 0x1e,BYTE 0x18,BYTE 0x34,BYTE 0x60,BYTE 0x48,BYTE 0x19,BYTE 0xff,BYTE 0x9c,BYTE 0x17,BYTE 0xf5,BYTE 0xe9,BYTE 0xd5,BYTE 0xdd,BYTE 0x07,BYTE 0x8f,OPCODE OP_PUSHBYTES_33,BYTE 0x02,BYTE 0xb4,BYTE 0x63,BYTE 0x2d,BYTE 0x08,BYTE 0x48,BYTE 0x5f,BYTE 0xf1,BYTE 0xdf,BYTE 0x2d,BYTE 0xb5,BYTE 0x5b,BYTE 0x9d,BYTE 0xaf,BYTE 0xd2,BYTE 0x33,BYTE 0x47,BYTE 0xd1,BYTE 0xc4,BYTE 0x7a,BYTE 0x45,BYTE 0x70,BYTE 0x72,BYTE 0xa1,BYTE 0xe8,BYTE 0x7b,BYTE 0xe2,BYTE 0x68,BYTE 0x96,BYTE 0x54,BYTE 0x9a,BYTE 0x87,BYTE 0x37,OPCODE OP_3,OPCODE OP_CHECKMULTISIG]
p2sh_script_decodes_as_expected :: H.Assertion
p2sh_script_decodes_as_expected = p2sh `decodes_to` p2sh_terms