shrub/app/btc-wallet-store.hoon

214 lines
5.5 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-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
|%
+$ 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
:: batch-size: how many addresses to send out at once for checking
2020-10-19 12:04:51 +03:00
::
2020-10-30 12:06:42 +03:00
+$ state-0
$: %0
2020-10-31 20:26:58 +03:00
walts=(map xpub:btc _walt)
=scans
batch-size=@
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'
`this(state [%0 *(map xpub:btc _walt) *^scans default-max-gap])
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 *]
`this
[%updates *]
`this
==
2020-10-19 12:04:51 +03:00
++ on-leave on-leave:def
++ on-peek on-peek: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-02 16:12:44 +03:00
=/ w=_walt (from-xpub:walt +.act)
2020-11-04 18:24:00 +03:00
=. walts (~(put by walts) xpub.act w)
2020-11-11 11:17:54 +03:00
(init-batches xpub.act (dec max-gap.st.w))
::
%watch-address
(watch-address +.act)
2020-10-29 21:07:43 +03:00
::
%update-address
2020-10-29 15:10:54 +03:00
`state
2020-11-11 11:17:54 +03:00
::
%generate-address
=/ uw=(unit _walt) (~(get by walts) xpub.act)
?~ uw
~|("btc-wallet-store, %generate-address: non-existent wallet" !!)
=/ [a=address:btc w=_walt]
~(gen-address u.uw chyg.act)
:_ state(walts (~(put by walts) xpub.act w))
~[[%give %fact ~[/updates] %btc-wallet-store-update !>([%generate-address a])]]
==
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
|= [b=batch =xpub =chyg]
^- (list card)
2020-11-04 18:24:00 +03:00
=/ w=_walt (~(got by walts) xpub)
2020-11-02 20:51:19 +03:00
%+ turn ~(tap in todo.b)
|= =idx
:* %give %fact ~[/requests]
%btc-wallet-store-request
2020-11-04 18:24:00 +03:00
!>([%scan-address (~(mk-address w chyg) idx) xpub chyg idx])
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-11-02 20:51:19 +03:00
:- (weld (req-scan b xpub %0) (req-scan 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
::
++ bump-batch
|= [=xpub =chyg]
^- (quip card batch)
=/ b=batch (~(got by scans) xpub chyg)
=/ s (scan-status xpub chyg)
?. ?&(empty.s ?!(done.s))
`b
=/ w=_walt (~(got by walts) xpub)
=/ newb=batch
:* (sy (gulf +(endpoint.b) (add endpoint.b max-gap.st.w)))
(add endpoint.b max-gap.st.w)
2020-11-03 14:50:10 +03:00
%.n
2020-11-02 20:51:19 +03:00
==
:- (req-scan newb xpub chyg)
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]
^- _state
=/ w=_walt (~(got by walts) xpub)
=. scans (~(del by scans) [xpub %0])
=. scans (~(del by scans) [xpub %1])
state(walts (~(put by walts) xpub w(scanned.st %.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-02 16:12:44 +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))
2020-11-02 16:12:44 +03:00
:: watch the address passed, update wallet if it's used
2020-11-03 14:50:10 +03:00
:: if this idx was the last in todo.scans, do run-scan to see whether scan is done
::
++ watch-address
|= [=xpub:btc =chyg =idx utxos=(set utxo) used=?]
^- (quip card _state)
?. (~(has by scans) [xpub chyg]) `state
=/ w=_walt (~(got by walts) xpub)
2020-11-02 16:12:44 +03:00
=/ b=batch (~(got by scans) [xpub chyg])
2020-11-03 14:50:10 +03:00
=? walts used
%+ ~(put by walts)
xpub
%+ ~(watch-address w chyg)
(~(mk-address w chyg) idx)
[chyg idx utxos]
2020-11-03 14:50:10 +03:00
=. scans
(iter-scan b(has-used ?|(used has-used.b)) xpub chyg idx)
?: empty:(scan-status xpub chyg)
(run-scan xpub)
`state
2020-10-29 15:10:54 +03:00
--