fetchai / fetchd

Source code for Fetch.ai blockchain nodes
Other
96 stars 40 forks source link

Bug report: Unable to generate the expected addresses for Injective #332

Closed Jackson-DKMG closed 1 year ago

Jackson-DKMG commented 1 year ago

Prerequisites

Expected Behavior

Injective support was added by the developer of bip_utils, which now allows to generate the expected addresses from a mnemonic.

from cosmpy.aerial.wallet import LocalWallet
from cosmpy.crypto.keypairs import PrivateKey, PublicKey
from bip_utils import Bip39SeedGenerator, Bip44, Bip44Coins

mnemonic = "24 WORDS HERE"
seed_bytes = Bip39SeedGenerator(mnemonic).Generate()

bip44_mst_ctx = Bip44.FromSeed(seed_bytes, Bip44Coins.INJECTIVE)
bip44_acc_ctx = bip44_mst_ctx.Purpose().Coin().Account(0)
bip44_chg_ctx = bip44_acc_ctx.Change(Bip44Changes.CHAIN_EXT)

bip44_addr_ctx = bip44_chg_ctx.AddressIndex(0)   #index 0 appears to be the right one here

print(bip44_addr_ctx.PublicKey().ToAddress())  # --> CORRECT ADDRESS

print(LocalWallet(PrivateKey(bip44_addr_ctx.PrivateKey().Raw().ToBytes()), prefix='inj')) #--> WRONG ADDRESS

The above is expected to print the same address twice.

Current Behavior

Using LocalWallet the generated address is incorrect. This seems to be related to the fact that Cosmpy uses the Atom address encoder.

Running this code generates the same address than LocalWallet:

from bip_utils import AtomAddrEncoder

print(AtomAddrEncoder.EncodeKey(
    bip44_addr_ctx.PublicKey().Bip32Key().KeyObject(),
    hrp="inj"
))

To Reproduce

No response

Context

Tested on Ubuntu 23.04 with python3.11 and latest Fetchd and bip_utils.

Do you know of a workaround to this issue?

Failure Logs

No response

Jackson-DKMG commented 1 year ago

Oops sorry, I meant to post that in the Cosmpy repo. Closing.