ebellocchia / bip_utils

Generation of mnemonics, seeds, private/public keys and addresses for different types of cryptocurrencies

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Trying to match NIST256p sign/verif keys

FrankC01 opened this issue · comments

I used a tool from Sui to generate a secp256r1 keypair, the tool generated the keys with.

recovery_phrase = "impulse pipe canoe catalog add weekend adjust since parent cargo item emerge royal surge unknown napkin chalk welcome swamp nest antique cram open current"
derivation_path = "m/74'/784'/0'/0/0"

This original produced the following (base64) keys:

o-sign: w0XBGG6J2L2HbbAAH8XphWIAMHSFB209y5ZOlLUeFuY=
o-verf: A2B6+GyuJK67ePAvxN5j2DutAkc50J1UL1FsCDCLs154

Using ecdsa directly, I am getting the same (as expected) when printing said keys:

print("ecdsa key load")
sign_key = ecdsa.SigningKey.from_string(prv_bytes, ecdsa.NIST256p)
ver_key = sign_key.verifying_key
print(f"e-sign: {base64.b64encode(sign_key.to_string()).decode()}")
print(f"e-verf: {base64.b64encode(ver_key.to_string('compressed')).decode()}")

As expected:

ecdsa key load
e-sign: w0XBGG6J2L2HbbAAH8XphWIAMHSFB209y5ZOlLUeFuY=
e-verf: A2B6+GyuJK67ePAvxN5j2DutAkc50J1UL1FsCDCLs154

However; when trying to recreate from recovery_phrase alone and with derivation path using bip-utils:

    seed_bytes = bip_utils.Bip39SeedGenerator(recovery_phrase).Generate()
    bip32_ctx = bip_utils.Bip32Slip10Nist256p1.FromSeed(seed_bytes)

    print("master key")
    prv_key = bip32_ctx.PrivateKey().Raw().ToBytes()
    sk1 = ecdsa.SigningKey.from_string(prv_key, curve=ecdsa.NIST256p)
    sk1_bytes = sk1.to_string()
    print(f"b-sign: len {len(sk1_bytes)} key: {base64.b64encode(sk1_bytes).decode()}")
    vk1 = sk1.get_verifying_key()
    vk1_bytes = vk1.to_string(encoding="compressed")
    print(f"b-verf: len {len(vk1_bytes)} key: {base64.b64encode(vk1_bytes).decode()}")

    print("master derivation")
    bip32_der_ctx = bip32_ctx.DerivePath(derivation_path)
    prv_key = bip32_der_ctx.PrivateKey().Raw().ToBytes()
    sk1 = ecdsa.SigningKey.from_string(prv_key, curve=ecdsa.NIST256p)
    sk1_bytes = sk1.to_string()
    print(f"b1-sign: len {len(sk1_bytes)} key: {base64.b64encode(sk1_bytes).decode()}")
    vk1 = sk1.get_verifying_key()
    vk1_bytes = vk1.to_string(encoding="compressed")
    print(f"b1-verf: len {len(vk1_bytes)} key: {base64.b64encode(vk1_bytes).decode()}")

I'm getting (either just using the mnemonic seed or seed and path) different keys:

master key
b-sign: len 32 key: 42jAPx+2tmQfqeiKwClEmoMmNUNgrYVLTCMaHDAonQg=
b-verf: len 33 key: A7WwLf3A+h46GDya351bHX6tnlAFdv423Lati4Otecwy
master derivation
b1-sign: len 32 key: IcZ6pGVfEWDYrVSVcjOYpm0kXnHoWgVnoU5XjSVg3y4=
b1-verf: len 33 key: Atax+wDsbOBnb7SUZOvjKFlV8CD7iwKCLMfuIrmhtSeN

Am I doing something wrong here?

I close it since it has passed lot of time and nobody answered.
If someone wants to contribute, feel free to re-open it.