/** * Get a (cached after first call) serverVersion number * @return serverVersion number of bitcoin node * @throws JsonRpcStatusException JSON RPC status exception * @throws IOException network error */ private int getServerVersion() throws IOException, JsonRpcStatusException { if (serverVersion == 0) { serverVersion = getNetworkInfo().getVersion(); } return serverVersion; }
/** * Sends BTC from an address to the destinations, whereby no new change address is created, and any leftover is * returned to the sending address. * * @param fromAddress The source to spent from * @param outputs The destinations and amounts to transfer * @return The transaction hash */ public Sha256Hash sendBitcoin(Address fromAddress, Map<Address, Coin> outputs) throws JsonRpcStatusException, IOException { String unsignedTxHex = createRawTransaction(fromAddress, outputs); SignedRawTransaction signingResult = signRawTransaction(unsignedTxHex); Boolean complete = signingResult.isComplete(); assert complete; String signedTxHex = signingResult.getHex(); Sha256Hash txid = sendRawTransaction(signedTxHex); return txid; }
amountIn += unspentOutput.getAmount().value; inputs.add(new Outpoint(unspentOutput.getTxid(), unspentOutput.getVout())); SignedRawTransaction signingResult = client.signRawTransaction(unsignedTxHex); Boolean complete = signingResult.isComplete(); assert complete; String signedTxHex = signingResult.getHex(); Object txid = client.sendRawTransaction(signedTxHex, true);
/** * Construct from a bitcoinj transaction * @param transaction A bitcoinj confirmed or unconfirmed transaction */ public RawTransactionInfo(Transaction transaction) { this.hex = TransactionHexSerializer.bytesToHexString(transaction.bitcoinSerialize()); this.txid = transaction.getHash(); this.version = transaction.getVersion(); this.locktime = transaction.getLockTime(); this.blockhash = null; // For now this.confirmations = transaction.getConfidence().getDepthInBlocks(); this.time = 0; // TODO: block header time of block including transaction this.blocktime = this.time; // same as time (see API doc) vin = new VinList(); for (TransactionInput input : transaction.getInputs()) { vin.add(new Vin(txid, input.getOutpoint().getIndex(), input.getScriptSig().toString(), input.getSequenceNumber())); } vout = new VoutList(); for (TransactionOutput output : transaction.getOutputs()) { vout.add(new Vout(output.getValue(), output.getIndex(), output.getScriptPubKey().toString())); } }
@Override protected void doHealthCheck(Health.Builder builder) throws IOException { builder.withDetail("netVersion", bitcoinClient.getNetworkInfo().getVersion()); builder.withDetail("blockNumber", bitcoinClient.getBlockChainInfo().getBlocks()); builder.withDetail("difficulty", bitcoinClient.getBlockChainInfo().getDifficulty()); builder.withDetail("chain", bitcoinClient.getBlockChainInfo().getChain()); builder.status(Status.UP); } }
if (txout != null && txout.getValue().value > 0) { log.debug("txout = {}, value = {}", txout, txout.getValue().value); amountGatheredSoFar += txout.getValue().value; inputs.add(new Outpoint(coinbaseTx, 0)); SignedRawTransaction signingResult = client.signRawTransaction(unsignedTxHex); assert signingResult.isComplete(); String signedTxHex = signingResult.getHex(); Sha256Hash txid = client.sendRawTransaction(signedTxHex, true);
public List<TransactionOutPoint> listUnspentOutPoints(Address fromAddress) throws JsonRpcStatusException, IOException { List<Address> addresses = Collections.singletonList(fromAddress); List<UnspentOutput> unspentOutputsRPC = listUnspent(0, defaultMaxConf, addresses); // RPC UnspentOutput objects List<TransactionOutPoint> unspentOutPoints = new ArrayList<TransactionOutPoint>(); for (UnspentOutput it : unspentOutputsRPC) { unspentOutPoints.add(new TransactionOutPoint(getNetParams(), it.getVout(), it.getTxid())); } return unspentOutPoints; } }
@Override public ServerInfo getinfo() { // Dummy up a response for now. // Since ServerInfo is immutable, we have to build it entirely with the constructor. Coin balance = Coin.valueOf(0); boolean testNet = !netParams.getId().equals(NetworkParameters.ID_MAINNET); int keyPoolOldest = 0; int keyPoolSize = 0; return new ServerInfo( version, protocolVersion, walletVersion, balance, getblockcount(), timeOffset, getconnectioncount(), "proxy", difficulty, testNet, keyPoolOldest, keyPoolSize, Transaction.REFERENCE_DEFAULT_MIN_TX_FEE, Transaction.REFERENCE_DEFAULT_MIN_TX_FEE, // relayfee "no errors" // errors ); }
/** * Returns list of related addresses * Also useful for finding all change addresses in the wallet * @return a lost of address groupings * @throws JsonRpcStatusException JSON RPC status exception * @throws IOException network error */ public List<List<AddressGroupingItem>> listAddressGroupings() throws JsonRpcStatusException, IOException { // TODO: I'm not sure how to make Jackson mapping work automatically here. List<List<List<Object>>> raw = send("listaddressgroupings"); List<List<AddressGroupingItem>> result = new ArrayList<>(); for (List<List<Object>> rawGrouping : raw) { List<AddressGroupingItem> grouping = new ArrayList<>(); for (List<Object> addressItem : rawGrouping) { AddressGroupingItem item = new AddressGroupingItem(addressItem, getNetParams()); grouping.add(item); } result.add(grouping); } return result; }
/** * Returns the Bitcoin balance of an address where spendable outputs have at least {@code minConf} and not more * than {@code maxConf} confirmations. * * @param address The address (must be in wallet) * @param minConf Minimum amount of confirmations * @param maxConf Maximum amount of confirmations * @return The balance */ public Coin getBitcoinBalance(Address address, Integer minConf, Integer maxConf) throws JsonRpcStatusException, IOException { long btcBalance = 0; List<UnspentOutput> unspentOutputs = listUnspent(minConf, maxConf, Collections.singletonList(address)); for (UnspentOutput unspentOutput : unspentOutputs) { btcBalance += unspentOutput.getAmount().value; } return Coin.valueOf(btcBalance); }
/** * Build a list of bitcoinj <code>TransactionOutput</code>s using <code>listUnspent</code> * and <code>getRawTransaction</code> RPCs * * @param fromAddress Address to get UTXOs for * @return All unspent TransactionOutputs for fromAddress */ public List<TransactionOutput> listUnspentJ(Address fromAddress) throws JsonRpcStatusException, IOException { List<Address> addresses = Collections.singletonList(fromAddress); List<UnspentOutput> unspentOutputsRPC = listUnspent(0, defaultMaxConf, addresses); // RPC UnspentOutput objects List<TransactionOutput> unspentOutputsJ = new ArrayList<TransactionOutput>(); for (UnspentOutput it : unspentOutputsRPC) { unspentOutputsJ.add(getRawTransaction(it.getTxid()).getOutput(it.getVout())); } return unspentOutputsJ; }
@Override public ServerInfo getinfo() { // Dummy up a response for now. // Since ServerInfo is immutable, we have to build it entirely with the constructor. Coin balance = Coin.valueOf(0); boolean testNet = !netParams.getId().equals(NetworkParameters.ID_MAINNET); int keyPoolOldest = 0; int keyPoolSize = 0; return new ServerInfo( version, protocolVersion, walletVersion, balance, getblockcount(), timeOffset, getconnectioncount(), "proxy", difficulty, testNet, keyPoolOldest, keyPoolSize, Transaction.REFERENCE_DEFAULT_MIN_TX_FEE, Transaction.REFERENCE_DEFAULT_MIN_TX_FEE, // relayfee "no errors" // errors ); }