1

Can someone tell me how can I generate a secret/privateKey/miniSecretKey, publicKey without subkey the tool from a mnemonic?

I am working on a c# implementation of the wallet generation and I'd appreciate any help :) The goal is: using the schnorrkel signature and generate the same private/pubkey as the subkey does.(the address generation is working, but i can't derive the correct seed, and i even need it in 64byte version)

The mnemonic i am trying to use and get the results are these: maple rather inject cage food unable enter economy adapt mandate novel start

  Network ID/version: substrate
  Secret seed:        0xce456c3f305bf2c31868b1f51eb297e8b304551f1e456758cfa555d0040b31f7
  64-byte secret:     0x1B04EA5667F6D63B7D405503651F56129B64ED148336F35EECCA53716B872B0B542E9D846415B636BB1741C22C8BCA827CEF9D170B9E671E36B60345D18B3894
  Public key (hex):   0xa805c199eaa3d4d3f0865691e94b3ce4a508ee68aef6d7b42642d0d5d389667d
  Account ID:         0xa805c199eaa3d4d3f0865691e94b3ce4a508ee68aef6d7b42642d0d5d389667d
  SS58 Address:       5Fs1cSCX2ZDvtfC6wvrWWFoQDRdcESLuoTyYofdE1AM7LVhD

so the 64-byte secret how can be generated from this mnemonic "manually"? (i can't really read rust yet btw)

3 Answers 3

2

Just a little bit of Rust :) https://github.com/paritytech/substrate-bip39/blob/eef2f86/src/lib.rs#L44L61

See the README for rationale behind this entropy to seed method.

2

Besides Joe's Rust implementation, you can also check how it is implemented in PolkadotJS#keyring and py-substrate-interface#Keypair:

from substrateinterface import Keypair


mnemonic = 'maple rather inject cage food unable enter economy adapt mandate novel start'
keypair = Keypair.create_from_mnemonic(mnemonic)

print(
    f'Secret seed:\t\t0x{keypair.seed_hex}\n' + \
    f'64-byte secret\t\t0x{keypair.private_key.hex()}\n' + \
    f'Public key (hex):\t0x{keypair.public_key.hex()}\n' + \
    f'Account ID:\t\t0x{keypair.public_key.hex()}\n' + \
    f'SS58 Address:\t\t{keypair.ss58_address}'
)
0

Thanks for the answers, i was kinda aware of these repos, but still thx :)

Since i used NBitcoin lib in c# to genereate pk pairs and it uses a different algo the derive seed, i had to calculate the entropy first, then use the

var pbkdf2 = new NBitcoin.Crypto.Pbkdf2(new System.Security.Cryptography.HMACSHA512(entropy), salt2, 2048);
var seed = pbkdf2.Read(32);
var minis = new Schnorrkel.MiniSecret(seed, ExpandMode.Ed25519);

take the first 32 byte, then create a Schnorrkel.MiniSecret and use the public key to generate the address... stg like this (syntax err, i know...):

...
var ss58Prefixed = new List<byte> { 0x53, 0x53, 0x35, 0x38, 0x50, 0x52, 0x45, networkprefix, publickeybytes };
var blake2bHashed = HashAlgorithm.Blake2b_512.Hash(ss58Prefixed.ToArray());
var addressBytes = [prefix, publickeybytes, blake2bHashed[..2]]
var address = Encoders.Base58.EncodeData(addressBytes);
...

ofc now i have an another issue: transaction fails in case of kusama and westend chains

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.