1
0

cmd: add sign with privkey and wallet (#7147)

* Remove check for sign with privkey in init_cmdline

* Add with_privkey and with_wallet variants of signtransaction command

* Add unit tests for signtransaction_with_privkey and signtransaction_with_wallet commands
This commit is contained in:
Nima
2021-06-08 16:33:55 +02:00
committed by GitHub
parent 7e6d65ec11
commit 13e4424922
3 changed files with 50 additions and 19 deletions

View File

@@ -33,6 +33,7 @@ import base64
import operator
import asyncio
import inspect
from collections import defaultdict
from functools import wraps, partial
from itertools import repeat
from decimal import Decimal
@@ -393,21 +394,35 @@ class Commands:
tx.sign(keypairs)
return tx.serialize()
@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
@command('')
async def signtransaction_with_privkey(self, tx, privkey):
"""Sign a transaction. The provided list of private keys will be used to sign the transaction."""
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)
for txin in tx.inputs():
if txin.address and txin.address == bitcoin.pubkey_to_address(txin_type, pubkey.hex()):
txins_dict = defaultdict(list)
for txin in tx.inputs():
txins_dict[txin.address].append(txin)
if not isinstance(privkey, list):
privkey = [privkey]
for priv in privkey:
txin_type, priv2, compressed = bitcoin.deserialize_privkey(priv)
pubkey = ecc.ECPrivkey(priv2).get_public_key_bytes(compressed=compressed)
address = bitcoin.pubkey_to_address(txin_type, pubkey.hex())
if address in txins_dict.keys():
for txin in txins_dict[address]:
txin.pubkeys = [pubkey]
txin.script_type = txin_type
tx.sign({pubkey.hex(): (privkey2, compressed)})
else:
wallet.sign_transaction(tx, password)
tx.sign({pubkey.hex(): (priv2, compressed)})
return tx.serialize()
@command('wp')
async def signtransaction(self, tx, password=None, wallet: Abstract_Wallet = None):
"""Sign a transaction. The wallet keys will be used to sign the transaction."""
tx = tx_from_any(tx)
wallet.sign_transaction(tx, password)
return tx.serialize()
@command('')