Code Monkey home page Code Monkey logo

Comments (5)

kovalevvlad avatar kovalevvlad commented on July 29, 2024 4

OK after reading the source of a few libraries I have figured this out. For unknown to me reason comspy and Injective use different hashing strategies for address derivation. Here is a reproduction of this issue:

import sha3
from bip_utils import Hash160
from pyinjective import PrivateKey as InjectivePrivateKey

from cosmpy.crypto.address import Address as CosmpyAddress
from cosmpy.crypto.keypairs import PrivateKey as CosmpyPrivateKey


def via_injective(private_key_hex: str) -> str:
    priv_key = InjectivePrivateKey.from_hex(private_key_hex)
    pub_key = priv_key.to_public_key()
    return pub_key.to_address().to_acc_bech32()


def via_cosmpy(private_key_hex: str) -> str:
    private_key = CosmpyPrivateKey(bytes.fromhex(private_key_hex))
    public_key = private_key.public_key
    return str(CosmpyAddress(public_key, prefix="inj"))


def keccak256(pubkey: bytes) -> bytes:
    keccak_hash = sha3.keccak_256()
    keccak_hash.update(pubkey[1:])
    return keccak_hash.digest()[12:]


def via_bip_utils(private_key_hex: str, hrp="inj", use_keccak=False) -> str:
    from bip_utils import Secp256k1PrivateKey, Bech32Encoder
    import binascii

    # Decode the hex string to bytes
    private_key_bytes = binascii.unhexlify(private_key_hex)

    # Create a private key object
    private_key = Secp256k1PrivateKey.FromBytes(private_key_bytes)

    # Get the public key from the private key
    public_key = private_key.PublicKey()

    # Hash the public key using SHA256, then RIPEMD-160 or keccak256
    pub_key_hash = keccak256(public_key.RawUncompressed().ToBytes()) if use_keccak else Hash160.QuickDigest(public_key.RawCompressed().ToBytes())

    # Convert the hash of the public key to an address using bech32 encoding
    return Bech32Encoder.Encode(hrp, pub_key_hash)


private_key = "1112f3b9df973c25c5423f9c1ed5eb46b64059ba28a1b5870c525c816f9dbb52"
print("Injective lib", via_injective(private_key))
print("compy", via_cosmpy(private_key))
print("manual via keccak256", via_bip_utils(private_key, use_keccak=True))
print("manual via ripemd160", via_bip_utils(private_key, use_keccak=False))

Output is:

Injective lib inj1ftqt0zhaa7dvnx77q8y5ls2ups5khdx7wk8n4l
compy inj1pkczxghy5ms2fy3cqtty0l6kxqg4wa9eetwpx2
manual via keccak256 inj1ftqt0zhaa7dvnx77q8y5ls2ups5khdx7wk8n4l
manual via ripemd160 inj1pkczxghy5ms2fy3cqtty0l6kxqg4wa9eetwpx2

Would love to hear somebody's opinion on why this might be the case!

from cosmpy.

Jackson-DKMG avatar Jackson-DKMG commented on July 29, 2024 1

No, I ended up using py-injective for this particular wallet, cosmpy for the other ones.

from cosmpy.

TheUnity avatar TheUnity commented on July 29, 2024

Hi. Did you solve it?

from cosmpy.

kovalevvlad avatar kovalevvlad commented on July 29, 2024

same issue here

from cosmpy.

matomoniwano avatar matomoniwano commented on July 29, 2024

yes I noticed the same thing here

from cosmpy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.