get rid of buffers

This commit is contained in:
timlucmiptev 2021-02-08 23:21:19 +04:00 committed by ixv
parent c5690b46e1
commit 56b7f09559
6 changed files with 114 additions and 125 deletions

View File

@ -35,8 +35,8 @@ Install files:
### On Moon1: ### On Moon1:
``` ```
=moon1 ~sarsev-dapwel-timluc-miptev =moon1 ~massev-racsyt-timluc-miptev
=moon2 ~pidlun-hadwyx-timluc-miptev =moon2 ~raprup-hanpec-dinleb-rambep
|commit %home |commit %home
|start %btc-provider |start %btc-provider
@ -44,18 +44,18 @@ Install files:
|start %btc-wallet-hook |start %btc-wallet-hook
:btc-provider|command [%set-credentials api-url='http://localhost:50002' %main] :btc-provider|command [%set-credentials api-url='http://localhost:50002' %main]
:btc-provider|command [%whitelist-clients `(set ship)`(sy ~[moon2])] :btc-provider|command [%whitelist-clients `(set ship)`(sy ~[moon2])]
=xpub1 'zpub6rFR7y4Q2AijBEqTUquhVz398htDFrtymD9xYYfG1m4wAcvPhXNfE3EfH1r1ADqtfSdVCToUG868RvUUkgDKf31mGDtKsAYz2oz2AGutZYs' =xpubmain 'zpub6rFR7y4Q2AijBEqTUquhVz398htDFrtymD9xYYfG1m4wAcvPhXNfE3EfH1r1ADqtfSdVCToUG868RvUUkgDKf31mGDtKsAYz2oz2AGutZYs'
:btc-wallet-hook|action [%set-provider moon1 %main] :btc-wallet-hook|action [%set-provider moon1 %main]
=fprint [%4 0xdead.beef] =fprint [%4 0xdead.beef]
:btc-wallet-store|action [%add-wallet xpub1 fprint ~ [~ 8] [~ 6]] :btc-wallet-store|action [%add-wallet xpubmain fprint ~ [~ 8] [~ 6]]
``` ```
### On Moon2: ### On Moon2:
``` ```
:: xpub from PRIVATE.md :: xpub from PRIVATE.md
=moon1 ~sarsev-dapwel-timluc-miptev =moon1
=moon2 ~pidlun-hadwyx-timluc-miptev =moon2
|commit %home |commit %home
|start %btc-wallet-store |start %btc-wallet-store
@ -69,7 +69,7 @@ Install files:
### Request Address ### Request Address
Moon2: Moon2:
``` ```
:btc-wallet-hook|action [%req-pay-address payee=~sarsev-dapwel-timluc-miptev value=10.000 [~ 37]] :btc-wallet-hook|action [%req-pay-address payee=~massev-racsyt-timluc-miptev value=10.000 [~ 37]]
:btc-wallet-hook +dbug [%state 'poym'] :btc-wallet-hook +dbug [%state 'poym']
``` ```
@ -77,5 +77,8 @@ Moon2:
``` ```
.^((list @t) %gx /=btc-wallet-store=/scanned/noun) .^((list @t) %gx /=btc-wallet-store=/scanned/noun)
.^(@ud %gx /=btc-wallet-store=/balance/[xpub2]/noun) .^(@ud %gx /=btc-wallet-store=/balance/[xpubmain]/noun)
``` ```
1cb10bd68e827de119d89332883f8736038bd703c30e6dbecbb829d448608e94

View File

@ -16,7 +16,7 @@ bitcoin-cli -rpcuser=__cookie__ -rpcpassword=9e0ca21c70d6b7307b750c8012d12df04ef
=btc -build-file %/lib/btc/hoon =btc -build-file %/lib/btc/hoon
=psbt 'cHNidP8BAKACAAAAAqsJSaCMWvfEm4IS9Bfi8Vqz9cM9zxU4IagTn4d6W3vkAAAAAAD+////qwlJoIxa98SbghL0F+LxWrP1wz3PFTghqBOfh3pbe+QBAAAAAP7///8CYDvqCwAAAAAZdqkUdopAu9dAy+gdmI5x3ipNXHE5ax2IrI4kAAAAAAAAGXapFG9GILVT+glechue4O/p+gOcykWXiKwAAAAAAAEHakcwRAIgR1lmF5fAGwNrJZKJSGhiGDR9iYZLcZ4ff89X0eURZYcCIFMJ6r9Wqk2Ikf/REf3xM286KdqGbX+EhtdVRs7tr5MZASEDXNxh/HupccC1AaZGoqg7ECy0OIEhfKaC3Ibi1z+ogpIAAQEgAOH1BQAAAAAXqRQ1RebjO4MsRwUPJNPuuTycA5SLx4cBBBYAFIXRNTfy4mVAWjTbr6nj3aAfuCMIAAAA' =psbt 'cHNidP8BAKACAAAAAqsJSaCMWvfEm4IS9Bfi8Vqz9cM9zxU4IagTn4d6W3vkAAAAAAD+////qwlJoIxa98SbghL0F+LxWrP1wz3PFTghqBOfh3pbe+QBAAAAAP7///8CYDvqCwAAAAAZdqkUdopAu9dAy+gdmI5x3ipNXHE5ax2IrI4kAAAAAAAAGXapFG9GILVT+glechue4O/p+gOcykWXiKwAAAAAAAEHakcwRAIgR1lmF5fAGwNrJZKJSGhiGDR9iYZLcZ4ff89X0eURZYcCIFMJ6r9Wqk2Ikf/REf3xM286KdqGbX+EhtdVRs7tr5MZASEDXNxh/HupccC1AaZGoqg7ECy0OIEhfKaC3Ibi1z+ogpIAAQEgAOH1BQAAAAAXqRQ1RebjO4MsRwUPJNPuuTycA5SLx4cBBBYAFIXRNTfy4mVAWjTbr6nj3aAfuCMIAAAA'
(get-txid:^psbt:btc psbt) (get-txid:pbt:btc psbt)
:: gives [wid=%32 dat=0xfed6.cd1f.de4d.b4e1.3e7e.8003.17e3.7f9c.bd75.ec36.4389.670e.eff8.0da9.93c7.e560] :: gives [wid=%32 dat=0xfed6.cd1f.de4d.b4e1.3e7e.8003.17e3.7f9c.bd75.ec36.4389.670e.eff8.0da9.93c7.e560]
``` ```
@ -74,3 +74,6 @@ Maps:
Input: keeps above keyvals, and adds one more for the partial signature: Input: keeps above keyvals, and adds one more for the partial signature:
* (key type {0x2}|{publicKey}) * (key type {0x2}|{publicKey})
* val: 71 byte signature * val: 71 byte signature
'0200000002ab0949a08c5af7c49b8212f417e2f15ab3f5c33dcf153821a8139f877a5b7be40000000000feffffffab0949a08c5af7c49b8212f417e2f15ab3f5c33dcf153821a8139f877a5b7be40100000000feffffff02603bea0b000000001976a914768a40bbd740cbe81d988e71de2a4d5c71396b1d88ac8e240000000000001976a9146f4620b553fa095e721b9ee0efe9fa039cca459788ac00000000'

View File

@ -168,22 +168,11 @@
(to-range item f k) (to-range item f k)
lth lth
-- --
:: +get-n: get N from head of block filter, little endian
::
++ get-n
|= filter=hexb
^- hexb
=/ n=hexb (take:byt 1 filter)
%- flip:byt
?: =(dat.n 0xfd) (take:byt 2 (drop:byt 1 filter))
?: =(dat.n 0xfe) (take:byt 4 (drop:byt 1 filter))
?: =(dat.n 0xff) (take:byt 8 (drop:byt 1 filter))
n
:: ::
++ parse-filter ++ parse-filter
|= filter=hexb |= filter=hexb
^- [n=@ux gcs-set=bits] ^- [n=@ux gcs-set=bits]
=+ n=(get-n filter) =+ n=n:(compact-size 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 lead filter)]

View File

@ -79,6 +79,24 @@
:- 20 :- 20
%- ripemd-160 %- ripemd-160
(sha256 val) (sha256 val)
:: +compact-size: parse compactSize int from head of hex and return result and remainder
:: - input "h" is little endian, output "n" is big endian
::
++ compact-size
|= h=hexb
^- [n=hexb rest=hexb]
=/ s=@ux dat:(take:byt 1 h)
?: (lth s 0xfd) [1^s (drop:byt 1 h)]
~| "Invalid compact-size at start of {<h>}"
=/ len=bloq
?+ s !!
%0xfd 1
%0xfe 2
%0xff 3
==
:_ (drop:byt (add 1 len) h)
%- flip:byt
(take:byt (bex len) (drop:byt 1 h))
:: ::
++ pubkey-to-address ++ pubkey-to-address
|= [=bipt =network pubkey=hexb] |= [=bipt =network pubkey=hexb]
@ -172,70 +190,69 @@
++ de ++ de
|% |%
++ nversion ++ nversion
|= b=buffer |= b=hexb
^- [nversion=@ud rest=buffer] ^- [nversion=@ud rest=hexb]
:_ (slag 4 b) :- dat:(flip:byt (take:byt 4 b))
=< dat (drop:byt 4 b)
%- flip:byt
(to-byts:buf (scag 4 b))
:: ::
++ segwit ++ segwit
|= b=buffer |= b=hexb
^- [segwit=(unit @ud) rest=buffer] ^- [segwit=(unit @ud) rest=hexb]
?. =(0x0 (snag 0 b)) ?. =(1^0x0 (take:byt 1 b))
[~ b] [~ b]
:_ (slag 2 b) :- [~ dat:(take:byt 2 b)]
=< [~ dat] (drop:byt 2 b)
(to-byts:buf (scag 2 b))
:: returns value as 0 since we don't know it when we decode :: returns value as 0 since we don't know it when we decode
:: ::
++ input ++ input
|= b=buffer |= b=hexb
^- input:tx ^- input:tx
:* (flip:byt (to-byts:buf (scag 32 b))) :* (flip:byt (take:byt 32 b))
dat:(flip:byt (take:byt 4 (drop:byt 32 b)))
=<(dat (flip:byt (to-byts:buf (swag [32 4] b)))) (flip:byt (take:byt 4 (drop:byt 37 b)))
(flip:byt (to-byts:buf (swag [37 4] b)))
~ ~
~ ~
0 0
== ==
:: ::
++ output ++ output
|= b=buffer |= b=hexb
^- output:tx ^- output:tx
:: slag 9 instead of 8 to skip the length byte :: TODO: parse length byte as compactsize?
:- (to-byts:buf (slag 9 b)) :: drop 9 instead of 8 to skip the length byte
=< dat ::
(flip:byt (to-byts:buf (scag 8 b))) :- (drop:byt 9 b)
dat:(flip:byt (take:byt 8 b))
:: ::
++ inputs ++ inputs
|= b=buffer |= b=hexb
^- [is=(list input:tx) rest=buffer] ^- [is=(list input:tx) rest=hexb]
=| acc=(list input:tx) =| acc=(list input:tx)
=^ count b =^ count b
[(snag 0 b) (slag 1 b)] [dat:(take:byt 1 b) (drop:byt 1 b)]
|- |-
?: =(0 count) [acc b] ?: =(0 count) [acc b]
%= $ %= $
acc %+ snoc acc acc %+ snoc acc
(input (scag 41 b)) (input (take:byt 41 b))
b (slag 41 b) b (drop:byt 41 b)
count (dec count) count (dec count)
== ==
:: ::
++ outputs ++ outputs
|= b=buffer |= b=hexb
^- [os=(list output:tx) rest=buffer] ^- [os=(list output:tx) rest=hexb]
=| acc=(list output:tx) =| acc=(list output:tx)
:: TODO count should be compact size
=^ count b =^ count b
[(snag 0 b) (slag 1 b)] [dat:(take:byt 1 b) (drop:byt 1 b)]
|- |-
?: =(0 count) [acc b] ?: =(0 count) [acc b]
:: TODO: below is wrong because output is sometimes not 31 length. It's 8 + compactsize
%= $ %= $
acc %+ snoc acc acc %+ snoc acc
(output (scag 31 b)) (output (take:byt 31 b))
b (slag 31 b) b (drop:byt 31 b)
count (dec count) count (dec count)
== ==
-- --
@ -263,17 +280,16 @@
++ decode ++ decode
|= b=hexb |= b=hexb
^- data:tx ^- data:tx
=/ bu=buffer (from-byts:buf b) =^ nversion b
=^ nversion bu (nversion:de b)
(nversion:de bu) =^ segwit b
=^ segwit bu (segwit:de b)
(segwit:de bu) =^ inputs b
=^ inputs bu (inputs:de b)
(inputs:de bu) =^ outputs b
=^ outputs bu (outputs:de b)
(outputs:de bu)
=/ locktime=@ud =/ locktime=@ud
dat:(to-byts:buf (scag 4 (flop bu))) dat:(take:byt 4 (flip:byt b))
[inputs outputs locktime nversion segwit] [inputs outputs locktime nversion segwit]
-- --
:: core to handle BIP174 PSBTs :: core to handle BIP174 PSBTs
@ -393,17 +409,19 @@
++ parse ++ parse
|= psbt-base64=cord |= psbt-base64=cord
^- (list map:psbt) ^- (list map:psbt)
=/ todo=buffer =/ todo=hexb
%+ slag 5 (to-buffer psbt-base64) (drop:byt 5 (to-byts psbt-base64))
=| acc=(list map:psbt) =| acc=(list map:psbt)
=| m=map:psbt =| m=map:psbt
|- |-
?~ todo (snoc acc m) ?: =(wid.todo 0)
(snoc acc m)
:: 0x0: map separator :: 0x0: map separator
?: =(0x0 i.todo) ::
$(acc (snoc acc m), m *map:psbt, todo t.todo) ?: =(1^0x0 (take:byt 1 todo))
=+ [kv rest]=(next-keyval todo) $(acc (snoc acc m), m *map:psbt, todo (drop:byt 1 todo))
$(m (snoc m kv), todo rest) =^ kv todo (next-keyval todo)
$(m (snoc m kv))
:: +get-txid: extract txid from a valid PSBT :: +get-txid: extract txid from a valid PSBT
:: ::
++ get-txid ++ get-txid
@ -411,78 +429,47 @@
^- hexb ^- hexb
=/ tx=hexb =/ tx=hexb
%- raw-tx %- raw-tx
%+ slag 5 %+ drop:byt 5
(to-buffer psbt-base64) (to-byts psbt-base64)
%- flip:byt %- flip:byt
(dsha256 tx) (dsha256 tx)
:: +raw-tx: extract hex transaction :: +raw-tx: extract hex transaction
:: looks for key 0x0 in global map :: looks for key 0x0 in global map
:: crashes if tx not in buffer :: crashes if tx not in hex
:: ::
++ raw-tx ++ raw-tx
|= b=buffer |= b=hexb
|- ^- hexb ^- hexb
?~ b !! |-
?: =(0x0 i.b) !! ?: =(wid.b 0) !!
=+ nk=(next-keyval b) ?: =(1^0x0 (take:byt 1 b)) !!
=/ nk (next-keyval b)
?: =(0x0 dat.key.kv.nk) ?: =(0x0 dat.key.kv.nk)
val.kv.nk val.kv.nk
$(b rest.nk) $(b rest.nk)
:: +next-keyval: returns next key-val in a PSBT map :: +next-keyval: returns next key-val in a PSBT map
:: input buffer head must be a map key length :: input first byte must be a map key length
:: ::
++ next-keyval ++ next-keyval
|= b=buffer |= b=hexb
^- [kv=keyval:psbt rest=buffer] ^- [kv=keyval:psbt rest=hexb]
=+ klen=(snag 0 b) =/ klen dat:(take:byt 1 b)
=+ k=(swag [1 klen] b) =/ k (take:byt klen (drop:byt 1 b))
=+ vlen=(snag (add 1 klen) b) =/ vlen dat:(take:byt 1 (drop:byt (add 1 klen) b))
=+ v=(swag [(add 2 klen) vlen] b) =/ v (take:byt vlen (drop:byt (add 2 klen) b))
=+ len=(add 2 (add klen vlen)) ?> ?&((gth wid.k 0) (gth wid.v 0))
?> ?=([^ ^] [k v]) :- [k v]
:_ (slag len b) (drop:byt ;:(add 2 klen vlen) b)
:- (to-byts:buf k)
(to-byts:buf v)
:: ::
++ to-buffer ++ to-byts
|= psbt-base64=cord |= psbt-base64=cord
^- buffer ^- hexb
~| "Invalid PSBT" ~| "Invalid PSBT"
=+ p=(de:base64:mimes:html psbt-base64) =+ p=(de:base64:mimes:html psbt-base64)
?~ p !! ?~ p !!
(from-byts:buf (flip:byt u.p)) (flip:byt u.p)
-- --
:: buffer: byte buffer utilities
:: list of @ux that is big endian for hashing purposes
:: used to preserve 0s when concatenating byte sequences
:: ::
++ buf
|%
++ from-byts
|= =byts ^- buffer
=/ b=(list @ux)
(flop (rip 3 dat.byts))
=/ pad=@ (sub wid.byts (lent b))
(weld (reap pad 0x0) b)
:: converts byts to a little endian buffer with wid length (trailing 0s)
:: atom 1 with wid=4 becomes ~[0x1 0x0 0x0 0x0]
:: 0xff11 with wid=8 becomes ~[0x11 0xff 0x0 0x0 0x0 0x0 0x0 0x0]
::
++ from-byts-le
|= =byts
^- buffer
=/ b=(list @ux) (rip 3 dat.byts)
=/ pad=@ (sub wid.byts (lent b))
(weld b (reap pad 0x0))
::
++ to-byts
|= b=buffer ^- byts
[(lent b) (rep 3 (flop b))]
::
++ concat-as-byts
|= bs=(list buffer) ^- byts
%- to-byts (zing bs)
--
++ byt ++ byt
|% |%
:: +cat: concat byts, preserving MSB order :: +cat: concat byts, preserving MSB order

View File

@ -19,7 +19,6 @@
+$ hdkey [=fprint pubkey=hexb =network =bipt =chyg =idx] +$ hdkey [=fprint pubkey=hexb =network =bipt =chyg =idx]
+$ sats @ud +$ sats @ud
+$ vbytes @ud +$ vbytes @ud
+$ buffer (list @ux)
+$ utxo [pos=@ txid=hexb height=@ value=sats recvd=(unit @da)] +$ utxo [pos=@ txid=hexb height=@ value=sats recvd=(unit @da)]
++ address-info ++ address-info
$: =address $: =address

View File

@ -4,6 +4,7 @@
|% |%
+$ chyg ?(%0 %1) +$ chyg ?(%0 %1)
+$ bits-vector [bitwidth=@ atoms=(list @) =bits] +$ bits-vector [bitwidth=@ atoms=(list @) =bits]
+$ tx-vector [hex-cord=@t txid=hexb]
+$ psbt-vector +$ psbt-vector
$: =hdkey $: =hdkey
hdkey-hex=hexb hdkey-hex=hexb
@ -17,6 +18,7 @@
== ==
+$ script-pubkey-vector [=address spk=hexb] +$ script-pubkey-vector [=address spk=hexb]
:: ::
++ fprint 4^0xdead.beef
++ bits-vectors ++ bits-vectors
^- (list bits-vector) ^- (list bits-vector)
:~ :* 5 :~ :* 5
@ -25,7 +27,13 @@
== ==
== ==
:: ::
++ fprint 4^0xdead.beef ++ tx-vectors
^- (list tx-vector)
:~ :* '0200000002ab0949a08c5af7c49b8212f417e2f15ab3f5c33dcf153821a8139f877a5b7be40000000000feffffffab0949a08c5af7c49b8212f417e2f15ab3f5c33dcf153821a8139f877a5b7be40100000000feffffff02603bea0b000000001976a914768a40bbd740cbe81d988e71de2a4d5c71396b1d88ac8e240000000000001976a9146f4620b553fa095e721b9ee0efe9fa039cca459788ac00000000'
32^0xfed6.cd1f.de4d.b4e1.3e7e.8003.17e3.7f9c.bd75.ec36.4389.670e.eff8.0da9.93c7.e560
==
==
::
++ psbt-vectors ++ psbt-vectors
^- (list psbt-vector) ^- (list psbt-vector)
:~ :* [fprint 33^0x1 %testnet %44 %0 1] :~ :* [fprint 33^0x1 %testnet %44 %0 1]