3 # Electrum - lightweight Bitcoin client
4 # Copyright (C) 2011 thomasv@gitorious
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.
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.
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/>.
20 # Note: The deserialization code originally comes from ABE.
25 from util import print_error
30 # Workalike python implementation of Bitcoin's CDataStream class.
38 class SerializationError(Exception):
39 """ Thrown when there's a problem deserializing or serializing """
41 class BCDataStream(object):
50 def write(self, bytes): # Initialize with string of bytes
51 if self.input is None:
56 def map_file(self, file, start): # Initialize with bytes from file
57 self.input = mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ)
58 self.read_cursor = start
60 def seek_file(self, position):
61 self.read_cursor = position
66 def read_string(self):
67 # Strings are encoded depending on length:
68 # 0 to 252 : 1-byte-length followed by bytes (if any)
69 # 253 to 65,535 : byte'253' 2-byte-length followed by bytes
70 # 65,536 to 4,294,967,295 : byte '254' 4-byte-length followed by bytes
71 # ... and the Bitcoin client is coded to understand:
72 # greater than 4,294,967,295 : byte '255' 8-byte-length followed by bytes of string
73 # ... but I don't think it actually handles any strings that big.
74 if self.input is None:
75 raise SerializationError("call write(bytes) before trying to deserialize")
78 length = self.read_compact_size()
80 raise SerializationError("attempt to read past end of buffer")
82 return self.read_bytes(length)
84 def write_string(self, string):
85 # Length-encoded as with read-string
86 self.write_compact_size(len(string))
89 def read_bytes(self, length):
91 result = self.input[self.read_cursor:self.read_cursor+length]
92 self.read_cursor += length
95 raise SerializationError("attempt to read past end of buffer")
99 def read_boolean(self): return self.read_bytes(1)[0] != chr(0)
100 def read_int16(self): return self._read_num('<h')
101 def read_uint16(self): return self._read_num('<H')
102 def read_int32(self): return self._read_num('<i')
103 def read_uint32(self): return self._read_num('<I')
104 def read_int64(self): return self._read_num('<q')
105 def read_uint64(self): return self._read_num('<Q')
107 def write_boolean(self, val): return self.write(chr(1) if val else chr(0))
108 def write_int16(self, val): return self._write_num('<h', val)
109 def write_uint16(self, val): return self._write_num('<H', val)
110 def write_int32(self, val): return self._write_num('<i', val)
111 def write_uint32(self, val): return self._write_num('<I', val)
112 def write_int64(self, val): return self._write_num('<q', val)
113 def write_uint64(self, val): return self._write_num('<Q', val)
115 def read_compact_size(self):
116 size = ord(self.input[self.read_cursor])
117 self.read_cursor += 1
119 size = self._read_num('<H')
121 size = self._read_num('<I')
123 size = self._read_num('<Q')
126 def write_compact_size(self, size):
128 raise SerializationError("attempt to write size < 0")
130 self.write(chr(size))
133 self._write_num('<H', size)
136 self._write_num('<I', size)
139 self._write_num('<Q', size)
141 def _read_num(self, format):
142 (i,) = struct.unpack_from(format, self.input, self.read_cursor)
143 self.read_cursor += struct.calcsize(format)
146 def _write_num(self, format, num):
147 s = struct.pack(format, num)
152 # From the Python Cookbook, downloaded from http://code.activestate.com/recipes/67107/
154 import types, string, exceptions
156 class EnumException(exceptions.Exception):
160 def __init__(self, name, enumList):
168 if type(x) == types.TupleType:
170 if type(x) != types.StringType:
171 raise EnumException, "enum name is not a string: " + x
172 if type(i) != types.IntType:
173 raise EnumException, "enum value is not an integer: " + i
175 raise EnumException, "enum name is not unique: " + x
176 if i in uniqueValues:
177 raise EnumException, "enum value is not unique for " + x
178 uniqueNames.append(x)
179 uniqueValues.append(i)
184 self.reverseLookup = reverseLookup
185 def __getattr__(self, attr):
186 if not self.lookup.has_key(attr):
188 return self.lookup[attr]
189 def whatis(self, value):
190 return self.reverseLookup[value]
193 # This function comes from bitcointools, bct-LICENSE.txt.
195 return bytes.encode('hex_codec')
197 # This function comes from bitcointools, bct-LICENSE.txt.
198 def short_hex(bytes):
199 t = bytes.encode('hex_codec')
202 return t[0:4]+"..."+t[-4:]
207 def parse_redeemScript(bytes):
208 dec = [ x for x in script_GetOp(bytes.decode('hex')) ]
211 match = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_2, opcodes.OP_CHECKMULTISIG ]
212 if match_decoded(dec, match):
213 pubkeys = [ dec[1][1].encode('hex'), dec[2][1].encode('hex') ]
217 match = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_3, opcodes.OP_CHECKMULTISIG ]
218 if match_decoded(dec, match):
219 pubkeys = [ dec[1][1].encode('hex'), dec[2][1].encode('hex'), dec[3][1].encode('hex') ]
224 opcodes = Enumeration("Opcodes", [
225 ("OP_0", 0), ("OP_PUSHDATA1",76), "OP_PUSHDATA2", "OP_PUSHDATA4", "OP_1NEGATE", "OP_RESERVED",
226 "OP_1", "OP_2", "OP_3", "OP_4", "OP_5", "OP_6", "OP_7",
227 "OP_8", "OP_9", "OP_10", "OP_11", "OP_12", "OP_13", "OP_14", "OP_15", "OP_16",
228 "OP_NOP", "OP_VER", "OP_IF", "OP_NOTIF", "OP_VERIF", "OP_VERNOTIF", "OP_ELSE", "OP_ENDIF", "OP_VERIFY",
229 "OP_RETURN", "OP_TOALTSTACK", "OP_FROMALTSTACK", "OP_2DROP", "OP_2DUP", "OP_3DUP", "OP_2OVER", "OP_2ROT", "OP_2SWAP",
230 "OP_IFDUP", "OP_DEPTH", "OP_DROP", "OP_DUP", "OP_NIP", "OP_OVER", "OP_PICK", "OP_ROLL", "OP_ROT",
231 "OP_SWAP", "OP_TUCK", "OP_CAT", "OP_SUBSTR", "OP_LEFT", "OP_RIGHT", "OP_SIZE", "OP_INVERT", "OP_AND",
232 "OP_OR", "OP_XOR", "OP_EQUAL", "OP_EQUALVERIFY", "OP_RESERVED1", "OP_RESERVED2", "OP_1ADD", "OP_1SUB", "OP_2MUL",
233 "OP_2DIV", "OP_NEGATE", "OP_ABS", "OP_NOT", "OP_0NOTEQUAL", "OP_ADD", "OP_SUB", "OP_MUL", "OP_DIV",
234 "OP_MOD", "OP_LSHIFT", "OP_RSHIFT", "OP_BOOLAND", "OP_BOOLOR",
235 "OP_NUMEQUAL", "OP_NUMEQUALVERIFY", "OP_NUMNOTEQUAL", "OP_LESSTHAN",
236 "OP_GREATERTHAN", "OP_LESSTHANOREQUAL", "OP_GREATERTHANOREQUAL", "OP_MIN", "OP_MAX",
237 "OP_WITHIN", "OP_RIPEMD160", "OP_SHA1", "OP_SHA256", "OP_HASH160",
238 "OP_HASH256", "OP_CODESEPARATOR", "OP_CHECKSIG", "OP_CHECKSIGVERIFY", "OP_CHECKMULTISIG",
239 "OP_CHECKMULTISIGVERIFY",
240 ("OP_SINGLEBYTE_END", 0xF0),
241 ("OP_DOUBLEBYTE_BEGIN", 0xF000),
242 "OP_PUBKEY", "OP_PUBKEYHASH",
243 ("OP_INVALIDOPCODE", 0xFFFF),
247 def script_GetOp(bytes):
249 while i < len(bytes):
251 opcode = ord(bytes[i])
253 if opcode >= opcodes.OP_SINGLEBYTE_END:
255 opcode |= ord(bytes[i])
258 if opcode <= opcodes.OP_PUSHDATA4:
260 if opcode == opcodes.OP_PUSHDATA1:
261 nSize = ord(bytes[i])
263 elif opcode == opcodes.OP_PUSHDATA2:
264 (nSize,) = struct.unpack_from('<H', bytes, i)
266 elif opcode == opcodes.OP_PUSHDATA4:
267 (nSize,) = struct.unpack_from('<I', bytes, i)
269 vch = bytes[i:i+nSize]
272 yield (opcode, vch, i)
275 def script_GetOpName(opcode):
276 return (opcodes.whatis(opcode)).replace("OP_", "")
279 def decode_script(bytes):
281 for (opcode, vch, i) in script_GetOp(bytes):
282 if len(result) > 0: result += " "
283 if opcode <= opcodes.OP_PUSHDATA4:
284 result += "%d:"%(opcode,)
285 result += short_hex(vch)
287 result += script_GetOpName(opcode)
291 def match_decoded(decoded, to_match):
292 if len(decoded) != len(to_match):
294 for i in range(len(decoded)):
295 if to_match[i] == opcodes.OP_PUSHDATA4 and decoded[i][0] <= opcodes.OP_PUSHDATA4 and decoded[i][0]>0:
296 continue # Opcodes below OP_PUSHDATA4 all just push data onto stack, and are equivalent.
297 if to_match[i] != decoded[i][0]:
302 def parse_sig(x_sig):
308 assert sig == NO_SIGNATURE
312 def is_extended_pubkey(x_pubkey):
313 return x_pubkey[0:2] in ['fe', 'ff']
315 def x_to_xpub(x_pubkey):
316 if x_pubkey[0:2] == 'ff':
317 from account import BIP32_Account
318 xpub, s = BIP32_Account.parse_xpubkey(x_pubkey)
323 def parse_xpub(x_pubkey):
324 if x_pubkey[0:2] == 'ff':
325 from account import BIP32_Account
326 xpub, s = BIP32_Account.parse_xpubkey(x_pubkey)
327 pubkey = BIP32_Account.derive_pubkey_from_xpub(xpub, s[0], s[1])
328 elif x_pubkey[0:2] == 'fe':
329 from account import OldAccount
330 mpk, s = OldAccount.parse_xpubkey(x_pubkey)
331 pubkey = OldAccount.get_pubkey_from_mpk(mpk.decode('hex'), s[0], s[1])
337 def parse_scriptSig(d, bytes):
339 decoded = [ x for x in script_GetOp(bytes) ]
341 # coinbase transactions raise an exception
342 print_error("cannot find address in input script", bytes.encode('hex'))
346 match = [ opcodes.OP_PUSHDATA4 ]
347 if match_decoded(decoded, match):
348 sig = decoded[0][1].encode('hex')
349 d['address'] = "(pubkey)"
350 d['signatures'] = [sig]
352 d['x_pubkeys'] = ["(pubkey)"]
353 d['pubkeys'] = ["(pubkey)"]
356 # non-generated TxIn transactions push a signature
357 # (seventy-something bytes) and then their public key
358 # (65 bytes) onto the stack:
359 match = [ opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4 ]
360 if match_decoded(decoded, match):
361 sig = decoded[0][1].encode('hex')
362 x_pubkey = decoded[1][1].encode('hex')
364 signatures = parse_sig([sig])
365 pubkey = parse_xpub(x_pubkey)
368 traceback.print_exc(file=sys.stdout)
369 print_error("cannot find address in input script", bytes.encode('hex'))
371 d['signatures'] = signatures
372 d['x_pubkeys'] = [x_pubkey]
374 d['pubkeys'] = [pubkey]
375 d['address'] = public_key_to_bc_address(pubkey.decode('hex'))
378 # p2sh transaction, 2 of n
379 match = [ opcodes.OP_0 ]
380 while len(match) < len(decoded):
381 match.append(opcodes.OP_PUSHDATA4)
383 if not match_decoded(decoded, match):
384 print_error("cannot find address in input script", bytes.encode('hex'))
387 x_sig = map(lambda x:x[1].encode('hex'), decoded[1:-1])
388 d['signatures'] = parse_sig(x_sig)
391 dec2 = [ x for x in script_GetOp(decoded[-1][1]) ]
392 match_2of2 = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_2, opcodes.OP_CHECKMULTISIG ]
393 match_2of3 = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_3, opcodes.OP_CHECKMULTISIG ]
394 if match_decoded(dec2, match_2of2):
395 x_pubkeys = [ dec2[1][1].encode('hex'), dec2[2][1].encode('hex') ]
396 elif match_decoded(dec2, match_2of3):
397 x_pubkeys = [ dec2[1][1].encode('hex'), dec2[2][1].encode('hex'), dec2[3][1].encode('hex') ]
399 print_error("cannot find address in input script", bytes.encode('hex'))
402 d['x_pubkeys'] = x_pubkeys
403 pubkeys = map(parse_xpub, x_pubkeys)
404 d['pubkeys'] = pubkeys
405 redeemScript = Transaction.multisig_script(pubkeys,2)
406 d['redeemScript'] = redeemScript
407 d['address'] = hash_160_to_bc_address(hash_160(redeemScript.decode('hex')), 20)
412 def get_address_from_output_script(bytes):
413 decoded = [ x for x in script_GetOp(bytes) ]
415 # The Genesis Block, self-payments, and pay-by-IP-address payments look like:
416 # 65 BYTES:... CHECKSIG
417 match = [ opcodes.OP_PUSHDATA4, opcodes.OP_CHECKSIG ]
418 if match_decoded(decoded, match):
419 return 'pubkey', decoded[0][1].encode('hex')
421 # Pay-by-Bitcoin-address TxOuts look like:
422 # DUP HASH160 20 BYTES:... EQUALVERIFY CHECKSIG
423 match = [ opcodes.OP_DUP, opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUALVERIFY, opcodes.OP_CHECKSIG ]
424 if match_decoded(decoded, match):
425 return 'address', hash_160_to_bc_address(decoded[2][1])
428 match = [ opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUAL ]
429 if match_decoded(decoded, match):
430 return 'address', hash_160_to_bc_address(decoded[1][1],20)
432 return "(None)", "(None)"
438 def parse_input(vds):
440 prevout_hash = hash_encode(vds.read_bytes(32))
441 prevout_n = vds.read_uint32()
442 scriptSig = vds.read_bytes(vds.read_compact_size())
443 sequence = vds.read_uint32()
444 if prevout_hash == '00'*32:
445 d['is_coinbase'] = True
447 d['is_coinbase'] = False
448 d['prevout_hash'] = prevout_hash
449 d['prevout_n'] = prevout_n
450 d['sequence'] = sequence
455 parse_scriptSig(d, scriptSig)
459 def parse_output(vds, i):
461 d['value'] = vds.read_int64()
462 scriptPubKey = vds.read_bytes(vds.read_compact_size())
463 type, address = get_address_from_output_script(scriptPubKey)
465 d['address'] = address
466 d['scriptPubKey'] = scriptPubKey.encode('hex')
471 def deserialize(raw):
473 vds.write(raw.decode('hex'))
475 start = vds.read_cursor
476 d['version'] = vds.read_int32()
477 d['timestamp'] = vds.read_int32()
478 n_vin = vds.read_compact_size()
480 for i in xrange(n_vin):
481 d['inputs'].append(parse_input(vds))
482 n_vout = vds.read_compact_size()
484 for i in xrange(n_vout):
485 d['outputs'].append(parse_output(vds, i))
486 d['lockTime'] = vds.read_uint32()
490 push_script = lambda x: op_push(len(x)/2) + x
496 self.raw = self.serialize(self.timestamp, self.inputs, self.outputs, for_sig = None) # for_sig=-1 means do not sign
499 def __init__(self, timestamp, inputs, outputs, locktime=0):
500 self.timestamp = timestamp
502 self.outputs = outputs
503 self.locktime = locktime
507 def deserialize(klass, raw):
508 self = klass(0, [],[])
512 def update(self, raw):
515 self.timestamp = d['timestamp']
516 self.inputs = d['inputs']
517 self.outputs = map(lambda x: (x['type'], x['address'], x['value']), d['outputs'])
518 self.locktime = d['lockTime']
521 def sweep(klass, privkeys, network, to_address, fee):
523 for privkey in privkeys:
524 pubkey = public_key_from_private_key(privkey)
525 address = address_from_private_key(privkey)
526 u = network.synchronous_get([ ('blockchain.address.listunspent',[address])])[0]
527 pay_script = klass.pay_script('address', address)
529 item['scriptPubKey'] = pay_script
530 item['redeemPubkey'] = pubkey
531 item['address'] = address
532 item['prevout_hash'] = item['tx_hash']
533 item['prevout_n'] = item['tx_pos']
539 total = sum( map(lambda x:int(x.get('value')), inputs) ) - fee
540 outputs = [('address', to_address, total)]
541 self = klass(inputs, outputs)
542 self.sign({ pubkey:privkey })
546 def multisig_script(klass, public_keys, num=None):
548 if num is None: num = n
549 # supports only "2 of 2", and "2 of 3" transactions
550 assert num <= n and n in [2,3]
559 for k in public_keys:
560 s += op_push(len(k)/2)
574 def pay_script(self, type, payto):
575 assert type == 'address' or type == 'pubkey' or type == 'op_return'
577 if type == 'op_return':
578 h = payto.encode('hex')
579 return '6a' + push_script(h)
580 elif type == 'pubkey':
581 script = push_script(payto)
582 script += 'ac' # op_checksig
585 addrtype, hash_160 = bc_address_to_hash_160(payto)
587 script = '76a9' # op_dup, op_hash_160
588 script += push_script(hash_160.encode('hex'))
589 script += '88ac' # op_equalverify, op_checksig
591 script = 'a9' # op_hash_160
592 script += push_script(hash_160.encode('hex'))
593 script += '87' # op_equal
600 def serialize(klass, timestamp, inputs, outputs, for_sig = None ):
601 s = int_to_hex(1,4) # version
602 s += int_to_hex(timestamp, 4) # timestamp
603 s += var_int( len(inputs) ) # number of inputs
604 for i in range(len(inputs)):
607 s += txin['prevout_hash'].decode('hex')[::-1].encode('hex') # prev hash
608 s += int_to_hex(txin['prevout_n'],4) # prev index
610 p2sh = txin.get('redeemScript') is not None
611 num_sig = txin['num_sig']
612 payto = txin['address'] if txin["type"] == 'address' else txin['pubkeys'][0]
614 x_signatures = txin['signatures']
615 signatures = filter(lambda x: x is not None, x_signatures)
616 is_complete = len(signatures) == num_sig
619 # if we have enough signatures, we use the actual pubkeys
620 # use extended pubkeys (with bip32 derivation)
623 pubkeys = txin['pubkeys']
624 for signature in signatures:
625 sig_list.append(signature + '01')
627 pubkeys = txin['x_pubkeys']
628 for signature in x_signatures:
629 sig_list.append((signature + '01') if signature is not None else NO_SIGNATURE)
631 sig_list = ''.join( map( lambda x: push_script(x), sig_list))
634 if txin["type"] != 'pubkey':
635 script += push_script(pubkeys[0])
639 redeem_script = klass.multisig_script(pubkeys,2)
640 script += push_script(redeem_script)
643 script = txin['redeemScript'] if p2sh else klass.pay_script(txin["type"], payto)
647 s += var_int( len(script)/2 ) # script length
649 s += "ffffffff" # sequence
651 s += var_int( len(outputs) ) # number of outputs
652 for output in outputs:
653 type, addr, amount = output
654 s += int_to_hex( amount, 8) # amount
655 script = klass.pay_script(type, addr)
656 s += var_int( len(script)/2 ) # script length
658 s += int_to_hex(0,4) # lock time
659 if for_sig is not None and for_sig != -1:
660 s += int_to_hex(1, 4) # hash type
664 def tx_for_sig(self,i):
665 return self.serialize(self.timestamp, self.inputs, self.outputs, for_sig = i)
669 return Hash(self.raw.decode('hex') )[::-1].encode('hex')
671 def add_signature(self, i, pubkey, sig):
672 print_error("adding signature for", pubkey)
673 txin = self.inputs[i]
674 pubkeys = txin['pubkeys']
675 ii = pubkeys.index(pubkey)
676 txin['signatures'][ii] = sig
677 txin['x_pubkeys'][ii] = pubkey
678 self.inputs[i] = txin
679 self.raw = self.serialize(self.timestamp, self.inputs, self.outputs)
682 def signature_count(self):
685 for txin in self.inputs:
686 signatures = filter(lambda x: x is not None, txin['signatures'])
692 def is_complete(self):
693 s, r = self.signature_count()
697 def inputs_to_sign(self):
698 from account import BIP32_Account, OldAccount
701 for txin in self.inputs:
702 x_signatures = txin['signatures']
703 signatures = filter(lambda x: x is not None, x_signatures)
705 if len(signatures) == txin['num_sig']:
709 for k, x_pubkey in enumerate(txin['x_pubkeys']):
711 if x_signatures[k] is not None:
712 # this pubkey already signed
715 if x_pubkey[0:2] == 'ff':
716 xpub, sequence = BIP32_Account.parse_xpubkey(x_pubkey)
717 xpub_list.append((xpub,sequence))
718 elif x_pubkey[0:2] == 'fe':
719 xpub, sequence = OldAccount.parse_xpubkey(x_pubkey)
720 xpub_list.append((xpub,sequence))
722 addr_list.add(txin['address'])
724 return addr_list, xpub_list
727 def sign(self, keypairs):
728 print_error("tx.sign(), keypairs:", keypairs)
730 for i, txin in enumerate(self.inputs):
732 # continue if this txin is complete
733 signatures = filter(lambda x: x is not None, txin['signatures'])
734 num = txin['num_sig']
735 if len(signatures) == num:
738 redeem_pubkeys = txin['pubkeys']
739 for_sig = Hash(self.tx_for_sig(i).decode('hex'))
740 for pubkey in redeem_pubkeys:
741 if pubkey in keypairs.keys():
743 sec = keypairs[pubkey]
744 pkey = regenerate_key(sec)
746 private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
747 public_key = private_key.get_verifying_key()
748 sig = private_key.sign_digest_deterministic( for_sig, hashfunc=hashlib.sha256, sigencode = ecdsa.util.sigencode_der_canonize )
750 assert public_key.verify_digest( sig, for_sig, sigdecode = ecdsa.util.sigdecode_der)
751 self.add_signature(i, pubkey, sig.encode('hex'))
753 print_error("is_complete", self.is_complete())
754 self.raw = self.serialize( self.timestamp, self.inputs, self.outputs )
757 def add_pubkey_addresses(self, txlist):
758 for i in self.inputs:
760 if i.get("address") == "(pubkey)":
761 prev_tx = txlist.get(i.get('prevout_hash'))
763 type, address, value = prev_tx.get_outputs()[i.get('prevout_n')]
764 print_error("found pay-to-pubkey address:", address)
765 i["address"] = address
769 def get_outputs(self):
770 """convert pubkeys to addresses"""
772 for type, x, v in self.outputs:
773 if type == 'address':
775 elif type == 'pubkey':
776 addr = public_key_to_bc_address(x.decode('hex'))
779 o.append((type,addr,v))
783 def get_output_addresses(self):
784 return map(lambda x:x[1], self.get_outputs())
787 def has_address(self, addr):
789 for txin in self.inputs:
790 if addr == txin.get('address'):
793 if addr in self.get_output_addresses():
799 def get_value(self, addresses, prevout_values):
800 # return the balance for that tx
805 v_in = v_out = v_out_mine = 0
807 for item in self.inputs:
808 addr = item.get('address')
809 if addr in addresses:
812 key = item['prevout_hash'] + ':%d'%item['prevout_n']
813 value = prevout_values.get( key )
821 if not is_send: is_partial = False
823 for type, addr, value in self.get_outputs():
825 if addr in addresses:
830 # some inputs are mine:
833 v = v_out_mine - v_out
839 v = v_out_mine - v_in
842 # some inputs are mine, but not all
846 # all inputs are mine
849 return is_relevant, is_send, v, fee
856 "complete":self.is_complete()
861 def requires_fee(self, verifier):
863 size = len(self.raw)/2
867 for o in self.get_outputs():
872 for i in self.inputs:
873 age = verifier.get_confirmations(i["prevout_hash"])[0]
874 sum += i["value"] * age
875 priority = sum / size
876 print_error(priority, threshold)
877 return priority < threshold