re-arrange bitcoin-utils

This commit is contained in:
timlucmiptev 2021-02-20 12:28:49 +02:00 committed by ixv
parent c04304503a
commit 68d9d7ae57
16 changed files with 1291 additions and 142 deletions

10
.README.md.~undo-tree~ Normal file
View File

@ -0,0 +1,10 @@
(undo-tree-save-format-version . 1)
"d879f99ea831bde5c1b526d30e9dbad055578a0f"
[nil nil nil nil (24624 58013 257440 0) 0 nil]
([nil nil ((#("-test %/tests/lib/bip/b158 ~" 0 28 (markdown-gfm-code (1371 1479) fontified t face (markdown-pre-face markdown-code-face) font-lock-multiline t)) . 1385) (t 24623 50711 138219 760000)) nil (24624 58013 257439 0) 0 nil])
([nil nil ((nil rear-nonsticky nil 1412 . 1413) (nil fontified nil 1385 . 1413) (1385 . 1413)) nil (24624 58013 257438 0) 0 nil])
([nil nil ((#("-test %/tests/lib/bitcoin-utils ~" 0 9 (fontified t markdown-gfm-code (1371 1479) face (markdown-pre-face markdown-code-face) font-lock-multiline t) 9 33 (fontified t markdown-gfm-code (1371 1479) face (markdown-pre-face markdown-code-face) font-lock-multiline t)) . 1445) (undo-tree-id0 . -9) (undo-tree-id1 . -9) (undo-tree-id2 . -9) (undo-tree-id3 . -9) (undo-tree-id4 . -9) (undo-tree-id5 . -9) (undo-tree-id6 . -9) (undo-tree-id7 . -9) (undo-tree-id8 . -33)) nil (24624 58013 257436 0) 0 nil])
([nil nil ((nil rear-nonsticky nil 1477 . 1478) (nil fontified nil 1454 . 1478) (nil fontified nil 1445 . 1454) (1445 . 1478)) nil (24624 58013 257407 0) 0 nil])
([nil nil ((#("-test %/tests/lib/btc-wallet ~" 0 30 (fontified t markdown-gfm-code (1371 1479) face (markdown-pre-face markdown-code-face) font-lock-multiline t)) . 1414) (undo-tree-id9 . -30) (undo-tree-id10 . -30) (undo-tree-id11 . -30) (undo-tree-id12 . -30) (undo-tree-id13 . -30) (undo-tree-id14 . -30) (undo-tree-id15 . -30) (undo-tree-id16 . -30) (undo-tree-id17 . -30) (undo-tree-id18 . -30) (t 24624 58013 261118 727000)) nil (24624 58112 573210 0) 0 nil])
([nil current ((nil rear-nonsticky nil 1443 . 1444) (nil fontified nil 1414 . 1444) (1414 . 1444)) nil (24624 58112 573188 0) 0 nil])
nil

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,11 @@
(undo-tree-save-format-version . 1)
"419370391a07dbe65ea01a443bda56e6e6cc54c3"
[nil nil nil nil (24624 57978 396382 0) 0 nil]
([nil nil ((4106 . 4115) (t 24623 50906 990784 546000)) nil (24624 57978 396381 0) 0 nil])
([nil nil ((#("b" 0 1 (fontified t ws-butler-chg delete)) . 4115) (#("x" 0 1 (fontified t ws-butler-chg delete)) . 4115) (#("e" 0 1 (fontified t ws-butler-chg delete)) . 4115) (#("h" 0 1 (fontified t ws-butler-chg delete)) . 4115) (#("-" 0 1 (fontified t ws-butler-chg delete)) . 4115) (#("o" 0 1 (fontified t ws-butler-chg delete)) . 4115) (#("t" 0 1 (fontified t)) . 4115)) nil (24624 57978 396379 0) 0 nil])
([nil nil ((4115 . 4116)) nil (24624 57978 396378 0) 0 nil])
([nil nil ((#("H" 0 1 (fontified t ws-butler-chg chg)) . -4115) (undo-tree-id32 . -1) (undo-tree-id33 . -1) 4116) nil (24624 57978 396376 0) 0 nil])
([nil nil ((4115 . 4119)) nil (24624 57978 396373 0) 0 nil])
([nil nil ((#("e" 0 1 (fontified t ws-butler-chg chg)) . -4117) (undo-tree-id0 . -1) (undo-tree-id1 . -1) (undo-tree-id2 . -1) (undo-tree-id3 . -1) (undo-tree-id4 . -1) (undo-tree-id5 . -1) (undo-tree-id6 . -1) (undo-tree-id7 . -1) (undo-tree-id8 . -1) (undo-tree-id9 . -1) (undo-tree-id10 . -1) (undo-tree-id11 . -1) (undo-tree-id12 . -1) (undo-tree-id13 . -1) (undo-tree-id14 . -1) (undo-tree-id15 . -1) (undo-tree-id16 . -1) (undo-tree-id17 . -1) (undo-tree-id18 . -1) (undo-tree-id19 . -1) (undo-tree-id20 . -1) (undo-tree-id21 . -1) (undo-tree-id22 . -1) (undo-tree-id23 . -1) (#("x" 0 1 (fontified t ws-butler-chg chg)) . -4118) (undo-tree-id24 . -1) (undo-tree-id25 . -1) (undo-tree-id26 . -1) (undo-tree-id27 . -1) (undo-tree-id28 . -1) (undo-tree-id29 . -1) (undo-tree-id30 . -1) (undo-tree-id31 . -1) 4119) nil (24624 57978 396370 0) 0 nil])
([nil current ((4117 . 4119)) nil (24624 57978 396316 0) 0 nil])
nil

View File

@ -184,7 +184,7 @@
^- byts
%+ take:byt:bcu 16
%- flip:byt:bcu
(to-hexb:bcu (crip blockhash))
(from-cord:hxb: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)

View File

@ -7,7 +7,7 @@
=, sur
|%
::
:: TODO: move this to zuse
:: TODO: move this bit/byt stuff to zuse
:: bit/byte utilities
::
::
@ -85,20 +85,31 @@
bs (drop:bit bitwidth bs)
==
--
++ to-hexb
|= h=@t
^- hexb
?: =('' h) 1^0x0
:: Add leading 00
::
:: hxb: hex parsing utilities
::
++ hxb
|%
++ from-cord
|= h=@t
^- hexb
?: =('' h) 1^0x0
:: Add leading 00
::
=+ (lsh [3 2] h)
:: Group by 4-size block
::
=+ (rsh [3 2] -)
:: Parse hex to atom
::
:- (div (lent (trip h)) 2)
`@ux`(rash - hex)
::
=+ (lsh [3 2] h)
:: Group by 4-size block
::
=+ (rsh [3 2] -)
:: Parse hex to atom
::
:- (div (lent (trip h)) 2)
`@ux`(rash - hex)
++ to-cord
|= =hexb
^- cord
(en:base16:mimes:html hexb)
--
::
++ overhead-weight ^-(vbytes 11)
++ input-weight
@ -130,15 +141,118 @@
?: =("zpub" prefix) [%84 %main]
~|("invalid xpub: {<xpub>}" !!)
::
++ address-bipt
|= a=address
^- bipt
=/ spk=hexb (script-pubkey a)
?: =(25 wid.spk) %44
?: =(23 wid.spk) %49
?: =(22 wid.spk) %84
?: =(34 wid.spk) %84
~|("Invalid address" !!)
:: adr: address manipulation
::
++ adr
|%
++ get-bipt
|= a=address
^- bipt
=/ spk=hexb (to-script-pubkey:adr a)
?: =(25 wid.spk) %44
?: =(23 wid.spk) %49
?: =(22 wid.spk) %84
?: =(34 wid.spk) %84
~|("Invalid address" !!)
::
++ to-cord
|= a=address ^- cord
?: ?=([%base58 *] a)
(scot %uc +.a)
+.a
::
++ from-pubkey
|= [=bipt =network pubkey=hexb]
^- address
?- bipt
%44
:- %base58
=< ^-(@uc dat)
%- cat:byt
:- ?- network
%main 1^0x0
%testnet 1^0x6f
==
~[(hash-160 pubkey)]
::
%49
:- %base58
=< ^-(@uc dat)
%- cat:byt
:~ ?- network
%main 1^0x5
%testnet 1^0xc4
==
%- hash-160
(cat:byt ~[2^0x14 (hash-160 pubkey)])
==
::
%84
:- %bech32
(need (encode-pubkey:bech32 network pubkey))
==
::
++ from-cord
|= addrc=@t
|^
=/ addrt=tape (trip addrc)
^- address
?: (is-base58 addrt)
[%base58 `@uc`(scan addrt fim:ag)]
?: (is-bech32 addrt)
[%bech32 addrc]
~|("Invalid address: {<addrc>}" !!)
::
++ is-base58
|= at=tape
^- ?
?| =("m" (scag 1 at))
=("1" (scag 1 at))
=("3" (scag 1 at))
=("2" (scag 1 at))
==
::
++ is-bech32
|= at=tape
^- ?
?| =("bc1" (scag 3 at))
=("tb1" (scag 3 at))
==
--
::
++ to-script-pubkey
|= =address
^- hexb
?- -.address
%bech32
=+ h=(from-address:bech32 +.address)
%- cat:byt
:~ 1^0x0
1^wid.h
h
==
::
%base58
=/ h=hexb [21 `@ux`+.address]
=+ lead-byt=dat:(take:byt 1 h)
=/ version-network=[bipt network]
?: =(0x0 lead-byt) [%44 %main]
?: =(0x6f lead-byt) [%44 %testnet]
?: =(0x5 lead-byt) [%49 %main]
?: =(0xc4 lead-byt) [%49 %testnet]
~|("Invalid base58 address: {<+.address>}" !!)
%- cat:byt
?: ?=(%44 -.version-network)
:~ 3^0x76.a914
(drop:byt 1 h)
2^0x88ac
==
:~ 2^0xa914
(drop:byt 1 h)
1^0x87
==
==
--
:: big endian sha256: input and output are both MSB first (big endian)
::
++ sha256
@ -158,7 +272,9 @@
:- 20
%- ripemd-160
(sha256 val)
:: +csiz: handle compact-size integers
::
:: +csiz: CompactSize integers (a Bitcoin-specific datatype)
:: https://btcinformation.org/en/developer-reference#compactsize-unsigned-integers
:: - encode: big endian to little endian
:: - decode: little endian to big endian
::
@ -198,69 +314,6 @@
[dat.n rest]
--
::
++ pubkey-to-address
|= [=bipt =network pubkey=hexb]
^- address
?- bipt
%44
:- %base58
=< ^-(@uc dat)
%- cat:byt
:- ?- network
%main 1^0x0
%testnet 1^0x6f
==
~[(hash-160 pubkey)]
::
%49
:- %base58
=< ^-(@uc dat)
%- cat:byt
:~ ?- network
%main 1^0x5
%testnet 1^0xc4
==
%- hash-160
(cat:byt ~[2^0x14 (hash-160 pubkey)])
==
::
%84
:- %bech32
(need (encode-pubkey:bech32 network pubkey))
==
::
++ script-pubkey
|= =address
^- hexb
?- -.address
%bech32
=+ h=(from-address:bech32 +.address)
%- cat:byt
:~ 1^0x0
1^wid.h
h
==
::
%base58
=/ h=hexb [21 `@ux`+.address]
=+ lead-byt=dat:(take:byt 1 h)
=/ version-network=[bipt network]
?: =(0x0 lead-byt) [%44 %main]
?: =(0x6f lead-byt) [%44 %testnet]
?: =(0x5 lead-byt) [%49 %main]
?: =(0xc4 lead-byt) [%49 %testnet]
~|("Invalid base58 address: {<+.address>}" !!)
%- cat:byt
?: ?=(%44 -.version-network)
:~ 3^0x76.a914
(drop:byt 1 h)
2^0x88ac
==
:~ 2^0xa914
(drop:byt 1 h)
1^0x87
==
==
:: +txu: transaction utility core
:: - primarily used for calculating txids
:: - ignores signatures in inputs

View File

@ -4,24 +4,6 @@
::=< [sur .]
::=, sur
|%
++ address-to-cord
|= =address:bc ^- cord
?: ?=([%base58 *] address)
(scot %uc +.address)
+.address
::
++ address-from-cord
|= addrc=@t
^- address:bc
?. ?| =("bc1" (scag 3 (trip addrc)))
=("tb1" (scag 3 (trip addrc)))
==
~|("base58 addresses not yet supported" !!)
[%bech32 addrc]
::
++ hexb-to-cord
|= =hexb:bc ^- cord
(en:base16:mimes:html hexb)
:: +from-epoch: time since Jan 1, 1970 in seconds.
::
++ from-epoch
@ -82,7 +64,7 @@
==
++ address-info
%- ot
:~ [%address (cu address-from-cord so)]
:~ [%address (cu from-cord:adr:bcu so)]
[%utxos (as utxo)]
[%used bo]
[%block ni]
@ -90,7 +72,7 @@
++ utxo
%- ot
:~ ['tx_pos' ni]
['tx_hash' (cu to-hexb:bcu so)]
['tx_hash' (cu from-cord:hxb:bcu so)]
[%height ni]
[%value ni]
[%recvd (cu from-epoch ni)]
@ -98,7 +80,7 @@
++ tx-vals
%- ot
:~ [%included bo]
[%txid (cu to-hexb:bcu so)]
[%txid (cu from-cord:hxb:bcu so)]
[%confs ni]
[%recvd (cu from-epoch ni)]
[%inputs (ar tx-val)]
@ -106,19 +88,19 @@
==
++ tx-val
%- ot
:~ [%txid (cu to-hexb:bcu so)]
:~ [%txid (cu from-cord:hxb:bcu so)]
[%pos ni]
[%address (cu address-from-cord so)]
[%address (cu from-cord:adr:bcu so)]
[%value ni]
==
++ raw-tx
%- ot
:~ [%txid (cu to-hexb:bcu so)]
[%rawtx (cu to-hexb:bcu so)]
:~ [%txid (cu from-cord:hxb:bcu so)]
[%rawtx (cu from-cord:hxb:bcu so)]
==
++ broadcast-tx
%- ot
:~ [%txid (cu to-hexb:bcu so)]
:~ [%txid (cu from-cord:hxb:bcu so)]
[%broadcast bo]
[%included bo]
==
@ -126,8 +108,8 @@
%- ot
:~ [%block ni]
[%fee (mu ni)]
[%blockhash (cu to-hexb:bcu so)]
[%blockfilter (cu to-hexb:bcu so)]
[%blockhash (cu from-cord:hxb:bcu so)]
[%blockfilter (cu from-cord:hxb:bcu so)]
==
--
--
@ -139,22 +121,22 @@
%get-address-info
%- get-request
%+ mk-url '/addresses/info/'
(address-to-cord address.ract)
(to-cord:adr:bcu address.ract)
::
%get-tx-vals
%- get-request
%+ mk-url '/gettxvals/'
(hexb-to-cord txid.ract)
(to-cord:hxb:bcu txid.ract)
::
%get-raw-tx
%- get-request
%+ mk-url '/getrawtx/'
(hexb-to-cord txid.ract)
(to-cord:hxb:bcu txid.ract)
::
%broadcast-tx
%- get-request
%+ mk-url '/broadcasttx/'
(hexb-to-cord rawtx.ract)
(to-cord:hxb:bcu rawtx.ract)
::
%get-block-count
%- get-request

View File

@ -1,7 +1,7 @@
::
::
/- *btc-wallet
/+ bip32, bc=bitcoin, bp=btc-provider
/- *btc-wallet, bc=bitcoin
/+ bip32, bcu=bitcoin-utils, bp=btc-provider
=, secp:crypto
=+ ecc=secp256k1
|%
@ -31,7 +31,7 @@
confs=(unit @ud)
==
^- walt
=/ [=bipt =network] (xpub-type:bc xpub)
=/ [=bipt =network] (xpub-type:bcu xpub)
:* xpub
network
fprint
@ -99,13 +99,13 @@
::
++ vbytes
^- vbytes:bc
%+ add overhead-weight:bc
%+ add overhead-weight:bcu
%+ add
%+ roll
(turn txis.t |=(t=txi (input-weight:bc bipt.hdkey.t)))
(turn txis.t |=(t=txi (input-weight:bcu bipt.hdkey.t)))
add
%+ roll
(turn txos.t |=(t=txo (output-weight:bc (address-bipt:bc address.t))))
(turn txos.t |=(t=txo (output-weight:bcu (get-bipt:adr:bcu address.t))))
add
++ tx-data
|^
@ -122,16 +122,16 @@
==
++ txo-data
|= =txo
:- (script-pubkey:bc address.txo)
:- (to-script-pubkey:adr:bcu address.txo)
value.txo
--
::
++ get-txid
^- txid
(get-id:txu:bc tx-data)
(get-id:txu:bcu tx-data)
::
++ get-rawtx
(basic-encode:txu:bc tx-data)
(basic-encode:txu:bcu tx-data)
:: +add-output: append output (usually change) to txos
::
++ add-output
@ -154,7 +154,7 @@
=/ outs=(list out:psbt:bc)
%+ turn txos.t
|=(=txo [address.txo hk.txo])
`(encode:pbt:bc %.y get-rawtx get-txid ins outs)
`(encode:pbt:bcu %.y get-rawtx get-txid ins outs)
--
:: wad: door for processing walts (wallets)
:: parameterized on a walt and it's chyg account
@ -177,7 +177,7 @@
++ mk-address
|= =idx:bc
^- address:bc
(pubkey-to-address:bc bipt.w network.w (pubkey idx))
(from-pubkey:adr:bcu bipt.w network.w (pubkey idx))
:: +nixt-address: used to get change addresses
:: - gets the current next available address
:: - doesn't bump nixt-address if it's unused
@ -258,7 +258,7 @@
++ dust-threshold
|= output-bipt=bipt:bc
^- vbytes
(mul dust-sats (input-weight:bc output-bipt))
(mul dust-sats (input-weight:bcu output-bipt))
::
++ target-value
^- sats
@ -267,24 +267,24 @@
::
++ base-weight
^- vbytes
%+ add overhead-weight:bc
%+ add overhead-weight:bcu
%+ roll
%+ turn txos
|=(=txo (output-weight:bc (address-bipt:bc address.txo)))
|=(=txo (output-weight:bcu (get-bipt:adr:bcu address.txo)))
add
::
++ total-vbytes
|= selected=(list insel)
^- vbytes
%+ add base-weight
(mul (input-weight:bc bipt.w) (lent selected))
(mul (input-weight:bcu bipt.w) (lent selected))
:: value of an input after fee
:: 0 if net is <= 0
::
++ net-value
|= val=sats
^- sats
=/ cost (mul (input-weight:bc bipt.w) feyb)
=/ cost (mul (input-weight:bcu bipt.w) feyb)
?: (lte val cost) 0
(sub val cost)
::
@ -303,7 +303,7 @@
?~ tb [~ ~]
=+ excess=~(fee txb u.tb) :: (inputs - outputs)
=/ new-fee=sats :: cost of this tx + one more output
(mul feyb (add (output-weight:bc bipt.w) vbytes.u.tb))
(mul feyb (add (output-weight:bcu bipt.w) vbytes.u.tb))
?. (gth excess new-fee)
[tb ~]
?. (gth (sub excess new-fee) (dust-threshold bipt.w))
@ -317,7 +317,7 @@
?. %+ levy txos
|= =txo
%+ gth value.txo
(dust-threshold (address-bipt:bc address.txo))
(dust-threshold (get-bipt:adr:bcu address.txo))
~|("One or more suggested outputs is dust." !!)
=/ is=(unit (list insel))
%- single-random-draw

45
ted/.rpc.hoon.~undo-tree~ Normal file

File diff suppressed because one or more lines are too long

13
ted/rpc.hoon Normal file
View File

@ -0,0 +1,13 @@
/- spider, rpc=json-rpc
/+ strandio, bcu=bitcoin-utils
=, strand=strand:spider
=>
|%
++ blah 2
--
^- thread:spider
|= arg=vase
:: =+ !<([~ a=@ud] arg)
=/ m (strand ,vase)
^- form:m
(pure:m !>(blah))

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,5 @@
(undo-tree-save-format-version . 1)
"406335d79bb3ba0775b5d2691dbd9843a80bb24b"
[nil nil nil nil (24624 58001 392493 0) 0 nil]
([nil current ((31 . 37) (t 24622 23886 325653 353000)) nil (24624 58001 392488 0) 0 nil])
nil

View File

@ -1,4 +1,4 @@
/+ *test, *bip-b158, *bitcoin
/+ *test, *bip-b158, *bitcoin-utils
|%
+$ filter-vector
$: filter=hexb

View File

@ -209,7 +209,7 @@
|= v=tx-vector
%+ expect-eq
!>(txid.v)
!>((get-id:txu (decode:txu (to-hexb hex-cord.v))))
!>((get-id:txu (decode:txu (from-cord:hxb hex-cord.v))))
::
++ check-psbt
|= v=psbt-vector
@ -237,13 +237,13 @@
=/ [b=bipt n=network] (xpub-type xpub.v)
%+ expect-eq
!>(address.v)
!>((pubkey-to-address b n pubkey.v))
!>((from-pubkey:adr b n pubkey.v))
::
++ check-script-pubkey-derivation
|= v=script-pubkey-vector
%+ expect-eq
!>(spk.v)
!>((script-pubkey address.v))
!>((to-script-pubkey:adr address.v))
--
::
--