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

View File

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

View File

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

View File

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

View File

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