README.md (3673B)
1 # script 2 3 [](https://hackage.haskell.org/package/ppad-script) 4  5 [](https://docs.ppad.tech/script) 6 7 Representations for [Script](https://en.bitcoin.it/wiki/Script), 8 including abstract syntax, 'ByteArray', and base16-encoded 'ByteString' 9 versions, as well as fast conversion utilities for working with them. 10 11 ## Usage 12 13 A sample GHCi session: 14 15 ``` 16 > :set -XOverloadedStrings 17 > 18 > -- import qualified 19 > import qualified Bitcoin.Prim.Script as S 20 > 21 > -- base16-encoded p2pkh scriptPubKey 22 > let p2pkh = "76a91489abcdefabbaabbaabbaabbaabbaabbaabbaabba88ac" 23 > 24 > -- bytearray-encoded 25 > let Just script = S.from_base16 p2pkh 26 > script 27 Script [ 0x76, 0xa9, 0x14, 0x89, 0xab, 0xcd, 0xef 28 , 0xab, 0xba, 0xab, 0xba, 0xab, 0xba, 0xab 29 , 0xba, 0xab, 0xba, 0xab, 0xba, 0xab, 0xba 30 , 0xab, 0xba, 0x88, 0xac 31 ] 32 > 33 > -- abstract syntax-encoded 34 > let terms = S.from_script script 35 > terms 36 [ OP_DUP, OP_HASH160, OP_PUSHBYTES_20, 0x89, 0xab, 0xcd, 0xef 37 , 0xab, 0xba, 0xab, 0xba, 0xab, 0xba, 0xab 38 , 0xba, 0xab, 0xba, 0xab, 0xba, 0xab, 0xba 39 , 0xab, 0xba, OP_EQUALVERIFY, OP_CHECKSIG 40 ] 41 > 42 > -- round-trip 43 > S.to_base16 (S.to_script terms) 44 "76a91489abcdefabbaabbaabbaabbaabbaabbaabbaabba88ac" 45 ``` 46 47 ## Documentation 48 49 Haddocks (API documentation, etc.) are hosted at 50 [docs.ppad.tech/script](https://docs.ppad.tech/script). 51 52 ## Performance 53 54 The aim is best-in-class performance for highly-auditable Haskell code. 55 56 Current benchmark figures on my M4 Silicon MacBook Air look like (use 57 `cabal bench` to run the benchmark suite): 58 59 ``` 60 benchmarking to_script 61 time 228.1 ns (227.9 ns .. 228.6 ns) 62 1.000 R² (1.000 R² .. 1.000 R²) 63 mean 228.9 ns (228.5 ns .. 230.0 ns) 64 std dev 2.167 ns (1.241 ns .. 3.840 ns) 65 66 benchmarking from_script 67 time 329.3 ns (327.4 ns .. 331.4 ns) 68 1.000 R² (1.000 R² .. 1.000 R²) 69 mean 331.3 ns (330.1 ns .. 332.3 ns) 70 std dev 3.502 ns (2.723 ns .. 4.492 ns) 71 72 benchmarking to_base16 73 time 150.3 ns (149.6 ns .. 150.9 ns) 74 1.000 R² (1.000 R² .. 1.000 R²) 75 mean 149.6 ns (149.3 ns .. 149.9 ns) 76 std dev 1.101 ns (884.2 ps .. 1.334 ns) 77 78 benchmarking from_base16 79 time 101.1 ns (100.8 ns .. 101.4 ns) 80 1.000 R² (1.000 R² .. 1.000 R²) 81 mean 100.7 ns (100.4 ns .. 101.0 ns) 82 std dev 949.7 ps (766.3 ps .. 1.162 ns) 83 ``` 84 85 where the inputs to the above functions are variations of the script found 86 in the 'Usage' section. 87 88 ## Security 89 90 This library aims at the maximum security achievable in a 91 garbage-collected language under an optimizing compiler such as GHC, in 92 which strict constant-timeness can be challenging to achieve. 93 94 If you discover any vulnerabilities, please disclose them via 95 security@ppad.tech. 96 97 ## Development 98 99 You'll require [Nix][nixos] with [flake][flake] support enabled. Enter a 100 development shell with: 101 102 ``` 103 $ nix develop 104 ``` 105 106 Then do e.g.: 107 108 ``` 109 $ cabal repl ppad-script 110 ``` 111 112 to get a REPL for the main library. 113 114 ## Attribution 115 116 The list of opcodes was originally taken 117 verbatim from the 'opcode' crate found in 118 [rust-bitcoin](https://github.com/rust-bitcoin/rust-bitcoin). 119 120 [nixos]: https://nixos.org/ 121 [flake]: https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html