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-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
|
2020-11-22 15:57:25 +03:00
|
|
|
poym=(unit txbu:bws)
|
2020-11-17 13:10:54 +03:00
|
|
|
=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]
|
|
|
|
::
|
2020-11-20 18:57:19 +03:00
|
|
|
:: TODO: handle /sign-me path
|
2020-10-24 19:13:36 +03:00
|
|
|
++ 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)
|
2020-11-10 13:30:06 +03:00
|
|
|
%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
|
2020-11-10 13:30:06 +03:00
|
|
|
%btc-provider-status
|
|
|
|
(handle-provider-status:hc !<(status:bp q.cage.sign))
|
|
|
|
::
|
2020-11-09 16:27:04 +03:00
|
|
|
%btc-provider-update
|
2020-11-10 17:33:27 +03:00
|
|
|
(handle-provider-update:hc !<(update:bp q.cage.sign))
|
2020-11-10 13:30:06 +03:00
|
|
|
::
|
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
|
2020-11-10 13:30:06 +03:00
|
|
|
[%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]
|
2020-11-10 13:30:06 +03:00
|
|
|
:~ [%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
|
2020-11-22 15:57:25 +03:00
|
|
|
:: can't request payment address if poym already building a tx
|
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
|
|
|
|
:: wire is /payer/value/timestamp
|
|
|
|
::
|
2020-11-22 15:57:25 +03:00
|
|
|
~| "Can't pay ourselves; can't pay if poym full; no comets"
|
2020-11-17 13:10:54 +03:00
|
|
|
?< =(src.bowl payee.act)
|
2020-11-22 15:57:25 +03:00
|
|
|
?< ?&(=(src.bowl our.bowl) ?=(^ poym))
|
2020-11-17 13:10:54 +03:00
|
|
|
?< ?=(%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-22 15:57:25 +03:00
|
|
|
[retry-pend-addr state]
|
2020-10-24 19:13:36 +03:00
|
|
|
==
|
2020-11-10 17:33:27 +03:00
|
|
|
:: if status is %connected, retry all pending address lookups
|
2020-11-20 14:58:27 +03:00
|
|
|
:: only retry if previously disconnected
|
2020-11-10 17:33:27 +03:00
|
|
|
::
|
|
|
|
++ 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
|
2020-11-10 17:33:27 +03:00
|
|
|
%connected
|
2020-11-22 15:57:25 +03:00
|
|
|
:- ?: connected.u.provider ~
|
|
|
|
(weld retry-pend-addr retry-txbu)
|
2020-11-16 20:06:45 +03:00
|
|
|
%= state
|
|
|
|
provider `[host.u.provider %.y]
|
|
|
|
btc-state [blockcount.s fee.s now.bowl]
|
|
|
|
==
|
2020-11-10 17:33:27 +03:00
|
|
|
%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
|
2020-11-10 17:33:27 +03:00
|
|
|
%address-info
|
2020-11-20 14:58:27 +03:00
|
|
|
=/ ureq (~(get by pend-addr) req-id.p.upd)
|
2020-11-10 17:33:27 +03:00
|
|
|
?~ 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
|
2020-11-13 17:05:25 +03:00
|
|
|
:* %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-10 17:33:27 +03:00
|
|
|
==
|
|
|
|
==
|
2020-11-20 14:58:27 +03:00
|
|
|
::
|
|
|
|
%raw-tx
|
2020-11-22 15:57:25 +03:00
|
|
|
?~ poym ~&(>> "btc-wallet-hook: %raw-tx: poym is empty" `state)
|
|
|
|
=. 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-11-10 17:33:27 +03:00
|
|
|
==
|
|
|
|
::
|
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)]
|
2020-11-10 17:33:27 +03:00
|
|
|
~& > "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-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-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-22 15:57:25 +03:00
|
|
|
:: insert rawtx into our poym txbu, if it exists
|
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))
|
|
|
|
:: poym-ready: do we have all rawtx for inputs?
|
|
|
|
::
|
|
|
|
++ 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-11-22 15:57:25 +03:00
|
|
|
++ retry-pend-addr
|
2020-11-09 16:27:04 +03:00
|
|
|
^- (list card)
|
|
|
|
?~ provider ~|("provider not set" !!)
|
2020-11-22 15:57:25 +03:00
|
|
|
%+ turn ~(tap by pend-addr)
|
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-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
|
|
|
--
|