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-09 15:29:30 +03:00
|
|
|
/- *btc, *btc-wallet-hook, bws=btc-wallet-store, bp=btc-provider
|
2020-11-11 11:00:32 +03:00
|
|
|
/+ dbug, default-agent, bwsl=btc-wallet-store
|
2020-10-24 19:13:36 +03:00
|
|
|
|%
|
|
|
|
+$ versioned-state
|
|
|
|
$% state-0
|
|
|
|
==
|
2020-11-19 17:42:53 +03:00
|
|
|
++ def-moon-limit 10
|
|
|
|
:: provider: maybe ship if provider is set
|
|
|
|
:: moon-limit: how many addresses a ship and its moons can request in piym
|
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
|
|
|
|
=pend-txbu
|
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-19 17:42:53 +03:00
|
|
|
:_ this(moon-limit.state def-moon-limit)
|
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)
|
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
|
|
|
|
:: 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 12:45:05 +03:00
|
|
|
~[(poke-wallet-store / [%generate-address u.def-wallet %0 `[src.bowl value.act]])]
|
2020-11-17 11:25:50 +03:00
|
|
|
::
|
|
|
|
%pay-address
|
2020-11-19 14:44:12 +03:00
|
|
|
:: we get passed address/payer/value
|
|
|
|
:: TODO: ask wallet to generate txbu for poym
|
2020-11-17 18:34:48 +03:00
|
|
|
:: TODO: update poym
|
|
|
|
:: send tx request out for poym
|
2020-11-16 20:47:00 +03:00
|
|
|
`state
|
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
|
|
|
==
|
2020-11-10 17:33:27 +03:00
|
|
|
:: if status is %connected, retry all pending address lookups
|
|
|
|
::
|
|
|
|
++ 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
|
|
|
|
:: only retry if previously disconnected
|
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]
|
|
|
|
==
|
2020-11-10 17:33:27 +03:00
|
|
|
%disconnected
|
|
|
|
`state(provider `[host.u.provider %.n])
|
|
|
|
==
|
|
|
|
::
|
|
|
|
++ handle-provider-update
|
|
|
|
|= =update:bp
|
2020-11-17 11:25:50 +03:00
|
|
|
^- (quip card _state)
|
2020-11-10 17:33:27 +03:00
|
|
|
?. ?=(%& -.update) `state
|
|
|
|
?- -.body.p.update
|
|
|
|
%address-info
|
2020-11-17 18:34:48 +03:00
|
|
|
=/ ureq (~(get by pend-addr) req-id.p.update)
|
2020-11-10 17:33:27 +03:00
|
|
|
?~ ureq `state
|
2020-11-17 18:34:48 +03:00
|
|
|
:_ state(pend-addr (~(del by pend-addr) req-id.p.update))
|
2020-11-17 12:34:04 +03:00
|
|
|
:~ %+ poke-wallet-store /
|
2020-11-13 17:05:25 +03:00
|
|
|
:* %address-info xpub.u.ureq chyg.u.ureq idx.u.ureq
|
|
|
|
utxos.body.p.update used.body.p.update blockcount.body.p.update
|
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-09 15:43:30 +03:00
|
|
|
=/ ri=req-id:bp (mk-req-id (hash-xpub:bwsl +>.req))
|
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 14:44:12 +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-17 13:10:54 +03:00
|
|
|
:- ~[(poke-wallet-hook payer [%pay-address address.upd payer value])]
|
2020-11-19 17:42:53 +03:00
|
|
|
(update-piym address.upd u.meta.upd)
|
|
|
|
::
|
|
|
|
%generate-txbu
|
|
|
|
:: TODO: finish
|
|
|
|
`state
|
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-09 15:43:30 +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-09 15:29:30 +03:00
|
|
|
++ mk-req-id
|
|
|
|
|= hash=@ux ^- req-id:bp
|
|
|
|
(scot %ux hash)
|
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
|
|
|
|
|= [prefix=wire act=action:bws]
|
|
|
|
^- card
|
|
|
|
:* %pass (weld prefix /[(scot %da now.bowl)])
|
|
|
|
%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
|
|
|
--
|