2012-11-13 16:07:11 +00:00
|
|
|
import os
|
2012-11-15 10:42:19 +00:00
|
|
|
import bitkey_pb2 as proto
|
2012-12-03 15:36:03 +00:00
|
|
|
import random
|
2012-11-13 16:07:11 +00:00
|
|
|
|
|
|
|
def show_message(message):
|
|
|
|
print "MESSAGE FROM DEVICE:", message
|
|
|
|
|
|
|
|
def show_input(input_text, message=None):
|
|
|
|
if message:
|
|
|
|
print "QUESTION FROM DEVICE:", message
|
|
|
|
return raw_input(input_text)
|
|
|
|
|
2013-01-14 16:04:58 +00:00
|
|
|
class CallException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class PinException(CallException):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class OtpException(CallException):
|
|
|
|
pass
|
|
|
|
|
2012-11-13 16:07:11 +00:00
|
|
|
class BitkeyClient(object):
|
|
|
|
|
2012-12-03 15:36:03 +00:00
|
|
|
def __init__(self, transport, debuglink=None,
|
|
|
|
algo=proto.BIP32,
|
|
|
|
message_func=show_message, input_func=show_input, debug=False):
|
|
|
|
self.transport = transport
|
2012-11-15 20:10:23 +00:00
|
|
|
self.debuglink = debuglink
|
2012-11-13 16:07:11 +00:00
|
|
|
|
2012-12-03 15:36:03 +00:00
|
|
|
self.algo = algo
|
2012-11-13 16:07:11 +00:00
|
|
|
self.message_func = message_func
|
|
|
|
self.input_func = input_func
|
|
|
|
self.debug = debug
|
2012-12-03 15:36:03 +00:00
|
|
|
|
|
|
|
self.setup_debuglink()
|
|
|
|
self.init_device()
|
2013-01-14 18:22:02 +00:00
|
|
|
|
|
|
|
def _get_local_entropy(self):
|
|
|
|
return os.urandom(32)
|
2012-12-03 15:36:03 +00:00
|
|
|
|
|
|
|
def init_device(self):
|
|
|
|
self.master_public_key = None
|
|
|
|
self.session_id = ''.join([ chr(random.randrange(0, 255, 1)) for _ in xrange(0, 16) ])
|
|
|
|
self.features = self.call(proto.Initialize(session_id=self.session_id))
|
2012-11-13 16:07:11 +00:00
|
|
|
self.UUID = self.call(proto.GetUUID())
|
2012-12-03 15:36:03 +00:00
|
|
|
|
|
|
|
def get_master_public_key(self):
|
|
|
|
if self.master_public_key:
|
|
|
|
return self.master_public_key
|
|
|
|
|
2013-01-05 14:42:49 +00:00
|
|
|
self.master_public_key = self.call(proto.GetMasterPublicKey(algo=self.algo)).key
|
2012-12-03 15:36:03 +00:00
|
|
|
return self.master_public_key
|
|
|
|
|
2013-01-05 14:42:49 +00:00
|
|
|
def get_address(self, n):
|
|
|
|
return self.call(proto.GetAddress(algo=self.algo, address_n=n)).address
|
|
|
|
|
2012-12-03 15:36:03 +00:00
|
|
|
def get_entropy(self, size):
|
|
|
|
return self.call(proto.GetEntropy(size=size)).entropy
|
|
|
|
|
2012-11-13 16:07:11 +00:00
|
|
|
def _pprint(self, msg):
|
|
|
|
return "<%s>:\n%s" % (msg.__class__.__name__, msg)
|
|
|
|
|
2012-12-13 19:05:04 +00:00
|
|
|
def setup_debuglink(self, button=None, pin_correct=False, otp_correct=False):
|
|
|
|
self.debug_button = button
|
2012-12-03 15:36:03 +00:00
|
|
|
self.debug_pin = pin_correct
|
|
|
|
self.debug_otp = otp_correct
|
|
|
|
|
2012-12-13 19:05:04 +00:00
|
|
|
def call(self, msg):
|
2012-11-13 16:07:11 +00:00
|
|
|
if self.debug:
|
|
|
|
print '----------------------'
|
|
|
|
print "Sending", self._pprint(msg)
|
|
|
|
|
2012-11-15 20:10:23 +00:00
|
|
|
self.transport.write(msg)
|
2012-12-13 19:05:04 +00:00
|
|
|
resp = self.transport.read_blocking()
|
2012-11-15 20:10:23 +00:00
|
|
|
|
2012-12-13 19:05:04 +00:00
|
|
|
if isinstance(resp, proto.ButtonRequest):
|
|
|
|
if self.debuglink and self.debug_button:
|
|
|
|
print "Pressing button", self.debug_button
|
|
|
|
self.debuglink.press_button(self.debug_button)
|
2013-01-14 16:04:58 +00:00
|
|
|
|
|
|
|
return self.call(proto.ButtonAck())
|
2012-11-15 20:10:23 +00:00
|
|
|
|
2012-11-13 16:07:11 +00:00
|
|
|
if isinstance(resp, proto.OtpRequest):
|
2012-11-15 20:10:23 +00:00
|
|
|
if self.debuglink:
|
|
|
|
otp = self.debuglink.read_otp()
|
2012-12-03 15:36:03 +00:00
|
|
|
if self.debug_otp:
|
2012-12-05 19:45:53 +00:00
|
|
|
msg2 = otp
|
2012-11-15 20:10:23 +00:00
|
|
|
else:
|
2012-12-05 19:45:53 +00:00
|
|
|
msg2 = proto.OtpAck(otp='__42__')
|
2012-11-15 20:10:23 +00:00
|
|
|
else:
|
|
|
|
otp = self.input_func("OTP required: ", resp.message)
|
2012-12-05 19:45:53 +00:00
|
|
|
msg2 = proto.OtpAck(otp=otp)
|
2012-11-15 20:10:23 +00:00
|
|
|
|
2012-12-13 19:05:04 +00:00
|
|
|
return self.call(msg2)
|
2012-11-13 16:07:11 +00:00
|
|
|
|
|
|
|
if isinstance(resp, proto.PinRequest):
|
2012-11-15 20:10:23 +00:00
|
|
|
if self.debuglink:
|
|
|
|
pin = self.debuglink.read_pin()
|
2012-12-03 15:36:03 +00:00
|
|
|
if self.debug_pin:
|
2012-12-05 19:45:53 +00:00
|
|
|
msg2 = pin
|
2012-11-15 20:10:23 +00:00
|
|
|
else:
|
2012-12-05 19:45:53 +00:00
|
|
|
msg2 = proto.PinAck(pin='__42__')
|
2012-11-15 20:10:23 +00:00
|
|
|
else:
|
|
|
|
pin = self.input_func("PIN required: ", resp.message)
|
2012-12-05 19:45:53 +00:00
|
|
|
msg2 = proto.PinAck(pin=pin)
|
2012-11-15 20:10:23 +00:00
|
|
|
|
2012-12-13 19:05:04 +00:00
|
|
|
return self.call(msg2)
|
2012-11-13 16:07:11 +00:00
|
|
|
|
|
|
|
if isinstance(resp, proto.Failure):
|
|
|
|
self.message_func(resp.message)
|
|
|
|
|
|
|
|
if resp.code == 3:
|
2013-01-14 16:04:58 +00:00
|
|
|
raise OtpException("OTP is invalid")
|
2012-11-13 16:07:11 +00:00
|
|
|
|
|
|
|
elif resp.code == 4:
|
2013-01-14 16:04:58 +00:00
|
|
|
raise CallException("Action cancelled by user")
|
2012-11-13 16:07:11 +00:00
|
|
|
|
|
|
|
elif resp.code == 6:
|
2013-01-14 16:04:58 +00:00
|
|
|
raise PinException("PIN is invalid")
|
2012-11-13 16:07:11 +00:00
|
|
|
|
2013-01-14 16:04:58 +00:00
|
|
|
raise CallException(resp.code, resp.message)
|
2012-11-13 16:07:11 +00:00
|
|
|
|
|
|
|
if self.debug:
|
|
|
|
print "Received", self._pprint(resp)
|
|
|
|
|
|
|
|
return resp
|
|
|
|
|
2012-12-03 15:36:03 +00:00
|
|
|
def get_uuid(self):
|
2013-01-05 14:42:49 +00:00
|
|
|
return self.call(proto.GetUUID()).UUID
|
2012-12-03 15:36:03 +00:00
|
|
|
|
|
|
|
def sign_tx(self, inputs, outputs):
|
2012-11-13 16:07:11 +00:00
|
|
|
'''
|
|
|
|
inputs: list of TxInput
|
|
|
|
outputs: list of TxOutput
|
2013-01-14 18:22:02 +00:00
|
|
|
|
|
|
|
proto.TxInput(index=0,
|
|
|
|
address_n=0,
|
|
|
|
amount=0,
|
|
|
|
prev_hash='',
|
|
|
|
prev_index=0,
|
|
|
|
#script_sig=
|
|
|
|
)
|
|
|
|
proto.TxOutput(index=0,
|
|
|
|
address='1Bitkey',
|
|
|
|
#address_n=[],
|
|
|
|
amount=100000000,
|
|
|
|
script_type=proto.PAYTOADDRESS,
|
|
|
|
#script_args=
|
|
|
|
)
|
2012-11-13 16:07:11 +00:00
|
|
|
'''
|
2013-01-14 18:22:02 +00:00
|
|
|
|
|
|
|
# Prepare and send initial message
|
2012-11-13 16:07:11 +00:00
|
|
|
tx = proto.SignTx()
|
2012-12-03 15:36:03 +00:00
|
|
|
tx.algo = self.algo # Choose BIP32 or ELECTRUM way for deterministic keys
|
2013-01-14 18:22:02 +00:00
|
|
|
tx.random = self._get_local_entropy() # Provide additional entropy to the device
|
|
|
|
tx.inputs_count = len(inputs)
|
|
|
|
tx.outputs_count = len(outputs)
|
|
|
|
res = self.call(tx)
|
|
|
|
|
|
|
|
# Prepare structure for signatures
|
|
|
|
signatures = [None]*len(inputs)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
if isinstance(res, proto.OutputRequest):
|
|
|
|
res = self.call(outputs[res.request_index])
|
|
|
|
continue
|
2012-11-13 16:07:11 +00:00
|
|
|
|
2013-01-14 18:22:02 +00:00
|
|
|
if isinstance(res, proto.InputRequest):
|
|
|
|
if res.signed_index >= 0:
|
|
|
|
print "!!! SIGNED INPUT"
|
|
|
|
signatures[res.signed_index] = res.signature
|
|
|
|
|
|
|
|
if res.request_index >= 0:
|
|
|
|
print "REQUESTING", res.request_index
|
|
|
|
res = self.call(inputs[res.request_index])
|
|
|
|
continue
|
|
|
|
|
|
|
|
# There was no request for another input,
|
|
|
|
# so we're done!
|
|
|
|
break
|
|
|
|
|
|
|
|
if isinstance(res, proto.Failure):
|
|
|
|
raise CallException("Signing failed")
|
|
|
|
|
|
|
|
return signatures
|
|
|
|
|
2012-12-03 15:36:03 +00:00
|
|
|
#print "PBDATA", tx.SerializeToString().encode('hex')
|
2012-11-13 16:07:11 +00:00
|
|
|
|
|
|
|
#################
|
|
|
|
#################
|
|
|
|
#################
|
|
|
|
|
2012-12-03 15:36:03 +00:00
|
|
|
'''
|
2012-11-13 16:07:11 +00:00
|
|
|
signatures = [('add550d6ba9ab7e01d37e17658f98b6e901208d241f24b08197b5e20dfa7f29f095ae01acbfa5c4281704a64053dcb80e9b089ecbe09f5871d67725803e36edd', '3045022100dced96eeb43836bc95676879eac303eabf39802e513f4379a517475c259da12502201fd36c90ecd91a32b2ca8fed2e1755a7f2a89c2d520eb0da10147802bc7ca217')]
|
|
|
|
|
|
|
|
s_inputs = []
|
|
|
|
for i in range(len(inputs)):
|
|
|
|
addr, v, p_hash, p_pos, p_scriptPubKey, _, _ = inputs[i]
|
|
|
|
pubkey = signatures[i][0].decode('hex')
|
|
|
|
sig = signatures[i][1].decode('hex')
|
|
|
|
s_inputs.append((addr, v, p_hash, p_pos, p_scriptPubKey, pubkey, sig))
|
|
|
|
|
|
|
|
return s_inputs
|
2012-12-03 15:36:03 +00:00
|
|
|
|
2012-11-13 16:07:11 +00:00
|
|
|
s_inputs = []
|
|
|
|
for i in range(len(inputs)):
|
|
|
|
addr, v, p_hash, p_pos, p_scriptPubKey, _, _ = inputs[i]
|
|
|
|
private_key = ecdsa.SigningKey.from_string( self.get_private_key(addr, password), curve = SECP256k1 )
|
|
|
|
public_key = private_key.get_verifying_key()
|
|
|
|
pubkey = public_key.to_string()
|
|
|
|
tx = filter( raw_tx( inputs, outputs, for_sig = i ) )
|
|
|
|
sig = private_key.sign_digest( Hash( tx.decode('hex') ), sigencode = ecdsa.util.sigencode_der )
|
|
|
|
assert public_key.verify_digest( sig, Hash( tx.decode('hex') ), sigdecode = ecdsa.util.sigdecode_der)
|
|
|
|
s_inputs.append( (addr, v, p_hash, p_pos, p_scriptPubKey, pubkey, sig) )
|
|
|
|
return s_inputs
|
|
|
|
'''
|
2012-12-03 15:36:03 +00:00
|
|
|
|
2013-01-14 18:22:02 +00:00
|
|
|
def reset_device(self):
|
|
|
|
resp = self.call(proto.ResetDevice(random=self._get_local_entropy()))
|
|
|
|
self.init_device()
|
|
|
|
return isinstance(resp, proto.Success)
|
|
|
|
|
2012-12-13 19:05:04 +00:00
|
|
|
def load_device(self, seed, otp, pin, spv):
|
|
|
|
resp = self.call(proto.LoadDevice(seed=seed, otp=otp, pin=pin, spv=spv))
|
|
|
|
self.init_device()
|
|
|
|
return isinstance(resp, proto.Success)
|