From 2730631e6d458b7a2567f7a64f34aea6eff0e407 Mon Sep 17 00:00:00 2001 From: slush Date: Mon, 14 Jan 2013 19:22:02 +0100 Subject: [PATCH] Implemented responder for signing Basic signtx unit test --- bitkeylib/client.py | 61 ++++++++++++++++++++++++++++++++++++++++---- tests/test_signtx.py | 38 ++++++++++++++++++++++++--- 2 files changed, 91 insertions(+), 8 deletions(-) diff --git a/bitkeylib/client.py b/bitkeylib/client.py index c4cd5e2d0..83f09dcd7 100644 --- a/bitkeylib/client.py +++ b/bitkeylib/client.py @@ -34,6 +34,9 @@ class BitkeyClient(object): self.setup_debuglink() self.init_device() + + def _get_local_entropy(self): + return os.urandom(32) def init_device(self): self.master_public_key = None @@ -129,15 +132,58 @@ class BitkeyClient(object): ''' inputs: list of TxInput 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.algo = self.algo # Choose BIP32 or ELECTRUM way for deterministic keys - tx.random = os.urandom(256) # Provide additional entropy to the device - tx.outputs.extend(outputs) + 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 - return self.call(tx) + 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 + #print "PBDATA", tx.SerializeToString().encode('hex') ################# @@ -169,6 +215,11 @@ class BitkeyClient(object): 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): resp = self.call(proto.LoadDevice(seed=seed, otp=otp, pin=pin, spv=spv)) self.init_device() diff --git a/tests/test_signtx.py b/tests/test_signtx.py index bfa1ef84d..ca9da1b74 100644 --- a/tests/test_signtx.py +++ b/tests/test_signtx.py @@ -1,11 +1,43 @@ import unittest import common +import bitkeylib.bitkey_pb2 as proto + class TestSignTx(common.BitkeyTest): - ''' 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__': unittest.main() \ No newline at end of file