Safe Haskell | None |
---|---|
Language | Haskell98 |
Bitcoin scripts
- newtype RawScript = RawScript {}
- newtype Script = Script {
- fromScript :: [Opcode]
- newtype ScriptHash = ScriptHash {}
- emptyRawScript :: RawScript
- emptyScript :: Script
- scriptHash :: RawScript -> ScriptHash
- rawScriptFromString :: String -> RawScript
- op_FALSE :: Opcode
- op_TRUE :: Opcode
- op_0 :: Opcode
- op_1 :: Opcode
- op_2 :: Opcode
- op_3 :: Opcode
- op_LT :: Opcode
- op_GT :: Opcode
- op_LE :: Opcode
- op_GE :: Opcode
- op_PUSHDATA :: ByteString -> Opcode
- op_BIGNUMBER :: Integer -> Opcode
- data Opcode
- = OP_SMALLNUM !Int
- | OP_1NEGATE
- | OP_PUSHDATA !Word8 !ByteString
- | OP_NOP !Word8
- | OP_IF
- | OP_NOTIF
- | OP_ELSE
- | OP_ENDIF
- | OP_VERIFY
- | OP_RETURN
- | OP_TOALTSTACK
- | OP_FROMALTSTACK
- | OP_IFDUP
- | OP_DEPTH
- | OP_DROP
- | OP_DUP
- | OP_NIP
- | OP_OVER
- | OP_PICK
- | OP_ROLL
- | OP_ROT
- | OP_SWAP
- | OP_TUCK
- | OP_2DROP
- | OP_2DUP
- | OP_3DUP
- | OP_2OVER
- | OP_2ROT
- | OP_2SWAP
- | OP_CAT
- | OP_SUBSTR
- | OP_LEFT
- | OP_RIGHT
- | OP_SIZE
- | OP_INVERT
- | OP_AND
- | OP_OR
- | OP_XOR
- | OP_EQUAL
- | OP_EQUALVERIFY
- | OP_1ADD
- | OP_1SUB
- | OP_2MUL
- | OP_2DIV
- | OP_NEGATE
- | OP_ABS
- | OP_NOT
- | OP_0NOTEQUAL
- | OP_ADD
- | OP_SUB
- | OP_MUL
- | OP_DIV
- | OP_MOD
- | OP_LSHIFT
- | OP_RSHIFT
- | OP_BOOLAND
- | OP_BOOLOR
- | OP_NUMEQUAL
- | OP_NUMEQUALVERIFY
- | OP_NUMNOTEQUAL
- | OP_LESSTHAN
- | OP_GREATERTHAN
- | OP_LESSTHANOREQUAL
- | OP_GREATERTHANOREQUAL
- | OP_MIN
- | OP_MAX
- | OP_WITHIN
- | OP_RIPEMD160
- | OP_SHA1
- | OP_SHA256
- | OP_HASH160
- | OP_HASH256
- | OP_CODESEPARATOR
- | OP_CHECKSIG
- | OP_CHECKSIGVERIFY
- | OP_CHECKMULTISIG
- | OP_CHECKMULTISIGVERIFY
- | OP_RESERVED
- | OP_VER
- | OP_VERIF
- | OP_VERNOTIF
- | OP_RESERVED1
- | OP_RESERVED2
- | OP_INVALIDOPCODE
- | OP_UNKNOWN !Word8
- is_op_pushdata :: Opcode -> Maybe ByteString
- is_valid_pushdata :: Word8 -> ByteString -> Bool
- is_op_smallnum :: Opcode -> Maybe Int
- is_nop :: Word8 -> Bool
- showOpcode :: Opcode -> String -> String
- showList' :: (a -> String -> String) -> [a] -> String -> String
- showOpcodeList :: [Opcode] -> String -> String
Documentation
A raw script (octet stream)
A parsed script (opcode stream)
Script | |
|
newtype ScriptHash Source #
The 160-bit hash of a (raw) script
emptyScript :: Script Source #
scriptHash :: RawScript -> ScriptHash Source #
shorthands
Pushes the corresponding number to the stack (0 is represented by an empty array)
op_PUSHDATA :: ByteString -> Opcode Source #
Figures out which opcode to use
op_BIGNUMBER :: Integer -> Opcode Source #
Pushes a (signed) integer onto the stack, never using OP_SMALLNUM (except for 0, which is represented by the empty array)
opcodes
OP_SMALLNUM !Int | OP_0, OP_1 .. OP_16 (bytes 0,81,82..96). Pushes the number to the stack (0 is represented by an empty array) |
OP_1NEGATE | The number -1 is pushed onto the stack. |
OP_PUSHDATA !Word8 !ByteString | Pushes data to the stack. The Word8 is the opcode: it can be 0, 1..75 and 76,77,78. |
OP_NOP !Word8 | Does nothing. The argument is the opcode, either 0x61 or 0xb0-0xb9 |
OP_IF | If the top stack value is not 0, the statements are executed. The top stack value is removed. |
OP_NOTIF | If the top stack value is 0, the statements are executed. The top stack value is removed. |
OP_ELSE | If the preceding OP_IF or OP_NOTIF or OP_ELSE was not executed then these statements are and if the preceding OP_IF or OP_NOTIF or OP_ELSE was executed then these statements are not. |
OP_ENDIF | Ends an if/else block. |
OP_VERIFY | Marks transaction as invalid if top stack value is not true. True is removed, but false is not. |
OP_RETURN | Marks transaction as invalid. |
OP_TOALTSTACK | Puts the input onto the top of the alt stack. Removes it from the main stack. |
OP_FROMALTSTACK | Puts the input onto the top of the main stack. Removes it from the alt stack. |
OP_IFDUP | If the top stack value is not 0, duplicate it. |
OP_DEPTH | Puts the number of stack items onto the stack. |
OP_DROP | Removes the top stack item. |
OP_DUP | Duplicates the top stack item. |
OP_NIP | Removes the second-to-top stack item. |
OP_OVER | Copies the second-to-top stack item to the top. |
OP_PICK | The item n back in the stack is copied to the top. |
OP_ROLL | The item n back in the stack is moved to the top. |
OP_ROT | The top three items on the stack are rotated to the left. |
OP_SWAP | The top two items on the stack are swapped. |
OP_TUCK | The item at the top of the stack is copied and inserted before the second-to-top item. |
OP_2DROP | Removes the top two stack items. |
OP_2DUP | Duplicates the top two stack items. |
OP_3DUP | Duplicates the top three stack items. |
OP_2OVER | Copies the pair of items two spaces back in the stack to the front. |
OP_2ROT | The fifth and sixth items back are moved to the top of the stack. |
OP_2SWAP | Swaps the top two pairs of items. |
OP_CAT | Concatenates two strings. Currently disabled. |
OP_SUBSTR | Returns a section of a string. Currently disabled. |
OP_LEFT | Keeps only characters left of the specified point in a string. Currently disabled. |
OP_RIGHT | Keeps only characters right of the specified point in a string. Currently disabled. |
OP_SIZE | Returns the length of the input string. |
OP_INVERT | Flips all of the bits in the input. Currently disabled. |
OP_AND | Boolean and between each bit in the inputs. Currently disabled. |
OP_OR | Boolean or between each bit in the inputs. Currently disabled. |
OP_XOR | Boolean exclusive or between each bit in the inputs. Currently disabled. |
OP_EQUAL | Returns 1 if the inputs are exactly equal, 0 otherwise. |
OP_EQUALVERIFY | Same as OP_EQUAL, but runs OP_VERIFY afterward. |
OP_1ADD | 1 is added to the input. |
OP_1SUB | 1 is subtracted from the input. |
OP_2MUL | The input is multiplied by 2. Currently disabled. |
OP_2DIV | The input is divided by 2. Currently disabled. |
OP_NEGATE | The sign of the input is flipped. |
OP_ABS | The input is made positive. |
OP_NOT | If the input is 0 or 1, it is flipped. Otherwise the output will be 0. |
OP_0NOTEQUAL | Returns 0 if the input is 0. 1 otherwise. |
OP_ADD | a is added to b. |
OP_SUB | b is subtracted from a. |
OP_MUL | a is multiplied by b. Currently disabled. |
OP_DIV | a is divided by b. Currently disabled. |
OP_MOD | Returns the remainder after dividing a by b. Currently disabled. |
OP_LSHIFT | Shifts a left b bits, preserving sign. Currently disabled. |
OP_RSHIFT | Shifts a right b bits, preserving sign. Currently disabled. |
OP_BOOLAND | If both a and b are not 0, the output is 1. Otherwise 0. |
OP_BOOLOR | If a or b is not 0, the output is 1. Otherwise 0. |
OP_NUMEQUAL | Returns 1 if the numbers are equal, 0 otherwise. |
OP_NUMEQUALVERIFY | Same as OP_NUMEQUAL, but runs OP_VERIFY afterward. |
OP_NUMNOTEQUAL | Returns 1 if the numbers are not equal, 0 otherwise. |
OP_LESSTHAN | Returns 1 if a is less than b, 0 otherwise. |
OP_GREATERTHAN | Returns 1 if a is greater than b, 0 otherwise. |
OP_LESSTHANOREQUAL | Returns 1 if a is less than or equal to b, 0 otherwise. |
OP_GREATERTHANOREQUAL | Returns 1 if a is greater than or equal to b, 0 otherwise. |
OP_MIN | Returns the smaller of a and b. |
OP_MAX | Returns the larger of a and b. |
OP_WITHIN | Returns 1 if x is within the specified range (left-inclusive), 0 otherwise |
OP_RIPEMD160 | The input is hashed using RIPEMD-160. |
OP_SHA1 | The input is hashed using SHA-1. |
OP_SHA256 | The input is hashed using SHA-256. |
OP_HASH160 | The input is hashed twice: first with SHA-256 and then with RIPEMD-160. |
OP_HASH256 | The input is hashed two times with SHA-256. |
OP_CODESEPARATOR | All of the signature checking words will only match signatures to the data after the most recently-executed OP_CODESEPARATOR. |
OP_CHECKSIG | The entire transaction's outputs, inputs, and script (from the most recently-executed OP_CODESEPARATOR to the end) are hashed. The signature used by OP_CHECKSIG must be a valid signature for this hash and public key. If it is, 1 is returned, 0 otherwise. |
OP_CHECKSIGVERIFY | Same as OP_CHECKSIG, but OP_VERIFY is executed afterward. |
OP_CHECKMULTISIG | For each signature and public key pair, OP_CHECKSIG is executed. If more public keys than signatures are listed, some key/sig pairs can fail. All signatures need to match a public key. If all signatures are valid, 1 is returned, 0 otherwise. Due to a bug, one extra unused value is removed from the stack. |
OP_CHECKMULTISIGVERIFY | Same as OP_CHECKMULTISIG, but OP_VERIFY is executed afterward. |
OP_RESERVED | Transaction is invalid unless occuring in an unexecuted OP_IF branch |
OP_VER | Transaction is invalid unless occuring in an unexecuted OP_IF branch |
OP_VERIF | Transaction is invalid even when occuring in an unexecuted OP_IF branch |
OP_VERNOTIF | Transaction is invalid even when occuring in an unexecuted OP_IF branch |
OP_RESERVED1 | Transaction is invalid unless occuring in an unexecuted OP_IF branch |
OP_RESERVED2 | Transaction is invalid unless occuring in an unexecuted OP_IF branch |
OP_INVALIDOPCODE | this is a pseudo opcode, but it appears in the testnet3 blockchain... |
OP_UNKNOWN !Word8 | unknown opcodes also appear in the testnet, inside (unexecuted?) OP_IF branches... |
helper functions
is_op_pushdata :: Opcode -> Maybe ByteString Source #
Note: this function returns Nothing
for OP_0
(which technically pushes an empty array to the stack).
Also we don't check for validity (see is_valid_pushdata
)
is_valid_pushdata :: Word8 -> ByteString -> Bool Source #
Note that OP_0 = push empty array, so we accept that as a valid OP_PUSHDATA