shrub/app/btc-wallet-hook.hoon

410 lines
11 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-11-20 18:57:19 +03:00
:: /sign-me
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-28 10:25:32 +03:00
:: feybs: fee/byte in sats used for a given ship payee
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-26 12:38:30 +03:00
feybs=(map ship sats)
2020-11-28 10:25:32 +03:00
=reqs
2020-11-17 13:10:54 +03:00
=piym
2020-12-06 14:46:32 +03:00
=poym
=piym-lock
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]
::
2020-11-22 17:49:55 +03:00
++ on-watch
|= pax=path
^- (quip card _this)
?+ pax (on-watch:def pax)
[%sign-me ~]
`this
==
2020-10-24 19:13:36 +03:00
++ 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
2020-11-26 12:38:30 +03:00
~& >>> "kicked from provider {<src.bowl>}"
?~ 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
==
2020-11-24 22:30:41 +03:00
++ on-arvo on-arvo:def
2020-10-24 19:13:36 +03:00
++ 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-26 12:38:30 +03:00
:: overwrites any payment being built currently
2020-11-17 13:10:54 +03:00
:: can't pay yourself; comets can't pay (could spam requests)
:: forwards poke to payee if payee isn't us
2020-12-06 14:46:32 +03:00
:: deletes poym since we'll be making a new outgoing payment
:: lets us set fee per byte and recall it once we get a payment address back
:: wire is /payer/value/timestamp
2020-11-17 13:10:54 +03:00
::
2020-11-26 12:38:30 +03:00
~| "Can't pay ourselves; no comets"
2020-11-17 13:10:54 +03:00
?< =(src.bowl payee.act)
2020-11-26 12:38:30 +03:00
?< ?=(%pawn (clan:title payee.act))
=+ feyb=?~(feyb.act fee.btc-state u.feyb.act)
=> .(poym ~, feybs (~(put by feybs) payee.act feyb))
2020-11-17 12:34:04 +03:00
:_ state
2020-11-26 12:38:30 +03:00
~[(poke-wallet-hook payee.act [%gen-pay-address value.act])]
::
%gen-pay-address
2020-12-06 14:46:32 +03:00
:: TODO: only run this if the moon limit isn't passed
2020-11-26 12:38:30 +03:00
~| "Can't pay ourselves; no comets"
?< =(src.bowl our.bowl)
?< ?=(%pawn (clan:title src.bowl))
2020-11-17 12:34:04 +03:00
?~ def-wallet ~|("btc-wallet-hook: no def-wallet set" !!)
2020-12-06 14:46:32 +03:00
=+ fam=(piym-key src.bowl)
?~ fam
~& >>> "not enough moon space or payer has an unconfirmed tx in to us"
`state
2020-11-26 12:38:30 +03:00
:_ state
:~ %- 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
2020-11-26 12:38:30 +03:00
?: =(src.bowl our.bowl) ~|("Can't pay ourselves" !!)
2020-11-19 18:21:32 +03:00
?~ def-wallet ~|("btc-wallet-hook: no def-wallet set" !!)
2020-11-26 12:38:30 +03:00
=+ feyb=(~(gut by feybs) src.bowl fee.btc-state)
2020-11-19 18:21:32 +03:00
?> =(payer.act our.bowl)
:_ state
:~ %- poke-wallet-store
2020-11-26 12:38:30 +03:00
[%generate-txbu u.def-wallet `src.bowl feyb ~[[address.act value.act]]]
2020-11-19 18:21:32 +03:00
==
2020-12-06 14:46:32 +03:00
::
%broadcast-tx
`state
2020-11-26 12:38:30 +03:00
::
%clear-poym
`state(poym ~)
2020-11-09 18:34:29 +03:00
::
%force-retry
2020-12-04 21:54:59 +03:00
[(retry-reqs block.btc-state) state]
2020-10-24 19:13:36 +03:00
==
2020-11-25 11:49:33 +03:00
:: +handle-provider-status: handle connectivity updates from provider
2020-12-04 21:54:59 +03:00
:: - if status is %connected, retry all pending address lookups
:: - only retry all if previously disconnected
:: - if block is updated, retry all address reqs
::
++ 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-12-04 21:54:59 +03:00
:_ %= state
provider `[host.u.provider %.y]
btc-state [block.s fee.s now.bowl]
==
?: ?!(connected.u.provider)
(weld (retry-reqs block.s) retry-txbu)
2020-12-04 23:20:43 +03:00
?. (lth block.btc-state block.s) ~
~& > "got new block, retrying {<(lent (retry-reqs block.s))>} reqs "
2020-12-04 21:54:59 +03:00
(retry-reqs block.s)
::
%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-12-03 12:47:54 +03:00
=/ req=(unit request:bws)
(~(get by reqs) req-id.p.upd)
2020-11-25 11:49:33 +03:00
?~ req `state
2020-12-03 12:47:54 +03:00
:_ state(reqs (~(del by reqs) req-id.p.upd))
2020-11-19 18:21:32 +03:00
:~ %- poke-wallet-store
2020-11-25 11:49:33 +03:00
:* %address-info xpub.u.req chyg.u.req idx.u.req
2020-12-03 12:47:54 +03:00
utxos.body.p.upd used.body.p.upd block.body.p.upd
==
==
2020-11-20 14:58:27 +03:00
::
%raw-tx
2020-11-26 12:38:30 +03:00
?~ poym `state
2020-11-22 15:57:25 +03:00
=. txis.u.poym (update-poym-txis txis.u.poym +.body.p.upd)
2020-11-20 18:57:19 +03:00
:_ state
2020-11-22 15:57:25 +03:00
?:(poym-ready ~[(send-sign-tx u.poym)] ~)
==
2020-12-04 21:54:59 +03:00
:: get address-info for the request if block in request is old
::
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
2020-12-03 12:47:54 +03:00
%address-info
2020-11-25 11:49:33 +03:00
=+ ri=(gen-req-id:bp eny.bowl)
2020-12-03 12:47:54 +03:00
:_ state(reqs (~(put by reqs) ri req))
2020-11-10 14:09:59 +03:00
?~ provider ~
2020-12-04 21:54:59 +03:00
?: ?& provider-connected
(lth last-block.req block.btc-state)
==
2020-11-10 14:09:59 +03:00
~[(get-address-info ri host.u.provider a.req)]
~
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-12-06 14:46:32 +03:00
:: if no peta (payer/value), just prints address
2020-11-17 13:10:54 +03:00
::
2020-12-06 14:46:32 +03:00
?~ peta.upd ~&(> address.upd `state)
=/ [payer=ship value=sats] u.peta.upd
2020-12-06 20:53:02 +03:00
:_ (update-piym xpub.upd address.upd payer value)
2020-12-06 14:46:32 +03:00
:~ (poke-wallet-hook payer [%ret-pay-address address.upd payer value])
(poke-wallet-store [%add-piym address.upd peta.upd])
==
2020-11-19 17:42:53 +03:00
::
%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-22 15:57:25 +03:00
:_ state(poym `txbu.upd)
2020-11-20 16:30:35 +03:00
?~ provider ~&(>>> "provider not set" ~)
%+ turn txis.txbu.upd
|=(=txi:bws (get-raw-tx host.u.provider txid.utxo.txi))
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-12-06 14:46:32 +03:00
:: +piym-key: returns (unit ship), the sponsor of a ship
:: checks whether
:: - too many moons are already stored
:: - input ship is locked (i.e. broadcast a tx not in mempool yet)
::
++ piym-key
|= payer=ship
^- (unit ship)
?: (~(has by piym-lock) payer)
~
=/ fam=ship
?: =(%earl (clan:title payer))
(sein:title our.bowl now.bowl payer)
payer
=+ ps=(~(get by piym) fam)
?~ ps `fam
?: (lte (lent u.ps) moon-limit.state)
`fam
~
2020-11-25 11:49:33 +03:00
:: +update-piym: store an expected incoming payment by ship
:: moons are stored with their sponsor
:: if ship already has a payment for the payer ship, replace
2020-11-19 17:42:53 +03:00
::
++ update-piym
|= p=payment
|^ ^- _state
2020-12-06 14:46:32 +03:00
=+ fam=(piym-key payer.p)
?~ fam ~!("Too many moons or payer is locked" !!)
=+ ps=(~(get by piym) u.fam)
?~ ps (insert u.fam ~[p])
2020-11-19 17:42:53 +03:00
=/ i=(unit @)
2020-11-25 11:49:33 +03:00
(find ~[payer.p] (turn u.ps |=([* py=ship *] py)))
2020-12-06 14:46:32 +03:00
?~ i (insert u.fam [p u.ps])
(insert u.fam (snap u.ps u.i p))
2020-11-19 17:42:53 +03:00
++ insert
2020-12-06 14:46:32 +03:00
|= [u.fam=ship ps=(list payment)]
state(piym (~(put by piym) u.fam ps))
2020-11-19 17:42:53 +03:00
--
2020-11-25 11:49:33 +03:00
:: +update-poym-txis:
:: update outgoing payment with a rawtx, if the txid is in poym's txis
2020-11-17 12:34:04 +03:00
::
2020-11-22 15:57:25 +03:00
++ update-poym-txis
|= [txis=(list txi:bws) =txid rt=rawtx]
^- (list txi:bws)
2020-11-20 18:57:19 +03:00
=| i=@
2020-11-22 15:57:25 +03:00
|- ?: (gte i (lent txis)) txis
2020-11-20 18:57:19 +03:00
=/ ith=txi:bws (snag i txis)
=? txis =(txid txid.utxo.ith)
(snap txis i `txi:bws`ith(ur `rt))
$(i +(i))
2020-11-25 11:49:33 +03:00
:: +poym-ready: whether all txis in poym have rawtxs
2020-11-20 18:57:19 +03:00
::
++ poym-ready
^- ?
2020-11-22 15:57:25 +03:00
?~ poym %.n
%+ levy txis.u.poym
2020-11-20 18:57:19 +03:00
|=(t=txi:bws ?=(^ ur.t))
2020-12-04 21:54:59 +03:00
:: +retry-reqs: get-address-info for any reqs with old last-block
2020-11-20 18:57:19 +03:00
::
2020-12-03 12:47:54 +03:00
++ retry-reqs
2020-12-04 21:54:59 +03:00
|= [latest-block=@ud]
2020-11-09 16:27:04 +03:00
^- (list card)
?~ provider ~|("provider not set" !!)
2020-12-04 21:54:59 +03:00
%+ murn ~(tap by reqs)
2020-11-09 16:27:04 +03:00
|= [ri=req-id:bp req=request:bws]
2020-12-04 21:54:59 +03:00
?: (gte last-block.req latest-block) ~
`(get-address-info ri host.u.provider a.req)
2020-11-09 16:27:04 +03:00
::
2020-11-22 15:57:25 +03:00
++ retry-txbu
^- (list card)
?~ poym ~
?~ provider ~|("provider not set" !!)
%+ turn txis.u.poym
|= =txi:bws
(get-raw-tx host.u.provider txid.utxo.txi)
::
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]
2020-11-20 16:30:35 +03:00
%poke %btc-provider-action !>([ri %raw-tx txid])
2020-11-20 16:22:24 +03:00
==
::
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)
==
::
2020-11-22 15:57:25 +03:00
++ send-sign-tx
|= =txbu:bws
2020-11-20 18:57:19 +03:00
^- card
2020-11-22 15:57:25 +03:00
[%give %fact ~[/sign-me] %btc-wallet-hook-request !>([%sign-tx txbu])]
2020-11-20 18:57:19 +03:00
::
2020-11-17 12:34:04 +03:00
++ 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
--