shrub/lib/btc-provider.hoon

103 lines
2.0 KiB
Plaintext
Raw Normal View History

2020-10-20 13:44:31 +03:00
/- sur=btc-provider, *btc
2020-11-12 18:20:54 +03:00
/+ base64
2020-10-20 13:44:31 +03:00
^?
=< [sur .]
=, sur
|%
2020-11-12 18:20:54 +03:00
++ address-to-cord
|= =address ^- cord
?: ?=([%legacy *] address)
(scot %uc +.address)
+.address
::
++ address-from-cord
|= addrc=@t ^- address
?. ?| =("bc1" (scag 3 (trip addrc)))
=("tb1" (scag 3 (trip addrc)))
==
~|("legacy addresses not yet supported" !!)
[%bech32 addrc]
::
++ 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)
2020-10-20 17:13:52 +03:00
::
2020-11-12 18:20:54 +03:00
++ to-hash256
|= h=@t
(hash256 [32 (to-hex h)])
::
2020-11-13 13:14:35 +03:00
++ get-request
2020-11-12 18:20:54 +03:00
|= url=@t
^- request:http
[%'GET' url ~ ~]
2020-10-20 17:13:52 +03:00
::
++ gen-request
2020-11-12 18:20:54 +03:00
|= [=host-info ract=action:rpc]
2020-10-20 17:13:52 +03:00
^- request:http
2020-11-13 13:14:35 +03:00
%+ rpc-action-to-http
api-url.host-info ract
2020-10-20 17:13:52 +03:00
::
2020-10-20 13:44:31 +03:00
++ to-response
2020-10-23 20:35:04 +03:00
|= result:rpc
^- result
*result
2020-11-13 13:14:35 +03:00
++ parse-response
|= res=response:rpc:jstd
|^ ^- response:rpc
~| -.res
:: ignores RPC responses of %error, %fails and %batch
::
?> ?=(%result -.res)
?+ id.res ~|([%unsupported-response id.res] !!)
%get-address-info
[id.res (address-info res.res)]
2020-11-12 18:20:54 +03:00
::
2020-11-13 13:14:35 +03:00
%get-block-count
[id.res (ni:dejs:format res.res)]
==
++ address-info
%- ot:dejs:format
:~ [%utxos (as:dejs:format utxo)]
[%used bo:dejs:format]
[%blockcount ni:dejs:format]
2020-11-12 18:20:54 +03:00
==
2020-11-13 13:14:35 +03:00
++ 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]
2020-11-12 18:20:54 +03:00
==
2020-11-13 13:14:35 +03:00
--
::
++ rpc-action-to-http
|= [endpoint=@t ract=action:rpc]
|^ ^- request:http
%- get-request
?- -.ract
%get-address-info
(mk-url '/addresses/info/' `address.ract)
::
%get-block-count
(mk-url '/getblockcount' ~)
==
++ mk-url
|= [base=@t uaddr=(unit address)]
=/ addr=@t
?~ uaddr '' (address-to-cord u.uaddr)
%^ cat 3
(cat 3 endpoint base) addr
2020-11-12 18:20:54 +03:00
--
2020-10-20 13:44:31 +03:00
--
2020-11-13 13:14:35 +03:00