Skip to content

Commit

Permalink
commands: fix signtransaction cmd when used with "privkey" arg
Browse files Browse the repository at this point in the history
A naive "solver" is implemented locally for p2pkh-like inputs.
Other more complex scripts cannot be signed atm without a wallet.

closes #7117
  • Loading branch information
SomberNight committed Mar 24, 2021
1 parent 4b0f439 commit 638d4f7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 7 additions & 2 deletions electrum/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,16 @@ async def serialize(self, jsontx):
@command('wp')
async def signtransaction(self, tx, privkey=None, password=None, wallet: Abstract_Wallet = None):
"""Sign a transaction. The wallet keys will be used unless a private key is provided."""
# TODO this command should be split in two... (1) *_with_wallet, (2) *_with_privkey
tx = tx_from_any(tx)
if privkey:
txin_type, privkey2, compressed = bitcoin.deserialize_privkey(privkey)
pubkey = ecc.ECPrivkey(privkey2).get_public_key_bytes(compressed=compressed).hex()
tx.sign({pubkey:(privkey2, compressed)})
pubkey = ecc.ECPrivkey(privkey2).get_public_key_bytes(compressed=compressed)
for txin in tx.inputs():
if txin.address and txin.address == bitcoin.pubkey_to_address(txin_type, pubkey.hex()):
txin.pubkeys = [pubkey]
txin.script_type = txin_type
tx.sign({pubkey.hex(): (privkey2, compressed)})
else:
wallet.sign_transaction(tx, password)
return tx.serialize()
Expand Down
10 changes: 10 additions & 0 deletions electrum/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,13 @@ def test_getprivatekeyforpath(self, mock_save_db):
cmds._run('getprivatekeyforpath', ("m/0/10000",), wallet=wallet))
self.assertEqual("p2wpkh:cQAj4WGf1socCPCJNMjXYCJ8Bs5JUAk5pbDr4ris44QdgAXcV24S",
cmds._run('getprivatekeyforpath', ("m/5h/100000/88h/7",), wallet=wallet))

@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
def test_signtransaction_without_wallet(self, mock_save_db):
dummy_wallet = restore_wallet_from_text(
'9dk', gap_limit=2, path='if_this_exists_mocking_failed_648151893', config=self.config)['wallet']
cmds = Commands(config=self.config)
unsigned_tx = "70736274ff0100a0020000000221d3645ba44f33fff6fe2666dc080279bc34b531c66888729712a80b204a32a10100000000fdffffffdd7f90d51acf98dc45ad7489316a983868c75e16bf14ffeb9eae01603a7b4da40100000000fdffffff02e8030000000000001976a9149a9ec2b35a7660c80dae38dd806fdf9b0fde68fd88ac74c11000000000001976a914f0dc093f7fb1b76cfd06610d5359d6595676cc2b88aca79b1d00000100e102000000018ba8cf9f0ff0b44c389e4a1cd25c0770636d95ccef161e313542647d435a5fd0000000006a4730440220373b3989905177f2e36d7e3d02b967d03092747fe7bbd3ba7b2c24623a88538c02207be79ee1d981060c2be6783f4946ce1bda1f64671b349ef14a4a6fecc047a71e0121030de43c5ed4c6272d20ce3becf3fb7afd5c3ccfb5d58ddfdf3047981e0b005e0dfdffffff02c0010700000000001976a9141cd3eb65bce2cae9f54544b65e46b3ad1f0b187288ac40420f00000000001976a914f0dc093f7fb1b76cfd06610d5359d6595676cc2b88ac979b1d00000100e102000000014e39236158716e91b0b2170ebe9d6b359d139e9ebfff163f2bafd0bec9890d04000000006a473044022070340deb95ca25ef86c4c7a9539b5c8f7b8351941635450311f914cd9c2f45ea02203fa7576e032ab5ae4763c78f5c2124573213c956286fd766582d9462515dc6540121033f6737e40a3a6087bc58bc5b82b427f9ed26d710b8fe2f70bfdd3d62abebcf74fdffffff02e8030000000000001976a91490350959750b3b38e451df16bd5957b7649bf5d288acac840100000000001976a914f0dc093f7fb1b76cfd06610d5359d6595676cc2b88ac979b1d00000000"
privkey = "cVtE728tULSA4gut4QWxo218q6PRsXHQAv84SXix83cuvScvGd1H"
self.assertEqual("020000000221d3645ba44f33fff6fe2666dc080279bc34b531c66888729712a80b204a32a1010000006a47304402205b30e188e30c846f98dacc714c16b7cd3a58a3fa24973d289683c9d32813e24c0220153855a29e96fb083084417ba3e3873ccaeb08435dad93773ab60716f94a36160121033f6737e40a3a6087bc58bc5b82b427f9ed26d710b8fe2f70bfdd3d62abebcf74fdffffffdd7f90d51acf98dc45ad7489316a983868c75e16bf14ffeb9eae01603a7b4da4010000006a473044022010daa3dadf53bdcb071c6eff6b8787e3f675ed61feb4fef72d0bf9d99c0162f802200e73abd880b6f2ee5fe8c0abab731f1dddeb0f60df5e050a79c365bd718da1c80121033f6737e40a3a6087bc58bc5b82b427f9ed26d710b8fe2f70bfdd3d62abebcf74fdffffff02e8030000000000001976a9149a9ec2b35a7660c80dae38dd806fdf9b0fde68fd88ac74c11000000000001976a914f0dc093f7fb1b76cfd06610d5359d6595676cc2b88aca79b1d00",
cmds._run('signtransaction', (), tx=unsigned_tx, privkey=privkey, wallet=dummy_wallet))

0 comments on commit 638d4f7

Please sign in to comment.