kigawas/python-bip44

why is the wrong generation happening.

Noname400 opened this issue · 2 comments

#!/usr/bin/env python
from coincurve import PrivateKey,PublicKey
from bip44 import Wallet
import time
from bip_utils import Bip39MnemonicGenerator, Bip39SeedGenerator, Bip44, Bip44Coins, Bip44Changes
import hashlib
import base58



def hash160(hex_str):
    sha = hashlib.sha256()
    rip = hashlib.new('ripemd160')
    sha.update(hex_str)
    rip.update( sha.digest() )
    #print ( "key_hash = \t" + rip.hexdigest() )
    return rip.hexdigest()  # .hexdigest() is hex ASCII

def chk_pub(ppub):
    if (compress_pubkey):
        if (ord(bytearray.fromhex(ppub[-2:])) % 2 == 0):
            pubkey_compressed = '02'
        else:
            pubkey_compressed = '03'
        pubkey_compressed += ppub[2:66]
        hex_str = bytearray.fromhex(pubkey_compressed)
    else:
        hex_str = bytearray.fromhex(ppub)
    return hex_str

mnemonic = "purity tunnel grid error scout long fruit false embody caught skin gate"
compress_pubkey = True
st = time.time()
for i in range(5):
    w = Wallet(mnemonic,language = "english", passphrase = "")
    pk = w.derive_public_key("m/44'/0'/0'/0/5")
    pk_64bytes = PublicKey(pk).format(True)#[1:]
    print(pk_64bytes.hex())
    hex_str = chk_pub(pk_64bytes.hex())
    key_hash = '00' + hash160(hex_str)
    sha = hashlib.sha256()
    sha.update( bytearray.fromhex(key_hash) )
    checksum = sha.digest()
    sha = hashlib.sha256()
    sha.update(checksum)
    checksum = sha.hexdigest()[0:8]
    addr = base58.b58encode( bytes(bytearray.fromhex(key_hash + checksum))).decode('utf-8')
    print(mnemonic)
    print(addr)
print(time.time()-st)

https://iancoleman.io/bip39/

m/44'/0'/0'/0/0 = 16vWDpbE4aAi1WBm2BtntoBNvic9VATFnQ = True
m/44'/0'/0'/0/1 = 1NWtsHo92FcpxjSKiRNF2BreGy2JaMfWR2 = False
m/44'/0'/0'/0/2 = 1Dp3gA5KEJaUMguAVkXSbKu4Es1AiEEtNY = False
m/44'/0'/0'/0/2 = 1BYG8H6tuf3hBdDvuzMi8qFNWuX4i5GKwS = True

why it can be. ?

Sorry I didn't understand. What's your expected output?

Excuse for troubling.
it is my fault and my code is not correct.
Thanks for your work. everything works perfectly.
how can you still speed up the code?