serialize: do not use x_pubkeys if we have enough signatures.
[electrum-nvc.git] / lib / transaction.py
1 #!/usr/bin/env python
2 #
3 # Electrum - lightweight Bitcoin client
4 # Copyright (C) 2011 thomasv@gitorious
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19
20 # Note: The deserialization code originally comes from ABE.
21
22
23 import bitcoin
24 from bitcoin import *
25 from util import print_error
26 import time
27 import struct
28
29 #
30 # Workalike python implementation of Bitcoin's CDataStream class.
31 #
32 import struct
33 import StringIO
34 import mmap
35
36 class SerializationError(Exception):
37     """ Thrown when there's a problem deserializing or serializing """
38
39 class BCDataStream(object):
40     def __init__(self):
41         self.input = None
42         self.read_cursor = 0
43
44     def clear(self):
45         self.input = None
46         self.read_cursor = 0
47
48     def write(self, bytes):  # Initialize with string of bytes
49         if self.input is None:
50             self.input = bytes
51         else:
52             self.input += bytes
53
54     def map_file(self, file, start):  # Initialize with bytes from file
55         self.input = mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ)
56         self.read_cursor = start
57
58     def seek_file(self, position):
59         self.read_cursor = position
60         
61     def close_file(self):
62         self.input.close()
63
64     def read_string(self):
65         # Strings are encoded depending on length:
66         # 0 to 252 :  1-byte-length followed by bytes (if any)
67         # 253 to 65,535 : byte'253' 2-byte-length followed by bytes
68         # 65,536 to 4,294,967,295 : byte '254' 4-byte-length followed by bytes
69         # ... and the Bitcoin client is coded to understand:
70         # greater than 4,294,967,295 : byte '255' 8-byte-length followed by bytes of string
71         # ... but I don't think it actually handles any strings that big.
72         if self.input is None:
73             raise SerializationError("call write(bytes) before trying to deserialize")
74
75         try:
76             length = self.read_compact_size()
77         except IndexError:
78             raise SerializationError("attempt to read past end of buffer")
79
80         return self.read_bytes(length)
81
82     def write_string(self, string):
83         # Length-encoded as with read-string
84         self.write_compact_size(len(string))
85         self.write(string)
86
87     def read_bytes(self, length):
88         try:
89             result = self.input[self.read_cursor:self.read_cursor+length]
90             self.read_cursor += length
91             return result
92         except IndexError:
93             raise SerializationError("attempt to read past end of buffer")
94
95         return ''
96
97     def read_boolean(self): return self.read_bytes(1)[0] != chr(0)
98     def read_int16(self): return self._read_num('<h')
99     def read_uint16(self): return self._read_num('<H')
100     def read_int32(self): return self._read_num('<i')
101     def read_uint32(self): return self._read_num('<I')
102     def read_int64(self): return self._read_num('<q')
103     def read_uint64(self): return self._read_num('<Q')
104
105     def write_boolean(self, val): return self.write(chr(1) if val else chr(0))
106     def write_int16(self, val): return self._write_num('<h', val)
107     def write_uint16(self, val): return self._write_num('<H', val)
108     def write_int32(self, val): return self._write_num('<i', val)
109     def write_uint32(self, val): return self._write_num('<I', val)
110     def write_int64(self, val): return self._write_num('<q', val)
111     def write_uint64(self, val): return self._write_num('<Q', val)
112
113     def read_compact_size(self):
114         size = ord(self.input[self.read_cursor])
115         self.read_cursor += 1
116         if size == 253:
117             size = self._read_num('<H')
118         elif size == 254:
119             size = self._read_num('<I')
120         elif size == 255:
121             size = self._read_num('<Q')
122         return size
123
124     def write_compact_size(self, size):
125         if size < 0:
126             raise SerializationError("attempt to write size < 0")
127         elif size < 253:
128             self.write(chr(size))
129         elif size < 2**16:
130             self.write('\xfd')
131             self._write_num('<H', size)
132         elif size < 2**32:
133             self.write('\xfe')
134             self._write_num('<I', size)
135         elif size < 2**64:
136             self.write('\xff')
137             self._write_num('<Q', size)
138
139     def _read_num(self, format):
140         (i,) = struct.unpack_from(format, self.input, self.read_cursor)
141         self.read_cursor += struct.calcsize(format)
142         return i
143
144     def _write_num(self, format, num):
145         s = struct.pack(format, num)
146         self.write(s)
147
148 #
149 # enum-like type
150 # From the Python Cookbook, downloaded from http://code.activestate.com/recipes/67107/
151 #
152 import types, string, exceptions
153
154 class EnumException(exceptions.Exception):
155     pass
156
157 class Enumeration:
158     def __init__(self, name, enumList):
159         self.__doc__ = name
160         lookup = { }
161         reverseLookup = { }
162         i = 0
163         uniqueNames = [ ]
164         uniqueValues = [ ]
165         for x in enumList:
166             if type(x) == types.TupleType:
167                 x, i = x
168             if type(x) != types.StringType:
169                 raise EnumException, "enum name is not a string: " + x
170             if type(i) != types.IntType:
171                 raise EnumException, "enum value is not an integer: " + i
172             if x in uniqueNames:
173                 raise EnumException, "enum name is not unique: " + x
174             if i in uniqueValues:
175                 raise EnumException, "enum value is not unique for " + x
176             uniqueNames.append(x)
177             uniqueValues.append(i)
178             lookup[x] = i
179             reverseLookup[i] = x
180             i = i + 1
181         self.lookup = lookup
182         self.reverseLookup = reverseLookup
183     def __getattr__(self, attr):
184         if not self.lookup.has_key(attr):
185             raise AttributeError
186         return self.lookup[attr]
187     def whatis(self, value):
188         return self.reverseLookup[value]
189
190
191 # This function comes from bitcointools, bct-LICENSE.txt.
192 def long_hex(bytes):
193     return bytes.encode('hex_codec')
194
195 # This function comes from bitcointools, bct-LICENSE.txt.
196 def short_hex(bytes):
197     t = bytes.encode('hex_codec')
198     if len(t) < 11:
199         return t
200     return t[0:4]+"..."+t[-4:]
201
202
203
204
205 def parse_redeemScript(bytes):
206     dec = [ x for x in script_GetOp(bytes.decode('hex')) ]
207
208     # 2 of 2
209     match = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_2, opcodes.OP_CHECKMULTISIG ]
210     if match_decoded(dec, match):
211         pubkeys = [ dec[1][1].encode('hex'), dec[2][1].encode('hex') ]
212         return 2, pubkeys
213
214     # 2 of 3
215     match = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_3, opcodes.OP_CHECKMULTISIG ]
216     if match_decoded(dec, match):
217         pubkeys = [ dec[1][1].encode('hex'), dec[2][1].encode('hex'), dec[3][1].encode('hex') ]
218         return 2, pubkeys
219
220
221
222 opcodes = Enumeration("Opcodes", [
223     ("OP_0", 0), ("OP_PUSHDATA1",76), "OP_PUSHDATA2", "OP_PUSHDATA4", "OP_1NEGATE", "OP_RESERVED",
224     "OP_1", "OP_2", "OP_3", "OP_4", "OP_5", "OP_6", "OP_7",
225     "OP_8", "OP_9", "OP_10", "OP_11", "OP_12", "OP_13", "OP_14", "OP_15", "OP_16",
226     "OP_NOP", "OP_VER", "OP_IF", "OP_NOTIF", "OP_VERIF", "OP_VERNOTIF", "OP_ELSE", "OP_ENDIF", "OP_VERIFY",
227     "OP_RETURN", "OP_TOALTSTACK", "OP_FROMALTSTACK", "OP_2DROP", "OP_2DUP", "OP_3DUP", "OP_2OVER", "OP_2ROT", "OP_2SWAP",
228     "OP_IFDUP", "OP_DEPTH", "OP_DROP", "OP_DUP", "OP_NIP", "OP_OVER", "OP_PICK", "OP_ROLL", "OP_ROT",
229     "OP_SWAP", "OP_TUCK", "OP_CAT", "OP_SUBSTR", "OP_LEFT", "OP_RIGHT", "OP_SIZE", "OP_INVERT", "OP_AND",
230     "OP_OR", "OP_XOR", "OP_EQUAL", "OP_EQUALVERIFY", "OP_RESERVED1", "OP_RESERVED2", "OP_1ADD", "OP_1SUB", "OP_2MUL",
231     "OP_2DIV", "OP_NEGATE", "OP_ABS", "OP_NOT", "OP_0NOTEQUAL", "OP_ADD", "OP_SUB", "OP_MUL", "OP_DIV",
232     "OP_MOD", "OP_LSHIFT", "OP_RSHIFT", "OP_BOOLAND", "OP_BOOLOR",
233     "OP_NUMEQUAL", "OP_NUMEQUALVERIFY", "OP_NUMNOTEQUAL", "OP_LESSTHAN",
234     "OP_GREATERTHAN", "OP_LESSTHANOREQUAL", "OP_GREATERTHANOREQUAL", "OP_MIN", "OP_MAX",
235     "OP_WITHIN", "OP_RIPEMD160", "OP_SHA1", "OP_SHA256", "OP_HASH160",
236     "OP_HASH256", "OP_CODESEPARATOR", "OP_CHECKSIG", "OP_CHECKSIGVERIFY", "OP_CHECKMULTISIG",
237     "OP_CHECKMULTISIGVERIFY",
238     ("OP_SINGLEBYTE_END", 0xF0),
239     ("OP_DOUBLEBYTE_BEGIN", 0xF000),
240     "OP_PUBKEY", "OP_PUBKEYHASH",
241     ("OP_INVALIDOPCODE", 0xFFFF),
242 ])
243
244
245 def script_GetOp(bytes):
246     i = 0
247     while i < len(bytes):
248         vch = None
249         opcode = ord(bytes[i])
250         i += 1
251         if opcode >= opcodes.OP_SINGLEBYTE_END:
252             opcode <<= 8
253             opcode |= ord(bytes[i])
254             i += 1
255
256         if opcode <= opcodes.OP_PUSHDATA4:
257             nSize = opcode
258             if opcode == opcodes.OP_PUSHDATA1:
259                 nSize = ord(bytes[i])
260                 i += 1
261             elif opcode == opcodes.OP_PUSHDATA2:
262                 (nSize,) = struct.unpack_from('<H', bytes, i)
263                 i += 2
264             elif opcode == opcodes.OP_PUSHDATA4:
265                 (nSize,) = struct.unpack_from('<I', bytes, i)
266                 i += 4
267             vch = bytes[i:i+nSize]
268             i += nSize
269
270         yield (opcode, vch, i)
271
272
273 def script_GetOpName(opcode):
274     return (opcodes.whatis(opcode)).replace("OP_", "")
275
276
277 def decode_script(bytes):
278     result = ''
279     for (opcode, vch, i) in script_GetOp(bytes):
280         if len(result) > 0: result += " "
281         if opcode <= opcodes.OP_PUSHDATA4:
282             result += "%d:"%(opcode,)
283             result += short_hex(vch)
284         else:
285             result += script_GetOpName(opcode)
286     return result
287
288
289 def match_decoded(decoded, to_match):
290     if len(decoded) != len(to_match):
291         return False;
292     for i in range(len(decoded)):
293         if to_match[i] == opcodes.OP_PUSHDATA4 and decoded[i][0] <= opcodes.OP_PUSHDATA4 and decoded[i][0]>0:
294             continue  # Opcodes below OP_PUSHDATA4 all just push data onto stack, and are equivalent.
295         if to_match[i] != decoded[i][0]:
296             return False
297     return True
298
299
300 def parse_sig(x_sig):
301     s = []
302     for sig in x_sig:
303         if sig[-2:] == '01':
304             s.append(sig[:-2])
305         else:
306             assert sig == 'ff'
307     return s
308
309 def is_extended_pubkey(x_pubkey):
310     return x_pubkey[0:2] in ['fe', 'ff']
311
312 def parse_xpub(x_pubkey):
313     if x_pubkey[0:2] == 'ff':
314         from account import BIP32_Account
315         xpub, s = BIP32_Account.parse_xpubkey(x_pubkey)
316         pubkey = BIP32_Account.get_pubkey_from_x(xpub, s[0], s[1])
317     elif x_pubkey[0:2] == 'fe':
318         from account import OldAccount
319         mpk, s = OldAccount.parse_xpubkey(x_pubkey)
320         pubkey = OldAccount.get_pubkey_from_mpk(mpk.decode('hex'), s[0], s[1])
321     else:
322         pubkey = x_pubkey
323     return pubkey
324
325
326 def parse_scriptSig(d, bytes):
327     try:
328         decoded = [ x for x in script_GetOp(bytes) ]
329     except Exception:
330         # coinbase transactions raise an exception
331         print_error("cannot find address in input script", bytes.encode('hex'))
332         return
333
334     # payto_pubkey
335     match = [ opcodes.OP_PUSHDATA4 ]
336     if match_decoded(decoded, match):
337         return
338
339     # non-generated TxIn transactions push a signature
340     # (seventy-something bytes) and then their public key
341     # (65 bytes) onto the stack:
342     match = [ opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4 ]
343     if match_decoded(decoded, match):
344         sig = decoded[0][1].encode('hex')
345         x_pubkey = decoded[1][1].encode('hex')
346         try:
347             signatures = parse_sig([sig])
348             pubkey = parse_xpub(x_pubkey)
349         except:
350             import traceback
351             traceback.print_exc(file=sys.stdout)
352             print_error("cannot find address in input script", bytes.encode('hex'))
353             return
354         d['signatures'] = signatures
355         d['x_pubkeys'] = [x_pubkey]
356         d['num_sig'] = 1
357         d['pubkeys'] = [pubkey]
358         d['address'] = public_key_to_bc_address(pubkey.decode('hex'))
359         return
360
361     # p2sh transaction, 2 of n
362     match = [ opcodes.OP_0 ]
363     while len(match) < len(decoded):
364         match.append(opcodes.OP_PUSHDATA4)
365
366     if not match_decoded(decoded, match):
367         print_error("cannot find address in input script", bytes.encode('hex'))
368         return
369
370     x_sig = map(lambda x:x[1].encode('hex'), decoded[1:-1])
371     d['signatures'] = parse_sig(x_sig)
372     d['num_sig'] = 2
373
374     dec2 = [ x for x in script_GetOp(decoded[-1][1]) ]
375     match_2of2 = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_2, opcodes.OP_CHECKMULTISIG ]
376     match_2of3 = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_3, opcodes.OP_CHECKMULTISIG ]
377     if match_decoded(dec2, match_2of2):
378         x_pubkeys = [ dec2[1][1].encode('hex'), dec2[2][1].encode('hex') ]
379     elif match_decoded(dec2, match_2of3):
380         x_pubkeys = [ dec2[1][1].encode('hex'), dec2[2][1].encode('hex'), dec2[3][1].encode('hex') ]
381     else:
382         print_error("cannot find address in input script", bytes.encode('hex'))
383         return
384
385     d['x_pubkeys'] = x_pubkeys
386     pubkeys = map(parse_xpub, x_pubkeys)
387     d['pubkeys'] = pubkeys
388     redeemScript = Transaction.multisig_script(pubkeys,2)
389     d['redeemScript'] = redeemScript
390     d['address'] = hash_160_to_bc_address(hash_160(redeemScript.decode('hex')), 5)
391
392
393
394
395 def get_address_from_output_script(bytes):
396     decoded = [ x for x in script_GetOp(bytes) ]
397
398     # The Genesis Block, self-payments, and pay-by-IP-address payments look like:
399     # 65 BYTES:... CHECKSIG
400     match = [ opcodes.OP_PUSHDATA4, opcodes.OP_CHECKSIG ]
401     if match_decoded(decoded, match):
402         return True, public_key_to_bc_address(decoded[0][1])
403
404     # Pay-by-Bitcoin-address TxOuts look like:
405     # DUP HASH160 20 BYTES:... EQUALVERIFY CHECKSIG
406     match = [ opcodes.OP_DUP, opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUALVERIFY, opcodes.OP_CHECKSIG ]
407     if match_decoded(decoded, match):
408         return False, hash_160_to_bc_address(decoded[2][1])
409
410     # p2sh
411     match = [ opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUAL ]
412     if match_decoded(decoded, match):
413         return False, hash_160_to_bc_address(decoded[1][1],5)
414
415     return False, "(None)"
416
417
418 class Transaction:
419     
420     def __init__(self, raw):
421         self.raw = raw
422         self.deserialize()
423         self.inputs = self.d['inputs']
424         self.outputs = self.d['outputs']
425         self.outputs = map(lambda x: (x['address'],x['value']), self.outputs)
426         self.locktime = self.d['lockTime']
427
428     def __str__(self):
429         return self.raw
430
431     @classmethod
432     def from_io(klass, inputs, outputs):
433         raw = klass.serialize(inputs, outputs, for_sig = None) # for_sig=-1 means do not sign
434         self = klass(raw)
435         self.inputs = inputs
436         self.outputs = outputs
437         return self
438
439     @classmethod 
440     def sweep(klass, privkeys, network, to_address, fee):
441         inputs = []
442         for privkey in privkeys:
443             pubkey = public_key_from_private_key(privkey)
444             address = address_from_private_key(privkey)
445             u = network.synchronous_get([ ('blockchain.address.listunspent',[address])])[0]
446             pay_script = klass.pay_script(address)
447             for item in u:
448                 item['scriptPubKey'] = pay_script
449                 item['redeemPubkey'] = pubkey
450                 item['address'] = address
451                 item['prevout_hash'] = item['tx_hash']
452                 item['prevout_n'] = item['tx_pos']
453             inputs += u
454
455         if not inputs:
456             return
457
458         total = sum( map(lambda x:int(x.get('value')), inputs) ) - fee
459         outputs = [(to_address, total)]
460         self = klass.from_io(inputs, outputs)
461         self.sign({ pubkey:privkey })
462         return self
463
464     @classmethod
465     def multisig_script(klass, public_keys, num=None):
466         n = len(public_keys)
467         if num is None: num = n
468         # supports only "2 of 2", and "2 of 3" transactions
469         assert num <= n and n in [2,3]
470     
471         if num==2:
472             s = '52'
473         elif num == 3:
474             s = '53'
475         else:
476             raise
477     
478         for k in public_keys:
479             s += op_push(len(k)/2)
480             s += k
481         if n==2:
482             s += '52'
483         elif n==3:
484             s += '53'
485         else:
486             raise
487         s += 'ae'
488
489         return s
490
491
492     @classmethod
493     def pay_script(self, addr):
494         addrtype, hash_160 = bc_address_to_hash_160(addr)
495         if addrtype == 0:
496             script = '76a9'                                      # op_dup, op_hash_160
497             script += '14'                                       # push 0x14 bytes
498             script += hash_160.encode('hex')
499             script += '88ac'                                     # op_equalverify, op_checksig
500         elif addrtype == 5:
501             script = 'a9'                                        # op_hash_160
502             script += '14'                                       # push 0x14 bytes
503             script += hash_160.encode('hex')
504             script += '87'                                       # op_equal
505         else:
506             raise
507         return script
508
509
510     @classmethod
511     def serialize( klass, inputs, outputs, for_sig = None ):
512
513         NO_SIGNATURE = 'ff'
514
515         push_script = lambda x: op_push(len(x)/2) + x
516         s  = int_to_hex(1,4)                                         # version
517         s += var_int( len(inputs) )                                  # number of inputs
518         for i in range(len(inputs)):
519             txin = inputs[i]
520
521             s += txin['prevout_hash'].decode('hex')[::-1].encode('hex')   # prev hash
522             s += int_to_hex(txin['prevout_n'],4)                          # prev index
523
524             p2sh = txin.get('redeemScript') is not None
525             n_sig = 2 if p2sh else 1
526
527             pubkeys = txin['pubkeys'] # pubkeys should always be known
528             address = txin['address']
529
530             if for_sig is None:
531
532                 # list of signatures
533                 signatures = txin.get('signatures',[])
534                 sig_list = []
535                 for signature in signatures:
536                     sig_list.append(signature + '01')
537                 if len(sig_list) > n_sig:
538                     sig_list = sig_list[:n_sig]
539                 while len(sig_list) < n_sig:
540                     sig_list.append(NO_SIGNATURE)
541                 sig_list = ''.join( map( lambda x: push_script(x), sig_list))
542
543                 if len(signatures) < n_sig:
544                     # extended pubkeys (with bip32 derivation)
545                     x_pubkeys = txin['x_pubkeys']
546                 else:
547                     # if we have enough signatures, we use the actual pubkeys
548                     x_pubkeys = txin['pubkeys']
549
550                 if not p2sh:
551                     script = sig_list
552                     script += push_script(x_pubkeys[0])
553                 else:
554                     script = '00'                                    # op_0
555                     script += sig_list
556                     redeem_script = klass.multisig_script(x_pubkeys,2)
557                     script += push_script(redeem_script)
558
559             elif for_sig==i:
560                 script = txin['redeemScript'] if p2sh else klass.pay_script(address)
561             else:
562                 script = ''
563             s += var_int( len(script)/2 )                            # script length
564             s += script
565             s += "ffffffff"                                          # sequence
566
567         s += var_int( len(outputs) )                                 # number of outputs
568         for output in outputs:
569             addr, amount = output
570             s += int_to_hex( amount, 8)                              # amount
571             script = klass.pay_script(addr)
572             s += var_int( len(script)/2 )                           #  script length
573             s += script                                             #  script
574         s += int_to_hex(0,4)                                        #  lock time
575         if for_sig is not None and for_sig != -1:
576             s += int_to_hex(1, 4)                                   #  hash type
577         return s
578
579
580     def tx_for_sig(self,i):
581         return self.serialize(self.inputs, self.outputs, for_sig = i)
582
583
584     def hash(self):
585         return Hash(self.raw.decode('hex') )[::-1].encode('hex')
586
587     def add_signature(self, i, pubkey, sig):
588         txin = self.inputs[i]
589         signatures = txin.get("signatures",[])
590         if sig not in signatures:
591             signatures.append(sig)
592         txin["signatures"] = signatures
593         self.inputs[i] = txin
594         print_error("adding signature for", pubkey)
595         # replace x_pubkey
596         i = txin['pubkeys'].index(pubkey)
597         txin['x_pubkeys'][i] = pubkey
598
599         self.raw = self.serialize( self.inputs, self.outputs )
600
601
602     def is_complete(self):
603         for i, txin in enumerate(self.inputs):
604             pubkeys = txin['pubkeys']
605             signatures = txin.get("signatures",{})
606             if len(signatures) == txin['num_sig']:
607                 continue
608             else:
609                 return False
610         return True
611
612
613
614     def sign(self, keypairs):
615         print_error("tx.sign(), keypairs:", keypairs)
616
617         for i, txin in enumerate(self.inputs):
618
619             redeem_pubkeys = txin['pubkeys']
620             num = len(redeem_pubkeys)
621
622             # get list of already existing signatures
623             signatures = txin.get("signatures",{})
624             # continue if this txin is complete
625             if len(signatures) == num:
626                 continue
627
628             for_sig = Hash(self.tx_for_sig(i).decode('hex'))
629             for pubkey in redeem_pubkeys:
630                 if pubkey in keypairs.keys():
631                     # add signature
632                     sec = keypairs[pubkey]
633                     pkey = regenerate_key(sec)
634                     secexp = pkey.secret
635                     private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
636                     public_key = private_key.get_verifying_key()
637                     sig = private_key.sign_digest_deterministic( for_sig, hashfunc=hashlib.sha256, sigencode = ecdsa.util.sigencode_der )
638                     assert public_key.verify_digest( sig, for_sig, sigdecode = ecdsa.util.sigdecode_der)
639                     self.add_signature(i, pubkey, sig.encode('hex'))
640
641
642         print_error("is_complete", self.is_complete())
643         self.raw = self.serialize( self.inputs, self.outputs )
644
645
646
647     def deserialize(self):
648         vds = BCDataStream()
649         vds.write(self.raw.decode('hex'))
650         d = {}
651         start = vds.read_cursor
652         d['version'] = vds.read_int32()
653         n_vin = vds.read_compact_size()
654         d['inputs'] = []
655         for i in xrange(n_vin):
656             d['inputs'].append(self.parse_input(vds))
657         n_vout = vds.read_compact_size()
658         d['outputs'] = []
659         for i in xrange(n_vout):
660             d['outputs'].append(self.parse_output(vds, i))
661         d['lockTime'] = vds.read_uint32()
662         self.d = d
663         return self.d
664     
665
666     def parse_input(self, vds):
667         d = {}
668         prevout_hash = hash_encode(vds.read_bytes(32))
669         prevout_n = vds.read_uint32()
670         scriptSig = vds.read_bytes(vds.read_compact_size())
671         sequence = vds.read_uint32()
672
673         if prevout_hash == '00'*32:
674             d['is_coinbase'] = True
675         else:
676             d['is_coinbase'] = False
677             d['prevout_hash'] = prevout_hash
678             d['prevout_n'] = prevout_n
679             d['sequence'] = sequence
680
681             d['pubkeys'] = []
682             d['signatures'] = {}
683             d['address'] = None
684             if scriptSig:
685                 parse_scriptSig(d, scriptSig)
686         return d
687
688
689     def parse_output(self, vds, i):
690         d = {}
691         d['value'] = vds.read_int64()
692         scriptPubKey = vds.read_bytes(vds.read_compact_size())
693         is_pubkey, address = get_address_from_output_script(scriptPubKey)
694         d['is_pubkey'] = is_pubkey
695         d['address'] = address
696         d['scriptPubKey'] = scriptPubKey.encode('hex')
697         d['prevout_n'] = i
698         return d
699
700
701     def add_extra_addresses(self, txlist):
702         for i in self.inputs:
703             if i.get("address") == "(pubkey)":
704                 prev_tx = txlist.get(i.get('prevout_hash'))
705                 if prev_tx:
706                     address, value = prev_tx.outputs[i.get('prevout_n')]
707                     print_error("found pay-to-pubkey address:", address)
708                     i["address"] = address
709
710
711     def has_address(self, addr):
712         found = False
713         for txin in self.inputs:
714             if addr == txin.get('address'): 
715                 found = True
716                 break
717         for txout in self.outputs:
718             if addr == txout[0]:
719                 found = True
720                 break
721         return found
722
723
724     def get_value(self, addresses, prevout_values):
725         # return the balance for that tx
726         is_relevant = False
727         is_send = False
728         is_pruned = False
729         is_partial = False
730         v_in = v_out = v_out_mine = 0
731
732         for item in self.inputs:
733             addr = item.get('address')
734             if addr in addresses:
735                 is_send = True
736                 is_relevant = True
737                 key = item['prevout_hash']  + ':%d'%item['prevout_n']
738                 value = prevout_values.get( key )
739                 if value is None:
740                     is_pruned = True
741                 else:
742                     v_in += value
743             else:
744                 is_partial = True
745
746         if not is_send: is_partial = False
747                     
748         for item in self.outputs:
749             addr, value = item
750             v_out += value
751             if addr in addresses:
752                 v_out_mine += value
753                 is_relevant = True
754
755         if is_pruned:
756             # some inputs are mine:
757             fee = None
758             if is_send:
759                 v = v_out_mine - v_out
760             else:
761                 # no input is mine
762                 v = v_out_mine
763
764         else:
765             v = v_out_mine - v_in
766
767             if is_partial:
768                 # some inputs are mine, but not all
769                 fee = None
770                 is_send = v < 0
771             else:
772                 # all inputs are mine
773                 fee = v_out - v_in
774
775         return is_relevant, is_send, v, fee
776
777
778     def as_dict(self):
779         import json
780         out = {
781             "hex":self.raw,
782             "complete":self.is_complete()
783             }
784         return out
785
786
787     def requires_fee(self, verifier):
788         # see https://en.bitcoin.it/wiki/Transaction_fees
789         threshold = 57600000
790         size = len(self.raw)/2
791         if size >= 10000: 
792             return True
793
794         for o in self.outputs:
795             value = o[1]
796             if value < 1000000:
797                 return True
798         sum = 0
799         for i in self.inputs:
800             age = verifier.get_confirmations(i["prevout_hash"])[0]
801             sum += i["value"] * age
802         priority = sum / size
803         print_error(priority, threshold)
804         return priority < threshold 
805
806
807