checksum0 / go-electrum

A pure Go ElectrumX JSON-RPC library.
MIT License
27 stars 26 forks source link

Handle error string returned in the JSON-RPC response #8

Closed nkuba closed 1 year ago

nkuba commented 1 year ago

Closes: https://github.com/checksum0/go-electrum/issues/5

Here we provide a workaround for servers that don't follow the JSON-RPC 2.0 specification for error objects.

According to the specification, an error should be an object containing code, message, and data properties (see: https://www.jsonrpc.org/specification#error_object). Unfortunately, Electrs returns an error as a string (see: https://github.com/Blockstream/esplora/issues/453).

We define an error unmarshaling function to handle both types of errors.

Sample outputs from servers that the client has to handle: ElectrumX

✗ echo '{"jsonrpc": "2.0", "method": "blockchain.block.header", "params": [4294967295], "id": 0}' | netcat 49.12.127.114 10068
{"jsonrpc":"2.0","error":{"code":1,"message":"height 4,294,967,295 out of range"},"id":0}

Fulcrum

✗ echo '{"jsonrpc": "2.0", "method": "blockchain.block.header", "params": [4294967295], "id": 0}' | netcat 203.132.94.196 51001
{"error":{"code":1,"message":"Invalid height"},"id":0,"jsonrpc":"2.0"}

Esplora/Electrs

✗ echo '{"jsonrpc": "2.0", "method": "blockchain.block.header", "params": [4294967295], "id": 0}' | netcat 35.225.54.191 50001
{"error":"missing header","id":0,"jsonrpc":"2.0"}
nkuba commented 1 year ago

@checksum0 will you be interested in merging this PR? We confirmed errors are handled correctly with integration tests in https://github.com/keep-network/keep-core/pull/3567.

nkuba commented 1 year ago

Closing in favor of https://github.com/checksum0/go-electrum/pull/9.