Browse Source

psbt: follow-ups: fix ledger

patch-1
SomberNight 5 years ago
parent
commit
9e86352022
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 39
      electrum/plugins/ledger/ledger.py

39
electrum/plugins/ledger/ledger.py

@ -4,7 +4,9 @@ import sys
import traceback import traceback
from electrum import ecc from electrum import ecc
from electrum.bitcoin import TYPE_ADDRESS, int_to_hex, var_int, is_segwit_script_type from electrum import bip32
from electrum.crypto import hash_160
from electrum.bitcoin import int_to_hex, var_int, is_segwit_script_type
from electrum.bip32 import BIP32Node, convert_bip32_intpath_to_strpath from electrum.bip32 import BIP32Node, convert_bip32_intpath_to_strpath
from electrum.i18n import _ from electrum.i18n import _
from electrum.keystore import Hardware_KeyStore from electrum.keystore import Hardware_KeyStore
@ -78,9 +80,6 @@ class Ledger_Client():
def label(self): def label(self):
return "" return ""
def i4b(self, x):
return pack('>I', x)
def has_usable_connection_with_device(self): def has_usable_connection_with_device(self):
try: try:
self.dongleObject.getFirmwareVersion() self.dongleObject.getFirmwareVersion()
@ -101,29 +100,27 @@ class Ledger_Client():
raise UserFacingException(MSG_NEEDS_FW_UPDATE_SEGWIT) raise UserFacingException(MSG_NEEDS_FW_UPDATE_SEGWIT)
if xtype in ['p2wpkh-p2sh', 'p2wsh-p2sh'] and not self.supports_segwit(): if xtype in ['p2wpkh-p2sh', 'p2wsh-p2sh'] and not self.supports_segwit():
raise UserFacingException(MSG_NEEDS_FW_UPDATE_SEGWIT) raise UserFacingException(MSG_NEEDS_FW_UPDATE_SEGWIT)
splitPath = bip32_path.split('/') bip32_path = bip32.normalize_bip32_derivation(bip32_path)
if splitPath[0] == 'm': bip32_intpath = bip32.convert_bip32_path_to_list_of_uint32(bip32_path)
splitPath = splitPath[1:] bip32_path = bip32_path[2:] # cut off "m/"
bip32_path = bip32_path[2:] if len(bip32_intpath) >= 1:
fingerprint = 0 prevPath = bip32.convert_bip32_intpath_to_strpath(bip32_intpath[:-1])[2:]
if len(splitPath) > 1:
prevPath = "/".join(splitPath[0:len(splitPath) - 1])
nodeData = self.dongleObject.getWalletPublicKey(prevPath) nodeData = self.dongleObject.getWalletPublicKey(prevPath)
publicKey = compress_public_key(nodeData['publicKey']) publicKey = compress_public_key(nodeData['publicKey'])
h = hashlib.new('ripemd160') fingerprint_bytes = hash_160(publicKey)[0:4]
h.update(hashlib.sha256(publicKey).digest()) childnum_bytes = bip32_intpath[-1].to_bytes(length=4, byteorder="big")
fingerprint = unpack(">I", h.digest()[0:4])[0] else:
fingerprint_bytes = bytes(4)
childnum_bytes = bytes(4)
nodeData = self.dongleObject.getWalletPublicKey(bip32_path) nodeData = self.dongleObject.getWalletPublicKey(bip32_path)
publicKey = compress_public_key(nodeData['publicKey']) publicKey = compress_public_key(nodeData['publicKey'])
depth = len(splitPath) depth = len(bip32_intpath)
lastChild = splitPath[len(splitPath) - 1].split('\'')
childnum = int(lastChild[0]) if len(lastChild) == 1 else 0x80000000 | int(lastChild[0])
return BIP32Node(xtype=xtype, return BIP32Node(xtype=xtype,
eckey=ecc.ECPubkey(publicKey), eckey=ecc.ECPubkey(publicKey),
chaincode=nodeData['chainCode'], chaincode=nodeData['chainCode'],
depth=depth, depth=depth,
fingerprint=self.i4b(fingerprint), fingerprint=fingerprint_bytes,
child_number=self.i4b(childnum)).to_xpub() child_number=childnum_bytes).to_xpub()
def has_detached_pin_support(self, client): def has_detached_pin_support(self, client):
try: try:
@ -345,7 +342,7 @@ class Ledger_KeyStore(Hardware_KeyStore):
my_pubkey, full_path = self.find_my_pubkey_in_txinout(txin) my_pubkey, full_path = self.find_my_pubkey_in_txinout(txin)
if not full_path: if not full_path:
self.give_error("No matching pubkey for sign_transaction") # should never happen self.give_error("No matching pubkey for sign_transaction") # should never happen
full_path = convert_bip32_intpath_to_strpath(full_path) full_path = convert_bip32_intpath_to_strpath(full_path)[2:]
redeemScript = Transaction.get_preimage_script(txin) redeemScript = Transaction.get_preimage_script(txin)
txin_prev_tx = txin.utxo txin_prev_tx = txin.utxo
@ -393,7 +390,7 @@ class Ledger_KeyStore(Hardware_KeyStore):
if txout.is_change == any_output_on_change_branch: if txout.is_change == any_output_on_change_branch:
my_pubkey, changePath = self.find_my_pubkey_in_txinout(txout) my_pubkey, changePath = self.find_my_pubkey_in_txinout(txout)
assert changePath assert changePath
changePath = convert_bip32_intpath_to_strpath(changePath) changePath = convert_bip32_intpath_to_strpath(changePath)[2:]
has_change = True has_change = True
else: else:
output = txout.address output = txout.address

Loading…
Cancel
Save