re-worked as provider; parse out ElectRS response

This commit is contained in:
timlucmiptev 2020-10-19 15:21:54 +03:00 committed by ixv
parent 7789dde31a
commit 4c385796c8
10 changed files with 295 additions and 28 deletions

View File

@ -27,8 +27,7 @@
++ on-init
^- (quip card _this)
~& > '%btc-bridge initialized successfully'
:- ~
this(status [%client connected=%.n host=~])
`this(status [%client connected=%.n host=~])
++ on-save
^- vase
!>(state)
@ -51,11 +50,15 @@
(handle-rpc-action !<(rpc-action vase))
==
[cards this]
::
++ on-watch on-watch:def
:: TODO: handle /clients path here
:: only allow if we're a host
:: only allow if it's a valid client
::
++ on-leave on-leave:def
++ on-peek on-peek:def
++ on-agent on-agent:def
:: TODO: add our host to status if we're a client when subscription is ack'd here
++ on-arvo
|= [=wire =sign-arvo]
^- (quip card _this)
@ -83,16 +86,20 @@
?- -.comm
%become-host
`state(credentials credentials.comm, status [%host connected=%.y clients=*(set ship)])
::
%connect-as-client
:: TODO send a subscription to the btc-bridge host
:: update status in on-agent when ack'd by BTC-BRIDGE
`state
:_ state
~[[%pass /connect-client %agent [host.comm %btc-provider] %watch /clients]]
::
%allow-clients
?+ -.status ~&(>>> 'Only a %host can add clients' `state)
%host
`state(clients.status (~(uni in clients.status) users.comm))
==
==
++ is-client
|= user=ship ^- ?
%.n
++ btc-rpc-auth-header
=* user rpc-user.bc.credentials
=* pass rpc-password.bc.credentials

233
app/btc-provider.hoon Normal file
View File

@ -0,0 +1,233 @@
:: btc-provider.hoon
:: Proxy that serves a BTC full node and ElectRS address indexer
::
/- *btc-provider, bnh=btc-node-hook
/+ dbug, default-agent, base64, blib=btc-node-json, elib=electrum-rpc-http
|%
+$ versioned-state
$% state-0
==
::
+$ state-0 [%0 =status whitelist=(set ship)]
::
+$ 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'
`this(status [*credentials connected=%.n clients=*(set ship)], whitelist *(set ship))
++ on-save
^- vase
!>(state)
++ on-load
|= old-state=vase
^- (quip card _this)
~& > '%btc-provider recompiled successfully'
`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
?+ mark (on-poke:def mark vase)
%btc-provider-command
(handle-command:hc !<(command vase))
%btc-provider-rpc-action
(handle-rpc-action !<(rpc-action vase))
==
[cards this]
++ on-watch
|= pax=path
^- (quip card _this)
~& >> pax
`this
:: ?> (is-whitelisted:hc src.bowl)
:: ~& > "added client {<src.bowl>}"
:: :_ this(clients.status (~(put in clients.status) src.bowl))
:: ~[[%give %fact ~ [%btc-provider-update !>([%status connected.status])]]]
::
++ on-leave on-leave:def
++ on-peek on-peek:def
++ on-agent on-agent:def
++ on-arvo
|= [=wire =sign-arvo]
^- (quip card _this)
=* response client-response.sign-arvo
=^ cards state
?+ +<.sign-arvo (on-arvo:def wire sign-arvo)
%http-response
?+ wire (on-arvo:def wire sign-arvo)
[%erpc *]
(electrum-http-response:hc wire response)
[%brpc *]
(btc-http-response:hc wire response)
==
==
[cards this]
::
++ on-fail on-fail:def
--
:: helper core
|_ =bowl:gall
++ handle-command
|= comm=command
^- (quip card _state)
?- -.comm
%set-credentials
`state(status [creds.comm connected=%.y clients=*(set ship)])
::
%whitelist-clients
`state(whitelist (~(uni in whitelist) clients.comm))
==
++ is-whitelisted
|= user=ship ^- ?
?| (~(has in whitelist) user)
=(our.bowl user)
==
++ is-client
|= user=ship ^- ?
(~(has in clients.status) user)
++ btc-rpc-auth-header
=* user rpc-user.bc.creds.status
=* pass rpc-password.bc.creds.status
:- 'Authorization'
;: (cury cat 3)
'Basic '
%- ~(en base64 | &)
(as-octs:mimes:html :((cury cat 3) user ':' pass))
==
++ btc-gen-request
|= req=request:bitcoin-core:rpc
^- request:http
=* endpoint rpc-url.bc.creds.status
=/ body=request:rpc:jstd
(request-to-rpc:btc-rpc:blib req)
=/ =header-list:http
:~ ['Content-Type' 'application/json']
btc-rpc-auth-header
==
:* %'POST'
endpoint
header-list
=, html
%- some
%- as-octt:mimes
(en-json (request-to-json:rpc:jstd body))
==
::
++ electrum-gen-request
|= req=request:electrum:rpc
%+ request-to-http:electrum-rpc:elib
rpc-url.ec.creds.status req
++ gen-request
|= ract=rpc-action
^- request:http
?- -.ract
%erpc
(electrum-gen-request +.ract)
%brpc
(btc-gen-request +.ract)
==
::
++ handle-rpc-action
|= ract=rpc-action
^- (quip card _state)
=/ out-wire=path
/[-.ract]/[(scot %da now.bowl)]
=/ out *outbound-config:iris
=/ req=request:http
(gen-request ract)
:_ state
[%pass out-wire %arvo %i %request req out]~
::
++ 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)
::
++ 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)]
== ==
::
++ btc-http-response
|= [=wire response=client-response:iris]
^- (quip card _state)
?. ?=(%finished -.response)
[~ state]
=* status status-code.response-header.response
=/ rpc-resp=response:rpc:jstd
%- httr-to-rpc-response
%+ to-httr:iris
response-header.response
full-file.response
?. ?=([%result *] rpc-resp)
~& [%error +.rpc-resp]
[~ state]
~& > (parse-response:btc-rpc:blib rpc-resp)
[~ state]
::
++ electrum-http-response
|= [=wire response=client-response:iris]
^- (quip card _state)
?. ?=(%finished -.response)
`state
=* status-code status-code.response-header.response
=/ rpc-resp=response:rpc:jstd
%- httr-to-rpc-response
%+ to-httr:iris
response-header.response
full-file.response
~& > rpc-resp
`state
::
--

View File

@ -1,6 +1,7 @@
:: btc-wallet.hoon
:: Manages wallet pubkeys
::
/- *btc-wallet
/+ shoe, dbug, default-agent
|%
+$ versioned-state
@ -54,6 +55,9 @@
++ on-leave on-leave:def
++ on-peek on-peek:def
++ on-agent on-agent:def
:: TODO: add our provider to status if we're a client when subscription is ack'd here
++ on-arvo on-arvo:def
++ on-fail on-fail:def
--
:: ~[[%pass /connect-client %agent [host.comm %btc-provider] %watch /clients]]

View File

@ -3,10 +3,11 @@ The below requires norsyr's fix to `decompress-point` in order to work.
## Set Credentials and Ping Servers
```
:btc-bridge|command [%become-host [rpc-url='http://localhost:8332' rpc-user='__cookie__' rpc-password='2cce52a532a078764cd9e56630603adaedfbb130ccf3b2f0a5f0dd718a5c35e0'] [rpc-url='http://localhost:50002']]
=rpc-pass '57669fe0365412f8b2152cba2aa6fbada8a9483f9f01fee24b05ed407b1c1635'
:btc-provider|command [%set-credentials [rpc-url='http://localhost:8332' rpc-user='__cookie__' rpc-pass] [rpc-url='http://localhost:50002']]
:btc-bridge|rpc-action [%brpc %get-block-count ~]
:btc-bridge|rpc-action [%erpc %get-address-balance [%bech32 'bc1q59u5epktervh6fxqay2dlph0wxu9hjnx6v8n66']]
:btc-provider|rpc-action [%brpc %get-block-count ~]
:btc-provider|rpc-action [%erpc %get-address-balance [%bech32 'bc1q59u5epktervh6fxqay2dlph0wxu9hjnx6v8n66']]
```
## Transactions

View File

@ -8,6 +8,6 @@
::
:- %say
|= $: [now=@da eny=@uvJ =beak]
[[ract=rpc-action ~] ~]
[[act=action ~] ~]
==
[%btc-bridge-rpc-action ract]
[%btc-bridge-action act]

View File

@ -1,13 +1,13 @@
:: Sends a command to the BTC Bridge
:: Sends a command to the BTC Provider
::
:: Commands:
::
::
::
/- *btc-bridge
/- *btc-provider
::
:- %say
|= $: [now=@da eny=@uvJ =beak]
[[comm=command ~] ~]
==
[%btc-bridge-command comm]
[%btc-provider-command comm]

View File

@ -0,0 +1,14 @@
:: Sends a raw RPC action to the BTC Provider
::
:: Commands:
::
::
::
/- *btc-provider
::
:- %say
|= $: [now=@da eny=@uvJ =beak]
[[ract=rpc-action ~] ~]
==
[%btc-provider-rpc-action ract]

View File

@ -1,6 +1,5 @@
/- *btc-bridge, *btc
/+ lib=btc-node-json
/- *btc-provider, *btc
|%
++ address-to-cord
|= =address ^- cord
@ -14,6 +13,9 @@
::
++ electrum-rpc
|%
++ parse-response
|= rpc-resp=@t
%.y
++ request-to-http
|= [endpoint=@t req=request:electrum:rpc]
^- request:http

View File

@ -3,21 +3,22 @@
+$ btc-credentials [rpc-url=@t rpc-user=@t rpc-password=@t]
+$ electrum-credentials [rpc-url=@t]
+$ credentials [bc=btc-credentials ec=electrum-credentials]
+$ status
$% [%host connected=? clients=(set ship)]
[%client connected=? host=(unit ship)]
==
+$ status [creds=credentials connected=? clients=(set ship)]
+$ action
$% [%check-status ~]
[%get-block-count ~]
[%balance =address]
[%transactions =address]
$% [%get-balance addresses=(set address)]
[%get-transactions addresses=(set address)]
==
+$ response
$% [%block-count count=@]
[%balance (set [=address balance=sats])]
==
+$ update :: sub updates from /clients path (connection etc.)
$% [%status connected=?]
==
::
+$ command
$% [%become-host =credentials]
[%connect-as-client host=ship]
[%allow-clients users=(set ship)]
$% [%set-credentials creds=credentials]
[%whitelist-clients clients=(set ship)]
==
+$ rpc-action
$% [%erpc request:electrum:rpc]

View File

@ -1,7 +1,12 @@
|%
+$ xpub tape
+$ action
$% [%check-status ~]
[%get-block-count ~]
[%get-balance addresses=(set address)]
[%get-transactions =address]
==
--
:: wallets
:: how many should we store?
:: seems like a list or a map keyed by xpub