btc-provider lib

This commit is contained in:
timlucmiptev 2020-10-20 13:44:31 +03:00 committed by ixv
parent 9dfe6210d2
commit dc09351e0e
7 changed files with 91 additions and 44 deletions

View File

@ -1,9 +1,8 @@
:: btc-provider.hoon
:: 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
/+ *btc-provider, dbug, default-agent, base64, blib=btc-node-json, elib=electrum-rpc
|%
+$ versioned-state
$% state-0
@ -225,7 +224,7 @@
++ electrum-http-response
|= [status=@ud rpc-resp=response:rpc:jstd]
^- (quip card _state)
~& > rpc-resp
~& >> (parse-response:electrum-rpc:elib rpc-resp)
`state
::
--

View File

@ -3,7 +3,7 @@ The below requires norsyr's fix to `decompress-point` in order to work.
## Set Credentials and Ping Servers
```
=rpc-pass '22b159ae3ebfdc3404b446fc61df4760de0fece271586dfb5039e1260cb15775'
=rpc-pass '302a586ef1d1d4d93f22b1c69cadfdfc8323b984c0f469356529b1a8fdd2558e'
:btc-provider|command [%set-credentials [rpc-url='http://localhost:8332' rpc-user='__cookie__' rpc-pass] [rpc-url='http://localhost:50002']]
:btc-provider|rpc-action [%brpc %get-block-count ~]

10
lib/btc-provider.hoon Normal file
View File

@ -0,0 +1,10 @@
/- sur=btc-provider, *btc
^?
=< [sur .]
=, sur
|%
++ to-response
|= response:rpc
^- response:sur
[%get-address-info *address-info]
--

View File

@ -1,35 +0,0 @@
/- *btc-provider, *btc
|%
++ address-to-cord
|= =address ^- cord
?: ?=([%legacy *] address)
(scot %uc +.address)
+.address
++ http-request
|= url=@t
^- request:http
[%'GET' url ~ ~]
::
++ electrum-rpc
|%
++ parse-response
|= rpc-resp=@t
%.y
++ request-to-http
|= [endpoint=@t req=request:electrum:rpc]
^- request:http
%- http-request
?- -.req
%get-address-balance
%^ cat 3
(cat 3 endpoint '/addresses/balance/')
(address-to-cord address.req)
::
%get-address-utxos
%^ cat 3
(cat 3 endpoint '/addresses/listunspent/')
(address-to-cord address.req)
==
--
--

68
lib/electrum-rpc.hoon Normal file
View File

@ -0,0 +1,68 @@
/- *btc-provider, *btc
|%
++ address-to-cord
|= =address ^- cord
?: ?=([%legacy *] address)
(scot %uc +.address)
+.address
++ to-hex
|= h=@t
^- @ux
?: =('' h) 0x0
:: Add leading 00
::
=+ (lsh 3 2 h)
:: Group by 4-size block
::
=+ (rsh 3 2 -)
:: Parse hex to atom
::
`@ux`(rash - hex)
++ to-hash256
|= h=@t
(hash256 [32 (to-hex h)])
++ http-request
|= url=@t
^- request:http
[%'GET' url ~ ~]
::
++ electrum-rpc
|%
++ parse-response
|= res=response:rpc:jstd
|^ ^- response:electrum:rpc
~| -.res
:: only deals with successful requests
:: ignores (%error, %fails and %batch)
::
?> ?=(%result -.res)
?+ id.res ~|([%unsupported-response id.res] !!)
%get-address-utxos
[id.res ((as:dejs:format utxo) res.res)]
==
++ utxo
%- ot:dejs:format
:~ ['tx_pos' ni:dejs:format]
['tx_hash' (cu:dejs:format to-hash256 so:dejs:format)]
[%height ni:dejs:format]
[%value ni:dejs:format]
==
--
::
++ request-to-http
|= [endpoint=@t req=request:electrum:rpc]
^- request:http
%- http-request
?- -.req
%get-address-balance
%^ cat 3
(cat 3 endpoint '/addresses/balance/')
(address-to-cord address.req)
::
%get-address-utxos
%^ cat 3
(cat 3 endpoint '/addresses/utxos/')
(address-to-cord address.req)
==
--
--

View File

@ -574,10 +574,7 @@
+$ btc-node-hook-command command:btc-rpc
:: Expand the response type to include connectivity
::
+$ btc-node-hook-response
$% [%status connected=? status-code=@ud]
response:btc-rpc
==
+$ btc-node-hook-response response:btc-rpc
::
++ btc-rpc
|%

View File

@ -25,6 +25,10 @@
$% [%erpc request:electrum:rpc]
[%brpc request:bitcoin-core:rpc]
==
+$ rpc-response
$% [%erpc response:electrum:rpc]
[%brpc response:bitcoin-core:rpc]
==
++ rpc
|%
++ electrum
@ -33,10 +37,14 @@
$% [%get-address-balance =address]
[%get-address-utxos =address]
==
+$ response
$% [%get-address-utxos utxos=(set utxo)]
==
--
++ bitcoin-core
|%
+$ request btc-node-hook-action:bnh
+$ response btc-node-hook-response:bnh
--
--
--