2012-01-19 08:11:36 -08:00
#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client
# Copyright (C) 2011 thomasv@gitorious
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2012-08-23 18:21:17 -07:00
import sys
import base64
import os
import re
import hashlib
import copy
import operator
import ast
import threading
import random
import aes
import ecdsa
2012-10-20 17:57:31 -07:00
import Queue
2012-10-22 08:18:07 -07:00
import time
2012-01-19 08:11:36 -08:00
2012-05-14 05:09:50 -07:00
from ecdsa . util import string_to_number , number_to_string
2012-10-19 05:55:01 -07:00
from util import print_error , user_dir , format_satoshis
from bitcoin import *
2012-07-06 21:45:57 -07:00
2012-02-14 03:45:39 -08:00
# URL decode
_ud = re . compile ( ' % ([0-9a-hA-H] {2} ) ' , re . MULTILINE )
urldecode = lambda x : _ud . sub ( lambda m : chr ( int ( m . group ( 1 ) , 16 ) ) , x )
2012-10-19 05:55:01 -07:00
# AES encryption
2012-01-19 08:11:36 -08:00
EncodeAES = lambda secret , s : base64 . b64encode ( aes . encryptData ( secret , s ) )
DecodeAES = lambda secret , e : aes . decryptData ( secret , base64 . b64decode ( e ) )
2012-02-05 22:48:52 -08:00
from version import ELECTRUM_VERSION , SEED_VERSION
2012-01-19 08:11:36 -08:00
class Wallet :
2012-10-11 11:10:12 -07:00
def __init__ ( self , config = { } ) :
2012-01-19 08:11:36 -08:00
2012-10-11 11:10:12 -07:00
self . config = config
2012-01-19 08:11:36 -08:00
self . electrum_version = ELECTRUM_VERSION
# saved fields
2012-10-11 11:10:12 -07:00
self . seed_version = config . get ( ' seed_version ' , SEED_VERSION )
self . gap_limit = config . get ( ' gap_limit ' , 5 )
self . use_change = config . get ( ' use_change ' , True )
self . fee = int ( config . get ( ' fee ' , 100000 ) )
self . num_zeros = int ( config . get ( ' num_zeros ' , 0 ) )
2012-10-16 00:24:38 -07:00
self . master_public_key = config . get ( ' master_public_key ' , ' ' )
2012-10-11 11:10:12 -07:00
self . use_encryption = config . get ( ' use_encryption ' , False )
self . addresses = config . get ( ' addresses ' , [ ] ) # receiving addresses visible for user
self . change_addresses = config . get ( ' change_addresses ' , [ ] ) # addresses used as change
self . seed = config . get ( ' seed ' , ' ' ) # encrypted
self . labels = config . get ( ' labels ' , { } ) # labels for addresses and transactions
self . aliases = config . get ( ' aliases ' , { } ) # aliases for addresses
self . authorities = config . get ( ' authorities ' , { } ) # trusted addresses
self . frozen_addresses = config . get ( ' frozen_addresses ' , [ ] )
self . prioritized_addresses = config . get ( ' prioritized_addresses ' , [ ] )
self . receipts = config . get ( ' receipts ' , { } ) # signed URIs
self . addressbook = config . get ( ' contacts ' , [ ] ) # outgoing addresses, for payments
self . imported_keys = config . get ( ' imported_keys ' , { } )
2012-11-04 06:47:19 -08:00
self . history = config . get ( ' addr_history ' , { } ) # address -> list(txid, height)
2012-11-03 01:17:40 -07:00
self . transactions = config . get ( ' transactions ' , { } ) # txid -> deserialised
2012-01-19 08:11:36 -08:00
# not saved
2012-11-03 01:17:40 -07:00
self . prevout_values = { }
self . spent_outputs = [ ]
2012-10-11 11:10:12 -07:00
self . receipt = None # next receipt
2012-03-23 08:34:34 -07:00
self . banner = ' '
2012-05-04 02:14:07 -07:00
2012-10-26 01:02:09 -07:00
# spv
self . verifier = None
2012-10-14 22:43:00 -07:00
# there is a difference between wallet.up_to_date and interface.is_up_to_date()
# interface.is_up_to_date() returns true when all requests have been answered and processed
# wallet.up_to_date is true when the wallet is synchronized (stronger requirement)
2012-03-23 08:34:34 -07:00
self . up_to_date_event = threading . Event ( )
self . up_to_date_event . clear ( )
2012-03-28 05:22:46 -07:00
self . up_to_date = False
2012-03-31 02:47:16 -07:00
self . lock = threading . Lock ( )
2012-03-24 05:15:23 -07:00
self . tx_event = threading . Event ( )
2012-03-23 08:34:34 -07:00
2012-10-11 11:10:12 -07:00
if self . seed_version != SEED_VERSION :
raise ValueError ( " This wallet seed is deprecated. Please run upgrade.py for a diagnostic. " )
2012-11-03 01:17:40 -07:00
for tx_hash in self . transactions . keys ( ) :
self . update_tx_outputs ( tx_hash )
2012-10-22 05:49:29 -07:00
def init_up_to_date ( self ) :
self . up_to_date_event . clear ( )
self . up_to_date = False
2012-04-01 08:50:12 -07:00
2012-11-03 01:17:40 -07:00
2012-01-19 08:11:36 -08:00
def import_key ( self , keypair , password ) :
address , key = keypair . split ( ' : ' )
2012-05-30 05:42:30 -07:00
if not self . is_valid ( address ) :
raise BaseException ( ' Invalid Bitcoin address ' )
if address in self . all_addresses ( ) :
raise BaseException ( ' Address already in wallet ' )
2012-01-19 08:11:36 -08:00
b = ASecretToSecret ( key )
2012-06-06 09:32:29 -07:00
if not b :
raise BaseException ( ' Unsupported key format ' )
2012-01-19 08:11:36 -08:00
secexp = int ( b . encode ( ' hex ' ) , 16 )
private_key = ecdsa . SigningKey . from_secret_exponent ( secexp , curve = SECP256k1 )
# sanity check
public_key = private_key . get_verifying_key ( )
2012-05-30 05:42:30 -07:00
if not address == public_key_to_bc_address ( ' 04 ' . decode ( ' hex ' ) + public_key . to_string ( ) ) :
raise BaseException ( ' Address does not match private key ' )
2012-01-19 08:11:36 -08:00
self . imported_keys [ address ] = self . pw_encode ( key , password )
2012-10-11 11:10:12 -07:00
2012-01-19 08:11:36 -08:00
def new_seed ( self , password ) :
seed = " %032x " % ecdsa . util . randrange ( pow ( 2 , 128 ) )
2012-04-09 02:38:21 -07:00
#self.init_mpk(seed)
2012-01-19 08:11:36 -08:00
# encrypt
2012-01-19 08:39:24 -08:00
self . seed = self . pw_encode ( seed , password )
2012-01-19 08:11:36 -08:00
2012-02-21 05:36:45 -08:00
2012-01-19 08:11:36 -08:00
def init_mpk ( self , seed ) :
# public key
curve = SECP256k1
secexp = self . stretch_key ( seed )
master_private_key = ecdsa . SigningKey . from_secret_exponent ( secexp , curve = SECP256k1 )
2012-10-16 00:24:38 -07:00
self . master_public_key = master_private_key . get_verifying_key ( ) . to_string ( ) . encode ( ' hex ' )
2012-01-19 08:11:36 -08:00
def all_addresses ( self ) :
return self . addresses + self . change_addresses + self . imported_keys . keys ( )
def is_mine ( self , address ) :
return address in self . all_addresses ( )
def is_change ( self , address ) :
return address in self . change_addresses
def is_valid ( self , addr ) :
ADDRESS_RE = re . compile ( ' [1-9A-HJ-NP-Za-km-z] { 26,} \\ Z ' )
if not ADDRESS_RE . match ( addr ) : return False
try :
h = bc_address_to_hash_160 ( addr )
except :
return False
return addr == hash_160_to_bc_address ( h )
def stretch_key ( self , seed ) :
oldseed = seed
for i in range ( 100000 ) :
seed = hashlib . sha256 ( seed + oldseed ) . digest ( )
return string_to_number ( seed )
def get_sequence ( self , n , for_change ) :
2012-10-16 00:24:38 -07:00
return string_to_number ( Hash ( " %d : %d : " % ( n , for_change ) + self . master_public_key . decode ( ' hex ' ) ) )
2012-01-19 08:11:36 -08:00
2012-05-14 12:31:37 -07:00
def get_private_key_base58 ( self , address , password ) :
pk = self . get_private_key ( address , password )
if pk is None : return None
return SecretToASecret ( pk )
2012-02-06 09:10:30 -08:00
def get_private_key ( self , address , password ) :
2012-01-19 08:11:36 -08:00
""" Privatekey(type,n) = Master_private_key + H(n|S|type) """
order = generator_secp256k1 . order ( )
if address in self . imported_keys . keys ( ) :
b = self . pw_decode ( self . imported_keys [ address ] , password )
2012-05-17 09:10:36 -07:00
if not b : return None
2012-01-19 08:11:36 -08:00
b = ASecretToSecret ( b )
secexp = int ( b . encode ( ' hex ' ) , 16 )
else :
if address in self . addresses :
n = self . addresses . index ( address )
for_change = False
elif address in self . change_addresses :
n = self . change_addresses . index ( address )
for_change = True
else :
raise BaseException ( " unknown address " )
2012-02-21 14:27:21 -08:00
try :
seed = self . pw_decode ( self . seed , password )
except :
raise BaseException ( " Invalid password " )
2012-05-16 23:42:40 -07:00
if not seed : return None
2012-01-19 08:11:36 -08:00
secexp = self . stretch_key ( seed )
secexp = ( secexp + self . get_sequence ( n , for_change ) ) % order
pk = number_to_string ( secexp , order )
return pk
2012-02-01 11:27:03 -08:00
2012-02-02 06:26:10 -08:00
def msg_magic ( self , message ) :
return " \x18 Bitcoin Signed Message: \n " + chr ( len ( message ) ) + message
2012-02-01 11:27:03 -08:00
def sign_message ( self , address , message , password ) :
2012-02-06 09:10:30 -08:00
private_key = ecdsa . SigningKey . from_string ( self . get_private_key ( address , password ) , curve = SECP256k1 )
2012-02-01 11:27:03 -08:00
public_key = private_key . get_verifying_key ( )
2012-02-02 06:26:10 -08:00
signature = private_key . sign_digest ( Hash ( self . msg_magic ( message ) ) , sigencode = ecdsa . util . sigencode_string )
assert public_key . verify_digest ( signature , Hash ( self . msg_magic ( message ) ) , sigdecode = ecdsa . util . sigdecode_string )
2012-02-01 11:27:03 -08:00
for i in range ( 4 ) :
sig = base64 . b64encode ( chr ( 27 + i ) + signature )
2012-02-03 02:48:09 -08:00
try :
self . verify_message ( address , sig , message )
2012-02-01 11:27:03 -08:00
return sig
2012-02-03 02:48:09 -08:00
except :
continue
2012-02-01 11:27:03 -08:00
else :
raise BaseException ( " error: cannot sign message " )
2012-06-12 01:47:00 -07:00
2012-02-01 11:27:03 -08:00
def verify_message ( self , address , signature , message ) :
""" See http://www.secg.org/download/aid-780/sec1-v2.pdf for the math """
from ecdsa import numbertheory , ellipticcurve , util
import msqr
curve = curve_secp256k1
G = generator_secp256k1
order = G . order ( )
2012-02-02 06:26:10 -08:00
# extract r,s from signature
2012-02-01 11:27:03 -08:00
sig = base64 . b64decode ( signature )
2012-02-06 08:59:31 -08:00
if len ( sig ) != 65 : raise BaseException ( " Wrong encoding " )
2012-02-01 11:27:03 -08:00
r , s = util . sigdecode_string ( sig [ 1 : ] , order )
2012-06-12 01:47:00 -07:00
nV = ord ( sig [ 0 ] )
if nV < 27 or nV > = 35 :
raise BaseException ( " Bad encoding " )
if nV > = 31 :
compressed = True
nV - = 4
else :
compressed = False
recid = nV - 27
2012-02-01 11:27:03 -08:00
# 1.1
x = r + ( recid / 2 ) * order
# 1.3
alpha = ( x * x * x + curve . a ( ) * x + curve . b ( ) ) % curve . p ( )
beta = msqr . modular_sqrt ( alpha , curve . p ( ) )
y = beta if ( beta - recid ) % 2 == 0 else curve . p ( ) - beta
# 1.4 the constructor checks that nR is at infinity
2012-02-03 02:48:09 -08:00
R = ellipticcurve . Point ( curve , x , y , order )
2012-02-01 11:27:03 -08:00
# 1.5 compute e from message:
2012-02-02 06:26:10 -08:00
h = Hash ( self . msg_magic ( message ) )
2012-02-01 11:27:03 -08:00
e = string_to_number ( h )
minus_e = - e % order
# 1.6 compute Q = r^-1 (sR - eG)
inv_r = numbertheory . inverse_mod ( r , order )
Q = inv_r * ( s * R + minus_e * G )
public_key = ecdsa . VerifyingKey . from_public_point ( Q , curve = SECP256k1 )
# check that Q is the public key
2012-02-03 02:48:09 -08:00
public_key . verify_digest ( sig [ 1 : ] , h , sigdecode = ecdsa . util . sigdecode_string )
2012-02-01 11:27:03 -08:00
# check that we get the original signing address
2012-06-12 01:47:00 -07:00
addr = public_key_to_bc_address ( encode_point ( public_key , compressed ) )
2012-02-06 09:07:01 -08:00
if address != addr :
2012-02-06 08:59:31 -08:00
raise BaseException ( " Bad signature " )
2012-01-20 08:21:29 -08:00
2012-01-19 08:11:36 -08:00
2012-03-23 12:58:54 -07:00
def create_new_address ( self , for_change ) :
2012-01-19 08:11:36 -08:00
n = len ( self . change_addresses ) if for_change else len ( self . addresses )
2012-10-01 09:31:54 -07:00
address = self . get_new_address ( n , for_change )
2012-01-19 08:11:36 -08:00
if for_change :
self . change_addresses . append ( address )
else :
self . addresses . append ( address )
2012-02-21 05:36:45 -08:00
self . history [ address ] = [ ]
2012-10-01 09:31:54 -07:00
return address
def get_new_address ( self , n , for_change ) :
""" Publickey(type,n) = Master_public_key + H(n|S|type)*point """
curve = SECP256k1
z = self . get_sequence ( n , for_change )
2012-10-16 00:24:38 -07:00
master_public_key = ecdsa . VerifyingKey . from_string ( self . master_public_key . decode ( ' hex ' ) , curve = SECP256k1 )
2012-10-01 09:31:54 -07:00
pubkey_point = master_public_key . pubkey . point + z * curve . generator
public_key2 = ecdsa . VerifyingKey . from_public_point ( pubkey_point , curve = SECP256k1 )
address = public_key_to_bc_address ( ' 04 ' . decode ( ' hex ' ) + public_key2 . to_string ( ) )
2012-01-19 08:11:36 -08:00
print address
2012-02-21 05:36:45 -08:00
return address
2012-10-01 09:31:54 -07:00
2012-02-21 05:36:45 -08:00
2012-06-07 09:52:29 -07:00
def change_gap_limit ( self , value ) :
if value > = self . gap_limit :
self . gap_limit = value
self . save ( )
2012-10-26 15:02:52 -07:00
self . interface . poke ( ' synchronizer ' )
2012-06-07 09:52:29 -07:00
return True
elif value > = self . min_acceptable_gap ( ) :
k = self . num_unused_trailing_addresses ( )
n = len ( self . addresses ) - k + value
self . addresses = self . addresses [ 0 : n ]
self . gap_limit = value
self . save ( )
return True
else :
return False
def num_unused_trailing_addresses ( self ) :
k = 0
for a in self . addresses [ : : - 1 ] :
if self . history . get ( a ) : break
k = k + 1
return k
def min_acceptable_gap ( self ) :
# fixme: this assumes wallet is synchronized
n = 0
nmax = 0
k = self . num_unused_trailing_addresses ( )
for a in self . addresses [ 0 : - k ] :
if self . history . get ( a ) :
n = 0
else :
n + = 1
if n > nmax : nmax = n
2012-06-07 09:59:28 -07:00
return nmax + 1
2012-06-07 09:52:29 -07:00
2012-01-19 08:11:36 -08:00
def synchronize ( self ) :
2012-03-23 08:34:34 -07:00
if not self . master_public_key :
2012-03-23 12:58:54 -07:00
return [ ]
2012-02-22 06:39:06 -08:00
2012-03-23 12:58:54 -07:00
new_addresses = [ ]
2012-01-19 08:11:36 -08:00
while True :
if self . change_addresses == [ ] :
2012-03-23 12:58:54 -07:00
new_addresses . append ( self . create_new_address ( True ) )
2012-01-19 08:11:36 -08:00
continue
a = self . change_addresses [ - 1 ]
if self . history . get ( a ) :
2012-03-23 12:58:54 -07:00
new_addresses . append ( self . create_new_address ( True ) )
2012-01-19 08:11:36 -08:00
else :
break
n = self . gap_limit
while True :
if len ( self . addresses ) < n :
2012-03-23 12:58:54 -07:00
new_addresses . append ( self . create_new_address ( False ) )
2012-01-19 08:11:36 -08:00
continue
2012-10-26 09:00:32 -07:00
if map ( lambda a : self . history . get ( a , [ ] ) , self . addresses [ - n : ] ) == n * [ [ ] ] :
2012-01-19 08:11:36 -08:00
break
else :
2012-03-23 12:58:54 -07:00
new_addresses . append ( self . create_new_address ( False ) )
2012-01-19 08:11:36 -08:00
2012-03-23 12:58:54 -07:00
return new_addresses
2012-02-22 06:39:06 -08:00
2012-03-23 08:34:34 -07:00
2012-01-19 08:11:36 -08:00
def is_found ( self ) :
return ( len ( self . change_addresses ) > 1 ) or ( len ( self . addresses ) > self . gap_limit )
def fill_addressbook ( self ) :
2012-11-03 01:17:40 -07:00
for tx_hash , tx in self . transactions . items ( ) :
if self . get_tx_value ( tx_hash ) < 0 :
2012-01-19 08:11:36 -08:00
for i in tx [ ' outputs ' ] :
if not self . is_mine ( i ) and i not in self . addressbook :
self . addressbook . append ( i )
# redo labels
self . update_tx_labels ( )
2012-06-07 07:14:08 -07:00
def get_address_flags ( self , addr ) :
flags = " C " if self . is_change ( addr ) else " I " if addr in self . imported_keys . keys ( ) else " - "
flags + = " F " if addr in self . frozen_addresses else " P " if addr in self . prioritized_addresses else " - "
return flags
2012-01-19 08:11:36 -08:00
2012-11-03 01:17:40 -07:00
def get_tx_value ( self , tx_hash , addresses = None ) :
# return the balance for that tx
if addresses is None : addresses = self . all_addresses ( )
v = 0
d = self . transactions . get ( tx_hash )
if not d : return 0
for item in d . get ( ' inputs ' ) :
addr = item . get ( ' address ' )
if addr in addresses :
key = item [ ' prevout_hash ' ] + ' : %d ' % item [ ' prevout_n ' ]
value = self . prevout_values [ key ]
v - = value
for item in d . get ( ' outputs ' ) :
addr = item . get ( ' address ' )
if addr in addresses :
value = item . get ( ' value ' )
v + = value
return v
def update_tx_outputs ( self , tx_hash ) :
tx = self . transactions . get ( tx_hash )
for item in tx . get ( ' outputs ' ) :
value = item . get ( ' value ' )
key = tx_hash + ' : %d ' % item . get ( ' index ' )
with self . lock :
self . prevout_values [ key ] = value
for item in tx . get ( ' inputs ' ) :
if self . is_mine ( item . get ( ' address ' ) ) :
key = item [ ' prevout_hash ' ] + ' : %d ' % item [ ' prevout_n ' ]
self . spent_outputs . append ( key )
2012-01-19 08:11:36 -08:00
def get_addr_balance ( self , addr ) :
2012-05-01 01:55:10 -07:00
assert self . is_mine ( addr )
h = self . history . get ( addr , [ ] )
2012-01-19 08:11:36 -08:00
c = u = 0
2012-11-03 01:17:40 -07:00
for tx_hash , tx_height in h :
v = self . get_tx_value ( tx_hash , [ addr ] )
if tx_height :
2012-01-19 08:11:36 -08:00
c + = v
else :
u + = v
return c , u
def get_balance ( self ) :
conf = unconf = 0
for addr in self . all_addresses ( ) :
c , u = self . get_addr_balance ( addr )
conf + = c
unconf + = u
return conf , unconf
2012-02-10 04:23:39 -08:00
def choose_tx_inputs ( self , amount , fixed_fee , from_addr = None ) :
2012-01-19 08:11:36 -08:00
""" todo: minimize tx size """
total = 0
fee = self . fee if fixed_fee is None else fixed_fee
coins = [ ]
2012-06-06 06:40:57 -07:00
prioritized_coins = [ ]
2012-02-10 04:23:39 -08:00
domain = [ from_addr ] if from_addr else self . all_addresses ( )
2012-05-05 02:33:53 -07:00
for i in self . frozen_addresses :
if i in domain : domain . remove ( i )
2012-06-06 06:40:57 -07:00
for i in self . prioritized_addresses :
if i in domain : domain . remove ( i )
2012-02-10 04:23:39 -08:00
for addr in domain :
2012-11-03 01:17:40 -07:00
h = self . history . get ( addr , [ ] )
for tx_hash , tx_height , in h :
tx = self . transactions . get ( tx_hash )
for output in tx . get ( ' outputs ' ) :
if output . get ( ' address ' ) != addr : continue
key = tx_hash + " : %d " % output . get ( ' index ' )
if key in self . spent_outputs : continue
output [ ' tx_hash ' ] = tx_hash
coins . append ( output )
#coins = sorted( coins, key = lambda x: x[1]['timestamp'] )
2012-06-06 06:40:57 -07:00
2012-06-06 10:26:05 -07:00
for addr in self . prioritized_addresses :
2012-11-03 01:17:40 -07:00
h = self . history . get ( addr , [ ] )
for tx_hash , tx_height , in h :
for output in tx . get ( ' outputs ' ) :
if output . get ( ' address ' ) != addr : continue
key = tx_hash + " : %d " % output . get ( ' index ' )
if key in self . spent_outputs : continue
output [ ' tx_hash ' ] = tx_hash
prioritized_coins . append ( output )
2012-06-06 06:40:57 -07:00
2012-11-03 01:17:40 -07:00
#prioritized_coins = sorted( prioritized_coins, key = lambda x: x[1]['timestamp'] )
2012-06-06 06:40:57 -07:00
2012-01-19 08:11:36 -08:00
inputs = [ ]
2012-06-06 06:40:57 -07:00
coins = prioritized_coins + coins
2012-11-03 01:17:40 -07:00
for item in coins :
addr = item . get ( ' address ' )
2012-01-19 08:11:36 -08:00
v = item . get ( ' value ' )
total + = v
2012-03-30 01:48:32 -07:00
inputs . append ( ( addr , v , item [ ' tx_hash ' ] , item [ ' index ' ] , item [ ' raw_output_script ' ] , None , None ) )
2012-01-19 08:11:36 -08:00
fee = self . fee * len ( inputs ) if fixed_fee is None else fixed_fee
if total > = amount + fee : break
else :
2012-06-08 04:14:25 -07:00
#print "not enough funds: %s %s"%(format_satoshis(total), format_satoshis(fee))
2012-01-19 08:11:36 -08:00
inputs = [ ]
return inputs , total , fee
2012-02-07 19:22:18 -08:00
def choose_tx_outputs ( self , to_addr , amount , fee , total , change_addr = None ) :
2012-01-19 08:11:36 -08:00
outputs = [ ( to_addr , amount ) ]
change_amount = total - ( amount + fee )
if change_amount != 0 :
# normally, the update thread should ensure that the last change address is unused
2012-02-07 19:22:18 -08:00
if not change_addr :
change_addr = self . change_addresses [ - 1 ]
outputs . append ( ( change_addr , change_amount ) )
2012-01-19 08:11:36 -08:00
return outputs
def sign_inputs ( self , inputs , outputs , password ) :
s_inputs = [ ]
for i in range ( len ( inputs ) ) :
addr , v , p_hash , p_pos , p_scriptPubKey , _ , _ = inputs [ i ]
2012-02-06 09:10:30 -08:00
private_key = ecdsa . SigningKey . from_string ( self . get_private_key ( addr , password ) , curve = SECP256k1 )
2012-01-19 08:11:36 -08:00
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
def pw_encode ( self , s , password ) :
if password :
secret = Hash ( password )
return EncodeAES ( secret , s )
else :
return s
def pw_decode ( self , s , password ) :
if password is not None :
secret = Hash ( password )
d = DecodeAES ( secret , s )
if s == self . seed :
try :
d . decode ( ' hex ' )
except :
2012-08-23 18:11:57 -07:00
raise ValueError ( " Invalid password " )
2012-01-19 08:11:36 -08:00
return d
else :
return s
2012-11-03 01:17:40 -07:00
2012-03-30 05:15:05 -07:00
def get_status ( self , address ) :
2012-10-20 17:57:31 -07:00
with self . lock :
h = self . history . get ( address )
2012-11-03 01:17:40 -07:00
if not h : return None
status = ' '
for tx_hash , height in h :
status + = tx_hash + ' : %d : ' % height
return hashlib . sha256 ( status ) . digest ( ) . encode ( ' hex ' )
2012-03-30 05:15:05 -07:00
2012-03-31 02:47:16 -07:00
2012-11-03 01:17:40 -07:00
def receive_tx_callback ( self , tx_hash , d ) :
2012-03-20 07:30:36 -07:00
#print "updating history for", addr
2012-03-31 02:47:16 -07:00
with self . lock :
2012-11-03 01:17:40 -07:00
self . transactions [ tx_hash ] = d
if self . verifier : self . verifier . add ( tx_hash )
self . update_tx_outputs ( tx_hash )
self . save ( )
def receive_history_callback ( self , addr , hist ) :
#print "updating history for", addr
with self . lock :
self . history [ addr ] = hist
2012-03-31 02:47:16 -07:00
self . save ( )
2012-03-16 14:18:58 -07:00
2012-11-03 01:17:40 -07:00
2012-01-19 08:11:36 -08:00
def get_tx_history ( self ) :
2012-10-20 17:57:31 -07:00
with self . lock :
2012-11-03 01:17:40 -07:00
lines = self . transactions . values ( )
2012-03-30 01:48:32 -07:00
lines = sorted ( lines , key = operator . itemgetter ( " timestamp " ) )
2012-01-19 08:11:36 -08:00
return lines
2012-10-20 17:57:31 -07:00
def get_transactions_at_height ( self , height ) :
with self . lock :
2012-11-03 01:17:40 -07:00
values = self . transactions . values ( ) [ : ]
2012-10-20 17:57:31 -07:00
out = [ ]
for tx in values :
if tx [ ' height ' ] == height :
out . append ( tx [ ' tx_hash ' ] )
return out
2012-01-19 08:11:36 -08:00
2012-11-03 01:17:40 -07:00
def get_default_label ( self , tx_hash ) :
tx = self . transactions . get ( tx_hash )
if tx :
2012-01-19 08:11:36 -08:00
default_label = ' '
2012-11-03 01:17:40 -07:00
if self . get_tx_value ( tx_hash ) < 0 :
for o in tx [ ' outputs ' ] :
o_addr = o . get ( ' address ' )
2012-06-10 02:37:11 -07:00
if not self . is_mine ( o_addr ) :
2012-08-12 13:52:28 -07:00
try :
default_label = self . labels [ o_addr ]
except KeyError :
default_label = o_addr
2012-01-19 08:11:36 -08:00
else :
2012-11-03 01:17:40 -07:00
for o in tx [ ' outputs ' ] :
o_addr = o . get ( ' address ' )
2012-01-19 08:11:36 -08:00
if self . is_mine ( o_addr ) and not self . is_change ( o_addr ) :
2012-05-19 02:01:45 -07:00
break
else :
2012-11-03 01:17:40 -07:00
for o in tx [ ' outputs ' ] :
o_addr = o . get ( ' address ' )
2012-05-19 02:01:45 -07:00
if self . is_mine ( o_addr ) :
break
else :
o_addr = None
if o_addr :
dest_label = self . labels . get ( o_addr )
2012-08-12 13:52:28 -07:00
try :
default_label = self . labels [ o_addr ]
except KeyError :
default_label = o_addr
2012-05-19 02:01:45 -07:00
2012-11-03 01:17:40 -07:00
return default_label
2012-01-19 08:11:36 -08:00
2012-06-17 00:51:15 -07:00
def mktx ( self , to_address , amount , label , password , fee = None , change_addr = None , from_addr = None ) :
2012-01-19 08:11:36 -08:00
if not self . is_valid ( to_address ) :
2012-08-23 18:16:27 -07:00
raise ValueError ( " Invalid address " )
2012-02-10 04:23:39 -08:00
inputs , total , fee = self . choose_tx_inputs ( amount , fee , from_addr )
2012-01-19 08:11:36 -08:00
if not inputs :
2012-08-23 18:16:27 -07:00
raise ValueError ( " Not enough funds " )
2012-06-16 09:45:17 -07:00
2012-06-17 00:51:15 -07:00
if not self . use_change and not change_addr :
2012-06-16 09:45:17 -07:00
change_addr = inputs [ 0 ] [ 0 ]
2012-07-07 06:39:25 -07:00
print " Sending change to " , change_addr
2012-06-16 09:45:17 -07:00
2012-02-07 19:22:18 -08:00
outputs = self . choose_tx_outputs ( to_address , amount , fee , total , change_addr )
2012-01-20 08:21:29 -08:00
s_inputs = self . sign_inputs ( inputs , outputs , password )
2012-01-19 08:11:36 -08:00
tx = filter ( raw_tx ( s_inputs , outputs ) )
if to_address not in self . addressbook :
self . addressbook . append ( to_address )
if label :
tx_hash = Hash ( tx . decode ( ' hex ' ) ) [ : : - 1 ] . encode ( ' hex ' )
2012-01-20 08:21:29 -08:00
self . labels [ tx_hash ] = label
2012-02-10 04:44:10 -08:00
2012-01-19 08:11:36 -08:00
return tx
def sendtx ( self , tx ) :
2012-10-13 23:25:09 -07:00
# synchronous
h = self . send_tx ( tx )
self . tx_event . wait ( )
self . receive_tx ( h )
def send_tx ( self , tx ) :
# asynchronous
2012-03-24 05:15:23 -07:00
self . tx_event . clear ( )
2012-10-13 23:25:09 -07:00
tx_hash = Hash ( tx . decode ( ' hex ' ) ) [ : : - 1 ] . encode ( ' hex ' )
2012-10-26 07:06:54 -07:00
self . interface . send ( [ ( ' blockchain.transaction.broadcast ' , [ tx ] ) ] , ' synchronizer ' )
2012-10-13 23:25:09 -07:00
return tx_hash
def receive_tx ( self , tx_hash ) :
2012-03-24 05:15:23 -07:00
out = self . tx_result
2012-01-19 08:11:36 -08:00
if out != tx_hash :
return False , " error: " + out
2012-02-03 08:38:43 -08:00
if self . receipt :
self . receipts [ tx_hash ] = self . receipt
self . receipt = None
2012-01-19 08:11:36 -08:00
return True , out
2012-02-06 08:59:31 -08:00
def read_alias ( self , alias ) :
2012-02-03 06:45:41 -08:00
# this might not be the right place for this function.
import urllib
2012-02-06 14:45:21 -08:00
m1 = re . match ( ' ([ \ w \ - \ .]+)@(( \ w[ \ w \ -]+ \ .)+[ \ w \ -]+) ' , alias )
m2 = re . match ( ' (( \ w[ \ w \ -]+ \ .)+[ \ w \ -]+) ' , alias )
if m1 :
2012-10-01 06:10:51 -07:00
url = ' https:// ' + m1 . group ( 2 ) + ' /bitcoin.id/ ' + m1 . group ( 1 )
2012-02-06 14:45:21 -08:00
elif m2 :
2012-10-01 06:10:51 -07:00
url = ' https:// ' + alias + ' /bitcoin.id '
2012-02-03 06:45:41 -08:00
else :
2012-02-06 14:45:21 -08:00
return ' '
try :
lines = urllib . urlopen ( url ) . readlines ( )
except :
return ' '
2012-02-03 07:39:27 -08:00
2012-02-06 14:45:21 -08:00
# line 0
line = lines [ 0 ] . strip ( ) . split ( ' : ' )
if len ( line ) == 1 :
auth_name = None
target = signing_addr = line [ 0 ]
else :
target , auth_name , signing_addr , signature = line
msg = " alias: %s : %s : %s " % ( alias , target , auth_name )
print msg , signature
self . verify_message ( signing_addr , signature , msg )
# other lines are signed updates
for line in lines [ 1 : ] :
line = line . strip ( )
if not line : continue
line = line . split ( ' : ' )
previous = target
print repr ( line )
target , signature = line
self . verify_message ( previous , signature , " alias: %s : %s " % ( alias , target ) )
if not self . is_valid ( target ) :
2012-08-23 18:17:30 -07:00
raise ValueError ( " Invalid bitcoin address " )
2012-02-06 14:45:21 -08:00
return target , signing_addr , auth_name
2012-02-13 05:52:59 -08:00
2012-05-16 23:32:49 -07:00
def update_password ( self , seed , old_password , new_password ) :
2012-04-09 02:38:21 -07:00
if new_password == ' ' : new_password = None
self . use_encryption = ( new_password != None )
2012-02-13 05:52:59 -08:00
self . seed = self . pw_encode ( seed , new_password )
for k in self . imported_keys . keys ( ) :
a = self . imported_keys [ k ]
2012-05-16 23:32:49 -07:00
b = self . pw_decode ( a , old_password )
2012-02-13 05:52:59 -08:00
c = self . pw_encode ( b , new_password )
2012-02-13 05:56:31 -08:00
self . imported_keys [ k ] = c
self . save ( )
2012-02-14 00:52:03 -08:00
def get_alias ( self , alias , interactive = False , show_message = None , question = None ) :
try :
target , signing_address , auth_name = self . read_alias ( alias )
except BaseException , e :
# raise exception if verify fails (verify the chain)
if interactive :
2012-05-16 10:20:21 -07:00
show_message ( " Alias error: " + str ( e ) )
2012-02-14 00:52:03 -08:00
return
print target , signing_address , auth_name
if auth_name is None :
a = self . aliases . get ( alias )
if not a :
msg = " Warning: the alias ' %s ' is self-signed. \n The signing address is %s . \n \n Do you want to add this alias to your list of contacts? " % ( alias , signing_address )
if interactive and question ( msg ) :
self . aliases [ alias ] = ( signing_address , target )
else :
target = None
else :
if signing_address != a [ 0 ] :
msg = " Warning: the key of alias ' %s ' has changed since your last visit! It is possible that someone is trying to do something nasty!!! \n Do you accept to change your trusted key? " % alias
if interactive and question ( msg ) :
self . aliases [ alias ] = ( signing_address , target )
else :
target = None
else :
if signing_address not in self . authorities . keys ( ) :
msg = " The alias: ' %s ' links to %s \n \n Warning: this alias was signed by an unknown key. \n Signing authority: %s \n Signing address: %s \n \n Do you want to add this key to your list of trusted keys? " % ( alias , target , auth_name , signing_address )
if interactive and question ( msg ) :
self . authorities [ signing_address ] = auth_name
else :
target = None
if target :
self . aliases [ alias ] = ( signing_address , target )
return target
2012-02-14 03:45:39 -08:00
2012-02-14 04:49:05 -08:00
def parse_url ( self , url , show_message , question ) :
2012-02-14 03:45:39 -08:00
o = url [ 8 : ] . split ( ' ? ' )
address = o [ 0 ]
if len ( o ) > 1 :
params = o [ 1 ] . split ( ' & ' )
else :
params = [ ]
amount = label = message = signature = identity = ' '
for p in params :
k , v = p . split ( ' = ' )
uv = urldecode ( v )
if k == ' amount ' : amount = uv
elif k == ' message ' : message = uv
elif k == ' label ' : label = uv
elif k == ' signature ' :
identity , signature = uv . split ( ' : ' )
url = url . replace ( ' & %s = %s ' % ( k , v ) , ' ' )
else :
print k , v
2012-06-08 14:15:08 -07:00
if label and self . labels . get ( address ) != label :
if question ( ' Give label " %s " to address %s ? ' % ( label , address ) ) :
if address not in self . addressbook and address not in self . all_addresses ( ) :
self . addressbook . append ( address )
self . labels [ address ] = label
2012-02-14 04:49:05 -08:00
if signature :
if re . match ( ' ^(|([ \ w \ - \ .]+)@)(( \ w[ \ w \ -]+ \ .)+[ \ w \ -]+)$ ' , identity ) :
signing_address = self . get_alias ( identity , True , show_message , question )
elif self . is_valid ( identity ) :
signing_address = identity
else :
signing_address = None
if not signing_address :
return
try :
self . verify_message ( signing_address , signature , url )
self . receipt = ( signing_address , signature , url )
except :
2012-02-14 08:17:59 -08:00
show_message ( ' Warning: the URI contains a bad signature. \n The identity of the recipient cannot be verified. ' )
2012-02-14 04:49:05 -08:00
address = amount = label = identity = message = ' '
if re . match ( ' ^(|([ \ w \ - \ .]+)@)(( \ w[ \ w \ -]+ \ .)+[ \ w \ -]+)$ ' , address ) :
2012-02-14 08:16:28 -08:00
payto_address = self . get_alias ( address , True , show_message , question )
2012-02-14 04:49:05 -08:00
if payto_address :
address = address + ' < ' + payto_address + ' > '
2012-02-14 04:38:47 -08:00
return address , amount , label , message , signature , identity , url
2012-03-23 08:34:34 -07:00
def update ( self ) :
2012-10-21 14:22:46 -07:00
self . interface . poke ( ' synchronizer ' )
2012-05-19 06:52:06 -07:00
self . up_to_date_event . wait ( 10000000000 )
2012-03-23 08:34:34 -07:00
2012-03-23 09:46:48 -07:00
2012-06-07 02:18:11 -07:00
def freeze ( self , addr ) :
if addr in self . all_addresses ( ) and addr not in self . frozen_addresses :
2012-06-07 02:21:53 -07:00
self . unprioritize ( addr )
2012-06-07 02:18:11 -07:00
self . frozen_addresses . append ( addr )
2012-10-12 08:40:37 -07:00
self . config . set_key ( ' frozen_addresses ' , self . frozen_addresses , True )
2012-06-07 02:18:11 -07:00
return True
else :
return False
2012-05-02 05:21:58 -07:00
2012-06-07 02:18:11 -07:00
def unfreeze ( self , addr ) :
if addr in self . all_addresses ( ) and addr in self . frozen_addresses :
self . frozen_addresses . remove ( addr )
2012-10-12 08:40:37 -07:00
self . config . set_key ( ' frozen_addresses ' , self . frozen_addresses , True )
2012-06-07 02:18:11 -07:00
return True
else :
return False
2012-03-30 05:15:05 -07:00
2012-06-07 02:18:11 -07:00
def prioritize ( self , addr ) :
2012-06-07 02:25:23 -07:00
if addr in self . all_addresses ( ) and addr not in self . prioritized_addresses :
self . unfreeze ( addr )
2012-06-07 02:18:11 -07:00
self . prioritized_addresses . append ( addr )
2012-10-12 08:40:37 -07:00
self . config . set_key ( ' prioritized_addresses ' , self . prioritized_addresses , True )
2012-06-07 02:18:11 -07:00
return True
else :
return False
def unprioritize ( self , addr ) :
if addr in self . all_addresses ( ) and addr in self . prioritized_addresses :
self . prioritized_addresses . remove ( addr )
2012-10-12 08:40:37 -07:00
self . config . set_key ( ' prioritized_addresses ' , self . prioritized_addresses , True )
2012-06-07 02:18:11 -07:00
return True
else :
return False
2012-10-11 11:10:12 -07:00
def save ( self ) :
s = {
' seed_version ' : self . seed_version ,
' use_encryption ' : self . use_encryption ,
' use_change ' : self . use_change ,
2012-10-16 00:24:38 -07:00
' master_public_key ' : self . master_public_key ,
2012-10-11 11:10:12 -07:00
' fee ' : self . fee ,
' seed ' : self . seed ,
' addresses ' : self . addresses ,
' change_addresses ' : self . change_addresses ,
2012-11-04 06:47:19 -08:00
' addr_history ' : self . history ,
2012-10-11 11:10:12 -07:00
' labels ' : self . labels ,
' contacts ' : self . addressbook ,
' imported_keys ' : self . imported_keys ,
' aliases ' : self . aliases ,
' authorities ' : self . authorities ,
' receipts ' : self . receipts ,
' num_zeros ' : self . num_zeros ,
' frozen_addresses ' : self . frozen_addresses ,
' prioritized_addresses ' : self . prioritized_addresses ,
' gap_limit ' : self . gap_limit ,
2012-11-03 01:17:40 -07:00
' transactions ' : self . transactions ,
2012-10-11 11:10:12 -07:00
}
for k , v in s . items ( ) :
self . config . set_key ( k , v )
self . config . save ( )
2012-10-20 17:57:31 -07:00
2012-10-26 01:02:09 -07:00
def set_verifier ( self , verifier ) :
self . verifier = verifier
2012-11-03 01:17:40 -07:00
for tx_hash in self . transactions . keys ( ) :
self . verifier . add ( tx_hash )
2012-10-20 17:57:31 -07:00
2012-11-04 06:38:34 -08:00
def set_tx_timestamp ( self , tx_hash , tx_height ) :
if tx_height > 0 :
header = self . verifier . read_header ( tx_height )
timestamp = header . get ( ' timestamp ' )
else :
timestamp = 1e12
with self . lock :
self . transactions [ tx_hash ] [ ' timestamp ' ] = timestamp
2012-10-20 17:57:31 -07:00
class WalletSynchronizer ( threading . Thread ) :
def __init__ ( self , wallet , config ) :
threading . Thread . __init__ ( self )
self . daemon = True
self . wallet = wallet
self . interface = self . wallet . interface
self . interface . register_channel ( ' synchronizer ' )
2012-10-22 05:49:29 -07:00
self . wallet . interface . register_callback ( ' connected ' , self . wallet . init_up_to_date )
2012-10-22 06:01:13 -07:00
self . wallet . interface . register_callback ( ' connected ' , lambda : self . interface . send ( [ ( ' server.banner ' , [ ] ) ] , ' synchronizer ' ) )
2012-10-27 10:20:50 -07:00
self . was_updated = True
2012-10-20 17:57:31 -07:00
def synchronize_wallet ( self ) :
new_addresses = self . wallet . synchronize ( )
if new_addresses :
self . subscribe_to_addresses ( new_addresses )
if self . interface . is_up_to_date ( ' synchronizer ' ) :
if not self . wallet . up_to_date :
self . wallet . up_to_date = True
2012-10-27 10:20:50 -07:00
self . was_updated = True
2012-10-20 17:57:31 -07:00
self . wallet . up_to_date_event . set ( )
else :
if self . wallet . up_to_date :
self . wallet . up_to_date = False
2012-10-27 10:20:50 -07:00
self . was_updated = True
2012-10-20 17:57:31 -07:00
def subscribe_to_addresses ( self , addresses ) :
messages = [ ]
for addr in addresses :
messages . append ( ( ' blockchain.address.subscribe ' , [ addr ] ) )
self . interface . send ( messages , ' synchronizer ' )
def run ( self ) :
2012-11-03 01:17:40 -07:00
requested_tx = [ ]
2012-10-22 08:18:07 -07:00
# wait until we are connected, in case the user is not connected
while not self . interface . is_connected :
time . sleep ( 1 )
# request banner, because 'connected' event happens before this thread is started
2012-10-22 06:01:13 -07:00
self . interface . send ( [ ( ' server.banner ' , [ ] ) ] , ' synchronizer ' )
2012-10-20 17:57:31 -07:00
# subscriptions
self . subscribe_to_addresses ( self . wallet . all_addresses ( ) )
while True :
# 1. send new requests
self . synchronize_wallet ( )
2012-10-27 10:20:50 -07:00
if self . was_updated :
2012-10-22 04:43:58 -07:00
self . interface . trigger_callback ( ' updated ' )
2012-10-27 10:20:50 -07:00
self . was_updated = False
2012-10-22 04:43:58 -07:00
2012-10-20 17:57:31 -07:00
# 2. get a response
r = self . interface . get_response ( ' synchronizer ' )
if not r : continue
# 3. handle response
method = r [ ' method ' ]
params = r [ ' params ' ]
result = r [ ' result ' ]
if method == ' blockchain.address.subscribe ' :
addr = params [ 0 ]
if self . wallet . get_status ( addr ) != result :
2012-10-26 07:06:54 -07:00
self . interface . send ( [ ( ' blockchain.address.get_history ' , [ addr ] ) ] , ' synchronizer ' )
2012-10-20 17:57:31 -07:00
elif method == ' blockchain.address.get_history ' :
addr = params [ 0 ]
2012-11-03 01:17:40 -07:00
hist = [ ]
# in the new protocol, we will receive a list of (tx_hash, height)
2012-11-04 06:38:34 -08:00
for item in result : hist . append ( ( item [ ' tx_hash ' ] , item [ ' height ' ] ) )
2012-11-03 01:17:40 -07:00
# store it
self . wallet . receive_history_callback ( addr , hist )
# request transactions that we don't have
for tx_hash , tx_height in hist :
2012-11-04 06:38:34 -08:00
if self . wallet . transactions . get ( tx_hash ) is None :
if tx_hash not in requested_tx :
self . interface . send ( [ ( ' blockchain.transaction.get ' , [ tx_hash , tx_height ] ) ] , ' synchronizer ' )
requested_tx . append ( tx_hash )
else :
self . wallet . set_tx_timestamp ( tx_hash , tx_height )
2012-11-03 01:17:40 -07:00
elif method == ' blockchain.transaction.get ' :
tx_hash = params [ 0 ]
tx_height = params [ 1 ]
2012-11-04 06:38:34 -08:00
self . receive_tx ( tx_hash , tx_height , result )
self . wallet . set_tx_timestamp ( tx_hash , tx_height )
2012-11-03 01:17:40 -07:00
requested_tx . remove ( tx_hash )
2012-10-27 10:20:50 -07:00
self . was_updated = True
2012-10-20 17:57:31 -07:00
2012-11-03 01:17:40 -07:00
2012-10-20 17:57:31 -07:00
elif method == ' blockchain.transaction.broadcast ' :
self . wallet . tx_result = result
self . wallet . tx_event . set ( )
2012-10-22 02:34:21 -07:00
elif method == ' server.banner ' :
self . wallet . banner = result
2012-10-27 10:20:50 -07:00
self . was_updated = True
2012-10-20 17:57:31 -07:00
else :
2012-10-22 02:34:21 -07:00
print_error ( " Error: Unknown message: " + method + " , " + repr ( params ) + " , " + repr ( result ) )
2012-10-20 17:57:31 -07:00
2012-10-27 10:20:50 -07:00
if self . was_updated :
2012-10-22 04:43:58 -07:00
self . interface . trigger_callback ( ' updated ' )
2012-10-27 10:20:50 -07:00
self . was_updated = False
2012-10-22 04:43:58 -07:00
2012-10-20 17:57:31 -07:00
2012-11-04 06:38:34 -08:00
def receive_tx ( self , tx_hash , tx_height , raw_tx ) :
2012-11-03 01:17:40 -07:00
assert tx_hash == hash_encode ( Hash ( raw_tx . decode ( ' hex ' ) ) )
2012-11-04 06:38:34 -08:00
import deserialize
vds = deserialize . BCDataStream ( )
2012-11-03 01:17:40 -07:00
vds . write ( raw_tx . decode ( ' hex ' ) )
d = deserialize . parse_Transaction ( vds )
d [ ' tx_hash ' ] = tx_hash
self . wallet . receive_tx_callback ( tx_hash , d )