port JSON functions

This commit is contained in:
timlucmiptev 2020-10-09 15:34:36 +03:00 committed by ixv
parent cfd0b11992
commit 5276680cba
7 changed files with 116 additions and 374 deletions

View File

@ -2,13 +2,13 @@
:: Proxy for accessing BTC full node
::
/- *btc-bridge, bnh=btc-node-hook
/+ dbug, default-agent
/+ dbug, default-agent, base64, lib=btc-node-json
|%
+$ versioned-state
$% state-0
==
::
+$ state-0 [%0 =status]
+$ state-0 [%0 =credentials =status]
::
+$ card card:agent:gall
::
@ -27,7 +27,6 @@
^- (quip card _this)
~& > '%btc-bridge initialized successfully'
:- ~
:: :- ~[[%pass /btc-node-hook/[(scot %da now.bowl)] %agent [our.bowl %btc-node-hook] %watch /responses]]
this(status [%client connected=%.n host=~])
++ on-save
^- vase
@ -41,31 +40,32 @@
|= [=mark =vase]
^- (quip card _this)
:: Only allow poke from our ship, unless we're a host
::
?> ?|((team:title our.bowl src.bowl) ?=(%host -.status))
=^ cards state
?+ mark (on-poke:def mark vase)
%btc-bridge-command
(handle-command:hc !<(command vase))
%btc-bridge-action
`state
==
[cards this]
::
++ on-watch on-watch:def
++ on-leave on-leave:def
++ on-peek on-peek:def
++ on-agent
|= [=wire =sign:agent:gall]
++ on-agent on-agent:def
++ on-arvo
|= [=wire =sign-arvo]
^- (quip card _this)
?+ -.sign (on-agent:def wire sign)
%fact
?+ -.cage.sign (on-agent:def wire sign)
%btc-node-hook-response
=/ resp=btc-node-hook-response:bnh
!<(btc-node-hook-response:bnh +.cage.sign)
~& > resp
`this
==
=* response client-response.sign-arvo
=^ cards state
?+ +<.sign-arvo (on-arvo:def wire sign-arvo)
%http-response
(http-response:hc wire response)
==
++ on-arvo on-arvo:def
[cards this]
::
++ on-fail on-fail:def
--
:: helper core
@ -74,9 +74,9 @@
|= comm=command
^- (quip card _state)
?- -.comm
%connect-as-host
%become-host
:: TODO send a subscription to the node hook; update status in on-agent when ack'd
`state
`state(credentials credentials.comm)
%connect-as-client
:: TODO send a subscription to the btc-bridge host
:: update status in on-agent when ack'd by BTC-BRIDGE
@ -87,4 +87,101 @@
`state(clients.status (~(uni in clients.status) users.comm))
==
==
++ gen-request
|= act=btc-node-hook-action:bnh
^- request:http
=* user rpc-user.credentials
=* pass rpc-password.credentials
=* endpoint rpc-url.credentials
=/ body=request:rpc:jstd
(request-to-rpc:btc-rpc:lib act)
=/ =header-list:http
:~ ['Content-Type' 'application/json']
:- 'Authorization'
;: (cury cat 3)
'Basic '
%- ~(en base64 | &)
(as-octs:mimes:html :((cury cat 3) user ':' pass))
== ==
:* %'POST'
endpoint
header-list
=, html
%- some
%- as-octt:mimes
(en-json (request-to-json:rpc:jstd body))
==
::
++ handle-action
|= act=btc-node-hook-action:bnh
^- (quip card _state)
=/ out *outbound-config:iris
=/ req=request:http
(gen-request act)
:_ state
[%pass /[(scot %da now.bowl)] %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)]
== ==
::
++ 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]
[~ state]
:: %- handle-btc-response
:: (parse-response:btc-rpc:lib rpc-resp)
::
--

View File

@ -1,157 +0,0 @@
:: btc-node-store: data store for state received from a bitcoin full node
::
:: data: scry command:
::
:: default-wallet .^(@t %gx /=btc-node-store=/default-wallet/noun)
:: n-wallets .^(@ud %gx /=btc-node-store=/n-wallets/noun)
:: [def-wallet attr] .^((unit wallet) %gx /=btc-node-store=/wallet/noun)
:: [name attr] .^((unit wallet) %gx /=btc-node-store=/wallet/<name>/noun)
::
::
/- *btc-node-store
/+ *btc-node-json, default-agent, verb
::
=> |%
::
+$ card card:agent:gall
::
+$ state
$% [%0 state-zero]
==
::
+$ state-zero
$: =wallets
default-wallet=@t
==
--
::
=| state-zero
=* state -
:: Main
::
%+ verb |
^- agent:gall
=< |_ =bowl:gall
+* this .
btc-core +>
bc ~(. btc-core bowl)
def ~(. (default-agent this %|) bowl)
::
++ on-init
^- (quip card _this)
:- ~
%_ this
wallets (~(put by wallets) [*@t *wallet])
==
::
++ on-save !>(state)
++ on-load
|= old=vase
`this(state !<(state-zero old))
::
++ on-poke
|= [=mark =vase]
^- (quip card _this)
|^
?> (team:title our.bowl src.bowl)
?+ mark (on-poke:def mark vase)
%btc-node-store-action
(store-action !<(btc-node-store-action vase))
::
%btc-node-store-command
(store-command !<(btc-node-store-command vase))
==
::
++ store-action
|= action=btc-node-store-action
^- (quip card _this)
=^ cards state
?+ -.action ~|([%unsupported-action -.action] !!)
%add-wallet (handle-add:bc +.action)
%load-wallet (handle-switch:bc +.action)
%list-wallets handle-list-wallet:bc
%update-wallet (handle-update-wallet:bc +.action)
==
[cards this]
::
++ store-command
|= command=btc-node-store-command
^- (quip card _this)
=^ cards state
?+ -.command ~|([%unsupported-command -.command] !!)
%switch-wallet (handle-switch:bc +.command)
==
[cards this]
--
++ on-watch on-watch:def
++ on-leave on-leave:def
:: +on-peek: read from app state
::
++ on-peek
|= =path
^- (unit (unit cage))
?+ path (on-peek:def path)
[%x %default-wallet ~] ``noun+!>(default-wallet)
[%x %n-wallets ~] ``noun+!>(~(wyt by wallets))
[%x %wallet @t ~] ``noun+!>((~(get by wallets) i.t.t.path))
[%x %wallet ~] ``noun+!>((~(get by wallets) default-wallet))
==
::
++ on-agent on-agent:def
++ on-arvo on-arvo:def
++ on-fail on-fail:def
--
::
|_ =bowl:gall
::
++ handle-add
|= [name=@t warning=(unit @t)]
^- (quip card _state)
?: (~(has by wallets) name)
~& "This wallet already exists..."
[~ state]
:- ~
~& "Wallet {<name>} added succesfully..."
%_ state
wallets (~(put by wallets) [name name ~])
==
::
++ handle-list-wallet
^- (quip card _state)
=/ wallet-names=(list @t)
(turn ~(tap by wallets) |=([n=@t *] n))
:_ state
:_ ~
:* %pass / %arvo %d %flog
%text "local-wallets={<`wain`wallet-names>}"
==
::
++ handle-update-wallet
|= [name=@t attrs=wallet-attr]
^- (quip card _state)
=/ w=wallet [name (some attrs)]
:- ~
%_ state
wallets
?: (~(has by wallets) name.w)
~& "The wallet exists. Updating..."
(~(put by wallets) name.w w)
::
~& "The wallet doesn't exist. Creating..."
(~(put by wallets) [name.w w])
==
::
++ handle-switch
|= name=@t
^- (quip card _state)
:_ state(default-wallet name)
:_ ~
:* %pass / %arvo %d %flog
%text
%+ weld
"New default-wallet: {<name>}"
?: (~(has by wallets) name)
""
" (wallet is not local)"
==
--

View File

@ -1,9 +0,0 @@
:: Sends an action to the BTC hook app
::
/- *btc-node-hook
::
:- %say
|= $: [now=@da eny=@uvJ =beak]
[[act=btc-node-hook-action ~] ~]
==
[%btc-node-hook-action act]

View File

@ -1,19 +0,0 @@
:: Sends a command to the BTC hook app
::
:: Commands:
::
:: [%credentials 'http://127.0.0.1:18443/' 'user' 'password']
::
/- *btc-node-hook
::
:- %say
|= $: [now=@da eny=@uvJ =beak]
[[comm=btc-node-hook-command ~] ~]
==
:- %btc-node-hook-command
?+ -.comm ~| [%unsupported-command -.comm] !!
%credentials comm
%watch comm
%unwatch comm
%ping comm
==

View File

@ -1,16 +0,0 @@
:: Sends a command to the BTC store app
::
:: Commands:
::
:: > :btc-node-store|command [%switch-wallet 'local']
::
/- *btc-node-store
::
:- %say
|= $: [now=@da eny=@uvJ =beak]
[[comm=btc-node-store-command ~] ~]
==
:- %btc-node-store-command
?+ -.comm ~|([%unsupported-command -.comm] !!)
%switch-wallet comm
==

View File

@ -7,12 +7,13 @@
==
+$ action
$% [%check-status ~]
[%get-block-count ~]
[%balance =address]
[%transactions =address]
==
::
+$ command
$% [%connect-as-host =credentials]
$% [%become-host =credentials]
[%connect-as-client host=ship]
[%allow-clients users=(set ship)]
==

View File

@ -1,155 +0,0 @@
=> :: Helper types
::
|%
++ blockhash @ux
::
+$ purpose ?(%send %receive)
+$ address
$: address=?(@uc [%bech32 @t])
script-pubkey=@ux
is-mine=?
is-watchonly=?
solvable=?
desc=(unit @t)
is-script=?
is-change=?
is-witness=?
witness-version=(unit @t)
witness-program=(unit @ux)
script=(unit @t)
hex=(unit @ux)
pubkeys=(unit (list @ux))
sigs-required=(unit @ud)
pubkey=(unit @ux)
is-compressed=(unit ?)
label=(unit @t)
timestamp=(unit @t)
hd-key-path=(unit @t)
hd-seed-id=(unit @ux)
hd-master-finger-print=(unit @ux)
labels=(list [name=@t =purpose])
==
::
+$ wallet-attr
$: wallet-version=@ud
balance=@t
unconfirmed-balance=@t
immature-balance=@t
tx-count=@ud
key-pool-oldest=@ud
key-pool-size=@ud
key-pool-size-hd-internal=(unit @ud)
unlocked-until=(unit @ud)
pay-tx-fee=@t
hd-seed-id=(unit @ux)
private-keys-enabled=?
avoid-reuse=?
scanning=?(? [duration=@t progress=@t])
==
::
+$ wallet
[name=@t attrs=(unit wallet-attr)]
::
+$ wallets (map @t wallet)
::
+$ addresses (list address)
--
|%
::
+$ btc-node-store-action action:btc-rpc
+$ btc-node-store-update update:btc-rpc
+$ btc-node-store-command command:btc-rpc
::
++ btc-rpc
|%
:: %action: the result of an RPC action from the full node
::
+$ action
$% :: [%abandon-transaction]
:: [%abort-rescan]
[%add-multisig-address ~]
:: [%backup-wallet ~]
:: [%bump-fee]
:: Adds a new wallet to the list
::
[%add-wallet name=@t warning=(unit @t)]
:: [%dump-privkey ~]
:: [%dump-wallet ~]
:: [%encrypt-wallet ~]
:: [%get-addresses-by-label]
:: [%get-address-info]
:: [%get-balance]
:: [%get-new-address]
:: [%get-raw-change-address]
:: [%get-received-by-address]
:: [%get-received-by-label]
:: [%get-transaction]
:: [%get-unconfirmed-balance]
:: [%get-wallet-info ~] -> This is replaced by %update
[%update-wallet name=@t attrs=wallet-attr]
[%import-address ~]
[%import-multi ~]
[%import-privkey ~]
[%import-pruned-funds ~]
[%import-pubkey ~]
[%import-wallet ~]
[%key-pool-refill ~]
:: [%list-address-groupings]
:: [%list-labels]
:: [%list-lock-unspent]
:: [%list-received-by-address]
:: [%list-received-by-label]
:: [%lists-in-ceblock]
:: [%list-transactions]
:: [%list-unspent]
:: [%list-wallet-dir ~]
[%list-wallets ~]
[%load-wallet name=@t]
:: [%lock-unspent]
:: [%remove-pruned-funds ~]
:: [%rescan-blockchain ~]
:: [%send-many ~]
:: [%send-to-address ~]
:: [%set-hd-seed ~]
:: [%set-label ~]
:: [%set-tx-fee ~]
:: [%sign-message ~]
:: [%sign-raw-transaction-with-wallet ~]
:: [%unload-wallet ~]
:: [%wallet-create-fundedpsbt ~]
:: [%wallet-lock ~]
:: [%wallet-passphrase ~]
:: [%wallet-passphrase-change ~]
:: [%wallet-process-psbt ~]
:: [%get-zmq-notifications]
==
::
:: %update: modifies data on the %store app
::
+$ update
$% :: Wallet name has changed
::
[%wallet-name name=@t]
:: Updates wallet attributes
:: FIXME: all attrs might need to be units...
::
[%wallet-attrs name=@t attr=wallet-attr]
==
::
:: %command: instruction to perform over the stored data
::
+$ command
$% :: Updated the default wallet stored in btc-node-store
::
[%switch-wallet @t]
::
:: Loads an external wallet
::
[%load-wallet @t]
::
:: TODO: do the actual syncing
::
[%sync ~]
==
--
--