shrub/app/btc-wallet-hook.hoon

318 lines
8.1 KiB
Plaintext
Raw Normal View History

2020-10-24 19:13:36 +03:00
:: btc-wallet-hook.hoon
2020-10-28 16:20:24 +03:00
::
2020-11-04 16:55:47 +03:00
:: Subscribes to:
2020-10-28 16:20:24 +03:00
:: btc-provider:
2020-11-04 16:55:47 +03:00
:: - connection status
:: - RPC call results/errors
::
:: btc-wallet-store
:: - requests for address info
:: - updates to existing address info
::
:: Sends updates to:
2020-10-28 16:20:24 +03:00
:: none
2020-10-24 19:13:36 +03:00
::
2020-11-20 14:58:27 +03:00
/- *btc, *btc-wallet-hook, bws=btc-wallet-store
/+ dbug, default-agent, bwsl=btc-wallet-store, bp=btc-provider
2020-10-24 19:13:36 +03:00
|%
2020-11-20 14:58:27 +03:00
++ defaults
|%
++ moon-limit 10
--
2020-10-24 19:13:36 +03:00
+$ versioned-state
$% state-0
==
2020-11-19 17:42:53 +03:00
:: provider: maybe ship if provider is set
:: moon-limit: how many addresses a ship and its moons can request in piym
2020-11-19 18:54:01 +03:00
:: piym/poym-watch: listen to btc-wallet-store for address updates; update payment info
2020-10-24 19:13:36 +03:00
::
2020-11-04 16:55:47 +03:00
+$ state-0
$: %0
2020-11-09 16:27:04 +03:00
provider=(unit [host=ship connected=?])
2020-11-16 20:06:45 +03:00
=btc-state
2020-11-17 11:25:50 +03:00
def-wallet=(unit xpub)
2020-11-19 17:42:53 +03:00
moon-limit=@ud
2020-11-17 18:34:48 +03:00
=pend-addr
2020-11-17 13:10:54 +03:00
=piym
=poym
=piym-watch
=poym-watch
2020-11-04 16:55:47 +03:00
==
2020-10-24 19:13:36 +03:00
::
2020-11-11 11:00:32 +03:00
+$ card card:agent:gall
2020-10-24 19:13:36 +03:00
--
=| state-0
=* state -
%- agent:dbug
^- agent:gall
=<
|_ =bowl:gall
+* this .
def ~(. (default-agent this %|) bowl)
hc ~(. +> bowl)
::
++ on-init
^- (quip card _this)
~& > '%btc-wallet-hook initialized'
2020-11-20 14:58:27 +03:00
:_ this(moon-limit.state moon-limit:defaults)
2020-11-04 16:55:47 +03:00
:~ [%pass /r/[(scot %da now.bowl)] %agent [our.bowl %btc-wallet-store] %watch /requests]
[%pass /u/[(scot %da now.bowl)] %agent [our.bowl %btc-wallet-store] %watch /updates]
==
2020-10-24 19:13:36 +03:00
++ on-save
^- vase
!>(state)
++ on-load
|= old-state=vase
^- (quip card _this)
~& > '%btc-wallet-hook recompiled'
`this(state !<(versioned-state old-state))
++ on-poke
|= [=mark =vase]
^- (quip card _this)
=^ cards state
?+ mark (on-poke:def mark vase)
%btc-wallet-hook-action
(handle-action:hc !<(action vase))
==
[cards this]
::
++ on-watch on-watch:def
++ on-leave on-leave:def
++ on-peek on-peek:def
++ on-agent
|= [=wire =sign:agent:gall]
2020-11-09 15:43:30 +03:00
^- (quip card _this)
2020-10-24 19:13:36 +03:00
?+ -.sign (on-agent:def wire sign)
%kick
?~ provider `this
?: ?& ?=(%set-provider -.wire)
=(host.u.provider src.bowl)
==
`this(provider ~)
`this
::
2020-11-04 16:55:47 +03:00
%fact
=^ cards state
?+ p.cage.sign `state
%btc-provider-status
(handle-provider-status:hc !<(status:bp q.cage.sign))
::
2020-11-09 16:27:04 +03:00
%btc-provider-update
(handle-provider-update:hc !<(update:bp q.cage.sign))
::
2020-11-04 16:55:47 +03:00
%btc-wallet-store-request
2020-11-17 12:34:04 +03:00
(handle-wallet-store-request:hc !<(request:bws q.cage.sign))
::
%btc-wallet-store-update
2020-11-17 13:10:54 +03:00
(handle-wallet-store-update:hc wire !<(update:bws q.cage.sign))
2020-11-04 16:55:47 +03:00
==
[cards this]
2020-10-24 19:13:36 +03:00
==
++ on-arvo on-arvo:def
++ on-fail on-fail:def
--
|_ =bowl:gall
++ handle-action
|= act=action
^- (quip card _state)
?- -.act
%set-provider
2020-11-04 18:24:00 +03:00
=* sub-card
[%pass /set-provider %agent [provider.act %btc-provider] %watch /clients]
2020-11-09 18:34:29 +03:00
:_ state(provider [~ provider.act %.n])
2020-11-04 18:24:00 +03:00
?~ provider ~[sub-card]
:~ [%pass /set-provider %agent [host.u.provider %btc-provider] %leave ~]
2020-11-04 18:24:00 +03:00
sub-card
==
2020-11-16 20:47:00 +03:00
::
2020-11-17 11:25:50 +03:00
%set-default-wallet
=/ xs=(list xpub) scry-scanned
?. (gth (lent xs) 0) `state
`state(def-wallet `(snag 0 xs))
::
%req-pay-address
2020-11-17 13:10:54 +03:00
:: TODO: add whitelisting here instead of comet block
:: can't pay yourself; comets can't pay (could spam requests)
:: forwards poke to payee if payee isn't us
:: wire is /payer/value/timestamp
::
?< =(src.bowl payee.act)
?< ?=(%pawn (clan:title src.bowl))
2020-11-17 12:34:04 +03:00
:_ state
2020-11-17 13:10:54 +03:00
?. =(payee.act our.bowl)
2020-11-17 12:34:04 +03:00
~[(poke-wallet-hook payee.act act)]
?~ def-wallet ~|("btc-wallet-hook: no def-wallet set" !!)
2020-11-19 18:21:32 +03:00
~[(poke-wallet-store [%generate-address u.def-wallet %0 `[src.bowl value.act]])]
2020-11-17 11:25:50 +03:00
::
2020-11-19 18:21:32 +03:00
%ret-pay-address
?~ def-wallet ~|("btc-wallet-hook: no def-wallet set" !!)
?> =(payer.act our.bowl)
:_ state
:~ %- poke-wallet-store
2020-11-19 18:54:01 +03:00
[%generate-txbu u.def-wallet `src.bowl fee.btc-state ~[[address.act value.act]]]
2020-11-19 18:21:32 +03:00
==
2020-11-09 18:34:29 +03:00
::
%force-retry
2020-11-17 18:34:48 +03:00
[(retry pend-addr) state]
2020-10-24 19:13:36 +03:00
==
:: if status is %connected, retry all pending address lookups
2020-11-20 14:58:27 +03:00
:: only retry if previously disconnected
::
++ handle-provider-status
|= s=status:bp
^- (quip card _state)
?~ provider `state
?. =(host.u.provider src.bowl) `state
2020-11-16 20:06:45 +03:00
?- -.s
%connected
2020-11-17 18:34:48 +03:00
:- ?:(connected.u.provider ~ (retry pend-addr))
2020-11-16 20:06:45 +03:00
%= state
provider `[host.u.provider %.y]
btc-state [blockcount.s fee.s now.bowl]
==
%disconnected
`state(provider `[host.u.provider %.n])
==
::
++ handle-provider-update
2020-11-20 14:58:27 +03:00
|= upd=update:bp
2020-11-17 11:25:50 +03:00
^- (quip card _state)
2020-11-20 14:58:27 +03:00
?. ?=(%.y -.upd) `state
?- -.body.p.upd
%address-info
2020-11-20 14:58:27 +03:00
=/ ureq (~(get by pend-addr) req-id.p.upd)
?~ ureq `state
2020-11-20 14:58:27 +03:00
:_ state(pend-addr (~(del by pend-addr) req-id.p.upd))
2020-11-19 18:21:32 +03:00
:~ %- poke-wallet-store
:* %address-info xpub.u.ureq chyg.u.ureq idx.u.ureq
2020-11-20 14:58:27 +03:00
utxos.body.p.upd used.body.p.upd blockcount.body.p.upd
==
==
2020-11-20 14:58:27 +03:00
::
%raw-tx
2020-11-20 16:22:24 +03:00
:: TODO: check whether in poym
2020-11-20 14:58:27 +03:00
~& >> rawtx.body.p.upd
`state
==
::
2020-11-17 12:34:04 +03:00
++ handle-wallet-store-request
2020-11-04 16:55:47 +03:00
|= req=request:bws
^- (quip card _state)
?- -.req
%scan-address
2020-11-20 14:58:27 +03:00
=/ ri=req-id:bp (gen-req-id:bp eny.bowl)
2020-11-17 18:34:48 +03:00
:_ state(pend-addr (~(put by pend-addr) ri req))
2020-11-10 14:09:59 +03:00
?~ provider ~
?: provider-connected
~[(get-address-info ri host.u.provider a.req)]
~& > "provider not connected"
2020-11-10 14:09:59 +03:00
~
2020-11-04 16:55:47 +03:00
==
::
2020-11-17 12:34:04 +03:00
++ handle-wallet-store-update
2020-11-17 13:10:54 +03:00
|= [=wire upd=update:bws]
2020-11-17 12:34:04 +03:00
^- (quip card _state)
?- -.upd
%generate-address
2020-11-19 18:54:01 +03:00
:: if no meta (payer/value), just prints address
2020-11-17 13:10:54 +03:00
::
2020-11-19 17:42:53 +03:00
?~ meta.upd ~&(> address.upd `state)
2020-11-19 12:45:05 +03:00
=/ [payer=ship value=sats] u.meta.upd
2020-11-19 18:21:32 +03:00
:- ~[(poke-wallet-hook payer [%ret-pay-address address.upd payer value])]
2020-11-19 17:42:53 +03:00
(update-piym address.upd u.meta.upd)
::
%generate-txbu
2020-11-20 14:58:27 +03:00
:: txbus can potentially use the same UTXO inputs, so if another payment
:: was in process of fetching raw-txs for a txbu, replace it
::
2020-11-20 16:22:24 +03:00
=/ txids=(list txid)
%+ turn txis.txbu.upd
|= =txi:bws txid.utxo.txi
:_ state(poym [payee.upd txbu.upd])
2020-11-20 14:58:27 +03:00
:: TODO: send all its input tx-ids out for feedback
2020-11-17 12:34:04 +03:00
::
%scan-done
2020-11-17 18:34:48 +03:00
?~ def-wallet
`state(def-wallet `xpub.upd)
2020-11-17 12:34:04 +03:00
`state
==
2020-11-19 17:42:53 +03:00
:: update piym with a payment
:: moons are stored with their sponsor
:: if ship already has a payment for the payer ship, replace
::
++ update-piym
|= p=payment
|^ ^- _state
=/ fam=ship
?: =(%earl (clan:title payer.p))
(sein:title our.bowl now.bowl payer.p)
payer.p
=/ ups=(unit (list payment))
(~(get by piym) fam)
?~ ups (insert fam ~[p])
~| "btc-wallet-hook: too many address requests from moons"
?> (lte (lent u.ups) moon-limit.state)
=/ i=(unit @)
(find ~[payer.p] (turn u.ups |=([* py=ship *] py)))
?~ i (insert fam [p u.ups])
(insert fam (snap u.ups u.i p))
++ insert
|= [fam=ship ps=(list payment)]
state(piym (~(put by piym) fam ps))
--
2020-11-17 12:34:04 +03:00
::
2020-11-09 16:27:04 +03:00
++ retry
2020-11-17 18:34:48 +03:00
|= p=^pend-addr
2020-11-09 16:27:04 +03:00
^- (list card)
?~ provider ~|("provider not set" !!)
2020-11-17 11:25:50 +03:00
%+ turn ~(tap by p)
2020-11-09 16:27:04 +03:00
|= [ri=req-id:bp req=request:bws]
(get-address-info ri host.u.provider a.req)
::
2020-11-04 16:55:47 +03:00
++ get-address-info
2020-11-20 16:22:24 +03:00
|= [ri=req-id:bp host=ship a=address]
^- card
2020-11-04 16:55:47 +03:00
:* %pass /[(scot %da now.bowl)] %agent [host %btc-provider]
2020-11-09 18:34:29 +03:00
%poke %btc-provider-action !>([ri %address-info a])
2020-11-04 16:55:47 +03:00
==
2020-11-20 14:58:27 +03:00
::
2020-11-20 16:22:24 +03:00
++ get-raw-tx
|= [host=ship =txid]
^- card
=/ ri=req-id:bp (gen-req-id:bp eny.bowl)
:* %pass /[(scot %da now.bowl)] %agent [host %btc-provider]
%poke %btc-provider-action !>(ri %raw-tx txid)
==
::
2020-11-10 14:09:59 +03:00
++ provider-connected
^- ?
?~ provider %.n
connected.u.provider
2020-11-17 11:25:50 +03:00
::
2020-11-17 12:34:04 +03:00
++ poke-wallet-hook
|= [target=ship act=action]
^- card
2020-11-17 11:25:50 +03:00
:* %pass /[(scot %da now.bowl)] %agent
2020-11-17 12:34:04 +03:00
[target %btc-wallet-hook] %poke
%btc-wallet-hook-action !>(act)
==
::
++ poke-wallet-store
2020-11-19 18:21:32 +03:00
|= act=action:bws
2020-11-17 12:34:04 +03:00
^- card
2020-11-19 18:21:32 +03:00
:* %pass /[(scot %da now.bowl)]
2020-11-17 12:34:04 +03:00
%agent [our.bowl %btc-wallet-store] %poke
2020-11-17 11:25:50 +03:00
%btc-wallet-store-action !>(act)
==
::
++ scry-scanned
.^ (list xpub)
%gx
(scot %p our.bowl)
%btc-wallet-store
(scot %da now.bowl)
%scanned
%noun
==
2020-10-24 19:13:36 +03:00
--