Giter Site home page Giter Site logo

Comments (3)

ebellocchia avatar ebellocchia commented on August 15, 2024 1

Hi,
I took a look at the source code of solana-cli. Basically, it is working quite differently to other wallets (that's why it's generating different addresses) because it's not an HD-wallet (i.e. it's not deriving any children key, but only generating a master key).
So, what solana-cli does is:

  • Generate a standard BIP39 seed
  • Take the first 32-byte of this seed and use it directly as a private key (it's not computing the private key using HMAC-SHA512 like in BIP32, so the FromSeed method is not suitable for this, but it's just considering the bytes as the private key)
  • Get the public key from the private key and compute the address

So, to replicate this behavior FromPrivateKey method shall be used for construction, by taking only the first 32-byte of the seed, e.g.:

mnemonic = "long mango angle near comic pave useful mandate loop language quantum cruise"
seed_bytes = Bip39SeedGenerator(mnemonic).Generate()
bip44_ctx = Bip44.FromPrivateKey(seed_bytes[:32], Bip44Coins.SOLANA)
# Same address of the one printed by solana-cli
print(bip44_ctx.PublicKey().ToAddress())
# Print master key, no need to derive any children key
print(bip44_ctx.PublicKey().RawCompressed().ToHex())
print(bip44_ctx.PrivateKey().Raw().ToHex())

Moreover, the data it saves in the JSON file is not the seed bytes but the key pair, so 32-byte of private key and 32-byte of public key:

priv_key_bytes = bip44_ctx.PrivateKey().Raw().ToBytes()
pub_key_bytes = bip44_ctx.PublicKey().RawCompressed().ToBytes()[1:]
key_pair = priv_key_bytes + pub_key_bytes
print(list(key_pair))

These are the same bytes you'll find in the json file.

from bip_utils.

ebellocchia avatar ebellocchia commented on August 15, 2024

Hi,
I imported the same mnemonic in sollet.io and TrustWallet and I got the same address 5cHj95qPtR4Xwo8wBkX3b129D8PApqn64LUh5XMVYFo6 (and there is no XhYnQXtkTdbycmGvaJ7xBaSzKMpYzLPm7mn36PkRhQ3 address), so the computed address and keys are correct.
I'm not very familiar with solana-cli and I don't know exactly what the verify command is doing, maybe it's a problem of data format. I'll try to play around with it.

from bip_utils.

aleixisp avatar aleixisp commented on August 15, 2024

Amazing, I was about to give you a brief on how solana-cli works, since I just started using bip_utils and i don't know how all methods/classes works, yet.
Thanks for your fast and accurate feedback, worked flawlessly.

Keep in contact for future suggestions and questions

from bip_utils.

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.