[Python-modules-commits] [btchip-python] 01/03: Import btchip-python_0.1.18.orig.tar.gz

Tristan Seligmann mithrandi at moszumanska.debian.org
Sun Nov 20 22:24:42 UTC 2016


This is an automated email from the git hooks/post-receive script.

mithrandi pushed a commit to branch master
in repository btchip-python.

commit 6bb0e667aabe355955de8c253028058c03f0685f
Author: Tristan Seligmann <mithrandi at mithrandi.net>
Date:   Mon Nov 21 00:17:13 2016 +0200

    Import btchip-python_0.1.18.orig.tar.gz
---
 MANIFEST.in          |  2 ++
 btchip/btchip.py     | 76 +++++++++++++++++++++++++++++++++++++++++++---------
 btchip/btchipComm.py |  2 +-
 setup.py             |  5 ++--
 4 files changed, 70 insertions(+), 15 deletions(-)

diff --git a/MANIFEST.in b/MANIFEST.in
new file mode 100644
index 0000000..1c2510d
--- /dev/null
+++ b/MANIFEST.in
@@ -0,0 +1,2 @@
+include README.md
+
diff --git a/btchip/btchip.py b/btchip/btchip.py
index a419810..374b261 100644
--- a/btchip/btchip.py
+++ b/btchip/btchip.py
@@ -323,8 +323,13 @@ class btchip:
 			response = self.dongle.exchange(bytearray(apdu))
 			encryptedOutputData = encryptedOutputData + response[1 : 1 + response[0]]
 			offset += dataLength
-		result['confirmationNeeded'] = response[1 + response[0]] <> 0x00
-		result['confirmationType'] = response[1 + response[0]]
+		if len(response) > 1: 
+			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['confirmationType'] = response[0]
 		if result['confirmationType'] == 0x02:
 			result['keycardData'] = response[1 + response[0] + 1:] # legacy
 		if result['confirmationType'] == 0x03:
@@ -358,7 +363,7 @@ class btchip:
 		result[0] = 0x30
 		return result
 
-	def signMessagePrepare(self, path, message):
+	def signMessagePrepareV1(self, path, message):
 		donglePath = parse_bip32_path(path)
 		if self.needKeyCache:
 			self.resolvePublicKeysInPath(path)		
@@ -373,10 +378,57 @@ class btchip:
 		response = self.dongle.exchange(bytearray(apdu))
 		result['confirmationNeeded'] = response[0] <> 0x00
 		result['confirmationType'] = response[0]
-                if result['confirmationType'] == 0x02:
-                        result['keycardData'] = response[1:]
-                if result['confirmationType'] == 0x03:
-                        result['secureScreenData'] = response[1:]
+		if result['confirmationType'] == 0x02:
+			result['keycardData'] = response[1:]
+		if result['confirmationType'] == 0x03:
+			result['secureScreenData'] = response[1:]
+		return result
+
+	def signMessagePrepareV2(self, path, message):
+		donglePath = parse_bip32_path(path)
+		if self.needKeyCache:
+			self.resolvePublicKeysInPath(path)				
+		result = {}
+		offset = 0
+		encryptedOutputData = ""
+		while (offset < len(message)):
+			params = [];
+			if offset == 0:
+				params.extend(donglePath)
+				params.append((len(message) >> 8) & 0xff)
+				params.append(len(message) & 0xff)
+				p2 = 0x01
+			else:
+				p2 = 0x80
+			blockLength = 255 - len(params)
+			if ((offset + blockLength) < len(message)):
+				dataLength = blockLength
+			else:
+				dataLength = len(message) - offset
+			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]]
+			offset += blockLength
+		result['confirmationNeeded'] = response[1 + response[0]] <> 0x00
+		result['confirmationType'] = response[1 + response[0]]
+		if result['confirmationType'] == 0x03:
+			offset = 1 + response[0] + 1
+			result['secureScreenData'] = response[offset:]			
+			result['encryptedOutputData'] = encryptedOutputData 
+
+		return result
+
+	def signMessagePrepare(self, path, message):
+		try:
+			result = self.signMessagePrepareV2(path, message)
+		except BTChipException as e:
+			if (e.sw == 0x6b00): # Old firmware version, try older method
+				result = self.signMessagePrepareV1(path, message)
+			else:
+				raise
 		return result
 
 	def signMessageSign(self, pin=""):
@@ -423,11 +475,11 @@ class btchip:
 		self.setKeymapEncoding(keymapEncoding)
 		try:
 			self.setTypingBehaviour(0xff, 0xff, 0xff, 0x10)	
-                except BTChipException as e:
-                        if (e.sw == 0x6700): # Old firmware version, command not supported
-                                pass
-                        else:
-                                raise
+		except BTChipException as e:
+			if (e.sw == 0x6700): # Old firmware version, command not supported
+				pass
+			else:
+				raise
 		return result
 
 	def setKeymapEncoding(self, keymapEncoding):
diff --git a/btchip/btchipComm.py b/btchip/btchipComm.py
index b522179..0e4668c 100644
--- a/btchip/btchipComm.py
+++ b/btchip/btchipComm.py
@@ -178,7 +178,7 @@ def getDongle(debug=False):
 		if hidDevice['vendor_id'] == 0x2581 and hidDevice['product_id'] == 0x4b7c:
 			hidDevicePath = hidDevice['path']
 			ledger = True
-		if hidDevice['vendor_id'] == 0x2c97 and hidDevice['product_id'] == 0x0000:
+		if hidDevice['vendor_id'] == 0x2c97:
 			hidDevicePath = hidDevice['path']
 			ledger = True
 		if hidDevice['vendor_id'] == 0x2581 and hidDevice['product_id'] == 0x1807:
diff --git a/setup.py b/setup.py
index 9d015a3..e5a5074 100644
--- a/setup.py
+++ b/setup.py
@@ -7,11 +7,12 @@ from os.path import dirname, join
 here = dirname(__file__)
 setup(
     name='btchip-python',
-    version='0.1.17',
+    version='0.1.18',
     author='BTChip',
     author_email='hello at ledger.fr',
     description='Python library to communicate with Ledger Nano dongle',
     long_description=open(join(here, 'README.md')).read(),
+    url='https://github.com/LedgerHQ/btchip-python',
     packages=find_packages(),
     install_requires=['hidapi>=0.7.99'],
     extras_require = {
@@ -22,7 +23,7 @@ setup(
     classifiers=[
 	'License :: OSI Approved :: Apache Software License',
         'Operating System :: POSIX :: Linux',
-        'Operating System :: POSIX :: Windows',
+        'Operating System :: Microsoft :: Windows',
 	'Operating System :: MacOS :: MacOS X'
     ]
 )

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/btchip-python.git



More information about the Python-modules-commits mailing list