|
|
@ -17,24 +17,21 @@ |
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
|
|
|
|
|
|
import sys |
|
|
|
import base64 |
|
|
|
import os |
|
|
|
import re |
|
|
|
import hashlib |
|
|
|
import copy |
|
|
|
import operator |
|
|
|
import ast |
|
|
|
import threading |
|
|
|
import random |
|
|
|
import aes |
|
|
|
import Queue |
|
|
|
import time |
|
|
|
import math |
|
|
|
|
|
|
|
from util import print_msg, print_error, format_satoshis |
|
|
|
from util import print_msg, print_error |
|
|
|
|
|
|
|
from bitcoin import * |
|
|
|
from account import * |
|
|
|
from transaction import Transaction, is_extended_pubkey |
|
|
|
from version import * |
|
|
|
|
|
|
|
from transaction import Transaction |
|
|
|
from plugins import run_hook |
|
|
|
import bitcoin |
|
|
|
from synchronizer import WalletSynchronizer |
|
|
@ -47,9 +44,6 @@ IMPORTED_ACCOUNT = '/x' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from version import * |
|
|
|
|
|
|
|
|
|
|
|
class WalletStorage: |
|
|
|
|
|
|
|
def __init__(self, config): |
|
|
@ -133,15 +127,9 @@ class WalletStorage: |
|
|
|
os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Abstract_Wallet: |
|
|
|
|
|
|
|
def __init__(self, storage): |
|
|
|
|
|
|
|
self.storage = storage |
|
|
|
self.electrum_version = ELECTRUM_VERSION |
|
|
|
self.gap_limit_for_change = 3 # constant |
|
|
@ -203,11 +191,9 @@ class Abstract_Wallet: |
|
|
|
self.lock = threading.Lock() |
|
|
|
self.transaction_lock = threading.Lock() |
|
|
|
self.tx_event = threading.Event() |
|
|
|
|
|
|
|
for tx_hash, tx in self.transactions.items(): |
|
|
|
self.update_tx_outputs(tx_hash) |
|
|
|
|
|
|
|
|
|
|
|
def add_extra_addresses(self, tx): |
|
|
|
h = tx.hash() |
|
|
|
# find the address corresponding to pay-to-pubkey inputs |
|
|
@ -217,11 +203,9 @@ class Abstract_Wallet: |
|
|
|
for tx2 in self.transactions.values(): |
|
|
|
tx2.add_extra_addresses({h:tx}) |
|
|
|
|
|
|
|
|
|
|
|
def get_action(self): |
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
def convert_imported_keys(self, password): |
|
|
|
for k, v in self.imported_keys.items(): |
|
|
|
sec = pw_decode(v, password) |
|
|
@ -232,7 +216,6 @@ class Abstract_Wallet: |
|
|
|
self.imported_keys.pop(k) |
|
|
|
self.storage.put('imported_keys', self.imported_keys) |
|
|
|
|
|
|
|
|
|
|
|
def load_accounts(self): |
|
|
|
self.accounts = {} |
|
|
|
self.imported_keys = self.storage.get('imported_keys',{}) |
|
|
@ -255,7 +238,6 @@ class Abstract_Wallet: |
|
|
|
else: |
|
|
|
print_error("cannot load account", v) |
|
|
|
|
|
|
|
|
|
|
|
def synchronize(self): |
|
|
|
pass |
|
|
|
|
|
|
@ -268,7 +250,6 @@ class Abstract_Wallet: |
|
|
|
def is_up_to_date(self): |
|
|
|
with self.lock: return self.up_to_date |
|
|
|
|
|
|
|
|
|
|
|
def update(self): |
|
|
|
self.up_to_date = False |
|
|
|
while not self.is_up_to_date(): |
|
|
@ -304,7 +285,6 @@ class Abstract_Wallet: |
|
|
|
self.synchronizer.subscribe_to_addresses([address]) |
|
|
|
return address |
|
|
|
|
|
|
|
|
|
|
|
def delete_imported_key(self, addr): |
|
|
|
account = self.accounts[IMPORTED_ACCOUNT] |
|
|
|
account.remove(addr) |
|
|
@ -312,7 +292,6 @@ class Abstract_Wallet: |
|
|
|
self.accounts.pop(IMPORTED_ACCOUNT) |
|
|
|
self.save_accounts() |
|
|
|
|
|
|
|
|
|
|
|
def set_label(self, name, text = None): |
|
|
|
changed = False |
|
|
|
old_text = self.labels.get(name) |
|
|
@ -331,9 +310,6 @@ class Abstract_Wallet: |
|
|
|
run_hook('set_label', name, text, changed) |
|
|
|
return changed |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def addresses(self, include_change = True, _next=True): |
|
|
|
o = [] |
|
|
|
for a in self.accounts.keys(): |
|
|
@ -345,18 +321,15 @@ class Abstract_Wallet: |
|
|
|
o += [addr] |
|
|
|
return o |
|
|
|
|
|
|
|
|
|
|
|
def is_mine(self, address): |
|
|
|
return address in self.addresses(True) |
|
|
|
|
|
|
|
|
|
|
|
def is_change(self, address): |
|
|
|
if not self.is_mine(address): return False |
|
|
|
acct, s = self.get_address_index(address) |
|
|
|
if s is None: return False |
|
|
|
return s[0] == 1 |
|
|
|
|
|
|
|
|
|
|
|
def get_address_index(self, address): |
|
|
|
|
|
|
|
for account in self.accounts.keys(): |
|
|
@ -372,26 +345,22 @@ class Abstract_Wallet: |
|
|
|
|
|
|
|
raise Exception("Address not found", address) |
|
|
|
|
|
|
|
|
|
|
|
def getpubkeys(self, addr): |
|
|
|
assert is_valid(addr) and self.is_mine(addr) |
|
|
|
account, sequence = self.get_address_index(addr) |
|
|
|
a = self.accounts[account] |
|
|
|
return a.get_pubkeys( sequence ) |
|
|
|
|
|
|
|
|
|
|
|
def get_private_key(self, address, password): |
|
|
|
if self.is_watching_only(): |
|
|
|
return [] |
|
|
|
account_id, sequence = self.get_address_index(address) |
|
|
|
return self.accounts[account_id].get_private_key(sequence, self, password) |
|
|
|
|
|
|
|
|
|
|
|
def get_public_keys(self, address): |
|
|
|
account_id, sequence = self.get_address_index(address) |
|
|
|
return self.accounts[account_id].get_pubkeys(sequence) |
|
|
|
|
|
|
|
|
|
|
|
def can_sign(self, tx): |
|
|
|
|
|
|
|
if self.is_watching_only(): |
|
|
@ -412,14 +381,10 @@ class Abstract_Wallet: |
|
|
|
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_keypairs(self, tx, keypairs, password): |
|
|
|
# first check the provided password |
|
|
|
seed = self.get_seed(password) |
|
|
|
|
|
|
|
# first check the provided password. This will raise if invalid. |
|
|
|
self.get_seed(password) |
|
|
|
addr_list, xpub_list = tx.inputs_to_sign() |
|
|
|
|
|
|
|
for addr in addr_list: |
|
|
|
if self.is_mine(addr): |
|
|
|
private_keys = self.get_private_key(address, password) |
|
|
@ -441,12 +406,9 @@ class Abstract_Wallet: |
|
|
|
pubkey = public_key_from_private_key(sec) |
|
|
|
keypairs[pubkey] = sec |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def signrawtransaction(self, tx, private_keys, password): |
|
|
|
|
|
|
|
# check that the password is correct |
|
|
|
seed = self.get_seed(password) |
|
|
|
# check that the password is correct. This will raise if it's not. |
|
|
|
self.get_seed(password) |
|
|
|
|
|
|
|
# build a list of public/private keys |
|
|
|
keypairs = {} |
|
|
@ -462,7 +424,6 @@ class Abstract_Wallet: |
|
|
|
# sign the transaction |
|
|
|
self.sign_transaction(tx, keypairs, password) |
|
|
|
|
|
|
|
|
|
|
|
def sign_message(self, address, message, password): |
|
|
|
keys = self.get_private_key(address, password) |
|
|
|
assert len(keys) == 1 |
|
|
@ -471,8 +432,6 @@ class Abstract_Wallet: |
|
|
|
compressed = is_compressed(sec) |
|
|
|
return key.sign_message(message, compressed, address) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def decrypt_message(self, pubkey, message, password): |
|
|
|
address = public_key_to_bc_address(pubkey.decode('hex')) |
|
|
|
keys = self.get_private_key(address, password) |
|
|
@ -481,25 +440,20 @@ class Abstract_Wallet: |
|
|
|
decrypted = ec.decrypt_message(message) |
|
|
|
return decrypted |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_found(self): |
|
|
|
return self.history.values() != [[]] * len(self.history) |
|
|
|
|
|
|
|
|
|
|
|
def add_contact(self, address, label=None): |
|
|
|
self.addressbook.append(address) |
|
|
|
self.storage.put('contacts', self.addressbook, True) |
|
|
|
if label: |
|
|
|
self.set_label(address, label) |
|
|
|
|
|
|
|
|
|
|
|
def delete_contact(self, addr): |
|
|
|
if addr in self.addressbook: |
|
|
|
self.addressbook.remove(addr) |
|
|
|
self.storage.put('addressbook', self.addressbook, True) |
|
|
|
|
|
|
|
|
|
|
|
def fill_addressbook(self): |
|
|
|
for tx_hash, tx in self.transactions.items(): |
|
|
|
is_relevant, is_send, _, _ = self.get_tx_value(tx) |
|
|
@ -516,18 +470,15 @@ class Abstract_Wallet: |
|
|
|
if address in map(lambda x:x[0], tx.outputs): n += 1 |
|
|
|
return n |
|
|
|
|
|
|
|
|
|
|
|
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 "-" |
|
|
|
return flags |
|
|
|
|
|
|
|
|
|
|
|
def get_tx_value(self, tx, account=None): |
|
|
|
domain = self.get_account_addresses(account) |
|
|
|
return tx.get_value(domain, self.prevout_values) |
|
|
|
|
|
|
|
|
|
|
|
def update_tx_outputs(self, tx_hash): |
|
|
|
tx = self.transactions.get(tx_hash) |
|
|
|
|
|
|
@ -540,7 +491,6 @@ class Abstract_Wallet: |
|
|
|
key = item['prevout_hash'] + ':%d'%item['prevout_n'] |
|
|
|
self.spent_outputs.append(key) |
|
|
|
|
|
|
|
|
|
|
|
def get_addr_balance(self, address): |
|
|
|
#assert self.is_mine(address) |
|
|
|
h = self.history.get(address,[]) |
|
|
@ -581,18 +531,15 @@ class Abstract_Wallet: |
|
|
|
u += v |
|
|
|
return c, u |
|
|
|
|
|
|
|
|
|
|
|
def get_account_name(self, k): |
|
|
|
return self.labels.get(k, self.accounts[k].get_name(k)) |
|
|
|
|
|
|
|
|
|
|
|
def get_account_names(self): |
|
|
|
account_names = {} |
|
|
|
for k in self.accounts.keys(): |
|
|
|
account_names[k] = self.get_account_name(k) |
|
|
|
return account_names |
|
|
|
|
|
|
|
|
|
|
|
def get_account_addresses(self, a, include_change=True): |
|
|
|
if a is None: |
|
|
|
o = self.addresses(True) |
|
|
@ -602,7 +549,6 @@ class Abstract_Wallet: |
|
|
|
if include_change: o += ac.get_addresses(1) |
|
|
|
return o |
|
|
|
|
|
|
|
|
|
|
|
def get_account_balance(self, account): |
|
|
|
return self.get_balance(self.get_account_addresses(account)) |
|
|
|
|
|
|
@ -618,7 +564,6 @@ class Abstract_Wallet: |
|
|
|
uu += u |
|
|
|
return cc, uu |
|
|
|
|
|
|
|
|
|
|
|
def get_unspent_coins(self, domain=None): |
|
|
|
coins = [] |
|
|
|
if domain is None: domain = self.addresses(True) |
|
|
@ -647,7 +592,6 @@ class Abstract_Wallet: |
|
|
|
coins = coins[1:] + [ coins[0] ] |
|
|
|
return [x[1] for x in coins] |
|
|
|
|
|
|
|
|
|
|
|
def choose_tx_inputs( self, amount, fixed_fee, num_outputs, domain = None, coins = None ): |
|
|
|
""" todo: minimize tx size """ |
|
|
|
total = 0 |
|
|
@ -665,7 +609,6 @@ class Abstract_Wallet: |
|
|
|
for item in coins: |
|
|
|
if item.get('coinbase') and item.get('height') + COINBASE_MATURITY > self.network.get_local_height(): |
|
|
|
continue |
|
|
|
addr = item.get('address') |
|
|
|
v = item.get('value') |
|
|
|
total += v |
|
|
|
inputs.append(item) |
|
|
@ -676,7 +619,6 @@ class Abstract_Wallet: |
|
|
|
|
|
|
|
return inputs, total, fee |
|
|
|
|
|
|
|
|
|
|
|
def set_fee(self, fee): |
|
|
|
if self.fee != fee: |
|
|
|
self.fee = fee |
|
|
@ -687,7 +629,6 @@ class Abstract_Wallet: |
|
|
|
fee = self.fee * int(math.ceil(estimated_size/1000.)) |
|
|
|
return fee |
|
|
|
|
|
|
|
|
|
|
|
def add_tx_change( self, inputs, outputs, amount, fee, total, change_addr=None): |
|
|
|
"add change to a transaction" |
|
|
|
change_amount = total - ( amount + fee ) |
|
|
@ -708,12 +649,10 @@ class Abstract_Wallet: |
|
|
|
outputs[posn:posn] = [( change_addr, change_amount)] |
|
|
|
return outputs |
|
|
|
|
|
|
|
|
|
|
|
def get_history(self, address): |
|
|
|
with self.lock: |
|
|
|
return self.history.get(address) |
|
|
|
|
|
|
|
|
|
|
|
def get_status(self, h): |
|
|
|
if not h: return None |
|
|
|
if h == ['*']: return '*' |
|
|
@ -722,7 +661,6 @@ class Abstract_Wallet: |
|
|
|
status += tx_hash + ':%d:' % height |
|
|
|
return hashlib.sha256( status ).digest().encode('hex') |
|
|
|
|
|
|
|
|
|
|
|
def receive_tx_callback(self, tx_hash, tx, tx_height): |
|
|
|
|
|
|
|
with self.transaction_lock: |
|
|
@ -738,7 +676,6 @@ class Abstract_Wallet: |
|
|
|
self.verifier.add(tx_hash, tx_height) |
|
|
|
self.update_tx_outputs(tx_hash) |
|
|
|
|
|
|
|
|
|
|
|
def save_transactions(self): |
|
|
|
tx = {} |
|
|
|
for k,v in self.transactions.items(): |
|
|
@ -760,7 +697,6 @@ class Abstract_Wallet: |
|
|
|
# add it in case it was previously unconfirmed |
|
|
|
if self.verifier: self.verifier.add(tx_hash, tx_height) |
|
|
|
|
|
|
|
|
|
|
|
def get_tx_history(self, account=None): |
|
|
|
if not self.verifier: |
|
|
|
return [] |
|
|
@ -793,14 +729,12 @@ class Abstract_Wallet: |
|
|
|
|
|
|
|
return result |
|
|
|
|
|
|
|
|
|
|
|
def get_label(self, tx_hash): |
|
|
|
label = self.labels.get(tx_hash) |
|
|
|
is_default = (label == '') or (label is None) |
|
|
|
if is_default: label = self.get_default_label(tx_hash) |
|
|
|
return label, is_default |
|
|
|
|
|
|
|
|
|
|
|
def get_default_label(self, tx_hash): |
|
|
|
tx = self.transactions.get(tx_hash) |
|
|
|
default_label = '' |
|
|
@ -831,7 +765,6 @@ class Abstract_Wallet: |
|
|
|
o_addr = None |
|
|
|
|
|
|
|
if o_addr: |
|
|
|
dest_label = self.labels.get(o_addr) |
|
|
|
try: |
|
|
|
default_label = self.labels[o_addr] |
|
|
|
except KeyError: |
|
|
@ -839,7 +772,6 @@ class Abstract_Wallet: |
|
|
|
|
|
|
|
return default_label |
|
|
|
|
|
|
|
|
|
|
|
def make_unsigned_transaction(self, outputs, fee=None, change_addr=None, domain=None, coins=None ): |
|
|
|
for address, x in outputs: |
|
|
|
assert is_valid(address), "Address " + address + " is invalid!" |
|
|
@ -852,7 +784,6 @@ class Abstract_Wallet: |
|
|
|
outputs = self.add_tx_change(inputs, outputs, amount, fee, total, change_addr) |
|
|
|
return Transaction.from_io(inputs, outputs) |
|
|
|
|
|
|
|
|
|
|
|
def mktx(self, outputs, password, fee=None, change_addr=None, domain= None, coins = None ): |
|
|
|
tx = self.make_unsigned_transaction(outputs, fee, change_addr, domain, coins) |
|
|
|
keypairs = {} |
|
|
@ -861,7 +792,6 @@ class Abstract_Wallet: |
|
|
|
self.sign_transaction(tx, keypairs, password) |
|
|
|
return tx |
|
|
|
|
|
|
|
|
|
|
|
def add_input_info(self, txin): |
|
|
|
address = txin['address'] |
|
|
|
account_id, sequence = self.get_address_index(address) |
|
|
@ -878,12 +808,10 @@ class Abstract_Wallet: |
|
|
|
txin['redeemPubkey'] = account.get_pubkey(*sequence) |
|
|
|
txin['num_sig'] = 1 |
|
|
|
|
|
|
|
|
|
|
|
def sign_transaction(self, tx, keypairs, password): |
|
|
|
tx.sign(keypairs) |
|
|
|
run_hook('sign_transaction', tx, password) |
|
|
|
|
|
|
|
|
|
|
|
def sendtx(self, tx): |
|
|
|
# synchronous |
|
|
|
h = self.send_tx(tx) |
|
|
@ -907,7 +835,6 @@ class Abstract_Wallet: |
|
|
|
run_hook('receive_tx', tx, self) |
|
|
|
return True, out |
|
|
|
|
|
|
|
|
|
|
|
def update_password(self, old_password, new_password): |
|
|
|
if new_password == '': |
|
|
|
new_password = None |
|
|
@ -931,7 +858,6 @@ class Abstract_Wallet: |
|
|
|
self.use_encryption = (new_password != None) |
|
|
|
self.storage.put('use_encryption', self.use_encryption,True) |
|
|
|
|
|
|
|
|
|
|
|
def freeze(self,addr): |
|
|
|
if self.is_mine(addr) and addr not in self.frozen_addresses: |
|
|
|
self.frozen_addresses.append(addr) |
|
|
@ -940,7 +866,6 @@ class Abstract_Wallet: |
|
|
|
else: |
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
def unfreeze(self,addr): |
|
|
|
if self.is_mine(addr) and addr in self.frozen_addresses: |
|
|
|
self.frozen_addresses.remove(addr) |
|
|
@ -949,7 +874,6 @@ class Abstract_Wallet: |
|
|
|
else: |
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
def set_verifier(self, verifier): |
|
|
|
self.verifier = verifier |
|
|
|
|
|
|
@ -967,9 +891,7 @@ class Abstract_Wallet: |
|
|
|
if tx_hash not in vr: |
|
|
|
self.transactions.pop(tx_hash) |
|
|
|
|
|
|
|
|
|
|
|
def check_new_history(self, addr, hist): |
|
|
|
|
|
|
|
# check that all tx in hist are relevant |
|
|
|
if hist != ['*']: |
|
|
|
for tx_hash, height in hist: |
|
|
@ -1026,7 +948,6 @@ class Abstract_Wallet: |
|
|
|
|
|
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
def check_new_tx(self, tx_hash, tx): |
|
|
|
# 1 check that tx is referenced in addr_history. |
|
|
|
addresses = [] |
|
|
@ -1046,7 +967,6 @@ class Abstract_Wallet: |
|
|
|
|
|
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
def start_threads(self, network): |
|
|
|
from verifier import TxVerifier |
|
|
|
self.network = network |
|
|
@ -1095,7 +1015,6 @@ class Imported_Wallet(Abstract_Wallet): |
|
|
|
self.accounts[IMPORTED_ACCOUNT] = ImportedAccount({'imported':{}}) |
|
|
|
self.storage.put('wallet_type', 'imported', True) |
|
|
|
|
|
|
|
|
|
|
|
def is_watching_only(self): |
|
|
|
acc = self.accounts[IMPORTED_ACCOUNT] |
|
|
|
n = acc.keypairs.values() |
|
|
@ -1196,7 +1115,6 @@ class Deterministic_Wallet(Abstract_Wallet): |
|
|
|
if n > nmax: nmax = n |
|
|
|
return nmax + 1 |
|
|
|
|
|
|
|
|
|
|
|
def address_is_old(self, address): |
|
|
|
age = -1 |
|
|
|
h = self.history.get(address, []) |
|
|
@ -1211,7 +1129,6 @@ class Deterministic_Wallet(Abstract_Wallet): |
|
|
|
age = tx_age |
|
|
|
return age > 2 |
|
|
|
|
|
|
|
|
|
|
|
def synchronize_sequence(self, account, for_change): |
|
|
|
limit = self.gap_limit_for_change if for_change else self.gap_limit |
|
|
|
new_addresses = [] |
|
|
@ -1232,7 +1149,6 @@ class Deterministic_Wallet(Abstract_Wallet): |
|
|
|
|
|
|
|
return new_addresses |
|
|
|
|
|
|
|
|
|
|
|
def check_pending_accounts(self): |
|
|
|
for account_id, addr in self.next_addresses.items(): |
|
|
|
if self.address_is_old(addr): |
|
|
@ -1242,14 +1158,12 @@ class Deterministic_Wallet(Abstract_Wallet): |
|
|
|
self.add_account(account_id, account) |
|
|
|
self.next_addresses.pop(account_id) |
|
|
|
|
|
|
|
|
|
|
|
def synchronize_account(self, account): |
|
|
|
new = [] |
|
|
|
new += self.synchronize_sequence(account, 0) |
|
|
|
new += self.synchronize_sequence(account, 1) |
|
|
|
return new |
|
|
|
|
|
|
|
|
|
|
|
def synchronize(self): |
|
|
|
self.check_pending_accounts() |
|
|
|
new = [] |
|
|
@ -1262,7 +1176,6 @@ class Deterministic_Wallet(Abstract_Wallet): |
|
|
|
self.storage.put('addr_history', self.history, True) |
|
|
|
return new |
|
|
|
|
|
|
|
|
|
|
|
def restore(self, callback): |
|
|
|
from i18n import _ |
|
|
|
def wait_for_wallet(): |
|
|
@ -1290,10 +1203,8 @@ class Deterministic_Wallet(Abstract_Wallet): |
|
|
|
wait_for_wallet() |
|
|
|
else: |
|
|
|
self.synchronize() |
|
|
|
|
|
|
|
self.fill_addressbook() |
|
|
|
|
|
|
|
|
|
|
|
def create_account(self, name, password): |
|
|
|
i = self.num_accounts() |
|
|
|
account_id = self.account_id(i) |
|
|
@ -1310,8 +1221,6 @@ class Deterministic_Wallet(Abstract_Wallet): |
|
|
|
self.accounts[account_id] = account |
|
|
|
self.save_accounts() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def account_is_pending(self, k): |
|
|
|
return type(self.accounts.get(k)) == PendingAccount |
|
|
|
|
|
|
@ -1327,8 +1236,6 @@ class Deterministic_Wallet(Abstract_Wallet): |
|
|
|
self.save_accounts() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NewWallet(Deterministic_Wallet): |
|
|
|
|
|
|
|
def __init__(self, storage): |
|
|
@ -1365,22 +1272,19 @@ class NewWallet(Deterministic_Wallet): |
|
|
|
account = BIP32_Account({'xpub':xpub}) |
|
|
|
self.add_account("m/", account) |
|
|
|
|
|
|
|
|
|
|
|
def create_accounts(self, password): |
|
|
|
seed = pw_decode(self.seed, password) |
|
|
|
# First check the password is valid (this raises if it isn't). |
|
|
|
pw_decode(self.seed, password) |
|
|
|
self.create_account('Main account', password) |
|
|
|
|
|
|
|
|
|
|
|
def add_master_public_key(self, name, mpk): |
|
|
|
self.master_public_keys[name] = mpk |
|
|
|
self.storage.put('master_public_keys', self.master_public_keys, True) |
|
|
|
|
|
|
|
|
|
|
|
def add_master_private_key(self, name, xpriv, password): |
|
|
|
self.master_private_keys[name] = pw_encode(xpriv, password) |
|
|
|
self.storage.put('master_private_keys', self.master_private_keys, True) |
|
|
|
|
|
|
|
|
|
|
|
def add_master_keys(self, root, account_id, password): |
|
|
|
x = self.master_private_keys.get(root) |
|
|
|
if x: |
|
|
@ -1394,20 +1298,17 @@ class NewWallet(Deterministic_Wallet): |
|
|
|
self.add_master_public_key(account_id, xpub) |
|
|
|
return xpub |
|
|
|
|
|
|
|
|
|
|
|
def create_master_keys(self, password): |
|
|
|
xpriv, xpub = bip32_root(mnemonic_to_seed(self.get_seed(password),'').encode('hex')) |
|
|
|
self.add_master_public_key("m/", xpub) |
|
|
|
self.add_master_private_key("m/", xpriv, password) |
|
|
|
|
|
|
|
|
|
|
|
def find_root_by_master_key(self, xpub): |
|
|
|
for key, xpub2 in self.master_public_keys.items(): |
|
|
|
if key == "m/":continue |
|
|
|
if xpub == xpub2: |
|
|
|
return key |
|
|
|
|
|
|
|
|
|
|
|
def num_accounts(self): |
|
|
|
keys = [] |
|
|
|
for k, v in self.accounts.items(): |
|
|
@ -1422,7 +1323,6 @@ class NewWallet(Deterministic_Wallet): |
|
|
|
i += 1 |
|
|
|
return i |
|
|
|
|
|
|
|
|
|
|
|
def next_account_address(self, password): |
|
|
|
i = self.num_accounts() |
|
|
|
account_id = self.account_id(i) |
|
|
@ -1445,7 +1345,6 @@ class NewWallet(Deterministic_Wallet): |
|
|
|
account = BIP32_Account({'xpub':xpub}) |
|
|
|
return account |
|
|
|
|
|
|
|
|
|
|
|
def make_seed(self): |
|
|
|
import mnemonic, ecdsa |
|
|
|
entropy = ecdsa.util.randrange( pow(2,160) ) |
|
|
@ -1466,8 +1365,8 @@ class NewWallet(Deterministic_Wallet): |
|
|
|
return NEW_SEED_VERSION, unicodedata.normalize('NFC', unicode(seed.strip())) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Wallet_2of2(NewWallet): |
|
|
|
""" This class is used for multisignature addresses""" |
|
|
|
|
|
|
|
def __init__(self, storage): |
|
|
|
NewWallet.__init__(self, storage) |
|
|
@ -1499,8 +1398,8 @@ class Wallet_2of2(NewWallet): |
|
|
|
return 'create_2of2_2' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Wallet_2of3(Wallet_2of2): |
|
|
|
""" This class is used for multisignature addresses""" |
|
|
|
|
|
|
|
def __init__(self, storage): |
|
|
|
Wallet_2of2.__init__(self, storage) |
|
|
@ -1532,9 +1431,6 @@ class Wallet_2of3(Wallet_2of2): |
|
|
|
return 'create_2of3_2' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OldWallet(Deterministic_Wallet): |
|
|
|
|
|
|
|
def make_seed(self): |
|
|
@ -1560,7 +1456,6 @@ class OldWallet(Deterministic_Wallet): |
|
|
|
|
|
|
|
return OLD_SEED_VERSION, seed |
|
|
|
|
|
|
|
|
|
|
|
def create_master_keys(self, password): |
|
|
|
seed = self.get_seed(password) |
|
|
|
mpk = OldAccount.mpk_from_seed(seed) |
|
|
@ -1605,6 +1500,9 @@ class OldWallet(Deterministic_Wallet): |
|
|
|
|
|
|
|
# former WalletFactory |
|
|
|
class Wallet(object): |
|
|
|
"""The main wallet "entry point". |
|
|
|
This class is actually a factory that will return a wallet of the correct |
|
|
|
type when passed a WalletStorage instance.""" |
|
|
|
|
|
|
|
def __new__(self, storage): |
|
|
|
config = storage.config |
|
|
@ -1623,7 +1521,6 @@ class Wallet(object): |
|
|
|
if storage.get('wallet_type') == 'imported': |
|
|
|
return Imported_Wallet(storage) |
|
|
|
|
|
|
|
|
|
|
|
if not storage.file_exists: |
|
|
|
seed_version = NEW_SEED_VERSION if config.get('bip32') is True else OLD_SEED_VERSION |
|
|
|
else: |
|
|
@ -1642,8 +1539,6 @@ class Wallet(object): |
|
|
|
print msg |
|
|
|
sys.exit(1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod |
|
|
|
def is_seed(self, seed): |
|
|
|
if not seed: |
|
|
@ -1716,7 +1611,6 @@ class Wallet(object): |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def from_mpk(self, mpk, storage): |
|
|
|
|
|
|
|
try: |
|
|
|
int(mpk, 16) |
|
|
|
old = True |
|
|
|