1

I used SCALE Codec for java offline address generation. but I see that generated address and polkadot.js generated address are not alike. I use bitcoinj 's MnemonicCode generation mnemonic, and I toseed the mnemonic get seed; this is my code;

public void createAddress() throws Exception {
    System.out.println("Generate a new Root Key + derive a `demo` address from that key");
    int number = 0;
    byte[] initialEntropy = new byte[128 / 8];
    SecureRandomUtils.secureRandom().nextBytes(initialEntropy);
    String mnemonic = "devote fatigue dice clock lock cross creek neutral minute achieve betray curve";
    String[] mm = mnemonic.split(" ");
    List<String> strings = Arrays.asList(mm.clone());
    System.out.println(mnemonic);
    String n =  Utils.SPACE_JOINER.join(strings);
    System.out.println(n);
    byte[] seed = toSeed(strings, "");
    String s = Hex.encodeHexString(seed);
    System.out.println(s);
    Schnorrkel instance = Schnorrkel.getInstance();
    Schnorrkel.KeyPair rootKey = instance.generateKeyPairFromSeed(seed);
    System.out.println("  Root Key: " + Hex.encodeHexString(rootKey.getSecretKey()));
    Schnorrkel.KeyPair key = instance.deriveKeyPair(rootKey, Schnorrkel.ChainCode.from("".getBytes()));
    Address address = new Address(SS58Type.Network.LIVE, key.getPublicKey());
    System.out.println("   Address: " + address);
    System.out.println("Public Key: " + Hex.encodeHexString(key.getPublicKey()));
    System.out.println("Secret Key: " + Hex.encodeHexString(key.getSecretKey()));
}




 public static byte[] toSeed(List<String> words, String passphrase) {
    checkNotNull(passphrase, "A null passphrase is not allowed.");

    // To create binary seed from mnemonic, we use PBKDF2 function
    // with mnemonic sentence (in UTF-8) used as a password and
    // string "mnemonic" + passphrase (again in UTF-8) used as a
    // salt. Iteration count is set to 4096 and HMAC-SHA512 is
    // used as a pseudo-random function. Desired length of the
    // derived key is 512 bits (= 64 bytes).
    //
    String pass = Utils.SPACE_JOINER.join(words);
    String salt = "mnemonic" + passphrase;

    final Stopwatch watch = Stopwatch.createStarted();
    byte[] seed = PBKDF2SHA512.derive(pass, salt, 2048, 32);
    watch.stop();
    System.out.println("PBKDF2 took {}" +  watch);
    return seed;
}

polkadot.js generated address is "12EgmkT6NHjTjtwcvfBFu1dkPEN9TLFo3ftA4L2ZcmkCfQCp";

i use my code generated address is "12YTJRjPRsw34wBp2Ewfr9oBP9w47RpKKw4CPZF2zaCga1Hk"

how do I generate similar polkadot.js' address ?

Gurunath Sripad
  • 1,308
  • 2
  • 14
  • 24
liutianv1
  • 11
  • 2

0 Answers0