1

我使用 SCALE Codec 生成 java 离线地址。但我看到生成的地址和 polkadot.js 生成的地址不一样。我使用了 bitcoinj 的 MnemonicCode 生成助记词,然后我给助记词添加种子;这是我的代码;</p>

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 生成地址为“12EgmkT6NHjTjtwcvfBFu1dkPEN9TLFo3ftA4L2ZcmkCfQCp”;

我使用我的代码生成的地址是“12YTJRjPRsw34wBp2Ewfr9oBP9w47RpKKw4CPZF2zaCga1Hk”

如何生成类似的 polkadot.js 的地址?

4

0 回答 0