Bitcoin Forum

Bitcoin => Project Development => Topic started by: Sanka555 on September 18, 2021, 08:00:01 AM



Title: Trying to the I need help
Post by: Sanka555 on September 18, 2021, 08:00:01 AM
I do
Quote
   NetworkParameters params = MainNetParams.get();
      String sedCode = "tissue deliver beauty rare kind midnight focus indicate forum lawn this setup";
      DeterministicSed sed;
      try {
         sed = new DeterministicSed(Code, null, "", 0L);
         Wallet wallet = Wallet.fromSed(params, sed);
         List<String> keys = new ArrayList<>(5);
         for (int i = 0; i < 5; i++) {
            ECKey key = freshReceiveKey();
            e[] publickeyhash = key.getPubKeyHash();
            SegwitAddress Address = SegwitAddress(MainNetParams.get(), publickey);
            keys.add(segwitAddress.toBech32());
         }
         for (String key : keys) {
            System.out.println(key);
         }
......
I get
Quote
bc1qt9w4uxna2gdmscc67enj8azrld92msq6v87aty

but
gives me
Quote
bc1q0u3r6fp8mctx45e75f6p4suzja68alcxkc4cje   

what am I doing wrong?

I will be very grateful for any help  :)


Title: Re: Trying to get private keys from the seed. Java. I need help
Post by: PawGo on September 18, 2021, 08:17:52 AM
Wrong derivation path for a child key. For bc1q0u... you need to use m/84'/0'/0'/0/0.
Take a look at my program https://github.com/PawelGorny/lostword, you may launch it with configuration:
Code:
KNOWN_POSITION
bc1q0u3r6fp8mctx45e75f6p4suzja68alcxkc4cje
12
tissue
deliver
beauty
rare
kind
midnight
focus
indicate
forum
lawn
this
?
m/84'/0'/0'/0/0


Title: Re: Trying to get private keys from the seed. Java. I need help
Post by: NotATether on September 18, 2021, 11:00:58 AM
Also you'll probably want to discard that seed that you used for testing above, now that it has been posted in the open and web crawlers have archived it.


Title: Re: Trying to get private keys from the seed. Java. I need help
Post by: Sanka555 on September 18, 2021, 11:27:40 AM
Wrong derivation path for a child key. For bc1q0u... you need to use m/84'/0'/0'/0/0.
Take a look at my program https://github.com/PawelGorny/lostword, you may launch it with configuration:
Code:
KNOWN_POSITION
bc1q0u3r6fp8mctx45e75f6p4suzja68alcxkc4cje
12
tissue
deliver
beauty
rare
kind
midnight
focus
indicate
forum
lawn
this
?
m/84'/0'/0'/0/0

Thanks a lot.
How can I integrate m / 84 '/ 0' / 0 '/ 0/0 into my code? I just need to get the first 5 addresses. I looked at your program, but there is a whole class forming a complex configuration. Too difficult for a beginner. How can I just hardcode the derivation path into my method?


Title: Re: Trying to get private keys from the seed. Java. I need help
Post by: PawGo on September 18, 2021, 01:19:47 PM
Thanks a lot.
How can I integrate m / 84 '/ 0' / 0 '/ 0/0 into my code? I just need to get the first 5 addresses. I looked at your program, but there is a whole class forming a complex configuration. Too difficult for a beginner. How can I just hardcode the derivation path into my method?

Code:
public class Main {

    final static byte[] BITCOIN_SEED_BYTES = "Bitcoin seed".getBytes();
    final static byte[] SALT = "mnemonic".getBytes(StandardCharsets.UTF_8);

    public static void main(String[] args) throws IOException, MnemonicException, InterruptedException {       
        String seedCode = "tissue deliver beauty rare kind midnight focus indicate forum lawn this setup";
        byte[] seed = PBKDF2SHA512.derive(seedCode.getBytes(StandardCharsets.UTF_8), SALT, 2048, 64);
        DeterministicKey deterministicKey = createMasterPrivateKey(seed, createHmacSha512Digest());
        deterministicKey = HDKeyDerivation.deriveChildKey(deterministicKey, new ChildNumber(84, true));
        deterministicKey = HDKeyDerivation.deriveChildKey(deterministicKey, new ChildNumber(0, true));
        deterministicKey = HDKeyDerivation.deriveChildKey(deterministicKey, new ChildNumber(0, true));
        deterministicKey = HDKeyDerivation.deriveChildKey(deterministicKey, new ChildNumber(0, false));
        for (int i = 0; i <= 5; i++) {
            System.out.println(Address.fromKey(MainNetParams.get(), HDKeyDerivation.deriveChildKey(deterministicKey, new ChildNumber(i, false)), Script.ScriptType.P2WPKH));
        }
    }

    private static DeterministicKey createMasterPrivateKey(byte[] seed, HMac SHA512DIGEST) throws HDDerivationException {
        byte[] i = hmacSha512(SHA512DIGEST, seed);
        byte[] il = Arrays.copyOfRange(i, 0, 32);
        byte[] ir = Arrays.copyOfRange(i, 32, 64);
        Arrays.fill(i, (byte)0);
        DeterministicKey masterPrivKey = HDKeyDerivation.createMasterPrivKeyFromBytes(il, ir);
        Arrays.fill(il, (byte)0);
        Arrays.fill(ir, (byte)0);
        masterPrivKey.setCreationTimeSeconds(System.currentTimeMillis());
        return masterPrivKey;
    }

    private static byte[] hmacSha512(HMac hmacSha512, byte[] input) {
        hmacSha512.reset();
        hmacSha512.update(input, 0, input.length);
        byte[] out = new byte[64];
        hmacSha512.doFinal(out, 0);
        return out;
    }

    private static HMac createHmacSha512Digest() {
        SHA512Digest digest = new SHA512Digest();
        HMac hMac = new HMac(digest);
        hMac.init(new KeyParameter(BITCOIN_SEED_BYTES));
        return hMac;
    }
}


Title: Re: Trying to get private keys from the seed. Java. I need help
Post by: Sanka555 on September 18, 2021, 01:48:57 PM
God bless you, dear friend. As soon as I have some merits, they are all will be yours. Very, very helpful. thanks a lot