w btc-node

This commit is contained in:
timlucmiptev 2020-10-02 08:48:58 +03:00 committed by ixv
parent 129fe493ba
commit ec6b540483
8 changed files with 5559 additions and 0 deletions

358
app/btc-node-hook.hoon Normal file
View File

@ -0,0 +1,358 @@
:: btc-node-hook: send JSON rpc requests to bitcoin full node
:: and poke the responses into the btc-node-store
::
/- *btc-node-hook, *btc-node-store, sole
/+ default-agent, sole, base64, lib=btc-node-json, verb
::
=> |%
+$ card card:agent:gall
+$ versioned-state
$% [%0 state-zero]
==
::
+$ state-zero
$: user=@t
pass=@t
endpoint=@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(user '', pass '', endpoint '')]
::
++ on-save !>(state)
++ on-load
|= old=vase
`this(state !<(state-zero old))
::
++ on-poke
|= [=mark =vase]
^- (quip card _this)
=^ cards state
?+ mark (on-poke:def mark vase)
%btc-node-hook-action
(handle-action:bc !<(btc-node-hook-action vase))
::
%btc-node-hook-command
(handle-command:bc !<(btc-node-hook-command vase))
==
[cards this]
::
++ on-watch on-watch:def
++ 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 (http-response:bc wire response)
==
[cards this]
::
++ on-fail on-fail:def
--
::
|_ =bowl:gall
::
++ handle-action
|= act=btc-node-hook-action
^- (quip card _state)
=/ 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))
== ==
=/ req=request:http
:* %'POST'
(endpoint-url act)
header-list
=, html
%- some
%- as-octt:mimes
(en-json (request-to-json:rpc:jstd body))
==
=/ out *outbound-config:iris
:_ state
[%pass /[(scot %da now.bowl)] %arvo %i %request req out]~
::
++ handle-command
|= comm=btc-node-hook-command
^- (quip card _state)
?+ -.comm ~| [%unsupported-hook-command -.comm] !!
%credentials
:_ state(endpoint url.comm, user user.comm, pass pass.comm)
[%pass / %arvo %d %flog [%text "credentials updated..."]]~
==
::
++ 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]
%- handle-btc-response
(parse-response:btc-rpc:lib rpc-resp)
::
++ handle-btc-response
|= btc-resp=btc-node-hook-response
^- (quip card _state)
:_ state
^- (list card)
?+ -.btc-resp
:: By default we just print all RPC responses that are not
:: considered here explicitly for proper format printing or
:: for being passed on to the store app.
::
~&(btc-resp ~)
::
:: %abandon-transaction
:: %abort-rescan
:: %add-multisig-address
:: %backup-wallet
:: %bump-fee
%create-wallet
=/ btc-store-req=btc-node-store-action
:+ %add-wallet name.btc-resp
?:(=('' warning.btc-resp) ~ (some warning.btc-resp))
[(btc-node-store-poke /store btc-store-req)]~
::
:: %dump-privkey
:: %dump-wallet
:: %encrypt-wallet
:: %get-addresses-by-label
::
%get-address-info
~&([%address-info +.btc-resp] ~)
::
%get-balance
~&([%amount (trip +.btc-resp)] ~)
::
:: %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
^- (list card)
=/ btc-store-req=btc-node-store-action
[%update-wallet wallet-name.btc-resp +>:btc-resp]
[(btc-node-store-poke /update btc-store-req)]~
::
:: %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
~&([%transactions +.btc-resp] ~)
::
:: %list-unspent
:: %list-wallet-dir
::
%list-wallets
^- (list card)
:~ (btc-node-store-poke /list-wallets [%list-wallets ~])
:* %pass / %arvo %d %flog
%text "remote-wallets={<`wain`wallets.btc-resp>}"
== ==
::
%load-wallet
[(btc-node-store-poke /load [%load-wallet name.btc-resp])]~
::
:: %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
==
::
++ btc-node-store-poke
|= [=wire act=btc-node-store-action]
^- card
:* %pass
wire
%agent
[our.bowl %btc-node-store]
%poke
[%btc-node-store-action !>(act)]
==
::
++ default-wallet
.^ @t
%gx
(scot %p our.bowl)
%btc-node-store
(scot %da now.bowl)
/default-wallet/noun
==
::
++ n-wallets
.^ @ud
%gx
(scot %p our.bowl)
%btc-node-store
(scot %da now.bowl)
/n-wallets/noun
==
::
++ endpoint-url
|= [act=btc-node-hook-action]
^- @t
?. ?| ?=(%abandon-transaction -.act)
?=(%abort-rescan -.act)
?=(%add-multisig-address -.act)
?=(%backup-wallet -.act)
?=(%bump-fee -.act)
?=(%dump-privkey -.act)
?=(%dump-wallet -.act)
?=(%encrypt-wallet -.act)
?=(%fund-raw-transaction -.act)
?=(%get-balance -.act)
?=(%get-balances -.act)
?=(%get-addresses-by-label -.act)
?=(%get-address-info -.act)
?=(%get-new-address -.act)
?=(%get-raw-change-address -.act)
?=(%get-received-by-address -.act)
?=(%get-received-by-label -.act)
?=(%get-transaction -.act)
?=(%get-unconfirmed-balance -.act)
?=(%get-wallet-info -.act)
?=(%import-address -.act)
?=(%import-multi -.act)
?=(%import-privkey -.act)
?=(%import-pruned-funds -.act)
?=(%import-pubkey -.act)
?=(%import-wallet -.act)
?=(%key-pool-refill -.act)
?=(%list-address-groupings -.act)
?=(%list-labels -.act)
?=(%list-lock-unspent -.act)
?=(%list-received-by-address -.act)
?=(%list-received-by-label -.act)
?=(%lists-in-ceblock -.act)
?=(%list-transactions -.act)
?=(%list-unspent -.act)
?=(%lock-unspent -.act)
?=(%remove-pruned-funds -.act)
?=(%rescan-blockchain -.act)
?=(%send-many -.act)
?=(%send-to-address -.act)
?=(%set-hd-seed -.act)
?=(%set-label -.act)
?=(%set-tx-fee -.act)
?=(%sign-message -.act)
?=(%sign-raw-transaction-with-wallet -.act)
?=(%wallet-create-fundedpsbt -.act)
?=(%wallet-lock -.act)
?=(%wallet-passphrase -.act)
?=(%wallet-passphrase-change -.act)
?=(%wallet-process-psbt -.act)
==
endpoint
;: (cury cat 3)
endpoint
'wallet/'
::
?: ?=([?(%dump-wallet %import-wallet) filename=@t] act)
filename.act
default-wallet
==
--

156
app/btc-node-store.hoon Normal file
View File

@ -0,0 +1,156 @@
:: 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)
|^
?+ 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

@ -0,0 +1,9 @@
:: 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

@ -0,0 +1,16 @@
:: 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
==

View File

@ -0,0 +1,16 @@
:: 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
==

2821
lib/btc-node-json.hoon Normal file

File diff suppressed because it is too large Load Diff

2028
sur/btc-node-hook.hoon Normal file

File diff suppressed because it is too large Load Diff

155
sur/btc-node-store.hoon Normal file
View File

@ -0,0 +1,155 @@
=> :: 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 ~]
==
--
--