Author

Topic: How can I sign a transaction with a private key on electrum console (Read 187 times)

legendary
Activity: 2534
Merit: 6080
Self-proclaimed Genius
And... it's now working in the latest version 4.1.0.
But still wont work if you're using a SegWit wallet which is the default in that version (and the only option for new wallets in the GUI).

Sign (from 'tb1qw2es79xv7c8fyjtzuvfw40g6zxxun7puwtqlp0'):
Code:
signtransaction("cHNidP8BAFICAAAAAQyLfD2ldunBzv7gYokHsgSyVJVJ1M0OB2XyclWe1UGNAAAAAAD9////AVokAAAAAAAAFgAUiVyehjAcphkvodfumT9bcTbRVzUiDh4AAAEA4QIAAAAAAQEr4Vm9SXSNqt+1fmZajpKmcvsm9wtk40sO7LifvJ1AhgEAAAAA/f///wKAJgAAAAAAABYAFHKzDxTM9g6SSWLjEuq9GhGNyfg8ECcAAAAAAAAZdqkUmp7Cs1p2YMgNrjjdgG/fmw/eaP2IrAJHMEQCIFMl4t7a86EnmxiBNob6xv6RBMsWiw+b0YMXeWWA/BhOAiABqjZkZLWeKxpm4RPv34o9JWvuDE26aBdf+K4aCvpVhgEhAmgC3Eoj32CNfhENLd8lZBDmhHOGIsBi8g45NdSJ65KZxZsdACIGAoMHnvutArD1dSRqFZxZFyyh/HW7Gdl6j7tc+iTyGCimEB+KRbUAAACAAQAAACkAAAAAIgIDoexWuGiz0Il3NVfg1fCnIiGz8h4okUhIYCDKQI3vbjUQH4pFtQAAAIAAAAAAAAAAAAA=", privkey="cUoYdz49ZCG2UijNrwisrJESZgKvBrS15F9Q7brQrVLu9jZ3jzSY")

Result (still unsigned):
Code:
"cHNidP8BAFICAAAAAQyLfD2ldunBzv7gYokHsgSyVJVJ1M0OB2XyclWe1UGNAAAAAAD9////AVokAAAAAAAAFgAUiVyehjAcphkvodfumT9bcTbRVzUiDh4AAAEA4QIAAAAAAQEr4Vm9SXSNqt+1fmZajpKmcvsm9wtk40sO7LifvJ1AhgEAAAAA/f///wKAJgAAAAAAABYAFHKzDxTM9g6SSWLjEuq9GhGNyfg8ECcAAAAAAAAZdqkUmp7Cs1p2YMgNrjjdgG/fmw/eaP2IrAJHMEQCIFMl4t7a86EnmxiBNob6xv6RBMsWiw+b0YMXeWWA/BhOAiABqjZkZLWeKxpm4RPv34o9JWvuDE26aBdf+K4aCvpVhgEhAmgC3Eoj32CNfhENLd8lZBDmhHOGIsBi8g45NdSJ65KZxZsdACIGAoMHnvutArD1dSRqFZxZFyyh/HW7Gdl6j7tc+iTyGCimEB+KRbUAAACAAQAAACkAAAAAIgIDoexWuGiz0Il3NVfg1fCnIiGz8h4okUhIYCDKQI3vbjUQH4pFtQAAAIAAAAAAAAAAAAA="

Quote from: commit: 638d4f7
A naive "solver" is implemented locally for p2pkh-like inputs.
Other more complex scripts cannot be signed atm without a wallet.

legendary
Activity: 3472
Merit: 10611
How about?
After checking your transaction again, it seems like it also lacks the second part that comes after the transaction in a PSBT tx (it is all zeros 0x000000) so the parser fails to populate the txins then fails signing the transaction later one since they are empty.
https://github.com/spesmilo/electrum/blob/22e6fe09c17724cd75763306470fa46a7bb7c4e8/electrum/transaction.py#L1709

So your problem must be in the "payto" step which creates a wrong PSBT. I don't know what you should do to fix it though.
newbie
Activity: 8
Merit: 2
There is something weird happening in the sign_transaction() code that seems to make it ignore the privkey Huh
It is not ignoring the private key, it doesn't know how to sign the transaction because the signature scripts are empty. It also uses the given private key if the command is called using the "privkey" parameter, otherwise it will call the wallet instance and asks if it can sign the given tx.
https://github.com/spesmilo/electrum/blob/64a931f21e6f6699f192158e54ff8f2883682604/electrum/commands.py#L397-L402

How about?
I have entered the "privkey" parameter

privkey="cVtE728tULSA4gut4QWxo218q6PRsXHQAv84SXix83cuvScvGd1H"

legendary
Activity: 3472
Merit: 10611
There is something weird happening in the sign_transaction() code that seems to make it ignore the privkey Huh
It is not ignoring the private key, it doesn't know how to sign the transaction because the signature scripts are empty. It also uses the given private key if the command is called using the "privkey" parameter, otherwise it will call the wallet instance and asks if it can sign the given tx.
https://github.com/spesmilo/electrum/blob/64a931f21e6f6699f192158e54ff8f2883682604/electrum/commands.py#L397-L402
newbie
Activity: 8
Merit: 2
Mine works well on a just created testnet wallet.
There is something weird happening in the sign_transaction() code that seems to make it ignore the privkey Huh

that's exactly right!

So there is no way? ?? Undecided


HCP
legendary
Activity: 2086
Merit: 4361
Mine works well on a just created testnet wallet.
As far as I can tell, it works if the wallet already has the private key in it (ie. you create an imported wallet using the privkey)... but if you are attempting to sign a transaction using a wallet that doesn't contain the key, it won't work... even if you pass the private key.

There is something weird happening in the sign_transaction() code that seems to make it ignore the privkey Huh

@Ali2k, you might need to create an issue on the Electrum Github: https://github.com/spesmilo/electrum/issues
legendary
Activity: 2758
Merit: 6830
Please tell me how to fill in the command by electrum-console
Mine works well on a just created testnet wallet.

Code:
payto('tb1qwhj8k62gppsvluxuxkk422vxwkztzeawerxe8q', 0.005, 0.0001, from_addr='tb1qyw252j0u6prxnyh4s6g3fah4e95a6jvn0z3z4l', unsigned=True)

Returns:
Code:
cHNidP8BAHECAAAAAZYvmtOLqeK4mbQ9hw62GX2zTAWCpMHAeQMmnPtbvjipAAAAAAD9////AjhSBwAAAAAAFgAUfaShSC/3lj1xGnS2UPKDMm/0QLIgoQcAAAAAABYAFHXke2lICGDP8Nw1rVUphnWEsWeuOZwdAAABAP1TAQIAAAAAAQJ5A+3PThU69FarfP6SgWNUSVObvNG0iJI2e0pCwuDeDAAAAAAA/f///3kD7c9OFTr0Vqt8/pKBY1RJU5u80bSIkjZ7SkLC4N4MAQAAAAD9////AX4aDwAAAAAAFgAUI5VFSfzQRmmS9YaRFPb1yWndSZMCRzBEAiBFQjWFowbj5KnwtzwJuwjjwVsQ0LmsOk5AkKA+Ta5wJwIgNy6OWIIWDbAMkluLqlDveYghvepSSsTnRMErY9b/O5EBIQMCQp1SMoA3ni/qiiE6+OExs8Q22O1MT2yK/XaD01VBDgJHMEQCIHoToNE4tUGwIrJ0rNeMthylip9PPYHLU3alw7rPrl99AiBdJgX4aIIsHbx8XjORiqQHrV/pEJMVmHR6fW0SNNNh3gEhA8bz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8a2ST1F8KxDNvl+TAAAAgAAAAAABAAAAAA==')

Result:
Code:
02000000000101962f9ad38ba9e2b899b43d870eb6197db34c0582a4c1c07903269cfb5bbe38a90000000000fdffffff0238520700000000001600147da4a1482ff7963d711a74b650f283326ff440b220a107000000000016001475e47b69480860cff0dc35ad5529867584b167ae02473044022073a33bd4b4305c72619fdd97ca1c34ea63b07cddf037446186dd6f72186241820220095d8aad8070738fce70935fce8db3c9fd2578b13b2f5ea13ed3a6ccf373a2b90121025a8761ff50822fb0c0790ecdf85d01f672f2421ab01020d889756a4ddf944232399c1d00

You can also just skip the "unsigned=True" if it's not a watch-only address and the private-key can be found on the client you're generating the tx on the first place.
newbie
Activity: 8
Merit: 2
Code:
Address:
n3UW4UHn8mkdQCWJ9jB4oE1EpgjC2S3fh3
Public key:
033f6737e40a3a6087bc58bc5b82b427f9ed26d710b8fe2f70bfdd3d62abebcf74
Private key:
p2pkh:cVtE728tULSA4gut4QWxo218q6PRsXHQAv84SXix83cuvScvGd1H

Code:
Payment address:
mucWYULuBUfgmy5VPTxECcd7SaSamQsZHC

Please tell me how to fill in the command by electrum-console

Code:
payto(address, amount, opt_fee, opt_from, opt_change, opt_nocheck, opt_unsigned, opt_rbf, opt_password, opt_locktime)
signtransaction(tx, opt_privkey)
legendary
Activity: 3472
Merit: 10611
The signature scripts of the transaction you are trying to sign (the line before last) are empty and if I understand the code correctly the sign() method in transaction expects them to be filled with your public keys so that it can know which private key to use (the sign method gets multiple keys unlike the command). Since yours are empty it simply skips that input and doesn't sign anything, finally it returns the transaction no matter if it is signed or not.

https://github.com/spesmilo/electrum/blob/018f09f2afe1fb6ea32ba69b26d19cc4deb7cac5/electrum/transaction.py#L1881
newbie
Activity: 8
Merit: 2
command:
Code:
>>> payto('mucWYULuBUfgmy5VPTxECcd7SaSamQsZHC', 0.00001,unsigned=True)
response:
Code:
"cHNidP8BAKACAAAAAiHTZFukTzP/9v4mZtwIAnm8NLUxxmiIcpcSqAsgSjKhAQAAAAD9////3X+Q1RrPmNxFrXSJMWqYOGjHXha/FP/rnq4BYDp7TaQBAAAAAP3///8C6AMAAAAAAAAZdqkUmp7Cs1p2YMgNrjjdgG/fmw/eaP2IrHTBEAAAAAAAGXapFPDcCT9/sbds/QZhDVNZ1llWdswriKynmx0AAAEA4QIAAAABi6jPnw/wtEw4nkoc0lwHcGNtlczvFh4xNUJkfUNaX9AAAAAAakcwRAIgNzs5iZBRd/LjbX49Arln0DCSdH/nu9O6eywkYjqIU4wCIHvnnuHZgQYMK+Z4P0lGzhvaH2RnGzSe8UpKb+zAR6ceASEDDeQ8XtTGJy0gzjvs8/t6/Vw8z7XVjd/fMEeYHgsAXg39////AsABBwAAAAAAGXapFBzT62W84srp9UVEtl5Gs60fCxhyiKxAQg8AAAAAABl2qRTw3Ak/f7G3bP0GYQ1TWdZZVnbMK4isl5sdAAABAOECAAAAAU45I2FYcW6RsLIXDr6dazWdE56ev/8WPyuv0L7JiQ0EAAAAAGpHMEQCIHA0DeuVyiXvhsTHqVObXI97g1GUFjVFAxH5FM2cL0XqAiA/p1duAyq1rkdjx49cISRXMhPJVihv12ZYLZRiUV3GVAEhAz9nN+QKOmCHvFi8W4K0J/ntJtcQuP4vcL/dPWKr6890/f///wLoAwAAAAAAABl2qRSQNQlZdQs7OORR3xa9WVe3ZJv10oisrIQBAAAAAAAZdqkU8NwJP3+xt2z9BmENU1nWWVZ2zCuIrJebHQAAAAA="

I sign using the following command
signtransaction(tx, opt_privkey)

command:
Code:
>>> signtransaction("cHNidP8BAKACAAAAAiHTZFukTzP/9v4mZtwIAnm8NLUxxmiIcpcSqAsgSjKhAQAAAAD9////3X+Q1RrPmNxFrXSJMWqYOGjHXha/FP/rnq4BYDp7TaQBAAAAAP3///8C6AMAAAAAAAAZdqkUmp7Cs1p2YMgNrjjdgG/fmw/eaP2IrHTBEAAAAAAAGXapFPDcCT9/sbds/QZhDVNZ1llWdswriKynmx0AAAEA4QIAAAABi6jPnw/wtEw4nkoc0lwHcGNtlczvF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AAGXapFBzT62W84srp9UVEtl5Gs60fCxhyiKxAQg8AAAAAABl2qRTw3Ak/f7G3bP0GYQ1TWdZZVnbMK4isl5sdAAABAOECAAAAAU45I2FYcW6RsLIXDr6dazWdE56ev/8WPyuv0L7JiQ0EAAAAAGpHMEQCIHA0DeuVyiXvhsTHqVObXI97g1GUFjVFAxH5FM2cL0XqAiA/p1duAyq1rkdjx49cISRXMhPJVihv12ZYLZRiUV3GVAEhAz9nN+QKOmCHvFi8W4K0J/ntJtcQuP4vcL/dPWKr6890/f///wLoAwAAAAAAABl2qRSQNQlZdQs7OORR3xa9WVe3ZJv10oisrIQBAAAAAAAZdqkU8NwJP3+xt2z9BmENU1nWWVZ2zCuIrJebHQAAAAA="

But it is not signed  Undecided
? ? ? ? ? ? ?
Jump to: