1
0

[TREZOR] Added Segwit support.

Following changes were necessary outside the TREZOR plugin.
- transaction.py: update_transaction handles segwit transactions.
- keystore.py: added a segwit parameter to bip44_derivation,
  use m/49' instead of m/44' for segwit.
This commit is contained in:
Jochen Hoenicke
2017-08-16 15:45:38 +02:00
parent fbe27fce04
commit ec0de566a8
4 changed files with 30 additions and 18 deletions

View File

@@ -26,6 +26,9 @@ class TrezorCompatibleKeyStore(Hardware_KeyStore):
def get_derivation(self):
return self.derivation
def is_segwit(self):
return self.derivation.startswith("m/49'/")
def get_client(self, force_pair=True):
return self.plugin.get_client(self, force_pair)
@@ -241,8 +244,8 @@ class TrezorCompatiblePlugin(HW_PluginBase):
self.prev_tx = prev_tx
self.xpub_path = xpub_path
client = self.get_client(keystore)
inputs = self.tx_inputs(tx, True)
outputs = self.tx_outputs(keystore.get_derivation(), tx)
inputs = self.tx_inputs(tx, True, keystore.is_segwit())
outputs = self.tx_outputs(keystore.get_derivation(), tx, keystore.is_segwit())
signed_tx = client.sign_tx(self.get_coin_name(), inputs, outputs, lock_time=tx.locktime)[1]
raw = bh2u(signed_tx)
tx.update_signatures(raw)
@@ -258,7 +261,7 @@ class TrezorCompatiblePlugin(HW_PluginBase):
address_n = client.expand_path(address_path)
client.get_address(self.get_coin_name(), address_n, True)
def tx_inputs(self, tx, for_sig=False):
def tx_inputs(self, tx, for_sig=False, segwit=False):
inputs = []
for txin in tx.inputs():
txinputtype = self.types.TxInputType()
@@ -273,6 +276,7 @@ class TrezorCompatiblePlugin(HW_PluginBase):
xpub, s = parse_xpubkey(x_pubkey)
xpub_n = self.client_class.expand_path(self.xpub_path[xpub])
txinputtype.address_n.extend(xpub_n + s)
txinputtype.script_type = self.types.SPENDP2SHWITNESS if segwit else self.types.SPENDADDRESS
else:
def f(x_pubkey):
if is_xpubkey(x_pubkey):
@@ -288,8 +292,9 @@ class TrezorCompatiblePlugin(HW_PluginBase):
signatures=map(lambda x: bfh(x)[:-1] if x else b'', txin.get('signatures')),
m=txin.get('num_sig'),
)
script_type = self.types.SPENDP2SHWITNESS if segwit else self.types.SPENDMULTISIG
txinputtype = self.types.TxInputType(
script_type=self.types.SPENDMULTISIG,
script_type=script_type,
multisig=multisig
)
# find which key is mine
@@ -304,6 +309,8 @@ class TrezorCompatiblePlugin(HW_PluginBase):
prev_hash = unhexlify(txin['prevout_hash'])
prev_index = txin['prevout_n']
if 'value' in txin:
txinputtype.amount = txin['value']
txinputtype.prev_hash = prev_hash
txinputtype.prev_index = prev_index
@@ -317,7 +324,7 @@ class TrezorCompatiblePlugin(HW_PluginBase):
return inputs
def tx_outputs(self, derivation, tx):
def tx_outputs(self, derivation, tx, segwit=False):
outputs = []
has_change = False
@@ -327,14 +334,16 @@ class TrezorCompatiblePlugin(HW_PluginBase):
has_change = True # no more than one change address
addrtype, hash_160 = bc_address_to_hash_160(address)
index, xpubs, m = info
if addrtype == ADDRTYPE_P2PKH:
if len(xpubs) == 1:
script_type = self.types.PAYTOP2SHWITNESS if segwit else self.types.PAYTOADDRESS
address_n = self.client_class.expand_path(derivation + "/%d/%d"%index)
txoutputtype = self.types.TxOutputType(
amount = amount,
script_type = self.types.PAYTOADDRESS,
script_type = script_type,
address_n = address_n,
)
elif addrtype == ADDRTYPE_P2SH:
else:
script_type = self.types.PAYTOP2SHWITNESS if segwit else self.types.PAYTOMULTISIG
address_n = self.client_class.expand_path("/%d/%d"%index)
nodes = map(self.ckd_public.deserialize, xpubs)
pubkeys = [ self.types.HDNodePathType(node=node, address_n=address_n) for node in nodes]
@@ -346,7 +355,7 @@ class TrezorCompatiblePlugin(HW_PluginBase):
multisig = multisig,
amount = amount,
address_n = self.client_class.expand_path(derivation + "/%d/%d"%index),
script_type = self.types.PAYTOMULTISIG)
script_type = script_type)
else:
txoutputtype = self.types.TxOutputType()
txoutputtype.amount = amount