factor out bitcoin-utils

This commit is contained in:
timlucmiptev 2021-02-19 16:11:01 +02:00 committed by ixv
parent 483dc1a9ed
commit c04304503a
5 changed files with 67 additions and 65 deletions

View File

@ -1,4 +1,5 @@
/+ *bitcoin /- bc=bitcoin
/+ bcu=bitcoin-utils
|% |%
++ params ++ params
|% |%
@ -87,25 +88,25 @@
++ str ++ str
|% |%
++ read-bit ++ read-bit
|= s=bits |= s=bits:bc
^- [bit=@ub rest=bits] ^- [bit=@ub rest=bits:bc]
?> (gth wid.s 0) ?> (gth wid.s 0)
:* ?:((gth wid.s (met 0 dat.s)) 0b0 0b1) :* ?:((gth wid.s (met 0 dat.s)) 0b0 0b1)
[(dec wid.s) (end [0 (dec wid.s)] dat.s)] [(dec wid.s) (end [0 (dec wid.s)] dat.s)]
== ==
:: ::
++ read-bits ++ read-bits
|= [n=@ s=bits] |= [n=@ s=bits:bc]
^- [bits rest=bits] ^- [bits:bc rest=bits:bc]
=| bs=bits =| bs=bits:bc
|- |-
?: =(n 0) [bs s] ?: =(n 0) [bs s]
=^ b s (read-bit s) =^ b s (read-bit s)
$(n (dec n), bs (write-bits bs [1 b])) $(n (dec n), bs (write-bits bs [1 b]))
:: ::
++ write-bits ++ write-bits
|= [s1=bits s2=bits] |= [s1=bits:bc s2=bits:bc]
^- bits ^- bits:bc
[(add wid.s1 wid.s2) (can 0 ~[s2 s1])] [(add wid.s1 wid.s2) (can 0 ~[s2 s1])]
-- --
:: +gol: Golomb-Rice encoding/decoding :: +gol: Golomb-Rice encoding/decoding
@ -118,8 +119,8 @@
:: - p: golomb-rice p param :: - p: golomb-rice p param
:: ::
++ en ++ en
|= [s=bits x=@ p=@] |= [s=bits:bc x=@ p=@]
^- bits ^- bits:bc
=+ q=(rsh [0 p] x) =+ q=(rsh [0 p] x)
=+ unary=[+(q) (lsh [0 1] (dec (bex q)))] =+ unary=[+(q) (lsh [0 1] (dec (bex q)))]
=+ r=[p (end [0 p] x)] =+ r=[p (end [0 p] x)]
@ -127,15 +128,15 @@
(write-bits:str unary r) (write-bits:str unary r)
:: ::
++ de ++ de
|= [s=bits p=@] |= [s=bits:bc p=@]
^- [delta=@ rest=bits] ^- [delta=@ rest=bits:bc]
|^ ?> (gth wid.s 0) |^ ?> (gth wid.s 0)
=^ q s (get-q s) =^ q s (get-q s)
=^ r s (read-bits:str p s) =^ r s (read-bits:str p s)
[(add dat.r (lsh [0 p] q)) s] [(add dat.r (lsh [0 p] q)) s]
:: ::
++ get-q ++ get-q
|= s=bits |= s=bits:bc
=| q=@ =| q=@
=^ first-bit s (read-bit:str s) =^ first-bit s (read-bit:str s)
|- |-
@ -170,30 +171,30 @@
-- --
:: ::
++ parse-filter ++ parse-filter
|= filter=hexb |= filter=hexb:bc
^- [n=@ux gcs-set=bits] ^- [n=@ux gcs-set=bits:bc]
=/ n n:(de:csiz filter) =/ n n:(de:csiz:bcu filter)
=/ lead=@ ?:(=(1 wid.n) 1 +(wid.n)) =/ lead=@ ?:(=(1 wid.n) 1 +(wid.n))
:- dat.n :- dat.n
[(mul 8 (sub wid.filter lead)) `@ub`dat:(drop:byt lead filter)] [(mul 8 (sub wid.filter lead)) `@ub`dat:(drop:byt:bcu lead filter)]
:: +to-key: blockhash (little endian) to key for siphash :: +to-key: blockhash (little endian) to key for siphash
:: ::
++ to-key ++ to-key
|= blockhash=tape |= blockhash=tape
^- byts ^- byts
%+ take:byt 16 %+ take:byt:bcu 16
%- flip:byt %- flip:byt:bcu
(to-hexb (crip blockhash)) (to-hexb:bcu (crip blockhash))
:: +match: whether block filter matches *any* target scriptpubkeys :: +match: whether block filter matches *any* target scriptpubkeys
:: - filter: full block filter, with leading N :: - filter: full block filter, with leading N
:: - k: key for siphash (end of blockhash, reversed) :: - k: key for siphash (end of blockhash, reversed)
:: - targets: scriptpubkeys to match :: - targets: scriptpubkeys to match
:: ::
++ match ++ match
|= [filter=hexb k=byts targets=(list byts)] |= [filter=hexb:bc k=byts targets=(list byts)]
^- ? ^- ?
=/ [p=@ m=@] [p:params m:params] =/ [p=@ m=@] [p:params m:params]
=/ [n=@ux gcs-set=bits] (parse-filter filter) =/ [n=@ux gcs-set=bits:bc] (parse-filter filter)
=+ target-hs=(set-construct:hsh targets k (mul n m)) =+ target-hs=(set-construct:hsh targets k (mul n m))
=+ last-val=0 =+ last-val=0
|- |-
@ -214,15 +215,15 @@
:: - targets: scriptpubkeys to match :: - targets: scriptpubkeys to match
:: ::
++ all-match ++ all-match
|= [filter=hexb k=byts targets=(list byts)] |= [filter=hexb:bc k=byts targets=(list byts)]
^- (set hexb) ^- (set hexb:bc)
%- ~(gas in *(set hexb)) %- ~(gas in *(set hexb:bc))
=/ [p=@ m=@] [p:params m:params] =/ [p=@ m=@] [p:params m:params]
=/ [n=@ux gcs-set=bits] (parse-filter filter) =/ [n=@ux gcs-set=bits:bc] (parse-filter filter)
=/ target-map=(map @ hexb) =/ target-map=(map @ hexb:bc)
%- ~(gas by *(map @ hexb)) %- ~(gas by *(map @ hexb:bc))
%+ turn targets %+ turn targets
|=(t=hexb [(to-range:hsh t (mul n m) k) t]) |=(t=hexb:bc [(to-range:hsh t (mul n m) k) t])
=+ target-hs=(sort ~(tap in ~(key by target-map)) lth) =+ target-hs=(sort ~(tap in ~(key by target-map)) lth)
=+ last-val=0 =+ last-val=0
=| matches=(list @) =| matches=(list @)

View File

@ -1,9 +1,9 @@
:: lib/btc.hoon :: lib/bitcoin-utils.hoon
:: Utilities for working with BTC data types and transactions :: Utilities for working with BTC data types and transactions
:: ::
/- sur=bitcoin /- sur=bitcoin
^? :: ^?
=< [sur .] :: =< [sur .]
=, sur =, sur
|% |%
:: ::

View File

@ -1,18 +1,18 @@
/- sur=btc-provider, json-rpc /- bp=btc-provider, json-rpc, bc=bitcoin
/+ *bitcoin /+ bcu=bitcoin-utils
^? ^?
=< [sur .] ::=< [sur .]
=, sur ::=, sur
|% |%
++ address-to-cord ++ address-to-cord
|= =address ^- cord |= =address:bc ^- cord
?: ?=([%base58 *] address) ?: ?=([%base58 *] address)
(scot %uc +.address) (scot %uc +.address)
+.address +.address
:: ::
++ address-from-cord ++ address-from-cord
|= addrc=@t |= addrc=@t
^- address ^- address:bc
?. ?| =("bc1" (scag 3 (trip addrc))) ?. ?| =("bc1" (scag 3 (trip addrc)))
=("tb1" (scag 3 (trip addrc))) =("tb1" (scag 3 (trip addrc)))
== ==
@ -20,7 +20,7 @@
[%bech32 addrc] [%bech32 addrc]
:: ::
++ hexb-to-cord ++ hexb-to-cord
|= =hexb ^- cord |= =hexb:bc ^- cord
(en:base16:mimes:html hexb) (en:base16:mimes:html hexb)
:: +from-epoch: time since Jan 1, 1970 in seconds. :: +from-epoch: time since Jan 1, 1970 in seconds.
:: ::
@ -48,7 +48,7 @@
== ==
:: ::
++ gen-request ++ gen-request
|= [=host-info ract=action:rpc-types] |= [=host-info:bp ract=action:rpc-types:bp]
^- request:http ^- request:http
%+ rpc-action-to-http %+ rpc-action-to-http
api-url.host-info ract api-url.host-info ract
@ -58,7 +58,7 @@
|% |%
++ parse-result ++ parse-result
|= res=response:json-rpc |= res=response:json-rpc
|^ ^- result:rpc-types |^ ^- result:rpc-types:bp
~| -.res ~| -.res
?> ?=(%result -.res) ?> ?=(%result -.res)
?+ id.res ~|([%unsupported-result id.res] !!) ?+ id.res ~|([%unsupported-result id.res] !!)
@ -90,7 +90,7 @@
++ utxo ++ utxo
%- ot %- ot
:~ ['tx_pos' ni] :~ ['tx_pos' ni]
['tx_hash' (cu to-hexb so)] ['tx_hash' (cu to-hexb:bcu so)]
[%height ni] [%height ni]
[%value ni] [%value ni]
[%recvd (cu from-epoch ni)] [%recvd (cu from-epoch ni)]
@ -98,7 +98,7 @@
++ tx-vals ++ tx-vals
%- ot %- ot
:~ [%included bo] :~ [%included bo]
[%txid (cu to-hexb so)] [%txid (cu to-hexb:bcu so)]
[%confs ni] [%confs ni]
[%recvd (cu from-epoch ni)] [%recvd (cu from-epoch ni)]
[%inputs (ar tx-val)] [%inputs (ar tx-val)]
@ -106,19 +106,19 @@
== ==
++ tx-val ++ tx-val
%- ot %- ot
:~ [%txid (cu to-hexb so)] :~ [%txid (cu to-hexb:bcu so)]
[%pos ni] [%pos ni]
[%address (cu address-from-cord so)] [%address (cu address-from-cord so)]
[%value ni] [%value ni]
== ==
++ raw-tx ++ raw-tx
%- ot %- ot
:~ [%txid (cu to-hexb so)] :~ [%txid (cu to-hexb:bcu so)]
[%rawtx (cu to-hexb so)] [%rawtx (cu to-hexb:bcu so)]
== ==
++ broadcast-tx ++ broadcast-tx
%- ot %- ot
:~ [%txid (cu to-hexb so)] :~ [%txid (cu to-hexb:bcu so)]
[%broadcast bo] [%broadcast bo]
[%included bo] [%included bo]
== ==
@ -126,14 +126,14 @@
%- ot %- ot
:~ [%block ni] :~ [%block ni]
[%fee (mu ni)] [%fee (mu ni)]
[%blockhash (cu to-hexb so)] [%blockhash (cu to-hexb:bcu so)]
[%blockfilter (cu to-hexb so)] [%blockfilter (cu to-hexb:bcu so)]
== ==
-- --
-- --
:: ::
++ rpc-action-to-http ++ rpc-action-to-http
|= [endpoint=@t ract=action:rpc-types] |= [endpoint=@t ract=action:rpc-types:bp]
|^ ^- request:http |^ ^- request:http
?- -.ract ?- -.ract
%get-address-info %get-address-info

View File

@ -1,4 +1,4 @@
/+ *test, *bitcoin, bip32 /+ *test, *bitcoin-utils, bip32
=, secp:crypto =, secp:crypto
=+ ecc=secp256k1 =+ ecc=secp256k1
|% |%

View File

@ -1,23 +1,24 @@
/+ *test, *btc-wallet, btc /- bc=bitcoin
/+ *test, *btc-wallet
|% |%
+$ wallet-vector +$ wallet-vector
$: =xpub:btc $: =xpub:bc
=chyg =chyg
=idx:btc =idx:bc
=address:btc =address:bc
== ==
+$ vector +$ vector
$: =xpub:btc $: =xpub:bc
eny=@uv eny=@uv
block=@ud block=@ud
feyb=sats feyb=sats
ins=(list insel) ins=(list insel)
outs=(list txo) outs=(list txo)
expect=[selected=(unit (list insel)) chng=(unit sats:btc)] expect=[selected=(unit (list insel)) chng=(unit sats:bc)]
== ==
++ mk-utxo ++ mk-utxo
|= value=sats:btc |= value=sats:bc
^- utxo:btc ^- utxo:bc
:* pos=0 :* pos=0
[wid=32 dat=0xc493.f6f1.4668.5f76.b44f.0c77.ca88.120c.b8bc.89f5.34fe.69b6.8288.27b9.74e6.8849] [wid=32 dat=0xc493.f6f1.4668.5f76.b44f.0c77.ca88.120c.b8bc.89f5.34fe.69b6.8288.27b9.74e6.8849]
height=3 height=3
@ -82,7 +83,7 @@
[[%bech32 'bc1qlwd7mw33uea5m8r2lsnsrkc7gp2qynrxsfxpfm'] 200.000 ~] [[%bech32 'bc1qlwd7mw33uea5m8r2lsnsrkc7gp2qynrxsfxpfm'] 200.000 ~]
== ==
:* *(unit (list insel)) :* *(unit (list insel))
*(unit sats:btc) *(unit sats:bc)
== ==
== ==
:* 'zpub6rFR7y4Q2AijBEqTUquhVz398htDFrtymD9xYYfG1m4wAcvPhXNfE3EfH1r1ADqtfSdVCToUG868RvUUkgDKf31mGDtKsAYz2oz2AGutZYs' :* 'zpub6rFR7y4Q2AijBEqTUquhVz398htDFrtymD9xYYfG1m4wAcvPhXNfE3EfH1r1ADqtfSdVCToUG868RvUUkgDKf31mGDtKsAYz2oz2AGutZYs'
@ -94,7 +95,7 @@
[[%bech32 'bc1qlwd7mw33uea5m8r2lsnsrkc7gp2qynrxsfxpfm'] 200.000 ~] [[%bech32 'bc1qlwd7mw33uea5m8r2lsnsrkc7gp2qynrxsfxpfm'] 200.000 ~]
== ==
:* `~[[(mk-utxo 500.000) %0 2]] :* `~[[(mk-utxo 500.000) %0 2]]
*(unit sats:btc) *(unit sats:bc)
== ==
== ==
== ==
@ -112,7 +113,7 @@
[[%bech32 'bc1qlwd7mw33uea5m8r2lsnsrkc7gp2qynrxsfxpfm'] 204 ~] [[%bech32 'bc1qlwd7mw33uea5m8r2lsnsrkc7gp2qynrxsfxpfm'] 204 ~]
== ==
:* `~[[(mk-utxo 500.000) %0 2]] :* `~[[(mk-utxo 500.000) %0 2]]
*(unit sats:btc) *(unit sats:bc)
== ==
== ==
== ==
@ -136,7 +137,7 @@
|= v=wallet-vector |= v=wallet-vector
=/ w=walt (from-xpub xpub.v fprint ~ ~ ~) =/ w=walt (from-xpub xpub.v fprint ~ ~ ~)
=/ =address (~(mk-address wad w chyg.v) idx.v) =/ =address (~(mk-address wad w chyg.v) idx.v)
=. w (~(update-address wad w chyg.v) address [%.n %0 0 *(set utxo:btc)]) =. w (~(update-address wad w chyg.v) address [%.n %0 0 *(set utxo:bc)])
=/ [w2=walt c=chyg i=idx] (need (address-coords address ~[w])) =/ [w2=walt c=chyg i=idx] (need (address-coords address ~[w]))
;: weld ;: weld
%+ expect-eq %+ expect-eq
@ -158,7 +159,7 @@
|= v=vector |= v=vector
=/ w=walt (from-xpub xpub.v fprint ~ ~ ~) =/ w=walt (from-xpub xpub.v fprint ~ ~ ~)
=. wach.w =. wach.w
%- ~(gas by *(map address:btc addi)) %- ~(gas by *(map address:bc addi))
%+ turn ins.v %+ turn ins.v
|= i=insel |= i=insel
:- (~(mk-address wad w chyg.i) idx.i) :- (~(mk-address wad w chyg.i) idx.i)
@ -177,7 +178,7 @@
++ insels-to-wach ++ insels-to-wach
|= [w=walt is=(list insel)] |= [w=walt is=(list insel)]
^- wach ^- wach
%- ~(gas by *(map address:btc addi)) %- ~(gas by *(map address:bc addi))
%+ turn is %+ turn is
|= i=insel |= i=insel
:- (~(mk-address wad w chyg.i) idx.i) :- (~(mk-address wad w chyg.i) idx.i)