Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _hardware_wallet_init(self, auth_type):
"""Initialize hardware wallets"""
self._ledger = None
if auth_type in ["ledger", "trezor"]:
assert auth_type == 'ledger', "Unsupported hardware wallet, only Ledger is supported at this time"
try:
self._ledger = LedgerWallet()
return True
except CommException:
self.logger.error('No Ledger USB dongle found!')
raise
# When more data is available, the chip sends 0x61XX
# So 0x61xx as a SW must not be interpreted as an error
if (sw & 0xFF00) != 0x6100:
raise CommException("Invalid status %04x" % sw, sw)
else:
while (sw & 0xFF00) == 0x6100:
send_apdu(bytes.fromhex("00c0000000")) # GET RESPONSE
(sw, data) = get_data()
response += data
# Check that the last received SW is indeed 0x9000
if sw == 0x9000:
return bytearray(response)
# In any other case return an exception
raise CommException("Invalid status %04x" % sw, sw)
padded_ans = ans[2:].zfill(192)
print("y^2 from python", padded_ans)
print("y^2 from ledger", signature.hex()[:192])
# x^3 + ax + b
ansx = hex((pow(mnt6_g1_x, 3, field_modulus) + a*mnt6_g1_x + b) % field_modulus)
padded_ansx = ansx[2:].zfill(192)
print("x^2 + ax + b from python", padded_ansx)
print("x^2 + ax + b from ledger", signature.hex()[192:])
print(signature.hex()[:192] == padded_ansx)
print(signature.hex()[192:] == padded_ans)
print(signature.hex()[:192] == signature.hex()[192:])
# print "verified " + str(publicKey.ecdsa_verify(bytes(textToSign), signature))
except CommException as comm:
if comm.sw == 0x6985:
print('Aborted by user')
else:
print('Invalid status %x' % comm.sw)
if (len(textToSign) - offset) > 255:
chunk = textToSign[offset : offset + 255]
else:
chunk = textToSign[offset:]
if (offset + len(chunk)) == len(textToSign):
p1 = 0x80
else:
p1 = 0x00
apdu = bytes("8002".decode('hex')) + chr(p1) + chr(0x00) + chr(len(chunk)) + bytes(chunk)
signature += dongle.exchange(apdu)
offset += len(chunk)
print "signature " + str(signature).encode('hex')
publicKey = PublicKey(bytes(publicKey), raw=True)
signature = publicKey.ecdsa_deserialize(bytes(signature))
print "verified " + str(publicKey.ecdsa_verify(bytes(textToSign), signature))
except CommException as comm:
if comm.sw == 0x6985:
print "Aborted by user"
else:
print "Invalid status " + comm.sw
elif len(sys.argv) == 5:
(_, request, pkno, h, outfile) = sys.argv
if request == 'transaction':
# in this case h will be a file
get_transaction(pkno, h, outfile)
elif request == 'streamedtransaction':
# in this case h will be a file
stream_sign(pkno, h, outfile)
else:
print(errstring % sys.argv[0])
else:
print(errstring % sys.argv[0])
except CommException as comm:
if comm.sw == 0x6985:
print('Aborted by user')
elif comm.sw == 0x6B00:
print('SW_DEVELOPER_ERR')
elif comm.sw == 0x6B01:
print('SW_INVALID_PARAM')
elif comm.sw == 0x6B02:
print('SW_IMPROPER_INIT')
else:
print('Invalid status from Ledger: ', comm.sw, 'Is the device unlocked?')
def _exchange_ledger(self,cmd,data=None, sw=0x9000):
resp = b''
cond = True
while cond:
try:
resp = resp + self.token.exchange(cmd,300)
sw = 0x9000
cond = False
except CommException as e:
if (e.data) :
resp = resp + e.data
sw = e.sw
if (sw&0xFF00) == 0x6100 :
cmd = binascii.unhexlify("00C00000%.02x"%(sw&0xFF))
else:
cond = False
return resp,sw
def __init__(self, server, port, debug=False):
self.server = server
self.port = port
self.debug = debug
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.opened = True
try:
self.socket.connect((self.server, self.port))
except:
raise CommException("Proxy connection failed")
blockSize = responseLength
result = data[offset : offset + blockSize]
offset += blockSize
while (len(result) != responseLength):
sequenceIdx = sequenceIdx + 1
if (offset == len(data)):
return None
if not ble:
if struct.unpack(">H", data[offset : offset + 2])[0] != channel:
raise CommException("Invalid channel")
offset += 2
if data[offset] != 0x05:
raise CommException("Invalid tag")
offset += 1
if struct.unpack(">H", data[offset : offset + 2])[0] != sequenceIdx:
raise CommException("Invalid sequence")
offset += 2
if (responseLength - len(result)) > packetSize - 3 - extraHeaderSize:
blockSize = packetSize - 3 - extraHeaderSize
else:
blockSize = responseLength - len(result)
result += data[offset : offset + blockSize]
offset += blockSize
return bytearray(result)
def getKeysFromDongle(path, networkByte):
global dongle
while (True):
try:
data_bytes = bytes(("800400" + '{0:x}'.format(ord(networkByte)) + "14").decode('hex')) + path_to_bytes(path)
data = dongle.exchange(data_bytes)
return [data[0:32], data[32:67]]
except CommException as e:
if (e.sw == 0x6985):
print(colors.fg.red + "Required condition failed." + colors.reset)
if (e.sw == 0x9100):
print(colors.fg.red + "User denied signing request on Ledger Nano S device." + colors.reset)
break
except Exception as e:
raw_input(
"An error occurred while processing the request, repeat or correct your request (note what all the bip32 path parts should be hardened)")
sys.exc_clear()
break
def waitFirstResponse(self, timeout):
raise CommException("Invalid use")