├── docs ├── bitcoinml │ ├── Bitcoinml │ │ ├── .dune-keep │ │ ├── Base58 │ │ │ └── index.html │ │ ├── Script_verify │ │ │ ├── Sigver │ │ │ │ └── index.html │ │ │ ├── SStack │ │ │ │ └── index.html │ │ │ └── index.html │ │ ├── Address │ │ │ └── Bech32 │ │ │ │ └── index.html │ │ ├── Script_witnesscommitment │ │ │ ├── index.html │ │ │ ├── Output │ │ │ │ └── index.html │ │ │ └── Script_witnesspubkeyhash │ │ │ │ └── index.html │ │ ├── Script_pubkey │ │ │ ├── index.html │ │ │ ├── Input │ │ │ │ └── index.html │ │ │ ├── Script_pubkey │ │ │ │ └── index.html │ │ │ └── Output │ │ │ │ └── index.html │ │ ├── Script_nulldata │ │ │ ├── index.html │ │ │ ├── Script_nulldata │ │ │ │ └── index.html │ │ │ └── Output │ │ │ │ └── index.html │ │ ├── Script_pubkeyhash │ │ │ ├── index.html │ │ │ ├── Input │ │ │ │ └── index.html │ │ │ ├── Script_pubkeyhash │ │ │ │ └── index.html │ │ │ └── Output │ │ │ │ └── index.html │ │ ├── Script_scripthash │ │ │ ├── index.html │ │ │ ├── Input │ │ │ │ └── index.html │ │ │ ├── Script_scripthash │ │ │ │ └── index.html │ │ │ └── Output │ │ │ │ └── index.html │ │ ├── Script_multisig │ │ │ ├── Input │ │ │ │ └── index.html │ │ │ ├── index.html │ │ │ ├── Script_multisig │ │ │ │ └── index.html │ │ │ └── Output │ │ │ │ └── index.html │ │ ├── Script_witnesspubkeyhash │ │ │ ├── index.html │ │ │ ├── Input │ │ │ │ └── index.html │ │ │ ├── Script_witnesspubkeyhash │ │ │ │ └── index.html │ │ │ └── Output │ │ │ │ └── index.html │ │ ├── Script_witnessscripthash │ │ │ ├── index.html │ │ │ ├── Input │ │ │ │ └── index.html │ │ │ ├── Script_witnessscripthash │ │ │ │ └── index.html │ │ │ └── Output │ │ │ │ └── index.html │ │ ├── Script_template │ │ │ ├── EmptyInput │ │ │ │ └── index.html │ │ │ ├── module-type-Input │ │ │ │ └── index.html │ │ │ ├── Make_template │ │ │ │ ├── argument-1-ITemplate │ │ │ │ │ └── index.html │ │ │ │ ├── argument-2-OTemplate │ │ │ │ │ └── index.html │ │ │ │ └── index.html │ │ │ ├── module-type-Output │ │ │ │ └── index.html │ │ │ └── index.html │ │ ├── Merkle │ │ │ └── index.html │ │ ├── Varint │ │ │ └── index.html │ │ └── Tx │ │ │ └── Witness │ │ │ └── index.html │ ├── Bitcoinml__Base58 │ │ ├── .dune-keep │ │ └── index.html │ ├── Bitcoinml__Block │ │ └── .dune-keep │ ├── Bitcoinml__Hash │ │ └── .dune-keep │ ├── Bitcoinml__Merkle │ │ ├── .dune-keep │ │ └── index.html │ ├── Bitcoinml__Params │ │ └── .dune-keep │ ├── Bitcoinml__Proto │ │ └── .dune-keep │ ├── Bitcoinml__Script │ │ └── .dune-keep │ ├── Bitcoinml__Tx │ │ ├── .dune-keep │ │ └── Witness │ │ │ └── index.html │ ├── Bitcoinml__Varint │ │ ├── .dune-keep │ │ └── index.html │ ├── Bitcoinml__Address │ │ ├── .dune-keep │ │ └── Bech32 │ │ │ └── index.html │ ├── Bitcoinml__Block_lazy │ │ └── .dune-keep │ ├── Bitcoinml__Script_multisig │ │ ├── .dune-keep │ │ ├── Input │ │ │ └── index.html │ │ ├── index.html │ │ ├── Script_multisig │ │ │ └── index.html │ │ └── Output │ │ │ └── index.html │ ├── Bitcoinml__Script_nulldata │ │ ├── .dune-keep │ │ ├── index.html │ │ ├── Script_nulldata │ │ │ └── index.html │ │ └── Output │ │ │ └── index.html │ ├── Bitcoinml__Script_pubkey │ │ ├── .dune-keep │ │ ├── index.html │ │ ├── Input │ │ │ └── index.html │ │ ├── Script_pubkey │ │ │ └── index.html │ │ └── Output │ │ │ └── index.html │ ├── Bitcoinml__Script_template │ │ ├── .dune-keep │ │ ├── EmptyInput │ │ │ └── index.html │ │ ├── module-type-Input │ │ │ └── index.html │ │ ├── Make_template │ │ │ ├── argument-1-ITemplate │ │ │ │ └── index.html │ │ │ ├── index.html │ │ │ └── argument-2-OTemplate │ │ │ │ └── index.html │ │ ├── index.html │ │ └── module-type-Output │ │ │ └── index.html │ ├── Bitcoinml__Script_verify │ │ ├── .dune-keep │ │ ├── Sigver │ │ │ └── index.html │ │ └── SStack │ │ │ └── index.html │ ├── Bitcoinml__Script_pubkeyhash │ │ ├── .dune-keep │ │ ├── index.html │ │ ├── Input │ │ │ └── index.html │ │ ├── Script_pubkeyhash │ │ │ └── index.html │ │ └── Output │ │ │ └── index.html │ ├── Bitcoinml__Script_scripthash │ │ ├── .dune-keep │ │ ├── index.html │ │ ├── Input │ │ │ └── index.html │ │ ├── Script_scripthash │ │ │ └── index.html │ │ └── Output │ │ │ └── index.html │ ├── Bitcoinml__Script_witnesscommitment │ │ ├── .dune-keep │ │ ├── index.html │ │ ├── Output │ │ │ └── index.html │ │ └── Script_witnesspubkeyhash │ │ │ └── index.html │ ├── Bitcoinml__Script_witnesspubkeyhash │ │ ├── .dune-keep │ │ ├── index.html │ │ ├── Input │ │ │ └── index.html │ │ ├── Script_witnesspubkeyhash │ │ │ └── index.html │ │ └── Output │ │ │ └── index.html │ ├── Bitcoinml__Script_witnessscripthash │ │ ├── .dune-keep │ │ ├── index.html │ │ ├── Input │ │ │ └── index.html │ │ ├── Script_witnessscripthash │ │ │ └── index.html │ │ └── Output │ │ │ └── index.html │ └── index.html └── index.html ├── src ├── experimental │ ├── simplicity.mli │ ├── simplicity_test.mli │ ├── psbt.ml │ ├── .merlin │ ├── _dune │ ├── psbt.mli │ ├── script_abstraction.mli │ ├── simplicity.ml │ └── simplicity_test.ml ├── base58.mli ├── dune ├── merkle.mli ├── merkle.ml ├── script_nulldata.ml ├── varint.mli ├── script_multisig.ml ├── script_template.ml ├── script_witnessscripthash.ml ├── script_witnesspubkeyhash.ml ├── script_scripthash.ml ├── params.mli ├── base58.ml ├── block_lazy.mli ├── script_pubkey.ml ├── address.mli ├── script_verify.mli ├── script_witnesscommitment.ml ├── hash.mli ├── script_pubkeyhash.ml ├── varint.ml ├── block.mli ├── block_lazy.ml ├── proto.mli └── address.ml ├── dune-project ├── .gitignore ├── test ├── dune ├── proto_test.ml ├── test.ml └── address_test.ml ├── Makefile ├── .travis.yml ├── bitcoinml.opam └── LICENSE /docs/bitcoinml/Bitcoinml/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/experimental/simplicity.mli: -------------------------------------------------------------------------------- 1 | type t -------------------------------------------------------------------------------- /src/experimental/simplicity_test.mli: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Base58/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Block/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Hash/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Merkle/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Params/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Proto/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Tx/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Varint/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Address/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Block_lazy/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_multisig/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_nulldata/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_pubkey/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_template/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_verify/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /dune-project: -------------------------------------------------------------------------------- 1 | (lang dune 1.0) 2 | (name bitcoinml) 3 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_pubkeyhash/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_scripthash/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_witnesscommitment/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_witnesspubkeyhash/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_witnessscripthash/.dune-keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | _build 2 | bitcoinml.* 3 | test/data/*.hex 4 | .vscode/* -------------------------------------------------------------------------------- /src/experimental/psbt.ml: -------------------------------------------------------------------------------- 1 | 2 | type t = { 3 | version : int 4 | } 5 | 6 | -------------------------------------------------------------------------------- /src/experimental/.merlin: -------------------------------------------------------------------------------- 1 | B ../../_build/default/src/experimental 2 | FLG -open Experimental -w -40 3 | PKG cryptokit 4 | PKG unix 5 | PKG zarith 6 | -------------------------------------------------------------------------------- /src/base58.mli: -------------------------------------------------------------------------------- 1 | (** Base58 encoding utilities *) 2 | 3 | val encode_check : string -> string 4 | (** [encode_check data] encoded [data] in base58_check *) -------------------------------------------------------------------------------- /src/dune: -------------------------------------------------------------------------------- 1 | (library 2 | (name bitcoinml) 3 | (public_name bitcoinml) 4 | (libraries bitstring cryptokit stdint bignum hex) 5 | (preprocess 6 | (pps bitstring.ppx))) 7 | -------------------------------------------------------------------------------- /src/experimental/_dune: -------------------------------------------------------------------------------- 1 | (jbuild_version 1) 2 | 3 | (library 4 | ((name experimental) 5 | (public_name bitcoinml.experimental) 6 | (libraries (cryptokit)))) 7 | 8 | -------------------------------------------------------------------------------- /test/dune: -------------------------------------------------------------------------------- 1 | (executable 2 | (name test) 3 | (libraries bitcoinml oUnit hex)) 4 | 5 | (alias 6 | (name runtest) 7 | (deps 8 | (:< test.exe)) 9 | (action 10 | (run %{<}))) 11 | -------------------------------------------------------------------------------- /src/merkle.mli: -------------------------------------------------------------------------------- 1 | type t = Hash.t 2 | 3 | val of_txs : Tx.t list -> t 4 | (** [of_txs txl] returns the merkle tree hash of the [txl] transaction set *) 5 | 6 | val of_hashes : Hash.t list -> t 7 | (** [of_hashes hl] returns the merkle tree hash of the [hl] hash list *) 8 | -------------------------------------------------------------------------------- /src/experimental/psbt.mli: -------------------------------------------------------------------------------- 1 | open Stdint 2 | open Bitstring 3 | 4 | 5 | type t = { 6 | version : int 7 | } 8 | 9 | 10 | 11 | (* 12 | val create : unit -> t 13 | val set_lock_time : t -> uint32 -> t 14 | val set_version : t -> int -> t 15 | val add_input : t -> Hash.t -> int -> int -> Script.t -> t 16 | val add_output : t -> string -> uint32 -> t 17 | val sign : t -> int -> Keypair.t -> Script.t -> string -> int -> Script.t -> t 18 | val build : t -> Tx.t 19 | *) 20 | 21 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | dune build @install @runtest @doc --profile release 3 | rm -r docs/* && cp -r _build/default/_doc/_html/* docs/ 4 | clean: 5 | rm -rf _build 6 | 7 | 8 | .PHONY : coverage 9 | coverage : clean 10 | BISECT_ENABLE=YES jbuilder runtest 11 | bisect-ppx-report -I _build/default/ -html _coverage/ `find . -name 'bisect*.out'` 12 | 13 | 14 | .PHONY : pin 15 | pin: 16 | opam pin add bitcoinml . -n --working-dir && opam remove bitcoinml && opam install bitcoinml --working-dir 17 | -------------------------------------------------------------------------------- /src/merkle.ml: -------------------------------------------------------------------------------- 1 | type t = Hash.t;; 2 | 3 | let of_hashes hashl = 4 | let rec mround hs = match hs with 5 | | x' :: [] -> [Hash.dsha256 (x' ^ x')] 6 | | x' :: x'' :: hs' -> Hash.dsha256 (x' ^ x'') :: mround hs' 7 | | [] -> [] 8 | in 9 | let rec m hs = match List.length hs with 10 | | 0 -> Hash.of_bin @@ Hash.dsha256 (Hash.to_bin Hash.zero) 11 | | 1 -> Hash.of_bin (List.hd hs) 12 | | n when n > 1 -> m @@ mround hs 13 | in 14 | List.map (fun h -> Hash.to_bin h) hashl |> m 15 | ;; 16 | 17 | let of_txs txl = List.map (fun tx -> tx.Tx.hash) txl |> of_hashes;; -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | sudo: required 3 | install: test -e .travis-opam.sh || wget https://raw.githubusercontent.com/ocaml/ocaml-ci-scripts/master/.travis-opam.sh 4 | script: bash -ex .travis-opam.sh 5 | env: 6 | - OCAML_VERSION=4.05 PACKAGE="bitcoinml" TESTS=true 7 | - OCAML_VERSION=4.06 PACKAGE="bitcoinml" TESTS=true 8 | - OCAML_VERSION=4.07 PACKAGE="bitcoinml" TESTS=true 9 | - OCAML_VERSION=4.08 PACKAGE="bitcoinml" TESTS=true 10 | - OCAML_VERSION=4.09 PACKAGE="bitcoinml" TESTS=true 11 | - OCAML_VERSION=4.10 PACKAGE="bitcoinml" TESTS=true 12 | os: 13 | - linux 14 | dist: bionic 15 | -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | index 5 | 6 | 7 | 8 | 9 | 10 |
11 |
12 |

OCaml package documentation

13 |
    14 |
  1. bitcoinml 0.4.2
  2. 15 |
16 |
17 |
18 | 19 | -------------------------------------------------------------------------------- /src/experimental/script_abstraction.mli: -------------------------------------------------------------------------------- 1 | type ScriptKey = 2 | | ScriptPubKey of string 3 | | PubKey of string 4 | | Address of string 5 | 6 | 7 | type ScriptAbstraction = 8 | | P2PKH of ScriptKey 9 | | P2WPKH of ScriptKey 10 | | P2SH of ScriptKey 11 | | P2WSH of ScriptKey 12 | | P2PK of ScriptKey 13 | | Nulldata of string 14 | | Multisig of int * PubKey list 15 | | AbsoluteTimelock of Script.t * ScriptAbstraction 16 | | RelativeTimelock of Script.t * ScriptAbstraction 17 | | IfElse of ScriptAbstraction * ScriptAbstraction 18 | | Hashlock256 of Script.t * ScriptPubKey 19 | | Hashlock160 of Script.t * ScriptPubKey -------------------------------------------------------------------------------- /src/script_nulldata.ml: -------------------------------------------------------------------------------- 1 | open Script;; 2 | 3 | module Input = Script_template.EmptyInput;; 4 | 5 | module Output = struct 6 | type t = Script.data;; 7 | let name = "nulldata";; 8 | 9 | let check s = 10 | match fst s with 11 | | OP_RETURN (data) :: [] -> true 12 | | _ -> false 13 | ;; 14 | 15 | let encode data = Script.of_opcodes [ OP_RETURN (data) ];; 16 | 17 | let decode s = 18 | match fst s with 19 | | OP_RETURN (data) :: [] -> data 20 | | _ -> "" 21 | ;; 22 | 23 | let spendable_by s prefix = "";; 24 | end 25 | 26 | 27 | module Script_nulldata = Script_template.Make_template (Input) (Output);; -------------------------------------------------------------------------------- /src/varint.mli: -------------------------------------------------------------------------------- 1 | (** A VarInt (variable integer) is a field used in transaction data to indicate the number 2 | of upcoming fields, or the length of an upcoming field. *) 3 | open Stdint 4 | 5 | val parse_varint : Bitstring.t -> Uint64.t * Bitstring.t 6 | (** [parse_varint bits] parse Bitstring [bits] returing the Uint64.t value and the rest 7 | of the bitstring *) 8 | 9 | val bitstring_of_varint : Int64.t -> Bitstring.t 10 | (** [bitstring_of_varint num] encodes [num] to its bitstring varint representation *) 11 | 12 | val encoding_length : Uint64.t -> int 13 | (** [encoding_length num] returns the length of [num] if encoded as varint *) 14 | -------------------------------------------------------------------------------- /test/proto_test.ml: -------------------------------------------------------------------------------- 1 | open Bitcoinml;; 2 | open Stdint;; 3 | open OUnit2;; 4 | open Hex;; 5 | 6 | (* 7 | let parse_test prefix pub addr octx = 8 | assert_equal addr @@ Address.of_pubhash prefix (Hex.to_string pub) 9 | ;; 10 | 11 | let serialize_test msg octx = 12 | let ad = Proto.serialize msg in 13 | assert_equal address @@ ad 14 | ;; 15 | *) 16 | 17 | let tlist = [ 18 | (*"proto.parse" >:: address_of_pubhash_test 0x05 (`Hex "010966776006953D5567439E5E39F86A0D273BEE") "31nVrspaydBz8aMpxH9WkS2DuhgqS1fCuG"; 19 | "proto.serialize" >:: address_of_pubhash_test 0x00 (`Hex "010966776006953D5567439E5E39F86A0D273BEE") "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"; 20 | *) 21 | ];; -------------------------------------------------------------------------------- /src/script_multisig.ml: -------------------------------------------------------------------------------- 1 | open Script;; 2 | 3 | let op_int_base = Script.opcode_to_hex OP_RESERVED;; 4 | 5 | module Input = struct 6 | type t = int;; 7 | let check v = true;; 8 | let encode v = Script.empty;; 9 | let decode v = 0;; 10 | end 11 | 12 | module Output = struct 13 | type t = { 14 | m: int; 15 | pubkeys: string list; 16 | };; 17 | let name = "p2ms";; 18 | 19 | let check v = true;; 20 | 21 | let encode v = Script.of_opcodes [ OP_CHECKMULTISIG ];; 22 | 23 | let decode s = { 24 | m= 0; 25 | pubkeys= [] 26 | };; 27 | 28 | let spendable_by s prefix = "";; 29 | end 30 | 31 | 32 | module Script_multisig = Script_template.Make_template (Script_template.EmptyInput) (Output);; 33 | -------------------------------------------------------------------------------- /src/script_template.ml: -------------------------------------------------------------------------------- 1 | 2 | module EmptyInput = struct 3 | type t = string;; 4 | let check v = false;; 5 | let encode v = Script.empty;; 6 | let decode v = "";; 7 | end 8 | 9 | 10 | module type Output = sig 11 | type t 12 | 13 | val name: string 14 | val encode: t -> Script.t 15 | val decode: Script.t -> t 16 | val check: Script.t -> bool 17 | val spendable_by: Script.t -> Address.prefix -> Address.t 18 | end 19 | 20 | module type Input = sig 21 | type t 22 | 23 | val encode: t -> Script.t 24 | val decode: Script.t -> t 25 | val check: Script.t -> bool 26 | end 27 | 28 | 29 | module Make_template (ITemplate: Input) (OTemplate: Output) = struct 30 | type t_inp = ITemplate;; 31 | type t_out = OTemplate;; 32 | 33 | let input = ITemplate;; 34 | let output = OTemplate;; 35 | end 36 | -------------------------------------------------------------------------------- /src/script_witnessscripthash.ml: -------------------------------------------------------------------------------- 1 | open Script;; 2 | open Address;; 3 | 4 | module Input = struct 5 | type t = int;; 6 | let check v = true;; 7 | let encode v = Script.empty;; 8 | let decode v = 0;; 9 | end 10 | 11 | module Output = struct 12 | type t = string;; 13 | let name = "p2wsh";; 14 | 15 | let check s = 16 | match fst s with 17 | | OP_0 :: OP_DATA (32, wsh) :: [] -> true 18 | | _ -> false 19 | ;; 20 | 21 | let encode wsh = Script.of_opcodes [ OP_0; OP_DATA (32, wsh) ];; 22 | 23 | let decode s = 24 | match fst s with 25 | | OP_0 :: OP_DATA (32, wsh) :: [] -> wsh 26 | | _ -> "" 27 | ;; 28 | 29 | let spendable_by s prefix = decode s |> Address.of_witness prefix.hrp 0x00;; 30 | end 31 | 32 | 33 | module Script_witnessscripthash = Script_template.Make_template (Input) (Output);; -------------------------------------------------------------------------------- /src/script_witnesspubkeyhash.ml: -------------------------------------------------------------------------------- 1 | open Script;; 2 | open Address;; 3 | 4 | 5 | module Input = struct 6 | type t = int;; 7 | let check v = true;; 8 | let encode v = Script.empty;; 9 | let decode v = 0;; 10 | end 11 | 12 | module Output = struct 13 | type t = string;; 14 | let name = "p2wpkh";; 15 | 16 | let check s = 17 | match fst s with 18 | | OP_0 :: OP_DATA (20, wpkh) :: [] -> true 19 | | _ -> false 20 | ;; 21 | 22 | let encode pkh = Script.of_opcodes [ OP_0; OP_DATA (20, pkh) ];; 23 | 24 | let decode s = 25 | match fst s with 26 | | OP_0 :: OP_DATA (20, wpkh) :: [] -> wpkh 27 | | _ -> "" 28 | ;; 29 | 30 | let spendable_by s prefix = decode s |> Address.of_witness prefix.hrp 0x00;; 31 | end 32 | 33 | module Script_witnesspubkeyhash = Script_template.Make_template (Input) (Output);; -------------------------------------------------------------------------------- /src/script_scripthash.ml: -------------------------------------------------------------------------------- 1 | open Script;; 2 | open Address;; 3 | 4 | module Input = struct 5 | type t = int;; 6 | let check v = true;; 7 | let encode v = Script.empty;; 8 | let decode v = 0;; 9 | end 10 | 11 | module Output = struct 12 | type t = string;; 13 | let name = "p2sh";; 14 | 15 | let check s = 16 | match fst s with 17 | | OP_HASH160 :: OP_DATA (20, sh) :: OP_EQUAL :: [] -> true 18 | | _ -> false 19 | ;; 20 | 21 | let encode sh = Script.of_opcodes [ OP_HASH160; OP_DATA (20, sh); OP_EQUAL ];; 22 | 23 | let decode s = 24 | match fst s with 25 | | OP_HASH160 :: OP_DATA (20, sh) :: OP_EQUAL :: [] -> sh 26 | | _ -> "" 27 | ;; 28 | 29 | let spendable_by s prefix = decode s |> Address.of_pubhash prefix.scripthash;; 30 | end 31 | 32 | 33 | module Script_scripthash = Script_template.Make_template (Input) (Output);; -------------------------------------------------------------------------------- /src/params.mli: -------------------------------------------------------------------------------- 1 | (** Params handles different protocol parameters for different Bitcoin based blockchains *) 2 | 3 | open Stdint 4 | type e = BTC | XTN | BCH | LTC | LTN | SIDECHAIN | NOTFOUND 5 | 6 | type t = { 7 | hash_function : string -> string; 8 | block_size : int; 9 | block_time : int; 10 | genesis : Block.Header.t; 11 | magic : int; 12 | port : int; 13 | seeds : string list; 14 | network : e; 15 | checkpoints : (int * Hash.t) list; 16 | prefixes : Address.prefix; 17 | max_money : Int64.t; 18 | } 19 | 20 | val of_network : e -> t 21 | (** [of_network e] returns the params of the [e] network *) 22 | 23 | val name_of_network : e -> string 24 | (** [name_of_network e] returns the readable name of the [e] network *) 25 | 26 | val abbr_to_network : string -> e 27 | (** [abbr_to_network ab] returns the network of a readable abbreviation *) 28 | -------------------------------------------------------------------------------- /src/base58.ml: -------------------------------------------------------------------------------- 1 | open Big_int;; 2 | 3 | let bitcoin_alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";; 4 | let big_base = big_int_of_int 58;; 5 | 6 | let encode_check data = 7 | let string_to_bigint data = 8 | let rec btb data acc = match String.length data with 9 | | 0 -> acc 10 | | n -> 11 | let c' = String.get data 0 |> Char.code in 12 | let rest = String.sub data 1 (n - 1) in 13 | btb rest @@ add_int_big_int c' (mult_int_big_int 0x100 acc) 14 | in btb data zero_big_int 15 | in 16 | let rec encode x = 17 | match eq_big_int x zero_big_int with 18 | | true -> if (String.get data 0 |> Char.code) = 0x00 then "1" else "" 19 | | false -> 20 | let (q, index) = quomod_big_int x big_base in 21 | let ch = String.make 1 bitcoin_alphabet.[int_of_big_int index] in 22 | (encode q) ^ ch 23 | in encode @@ string_to_bigint data 24 | ;; 25 | 26 | -------------------------------------------------------------------------------- /docs/bitcoinml/index.html: -------------------------------------------------------------------------------- 1 | 2 | index (bitcoinml.index)

bitcoinml index

Library bitcoinml

The entry point of this library is the module: Bitcoinml.

-------------------------------------------------------------------------------- /src/block_lazy.mli: -------------------------------------------------------------------------------- 1 | (** Block_lazy is a special representation of Block, where only the header is parsed leaving the 2 | transactions data unparsed until the Lazy is forced *) 3 | 4 | type t = { 5 | header : Block.Header.t; 6 | ltxs : Tx.t list option Lazy.t; 7 | size : int 8 | } 9 | 10 | val parse : ?hex:bool -> string -> t option 11 | (** [parse ~hex:bool data] parses the [data] and returns a [t] option; [data] could be 12 | a binary data, or an hex human readable string if [~hex] is true *) 13 | 14 | val parse_legacy : ?hex:bool -> string -> t option 15 | (** [parse_legacy ~hex:bool data]; same as [parse] but disable segwit *) 16 | 17 | val force : t -> Block.t option 18 | (** [force lblock] forces the parsing of a lazy block [lblock], returning a [Block.t] option *) 19 | 20 | val force_option : t option -> Block.t option 21 | (** [force_option lblock]; same as [force] but receive an option [t] as parameter *) 22 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Base58/index.html: -------------------------------------------------------------------------------- 1 | 2 | Bitcoinml__Base58 (bitcoinml.Bitcoinml__Base58)

Module Bitcoinml__Base58

Base58 encoding utilities

val encode_check : string -> string

encode_check data encoded data in base58_check

-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_verify/Sigver/index.html: -------------------------------------------------------------------------------- 1 | 2 | Sigver (bitcoinml.Bitcoinml__Script_verify.Sigver)

Module Bitcoinml__Script_verify.Sigver

Abstract module for verify functions

type t = string -> string -> bool
-------------------------------------------------------------------------------- /bitcoinml.opam: -------------------------------------------------------------------------------- 1 | opam-version: "2.0" 2 | name: "bitcoinml" 3 | version: "0.4.2" 4 | synopsis: "Bitcoin data-structures library for OCaml" 5 | description: """ 6 | Bitcoin data-structures library for OCaml. Modules documentation is 7 | available at https://dakk.github.io/bitcoinml/ 8 | """ 9 | maintainer: "Davide Gessa " 10 | authors: [ 11 | "Davide Gessa " 12 | ] 13 | 14 | homepage: "https://github.com/dakk/bitcoinml" 15 | bug-reports: "https://github.com/dakk/bitcoinml/issues" 16 | license: "MIT" 17 | dev-repo: "git+https://github.com/dakk/bitcoinml.git" 18 | build: ["dune" "build" "-p" name "-j" jobs] 19 | 20 | depends: [ 21 | "ocaml" {>= "4.05.0"} 22 | 23 | "dune" {>= "1.6.0"} 24 | "bitstring" {>= "3.1.0"} 25 | "stdint" {>= "0.5.1"} 26 | "cryptokit" {>= "1.13"} 27 | "bignum" {>= "v0.10.0"} 28 | "hex" {>= "1.3.0"} 29 | 30 | "ounit" {with-test & >= "2.0.8"} 31 | "odoc" {with-test & >= "1.3.0"} 32 | ] 33 | 34 | depexts: [ 35 | ] 36 | 37 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Base58/index.html: -------------------------------------------------------------------------------- 1 | 2 | Base58 (bitcoinml.Bitcoinml.Base58)

Module Bitcoinml.Base58

val encode_check : string -> string

encode_check data encoded data in base58_check

-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_verify/Sigver/index.html: -------------------------------------------------------------------------------- 1 | 2 | Sigver (bitcoinml.Bitcoinml.Script_verify.Sigver)

Module Script_verify.Sigver

Abstract module for verify functions

type t = string -> string -> bool
-------------------------------------------------------------------------------- /src/script_pubkey.ml: -------------------------------------------------------------------------------- 1 | open Script;; 2 | open Address;; 3 | 4 | module Input = struct 5 | type t = Script.data;; 6 | let check v = (* TODO check if it's a canonical DER signaure (bip66) *) true;; 7 | let encode v = Script.of_opcodes [OP_DATA (String.length v, v)];; 8 | 9 | let decode v = 10 | match fst v with 11 | | [OP_DATA (n, v)] -> v 12 | | _ -> "" 13 | end 14 | 15 | 16 | module Output = struct 17 | type t = string;; 18 | let name = "p2pk";; 19 | 20 | let check s = 21 | match fst s with 22 | | OP_DATA (n, pk) :: OP_CHECKSIG :: [] when n = 33 || n = 65 -> true 23 | | _ -> false 24 | ;; 25 | 26 | let encode pk = Script.of_opcodes [OP_DATA (String.length pk, pk); OP_CHECKSIG];; 27 | 28 | let decode s = 29 | match fst s with 30 | | OP_DATA (n, pk) :: OP_CHECKSIG :: [] when n = 33 || n = 65 -> pk 31 | | _ -> "" 32 | ;; 33 | 34 | let spendable_by s prefix = decode s |> Address.of_pub prefix.pubkeyhash;; 35 | end 36 | 37 | 38 | module Script_pubkey = Script_template.Make_template (Input) (Output);; -------------------------------------------------------------------------------- /src/address.mli: -------------------------------------------------------------------------------- 1 | (** An address is an almost readable representation of Bitcoin wallet identifier *) 2 | 3 | type prefix = { 4 | pubkeyhash: int; 5 | scripthash: int; 6 | hrp: string; 7 | } 8 | (** Address prefixes structure *) 9 | 10 | type t = string 11 | (** Address abstract type *) 12 | 13 | (** Handle segwit native bech32 addresses *) 14 | module Bech32 : sig 15 | val encode : string -> int -> string -> t 16 | (** [encode hrp witver witprog] encodes [witprog] in bech32 address given the [hrp] and the 17 | [witver] witness version *) 18 | end 19 | 20 | 21 | val of_pub : int -> string -> t 22 | (** [of_pub prefix pk] encodes the address from a public key [pk] given the network prefix [prefix] *) 23 | 24 | val of_pubhash : int -> string -> t 25 | (** [of_pubhash prefix pkh] encodes the address from a public key hash [pkh] given the network 26 | prefix [prefix] *) 27 | 28 | val of_witness : string -> int -> string -> t 29 | (** [of_witness hrp witver witprog] encodes [witprog] in bech32 address given the [hrp] and the 30 | [witver] witness version *) -------------------------------------------------------------------------------- /src/script_verify.mli: -------------------------------------------------------------------------------- 1 | (** Verification of Bitcoin scripts *) 2 | 3 | (** Abstract module for verify functions *) 4 | module Sigver : sig 5 | type t = string -> string -> bool 6 | end 7 | 8 | (** Script execution stack *) 9 | module SStack : sig 10 | type t = int Stack.t 11 | 12 | val create : unit -> t 13 | val pop : t -> int 14 | val top : t -> int 15 | val push : int -> t -> unit 16 | val push_data : string -> t -> unit 17 | end 18 | 19 | val verify : Sigver.t -> Script.t -> Script.t -> bool 20 | (** [verify sigver s1 s2] execute a script [s1] and [s2] using [sigver] *) 21 | 22 | val is_spendable : Script.t -> bool 23 | (** [is_spendable s] returns true if [s] is spendable by a recognized address *) 24 | 25 | val spendable_by : Script.t -> Address.prefix -> string option 26 | (** [spendable_by s prefix] returns the address (using [prefix]) which is able to 27 | spend the given script *) 28 | 29 | 30 | val classify_output : Script.t -> string 31 | (** [classify_output s] returns the template name of the output script [s] if any *) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016-2020 Davide Gessa 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /src/script_witnesscommitment.ml: -------------------------------------------------------------------------------- 1 | open Script;; 2 | open Address;; 3 | open Bitstring;; 4 | 5 | 6 | module Output = struct 7 | type t = Hash.t;; 8 | let name = "wcommitment";; 9 | 10 | let encode wc = 11 | Script.of_opcodes [ OP_RETURN (string_of_bitstring [%bitstring {| 12 | 0x24 : 1*8 : littleendian; 13 | (Hex.to_string (`Hex "aa21a9ed")) : 4*8 : string; 14 | wc : 32 * 8 : string 15 | |}] ) ] 16 | ;; 17 | 18 | let decode s = 19 | match fst s with 20 | | OP_RETURN (data) :: [] when String.length data >= 38 -> ( 21 | match%bitstring (bitstring_of_string data) with 22 | | {| 23 | p24 : 1*8 : littleendian; 24 | header : 4*8 : string; 25 | hash : 4*8 : string; 26 | rest : -1 : bitstring 27 | |} -> 28 | if p24 <> 0x24 then "" 29 | else if "aa21a9ed" <> (Hex.of_string header |> Hex.show) then "" 30 | else Hash.of_bin hash) 31 | | _ -> "" 32 | ;; 33 | 34 | let check s = decode s <> "";; 35 | 36 | let spendable_by s prefix = "";; 37 | end 38 | 39 | module Script_witnesspubkeyhash = Script_template.Make_template (Script_template.EmptyInput) (Output);; -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Address/Bech32/index.html: -------------------------------------------------------------------------------- 1 | 2 | Bech32 (bitcoinml.Bitcoinml__Address.Bech32)

Module Bitcoinml__Address.Bech32

Handle segwit native bech32 addresses

val encode : string -> int -> string -> t

encode hrp witver witprog encodes witprog in bech32 address given the hrp and the witver witness version

-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Address/Bech32/index.html: -------------------------------------------------------------------------------- 1 | 2 | Bech32 (bitcoinml.Bitcoinml.Address.Bech32)

Module Address.Bech32

Handle segwit native bech32 addresses

val encode : string -> int -> string -> t

encode hrp witver witprog encodes witprog in bech32 address given the hrp and the witver witness version

-------------------------------------------------------------------------------- /src/experimental/simplicity.ml: -------------------------------------------------------------------------------- 1 | type t = 2 | | Unit 3 | | Iden 4 | | Take of t 5 | | Drop of t 6 | | Injl of t 7 | | Injr of t 8 | | Comp of t * t 9 | | Case of t * t 10 | | Pair of t * t 11 | | Sum of t * t 12 | ;; 13 | 14 | 15 | module Examples = struct 16 | let two = Sum (Unit, Unit);; 17 | let not = Comp ( 18 | (Pair (Iden, Unit)), 19 | (Case (Injr Unit, Injl Unit)) 20 | );; 21 | 22 | let halfadder = Case ( 23 | Drop (Pair (Injl (Unit), Iden)), 24 | Drop (Pair (Iden, not)) 25 | );; 26 | 27 | let fulladder1 = 28 | Comp ( 29 | Pair (Take halfadder, Drop Iden), 30 | Comp ( 31 | Pair ( 32 | Take (Take Iden), 33 | Comp ( 34 | Pair (Take (Drop Iden), Drop Iden), 35 | halfadder 36 | ) 37 | ), 38 | Pair ( 39 | Case (Injr Unit, Drop (Take Iden)), 40 | Drop (Drop Iden) 41 | ) 42 | ) 43 | ) 44 | ;; 45 | end 46 | 47 | (*let rec eval e = match e with 48 | | Iden -> fun x -> x 49 | | Comp (s, t) -> fun x -> eval t (eval s x) 50 | | Injl (t) -> fun x -> fst (eval t x) 51 | | Injr (t) -> fun x -> snd (eval t x) 52 | | Pair (s, t) -> fun x -> (eval s x, eval t x) 53 | ;;*) 54 | 55 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_witnesscommitment/index.html: -------------------------------------------------------------------------------- 1 | 2 | Bitcoinml__Script_witnesscommitment (bitcoinml.Bitcoinml__Script_witnesscommitment)

Module Bitcoinml__Script_witnesscommitment

module Output : sig ... end
module Script_witnesspubkeyhash : sig ... end
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_witnesscommitment/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_witnesscommitment (bitcoinml.Bitcoinml.Script_witnesscommitment)

Module Bitcoinml.Script_witnesscommitment

module Output : sig ... end
module Script_witnesspubkeyhash : sig ... end
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_pubkey/index.html: -------------------------------------------------------------------------------- 1 | 2 | Bitcoinml__Script_pubkey (bitcoinml.Bitcoinml__Script_pubkey)

Module Bitcoinml__Script_pubkey

module Input : sig ... end
module Output : sig ... end
module Script_pubkey : sig ... end
-------------------------------------------------------------------------------- /src/hash.mli: -------------------------------------------------------------------------------- 1 | (** An hash is a 32B binary string, represented in human readable hex encoded 64B string *) 2 | 3 | type t = string 4 | type hash = t 5 | type b = string 6 | 7 | val reverse : string -> string 8 | (** [reverse s] reverses the string [s] *) 9 | 10 | val to_bin : t -> b 11 | (** [to_bin h] transforms [h] in binary format *) 12 | 13 | val to_bin_norev : t -> b 14 | (** [to_bin_norev h] transforms [h] in binary format without reversing *) 15 | 16 | val to_bigint : t -> Big_int.big_int 17 | (** [to_bigint h] transforms [h] in a [Big_int] *) 18 | 19 | val of_bin : b -> t 20 | (** [of_bin data] transforms [data] in an hash *) 21 | 22 | val of_bin_norev : b -> t 23 | (** [of_bin_norev data] transforms [data] in an hash without reversing *) 24 | 25 | val zero : t 26 | (** [zero] hash with only zeros *) 27 | 28 | 29 | 30 | val sha1 : t -> t 31 | (** [sha1 data] returns the sha1 of [data] *) 32 | 33 | val sha256 : t -> t 34 | (** [sha256 data] returns the sha2 of [data] *) 35 | 36 | val ripemd160 : t -> t 37 | (** [ripemd160 data] returns the ripedm160 of [data] *) 38 | 39 | val hash160 : t -> t 40 | (** [hash160 data] returns the hash160 of [data] *) 41 | 42 | val hash256 : t -> t 43 | (** [hash256 data] returns the hash256 of [data] *) 44 | 45 | val dsha256 : t -> t 46 | (** [dsha256 data] returns the double sha2 of [data] *) 47 | 48 | val checksum4 : t -> t 49 | (** [checksum4 data] returns the checksum4 of [data] *) 50 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_nulldata/index.html: -------------------------------------------------------------------------------- 1 | 2 | Bitcoinml__Script_nulldata (bitcoinml.Bitcoinml__Script_nulldata)

Module Bitcoinml__Script_nulldata

module Output : sig ... end
module Script_nulldata : sig ... end
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_pubkey/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_pubkey (bitcoinml.Bitcoinml.Script_pubkey)

Module Bitcoinml.Script_pubkey

module Input : sig ... end
module Output : sig ... end
module Script_pubkey : sig ... end
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_nulldata/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_nulldata (bitcoinml.Bitcoinml.Script_nulldata)

Module Bitcoinml.Script_nulldata

module Output : sig ... end
module Script_nulldata : sig ... end
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_pubkeyhash/index.html: -------------------------------------------------------------------------------- 1 | 2 | Bitcoinml__Script_pubkeyhash (bitcoinml.Bitcoinml__Script_pubkeyhash)

Module Bitcoinml__Script_pubkeyhash

module Input : sig ... end
module Output : sig ... end
module Script_pubkeyhash : sig ... end
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_scripthash/index.html: -------------------------------------------------------------------------------- 1 | 2 | Bitcoinml__Script_scripthash (bitcoinml.Bitcoinml__Script_scripthash)

Module Bitcoinml__Script_scripthash

module Input : sig ... end
module Output : sig ... end
module Script_scripthash : sig ... end
-------------------------------------------------------------------------------- /test/test.ml: -------------------------------------------------------------------------------- 1 | open Bitcoinml;; 2 | open Stdint;; 3 | open OUnit2;; 4 | open Hex;; 5 | 6 | let hex_of_file f = try `Hex (input_line @@ open_in f) with | _ -> `Hex ("");; 7 | 8 | let base58_encode_check_test octx = 9 | let adr = Hex.to_string (`Hex "00010966776006953D5567439E5E39F86A0D273BEED61967F6") in 10 | let adrb58 = Base58.encode_check adr in 11 | assert_equal adrb58 "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM" 12 | ;; 13 | 14 | let varint_parse_test bl res octx = 15 | let v1 = Varint.parse_varint (Bitstring.bitstring_of_string (Hex.to_string bl)) in 16 | assert_equal (fst v1) (Uint64.of_int res) 17 | ;; 18 | 19 | let varint_serialize_test bl res octx = 20 | let l = Varint.bitstring_of_varint (Int64.of_int res) in 21 | assert_equal (Bitstring.string_of_bitstring l) (Hex.to_string bl) 22 | ;; 23 | 24 | 25 | let suite = "bitcoinml" >::: Address_test.tlist @ Block_test.tlist @ Tx_test.tlist @ Proto_test.tlist @ [ 26 | "base58.encode_check" >:: base58_encode_check_test; 27 | "varint.parse" >:: varint_parse_test (`Hex "16") 0x16; 28 | "varint.parse2" >:: varint_parse_test (`Hex "FE32323232") 0x32323232; 29 | (*"varint.parse3" >:: varint_parse_test (`Hex "FF3232323232323232") 0x3232323232323232;*) 30 | "varint.serialize" >:: varint_serialize_test (`Hex "16") 0x16; 31 | "varint.serialize2" >:: varint_serialize_test (`Hex "FE32323232") 0x32323232; 32 | (*"varint.serialize3" >:: varint_serialize_test (`Hex "FF3232323232323232") 0x3232323232323232;*) 33 | ];; 34 | 35 | let () = run_test_tt_main suite;; -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_pubkeyhash/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_pubkeyhash (bitcoinml.Bitcoinml.Script_pubkeyhash)

Module Bitcoinml.Script_pubkeyhash

module Input : sig ... end
module Output : sig ... end
module Script_pubkeyhash : sig ... end
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_scripthash/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_scripthash (bitcoinml.Bitcoinml.Script_scripthash)

Module Bitcoinml.Script_scripthash

module Input : sig ... end
module Output : sig ... end
module Script_scripthash : sig ... end
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_multisig/Input/index.html: -------------------------------------------------------------------------------- 1 | 2 | Input (bitcoinml.Bitcoinml__Script_multisig.Input)

Module Bitcoinml__Script_multisig.Input

type t = int
val check : 'a -> bool
val encode : 'a -> Bitcoinml.Script.t
val decode : 'a -> int
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_witnesspubkeyhash/index.html: -------------------------------------------------------------------------------- 1 | 2 | Bitcoinml__Script_witnesspubkeyhash (bitcoinml.Bitcoinml__Script_witnesspubkeyhash)

Module Bitcoinml__Script_witnesspubkeyhash

module Input : sig ... end
module Output : sig ... end
module Script_witnesspubkeyhash : sig ... end
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_witnessscripthash/index.html: -------------------------------------------------------------------------------- 1 | 2 | Bitcoinml__Script_witnessscripthash (bitcoinml.Bitcoinml__Script_witnessscripthash)

Module Bitcoinml__Script_witnessscripthash

module Input : sig ... end
module Output : sig ... end
module Script_witnessscripthash : sig ... end
-------------------------------------------------------------------------------- /src/script_pubkeyhash.ml: -------------------------------------------------------------------------------- 1 | open Script;; 2 | open Address;; 3 | 4 | 5 | module Input = struct 6 | type t = { 7 | signature: string; 8 | pubkey: string 9 | };; 10 | 11 | let check v = 12 | (* TODO check if [0] it's a canonical DER signaure (bip66) and [1] is a canonicalpubkey*) 13 | match fst v with 14 | | OP_DATA (n, sign) :: OP_DATA (n', pk) :: [] -> true 15 | | _ -> false 16 | ;; 17 | 18 | let encode v = Script.of_opcodes [ 19 | OP_DATA (String.length v.signature, v.signature); 20 | OP_DATA (String.length v.pubkey, v.pubkey) 21 | ];; 22 | 23 | let decode v = 24 | match fst v with 25 | | OP_DATA (n, sign) :: OP_DATA (n', pk) :: [] -> { signature= sign; pubkey= pk } 26 | | _ -> { signature= ""; pubkey= "" } 27 | ;; 28 | end 29 | 30 | module Output = struct 31 | type t = string;; 32 | let name = "p2pkh";; 33 | 34 | let check s = 35 | match fst s with 36 | | OP_DUP :: OP_HASH160 :: OP_DATA (20, pkh) :: OP_EQUALVERIFY :: OP_CHECKSIG :: [] -> true 37 | | _ -> false 38 | ;; 39 | 40 | let encode pkh = Script.of_opcodes [ 41 | OP_DUP; OP_HASH160; OP_DATA (20, pkh); OP_EQUALVERIFY; OP_CHECKSIG 42 | ];; 43 | 44 | let decode s = 45 | match fst s with 46 | | OP_DUP :: OP_HASH160 :: OP_DATA (20, pkh) :: OP_EQUALVERIFY :: OP_CHECKSIG :: [] -> pkh 47 | | _ -> "" 48 | ;; 49 | 50 | let spendable_by s prefix = decode s |> Address.of_pubhash prefix.pubkeyhash;; 51 | end 52 | 53 | 54 | module Script_pubkeyhash = Script_template.Make_template (Input) (Output);; -------------------------------------------------------------------------------- /src/varint.ml: -------------------------------------------------------------------------------- 1 | open Stdint;; 2 | open Bytes;; 3 | 4 | let parse_varint bits = 5 | let parse_tag_byte bits = 6 | match%bitstring bits with 7 | | {| tag : 1*8 : string; rest : -1 : bitstring |} -> (Uint8.of_bytes_little_endian (of_string tag) 0, rest) 8 | in 9 | let parse_value bits stringize = 10 | match%bitstring bits with 11 | | {| value : stringize * 8 : string; rest : -1 : bitstring |} -> 12 | match stringize with 13 | | 8 -> (Uint64.of_bytes_little_endian (of_string value) 0, rest) 14 | | 4 -> (Uint32.to_uint64 (Uint32.of_bytes_little_endian (of_string value) 0), rest) 15 | | 2 -> (Uint16.to_uint64 (Uint16.of_bytes_little_endian (of_string value) 0), rest) 16 | | _ -> failwith "Varint parse error" 17 | in 18 | let tag, rest = parse_tag_byte bits in 19 | match Uint8.to_int tag with 20 | | 0xFF -> parse_value rest 8 21 | | 0xFE -> parse_value rest 4 22 | | 0xFD -> parse_value rest 2 23 | | x -> (Uint64.of_uint8 tag, rest) 24 | ;; 25 | 26 | 27 | let bitstring_of_varint i = 28 | match i with 29 | | i when i < 0xFDL -> [%bitstring {| Int64.to_int i : 1*8 : littleendian |}] 30 | | i when i < 0xFFFFL -> [%bitstring {| 0xFD : 1*8; Int64.to_int i : 2*8 : littleendian |}] 31 | | i when i < 0xFFFFFFFFL -> [%bitstring {| 0xFE : 1*8; Int64.to_int32 i : 4*8 : littleendian |}] 32 | | i -> [%bitstring {| 0xFF : 1*8; i : 8*8 : littleendian |}] 33 | ;; 34 | 35 | 36 | let encoding_length i = match Uint64.to_int64 i with 37 | | i when i < 0xFDL -> 1 38 | | i when i < 0xFFFFL -> 3 39 | | i when i < 0xFFFFFFFFL -> 5 40 | | i -> 9 41 | ;; -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_scripthash/Input/index.html: -------------------------------------------------------------------------------- 1 | 2 | Input (bitcoinml.Bitcoinml__Script_scripthash.Input)

Module Bitcoinml__Script_scripthash.Input

type t = int
val check : 'a -> bool
val encode : 'a -> Bitcoinml.Script.t
val decode : 'a -> int
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_multisig/Input/index.html: -------------------------------------------------------------------------------- 1 | 2 | Input (bitcoinml.Bitcoinml.Script_multisig.Input)

Module Script_multisig.Input

type t = int
val check : 'a -> bool
val encode : 'a -> Script.t
val decode : 'a -> int
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_scripthash/Input/index.html: -------------------------------------------------------------------------------- 1 | 2 | Input (bitcoinml.Bitcoinml.Script_scripthash.Input)

Module Script_scripthash.Input

type t = int
val check : 'a -> bool
val encode : 'a -> Script.t
val decode : 'a -> int
-------------------------------------------------------------------------------- /src/block.mli: -------------------------------------------------------------------------------- 1 | (** Block serialization and parsing module *) 2 | 3 | open Stdint 4 | 5 | (** Block header parsing / serialization module *) 6 | module Header : sig 7 | type t = { 8 | hash : Hash.t; 9 | version : int32; 10 | prev_block : Hash.t; 11 | merkle_root : Hash.t; 12 | time : float; 13 | bits : string; 14 | nonce : uint32; 15 | } 16 | 17 | val parse : ?hex:bool -> string -> t option 18 | (** [parse ~hex:bool data] parses the [data] and returns a [t] option; [data] could be 19 | a binary data, or an hex human readable string if [~hex] is true *) 20 | 21 | val serialize : ?hex:bool -> t -> string 22 | (** [serialize ~hex:bool header] serializes a block [header] and returns a binary string 23 | or an hex human readable string if [~hex] is true *) 24 | 25 | val check_target : t -> bool 26 | (** [check_target header] checks the nbits / hash target *) 27 | end 28 | 29 | type t = { 30 | header : Header.t; 31 | txs : Tx.t list; 32 | size : int; 33 | } 34 | 35 | 36 | val parse : ?hex:bool -> string -> t option 37 | (** [parse ~hex:bool data] parses the [data] and returns a [t] option; [data] could be 38 | a binary data, or an hex human readable string if [~hex] is true *) 39 | 40 | val parse_legacy : ?hex:bool -> string -> t option 41 | (** [parse_legacy ~hex:bool data]; same as [parse] but disable segwit *) 42 | 43 | val serialize : ?hex:bool -> t -> string 44 | (** [serialize ~hex:bool block] serializes a block [block] and returns a binary string 45 | or an hex human readable string if [~hex] is true *) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_witnesspubkeyhash/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_witnesspubkeyhash (bitcoinml.Bitcoinml.Script_witnesspubkeyhash)

Module Bitcoinml.Script_witnesspubkeyhash

module Input : sig ... end
module Output : sig ... end
module Script_witnesspubkeyhash : sig ... end
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_witnessscripthash/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_witnessscripthash (bitcoinml.Bitcoinml.Script_witnessscripthash)

Module Bitcoinml.Script_witnessscripthash

module Input : sig ... end
module Output : sig ... end
module Script_witnessscripthash : sig ... end
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_template/EmptyInput/index.html: -------------------------------------------------------------------------------- 1 | 2 | EmptyInput (bitcoinml.Bitcoinml__Script_template.EmptyInput)

Module Bitcoinml__Script_template.EmptyInput

type t = string
val check : 'a -> bool
val encode : 'a -> Bitcoinml.Script.t
val decode : 'a -> string
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_witnesspubkeyhash/Input/index.html: -------------------------------------------------------------------------------- 1 | 2 | Input (bitcoinml.Bitcoinml__Script_witnesspubkeyhash.Input)

Module Bitcoinml__Script_witnesspubkeyhash.Input

type t = int
val check : 'a -> bool
val encode : 'a -> Bitcoinml.Script.t
val decode : 'a -> int
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_witnessscripthash/Input/index.html: -------------------------------------------------------------------------------- 1 | 2 | Input (bitcoinml.Bitcoinml__Script_witnessscripthash.Input)

Module Bitcoinml__Script_witnessscripthash.Input

type t = int
val check : 'a -> bool
val encode : 'a -> Bitcoinml.Script.t
val decode : 'a -> int
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_template/EmptyInput/index.html: -------------------------------------------------------------------------------- 1 | 2 | EmptyInput (bitcoinml.Bitcoinml.Script_template.EmptyInput)

Module Script_template.EmptyInput

type t = string
val check : 'a -> bool
val encode : 'a -> Script.t
val decode : 'a -> string
-------------------------------------------------------------------------------- /src/block_lazy.ml: -------------------------------------------------------------------------------- 1 | open Bitstring;; 2 | open Block;; 3 | open Varint;; 4 | open Stdint;; 5 | 6 | type t = { 7 | header : Block.Header.t; 8 | ltxs : Tx.t list option Lazy.t; 9 | size : int 10 | };; 11 | 12 | let parse ?(hex=false) data' = 13 | let data = (if hex then Hex.to_string @@ `Hex data' else data') in 14 | let header = Block.Header.parse (String.sub data 0 80) in 15 | match header with 16 | | None -> None 17 | | Some (header) -> 18 | let bdata = bitstring_of_string (String.sub data 80 ((String.length data) - 80)) in 19 | let txn, rest' = parse_varint bdata in 20 | let txs = lazy (Tx.parse_all (string_of_bitstring rest') (Uint64.to_int txn)) in 21 | Some ({ header= header; ltxs= txs; size= String.length data }) 22 | ;; 23 | 24 | 25 | let parse_legacy ?(hex=false) data' = 26 | let data = (if hex then Hex.to_string @@ `Hex data' else data') in 27 | let header = Block.Header.parse (String.sub data 0 80) in 28 | match header with 29 | | None -> None 30 | | Some (header) -> 31 | let bdata = bitstring_of_string (String.sub data 80 ((String.length data) - 80)) in 32 | let txn, rest' = parse_varint bdata in 33 | let txs = lazy (Tx.parse_all_legacy (string_of_bitstring rest') (Uint64.to_int txn)) in 34 | Some ({ header= header; ltxs= txs; size= String.length data }) 35 | ;; 36 | 37 | let force lb = match Lazy.force lb.ltxs with 38 | | None -> None 39 | | Some (txs) -> Some ({ header= lb.header; txs= List.rev txs; size= lb.size }) 40 | ;; 41 | 42 | let force_option lb = match lb with 43 | | Some (lb') -> force lb' 44 | | None -> None 45 | ;; -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_witnesspubkeyhash/Input/index.html: -------------------------------------------------------------------------------- 1 | 2 | Input (bitcoinml.Bitcoinml.Script_witnesspubkeyhash.Input)

Module Script_witnesspubkeyhash.Input

type t = int
val check : 'a -> bool
val encode : 'a -> Script.t
val decode : 'a -> int
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_witnessscripthash/Input/index.html: -------------------------------------------------------------------------------- 1 | 2 | Input (bitcoinml.Bitcoinml.Script_witnessscripthash.Input)

Module Script_witnessscripthash.Input

type t = int
val check : 'a -> bool
val encode : 'a -> Script.t
val decode : 'a -> int
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Merkle/index.html: -------------------------------------------------------------------------------- 1 | 2 | Merkle (bitcoinml.Bitcoinml.Merkle)

Module Bitcoinml.Merkle

type t = Hash.t
val of_txs : Tx.t list -> t

of_txs txl returns the merkle tree hash of the txl transaction set

val of_hashes : Hash.t list -> t

of_hashes hl returns the merkle tree hash of the hl hash list

-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_template/module-type-Input/index.html: -------------------------------------------------------------------------------- 1 | 2 | Input (bitcoinml.Bitcoinml.Script_template.Input)

Module type Script_template.Input

type t
val encode : t -> Script.t
val decode : Script.t -> t
val check : Script.t -> bool
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Merkle/index.html: -------------------------------------------------------------------------------- 1 | 2 | Bitcoinml__Merkle (bitcoinml.Bitcoinml__Merkle)

Module Bitcoinml__Merkle

type t = Bitcoinml.Hash.t
val of_txs : Bitcoinml.Tx.t list -> t

of_txs txl returns the merkle tree hash of the txl transaction set

val of_hashes : Bitcoinml.Hash.t list -> t

of_hashes hl returns the merkle tree hash of the hl hash list

-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_multisig/index.html: -------------------------------------------------------------------------------- 1 | 2 | Bitcoinml__Script_multisig (bitcoinml.Bitcoinml__Script_multisig)

Module Bitcoinml__Script_multisig

val op_int_base : int list
module Input : sig ... end
module Output : sig ... end
module Script_multisig : sig ... end
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_template/module-type-Input/index.html: -------------------------------------------------------------------------------- 1 | 2 | Input (bitcoinml.Bitcoinml__Script_template.Input)

Module type Bitcoinml__Script_template.Input

type t
val encode : t -> Bitcoinml.Script.t
val decode : Bitcoinml.Script.t -> t
val check : Bitcoinml.Script.t -> bool
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Varint/index.html: -------------------------------------------------------------------------------- 1 | 2 | Bitcoinml__Varint (bitcoinml.Bitcoinml__Varint)

Module Bitcoinml__Varint

val parse_varint : Bitstring.t -> Stdint.Uint64.t * Bitstring.t

parse_varint bits parse Bitstring bits returing the Uint64.t value and the rest of the bitstring

val bitstring_of_varint : Stdint.Int64.t -> Bitstring.t

bitstring_of_varint num encodes num to its bitstring varint representation

val encoding_length : Stdint.Uint64.t -> int

encoding_length num returns the length of num if encoded as varint

-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_multisig/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_multisig (bitcoinml.Bitcoinml.Script_multisig)

Module Bitcoinml.Script_multisig

val op_int_base : int list
module Input : sig ... end
module Output : sig ... end
module Script_multisig : sig ... end
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Varint/index.html: -------------------------------------------------------------------------------- 1 | 2 | Varint (bitcoinml.Bitcoinml.Varint)

Module Bitcoinml.Varint

val parse_varint : Bitstring.t -> Stdint.Uint64.t * Bitstring.t

parse_varint bits parse Bitstring bits returing the Uint64.t value and the rest of the bitstring

val bitstring_of_varint : Stdint.Int64.t -> Bitstring.t

bitstring_of_varint num encodes num to its bitstring varint representation

val encoding_length : Stdint.Uint64.t -> int

encoding_length num returns the length of num if encoded as varint

-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_template/Make_template/argument-1-ITemplate/index.html: -------------------------------------------------------------------------------- 1 | 2 | 1-ITemplate (bitcoinml.Bitcoinml.Script_template.Make_template.1-ITemplate)

Parameter Make_template.1-ITemplate

type t
val encode : t -> Script.t
val decode : Script.t -> t
val check : Script.t -> bool
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_template/Make_template/argument-1-ITemplate/index.html: -------------------------------------------------------------------------------- 1 | 2 | 1-ITemplate (bitcoinml.Bitcoinml__Script_template.Make_template.1-ITemplate)

Parameter Make_template.1-ITemplate

type t
val encode : t -> Bitcoinml.Script.t
val decode : Bitcoinml.Script.t -> t
val check : Bitcoinml.Script.t -> bool
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_pubkey/Input/index.html: -------------------------------------------------------------------------------- 1 | 2 | Input (bitcoinml.Bitcoinml.Script_pubkey.Input)

Module Script_pubkey.Input

type t = Script.data
val check : 'a -> bool
val encode : Script.data -> Script.t
val decode : (Script.opcode list * 'a) -> Script.data
-------------------------------------------------------------------------------- /test/address_test.ml: -------------------------------------------------------------------------------- 1 | open Bitcoinml;; 2 | open Stdint;; 3 | open OUnit2;; 4 | open Hex;; 5 | 6 | let address_of_pub_test prefix pub addr octx = 7 | assert_equal addr @@ Address.of_pub prefix (Hex.to_string pub) 8 | ;; 9 | 10 | let address_of_pubhash_test prefix pub addr octx = 11 | assert_equal addr @@ Address.of_pubhash prefix (Hex.to_string pub) 12 | ;; 13 | 14 | let address_bech32_test address hrp ver script octx = 15 | let ad = Address.of_witness hrp ver (Hex.to_string script) in 16 | assert_equal address @@ ad 17 | ;; 18 | 19 | let tlist = [ 20 | "address.of_pub" >:: address_of_pub_test 0x0 (`Hex "0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6") "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"; 21 | "address.of_pubhash" >:: address_of_pubhash_test 0x05 (`Hex "010966776006953D5567439E5E39F86A0D273BEE") "31nVrspaydBz8aMpxH9WkS2DuhgqS1fCuG"; 22 | "address.of_pubhash2" >:: address_of_pubhash_test 0x00 (`Hex "010966776006953D5567439E5E39F86A0D273BEE") "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"; 23 | 24 | "address.bech32" >:: address_bech32_test "bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx" "bc" 1 (`Hex "751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd6"); 25 | "address.bech322" >:: address_bech32_test "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4" "bc" 0 (`Hex "751e76e8199196d454941c45d1b3a323f1433bd6"); 26 | "address.bech323" >:: address_bech32_test "bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3" "bc" 0 (`Hex "1863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262"); 27 | (*"address.bech322" >:: address_bech32_test "tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7" "tb" 34 (`Hex "00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262"); *) 28 | ];; -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_pubkey/Input/index.html: -------------------------------------------------------------------------------- 1 | 2 | Input (bitcoinml.Bitcoinml__Script_pubkey.Input)

Module Bitcoinml__Script_pubkey.Input

type t = Bitcoinml.Script.data
val check : 'a -> bool
val encode : Bitcoinml.Script.data -> Bitcoinml.Script.t
val decode : (Bitcoinml.Script.opcode list * 'a) -> Bitcoinml.Script.data
-------------------------------------------------------------------------------- /src/experimental/simplicity_test.ml: -------------------------------------------------------------------------------- 1 | type unit0 = | Tt;; 2 | 3 | type ('a, 'b) sum = 4 | | Inl of 'a 5 | | Inr of 'b 6 | ;; 7 | 8 | type ('a, 'b) prod = | Pair of 'a * 'b;; 9 | 10 | let fst = function | Pair (x, _) -> x;; 11 | 12 | let snd = function | Pair (_, y) -> y;; 13 | 14 | type ty = 15 | | Unit 16 | | Sum of ty * ty 17 | | Prod of ty * ty 18 | ;; 19 | 20 | type term = 21 | | Iden 22 | | Comp of term * term 23 | | Unit 24 | | Injl of term 25 | | Injr of term 26 | | Case of term * term 27 | | Pair0 of term * term 28 | | Take of term 29 | | Drop of term 30 | ;; 31 | 32 | let rec eval x a = 33 | match x with 34 | | Iden -> a 35 | | Comp (s, t) -> eval t (eval s a) 36 | | Unit -> Obj.magic Tt 37 | | Injl (t) -> Obj.magic (Inl (eval t a)) 38 | | Injr (t) -> Obj.magic (Inr (eval t a)) 39 | | Case (s, t) -> 40 | let Pair (ab, c0) = Obj.magic a in 41 | (match ab with 42 | | Inl a1 -> eval s (Obj.magic (Pair (a1, c0))) 43 | | Inr b0 -> eval t (Obj.magic (Pair (b0, c0)))) 44 | | Pair0 (s, t) -> 45 | Obj.magic (Pair ((eval s a), (eval t a))) 46 | | Take (t) -> eval t (fst (Obj.magic a)) 47 | | Drop (t) -> eval t (snd (Obj.magic a)) 48 | 49 | 50 | 51 | module Examples = struct 52 | let two = Sum (Unit, Unit);; 53 | let not = Comp ( 54 | (Pair0 (Iden, Unit)), 55 | (Case (Injr Unit, Injl Unit)) 56 | );; 57 | 58 | let halfadder = Case ( 59 | Drop (Pair0 (Injl (Unit), Iden)), 60 | Drop (Pair0 (Iden, not)) 61 | );; 62 | 63 | let fulladder1 = 64 | Comp ( 65 | Pair0 (Take halfadder, Drop Iden), 66 | Comp ( 67 | Pair0 ( 68 | Take (Take Iden), 69 | Comp ( 70 | Pair0 (Take (Drop Iden), Drop Iden), 71 | halfadder 72 | ) 73 | ), 74 | Pair0 ( 75 | Case (Injr Unit, Drop (Take Iden)), 76 | Drop (Drop Iden) 77 | ) 78 | ) 79 | ) 80 | ;; 81 | end -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_verify/SStack/index.html: -------------------------------------------------------------------------------- 1 | 2 | SStack (bitcoinml.Bitcoinml__Script_verify.SStack)

Module Bitcoinml__Script_verify.SStack

Script execution stack

type t = int Stdlib.Stack.t
val create : unit -> t
val pop : t -> int
val top : t -> int
val push : int -> t -> unit
val push_data : string -> t -> unit
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_verify/SStack/index.html: -------------------------------------------------------------------------------- 1 | 2 | SStack (bitcoinml.Bitcoinml.Script_verify.SStack)

Module Script_verify.SStack

Script execution stack

type t = int Stdlib.Stack.t
val create : unit -> t
val pop : t -> int
val top : t -> int
val push : int -> t -> unit
val push_data : string -> t -> unit
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_pubkeyhash/Input/index.html: -------------------------------------------------------------------------------- 1 | 2 | Input (bitcoinml.Bitcoinml.Script_pubkeyhash.Input)

Module Script_pubkeyhash.Input

type t = {
signature : string;
pubkey : string;
}
val check : (Script.opcode list * 'a) -> bool
val encode : t -> Script.t
val decode : (Script.opcode list * 'a) -> t
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_template/module-type-Output/index.html: -------------------------------------------------------------------------------- 1 | 2 | Output (bitcoinml.Bitcoinml.Script_template.Output)

Module type Script_template.Output

type t
val name : string
val encode : t -> Script.t
val decode : Script.t -> t
val check : Script.t -> bool
val spendable_by : Script.t -> Address.prefix -> Address.t
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_pubkeyhash/Input/index.html: -------------------------------------------------------------------------------- 1 | 2 | Input (bitcoinml.Bitcoinml__Script_pubkeyhash.Input)

Module Bitcoinml__Script_pubkeyhash.Input

type t = {
signature : string;
pubkey : string;
}
val check : (Bitcoinml.Script.opcode list * 'a) -> bool
val encode : t -> Bitcoinml.Script.t
val decode : (Bitcoinml.Script.opcode list * 'a) -> t
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_template/index.html: -------------------------------------------------------------------------------- 1 | 2 | Bitcoinml__Script_template (bitcoinml.Bitcoinml__Script_template)

Module Bitcoinml__Script_template

module EmptyInput : sig ... end
module type Output = sig ... end
module type Input = sig ... end
module Make_template : functor (ITemplate : Input) -> functor (OTemplate : Output) -> sig ... end
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_pubkey/Script_pubkey/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_pubkey (bitcoinml.Bitcoinml__Script_pubkey.Script_pubkey)

Module Bitcoinml__Script_pubkey.Script_pubkey

type t_inp = Bitcoinml__Script_template.Make_template(Input)(Output).t_inp =
| ITemplate
type t_out = Bitcoinml__Script_template.Make_template(Input)(Output).t_out =
| OTemplate
val input : t_inp
val output : t_out
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_template/module-type-Output/index.html: -------------------------------------------------------------------------------- 1 | 2 | Output (bitcoinml.Bitcoinml__Script_template.Output)

Module type Bitcoinml__Script_template.Output

type t
val name : string
val encode : t -> Bitcoinml.Script.t
val decode : Bitcoinml.Script.t -> t
val check : Bitcoinml.Script.t -> bool
val spendable_by : Bitcoinml.Script.t -> Bitcoinml.Address.prefix -> Bitcoinml.Address.t
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_template/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_template (bitcoinml.Bitcoinml.Script_template)

Module Bitcoinml.Script_template

module EmptyInput : sig ... end
module type Output = sig ... end
module type Input = sig ... end
module Make_template : functor (ITemplate : Input) -> functor (OTemplate : Output) -> sig ... end
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_nulldata/Script_nulldata/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_nulldata (bitcoinml.Bitcoinml__Script_nulldata.Script_nulldata)

Module Bitcoinml__Script_nulldata.Script_nulldata

type t_inp = Bitcoinml__Script_template.Make_template(Input)(Output).t_inp =
| ITemplate
type t_out = Bitcoinml__Script_template.Make_template(Input)(Output).t_out =
| OTemplate
val input : t_inp
val output : t_out
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_template/Make_template/index.html: -------------------------------------------------------------------------------- 1 | 2 | Make_template (bitcoinml.Bitcoinml__Script_template.Make_template)

Module Bitcoinml__Script_template.Make_template

Parameters

Signature

type t_inp =
| ITemplate
type t_out =
| OTemplate
val input : t_inp
val output : t_out
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_pubkeyhash/Script_pubkeyhash/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_pubkeyhash (bitcoinml.Bitcoinml__Script_pubkeyhash.Script_pubkeyhash)

Module Bitcoinml__Script_pubkeyhash.Script_pubkeyhash

type t_inp = Bitcoinml__Script_template.Make_template(Input)(Output).t_inp =
| ITemplate
type t_out = Bitcoinml__Script_template.Make_template(Input)(Output).t_out =
| OTemplate
val input : t_inp
val output : t_out
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_scripthash/Script_scripthash/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_scripthash (bitcoinml.Bitcoinml__Script_scripthash.Script_scripthash)

Module Bitcoinml__Script_scripthash.Script_scripthash

type t_inp = Bitcoinml__Script_template.Make_template(Input)(Output).t_inp =
| ITemplate
type t_out = Bitcoinml__Script_template.Make_template(Input)(Output).t_out =
| OTemplate
val input : t_inp
val output : t_out
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_pubkey/Script_pubkey/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_pubkey (bitcoinml.Bitcoinml.Script_pubkey.Script_pubkey)

Module Script_pubkey.Script_pubkey

type t_inp = Bitcoinml__Script_template.Make_template(Input)(Output).t_inp =
| ITemplate
type t_out = Bitcoinml__Script_template.Make_template(Input)(Output).t_out =
| OTemplate
val input : t_inp
val output : t_out
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_witnesscommitment/Output/index.html: -------------------------------------------------------------------------------- 1 | 2 | Output (bitcoinml.Bitcoinml.Script_witnesscommitment.Output)

Module Script_witnesscommitment.Output

type t = Hash.t
val name : string
val encode : string -> Script.t
val decode : (Script.opcode list * 'a) -> Hash.t
val check : (Script.opcode list * 'a) -> bool
val spendable_by : 'a -> 'b -> string
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_nulldata/Script_nulldata/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_nulldata (bitcoinml.Bitcoinml.Script_nulldata.Script_nulldata)

Module Script_nulldata.Script_nulldata

type t_inp = Bitcoinml__Script_template.Make_template(Input)(Output).t_inp =
| ITemplate
type t_out = Bitcoinml__Script_template.Make_template(Input)(Output).t_out =
| OTemplate
val input : t_inp
val output : t_out
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_template/Make_template/argument-2-OTemplate/index.html: -------------------------------------------------------------------------------- 1 | 2 | 2-OTemplate (bitcoinml.Bitcoinml.Script_template.Make_template.2-OTemplate)

Parameter Make_template.2-OTemplate

type t
val name : string
val encode : t -> Script.t
val decode : Script.t -> t
val check : Script.t -> bool
val spendable_by : Script.t -> Address.prefix -> Address.t
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_template/Make_template/index.html: -------------------------------------------------------------------------------- 1 | 2 | Make_template (bitcoinml.Bitcoinml.Script_template.Make_template)

Module Script_template.Make_template

Parameters

Signature

type t_inp =
| ITemplate
type t_out =
| OTemplate
val input : t_inp
val output : t_out
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_pubkeyhash/Script_pubkeyhash/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_pubkeyhash (bitcoinml.Bitcoinml.Script_pubkeyhash.Script_pubkeyhash)

Module Script_pubkeyhash.Script_pubkeyhash

type t_inp = Bitcoinml__Script_template.Make_template(Input)(Output).t_inp =
| ITemplate
type t_out = Bitcoinml__Script_template.Make_template(Input)(Output).t_out =
| OTemplate
val input : t_inp
val output : t_out
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_scripthash/Script_scripthash/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_scripthash (bitcoinml.Bitcoinml.Script_scripthash.Script_scripthash)

Module Script_scripthash.Script_scripthash

type t_inp = Bitcoinml__Script_template.Make_template(Input)(Output).t_inp =
| ITemplate
type t_out = Bitcoinml__Script_template.Make_template(Input)(Output).t_out =
| OTemplate
val input : t_inp
val output : t_out
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_multisig/Script_multisig/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_multisig (bitcoinml.Bitcoinml__Script_multisig.Script_multisig)

Module Bitcoinml__Script_multisig.Script_multisig

type t_inp = Bitcoinml__Script_template.Make_template(Bitcoinml.Script_template.EmptyInput)(Output).t_inp =
| ITemplate
type t_out = Bitcoinml__Script_template.Make_template(Bitcoinml.Script_template.EmptyInput)(Output).t_out =
| OTemplate
val input : t_inp
val output : t_out
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_nulldata/Output/index.html: -------------------------------------------------------------------------------- 1 | 2 | Output (bitcoinml.Bitcoinml.Script_nulldata.Output)

Module Script_nulldata.Output

type t = Script.data
val name : string
val check : (Script.opcode list * 'a) -> bool
val encode : Script.data -> Script.t
val decode : (Script.opcode list * 'a) -> Script.data
val spendable_by : 'a -> 'b -> string
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_witnesspubkeyhash/Script_witnesspubkeyhash/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_witnesspubkeyhash (bitcoinml.Bitcoinml__Script_witnesspubkeyhash.Script_witnesspubkeyhash)

Module Bitcoinml__Script_witnesspubkeyhash.Script_witnesspubkeyhash

type t_inp = Bitcoinml__Script_template.Make_template(Input)(Output).t_inp =
| ITemplate
type t_out = Bitcoinml__Script_template.Make_template(Input)(Output).t_out =
| OTemplate
val input : t_inp
val output : t_out
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_witnessscripthash/Script_witnessscripthash/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_witnessscripthash (bitcoinml.Bitcoinml__Script_witnessscripthash.Script_witnessscripthash)

Module Bitcoinml__Script_witnessscripthash.Script_witnessscripthash

type t_inp = Bitcoinml__Script_template.Make_template(Input)(Output).t_inp =
| ITemplate
type t_out = Bitcoinml__Script_template.Make_template(Input)(Output).t_out =
| OTemplate
val input : t_inp
val output : t_out
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_multisig/Output/index.html: -------------------------------------------------------------------------------- 1 | 2 | Output (bitcoinml.Bitcoinml__Script_multisig.Output)

Module Bitcoinml__Script_multisig.Output

type t = {
m : int;
pubkeys : string list;
}
val name : string
val check : 'a -> bool
val encode : 'a -> Bitcoinml.Script.t
val decode : 'a -> t
val spendable_by : 'a -> 'b -> string
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_witnesscommitment/Output/index.html: -------------------------------------------------------------------------------- 1 | 2 | Output (bitcoinml.Bitcoinml__Script_witnesscommitment.Output)

Module Bitcoinml__Script_witnesscommitment.Output

type t = Bitcoinml.Hash.t
val name : string
val encode : string -> Bitcoinml.Script.t
val decode : (Bitcoinml.Script.opcode list * 'a) -> Bitcoinml.Hash.t
val check : (Bitcoinml.Script.opcode list * 'a) -> bool
val spendable_by : 'a -> 'b -> string
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_multisig/Script_multisig/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_multisig (bitcoinml.Bitcoinml.Script_multisig.Script_multisig) -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_template/Make_template/argument-2-OTemplate/index.html: -------------------------------------------------------------------------------- 1 | 2 | 2-OTemplate (bitcoinml.Bitcoinml__Script_template.Make_template.2-OTemplate)

Parameter Make_template.2-OTemplate

type t
val name : string
val encode : t -> Bitcoinml.Script.t
val decode : Bitcoinml.Script.t -> t
val check : Bitcoinml.Script.t -> bool
val spendable_by : Bitcoinml.Script.t -> Bitcoinml.Address.prefix -> Bitcoinml.Address.t
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_multisig/Output/index.html: -------------------------------------------------------------------------------- 1 | 2 | Output (bitcoinml.Bitcoinml.Script_multisig.Output)

Module Script_multisig.Output

type t = {
m : int;
pubkeys : string list;
}
val name : string
val check : 'a -> bool
val encode : 'a -> Script.t
val decode : 'a -> t
val spendable_by : 'a -> 'b -> string
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_witnesspubkeyhash/Script_witnesspubkeyhash/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_witnesspubkeyhash (bitcoinml.Bitcoinml.Script_witnesspubkeyhash.Script_witnesspubkeyhash)

Module Script_witnesspubkeyhash.Script_witnesspubkeyhash

type t_inp = Bitcoinml__Script_template.Make_template(Input)(Output).t_inp =
| ITemplate
type t_out = Bitcoinml__Script_template.Make_template(Input)(Output).t_out =
| OTemplate
val input : t_inp
val output : t_out
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_witnessscripthash/Script_witnessscripthash/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_witnessscripthash (bitcoinml.Bitcoinml.Script_witnessscripthash.Script_witnessscripthash)

Module Script_witnessscripthash.Script_witnessscripthash

type t_inp = Bitcoinml__Script_template.Make_template(Input)(Output).t_inp =
| ITemplate
type t_out = Bitcoinml__Script_template.Make_template(Input)(Output).t_out =
| OTemplate
val input : t_inp
val output : t_out
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Tx/Witness/index.html: -------------------------------------------------------------------------------- 1 | 2 | Witness (bitcoinml.Bitcoinml__Tx.Witness)

Module Bitcoinml__Tx.Witness

Witness data

type t = {
hash : Bitcoinml.Hash.t;
marker : int;
flag : int;
size : int;
}
val serialize_fields : In.t list -> string

serialize_fields inl serializes witness fields of inl inputs

val parse_fields : Bitstring.bitstring -> int -> Bitstring.bitstring * string list list option

parse_fields bdata parses n witness fields in bdata and returns the rest of the data and the list of fields

-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Tx/Witness/index.html: -------------------------------------------------------------------------------- 1 | 2 | Witness (bitcoinml.Bitcoinml.Tx.Witness)

Module Tx.Witness

Witness data

type t = {
hash : Hash.t;
marker : int;
flag : int;
size : int;
}
val serialize_fields : In.t list -> string

serialize_fields inl serializes witness fields of inl inputs

val parse_fields : Bitstring.bitstring -> int -> Bitstring.bitstring * string list list option

parse_fields bdata parses n witness fields in bdata and returns the rest of the data and the list of fields

-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_witnesscommitment/Script_witnesspubkeyhash/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_witnesspubkeyhash (bitcoinml.Bitcoinml__Script_witnesscommitment.Script_witnesspubkeyhash)

Module Bitcoinml__Script_witnesscommitment.Script_witnesspubkeyhash

type t_inp = Bitcoinml__Script_template.Make_template(Bitcoinml.Script_template.EmptyInput)(Output).t_inp =
| ITemplate
type t_out = Bitcoinml__Script_template.Make_template(Bitcoinml.Script_template.EmptyInput)(Output).t_out =
| OTemplate
val input : t_inp
val output : t_out
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_nulldata/Output/index.html: -------------------------------------------------------------------------------- 1 | 2 | Output (bitcoinml.Bitcoinml__Script_nulldata.Output)

Module Bitcoinml__Script_nulldata.Output

type t = Bitcoinml.Script.data
val name : string
val check : (Bitcoinml.Script.opcode list * 'a) -> bool
val encode : Bitcoinml.Script.data -> Bitcoinml.Script.t
val decode : (Bitcoinml.Script.opcode list * 'a) -> Bitcoinml.Script.data
val spendable_by : 'a -> 'b -> string
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_witnesscommitment/Script_witnesspubkeyhash/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_witnesspubkeyhash (bitcoinml.Bitcoinml.Script_witnesscommitment.Script_witnesspubkeyhash)

Module Script_witnesscommitment.Script_witnesspubkeyhash

type t_inp = Bitcoinml__Script_template.Make_template(Bitcoinml.Script_template.EmptyInput)(Output).t_inp =
| ITemplate
type t_out = Bitcoinml__Script_template.Make_template(Bitcoinml.Script_template.EmptyInput)(Output).t_out =
| OTemplate
val input : t_inp
val output : t_out
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_pubkey/Output/index.html: -------------------------------------------------------------------------------- 1 | 2 | Output (bitcoinml.Bitcoinml.Script_pubkey.Output)

Module Script_pubkey.Output

type t = string
val name : string
val check : (Script.opcode list * 'a) -> bool
val encode : Script.data -> Script.t
val decode : (Script.opcode list * 'a) -> Script.data
val spendable_by : (Script.opcode list * 'a) -> Address.prefix -> Address.t
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_pubkeyhash/Output/index.html: -------------------------------------------------------------------------------- 1 | 2 | Output (bitcoinml.Bitcoinml.Script_pubkeyhash.Output)

Module Script_pubkeyhash.Output

type t = string
val name : string
val check : (Script.opcode list * 'a) -> bool
val encode : Script.data -> Script.t
val decode : (Script.opcode list * 'a) -> Script.data
val spendable_by : (Script.opcode list * 'a) -> Address.prefix -> Address.t
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_scripthash/Output/index.html: -------------------------------------------------------------------------------- 1 | 2 | Output (bitcoinml.Bitcoinml.Script_scripthash.Output)

Module Script_scripthash.Output

type t = string
val name : string
val check : (Script.opcode list * 'a) -> bool
val encode : Script.data -> Script.t
val decode : (Script.opcode list * 'a) -> Script.data
val spendable_by : (Script.opcode list * 'a) -> Address.prefix -> Address.t
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_witnesspubkeyhash/Output/index.html: -------------------------------------------------------------------------------- 1 | 2 | Output (bitcoinml.Bitcoinml.Script_witnesspubkeyhash.Output)

Module Script_witnesspubkeyhash.Output

type t = string
val name : string
val check : (Script.opcode list * 'a) -> bool
val encode : Script.data -> Script.t
val decode : (Script.opcode list * 'a) -> Script.data
val spendable_by : (Script.opcode list * 'a) -> Address.prefix -> Address.t
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_witnessscripthash/Output/index.html: -------------------------------------------------------------------------------- 1 | 2 | Output (bitcoinml.Bitcoinml.Script_witnessscripthash.Output)

Module Script_witnessscripthash.Output

type t = string
val name : string
val check : (Script.opcode list * 'a) -> bool
val encode : Script.data -> Script.t
val decode : (Script.opcode list * 'a) -> Script.data
val spendable_by : (Script.opcode list * 'a) -> Address.prefix -> Address.t
-------------------------------------------------------------------------------- /src/proto.mli: -------------------------------------------------------------------------------- 1 | open Stdint 2 | 3 | type header = { 4 | magic : int32; 5 | command : string; 6 | length : uint32; 7 | checksum : string; 8 | } 9 | 10 | type invvect = 11 | INV_ERROR 12 | | INV_TX of Hash.t 13 | | INV_BLOCK of Hash.t 14 | | INV_FILTERED_BLOCK of Hash.t 15 | 16 | type addr = { 17 | services : uint64; 18 | address : string; 19 | port : uint16; 20 | } 21 | 22 | type inv = invvect list 23 | 24 | type getdata = inv 25 | type notfound = inv 26 | 27 | type version = { 28 | version : int32; 29 | services : int64; 30 | time : float; 31 | addr_recv : addr; 32 | addr_from : addr; 33 | nonce : int64; 34 | user_agent : string; 35 | start_height: int32; 36 | relay : bool; 37 | } 38 | 39 | 40 | 41 | type getheaders = { 42 | version : int32; 43 | hashes : Hash.t list; 44 | stop : Hash.t; 45 | } 46 | 47 | type getblocks = getheaders 48 | 49 | 50 | type headers = Block.Header.t list 51 | 52 | type ping = int64 53 | type pong = int64 54 | 55 | 56 | type t = 57 | | INVALID 58 | | VERSION of version 59 | | VERACK 60 | | PING of ping 61 | | PONG of pong 62 | | INV of inv 63 | | ADDR 64 | | GETDATA of inv 65 | | NOTFOUND of notfound 66 | | GETBLOCKS of getblocks 67 | | GETHEADERS of getheaders 68 | | TX of Tx.t 69 | | BLOCK of Block_lazy.t 70 | | HEADERS of headers 71 | | GETADDR 72 | | MEMPOOL 73 | | REJECT 74 | | FEEFILTER of Uint64.t 75 | | SENDHEADERS 76 | 77 | (* Bloom filter related *) 78 | | FILTERLOAD 79 | | FILTERADD 80 | | FILTERCLEAR 81 | | MERKLEBLOCK 82 | 83 | | ALERT 84 | ;; 85 | (** Protocol message type *) 86 | 87 | 88 | val string_of_command : t -> string 89 | (** [string_of_command m] returns the command name of the message *) 90 | 91 | val parse : header -> string -> t 92 | (** [parse h data] parses [data] and returns the message *) 93 | 94 | val parse_header: string -> header 95 | (** [parse_header h] parses the header [h] *) 96 | 97 | val bitstring_of_addr : addr -> Bitstring.t 98 | (** [bitstring_of_addr addr] returns the bitstring of an address [addr] *) 99 | 100 | 101 | val serialize_header : header -> string 102 | (** [serialize_header h] serializes the message header [h] *) 103 | 104 | val serialize_message : t -> string 105 | (** [serialize_message m] serializes the message [m] *) 106 | 107 | val serialize : Params.t -> t -> string 108 | (** [serialize p m] serializes the message [m] encapsulating it with the header comptabile with params [p] *) -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_pubkey/Output/index.html: -------------------------------------------------------------------------------- 1 | 2 | Output (bitcoinml.Bitcoinml__Script_pubkey.Output)

Module Bitcoinml__Script_pubkey.Output

type t = string
val name : string
val check : (Bitcoinml.Script.opcode list * 'a) -> bool
val encode : Bitcoinml.Script.data -> Bitcoinml.Script.t
val decode : (Bitcoinml.Script.opcode list * 'a) -> Bitcoinml.Script.data
val spendable_by : (Bitcoinml.Script.opcode list * 'a) -> Bitcoinml.Address.prefix -> Bitcoinml.Address.t
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_pubkeyhash/Output/index.html: -------------------------------------------------------------------------------- 1 | 2 | Output (bitcoinml.Bitcoinml__Script_pubkeyhash.Output)

Module Bitcoinml__Script_pubkeyhash.Output

type t = string
val name : string
val check : (Bitcoinml.Script.opcode list * 'a) -> bool
val encode : Bitcoinml.Script.data -> Bitcoinml.Script.t
val decode : (Bitcoinml.Script.opcode list * 'a) -> Bitcoinml.Script.data
val spendable_by : (Bitcoinml.Script.opcode list * 'a) -> Bitcoinml.Address.prefix -> Bitcoinml.Address.t
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_scripthash/Output/index.html: -------------------------------------------------------------------------------- 1 | 2 | Output (bitcoinml.Bitcoinml__Script_scripthash.Output)

Module Bitcoinml__Script_scripthash.Output

type t = string
val name : string
val check : (Bitcoinml.Script.opcode list * 'a) -> bool
val encode : Bitcoinml.Script.data -> Bitcoinml.Script.t
val decode : (Bitcoinml.Script.opcode list * 'a) -> Bitcoinml.Script.data
val spendable_by : (Bitcoinml.Script.opcode list * 'a) -> Bitcoinml.Address.prefix -> Bitcoinml.Address.t
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_witnesspubkeyhash/Output/index.html: -------------------------------------------------------------------------------- 1 | 2 | Output (bitcoinml.Bitcoinml__Script_witnesspubkeyhash.Output)

Module Bitcoinml__Script_witnesspubkeyhash.Output

type t = string
val name : string
val check : (Bitcoinml.Script.opcode list * 'a) -> bool
val encode : Bitcoinml.Script.data -> Bitcoinml.Script.t
val decode : (Bitcoinml.Script.opcode list * 'a) -> Bitcoinml.Script.data
val spendable_by : (Bitcoinml.Script.opcode list * 'a) -> Bitcoinml.Address.prefix -> Bitcoinml.Address.t
-------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml__Script_witnessscripthash/Output/index.html: -------------------------------------------------------------------------------- 1 | 2 | Output (bitcoinml.Bitcoinml__Script_witnessscripthash.Output)

Module Bitcoinml__Script_witnessscripthash.Output

type t = string
val name : string
val check : (Bitcoinml.Script.opcode list * 'a) -> bool
val encode : Bitcoinml.Script.data -> Bitcoinml.Script.t
val decode : (Bitcoinml.Script.opcode list * 'a) -> Bitcoinml.Script.data
val spendable_by : (Bitcoinml.Script.opcode list * 'a) -> Bitcoinml.Address.prefix -> Bitcoinml.Address.t
-------------------------------------------------------------------------------- /src/address.ml: -------------------------------------------------------------------------------- 1 | open Bitstring;; 2 | open Stdint;; 3 | 4 | let rec b2l t = match String.length t with 5 | | 0 -> [] 6 | | n -> 7 | let ch = String.get t 0 |> Char.code in 8 | ch :: (b2l @@ String.sub t 1 (n-1)) 9 | ;; 10 | 11 | type prefix = { 12 | pubkeyhash: int; 13 | scripthash: int; 14 | hrp: string; 15 | };; 16 | 17 | type t = string;; 18 | 19 | module Bech32 = struct 20 | let charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";; 21 | 22 | let polymod values = 23 | let generators = [ 0x3b6a57b2; 0x26508e6d; 0x1ea119fa; 0x3d4233dd; 0x2a1462b3 ] in 24 | let rec pm vl chk = match vl with 25 | | [] -> chk 26 | | v :: vl' -> 27 | let top = chk lsr 25 in 28 | let chk' = ((chk land 0x1ffffff) lsl 5) lxor v in 29 | let rec genapply gl chk i = match gl with 30 | | [] -> chk 31 | | g::gl' -> 32 | if ((top lsr i) land 1) = 1 then 33 | genapply gl' (chk lxor g) (i+1) 34 | else 35 | genapply gl' chk (i+1) 36 | in pm vl' @@ genapply generators chk' 0 37 | in pm values 1 38 | ;; 39 | 40 | let hrp_expand hrp = 41 | let hrl = b2l hrp in 42 | (List.map (fun x -> x lsr 5) hrl) @ [0] @ (List.map (fun x -> x land 31) hrl) 43 | ;; 44 | 45 | let verify_checksum hrp data = polymod ((hrp_expand hrp) @ data) = 1;; 46 | 47 | let create_checksum hrp data = 48 | let pm = polymod ((hrp_expand hrp) @ data @ [0; 0; 0; 0; 0; 0]) lxor 1 in 49 | let rec pmp i pm = match i with 50 | | 6 -> [] 51 | | i -> ((pm lsr (5 * (5 - i))) land 31) :: (pmp (i+1) pm) 52 | in pmp 0 pm 53 | ;; 54 | 55 | let b32_encode hrp data = 56 | let comb = data @ (create_checksum hrp data) in 57 | let st = (List.fold_left (fun acc x -> acc ^ (String.make 1 (charset.[x]))) "" comb) in 58 | hrp ^ "1" ^ st 59 | ;; 60 | 61 | let convertbits prog frombits tobits pad = 62 | let rec cv p = 63 | let l = if bitstring_length p >= tobits then tobits else bitstring_length p in 64 | match l with 65 | | 0 -> [] 66 | (*| l when l > 0 && l < tobits -> padding? *) 67 | | l -> 68 | match%bitstring p with 69 | | {| 70 | e : l : bigendian; 71 | pp : -1 : bitstring 72 | |} -> (Int64.to_int e) :: cv pp 73 | in cv @@ bitstring_of_string prog 74 | ;; 75 | 76 | let encode hrp witver witprog = b32_encode hrp @@ [witver] @ convertbits witprog 8 5 true;; 77 | end 78 | 79 | 80 | let of_pubhash prefix pkh = 81 | let epkh = (String.make 1 @@ Char.chr prefix) ^ pkh in 82 | let shrip = String.sub (Hash.dsha256 epkh) 0 4 in 83 | (epkh ^ shrip) |> Base58.encode_check 84 | ;; 85 | 86 | 87 | let of_pub prefix pk = pk |> Hash.sha256 |> Hash.ripemd160 |> of_pubhash prefix;; 88 | 89 | 90 | let of_witness hrp witver witprog = Bech32.encode hrp witver witprog;; -------------------------------------------------------------------------------- /docs/bitcoinml/Bitcoinml/Script_verify/index.html: -------------------------------------------------------------------------------- 1 | 2 | Script_verify (bitcoinml.Bitcoinml.Script_verify)

Module Bitcoinml.Script_verify

module Sigver : sig ... end

Abstract module for verify functions

module SStack : sig ... end

Script execution stack

val verify : Sigver.t -> Script.t -> Script.t -> bool

verify sigver s1 s2 execute a script s1 and s2 using sigver

val is_spendable : Script.t -> bool

is_spendable s returns true if s is spendable by a recognized address

val spendable_by : Script.t -> Address.prefix -> string option

spendable_by s prefix returns the address (using prefix) which is able to spend the given script

val classify_output : Script.t -> string

classify_output s returns the template name of the output script s if any

--------------------------------------------------------------------------------