Bitcoin Forum
August 14, 2024, 01:53:41 PM *
News: Latest Bitcoin Core release: 27.1 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Trying to the I need help  (Read 90 times)
Sanka555 (OP)
Member
**
Offline Offline

Activity: 96
Merit: 36


View Profile
September 18, 2021, 08:00:01 AM
Last edit: September 18, 2021, 06:28:18 PM by Sanka555
Merited by o_e_l_e_o (4)
 #1

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  Smiley
PawGo
Legendary
*
Offline Offline

Activity: 952
Merit: 1372


View Profile
September 18, 2021, 08:17:52 AM
Merited by ABCbits (2)
 #2

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
NotATether
Legendary
*
Offline Offline

Activity: 1694
Merit: 7150


In memory of o_e_l_e_o


View Profile WWW
September 18, 2021, 11:00:58 AM
 #3

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.

Sanka555 (OP)
Member
**
Offline Offline

Activity: 96
Merit: 36


View Profile
September 18, 2021, 11:27:40 AM
 #4

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?
PawGo
Legendary
*
Offline Offline

Activity: 952
Merit: 1372


View Profile
September 18, 2021, 01:19:47 PM
Merited by Sanka555 (7), o_e_l_e_o (4), nc50lc (1)
 #5

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;
    }
}
Sanka555 (OP)
Member
**
Offline Offline

Activity: 96
Merit: 36


View Profile
September 18, 2021, 01:48:57 PM
 #6

God bless you, dear friend. As soon as I have some merits, they are all will be yours. Very, very helpful. thanks a lot
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!