package org.sample.bitcoinj.explored.bitcoinj_explored;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import com.google.bitcoin.core.AbstractWalletEventListener;
import com.google.bitcoin.core.BlockChain;
import com.google.bitcoin.core.ECKey;
import com.google.bitcoin.core.InsufficientMoneyException;
import com.google.bitcoin.core.NetworkParameters;
import com.google.bitcoin.core.PeerAddress;
import com.google.bitcoin.core.PeerGroup;
import com.google.bitcoin.core.ScriptException;
import com.google.bitcoin.core.Transaction;
import com.google.bitcoin.core.TransactionInput;
import com.google.bitcoin.core.Utils;
import com.google.bitcoin.core.Wallet;
import com.google.bitcoin.core.Wallet.SendResult;
import com.google.bitcoin.store.BlockStore;
import com.google.bitcoin.store.BlockStoreException;
import com.google.bitcoin.store.SPVBlockStore;
import com.google.bitcoin.store.UnreadableWalletException;
public class MyWallet {
public static void main(String[] args) {
boolean testNet = true; //args.length > 0 && args[0].equalsIgnoreCase("testnet");
final NetworkParameters params = testNet ? NetworkParameters.testNet() : NetworkParameters.prodNet();
String filePrefix = testNet ? "MyWallet-testnet" : "MyWallet-prodnet";
// Try to read the wallet from storage, create a new one if not possible.
Wallet wallet = null;
final File walletFile = new File(filePrefix + ".wallet");
try {
wallet = Wallet.loadFromFile(walletFile);
} catch (UnreadableWalletException e) {
wallet = new Wallet(params);
wallet.addKey(new ECKey());
try {
wallet.saveToFile(walletFile);
} catch (IOException e1) {
e1.printStackTrace();
}
}
ECKey key = wallet.getKeys().get(0);
System.out.println(wallet);
// Load the block chain, if there is one stored locally.
System.out.println("Reading block store from disk");
File chainFile = new File("testnet.spvchain");
BlockStore blockStore = null;
try {
blockStore = new SPVBlockStore(params, chainFile);
} catch (BlockStoreException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
// Connect to the localhost node. One minute timeout since we won't try any other peers
System.out.println("Connecting ...");
BlockChain chain = null;
try {
chain = new BlockChain(params, wallet, blockStore);
} catch (BlockStoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
chain.addWallet(wallet);
final PeerGroup peerGroup = new PeerGroup(params, chain);
peerGroup.addWallet(wallet);
try {
peerGroup.addAddress(new PeerAddress(InetAddress.getLocalHost()));
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
peerGroup.start();
wallet.allowSpendingUnconfirmedTransactions();
// We want to know when the balance changes.
wallet.addEventListener(new AbstractWalletEventListener() {
@Override
public void onCoinsReceived(Wallet w, Transaction tx, BigInteger prevBalance, BigInteger newBalance) {
// Running on a peer thread.
//assert !newBalance.equals(BigInteger.ZERO);
// It's impossible to pick one specific identity that you receive coins from in BitCoin as there
// could be inputs from many addresses. So instead we just pick the first and assume they were all
// owned by the same person.
try {
TransactionInput input = tx.getInputs().get(0);
com.google.bitcoin.core.Address from = input.getFromAddress();
BigInteger value = tx.getValueSentToMe(w);
System.out.println("Received " + Utils.bitcoinValueToFriendlyString(value) + " from " + from.toString());
// Now send the coins back!
SendResult sendTx = null;
try {
sendTx = w.sendCoins(peerGroup, from, value);
} catch (InsufficientMoneyException e) {
e.printStackTrace();
}
System.out.println("Sent coins back! Transaction hash is " + sendTx.toString());
w.saveToFile(walletFile);
} catch (ScriptException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
});
try {
wallet.saveToFile(walletFile);
} catch (IOException e) {
e.printStackTrace();
}
peerGroup.downloadBlockChain();
System.out.println("Send coins to: " + key.toAddress(params).toString());
System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit.");
}
}
Unable to send fake coins from
http://tpfaucet.appspot.com to MyWallet-testnet.wallet file which is stored in my local hard disk. Always getting "Wallet containing 0 BTC (available: 0 BTC)".
Using below dependency in my pom.xml
<dependency>
<artifactId>bitcoinj</artifactId>
<version>0.11.3</version>
<scope>compile</scope>
</dependency>
Will you please suggest how to add fake btc coins in my local bitcoinj wallet.
It is urgent. Will you please help to resolve balance issue.
Thanks in Advance.