From 87b7b230f66525df8c6df40c6eb21996895ae016 Mon Sep 17 00:00:00 2001 From: timlucmiptev Date: Tue, 13 Oct 2020 12:25:37 +0300 Subject: [PATCH] move Electrum to GET --- app/btc-bridge.hoon | 57 +++++++++++++++++++++++--------------- bippy-scratch.md | 8 ++++++ lib/electrum-rpc-json.hoon | 21 ++++++++++++++ sur/btc-bridge.hoon | 16 ++++++----- 4 files changed, 73 insertions(+), 29 deletions(-) create mode 100644 lib/electrum-rpc-json.hoon diff --git a/app/btc-bridge.hoon b/app/btc-bridge.hoon index 8095f7d3d..f96465b94 100644 --- a/app/btc-bridge.hoon +++ b/app/btc-bridge.hoon @@ -2,7 +2,7 @@ :: Proxy for accessing BTC full node :: /- *btc-bridge, bnh=btc-node-hook -/+ dbug, default-agent, base64, lib=btc-node-json +/+ dbug, default-agent, base64, blib=btc-node-json |% +$ versioned-state $% state-0 @@ -47,11 +47,7 @@ %btc-bridge-command (handle-command:hc !<(command vase)) %btc-bridge-rpc-action - =/ act=rpc-action !<(rpc-action vase) - ?+ -.act (on-poke:def mark vase) - %brpc - (handle-action +.act) - == + (handle-rpc-action !<(rpc-action vase)) == [cards this] :: @@ -90,22 +86,25 @@ `state(clients.status (~(uni in clients.status) users.comm)) == == -++ gen-request - |= act=btc-node-hook-action:bnh +++ btc-rpc-auth-header + =* user rpc-user.bc.credentials + =* pass rpc-password.bc.credentials + :- 'Authorization' + ;: (cury cat 3) + 'Basic ' + %- ~(en base64 | &) + (as-octs:mimes:html :((cury cat 3) user ':' pass)) + == +++ gen-btc-request + |= req=request:bitcoin-core:rpc ^- request:http - =* user rpc-user.credentials - =* pass rpc-password.credentials - =* endpoint rpc-url.credentials + =* endpoint rpc-url.bc.credentials =/ body=request:rpc:jstd - (request-to-rpc:btc-rpc:lib act) + (request-to-rpc:btc-rpc:blib req) =/ =header-list:http :~ ['Content-Type' 'application/json'] - :- 'Authorization' - ;: (cury cat 3) - 'Basic ' - %- ~(en base64 | &) - (as-octs:mimes:html :((cury cat 3) user ':' pass)) - == == + btc-rpc-auth-header + == :* %'POST' endpoint header-list @@ -115,12 +114,26 @@ (en-json (request-to-json:rpc:jstd body)) == :: -++ handle-action - |= act=btc-node-hook-action:bnh +++ gen-electrum-request + |= req=request:electrum:rpc + ~& >>> req + *request:http +++ gen-request + |= ract=rpc-action + ^- request:http + ?- -.ract + %erpc + (gen-electrum-request +.ract) + %brpc + (gen-btc-request +.ract) + == +:: +++ handle-rpc-action + |= ract=rpc-action ^- (quip card _state) =/ out *outbound-config:iris =/ req=request:http - (gen-request act) + (gen-request ract) :_ state [%pass /[(scot %da now.bowl)] %arvo %i %request req out]~ :: @@ -184,7 +197,7 @@ ?. ?=([%result *] rpc-resp) ~& [%error +.rpc-resp] [~ state] - ~& > (parse-response:btc-rpc:lib rpc-resp) + ~& > (parse-response:btc-rpc:blib rpc-resp) [~ state] :: -- diff --git a/bippy-scratch.md b/bippy-scratch.md index e2913c97e..e047f324f 100644 --- a/bippy-scratch.md +++ b/bippy-scratch.md @@ -1,6 +1,14 @@ ## NOTE 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='b813c98bde1d4489e2889269559d1f42920fb367c59ed43eb7668ceeabde2272'] [rpc-url='http://localhost:50001']] + +:btc-bridge|rpc-action [%brpc %get-block-count ~] +:btc-bridge|rpc-action [%erpc %get-balance-scripthash 'c5d92092c3d6793e159d93a195c4b12b4110d2b4721862b1e208ae206858c25c'] +``` + ## Working with BTC RPC Library ``` |start :btc-bridge diff --git a/lib/electrum-rpc-json.hoon b/lib/electrum-rpc-json.hoon new file mode 100644 index 000000000..11ec0c759 --- /dev/null +++ b/lib/electrum-rpc-json.hoon @@ -0,0 +1,21 @@ +/- *btc-bridge +/+ lib=btc-node-json +:: dummy core until needed +|% +++ blank 42 +:: TODO: generate GET URLS here, or even the whole http request!! +:: ++ electrum-rpc +:: |% +:: ++ request-to-rpc +:: =, enjs:format +:: |= req=request:electrum:rpc +:: ^- request:rpc:jstd +:: ?- -.req +:: %get-address-balance +:: :* id='0' +:: method='blockchain.scripthash.get_balance' +:: params=[%list ~[[%s address.req]]] +:: == +:: == +:: -- +-- diff --git a/sur/btc-bridge.hoon b/sur/btc-bridge.hoon index 4a3277728..65ab354ab 100644 --- a/sur/btc-bridge.hoon +++ b/sur/btc-bridge.hoon @@ -1,7 +1,8 @@ /- *btc, bnh=btc-node-hook - |% -+$ credentials [rpc-url=@t rpc-user=@t rpc-password=@t] ++$ 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)] @@ -19,20 +20,21 @@ [%allow-clients users=(set ship)] == +$ rpc-action - $% [%erpc action:electrum:rpc] - [%brpc action:bitcoin-core:rpc] + $% [%erpc request:electrum:rpc] + [%brpc request:bitcoin-core:rpc] == ++ rpc |% ++ electrum |% - +$ action - $% [%timluc ~] + +$ request + $% [%get-address-balance =address] == -- ++ bitcoin-core |% - +$ action btc-node-hook-action:bnh + +$ request btc-node-hook-action:bnh -- -- -- +