|
| 1 | +package com.baeldung.bitcoinj; |
| 2 | + |
| 3 | +import org.bitcoinj.core.NetworkParameters; |
| 4 | +import org.bitcoinj.core.BlockChain; |
| 5 | +import org.bitcoinj.core.PeerGroup; |
| 6 | +import org.bitcoinj.core.Transaction; |
| 7 | +import org.bitcoinj.core.Coin; |
| 8 | +import org.bitcoinj.core.Utils; |
| 9 | +import org.bitcoinj.net.discovery.DnsDiscovery; |
| 10 | +import org.bitcoinj.params.TestNet3Params; |
| 11 | +import org.bitcoinj.script.Script; |
| 12 | +import org.bitcoinj.store.BlockStore; |
| 13 | +import org.bitcoinj.store.BlockStoreException; |
| 14 | +import org.bitcoinj.store.MemoryBlockStore; |
| 15 | +import org.bitcoinj.wallet.DeterministicSeed; |
| 16 | +import org.bitcoinj.wallet.UnreadableWalletException; |
| 17 | +import org.bitcoinj.wallet.Wallet; |
| 18 | +import org.slf4j.Logger; |
| 19 | +import org.slf4j.LoggerFactory; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.io.IOException; |
| 23 | + |
| 24 | +public class CreateWallet { |
| 25 | + |
| 26 | + Logger logger = LoggerFactory.getLogger(CreateWallet.class); |
| 27 | + |
| 28 | + NetworkParameters params = TestNet3Params.get(); |
| 29 | + |
| 30 | + public void createWallet() throws IOException { |
| 31 | + Wallet wallet = Wallet.createDeterministic(params, Script.ScriptType.P2PKH); |
| 32 | + File walletFile = new File("baeldung.dat"); |
| 33 | + wallet.saveToFile(walletFile); |
| 34 | + } |
| 35 | + |
| 36 | + public void createEncryptedWallet() throws IOException { |
| 37 | + Wallet wallet = Wallet.createDeterministic(params, Script.ScriptType.P2PKH); |
| 38 | + File walletFile = new File("baeldungencrypted.dat"); |
| 39 | + wallet.encrypt("password"); |
| 40 | + wallet.saveToFile(walletFile); |
| 41 | + } |
| 42 | + |
| 43 | + public Wallet loadEncryptedWallet() throws IOException, UnreadableWalletException { |
| 44 | + File walletFile = new File("baeldungencrypted.dat"); |
| 45 | + Wallet wallet = Wallet.loadFromFile(walletFile); |
| 46 | + logger.info("Address: " + wallet.currentReceiveAddress().toString()); |
| 47 | + logger.info("Seed Phrase: " + wallet.getKeyChainSeed().getMnemonicString()); |
| 48 | + logger.info("Balance: " + wallet.getBalance().toFriendlyString()); |
| 49 | + logger.info("Public Key: " + wallet.findKeyFromAddress(wallet.currentReceiveAddress()).getPublicKeyAsHex()); |
| 50 | + return wallet; |
| 51 | + } |
| 52 | + |
| 53 | + public Wallet loadDecryptedWallet() throws IOException, UnreadableWalletException { |
| 54 | + File walletFile = new File("baeldungencrypted.dat"); |
| 55 | + Wallet wallet = Wallet.loadFromFile(walletFile); |
| 56 | + wallet.decrypt("password"); |
| 57 | + return wallet; |
| 58 | + } |
| 59 | + |
| 60 | + public Wallet loadWallet() throws IOException, UnreadableWalletException { |
| 61 | + File walletFile = new File("baeldung.dat"); |
| 62 | + Wallet wallet = Wallet.loadFromFile(walletFile); |
| 63 | + logger.info("Address: " + wallet.currentReceiveAddress().toString()); |
| 64 | + logger.info("Seed Phrase: " + wallet.getKeyChainSeed().getMnemonicString()); |
| 65 | + logger.info("Balance: " + wallet.getBalance().toFriendlyString()); |
| 66 | + logger.info("Public Key: " + wallet.findKeyFromAddress(wallet.currentReceiveAddress()).getPublicKeyAsHex()); |
| 67 | + logger.info("Private Key: " + wallet.findKeyFromAddress(wallet.currentReceiveAddress()).getPrivateKeyAsHex()); |
| 68 | + return wallet; |
| 69 | + } |
| 70 | + |
| 71 | + public Wallet loadUsingSeed(String seedWord) throws UnreadableWalletException { |
| 72 | + DeterministicSeed seed = new DeterministicSeed(seedWord, null, "", Utils.currentTimeSeconds()); |
| 73 | + return Wallet.fromSeed(params, seed); |
| 74 | + } |
| 75 | + |
| 76 | + public void connectWalletToPeer() throws BlockStoreException, UnreadableWalletException, IOException { |
| 77 | + Wallet wallet = loadWallet(); |
| 78 | + BlockStore blockStore = new MemoryBlockStore(params); |
| 79 | + BlockChain chain = new BlockChain(params, wallet, blockStore); |
| 80 | + PeerGroup peerGroup = new PeerGroup(params, chain); |
| 81 | + peerGroup.addPeerDiscovery(new DnsDiscovery(params)); |
| 82 | + peerGroup.addWallet(wallet); |
| 83 | + |
| 84 | + wallet.addCoinsReceivedEventListener((Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) -> { |
| 85 | + logger.info("Received tx for " + tx.getValueSentToMe(wallet)); |
| 86 | + logger.info("New balance: " + newBalance.toFriendlyString()); |
| 87 | + }); |
| 88 | + |
| 89 | + peerGroup.start(); |
| 90 | + peerGroup.downloadBlockChain(); |
| 91 | + wallet.saveToFile(new File("baeldung.dat")); |
| 92 | + |
| 93 | + logger.info("Wallet balance: " + wallet.getBalance() |
| 94 | + .toFriendlyString()); |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments