I am trying to understand Electrum and working on a project that will use Electrum, but I have a problem understandint it's key generation.
I have figured out that the SHA-512 hash of the normalized mnemonic starts with 1 as an encoding for normal wallets. I will only work with normal wallets though.
So the
version.py has a constant:
SEED_PREFIX = '01' # Electrum standard wallet
That is called almost every time to check the version of the seed. So far so good.
But then you have in
bitcoin.py a code snippet like this:
hash_encode = lambda x: x[::-1].encode('hex')
hash_decode = lambda x: x.decode('hex')[::-1]
hmac_sha_512 = lambda x,y: hmac.new(x, y, hashlib.sha512).digest()
def is_new_seed(x, prefix=version.SEED_PREFIX):
import mnemonic
x = mnemonic.normalize_text(x)
s = hmac_sha_512("Seed version", x.encode('utf8')).encode('hex')
return s.startswith(prefix)
I am trying to figure out what this does, step by step.
1) For example the "hash_decode and hash_encode" structure which is never used again.
Is that what "x.encode" calls?
What is the function of these 2 lines?:
hash_encode = lambda x: x[::-1].encode('hex')
hash_decode = lambda x: x.decode('hex')[::-1]
2) I also don't understand what the "Seed version" phrase does exactly, does it append that to the encoded mnemonic?
3) Then it also checks for the start character of "s", but then what is the point of adding the "Seed version" into the hash itself?
It's a pretty complicated structure, somebody could please explain.