Skip to content

btcjson+rpcclient: add signrawtransactionwithkey - #2677

Open
rhmatzeka wants to merge 2 commits into
btcsuite:masterfrom
rhmatzeka:rpcclient-signrawtransactionwithkey
Open

rhmatzeka wants to merge 2 commits into
btcsuite:masterfrom
rhmatzeka:rpcclient-signrawtransactionwithkey

Conversation

@rhmatzeka

Copy link
Copy Markdown

Change Description

This adds signrawtransactionwithkey to btcjson and SignRawTransactionWithKey to rpcclient. With it, a caller can sign a raw transaction against bitcoind using its own WIF keys, without loading a wallet.

It continues #1885 by @galileo-pkm, who said there that they couldn't keep working on it and invited someone to take it over. Their commit is credited with a Co-authored-by trailer. Compared with #1885:

  • It is rebased onto current master (the v2 module paths).
  • Following @allocz's review, there is one SignRawTransactionWithKey(tx, privKeysWIF, inputs, hashType) instead of three numbered variants. A nil inputs and an empty hashType leave those parameters out. When only the hash type is set, an empty prevtxs list is sent, because the parameters are positional.
  • PrivKeys is a required []string rather than an optional pointer, since bitcoind requires the key list.
  • New code wraps at 80 columns, following the lnd formatting rules mentioned in the review.

Steps to Test

go test ./btcjson ./rpcclient
  • TestWalletSvrCmds has new cases for the command with keys only, with previous outputs, and with an empty input list plus a hash type.
  • TestSignRawTransactionWithKey checks which optional parameters the client sends in each case and that the result is decoded.

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions. (not a bug fix)

Code Style and Documentation

  • The change is not insubstantial.
  • The change obeys the Code Documentation and Commenting guidelines, and lines wrap at 80.
  • Commits follow the Ideal Git Commit Structure.
  • Any new logging statements use an appropriate subsystem and logging level. (none added)

Add the signrawtransactionwithkey command and its result to btcjson, and
SignRawTransactionWithKey to rpcclient so callers can sign with their own
WIF keys against bitcoind without loading a wallet.

The client has a single method that takes the keys, the optional
previous outputs and the optional signature hash type, instead of three
numbered variants. When only the hash type is set, an empty prevtxs list
is sent because the parameters are positional.

This continues btcsuite#1885, rebased onto master with the review on it applied.

Co-authored-by: galileo-pkm <8114900+galileo-pkm@users.noreply.github.com>

@allocz allocz left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As btcd does not have wallet related features and custom RPC's can be added with btcjson.RegisterCmd I don't think that we should add this method to the rpcclient.

@rhmatzeka

Copy link
Copy Markdown
Author

Thanks for taking a look. I agree btcd itself has no wallet. rpcclient, though, is also the Go client people use against bitcoind and btcwallet, and it already covers several commands btcd's server doesn't implement: SignRawTransactionWithWallet, CreateWallet, LoadWallet, ImportPrivKey, DumpPrivKey. Strictly speaking, signrawtransactionwithkey isn't a wallet command in bitcoind. It's in the rawtransactions group and works without a wallet loaded, so it's closer to TestMempoolAccept than to those.

btcjson.RegisterCmd works, but then every downstream project has to copy the command struct, the result type and the hex/deserialize step in Receive. That's what #1885 and @DhananjayPurohit's comment there were asking to avoid.

That said, I'll go with whatever the maintainers prefer here. @guggero, would you take this in rpcclient, or should it stay out?

@allocz allocz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you think that is better to use a function call instead of network call to do this kind of operation? I don't like the idea of incentivizing users to send private keys over network calls.

@rhmatzeka

Copy link
Copy Markdown
Author

That's a fair concern. The keys do go over the RPC connection here, but that's already true for SignRawTransaction3 and SignRawTransaction4, which send privKeysWIF to the old signrawtransaction. bitcoind removed that command in 0.18 and signrawtransactionwithkey is its replacement, so this keeps the existing path working against current nodes rather than adding a new way to send keys.

I agree local signing is the better choice when the caller holds the keys, so I added a note to the SignRawTransactionWithKey doc comment in 23f1b25: the keys are sent to the server, it should only be used against a node you control over a local or encrypted connection, and txscript.SignatureScript / txscript.WitnessSignature are the way to sign without sending keys anywhere.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants