ebellocchia / bip_utils

Generation of mnemonics, seeds, private/public keys and addresses for different types of cryptocurrencies
MIT License
324 stars 86 forks source link

support for Injective #107

Closed Jackson-DKMG closed 1 year ago

Jackson-DKMG commented 1 year ago

Hey,

I've spent quite some time trying to figure this out but in all honesty, it is way above my abilities.

It's not possible to generate the correct Injective addresses (because none of the Bip44Coins types are compatible at the moment).

For example to generate a Cosmos wallet adresse from a mnemonic, the below works:

mnemonic = "24 words here" seed_bytes = Bip39SeedGenerator(mnemonic).Generate() bip44_def_ctx = Bip44.FromSeed(seed_bytes, Bip44Coins.COSMOS).DeriveDefaultPath()

Then using the above with the Cosmpy library to get the expected wallet address.

Do you happen to have a solution for the Injective blockchain, by any chance?

ebellocchia commented 1 year ago

Hi, I have to add it. If it is like the other coins built on Cosmos it shoud be pretty quick.

Regards, Emanuele

Jackson-DKMG commented 1 year ago

Thanks for the quick reply! That would be awesome.

ebellocchia commented 1 year ago

Hello, I added Injective as a coin.

Please note that Injective uses a different algorithm for encoding the address with respect to other Cosmos-based coins, as you can see in the code snippet from their docs. So I added a specific address encoder/decoder just for it. With my implementation, I get the same addresses of Cosmostation wallet, which I always use as reference for Cosmos-based coins.

I don't know if the cosmpy library uses the Injective algorithm or just the standard Cosmos algorithm for encoding the address. I took a quick look and found no trace of it in the code. So, if you get different addresses with respect to cosmpy it's because of that.

Jackson-DKMG commented 1 year ago

Hello,

Great and thanks a lot This works as intended, although indeed when using Cosmpy the result is still wrong:

from cosmpy.aerial.wallet import LocalWallet
from cosmpy.crypto.keypairs import PrivateKey, PublicKey

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

I've ran a number of tests this morning but couldn't find a solution. I'll try some more and open an issue with the Cosmpy team if still no success.

ebellocchia commented 1 year ago

That's because it uses the standard Cosmos algorithm, to get the same result with bip_utils:

print(LocalWallet(PrivateKey(bip44_addr_ctx.PrivateKey().Raw().ToBytes()), prefix="inj"))
# Same address
print(AtomAddrEncoder.EncodeKey(
    bip44_addr_ctx.PublicKey().Bip32Key().KeyObject(),
    hrp="inj"
))
Jackson-DKMG commented 1 year ago

Indeed I'll mention this in the issue.