From a938ee4522228209c219e784edff15c193bfa7f6 Mon Sep 17 00:00:00 2001 From: chaeplin Date: Fri, 10 Mar 2017 19:32:08 +0900 Subject: [PATCH 1/7] python3 on signing --- .gitignore | 4 ++- btchip/bitcoinTransaction.py | 2 +- btchip/btchip.py | 67 ++++++++++++++++++----------------- btchip/btchipComm.py | 16 ++++----- btchip/btchipUtils.py | 22 ++++++------ btchip/ledgerWrapper.py | 22 ++++++------ samples/getFirmwareVersion.py | 2 +- study/getFirmwareVersion.py | 26 ++++++++++++++ study/getaddress.py | 20 +++++++++++ study/getsignMessage.py | 42 ++++++++++++++++++++++ study/hex2.py | 16 +++++++++ study/hex3.py | 5 +++ 12 files changed, 179 insertions(+), 65 deletions(-) create mode 100644 study/getFirmwareVersion.py create mode 100644 study/getaddress.py create mode 100644 study/getsignMessage.py create mode 100644 study/hex2.py create mode 100644 study/hex3.py diff --git a/.gitignore b/.gitignore index 2f78cf5..5296cc1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.pyc - +build +btchip_python.egg-info +dist diff --git a/btchip/bitcoinTransaction.py b/btchip/bitcoinTransaction.py index 4755683..e66ffba 100644 --- a/btchip/bitcoinTransaction.py +++ b/btchip/bitcoinTransaction.py @@ -95,7 +95,7 @@ def __init__(self, data=None): offset = 0 self.version = data[offset:offset + 4] offset += 4 - if (data[offset] == 0) and (data[offset + 1] <> 0): + if (data[offset] == 0) and (data[offset + 1] != 0): offset += 2 self.witness = True inputSize = readVarint(data, offset) diff --git a/btchip/btchip.py b/btchip/btchip.py index e1e14b1..90adf2d 100644 --- a/btchip/btchip.py +++ b/btchip/btchip.py @@ -16,6 +16,8 @@ * limitations under the License. ******************************************************************************** """ +import os, sys +sys.path.append(os.path.join(os.path.dirname(__file__), '.')) from btchipComm import * from bitcoinTransaction import * @@ -23,7 +25,7 @@ from btchipException import * from btchipHelpers import * from btchipKeyRecovery import * -from binascii import hexlify +from binascii import hexlify, unhexlify class btchip: BTCHIP_CLA = 0xe0 @@ -71,9 +73,9 @@ class btchip: FEATURE_FREE_SIGHASHTYPE = 0x04 FEATURE_NO_2FA_P2SH = 0x08 - QWERTY_KEYMAP = bytearray("000000000000000000000000760f00d4ffffffc7000000782c1e3420212224342627252e362d3738271e1f202122232425263333362e37381f0405060708090a0b0c0d0e0f101112131415161718191a1b1c1d2f3130232d350405060708090a0b0c0d0e0f101112131415161718191a1b1c1d2f313035".decode('hex')) - QWERTZ_KEYMAP = bytearray("000000000000000000000000760f00d4ffffffc7000000782c1e3420212224342627252e362d3738271e1f202122232425263333362e37381f0405060708090a0b0c0d0e0f101112131415161718191a1b1d1c2f3130232d350405060708090a0b0c0d0e0f101112131415161718191a1b1d1c2f313035".decode('hex')) - AZERTY_KEYMAP = bytearray("08000000010000200100007820c8ffc3feffff07000000002c38202030341e21222d352e102e3637271e1f202122232425263736362e37101f1405060708090a0b0c0d0e0f331112130415161718191d1b1c1a2f64302f2d351405060708090a0b0c0d0e0f331112130415161718191d1b1c1a2f643035".decode('hex')) + QWERTY_KEYMAP = bytearray(unhexlify('000000000000000000000000760f00d4ffffffc7000000782c1e3420212224342627252e362d3738271e1f202122232425263333362e37381f0405060708090a0b0c0d0e0f101112131415161718191a1b1c1d2f3130232d350405060708090a0b0c0d0e0f101112131415161718191a1b1c1d2f313035')) + QWERTZ_KEYMAP = bytearray(unhexlify('000000000000000000000000760f00d4ffffffc7000000782c1e3420212224342627252e362d3738271e1f202122232425263333362e37381f0405060708090a0b0c0d0e0f101112131415161718191a1b1d1c2f3130232d350405060708090a0b0c0d0e0f101112131415161718191a1b1d1c2f313035')) + AZERTY_KEYMAP = bytearray(unhexlify('08000000010000200100007820c8ffc3feffff07000000002c38202030341e21222d352e102e3637271e1f202122232425263736362e37101f1405060708090a0b0c0d0e0f331112130415161718191d1b1c1a2f64302f2d351405060708090a0b0c0d0e0f331112130415161718191d1b1c1a2f643035')) def __init__(self, dongle): self.dongle = dongle @@ -107,7 +109,7 @@ def getVerifyPinRemainingAttempts(self): apdu.extend(bytearray("0")) try: self.dongle.exchange(bytearray(apdu)) - except BTChipException, e: + except BTChipException as e: if ((e.sw & 0xfff0) == 0x63c0): return e.sw - 0x63c0 raise e @@ -123,7 +125,7 @@ def getWalletPublicKey(self, path): offset = 0 result['publicKey'] = response[offset + 1 : offset + 1 + response[offset]] offset = offset + 1 + response[offset] - result['address'] = str(response[offset + 1 : offset + 1 + response[offset]]) + result['address'] = response[offset + 1 : offset + 1 + response[offset]] offset = offset + 1 + response[offset] result['chainCode'] = response[offset : offset + 32] return result @@ -231,7 +233,7 @@ def startUntrustedTransaction(self, newTransaction, inputIndex, outputList, rede if ('trustedInput' in passedOutput) and passedOutput['trustedInput']: params.append(len(passedOutput['value'])) params.extend(passedOutput['value']) - if currentIndex <> inputIndex: + if currentIndex != inputIndex: script = bytearray() writeVarint(len(script), params) if len(script) == 0: @@ -266,7 +268,7 @@ def finalizeInput(self, outputAddress, amount, fees, changePath, rawTx=None): try: fullTx = bitcoinTransaction(bytearray(rawTx)) outputs = fullTx.serializeOutputs() - if len(donglePath) <> 0: + if len(donglePath) != 0: apdu = [ self.BTCHIP_CLA, self.BTCHIP_INS_HASH_INPUT_FINALIZE_FULL, 0xFF, 0x00 ] params = [] params.extend(donglePath) @@ -301,7 +303,7 @@ def finalizeInput(self, outputAddress, amount, fees, changePath, rawTx=None): apdu.append(len(params)) apdu.extend(params) response = self.dongle.exchange(bytearray(apdu)) - result['confirmationNeeded'] = response[1 + response[0]] <> 0x00 + result['confirmationNeeded'] = response[1 + response[0]] != 0x00 result['confirmationType'] = response[1 + response[0]] if result['confirmationType'] == 0x02: result['keycardData'] = response[1 + response[0] + 1:] @@ -341,11 +343,11 @@ def finalizeInputFull(self, outputData): encryptedOutputData = encryptedOutputData + response[1 : 1 + response[0]] offset += dataLength if len(response) > 1: - result['confirmationNeeded'] = response[1 + response[0]] <> 0x00 + result['confirmationNeeded'] = response[1 + response[0]] != 0x00 result['confirmationType'] = response[1 + response[0]] else: # Support for old style API before 1.0.2 - result['confirmationNeeded'] = response[0] <> 0x00 + result['confirmationNeeded'] = response[0] != 0x00 result['confirmationType'] = response[0] if result['confirmationType'] == 0x02: result['keycardData'] = response[1 + response[0] + 1:] # legacy @@ -393,7 +395,7 @@ def signMessagePrepareV1(self, path, message): apdu.append(len(params)) apdu.extend(params) response = self.dongle.exchange(bytearray(apdu)) - result['confirmationNeeded'] = response[0] <> 0x00 + result['confirmationNeeded'] = response[0] != 0x00 result['confirmationType'] = response[0] if result['confirmationType'] == 0x02: result['keycardData'] = response[1:] @@ -422,14 +424,14 @@ def signMessagePrepareV2(self, path, message): dataLength = blockLength else: dataLength = len(message) - offset - params.extend(bytearray(message[offset : offset + dataLength])) + params.extend(bytearray(message[offset : offset + dataLength], 'utf8')) apdu = [ self.BTCHIP_CLA, self.BTCHIP_INS_SIGN_MESSAGE, 0x00, p2 ] apdu.append(len(params)) apdu.extend(params) response = self.dongle.exchange(bytearray(apdu)) - encryptedOutputData = encryptedOutputData + response[1 : 1 + response[0]] + encryptedOutputData = encryptedOutputData + response[1 : 1 + response[0]].decode("utf-8") offset += blockLength - result['confirmationNeeded'] = response[1 + response[0]] <> 0x00 + result['confirmationNeeded'] = response[1 + response[0]] != 0x00 result['confirmationType'] = response[1 + response[0]] if result['confirmationType'] == 0x03: offset = 1 + response[0] + 1 @@ -451,11 +453,12 @@ def signMessagePrepare(self, path, message): def signMessageSign(self, pin=""): apdu = [ self.BTCHIP_CLA, self.BTCHIP_INS_SIGN_MESSAGE, 0x80, 0x00 ] params = [] - if pin is not None: - params.append(len(pin)) - params.extend(bytearray(pin)) - else: - params.append(0x00) +# if pin is not None: +# params.append(len(pin)) +# params.extend(bytearray(pin, 'utf8')) +# else: +# params.append(0x00) + params.append(0x00) apdu.append(len(params)) apdu.extend(params) response = self.dongle.exchange(bytearray(apdu)) @@ -522,10 +525,10 @@ def getOperationMode(self): return response[0] def setOperationMode(self, operationMode): - if operationMode <> btchip.OPERATION_MODE_WALLET \ - and operationMode <> btchip.OPERATION_MODE_RELAXED_WALLET \ - and operationMode <> btchip.OPERATION_MODE_SERVER \ - and operationMode <> btchip.OPERATION_MODE_DEVELOPER: + if operationMode != btchip.OPERATION_MODE_WALLET \ + and operationMode != btchip.OPERATION_MODE_RELAXED_WALLET \ + and operationMode != btchip.OPERATION_MODE_SERVER \ + and operationMode != btchip.OPERATION_MODE_DEVELOPER: raise BTChipException("Invalid operation mode") apdu = [ self.BTCHIP_CLA, self.BTCHIP_INS_SET_OPERATION_MODE, 0x00, 0x00, 0x01, operationMode ] self.dongle.exchange(bytearray(apdu)) @@ -572,9 +575,9 @@ def getPOSEncryptedSeed(self): def importPrivateKey(self, data, isSeed=False): apdu = [ self.BTCHIP_CLA, self.BTCHIP_INS_IMPORT_PRIVATE_KEY, (0x02 if isSeed else 0x01), 0x00 ] - apdu.append(len(data)) - apdu.extend(data) - return self.dongle.exchange(bytearray(apdu)) + apdu.append(len(data)) + apdu.extend(data) + return self.dongle.exchange(bytearray(apdu)) def getPublicKey(self, encodedPrivateKey): result = {} @@ -583,9 +586,9 @@ def getPublicKey(self, encodedPrivateKey): apdu.append(len(encodedPrivateKey)) apdu.extend(encodedPrivateKey) response = self.dongle.exchange(bytearray(apdu)) - offset = 1 - result['publicKey'] = response[offset + 1 : offset + 1 + response[offset]] - offset = offset + 1 + response[offset] + offset = 1 + result['publicKey'] = response[offset + 1 : offset + 1 + response[offset]] + offset = offset + 1 + response[offset] if response[0] == 0x02: result['chainCode'] = response[offset : offset + 32] offset = offset + 32 @@ -674,7 +677,7 @@ def resolvePublicKeysInPath(self, path): startOffset = offset break offset = offset + 1 - if startOffset <> 0: + if startOffset != 0: searchPath = splitPath[0:startOffset - 1] offset = startOffset - 1 while(offset < len(splitPath)): @@ -687,5 +690,5 @@ def getJCExtendedFeatures(self): result = {} apdu = [ self.BTCHIP_JC_EXT_CLA, self.BTCHIP_INS_EXT_CACHE_GET_FEATURES, 0x00, 0x00, 0x00 ] response = self.dongle.exchange(bytearray(apdu)) - result['proprietaryApi'] = ((response[0] & 0x01) <> 0) + result['proprietaryApi'] = ((response[0] & 0x01) != 0) return result diff --git a/btchip/btchipComm.py b/btchip/btchipComm.py index 0e4668c..17adc35 100644 --- a/btchip/btchipComm.py +++ b/btchip/btchipComm.py @@ -64,15 +64,15 @@ def __init__(self, device, ledger=False, debug=False): def exchange(self, apdu, timeout=20000): if self.debug: - print "=> %s" % hexlify(apdu) + print("=> %s" % hexlify(apdu)) if self.ledger: apdu = wrapCommandAPDU(0x0101, apdu, 64) padSize = len(apdu) % 64 tmp = apdu - if padSize <> 0: + if padSize != 0: tmp.extend([0] * (64 - padSize)) offset = 0 - while(offset <> len(tmp)): + while(offset != len(tmp)): data = tmp[offset:offset + 64] data = bytearray([0]) + data self.device.write(data) @@ -114,8 +114,8 @@ def exchange(self, apdu, timeout=20000): sw = (result[swOffset] << 8) + result[swOffset + 1] response = result[dataStart : dataLength + dataStart] if self.debug: - print "<= %s%.2x" % (hexlify(response), sw) - if sw <> 0x9000: + print("<= %s%.2x" % (hexlify(response), sw)) + if sw != 0x9000: raise BTChipException("Invalid status %04x" % sw, sw) return response @@ -148,12 +148,12 @@ def __init__(self, device, debug=False): def exchange(self, apdu, timeout=20000): if self.debug: - print "=> %s" % hexlify(apdu) + print("=> %s" % hexlify(apdu)) response, sw1, sw2 = self.device.transmit(toBytes(hexlify(apdu))) sw = (sw1 << 8) | sw2 if self.debug: - print "<= %s%.2x" % (toHexString(response).replace(" ", ""), sw) - if sw <> 0x9000: + print("<= %s%.2x" % (toHexString(response).replace(" ", ""), sw)) + if sw != 0x9000: raise BTChipException("Invalid status %04x" % sw, sw) return bytearray(response) diff --git a/btchip/btchipUtils.py b/btchip/btchipUtils.py index 81238a4..92f298a 100644 --- a/btchip/btchipUtils.py +++ b/btchip/btchipUtils.py @@ -23,7 +23,7 @@ def compress_public_key(publicKey): if publicKey[0] == 0x04: - if (publicKey[64] & 1) <> 0: + if (publicKey[64] & 1) != 0: prefix = 0x03 else: prefix = 0x02 @@ -64,16 +64,16 @@ def get_regular_input_script(sigHashtype, publicKey): def write_pushed_data_size(data, buffer): if (len(data) > 0xffff): raise BTChipException("unsupported encoding") - if (len(data) < 0x4c): - buffer.append(len(data)) - elif (len(data) > 255): - buffer.append(0x4d) - buffer.append(len(data) & 0xff) - buffer.append((len(data) >> 8) & 0xff) - else: - buffer.append(0x4c) - buffer.append(len(data)) - return buffer + if (len(data) < 0x4c): + buffer.append(len(data)) + elif (len(data) > 255): + buffer.append(0x4d) + buffer.append(len(data) & 0xff) + buffer.append((len(data) >> 8) & 0xff) + else: + buffer.append(0x4c) + buffer.append(len(data)) + return buffer def get_p2sh_input_script(redeemScript, sigHashtypeList): diff --git a/btchip/ledgerWrapper.py b/btchip/ledgerWrapper.py index 93af91b..31cb998 100644 --- a/btchip/ledgerWrapper.py +++ b/btchip/ledgerWrapper.py @@ -33,7 +33,7 @@ def wrapCommandAPDU(channel, command, packetSize): blockSize = len(command) result += command[offset : offset + blockSize] offset = offset + blockSize - while offset <> len(command): + while offset != len(command): result += struct.pack(">HBH", channel, 0x05, sequenceIdx) sequenceIdx = sequenceIdx + 1 if (len(command) - offset) > packetSize - 5: @@ -42,8 +42,8 @@ def wrapCommandAPDU(channel, command, packetSize): blockSize = len(command) - offset result += command[offset : offset + blockSize] offset = offset + blockSize - while (len(result) % packetSize) <> 0: - result += "\x00" + while (len(result) % packetSize) != 0: + result += b"\x00" return bytearray(result) def unwrapResponseAPDU(channel, data, packetSize): @@ -51,16 +51,16 @@ def unwrapResponseAPDU(channel, data, packetSize): offset = 0 if ((data is None) or (len(data) < 7 + 5)): return None - if struct.unpack(">H", str(data[offset : offset + 2]))[0] <> channel: + if struct.unpack(">H", data[offset : offset + 2])[0] != channel: raise BTChipException("Invalid channel") offset += 2 - if data[offset] <> 0x05: + if data[offset] != 0x05: raise BTChipException("Invalid tag") offset += 1 - if struct.unpack(">H", str(data[offset : offset + 2]))[0] <> sequenceIdx: + if struct.unpack(">H", data[offset : offset + 2])[0] != sequenceIdx: raise BTChipException("Invalid sequence") offset += 2 - responseLength = struct.unpack(">H", str(data[offset : offset + 2]))[0] + responseLength = struct.unpack(">H", data[offset : offset + 2])[0] offset += 2 if len(data) < 7 + responseLength: return None @@ -70,17 +70,17 @@ def unwrapResponseAPDU(channel, data, packetSize): blockSize = responseLength result = data[offset : offset + blockSize] offset += blockSize - while (len(result) <> responseLength): + while (len(result) != responseLength): sequenceIdx = sequenceIdx + 1 if (offset == len(data)): return None - if struct.unpack(">H", str(data[offset : offset + 2]))[0] <> channel: + if struct.unpack(">H", data[offset : offset + 2])[0] != channel: raise BTChipException("Invalid channel") offset += 2 - if data[offset] <> 0x05: + if data[offset] != 0x05: raise BTChipException("Invalid tag") offset += 1 - if struct.unpack(">H", str(data[offset : offset + 2]))[0] <> sequenceIdx: + if struct.unpack(">H", data[offset : offset + 2])[0] != sequenceIdx: raise BTChipException("Invalid sequence") offset += 2 if (responseLength - len(result)) > packetSize - 5: diff --git a/samples/getFirmwareVersion.py b/samples/getFirmwareVersion.py index 13266b9..57ffcd3 100644 --- a/samples/getFirmwareVersion.py +++ b/samples/getFirmwareVersion.py @@ -22,5 +22,5 @@ dongle = getDongle(True) app = btchip(dongle) -print app.getFirmwareVersion()['version'] +print(app.getFirmwareVersion()['version']) diff --git a/study/getFirmwareVersion.py b/study/getFirmwareVersion.py new file mode 100644 index 0000000..57ffcd3 --- /dev/null +++ b/study/getFirmwareVersion.py @@ -0,0 +1,26 @@ +""" +******************************************************************************* +* BTChip Bitcoin Hardware Wallet Python API +* (c) 2014 BTChip - 1BTChip7VfTnrPra5jqci7ejnMguuHogTn +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +******************************************************************************** +""" + +from btchip.btchip import * +import sys + +dongle = getDongle(True) +app = btchip(dongle) +print(app.getFirmwareVersion()['version']) + diff --git a/study/getaddress.py b/study/getaddress.py new file mode 100644 index 0000000..f20fb62 --- /dev/null +++ b/study/getaddress.py @@ -0,0 +1,20 @@ +from btchip.btchip import * +from btchip.btchipUtils import compress_public_key,format_transaction, get_regular_input_script +import sys + +def main(): +# dongle = getDongle(True) + dongle = getDongle(False) + app = btchip(dongle) + mpath = "44'/5'/0'/0" + + for i in range(11): + addr_path = mpath + '/' + str(i) + nodedata = app.getWalletPublicKey(addr_path) + publicKey = compress_public_key(nodedata.get('publicKey')) + address = (nodedata.get('address')).decode("utf-8") + print(addr_path, address) + + +if __name__ == '__main__': + main() diff --git a/study/getsignMessage.py b/study/getsignMessage.py new file mode 100644 index 0000000..837eb4d --- /dev/null +++ b/study/getsignMessage.py @@ -0,0 +1,42 @@ +from btchip.btchip import * +from btchip.btchipUtils import * +import sys +import base64 +import time + + +def main(): +# dongle = getDongle(True) + dongle = getDongle(False) + app = btchip(dongle) + + + msg = "test1234 4321" + + #addr_path = "44'/5'/0'/0/0" + addr_path = "44'/5'/0'/0/0" + nodedata = app.getWalletPublicKey(addr_path) + publicKey = compress_public_key(nodedata.get('publicKey')) + address = (nodedata.get('address')).decode("utf-8") + + info = app.signMessagePrepare(addr_path, msg) + signature = app.signMessageSign() + + rLength = signature[3] + r = signature[4 : 4 + rLength] + sLength = signature[4 + rLength + 1] + s = signature[4 + rLength + 2:] + if rLength == 33: + r = r[1:] + if sLength == 33: + s = s[1:] + + work = bytes(chr(27 + 4 + (signature[0] & 0x01)), "utf-8") + r + s + sig = base64.b64encode(work).decode("utf-8") + + print("path : %s, addr : %s, msg : \"%s\", sig : %s" % (addr_path, address, msg, sig)) + +if __name__ == '__main__': + main() + + diff --git a/study/hex2.py b/study/hex2.py new file mode 100644 index 0000000..55a7b9c --- /dev/null +++ b/study/hex2.py @@ -0,0 +1,16 @@ +import binascii + + +QWERTY_KEYMAP = bytearray("000000000000000000000000760f00d4ffffffc7000000782c1e3420212224342627252e362d3738271e1f202122232425263333362e37381f0405060708090a0b0c0d0e0f101112131415161718191a1b1c1d2f3130232d350405060708090a0b0c0d0e0f101112131415161718191a1b1c1d2f313035".decode('hex')) + +print(QWERTY_KEYMAP) +print(type(QWERTY_KEYMAP)) + + +str = '000000000000000000000000760f00d4ffffffc7000000782c1e3420212224342627252e362d3738271e1f202122232425263333362e37381f0405060708090a0b0c0d0e0f101112131415161718191a1b1c1d2f3130232d350405060708090a0b0c0d0e0f101112131415161718191a1b1c1d2f313035' + + +b = bytearray(binascii.unhexlify(str)) + +if b == QWERTY_KEYMAP: + print('1') diff --git a/study/hex3.py b/study/hex3.py new file mode 100644 index 0000000..47f3284 --- /dev/null +++ b/study/hex3.py @@ -0,0 +1,5 @@ + + +QWERTY_KEYMAP = bytearray.fromhex('000000000000000000000000760f00d4ffffffc7000000782c1e3420212224342627252e362d3738271e1f202122232425263333362e37381f0405060708090a0b0c0d0e0f101112131415161718191a1b1c1d2f3130232d350405060708090a0b0c0d0e0f101112131415161718191a1b1c1d2f313035') +print(QWERTY_KEYMAP) +print(type(QWERTY_KEYMAP)) From 00fb22f34caaab10ba6db0d0a5892c485cb2c83a Mon Sep 17 00:00:00 2001 From: Bertrand256 Date: Sat, 7 Oct 2017 17:02:59 +0200 Subject: [PATCH 2/7] Removing unnecessary bytearray to str conversion. --- btchip/btchip.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/btchip/btchip.py b/btchip/btchip.py index 134921e..a24e1a1 100644 --- a/btchip/btchip.py +++ b/btchip/btchip.py @@ -125,7 +125,7 @@ def getWalletPublicKey(self, path, showOnScreen=False, segwit=False, segwitNativ offset = 0 result['publicKey'] = response[offset + 1 : offset + 1 + response[offset]] offset = offset + 1 + response[offset] - result['address'] = str(response[offset + 1 : offset + 1 + response[offset]]) + result['address'] = response[offset + 1 : offset + 1 + response[offset]] offset = offset + 1 + response[offset] result['chainCode'] = response[offset : offset + 32] return result From b9eb45964e82e93c15116f25146c51c53cf5e0e4 Mon Sep 17 00:00:00 2001 From: Bertrand256 Date: Thu, 12 Oct 2017 22:16:20 +0200 Subject: [PATCH 3/7] Removing UTF-8 message decoding due to not supporting of non-ascii characters signing by Ledger. --- btchip/btchip.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/btchip/btchip.py b/btchip/btchip.py index a24e1a1..a553b7c 100644 --- a/btchip/btchip.py +++ b/btchip/btchip.py @@ -430,12 +430,12 @@ def signMessagePrepareV2(self, path, message): dataLength = blockLength else: dataLength = len(message) - offset - params.extend(bytearray(message[offset : offset + dataLength], 'utf8')) + params.extend(bytearray(message[offset: offset + dataLength])) apdu = [ self.BTCHIP_CLA, self.BTCHIP_INS_SIGN_MESSAGE, 0x00, p2 ] apdu.append(len(params)) apdu.extend(params) response = self.dongle.exchange(bytearray(apdu)) - encryptedOutputData = encryptedOutputData + response[1 : 1 + response[0]].decode("utf-8") + encryptedOutputData = encryptedOutputData + response[1 : 1 + response[0]].decode("ascii") offset += blockLength result['confirmationNeeded'] = response[1 + response[0]] != 0x00 result['confirmationType'] = response[1 + response[0]] From 7d1e89c1723a953fba4c3b4f8a9c7b33a2a70ab5 Mon Sep 17 00:00:00 2001 From: Bertrand256 Date: Thu, 12 Oct 2017 22:20:19 +0200 Subject: [PATCH 4/7] Removing UTF-8 message decoding due to not supporting of non-ascii characters signing by Ledger. --- btchip/btchip.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/btchip/btchip.py b/btchip/btchip.py index a553b7c..b01b6bc 100644 --- a/btchip/btchip.py +++ b/btchip/btchip.py @@ -430,7 +430,7 @@ def signMessagePrepareV2(self, path, message): dataLength = blockLength else: dataLength = len(message) - offset - params.extend(bytearray(message[offset: offset + dataLength])) + params.extend(bytearray(message[offset: offset + dataLength])) apdu = [ self.BTCHIP_CLA, self.BTCHIP_INS_SIGN_MESSAGE, 0x00, p2 ] apdu.append(len(params)) apdu.extend(params) From 31b898403a1e0d024a6adc3559754642203b6e92 Mon Sep 17 00:00:00 2001 From: Bertrand256 Date: Fri, 27 Oct 2017 14:46:15 +0200 Subject: [PATCH 5/7] Removed response decoding and fixed initial type of 'encryptedOutputData' to bytes. --- btchip/btchip.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/btchip/btchip.py b/btchip/btchip.py index b01b6bc..f5e3e06 100644 --- a/btchip/btchip.py +++ b/btchip/btchip.py @@ -415,9 +415,9 @@ def signMessagePrepareV2(self, path, message): self.resolvePublicKeysInPath(path) result = {} offset = 0 - encryptedOutputData = "" + encryptedOutputData = b"" while (offset < len(message)): - params = []; + params = [] if offset == 0: params.extend(donglePath) params.append((len(message) >> 8) & 0xff) @@ -435,7 +435,7 @@ def signMessagePrepareV2(self, path, message): apdu.append(len(params)) apdu.extend(params) response = self.dongle.exchange(bytearray(apdu)) - encryptedOutputData = encryptedOutputData + response[1 : 1 + response[0]].decode("ascii") + encryptedOutputData = encryptedOutputData + response[1 : 1 + response[0]] offset += blockLength result['confirmationNeeded'] = response[1 + response[0]] != 0x00 result['confirmationType'] = response[1 + response[0]] From 8047a60d2b95f97c9369fe87fa99383b376439dc Mon Sep 17 00:00:00 2001 From: Bertrand256 Date: Wed, 8 Nov 2017 11:41:52 +0100 Subject: [PATCH 6/7] Changes related to Python 3 support. --- btchip/bitcoinTransaction.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/btchip/bitcoinTransaction.py b/btchip/bitcoinTransaction.py index 35276e9..14737ae 100644 --- a/btchip/bitcoinTransaction.py +++ b/btchip/bitcoinTransaction.py @@ -23,9 +23,9 @@ class bitcoinInput: def __init__(self, bufferOffset=None): - self.prevOut = "" - self.script = "" - self.sequence = "" + self.prevOut = b"" + self.script = b"" + self.sequence = b"" if bufferOffset is not None: buf = bufferOffset['buffer'] offset = bufferOffset['offset'] @@ -48,16 +48,16 @@ def serialize(self): return result def __str__(self): - buf = "Prevout : " + hexlify(self.prevOut) + "\r\n" - buf += "Script : " + hexlify(self.script) + "\r\n" - buf += "Sequence : " + hexlify(self.sequence) + "\r\n" + buf = "Prevout : " + self.prevOut.hex() + "\r\n" + buf += "Script : " + self.script.hex() + "\r\n" + buf += "Sequence : " + self.sequence.hex() + "\r\n" return buf class bitcoinOutput: def __init__(self, bufferOffset=None): - self.amount = "" - self.script = "" + self.amount = b"" + self.script = b"" if bufferOffset is not None: buf = bufferOffset['buffer'] offset = bufferOffset['offset'] @@ -77,20 +77,20 @@ def serialize(self): return result def __str__(self): - buf = "Amount : " + hexlify(self.amount) + "\r\n" - buf += "Script : " + hexlify(self.script) + "\r\n" + buf = "Amount : " + self.amount.hex() + "\r\n" + buf += "Script : " + self.script.hex() + "\r\n" return buf class bitcoinTransaction: def __init__(self, data=None): - self.version = "" + self.version = b"" self.inputs = [] self.outputs = [] - self.lockTime = "" + self.lockTime = b"" self.witness = False - self.witnessScript = "" + self.witnessScript = b"" if data is not None: offset = 0 self.version = data[offset:offset + 4] @@ -148,7 +148,7 @@ def serializeOutputs(self): return result def __str__(self): - buf = "Version : " + hexlify(self.version) + "\r\n" + buf = "Version : " + self.version.hex() + "\r\n" index = 1 for trinput in self.inputs: buf += "Input #" + str(index) + "\r\n" @@ -159,7 +159,7 @@ def __str__(self): buf += "Output #" + str(index) + "\r\n" buf += str(troutput) index+=1 - buf += "Locktime : " + hexlify(self.lockTime) + "\r\n" + buf += "Locktime : " + self.lockTime.hex() + "\r\n" if self.witness: - buf += "Witness script : " + hexlify(self.witnessScript) + "\r\n" + buf += "Witness script : " + self.witnessScript.hex() + "\r\n" return buf From b385df16ff13d169d1da49805937b66b36c6d790 Mon Sep 17 00:00:00 2001 From: Bertrand256 Date: Thu, 23 Jan 2025 13:35:46 +0100 Subject: [PATCH 7/7] Update setup.py --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index accfe24..d45fa5f 100644 --- a/setup.py +++ b/setup.py @@ -17,7 +17,7 @@ packages=find_packages(), install_requires=['hidapi>=0.7.99', 'ecdsa>=0.9'], extras_require = { - 'smartcard': [ 'python-pyscard>=1.6.12-4build1' ] + 'smartcard': [ 'python-pyscard>=1.6.12-4' ] }, include_package_data=True, zip_safe=False,