upgrade digital bitbox plugin for firmware 2.2.2

This commit is contained in:
djb 2017-03-21 19:17:06 +01:00
parent 91deea89da
commit 4e40a0bc5b
No known key found for this signature in database
GPG Key ID: 75D4B1A518B37477
1 changed files with 98 additions and 27 deletions

View File

@ -5,7 +5,7 @@
try: try:
import electrum import electrum
from electrum.bitcoin import TYPE_ADDRESS, var_int, msg_magic, Hash, verify_message, public_key_to_p2pkh, EncodeAES, DecodeAES from electrum.bitcoin import TYPE_ADDRESS, var_int, msg_magic, Hash, verify_message, pubkey_from_signature, point_to_ser, public_key_to_p2pkh, EncodeAES, DecodeAES, MyVerifyingKey
from electrum.i18n import _ from electrum.i18n import _
from electrum.keystore import Hardware_KeyStore from electrum.keystore import Hardware_KeyStore
from ..hw_wallet import HW_PluginBase from ..hw_wallet import HW_PluginBase
@ -15,9 +15,11 @@ try:
import hid import hid
import json import json
import math import math
import struct
import hashlib import hashlib
from ecdsa.ecdsa import generator_secp256k1 from ecdsa.ecdsa import generator_secp256k1
from ecdsa.util import sigencode_der from ecdsa.util import sigencode_der
from ecdsa.curves import SECP256k1
DIGIBOX = True DIGIBOX = True
except ImportError as e: except ImportError as e:
DIGIBOX = False DIGIBOX = False
@ -36,7 +38,7 @@ class DigitalBitbox_Client():
self.password = None self.password = None
self.isInitialized = False self.isInitialized = False
self.setupRunning = False self.setupRunning = False
self.hidBufSize = 4096 self.usbReportSize = 64 # firmware > v2.0.0
def close(self): def close(self):
@ -248,13 +250,56 @@ class DigitalBitbox_Client():
return True return True
def hid_send_frame(self, data):
HWW_CID = 0xFF000000
HWW_CMD = 0x80 + 0x40 + 0x01
data = bytearray(data)
data_len = len(data)
seq = 0;
idx = 0;
write = []
while idx < data_len:
if idx == 0:
# INIT frame
write = data[idx : idx + min(data_len, self.usbReportSize - 7)]
self.dbb_hid.write('\0' + struct.pack(">IBH", HWW_CID, HWW_CMD, data_len & 0xFFFF) + write + '\xEE' * (self.usbReportSize - 7 - len(write)))
else:
# CONT frame
write = data[idx : idx + min(data_len, self.usbReportSize - 5)]
self.dbb_hid.write('\0' + struct.pack(">IB", HWW_CID, seq) + write + '\xEE' * (self.usbReportSize - 5 - len(write)))
seq += 1
idx += len(write)
def hid_read_frame(self):
# INIT response
read = self.dbb_hid.read(self.usbReportSize)
cid = ((read[0] * 256 + read[1]) * 256 + read[2]) * 256 + read[3]
cmd = read[4]
data_len = read[5] * 256 + read[6]
data = read[7:]
idx = len(read) - 7;
while idx < data_len:
# CONT response
read = self.dbb_hid.read(self.usbReportSize)
data += read[5:]
idx += len(read) - 5
return data
def hid_send_plain(self, msg): def hid_send_plain(self, msg):
reply = "" reply = ""
try: try:
self.dbb_hid.write('\0' + bytearray(msg) + '\0' * (self.hidBufSize - len(msg))) serial_number = self.dbb_hid.get_serial_number_string()
if "v2.0." in serial_number or "v1." in serial_number:
hidBufSize = 4096
self.dbb_hid.write('\0' + bytearray(msg) + '\0' * (hidBufSize - len(msg)))
r = [] r = []
while len(r) < self.hidBufSize: while len(r) < hidBufSize:
r = r + self.dbb_hid.read(self.hidBufSize) r = r + self.dbb_hid.read(hidBufSize)
else:
self.hid_send_frame(msg)
r = self.hid_read_frame()
r = str(bytearray(r)).rstrip(' \t\r\n\0') r = str(bytearray(r)).rstrip(' \t\r\n\0')
r = r.replace("\0", '') r = r.replace("\0", '')
reply = json.loads(r) reply = json.loads(r)
@ -338,6 +383,17 @@ class DigitalBitbox_KeyStore(Hardware_KeyStore):
if 'sign' not in reply: if 'sign' not in reply:
raise Exception("Could not sign message.") raise Exception("Could not sign message.")
if 'recid' in reply['sign'][0]:
# firmware > v2.1.1
sig = chr(27 + int(reply['sign'][0]['recid'], 16) + 4) + reply['sign'][0]['sig'].decode('hex')
h = Hash(msg_magic(message))
pk, compressed = pubkey_from_signature(sig, h)
pk = point_to_ser(pk.pubkey.point, compressed)
addr = public_key_to_p2pkh(pk)
if verify_message(addr, sig, message) is False:
raise Exception("Could not sign message")
elif 'pubkey' in reply['sign'][0]:
# firmware <= v2.1.1
for i in range(4): for i in range(4):
sig = chr(27 + i + 4) + reply['sign'][0]['sig'].decode('hex') sig = chr(27 + i + 4) + reply['sign'][0]['sig'].decode('hex')
try: try:
@ -349,6 +405,7 @@ class DigitalBitbox_KeyStore(Hardware_KeyStore):
else: else:
raise Exception("Could not sign message") raise Exception("Could not sign message")
except BaseException as e: except BaseException as e:
self.give_error(e) self.give_error(e)
return sig return sig
@ -361,6 +418,7 @@ class DigitalBitbox_KeyStore(Hardware_KeyStore):
try: try:
p2shTransaction = False p2shTransaction = False
derivations = self.get_tx_derivations(tx) derivations = self.get_tx_derivations(tx)
inputhasharray = []
hasharray = [] hasharray = []
pubkeyarray = [] pubkeyarray = []
@ -376,9 +434,10 @@ class DigitalBitbox_KeyStore(Hardware_KeyStore):
if x_pubkey in derivations: if x_pubkey in derivations:
index = derivations.get(x_pubkey) index = derivations.get(x_pubkey)
inputPath = "%s/%d/%d" % (self.get_derivation(), index[0], index[1]) inputPath = "%s/%d/%d" % (self.get_derivation(), index[0], index[1])
inputHash = Hash(tx.serialize_preimage(i).decode('hex')).encode('hex') inputHash = Hash(tx.serialize_preimage(i).decode('hex'))
hasharray_i = {'hash': inputHash, 'keypath': inputPath} hasharray_i = {'hash': inputHash.encode('hex'), 'keypath': inputPath}
hasharray.append(hasharray_i) hasharray.append(hasharray_i)
inputhasharray.append(inputHash)
break break
else: else:
self.give_error("No matching x_key for sign_transaction") # should never happen self.give_error("No matching x_key for sign_transaction") # should never happen
@ -455,12 +514,22 @@ class DigitalBitbox_KeyStore(Hardware_KeyStore):
break # txin is complete break # txin is complete
ii = txin['pubkeys'].index(pubkey) ii = txin['pubkeys'].index(pubkey)
signed = dbb_signatures[i] signed = dbb_signatures[i]
if signed['pubkey'] != pubkey: if 'recid' in signed:
# firmware > v2.1.1
recid = int(signed['recid'], 16)
s = signed['sig'].decode('hex')
h = inputhasharray[i]
pk = MyVerifyingKey.from_signature(s, recid, h, curve = SECP256k1)
pk = point_to_ser(pk.pubkey.point, True).encode('hex')
elif 'pubkey' in signed:
# firmware <= v2.1.1
pk = signed['pubkey']
if pk != pubkey:
continue continue
sig_r = int(signed['sig'][:64], 16) sig_r = int(signed['sig'][:64], 16)
sig_s = int(signed['sig'][64:], 16) sig_s = int(signed['sig'][64:], 16)
sig = sigencode_der(sig_r, sig_s, generator_secp256k1.order()) sig = sigencode_der(sig_r, sig_s, generator_secp256k1.order())
txin['signatures'][ii] = sig.encode('hex') txin['signatures'][ii] = sig.encode('hex') + '01'
tx._inputs[i] = txin tx._inputs[i] = txin
except BaseException as e: except BaseException as e:
@ -470,7 +539,6 @@ class DigitalBitbox_KeyStore(Hardware_KeyStore):
tx.raw = tx.serialize() tx.raw = tx.serialize()
class DigitalBitboxPlugin(HW_PluginBase): class DigitalBitboxPlugin(HW_PluginBase):
libraries_available = DIGIBOX libraries_available = DIGIBOX
@ -493,11 +561,14 @@ class DigitalBitboxPlugin(HW_PluginBase):
def create_client(self, device, handler): def create_client(self, device, handler):
if device.interface_number == 0 or device.usage_page == 0xffff:
self.handler = handler self.handler = handler
client = self.get_dbb_device(device) client = self.get_dbb_device(device)
if client <> None: if client <> None:
client = DigitalBitbox_Client(client) client = DigitalBitbox_Client(client)
return client return client
else:
return None
def setup_device(self, device_info, wizard): def setup_device(self, device_info, wizard):