shrub/app/btc-wallet-store.hoon

452 lines
12 KiB
Plaintext
Raw Normal View History

2020-10-24 19:13:36 +03:00
:: btc-wallet-store.hoon
2020-10-19 12:04:51 +03:00
:: Manages wallet pubkeys
::
2020-11-04 16:55:47 +03:00
:: Subscribes to: none
::
:: Sends updates on:
:: - /requests: to request data about addresses
:: - /updates: new data about one of our addresses
2020-10-28 16:20:24 +03:00
::
2020-11-17 18:34:48 +03:00
:: Scrys
:: x/scanned: (list xpub) of all scanned wallets
:: x/balance/xpub: balance (in sats) of wallet
::
2020-10-29 15:10:54 +03:00
/- *btc-wallet-store
2020-10-30 14:45:38 +03:00
/+ dbug, default-agent, *btc-wallet-store, btc, bip32
2020-10-19 12:04:51 +03:00
|%
2021-01-10 12:16:05 +03:00
++ req-pax /requests
2020-10-19 12:04:51 +03:00
+$ versioned-state
$% state-0
==
2020-10-31 18:03:00 +03:00
:: walts: all wallets, keyed by their xpubs
:: scans: batch info for wallets being scanned
2020-11-28 10:25:32 +03:00
:: gena: generated addresses that haven't had activity yet
:: batch-size: how many addresses to send out at once for checking
2020-11-13 15:47:11 +03:00
:: last-block: most recent block seen by the store
2020-10-19 12:04:51 +03:00
::
2020-10-30 12:06:42 +03:00
+$ state-0
$: %0
2020-11-11 16:54:47 +03:00
walts=(map xpub:btc walt)
2020-10-31 20:26:58 +03:00
=scans
2020-11-13 15:47:11 +03:00
batch-size=@ud
last-block=@ud
2020-12-07 16:27:26 +03:00
=history
2020-10-30 12:06:42 +03:00
==
2020-10-19 12:04:51 +03:00
::
2020-10-24 19:13:36 +03:00
+$ card card:agent:gall
2020-10-19 12:04:51 +03:00
::
--
=| state-0
=* state -
%- agent:dbug
^- agent:gall
2020-10-29 15:10:54 +03:00
=<
2020-10-19 12:04:51 +03:00
|_ =bowl:gall
+* this .
def ~(. (default-agent this %|) bowl)
2020-10-29 15:10:54 +03:00
hc ~(. +> bowl)
2020-10-19 12:04:51 +03:00
::
++ on-init
^- (quip card _this)
2020-10-24 19:13:36 +03:00
~& > '%btc-wallet-store initialized'
2020-12-07 23:31:09 +03:00
`this(state [%0 *(map xpub:btc walt) *^scans max-gap:defaults 0 *^history])
2020-10-19 12:04:51 +03:00
++ on-save
^- vase
!>(state)
++ on-load
|= old-state=vase
^- (quip card _this)
2020-10-24 19:13:36 +03:00
~& > '%btc-wallet-store recompiled'
2020-10-19 12:04:51 +03:00
`this(state !<(versioned-state old-state))
++ on-poke
|= [=mark =vase]
^- (quip card _this)
2020-10-29 15:10:54 +03:00
?> (team:title our.bowl src.bowl)
=^ cards state
?+ mark (on-poke:def mark vase)
%btc-wallet-store-action
(handle-action:hc !<(action vase))
==
[cards this]
2020-10-19 12:04:51 +03:00
::
2020-10-29 21:07:43 +03:00
++ on-watch
|= pax=path
^- (quip card _this)
?> (team:title our.bowl src.bowl)
2020-11-04 16:55:47 +03:00
?+ pax (on-watch:def pax)
[%requests *]
2020-11-11 12:41:13 +03:00
:_ this
%- zing
%~ val by
%- ~(urn by scans)
|* [k=[=xpub:btc =chyg] b=batch]
^- (list card)
(req-scan ~ b xpub.k chyg.k)
::
2020-11-04 16:55:47 +03:00
[%updates *]
`this
==
2020-11-11 12:41:13 +03:00
++ on-peek
|= pax=path
^- (unit (unit cage))
?+ pax (on-peek:def pax)
[%x %scanned ~]
``noun+!>(scanned-wallets)
2020-11-17 18:34:48 +03:00
::
[%x %balance @ ~]
``noun+!>((balance:hc (xpub:btc +>-.pax)))
2020-11-11 12:41:13 +03:00
==
2020-10-19 12:04:51 +03:00
++ on-leave on-leave:def
++ on-agent on-agent:def
++ on-arvo on-arvo:def
++ on-fail on-fail:def
--
2020-10-29 15:10:54 +03:00
::
|_ =bowl:gall
++ handle-action
|= act=action
^- (quip card _state)
?- -.act
%add-wallet
2020-11-11 16:54:47 +03:00
=/ w=walt (from-xpub +.act)
2020-11-04 18:24:00 +03:00
=. walts (~(put by walts) xpub.act w)
2020-11-11 16:54:47 +03:00
(init-batches xpub.act (dec max-gap.w))
2020-12-07 16:27:26 +03:00
:: TODO
:: if blank address we're watching gets a value
2020-12-17 18:28:42 +03:00
:: "blank" = unused
2021-01-10 12:16:05 +03:00
:: send a %tx-info request--txinfo handles history stuff
2020-12-23 14:21:48 +03:00
::
%address-info
(update-address +.act)
2020-12-07 16:27:26 +03:00
::
%tx-info
2021-01-10 12:16:05 +03:00
(handle-tx-info +.act)
2020-11-11 11:17:54 +03:00
::
%generate-address
2020-12-04 13:29:12 +03:00
(generate-address +.act)
2020-12-08 11:33:24 +03:00
:: %generate-txbu
:: - get txbu and change amount
:: - if txbu is blank, fail
:: - if change is blank, send txbu as update
:: - if change:
:: - generate new change address
:: - add that address+change value to the txbu
:: - send txbu update
:: - send a request for info on the address (watch it)
:: - DON'T send an address update for the address, since it's change
2020-11-19 14:44:12 +03:00
::
%generate-txbu
2020-12-08 11:33:24 +03:00
=+ uw=(~(get by walts) xpub.act)
?~ uw
~|("btc-wallet-store: non-existent xpub" !!)
?. scanned.u.uw
~|("btc-wallet-store: wallet not scanned yet" !!)
=/ [tb=(unit txbu) chng=(unit sats)]
%~ with-change sut
[u.uw eny.bowl last-block payee.act feyb.act txos.act]
?~ tb ~&(>>> "btc-wallet-store: insufficient balance" `state)
:: if no change, just return txbu
::
?~ chng
[~[(send-update [%generate-txbu xpub.act u.tb])] state]
=/ [addr=address:btc =idx w=walt]
~(nixt-address wad u.uw %1)
2020-12-10 16:22:48 +03:00
=/ new-txbu=txbu
(~(add-output txb u.tb) addr u.chng `(~(hdkey wad w %1) idx))
2020-12-08 11:33:24 +03:00
:_ state(walts (~(put by walts) xpub.act w))
:~ (send-update [%generate-txbu xpub.act new-txbu])
2021-01-10 12:16:05 +03:00
%+ send-req ~[req-pax]
2020-12-08 11:33:24 +03:00
:* %address-info last-block
addr xpub.act %1 idx
==
==
2020-12-07 17:31:48 +03:00
::
%add-history-entry
2021-01-10 12:16:05 +03:00
:_ state(history (~(put by history) txid.hest.act hest.act))
~[(send-req ~[req-pax] [%tx-info last-block txid.hest.act])]
2020-12-23 12:26:42 +03:00
::
%del-history-entry
2021-01-10 12:16:05 +03:00
:_ state(history (~(del by history) txid.act))
~[(send-req ~[req-pax] [%tx-info last-block txid.act])]
==
2020-11-11 11:17:54 +03:00
:: wallet scan algorithm:
2020-11-04 16:55:47 +03:00
:: Initiate a batch for each chyg, with max-gap idxs in it
:: Send that to /requests subscribers to call out to providers and get the info
:: Whenever a %watch-address result comes back
:: - remove that idx from todo.batch
:: - do run-scan to check whether that chyg is done
:: - if it isn't, refill it with idxs to scan
::
2020-11-02 20:51:19 +03:00
++ req-scan
2020-11-11 12:41:13 +03:00
|= [pax=(list path) b=batch =xpub =chyg]
2020-11-02 20:51:19 +03:00
^- (list card)
2020-11-11 16:54:47 +03:00
=/ w=walt (~(got by walts) xpub)
2020-11-02 20:51:19 +03:00
%+ turn ~(tap in todo.b)
|= =idx
2020-12-04 21:54:59 +03:00
=/ req=request
:* %address-info last-block=1
(~(mk-address wad w chyg) idx)
xpub chyg idx
==
2021-01-10 12:16:05 +03:00
(send-req pax req)
2020-11-02 20:51:19 +03:00
::
++ scan-status
|= [=xpub =chyg]
^- [empty=? done=?]
=/ b=batch (~(got by scans) [xpub chyg])
2020-11-03 14:50:10 +03:00
=/ empty=? =(0 ~(wyt in todo.b))
:- empty
?&(empty ?!(has-used.b))
2020-11-02 20:51:19 +03:00
::
++ insert-batches
|= [=xpub b0=batch b1=batch]
2020-11-02 16:12:44 +03:00
^- ^scans
2020-11-02 20:51:19 +03:00
=. scans (~(put by scans) [xpub %0] b0)
(~(put by scans) [xpub %1] b1)
::
++ init-batches
2020-11-03 14:50:10 +03:00
|= [=xpub endpoint=idx]
2020-11-02 20:51:19 +03:00
^- (quip card _state)
=/ b=batch
2020-11-03 14:50:10 +03:00
[(sy (gulf 0 endpoint)) endpoint %.n]
2020-12-06 20:53:02 +03:00
:- %+ weld
2021-01-10 12:16:05 +03:00
(req-scan ~[req-pax] b xpub %0)
(req-scan ~[req-pax] b xpub %1)
2020-11-02 20:51:19 +03:00
state(scans (insert-batches xpub b b))
2020-11-17 18:34:48 +03:00
:: if the batch is done but the wallet isn't done scanning,
:: returns new address requests and updated batch
2020-11-02 20:51:19 +03:00
::
++ bump-batch
|= [=xpub =chyg]
^- (quip card batch)
=/ b=batch (~(got by scans) xpub chyg)
=/ s (scan-status xpub chyg)
?. ?&(empty.s ?!(done.s))
`b
2020-11-11 16:54:47 +03:00
=/ w=walt (~(got by walts) xpub)
2020-11-02 20:51:19 +03:00
=/ newb=batch
2020-11-11 16:54:47 +03:00
:* (sy (gulf +(endpoint.b) (add endpoint.b max-gap.w)))
(add endpoint.b max-gap.w)
2020-11-03 14:50:10 +03:00
%.n
2020-11-02 20:51:19 +03:00
==
2021-01-10 12:16:05 +03:00
:- (req-scan ~[req-pax] newb xpub chyg)
2020-11-02 20:51:19 +03:00
newb
2020-11-03 14:50:10 +03:00
::
++ iter-scan
|= [b=batch =xpub =chyg to-delete=idx]
^- ^scans
%+ ~(put by scans) [xpub chyg]
b(todo (~(del in todo.b) to-delete))
2020-11-02 20:51:19 +03:00
:: delete the xpub from scans and set wallet to scanned
::
2020-11-02 16:12:44 +03:00
++ end-scan
|= [=xpub]
2020-11-13 15:47:11 +03:00
^- (quip card _state)
2020-11-11 16:54:47 +03:00
=/ w=walt (~(got by walts) xpub)
2020-11-02 16:12:44 +03:00
=. scans (~(del by scans) [xpub %0])
=. scans (~(del by scans) [xpub %1])
2020-11-13 15:47:11 +03:00
:- ~[[%give %fact ~[/updates] %btc-wallet-store-update !>([%scan-done xpub])]]
2020-11-11 16:54:47 +03:00
state(walts (~(put by walts) xpub w(scanned %.y)))
2020-11-02 20:51:19 +03:00
:: initiate a scan if one hasn't started
:: check status of scan if one is running
2020-11-02 16:12:44 +03:00
::
++ run-scan
|= =xpub
^- (quip card _state)
2020-11-02 20:51:19 +03:00
=/ s0 (scan-status xpub %0)
=/ s1 (scan-status xpub %1)
?: ?&(empty.s0 done.s0 empty.s1 done.s1)
2020-11-13 15:47:11 +03:00
(end-scan xpub)
2020-11-02 20:51:19 +03:00
=/ [cards0=(list card) batch0=batch]
(bump-batch xpub %0)
=/ [cards1=(list card) batch1=batch]
(bump-batch xpub %1)
:- (weld cards0 cards1)
state(scans (insert-batches xpub batch0 batch1))
:: +update-address: watch the address passed;
:: - update wallet with the address
2020-12-06 14:46:32 +03:00
:: - if address is unused, send %address-info request
:: - if address doesn't have enough confs, send %address-info request
2020-12-04 13:29:12 +03:00
:: - if this idx was the last in todo.scans, do run-scan to see whether scan is done
:: - updates wallet-store state to have last-block
::
++ update-address
|= [=xpub:btc =chyg =idx utxos=(set utxo) used=? last-block=@ud]
2020-12-04 13:29:12 +03:00
|^ ^- (quip card _state)
=? state (gth last-block last-block.state)
state(last-block last-block)
=/ w=(unit walt) (~(get by walts) xpub)
?~ w `state
=. walts
%+ ~(put by walts) xpub
%+ ~(update-address wad u.w chyg)
(~(mk-address wad u.w chyg) idx)
2020-12-05 17:51:25 +03:00
[used chyg idx utxos]
:: if the wallet is being scanned, update the scan batch
2020-12-04 21:54:59 +03:00
:: if not, just get more-info for the address if still being scanned
::
2020-12-04 21:54:59 +03:00
=+ b=(~(get by scans) [xpub chyg])
?~ b [(more-info u.w) state]
2020-11-03 14:50:10 +03:00
=. scans
(iter-scan u.b(has-used ?|(used has-used.u.b)) xpub chyg idx)
2020-11-03 14:50:10 +03:00
?: empty:(scan-status xpub chyg)
2020-12-04 13:29:12 +03:00
=^ cards state (run-scan xpub)
[(weld (more-info u.w) cards) state]
[(more-info u.w) state]
::
++ more-info
|= w=walt
^- (list card)
?: (is-done w) ~
:~
2021-01-10 12:16:05 +03:00
%+ send-req ~[req-pax]
2020-12-04 13:29:12 +03:00
:* %address-info last-block
(~(mk-address wad w chyg) idx)
xpub chyg idx
==
==
::
++ is-done
|= w=walt
?& used
2020-12-07 16:27:26 +03:00
%+ levy (turn ~(tap in utxos) (cury num-confs last-block))
2020-12-04 13:29:12 +03:00
|=(nc=@ud (gte nc confs:w))
==
--
2021-01-10 12:16:05 +03:00
::
:: -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
2021-01-12 16:21:48 +03:00
:: - ("included" = in mempool or blockchain)
2021-01-10 12:16:05 +03:00
:: - 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)
2021-01-12 16:21:48 +03:00
:: when addresses in wallets, but tx not in history
::
2021-01-10 12:16:05 +03:00
?~ h
?~ rs `state
2021-01-12 16:21:48 +03:00
:- [(send-req ~[req-pax] [%tx-info block txid.ti]) cards]
state(history (~(put by history) txid.ti (mk-hest rs)))
2021-01-10 12:16:05 +03:00
?. included.ti
:_ state(history (~(del by history) txid.ti))
2021-01-12 16:21:48 +03:00
~[(send-req ~[req-pax] [%tx-info block txid.ti])]
2021-01-10 12:16:05 +03:00
=+ 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
2021-01-12 16:21:48 +03:00
[(send-req ~[req-pax] [%tx-info block txid.ti]) cards]
2021-01-10 12:16:05 +03:00
::
++ 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
2021-01-12 16:21:48 +03:00
(send-req ~[req-pax] r)
2021-01-10 12:16:05 +03:00
::
++ mk-hest
|= rs=(lest request)
^- hest
=/ as=(set address:btc)
2021-01-12 16:21:48 +03:00
%- sy
2021-01-10 12:16:05 +03:00
%+ turn rs
2021-01-12 16:21:48 +03:00
|=(r=request ?>(?=(%address-info -.r) a.r))
:* ?>(?=(%address-info -.i.rs) xpub.i.rs)
2021-01-10 12:16:05 +03:00
txid.ti
confs.ti
recvd.ti
2021-01-12 16:21:48 +03:00
(turn inputs.ti |=(v=val:tx:btc (our-ship as v)))
(turn outputs.ti |=(v=val:tx:btc (our-ship as v)))
2021-01-10 12:16:05 +03:00
==
++ our-ship
|= [as=(set address:btc) v=val:tx:btc]
^- [=val:tx s=(unit ship)]
[v ?:((~(has in as) address.v) `our.bowl ~)]
--
2020-12-04 13:29:12 +03:00
:: +generate-address: generate and return address
2021-01-10 12:16:05 +03:00
:: sends a request for info on the new address
2020-12-04 13:29:12 +03:00
::
++ generate-address
2020-12-23 14:21:48 +03:00
|= [=xpub =chyg =pmet]
2020-12-08 11:33:24 +03:00
^- (quip card _state)
2020-12-04 13:29:12 +03:00
=+ uw=(~(get by walts) xpub)
?~ uw
~|("btc-wallet-store: non-existent xpub" !!)
2020-12-04 21:54:59 +03:00
?. scanned.u.uw
~|("btc-wallet-store: wallet not scanned yet" !!)
=/ [addr=address:btc =idx w=walt]
2020-12-04 13:29:12 +03:00
~(gen-address wad u.uw chyg)
2020-12-04 21:54:59 +03:00
:_ state(walts (~(put by walts) xpub w))
2020-12-23 14:21:48 +03:00
:~ (send-update [%generate-address xpub addr pmet])
2021-01-10 12:16:05 +03:00
%+ send-req ~[req-pax]
2020-12-04 21:54:59 +03:00
:* %address-info last-block
addr xpub chyg idx
==
==
2020-11-11 12:41:13 +03:00
::
++ scanned-wallets
^- (list xpub)
%+ murn ~(tap by walts)
2020-11-11 16:54:47 +03:00
|= [=xpub:btc w=walt]
2020-11-11 12:41:13 +03:00
^- (unit xpub:btc)
2020-11-11 16:54:47 +03:00
?: scanned.w `xpub ~
2020-11-17 18:34:48 +03:00
::
++ balance
|= =xpub:btc
^- sats:btc
=/ w (~(get by walts) xpub)
?~ w ~|("btc-wallet-store: non-existent xpub" !!)
=/ values=(list sats:btc)
%+ turn ~(val by wach.u.w)
|= =addi ^- sats:btc
%+ roll
%+ turn ~(tap by utxos.addi)
|=(=utxo:btc value.utxo)
add
(roll values add)
2020-11-19 12:45:05 +03:00
::
2021-01-10 12:16:05 +03:00
++ send-req
2020-12-04 21:54:59 +03:00
|= [pax=(list path) req=request] ^- card
2021-01-10 12:16:05 +03:00
:: ~& >> "send-req: {<chyg.req>}, {<idx.req>}"
2020-12-04 21:54:59 +03:00
:* %give %fact pax
2020-12-04 13:29:12 +03:00
%btc-wallet-store-request !>(req)
==
::
2020-11-19 12:45:05 +03:00
++ send-update
|= upd=update ^- card
[%give %fact ~[/updates] %btc-wallet-store-update !>(upd)]
2020-10-29 15:10:54 +03:00
--