forked from spesmilo/electrum
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ledger wallet support #398
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from struct import pack, unpack | ||
from distutils.version import LooseVersion | ||
import hashlib | ||
import sys | ||
import traceback | ||
|
@@ -26,6 +27,9 @@ | |
BTCHIP = False | ||
|
||
class Ledger_Client(): | ||
|
||
BITCOIN_CASH_SUPPORT = "1.1.8" | ||
|
||
def __init__(self, hidDevice): | ||
self.dongleObject = btchip(hidDevice) | ||
self.preflightDone = False | ||
|
@@ -96,16 +100,13 @@ def is_pin_validated(self, client): | |
return True | ||
raise e | ||
|
||
def supports_multi_output(self): | ||
return self.multiOutputSupported | ||
|
||
def perform_hw1_preflight(self): | ||
try: | ||
firmwareInfo = self.dongleObject.getFirmwareVersion() | ||
firmware = firmwareInfo['version'].split(".") | ||
self.multiOutputSupported = int(firmware[0]) >= 1 and int(firmware[1]) >= 1 and int(firmware[2]) >= 4 | ||
firmware = firmwareInfo['version'] | ||
bitcoinCashSupport = LooseVersion(firmware) >= LooseVersion(self.BITCOIN_CASH_SUPPORT) | ||
|
||
if not checkFirmware(firmware): | ||
if not checkFirmware(firmwareInfo) or not bitcoinCashSupport: | ||
self.dongleObject.dongle.close() | ||
raise Exception("HW1 firmware version too old. Please update at https://www.ledgerwallet.com") | ||
try: | ||
|
@@ -141,7 +142,7 @@ def checkDevice(self): | |
self.perform_hw1_preflight() | ||
except BTChipException as e: | ||
if (e.sw == 0x6d00): | ||
raise BaseException("Device not in Bitcoin mode") | ||
raise BaseException("Device not in Bitcoin Cash mode") | ||
raise e | ||
self.preflightDone = True | ||
|
||
|
@@ -295,16 +296,13 @@ def sign_transaction(self, tx, password): | |
for txout in tx.outputs(): | ||
output_type, addr, amount = txout | ||
txOutput += int_to_hex(amount, 8) | ||
script = tx.pay_script(output_type, addr) | ||
script = tx.pay_script(addr) | ||
txOutput += var_int(len(script)//2) | ||
txOutput += script | ||
txOutput = bfh(txOutput) | ||
|
||
# Recognize outputs - only one output and one change is authorized | ||
if not p2shTransaction: | ||
if not self.get_client_electrum().supports_multi_output(): | ||
if len(tx.outputs()) > 2: | ||
self.give_error("Transaction with more than 2 outputs not supported") | ||
for _type, address, amount in tx.outputs(): | ||
assert _type == TYPE_ADDRESS | ||
info = tx.output_info.get(address) | ||
|
@@ -321,47 +319,38 @@ def sign_transaction(self, tx, password): | |
# Get trusted inputs from the original transactions | ||
for utxo in inputs: | ||
sequence = int_to_hex(utxo[5], 4) | ||
if not p2shTransaction: | ||
txtmp = bitcoinTransaction(bfh(utxo[0])) | ||
trustedInput = self.get_client().getTrustedInput(txtmp, utxo[1]) | ||
trustedInput['sequence'] = sequence | ||
chipInputs.append(trustedInput) | ||
redeemScripts.append(txtmp.outputs[utxo[1]].script) | ||
else: | ||
tmp = bfh(utxo[3])[::-1] | ||
tmp += bfh(int_to_hex(utxo[1], 4)) | ||
chipInputs.append({'value' : tmp, 'sequence' : sequence}) | ||
redeemScripts.append(bfh(utxo[2])) | ||
txtmp = bitcoinTransaction(bfh(utxo[0])) | ||
tmp = bfh(utxo[3])[::-1] | ||
tmp += bfh(int_to_hex(utxo[1], 4)) | ||
tmp += txtmp.outputs[utxo[1]].amount | ||
chipInputs.append({'value' : tmp, 'witness' : True, 'sequence' : sequence}) | ||
redeemScripts.append(bfh(utxo[2])) | ||
|
||
# Sign all inputs | ||
firstTransaction = True | ||
inputIndex = 0 | ||
rawTx = tx.serialize() | ||
self.get_client().enableAlternate2fa(False) | ||
if True: | ||
while inputIndex < len(inputs): | ||
self.get_client().startUntrustedTransaction(firstTransaction, inputIndex, | ||
chipInputs, redeemScripts[inputIndex]) | ||
outputData = self.get_client().finalizeInputFull(txOutput) | ||
outputData['outputData'] = txOutput | ||
if firstTransaction: | ||
transactionOutput = outputData['outputData'] | ||
if outputData['confirmationNeeded']: | ||
outputData['address'] = output | ||
self.handler.finished() | ||
pin = self.handler.get_auth( outputData ) # does the authenticate dialog and returns pin | ||
if not pin: | ||
raise UserWarning() | ||
if pin != 'paired': | ||
self.handler.show_message(_("Confirmed. Signing Transaction...")) | ||
else: | ||
# Sign input with the provided PIN | ||
inputSignature = self.get_client().untrustedHashSign(inputsPaths[inputIndex], pin, lockTime=tx.locktime) | ||
inputSignature[0] = 0x30 # force for 1.4.9+ | ||
signatures.append(inputSignature) | ||
inputIndex = inputIndex + 1 | ||
if pin != 'paired': | ||
firstTransaction = False | ||
self.get_client().startUntrustedTransaction(True, inputIndex, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice clean up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only BIP143 signing is needed |
||
chipInputs, redeemScripts[inputIndex]) | ||
outputData = self.get_client().finalizeInputFull(txOutput) | ||
outputData['outputData'] = txOutput | ||
transactionOutput = outputData['outputData'] | ||
if outputData['confirmationNeeded']: | ||
outputData['address'] = output | ||
self.handler.finished() | ||
pin = self.handler.get_auth( outputData ) # does the authenticate dialog and returns pin | ||
if not pin: | ||
raise UserWarning() | ||
if pin != 'paired': | ||
self.handler.show_message(_("Confirmed. Signing Transaction...")) | ||
while inputIndex < len(inputs): | ||
singleInput = [ chipInputs[inputIndex] ] | ||
self.get_client().startUntrustedTransaction(False, 0, | ||
singleInput, redeemScripts[inputIndex]) | ||
inputSignature = self.get_client().untrustedHashSign(inputsPaths[inputIndex], pin, lockTime=tx.locktime, sighashType=tx.nHashType()) | ||
inputSignature[0] = 0x30 # force for 1.4.9+ | ||
signatures.append(inputSignature) | ||
inputIndex = inputIndex + 1 | ||
except UserWarning: | ||
self.handler.show_error(_('Cancelled by user')) | ||
return | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this support both P2SH and non-P2SH transactions using the Nano S?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've only tested 2 cosigner multisig, but it should (since all transactions are BIP143, unlike core where its only segwit)