NibiruChain / nibiru

Nibiru Chain: The breakthrough smart contract platform ushering in the next era of money. Nibiru powers an ecosystem of dApps including perps, RWAs, and more.
https://nibiru.fi
Apache License 2.0
179 stars 200 forks source link

[bug] gosdk: privkey generation util fn #1897

Closed Unique-Divine closed 1 month ago

Unique-Divine commented 2 months ago

Problem

While playing with the above I found a problem with GoSDK. Correct me if I'm wrong.` This private key has nothing to do with the address:

https://github.com/NibiruChain/nibiru/blob/main/gosdk/keys.go#L54

The actual private/public keys and address are produced within GenerateSaveCoinKey()

Test script:

func main() {
    app.SetPrefixes(appconst.AccountAddressPrefix)
    mnemonic := "guard cream sadness conduct invite crumble clock pudding hole grit liar hotel maid produce squeeze return argue turtle know drive eight casino maze host"
    encCfg := app.MakeEncodingConfig()
    kr := keyring.NewInMemory(encCfg.Codec)
    privKey, addr, err := gosdk.PrivKeyFromMnemonic(kr, mnemonic, "test")
    if err != nil {
        panic(err)
    }
    pubKey := privKey.PubKey()
    addrStr := addr.String()
    fmt.Println("Address (expected):", addrStr)
    fmt.Println("Address from the public key (wrong):", sdk.AccAddress(pubKey.Address().Bytes()).String())
}

OUTPUT: Address (expected): nibi1zaavvzxez0elundtn32qnk9lkm8kmcsz44g7xl Address from the public key (wrong): nibi1nphvjvd47w2re37radm4d67vjg094jjrnrhwdl

keys.go privKey := secp256k1.GenPrivKeyFromSecret([]byte(secret))

Solution

Read the internals of the GenerateSaveCoinKey function and use that logic inside "nibiru/gosdk".