4.3 C
New York
Sunday, April 13, 2025

I’m making an attempt to recreate pre-V2 electrum addresses from mnemonics, nonetheless, I cannot appear to get the deal with to be appropriate. Any perception?


I’m making a python program to recreate pockets addresses from mnemonics, which sounds easy, however I cannot get the addresses for Outdated Electrum (pre-v2) to match the addresses I get from the official GUI pockets.

I cannot appear to search out an excessive amount of details about Electrum deal with era previous to V2, so any info helps.

Would anybody have any perception as to what I’m doing unsuitable?

import hashlib, ecdsa, base58

def load_wordlist(filename):
    with open(filename, 'r') as f:
        return [word.strip() for word in f.readlines()]

# Decoding mnemonic into Electrum v1 seed (entropy)
def mnemonic_to_entropy(mnemonic, wordlist):
    phrases = mnemonic.cut up()
    entropy_bits=""
    for phrase in phrases:
        index = wordlist.index(phrase)
        entropy_bits += bin(index)[2:].zfill(11)

    # Pading entropy_bits with zeros to make it byte-aligned
    extra_bits = len(entropy_bits) % 8
    if extra_bits != 0:
        entropy_bits = entropy_bits.ljust(len(entropy_bits) + (8 - extra_bits), '0')

    entropy_hex = hex(int(entropy_bits, 2))[2:].zfill(len(entropy_bits) // 4)
    return bytes.fromhex(entropy_hex)

# Producing personal key from entropy + index
def electrum_v1_privkey(entropy, index):
    information = entropy + index.to_bytes(4, 'little')
    return hashlib.sha256(information).digest()

# Changing personal key to compressed public key
def privkey_to_pubkey(privkey):
    sk = ecdsa.SigningKey.from_string(privkey, curve=ecdsa.SECP256k1)
    vk = sk.verifying_key
    prefix = b'x02' if vk.to_string()[-1] % 2 == 0 else b'x03'
    return prefix + vk.to_string()[:32]

# Changing public key to deal with
def pubkey_to_address(pubkey):
    sha256_pubkey = hashlib.sha256(pubkey).digest()
    ripemd160_pubkey = hashlib.new('ripemd160', sha256_pubkey).digest()
    prefixed_pubkey = b'x00' + ripemd160_pubkey
    checksum = hashlib.sha256(hashlib.sha256(prefixed_pubkey).digest()).digest()[:4]
    return base58.b58encode(prefixed_pubkey + checksum).decode()

mnemonic = "pattern mnemonic"
wordlist = load_wordlist('old_electrum_wordlist')

entropy = mnemonic_to_entropy(mnemonic, wordlist)

# Producing first 5 addresses
for index in vary(5):
    privkey = electrum_v1_privkey(entropy, index)
    pubkey = privkey_to_pubkey(privkey)
    deal with = pubkey_to_address(pubkey)
    print(f'Deal with {index}: {deal with}')

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles