From 9218175301288af8ac468255312628824098b3ad Mon Sep 17 00:00:00 2001 From: ThomasV Date: Wed, 29 Jan 2014 17:12:08 +0100 Subject: [PATCH] enable bip32 --- lib/bitcoin.py | 2 ++ lib/version.py | 3 +-- lib/wallet.py | 45 +++++++++++++++++++-------------------------- 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/lib/bitcoin.py b/lib/bitcoin.py index 3ad2713d3..703f55683 100644 --- a/lib/bitcoin.py +++ b/lib/bitcoin.py @@ -61,6 +61,8 @@ hash_decode = lambda x: x.decode('hex')[::-1] hmac_sha_512 = lambda x,y: hmac.new(x, y, hashlib.sha512).digest() mnemonic_hash = lambda x: hmac_sha_512("Bitcoin mnemonic", x).encode('hex') +from version import SEED_PREFIX +is_seed = lambda x: hmac_sha_512("Seed version", x).encode('hex')[0:2].startswith(SEED_PREFIX) # pywallet openssl private key implementation diff --git a/lib/version.py b/lib/version.py index bb49d262c..6c01df87d 100644 --- a/lib/version.py +++ b/lib/version.py @@ -1,5 +1,4 @@ ELECTRUM_VERSION = "2.0" # version of the client package PROTOCOL_VERSION = '0.9' # protocol version requested -SEED_VERSION = 4 # bump this every time the seed generation is modified +SEED_VERSION = 6 # bump this every time the seed generation is modified SEED_PREFIX = '01' # the hash of the mnemonic seed must begin with this - diff --git a/lib/wallet.py b/lib/wallet.py index e8fbc3737..9a2d22004 100644 --- a/lib/wallet.py +++ b/lib/wallet.py @@ -289,8 +289,8 @@ class Wallet: # we keep only 13 words, that's approximately 139 bits of entropy words = mnemonic.mn_encode(s)[0:13] seed = ' '.join(words) - if mnemonic_hash(seed).startswith(SEED_PREFIX): - break # this removes 12 bits of entropy + if is_seed(seed): + break # this will remove 8 bits of entropy nonce += 1 return seed @@ -303,15 +303,10 @@ class Wallet: raise Exception("a seed exists") if not seed: - self.seed = random_seed(128) - self.seed_version = 4 + self.seed = self.make_seed() + self.seed_version = SEED_VERSION return - #if not seed: - # self.seed = self.make_seed() - # self.seed_version = SEED_VERSION - # return - # find out what kind of wallet we are try: seed.strip().decode('hex') @@ -322,22 +317,19 @@ class Wallet: pass words = seed.split() - self.seed_version = 4 - self.seed = mnemonic.mn_decode(words) + try: + mnemonic.mn_decode(words) + uses_electrum_words = True + except Exception: + uses_electrum_words = False - #try: - # mnemonic.mn_decode(words) - # uses_electrum_words = True - #except Exception: - # uses_electrum_words = False - # - #if uses_electrum_words and len(words) != 13: - # self.seed_version = 4 - # self.seed = mnemonic.mn_decode(words) - #else: - # assert mnemonic_hash(seed).startswith(SEED_PREFIX) - # self.seed_version = SEED_VERSION - # self.seed = seed + if uses_electrum_words and len(words) != 13: + self.seed_version = 4 + self.seed = mnemonic.mn_decode(words) + else: + #assert is_seed(seed) + self.seed_version = SEED_VERSION + self.seed = seed def save_seed(self, password): @@ -433,9 +425,10 @@ class Wallet: def deseed_branch(self, k): # check that parent has no seed - assert self.seed == '' - self.master_private_keys.pop(k) + # assert self.seed == '' + k = self.master_private_keys.pop(k) self.storage.put('master_private_keys', self.master_private_keys, True) + return k def is_watching_only(self): return (self.seed == '') and (self.master_private_keys == {})