2018-02-08 12:14:36 +00:00
|
|
|
from trezor.crypto.hashlib import sha256
|
2018-01-29 13:41:56 +00:00
|
|
|
from trezor.crypto import bip32
|
2018-02-27 13:45:11 +00:00
|
|
|
from trezor.utils import HashWriter
|
2018-01-29 13:41:56 +00:00
|
|
|
|
|
|
|
from trezor.messages.MultisigRedeemScriptType import MultisigRedeemScriptType
|
|
|
|
from trezor.messages.HDNodePathType import HDNodePathType
|
2018-02-27 11:58:37 +00:00
|
|
|
from trezor.messages import FailureType
|
2018-01-29 13:41:56 +00:00
|
|
|
|
2018-06-05 18:21:31 +00:00
|
|
|
from apps.wallet.sign_tx.writers import write_bytes, write_uint32
|
2018-02-08 12:14:36 +00:00
|
|
|
|
|
|
|
|
2018-02-27 11:58:37 +00:00
|
|
|
class MultisigError(ValueError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-02-23 18:55:28 +00:00
|
|
|
class MultisigFingerprint:
|
|
|
|
def __init__(self):
|
|
|
|
self.fingerprint = None # multisig fingerprint bytes
|
|
|
|
self.mismatch = False # flag if multisig input fingerprints are equal
|
|
|
|
|
|
|
|
def add(self, multisig: MultisigRedeemScriptType):
|
|
|
|
fp = multisig_fingerprint(multisig)
|
|
|
|
assert fp is not None
|
|
|
|
if self.fingerprint is None:
|
|
|
|
self.fingerprint = fp
|
|
|
|
elif self.fingerprint != fp:
|
|
|
|
self.mismatch = True
|
|
|
|
|
|
|
|
def matches(self, multisig: MultisigRedeemScriptType):
|
|
|
|
fp = multisig_fingerprint(multisig)
|
|
|
|
assert fp is not None
|
|
|
|
if self.mismatch is False and self.fingerprint == fp:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2018-02-08 12:14:36 +00:00
|
|
|
def multisig_fingerprint(multisig: MultisigRedeemScriptType) -> bytes:
|
|
|
|
pubkeys = multisig.pubkeys
|
|
|
|
m = multisig.m
|
|
|
|
n = len(pubkeys)
|
|
|
|
|
2018-02-23 18:55:28 +00:00
|
|
|
if n < 1 or n > 15 or m < 1 or m > 15:
|
2018-02-27 11:58:37 +00:00
|
|
|
raise MultisigError(FailureType.DataError, 'Invalid multisig parameters')
|
2018-02-08 12:14:36 +00:00
|
|
|
|
|
|
|
for hd in pubkeys:
|
|
|
|
d = hd.node
|
2018-02-23 18:55:28 +00:00
|
|
|
if len(d.public_key) != 33 or len(d.chain_code) != 32:
|
2018-02-27 11:58:37 +00:00
|
|
|
raise MultisigError(FailureType.DataError, 'Invalid multisig parameters')
|
2018-02-08 12:14:36 +00:00
|
|
|
|
|
|
|
# casting to bytes(), sorting on bytearray() is not supported in MicroPython
|
|
|
|
pubkeys = sorted(pubkeys, key=lambda hd: bytes(hd.node.public_key))
|
|
|
|
|
|
|
|
h = HashWriter(sha256)
|
|
|
|
write_uint32(h, m)
|
|
|
|
write_uint32(h, n)
|
|
|
|
for hd in pubkeys:
|
|
|
|
d = hd.node
|
|
|
|
write_uint32(h, d.depth)
|
|
|
|
write_uint32(h, d.fingerprint)
|
|
|
|
write_uint32(h, d.child_num)
|
|
|
|
write_bytes(h, d.chain_code)
|
|
|
|
write_bytes(h, d.public_key)
|
|
|
|
|
|
|
|
return h.get_digest()
|
2018-01-29 13:41:56 +00:00
|
|
|
|
|
|
|
|
2018-01-31 11:48:18 +00:00
|
|
|
def multisig_pubkey_index(multisig: MultisigRedeemScriptType, pubkey: bytes) -> int:
|
|
|
|
for i, hd in enumerate(multisig.pubkeys):
|
|
|
|
if multisig_get_pubkey(hd) == pubkey:
|
|
|
|
return i
|
2018-02-27 11:58:37 +00:00
|
|
|
raise MultisigError(FailureType.DataError,
|
2018-02-27 19:28:01 +00:00
|
|
|
'Pubkey not found in multisig script')
|
2018-01-31 11:48:18 +00:00
|
|
|
|
|
|
|
|
2018-01-29 13:41:56 +00:00
|
|
|
def multisig_get_pubkey(hd: HDNodePathType) -> bytes:
|
2018-02-26 13:17:39 +00:00
|
|
|
p = hd.address_n
|
2018-01-29 13:41:56 +00:00
|
|
|
n = hd.node
|
|
|
|
node = bip32.HDNode(
|
|
|
|
depth=n.depth,
|
|
|
|
fingerprint=n.fingerprint,
|
|
|
|
child_num=n.child_num,
|
|
|
|
chain_code=n.chain_code,
|
|
|
|
public_key=n.public_key)
|
|
|
|
for i in p:
|
|
|
|
node.derive(i, True)
|
|
|
|
return node.public_key()
|
|
|
|
|
|
|
|
|
|
|
|
def multisig_get_pubkeys(multisig: MultisigRedeemScriptType):
|
|
|
|
return [multisig_get_pubkey(hd) for hd in multisig.pubkeys]
|