Get private-keys from bitcoinj

0

I'm using bitcoinj library for bitcoin.

I want all private keys of all my generated addresses.

How can I achieve that? Please help who knows this answer.

Thanks.

Bandish

Posted 2018-10-03T06:27:43.997

Reputation: 23

Answers

1

I have write this sample code, hope this helps

public class Main {
    private static NetworkParameters params = NetworkParameters.testNet3();
    public static void main(String...args) {
        String mnemonic = "brisk wood symptom party betray ozone dad super beyond sea memory power pig business extra";
        DeterministicSeed seed = null;
        try {
            seed = new DeterministicSeed(mnemonic, null, "", new Date().getTime());
        } catch (UnreadableWalletException e) {
            e.printStackTrace();
        }
        Wallet wallet = Wallet.fromSeed(params, seed);
        System.out.println(wallet);
        DeterministicKeyChain keyChain = DeterministicKeyChain.builder().seed(seed).build();


        // Derive 100 children, default lookahead is 100
        keyChain.maybeLookAhead();


        // Get M/0H/0/5 key, drive address along with other info for specific path
        DeterministicKey key5 = keyChain.getKeyByPath(ImmutableList.of(ChildNumber.ZERO_HARDENED, ChildNumber.ZERO, new ChildNumber(5)), false);


        System.out.println(key5.toAddress(params).toString());

        // derive 100 addresses along with private key
        System.out.println("_________________________");
        int i = 0;
        while (i < 100) {
            System.out.println(wallet.freshReceiveKey().getPrivateKeyAsHex());
            System.out.println(wallet.freshReceiveAddress());
            i++;
        }

    }
}

Zombie

Posted 2018-10-03T06:27:43.997

Reputation: 538