Skip to content

Commit 61b5c5e

Browse files
committed
support multi sig
1 parent a959fcf commit 61b5c5e

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

tronapi/trx.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from typing import Any
1919

2020
from trx_utils import is_integer, is_hex
21-
from trx_utils.types import is_object, is_string
21+
from trx_utils.types import is_object, is_string, is_list
2222

2323
from tronapi.common.transactions import wait_for_transaction_id
2424
from tronapi.contract import Contract
@@ -516,7 +516,7 @@ def online_sign(self, transaction: dict):
516516
'privateKey': self.tron.private_key
517517
})
518518

519-
def sign(self, transaction: Any, use_tron: bool = True):
519+
def sign(self, transaction: Any, use_tron: bool = True, multisig: bool = False):
520520
"""Safe method for signing your transaction
521521
522522
Warnings:
@@ -525,6 +525,7 @@ def sign(self, transaction: Any, use_tron: bool = True):
525525
Args:
526526
transaction (Any): transaction details
527527
use_tron (bool): is Tron header
528+
multisig (bool): multi sign
528529
529530
"""
530531

@@ -546,21 +547,33 @@ def sign(self, transaction: Any, use_tron: bool = True):
546547

547548
return signed_message
548549

549-
if 'signature' in transaction:
550+
if not multisig and 'signature' in transaction:
550551
raise TronError('Transaction is already signed')
551552

552-
address = self.tron.address.from_private_key(self.tron.private_key).hex.lower()
553-
owner_address = transaction['raw_data']['contract'][0]['parameter']['value']['owner_address']
553+
try:
554+
if not multisig:
555+
address = self.tron.address.from_private_key(self.tron.private_key).hex.lower()
556+
owner_address = transaction['raw_data']['contract'][0]['parameter']['value']['owner_address']
554557

555-
if address != owner_address:
556-
raise ValueError('Private key does not match address in transaction')
558+
if address != owner_address:
559+
raise ValueError('Private key does not match address in transaction')
557560

558-
# This option deals with signing of transactions, and writing to the array
559-
signed_tx = Account.sign_hash(
560-
transaction['txID'], self.tron.private_key
561-
)
562-
transaction['signature'] = [signed_tx['signature'].hex()[2:]]
563-
return transaction
561+
# This option deals with signing of transactions, and writing to the array
562+
signed_tx = Account.sign_hash(
563+
transaction['txID'], self.tron.private_key
564+
)
565+
signature = signed_tx['signature'].hex()[2:]
566+
567+
# support multi sign
568+
if 'signature' in transaction and is_list(transaction['signature']):
569+
if not transaction['signature'].index(signature):
570+
transaction['signature'].append(signature)
571+
else:
572+
transaction['signature'] = [signature]
573+
574+
return transaction
575+
except ValueError as err:
576+
raise InvalidTronError(err)
564577

565578
def broadcast(self, signed_transaction):
566579
"""Broadcast the signed transaction

0 commit comments

Comments
 (0)