2020-10-20 17:13:52 +03:00
|
|
|
:: btc-provider.hoon
|
2020-10-19 15:21:54 +03:00
|
|
|
:: Proxy that serves a BTC full node and ElectRS address indexer
|
|
|
|
::
|
2020-10-28 16:20:24 +03:00
|
|
|
:: Subscriptions: none
|
|
|
|
:: To Subscribers:
|
|
|
|
:: current connection state
|
|
|
|
:: results/errors of RPC calls
|
|
|
|
::
|
2020-10-20 17:13:52 +03:00
|
|
|
/+ *btc-provider, dbug, default-agent
|
2020-10-19 15:21:54 +03:00
|
|
|
|%
|
|
|
|
+$ versioned-state
|
|
|
|
$% state-0
|
|
|
|
==
|
|
|
|
::
|
2020-10-22 10:36:25 +03:00
|
|
|
+$ state-0 [%0 =host-info whitelist=(set ship)]
|
2020-10-19 15:21:54 +03:00
|
|
|
::
|
|
|
|
+$ card card:agent:gall
|
|
|
|
::
|
|
|
|
--
|
|
|
|
%- agent:dbug
|
|
|
|
=| state-0
|
|
|
|
=* state -
|
|
|
|
^- agent:gall
|
|
|
|
=<
|
|
|
|
|_ =bowl:gall
|
|
|
|
+* this .
|
|
|
|
def ~(. (default-agent this %|) bowl)
|
|
|
|
hc ~(. +> bowl)
|
|
|
|
::
|
|
|
|
++ on-init
|
|
|
|
^- (quip card _this)
|
|
|
|
~& > '%btc-provider initialized successfully'
|
2020-10-22 10:36:25 +03:00
|
|
|
`this(host-info [*credentials connected=%.n clients=*(set ship)], whitelist *(set ship))
|
2020-10-19 15:21:54 +03:00
|
|
|
++ on-save
|
|
|
|
^- vase
|
|
|
|
!>(state)
|
|
|
|
++ on-load
|
|
|
|
|= old-state=vase
|
|
|
|
^- (quip card _this)
|
2020-10-20 17:13:52 +03:00
|
|
|
~& > '%btc-provider recompiled successfully '
|
2020-10-19 15:21:54 +03:00
|
|
|
`this(state !<(versioned-state old-state))
|
|
|
|
++ on-poke
|
|
|
|
|= [=mark =vase]
|
|
|
|
^- (quip card _this)
|
|
|
|
:: Only allow clients/authorized to poke
|
|
|
|
::
|
|
|
|
?> ?|((team:title our.bowl src.bowl) (is-client:hc src.bowl))
|
|
|
|
=^ cards state
|
2020-10-20 14:15:17 +03:00
|
|
|
?: ?=(%btc-provider-command mark)
|
2020-10-29 15:10:54 +03:00
|
|
|
?> (team:title our.bowl src.bowl)
|
2020-10-20 14:15:17 +03:00
|
|
|
(handle-command:hc !<(command vase))
|
2020-10-19 15:21:54 +03:00
|
|
|
?+ mark (on-poke:def mark vase)
|
2020-10-20 13:59:10 +03:00
|
|
|
%btc-provider-action
|
2020-10-23 20:35:04 +03:00
|
|
|
?. connected.host-info
|
|
|
|
~|("Not connected to RPC endpoints" !!)
|
2020-10-20 13:59:10 +03:00
|
|
|
(handle-action:hc !<(action vase))
|
2020-10-19 15:21:54 +03:00
|
|
|
==
|
|
|
|
[cards this]
|
|
|
|
++ on-watch
|
|
|
|
|= pax=path
|
|
|
|
^- (quip card _this)
|
2020-10-24 19:13:36 +03:00
|
|
|
?> (is-whitelisted:hc src.bowl)
|
|
|
|
~& > "btc-provider: added client {<src.bowl>}"
|
|
|
|
`this(clients.host-info (~(put in clients.host-info) src.bowl))
|
2020-10-19 15:21:54 +03:00
|
|
|
::
|
|
|
|
++ on-leave on-leave:def
|
|
|
|
++ on-peek on-peek:def
|
|
|
|
++ on-agent on-agent:def
|
|
|
|
++ on-arvo
|
|
|
|
|= [=wire =sign-arvo]
|
|
|
|
^- (quip card _this)
|
|
|
|
=^ cards state
|
|
|
|
?+ +<.sign-arvo (on-arvo:def wire sign-arvo)
|
|
|
|
%http-response
|
2020-10-20 17:13:52 +03:00
|
|
|
(handle-response:hc wire client-response.sign-arvo)
|
2020-10-19 15:21:54 +03:00
|
|
|
==
|
|
|
|
[cards this]
|
|
|
|
::
|
|
|
|
++ on-fail on-fail:def
|
|
|
|
--
|
|
|
|
:: helper core
|
|
|
|
|_ =bowl:gall
|
2020-10-20 13:59:10 +03:00
|
|
|
++ handle-action
|
|
|
|
|= act=action
|
2020-10-22 10:36:25 +03:00
|
|
|
|^ ^- (quip card _state)
|
|
|
|
=/ ract=action:rpc
|
2020-10-20 17:13:52 +03:00
|
|
|
?- -.act
|
2020-11-04 18:24:00 +03:00
|
|
|
%get-address-info
|
2020-10-22 10:36:25 +03:00
|
|
|
[%erpc %get-address-utxos address.act]
|
2020-10-20 17:13:52 +03:00
|
|
|
==
|
2020-10-22 10:36:25 +03:00
|
|
|
[~[(req-card act ract)] state]
|
|
|
|
++ req-card
|
|
|
|
|= [act=action ract=action:rpc]
|
|
|
|
=| out=outbound-config:iris
|
|
|
|
=/ req=request:http
|
|
|
|
(gen-request host-info ract)
|
|
|
|
[%pass /[-.act]/[-.ract]/[(scot %da now.bowl)] %arvo %i %request req out]
|
|
|
|
--
|
2020-10-19 15:21:54 +03:00
|
|
|
++ handle-command
|
|
|
|
|= comm=command
|
|
|
|
^- (quip card _state)
|
|
|
|
?- -.comm
|
|
|
|
%set-credentials
|
2020-10-22 10:36:25 +03:00
|
|
|
`state(host-info [creds.comm connected=%.y clients=*(set ship)])
|
2020-10-19 15:21:54 +03:00
|
|
|
::
|
|
|
|
%whitelist-clients
|
|
|
|
`state(whitelist (~(uni in whitelist) clients.comm))
|
|
|
|
==
|
2020-10-20 17:13:52 +03:00
|
|
|
++ handle-response
|
|
|
|
|= [=wire response=client-response:iris]
|
2020-10-24 19:13:36 +03:00
|
|
|
:: IMPORTANT: whatever we make here gets sent out to subscribers at the end
|
2020-10-20 17:13:52 +03:00
|
|
|
^- (quip card _state)
|
|
|
|
?. ?=(%finished -.response) `state
|
2020-10-23 20:35:04 +03:00
|
|
|
=/ e=(unit error) (check-connection status-code.response-header.response)
|
2020-10-24 19:13:36 +03:00
|
|
|
~& > "before"
|
2020-10-23 20:35:04 +03:00
|
|
|
?^ e
|
|
|
|
:_ state(connected.host-info %.n)
|
2020-10-28 16:20:24 +03:00
|
|
|
~[(send-update [%| u.e])]
|
2020-10-24 19:13:36 +03:00
|
|
|
~& > "after"
|
2020-10-20 17:13:52 +03:00
|
|
|
=/ rpc-resp=response:rpc:jstd
|
|
|
|
(get-rpc-response response)
|
2020-10-22 10:36:25 +03:00
|
|
|
:: TODO: error handling goes here
|
2020-10-23 20:35:04 +03:00
|
|
|
?+ wire ~|("Unexpected HTTP response" !!)
|
|
|
|
[%get-address-info %erpc *]
|
2020-10-22 10:36:25 +03:00
|
|
|
~& >> +<.wire
|
2020-10-20 17:13:52 +03:00
|
|
|
~& > rpc-resp
|
|
|
|
`state
|
|
|
|
==
|
2020-10-23 20:35:04 +03:00
|
|
|
++ check-connection
|
|
|
|
|= status=@ud
|
|
|
|
^- (unit error)
|
|
|
|
?: =(504 status)
|
|
|
|
`[%not-connected ~]
|
|
|
|
~
|
|
|
|
++ send-update
|
|
|
|
|= =update ^- card
|
|
|
|
[%give %fact [/clients]~ %btc-provider-update !>(update)]
|
2020-10-19 15:21:54 +03:00
|
|
|
++ is-whitelisted
|
|
|
|
|= user=ship ^- ?
|
|
|
|
?| (~(has in whitelist) user)
|
|
|
|
=(our.bowl user)
|
|
|
|
==
|
|
|
|
++ is-client
|
|
|
|
|= user=ship ^- ?
|
2020-10-22 10:36:25 +03:00
|
|
|
(~(has in clients.host-info) user)
|
2020-10-19 15:21:54 +03:00
|
|
|
::
|
|
|
|
++ httr-to-rpc-response
|
|
|
|
|= hit=httr:eyre
|
|
|
|
^- response:rpc:jstd
|
|
|
|
~| hit
|
|
|
|
=/ jon=json (need (de-json:html q:(need r.hit)))
|
|
|
|
?. =(%2 (div p.hit 100))
|
|
|
|
(parse-error jon)
|
|
|
|
=, dejs-soft:format
|
|
|
|
^- response:rpc:jstd
|
|
|
|
=; dere
|
|
|
|
=+ res=((ar dere) jon)
|
|
|
|
?~ res (need (dere jon))
|
|
|
|
[%batch u.res]
|
|
|
|
|= jon=json
|
|
|
|
^- (unit response:rpc:jstd)
|
|
|
|
=/ res=[id=(unit @t) res=(unit json) err=(unit json)]
|
|
|
|
%. jon
|
|
|
|
=, dejs:format
|
|
|
|
=- (ou -)
|
|
|
|
:~ ['id' (uf ~ (mu so))]
|
|
|
|
['result' (uf ~ (mu same))]
|
|
|
|
['error' (uf ~ (mu same))]
|
|
|
|
==
|
|
|
|
?: ?=([^ * ~] res)
|
|
|
|
`[%result [u.id.res ?~(res.res ~ u.res.res)]]
|
|
|
|
~| jon
|
|
|
|
`(parse-error jon)
|
|
|
|
::
|
2020-10-19 15:57:13 +03:00
|
|
|
++ get-rpc-response
|
|
|
|
|= response=client-response:iris
|
|
|
|
^- response:rpc:jstd
|
|
|
|
?> ?=(%finished -.response)
|
|
|
|
%- httr-to-rpc-response
|
|
|
|
%+ to-httr:iris
|
|
|
|
response-header.response
|
|
|
|
full-file.response
|
|
|
|
::
|
2020-10-19 15:21:54 +03:00
|
|
|
++ parse-error
|
|
|
|
|= =json
|
|
|
|
^- response:rpc:jstd
|
|
|
|
:- %error
|
|
|
|
?~ json ['' '' '']
|
|
|
|
%. json
|
|
|
|
=, dejs:format
|
|
|
|
=- (ou -)
|
|
|
|
:~ =- ['id' (uf '' (cu - (mu so)))]
|
|
|
|
|*(a=(unit) ?~(a '' u.a))
|
|
|
|
:- 'error'
|
|
|
|
=- (uf ['' ''] -)
|
|
|
|
=- (cu |*(a=(unit) ?~(a ['' ''] u.a)) (mu (ou -)))
|
|
|
|
:~ ['code' (uf '' no)]
|
|
|
|
['message' (uf '' so)]
|
|
|
|
== ==
|
|
|
|
::
|
2020-10-20 17:13:52 +03:00
|
|
|
:: TODO: BELOW are deprecated. Rip out their functionality
|
2020-10-19 15:21:54 +03:00
|
|
|
++ btc-http-response
|
2020-10-19 15:57:13 +03:00
|
|
|
|= [status=@ud rpc-resp=response:rpc:jstd]
|
2020-10-19 15:21:54 +03:00
|
|
|
^- (quip card _state)
|
|
|
|
?. ?=([%result *] rpc-resp)
|
|
|
|
~& [%error +.rpc-resp]
|
|
|
|
[~ state]
|
2020-10-20 17:13:52 +03:00
|
|
|
:: ~& > (parse-response:btc-rpc:blib rpc-resp)
|
2020-10-19 15:21:54 +03:00
|
|
|
[~ state]
|
|
|
|
::
|
|
|
|
++ electrum-http-response
|
2020-10-19 15:57:13 +03:00
|
|
|
|= [status=@ud rpc-resp=response:rpc:jstd]
|
2020-10-19 15:21:54 +03:00
|
|
|
^- (quip card _state)
|
2020-10-20 17:13:52 +03:00
|
|
|
:: ~& >> (to-response (rpc-response [%erpc (parse-response:electrum-rpc:elib rpc-resp)]))
|
2020-10-19 15:21:54 +03:00
|
|
|
`state
|
|
|
|
::
|
|
|
|
--
|