Implemented responder for signing
Basic signtx unit test
This commit is contained in:
parent
80e25b1410
commit
2730631e6d
@ -35,6 +35,9 @@ class BitkeyClient(object):
|
|||||||
self.setup_debuglink()
|
self.setup_debuglink()
|
||||||
self.init_device()
|
self.init_device()
|
||||||
|
|
||||||
|
def _get_local_entropy(self):
|
||||||
|
return os.urandom(32)
|
||||||
|
|
||||||
def init_device(self):
|
def init_device(self):
|
||||||
self.master_public_key = None
|
self.master_public_key = None
|
||||||
self.session_id = ''.join([ chr(random.randrange(0, 255, 1)) for _ in xrange(0, 16) ])
|
self.session_id = ''.join([ chr(random.randrange(0, 255, 1)) for _ in xrange(0, 16) ])
|
||||||
@ -129,15 +132,58 @@ class BitkeyClient(object):
|
|||||||
'''
|
'''
|
||||||
inputs: list of TxInput
|
inputs: list of TxInput
|
||||||
outputs: list of TxOutput
|
outputs: list of TxOutput
|
||||||
|
|
||||||
|
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=
|
||||||
|
)
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
# Prepare and send initial message
|
||||||
tx = proto.SignTx()
|
tx = proto.SignTx()
|
||||||
tx.algo = self.algo # Choose BIP32 or ELECTRUM way for deterministic keys
|
tx.algo = self.algo # Choose BIP32 or ELECTRUM way for deterministic keys
|
||||||
tx.random = os.urandom(256) # Provide additional entropy to the device
|
tx.random = self._get_local_entropy() # Provide additional entropy to the device
|
||||||
tx.outputs.extend(outputs)
|
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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
return self.call(tx)
|
|
||||||
#print "PBDATA", tx.SerializeToString().encode('hex')
|
#print "PBDATA", tx.SerializeToString().encode('hex')
|
||||||
|
|
||||||
#################
|
#################
|
||||||
@ -169,6 +215,11 @@ class BitkeyClient(object):
|
|||||||
return s_inputs
|
return s_inputs
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
def reset_device(self):
|
||||||
|
resp = self.call(proto.ResetDevice(random=self._get_local_entropy()))
|
||||||
|
self.init_device()
|
||||||
|
return isinstance(resp, proto.Success)
|
||||||
|
|
||||||
def load_device(self, seed, otp, pin, spv):
|
def load_device(self, seed, otp, pin, spv):
|
||||||
resp = self.call(proto.LoadDevice(seed=seed, otp=otp, pin=pin, spv=spv))
|
resp = self.call(proto.LoadDevice(seed=seed, otp=otp, pin=pin, spv=spv))
|
||||||
self.init_device()
|
self.init_device()
|
||||||
|
@ -1,11 +1,43 @@
|
|||||||
import unittest
|
import unittest
|
||||||
import common
|
import common
|
||||||
|
|
||||||
|
import bitkeylib.bitkey_pb2 as proto
|
||||||
|
|
||||||
class TestSignTx(common.BitkeyTest):
|
class TestSignTx(common.BitkeyTest):
|
||||||
'''
|
|
||||||
def test_signtx(self):
|
def test_signtx(self):
|
||||||
print self.bitkey.sign_tx([], [])
|
inp1 = proto.TxInput(index=0,
|
||||||
'''
|
address_n=[1,],
|
||||||
|
amount=100000000,
|
||||||
|
prev_hash='prevhash1234354346543456543654',
|
||||||
|
prev_index=11,
|
||||||
|
#script_sig=
|
||||||
|
)
|
||||||
|
|
||||||
|
inp2 = proto.TxInput(index=1,
|
||||||
|
address_n=[2,],
|
||||||
|
amount=100000000,
|
||||||
|
prev_hash='prevhash2222254346543456543654',
|
||||||
|
prev_index=11,
|
||||||
|
#script_sig=
|
||||||
|
)
|
||||||
|
|
||||||
|
out1 = proto.TxOutput(index=0,
|
||||||
|
address='1Bitkey',
|
||||||
|
#address_n=[],
|
||||||
|
amount=100000000,
|
||||||
|
script_type=proto.PAYTOADDRESS,
|
||||||
|
#script_args=
|
||||||
|
)
|
||||||
|
|
||||||
|
out2 = proto.TxOutput(index=1,
|
||||||
|
address='1Bitkey2',
|
||||||
|
#address_n=[],
|
||||||
|
amount=100000000,
|
||||||
|
script_type=proto.PAYTOADDRESS,
|
||||||
|
#script_args=
|
||||||
|
)
|
||||||
|
|
||||||
|
print self.bitkey.sign_tx([inp1, inp2], [out1, out2])
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
Loading…
Reference in New Issue
Block a user