Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using 'value_sats' in inputs for 'serialize' method in JSON-RPC API #8265

Closed
meglio opened this issue Mar 19, 2023 · 2 comments
Closed

Comments

@meglio
Copy link

meglio commented Mar 19, 2023

I encountered an issue when using the 'value_sats' field in the inputs for the 'serialize' method in the Electrum JSON-RPC API.

Steps to reproduce:

Construct a JSON-RPC call to the 'serialize' method with the following structure:

{
  "jsonrpc": "2.0",
  "id": "example",
  "method": "serialize",
  "params": [{
    "inputs": [
      {
        "prevout_hash": "...",
        "prevout_n": 0,
        "redeemPubkey": "...",
        "value_sats": 99
      }
    ],
    "outputs": [
      {"address": "...", "value": 2500},
      {"address": "...", "value": 200}
    ]
  }]
}
  1. Send the JSON-RPC request to the Electrum server.

Expected behavior:

The request should be processed successfully, and the 'value_sats' field should be used as the value in satoshis for the input.

Actual behavior:

The request fails with the following error message:

{
  "id": "example",
  "jsonrpc": "2.0",
  "error": {
    "code": 1,
    "message": "'value_sats'"
  }
}

The same error occurs when using the 'value' field instead of 'value_sats'. According to the Electrum source code, the line txin._trusted_value_sats = int(txin_dict.get('value', txin_dict['value_sats'])) should handle both value and value_sats fields in the inputs. However, it appears that neither of these fields is being processed correctly, causing the error.

Could you please provide guidance on how to properly use the 'value_sats' (or 'value') field in the inputs for the 'serialize' method in the Electrum JSON-RPC API.

Also, it would be helpful to make the error message that Electrum outputs more descriptive.

@SomberNight SomberNight self-assigned this Mar 19, 2023
@SomberNight
Copy link
Member

Also, it would be helpful to make the error message that Electrum outputs more descriptive.

Note that full stack traces are logged by the daemon electrum process. E.g.:

  6.65 | E | daemon.CommandsServer | internal error while executing RPC
Traceback (most recent call last):
  File "/home/user/wspace/electrum/electrum/daemon.py", line 238, in handle
    response['result'] = await f(**params)
  File "/home/user/wspace/electrum/electrum/commands.py", line 155, in func_wrapper
    return await func(*args, **kwargs)
  File "/home/user/wspace/electrum/electrum/commands.py", line 410, in serialize
    outputs = [PartialTxOutput.from_address_and_value(txout['address'], int(txout.get('value') or txout['value_sats']))
  File "/home/user/wspace/electrum/electrum/commands.py", line 410, in <listcomp>
    outputs = [PartialTxOutput.from_address_and_value(txout['address'], int(txout.get('value') or txout['value_sats']))
KeyError: 'value_sats'

logged here:

electrum/electrum/daemon.py

Lines 241 to 246 in 8b0a694

except BaseException as e:
self.logger.exception("internal error while executing RPC")
response['error'] = {
'code': 1,
'message': str(e),
}

@meglio
Copy link
Author

meglio commented Mar 20, 2023

Thanks @SomberNight for looking at it quickly.

While I do appreciate the logging, it is not what a regular user would try to find, read, and understand, if an error occurs while they are using Electrum through command line.

Although I can see you wrapped the value_sats retrieval in a try/catch block and added a more useful error message. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants