WIP refactor-hook-actions

This commit is contained in:
timlucmiptev 2021-01-10 10:16:05 +01:00 committed by ixv
parent 0ade7164b8
commit 3e1b73ccc5
7 changed files with 143 additions and 81 deletions

View File

@ -53,31 +53,6 @@ Mnemonic
abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about
```
### DEPRECATED manual scanning of empty wallet
SEE Address Generation instead
Uses `btc-wallet-hook`, with max-gap=3
```
:btc-provider|command [%set-credentials api-url='http://localhost:50002']
:btc-wallet-hook|action [%set-provider ~zod]
=scan-xpub 'zpub6rFR7y4Q2AijBEqTUquhVz398htDFrtymD9xYYfG1m4wAcvPhXNfE3EfH1r1ADqtfSdVCToUG868RvUUkgDKf31mGDtKsAYz2oz2AGutZYs'
=fprint [%4 0xdead.beef]
:btc-wallet-store|action [%add-wallet scan-xpub fprint ~ [~ 3] [~ 6]]
:btc-wallet-store +dbug
:: shows scans with the xpub and {0 1 2} todos
:: %0 account has no used
=btc -build-file %/lib/btc/hoon
:btc-wallet-store|action [%address-info scan-xpub %0 1 *(set utxo:btc) used=%.n 655000]
:btc-wallet-store|action [%address-info scan-xpub %0 2 *(set utxo:btc) used=%.n 655000]
:btc-wallet-store|action [%address-info scan-xpub %0 0 *(set utxo:btc) used=%.n 655000]
:: dbug should give empty for scans: [xpub %0]
:btc-wallet-store|action [%address-info scan-xpub %1 2 *(set utxo:btc) used=%.n 655000]
:btc-wallet-store|action [%address-info scan-xpub %1 0 *(set utxo:btc) used=%.n 655000]
:btc-wallet-store|action [%address-info scan-xpub %1 1 *(set utxo:btc) used=%.y 655000]
:: dbug should show re-filled scans: [xpub %1]
```
## Address Generation Integration Test
All on `~zod`:
```
@ -133,6 +108,18 @@ on `~dopzod`:
:btc-wallet-hook +dbug [%state 'poym']
```
Sign the tx, then paste:
```
=tx TXHEX
:btc-wallet-hook|action [%broadcast-tx tx]
```
### Send Transaction
Check wallet store
```
:btc-wallet-store +dbug [%state 'history']
```
## scrys
```

View File

@ -167,16 +167,18 @@
:: - send to provider
::
%broadcast-tx
?> =(src.bowl our.bowl)
?~ prov ~|("Provider not connected" !!)
=+ signed=(to-rawtx:bp txhex.act)
=/ tx-match=?
?~ poym %.n
=((get-id:txu (decode:txu signed.act)) ~(get-txid txb:bwsl u.poym))
=((get-id:txu (decode:txu signed)) ~(get-txid txb:bwsl u.poym))
:_ ?. tx-match state
?~ poym state
state(sitx.u.poym `signed.act)
state(sitx.u.poym `signed)
?. tx-match
~[(send-update [%broadcast-tx-mismatch-poym signed.act])]
?~ prov ~|("Provider not connected" !!)
~[(poke-provider host.u.prov [%broadcast-tx signed.act])]
~[(send-update [%broadcast-tx-mismatch-poym signed])]
~[(poke-provider host.u.prov [%broadcast-tx signed])]
::
%add-piym
?> =(src.bowl our.bowl)
@ -216,11 +218,13 @@
::
%fail-broadcast-tx
?> =(src.bowl our.bowl)
~& > "%fail-broadcast-tx"
:_ state(poym ~)
~[(send-update [%broadcast-tx-spent-utxos txid.act])]
::
%succeed-broadcast-tx
?> =(src.bowl our.bowl)
~& > "%succeed-broadcast-tx"
:_ %= state
poym ~
reqs (~(put by reqs) txid.act [%tx-info 0 txid.act])
@ -322,7 +326,6 @@
==
?. (lth block.btc-state block.s)
retry-pend-piym
~& > "got new block, retrying {<(lent (retry-reqs block.s))>} reqs "
(weld retry-pend-piym (retry-reqs block.s))
::
%disconnected
@ -425,20 +428,25 @@
?~ sitx.u.poym %.n
=(txid (get-id:txu (decode:txu u.sitx.u.poym)))
:: +poym-to-history:
:: - checks whether the txinfo is in poym
:: - checks whether poym has a signed tx
:: - checks whether the txid matches that signed tx
:: - if not, skip
:: - clears poym
:: - returns card that adds hest to wallet-store history
::
++ poym-to-history
|= ti=info:tx
|^ ^- (quip card _state)
:: TODO: delete prints
~& > "poym: {<poym>}"
?~ poym `state
?~ sitx.u.poym `state
?. (poym-has-txid txid.ti)
`state
=+ vout=(get-vout txos.u.poym)
?~ vout ~|("pioym-to-history: poym should always have an output" !!)
?~ vout ~|("poym-to-history: poym should always have an output" !!)
:_ state(poym ~)
~& >>> "poym: adding history"
~[(add-history-entry ti xpub.u.poym our.bowl payee.u.poym u.vout)]
::
++ get-vout
@ -457,13 +465,16 @@
:: - returns card that adds hest to wallet-store history
::
++ piym-to-history
:: TODO: delete prints
|= ti=info:tx
|^ ^- (quip card _state)
=+ pay=(~(get by pend-piym) txid.ti)
~& > "piym-to-history pay: {<pay>}"
?~ pay `state
:: if no matching output in piym, delete from pend-piym to stop DDOS of txids
::
=+ vout=(get-vout value.u.pay)
~& > "piym-to-history vout: {<vout>}"
?~ vout
`(del-pend-piym txid.ti)
:_ (del-all-piym txid.ti payer.u.pay)
@ -477,9 +488,11 @@
=| idx=@ud
=+ os=outputs.ti
|- ?~ os ~
~& >>> "vout idx: {<idx>}"
~& >>> "vout loop value: {<value.i.os>}"
?: =(value.i.os value)
`value
$(os t.os)
`idx
$(os t.os, idx +(idx))
::
::
++ del-pend-piym

View File

@ -14,7 +14,7 @@
/- *btc-wallet-store
/+ dbug, default-agent, *btc-wallet-store, btc, bip32
|%
++ requests-path /requests
++ req-pax /requests
+$ versioned-state
$% state-0
==
@ -114,34 +114,13 @@
:: TODO
:: if blank address we're watching gets a value
:: "blank" = unused
:: add it to history
:: send a %tx-info request
:: send a %tx-info request--txinfo handles history stuff
::
%address-info
(update-address +.act)
::
:: - if txid not "included" in blockchain AND was in history
:: - delete from history
:: - send info request again just in case
:: - check whether this txid is in history
:: - if yes, update its confs and received
:: - request info on the tx again if not enough confs
::
%tx-info
=* ti info.act
=+ h=(~(get by history) txid.ti)
?~ h `state
?. included.ti
:_ state(history (~(del by history) txid.ti))
~[(send-request ~[requests-path] [%tx-info block.act txid.ti])]
=+ w=(~(get by walts) xpub.u.h)
?~ w `state
=. history
%+ ~(put by history) txid.ti
u.h(confs confs.ti, recvd recvd.ti)
:_ state
?: (gte confs.ti confs.u.w) ~
~[(send-request ~[requests-path] [%tx-info block.act txid.ti])]
(handle-tx-info +.act)
::
%generate-address
(generate-address +.act)
@ -176,23 +155,19 @@
(~(add-output txb u.tb) addr u.chng `(~(hdkey wad w %1) idx))
:_ state(walts (~(put by walts) xpub.act w))
:~ (send-update [%generate-txbu xpub.act new-txbu])
%+ send-request ~[requests-path]
%+ send-req ~[req-pax]
:* %address-info last-block
addr xpub.act %1 idx
==
==
::
%add-history-entry
:: TODO
:: - add the hest
:: - send a tx-info request out
`state
:_ state(history (~(put by history) txid.hest.act hest.act))
~[(send-req ~[req-pax] [%tx-info last-block txid.hest.act])]
::
%del-history-entry
:: - delete txid from history
:: - send a tx-info request out in case it gets added
::
`state
:_ state(history (~(del by history) txid.act))
~[(send-req ~[req-pax] [%tx-info last-block txid.act])]
==
:: wallet scan algorithm:
:: Initiate a batch for each chyg, with max-gap idxs in it
@ -213,7 +188,7 @@
(~(mk-address wad w chyg) idx)
xpub chyg idx
==
(send-request pax req)
(send-req pax req)
::
++ scan-status
|= [=xpub =chyg]
@ -235,8 +210,8 @@
=/ b=batch
[(sy (gulf 0 endpoint)) endpoint %.n]
:- %+ weld
(req-scan ~[requests-path] b xpub %0)
(req-scan ~[requests-path] b xpub %1)
(req-scan ~[req-pax] b xpub %0)
(req-scan ~[req-pax] b xpub %1)
state(scans (insert-batches xpub b b))
:: if the batch is done but the wallet isn't done scanning,
:: returns new address requests and updated batch
@ -254,7 +229,7 @@
(add endpoint.b max-gap.w)
%.n
==
:- (req-scan ~[requests-path] newb xpub chyg)
:- (req-scan ~[req-pax] newb xpub chyg)
newb
::
++ iter-scan
@ -324,7 +299,7 @@
^- (list card)
?: (is-done w) ~
:~
%+ send-request ~[requests-path]
%+ send-req ~[req-pax]
:* %address-info last-block
(~(mk-address wad w chyg) idx)
xpub chyg idx
@ -338,8 +313,90 @@
|=(nc=@ud (gte nc confs:w))
==
--
::
:: -if txid not in history but has one of our wallet addresses
:: - add it to history and request info on the addresses+tx
:: - if txid not "included" in blockchain AND was in history
:: - delete from history
:: - send txinfo request again
:: - check whether this txid is in history
:: - if yes, update its confs and received
:: - request info on all its addresses
:: - request info on the tx again if not enough confs
::
++ handle-tx-info
|= [ti=info:tx:btc block=@ud]
|^
=. state state(last-block block)
=+ h=(~(get by history) txid.ti)
=/ rs=(list request) (address-reqs ti)
=/ cards=(list card) (turn rs to-card)
?~ h
?~ rs `state
:: when addresses in our wallets, but tx is not
::
:- [(send-req ~[req-pax] [%tx-info block.act txid.ti]) cards]
state(history (~(put by history) (mk-hest rs)))
?. included.ti
:_ state(history (~(del by history) txid.ti))
~[(send-req ~[req-pax] [%tx-info block.act txid.ti])]
=+ w=(~(get by walts) xpub.u.h)
?~ w `state
=. history
%+ ~(put by history) txid.ti
u.h(confs confs.ti, recvd recvd.ti)
:_ state
?: (gte confs.ti confs.u.w) cards
[(send-req ~[req-pax] [%tx-info block.act txid.ti]) cards]
::
++ address-reqs
|= ti=info:tx:btc
^- (list request)
=| rs=(list request)
=/ ws=(list walt) ~(val by walts)
|- ?~ ws rs
%= $
ws t.ws
rs
%- zing
:~ rs
(murn inputs.ti (cury to-req i.ws))
(murn outputs.ti (cury to-req i.ws))
==
==
::
++ to-req
|= [w=walt v=val:tx:btc]
^- (unit request)
=+ addi=(~(get by wach.w) address.v)
?~ addi ~
`[%address-info last-block address.v xpub.w chyg.u.addi idx.u.addi]
::
++ to-card
|= r=request ^- card
(send-request ~[req-pax] r)
::
++ mk-hest
|= rs=(lest request)
^- hest
=/ as=(set address:btc)
%- sy
%+ turn rs
|=(r=request ?>(?=(-.r %address-info) a.r))
:* ?>(?=(-.i.rs %address-info) xpub.i.rs)
txid.ti
confs.ti
recvd.ti
(turn inputs.ti (cury as our-ship))
(turn outputs.ti (cury as our-ship))
==
++ our-ship
|= [as=(set address:btc) v=val:tx:btc]
^- [=val:tx s=(unit ship)]
[v ?:((~(has in as) address.v) `our.bowl ~)]
--
:: +generate-address: generate and return address
:: sends a request for info on the new address (watches it)
:: sends a request for info on the new address
::
++ generate-address
|= [=xpub =chyg =pmet]
@ -353,7 +410,7 @@
~(gen-address wad u.uw chyg)
:_ state(walts (~(put by walts) xpub w))
:~ (send-update [%generate-address xpub addr pmet])
%+ send-request ~[requests-path]
%+ send-req ~[req-pax]
:* %address-info last-block
addr xpub chyg idx
==
@ -380,9 +437,9 @@
add
(roll values add)
::
++ send-request
++ send-req
|= [pax=(list path) req=request] ^- card
:: ~& >> "send-request: {<chyg.req>}, {<idx.req>}"
:: ~& >> "send-req: {<chyg.req>}, {<idx.req>}"
:* %give %fact pax
%btc-wallet-store-request !>(req)
==

View File

@ -32,7 +32,6 @@
^- (unit @da)
?: =(0 secs) ~
[~ (add ~1970.1.1 `@dr`(mul secs ~s1))]
::
++ to-hex
|= h=@t
@ -48,6 +47,11 @@
::
`@ux`(rash - hex)
::
++ to-rawtx
|= h=@t
^- rawtx
=+ bs=(to-hex h)
[(div (lent (trip h)) 2) bs]
++ to-hash256
|= h=@t
(hash256 [32 (to-hex h)])

View File

@ -1,8 +1,9 @@
/*
Usage:
var = require("./psbt_sign.js")
var p = require("./psbt_sign.js")
const psbt = PSBT_STRING_BASE64
const mnemonics = MNEMONICS_STRING
const [psbt, mnemonics] = []
p.run(mnemonics, psbt)
*/

View File

@ -40,7 +40,7 @@
==
+$ local
$% [%req-pay-address payee=ship value=sats feyb=(unit sats)]
[%broadcast-tx signed=rawtx]
[%broadcast-tx txhex=cord]
[%add-piym =xpub =address payer=ship value=sats]
[%add-poym =txbu:bws]
[%add-poym-txi =txid =rawtx]

View File

@ -107,6 +107,6 @@
::
+$ request
$% [%address-info last-block=@ud a=address =xpub =chyg =idx]
[%tx-info last-block=@ud =txid]
[%tx-info last-block=@ud =txidtxid]
==
--