@Override public TransactionDetails getTransactionDetails(Sha256Hash txid) { // Note that this method is not synchronized, and we might fetch the transaction history while // synchronizing // accounts. That should be ok as we write to the DB in a sane order. TransactionEx tex = _backing.getTransaction(txid); Transaction tx = TransactionEx.toTransaction(tex); if (tx == null) { throw new RuntimeException(); } List<TransactionDetails.Item> inputs = new ArrayList<TransactionDetails.Item>(tx.inputs.length); if (tx.isCoinbase()) { // We have a coinbase transaction. Create one input with the sum of the outputs as its value, // and make the address the null address long value = 0; for (TransactionOutput out : tx.outputs) { value += out.value; } inputs.add(new TransactionDetails.Item(Address.getNullAddress(_network), value, true)); } else { // Populate the inputs for (TransactionInput input : tx.inputs) { Sha256Hash parentHash = input.outPoint.hash; // Get the parent transaction TransactionOutputEx parentOutput = _backing.getParentTransactionOutput(input.outPoint); if (parentOutput == null) { // We never heard about the parent, skip continue; } // Determine the parent address Address parentAddress; ScriptOutput parentScript = ScriptOutput.fromScriptBytes(parentOutput.script); if (parentScript == null) { // Null address means we couldn't figure out the address, strange script parentAddress = Address.getNullAddress(_network); } else { parentAddress = parentScript.getAddress(_network); } inputs.add(new TransactionDetails.Item(parentAddress, parentOutput.value, false)); } } // Populate the outputs TransactionDetails.Item[] outputs = new TransactionDetails.Item[tx.outputs.length]; for (int i = 0; i < tx.outputs.length; i++) { Address address = tx.outputs[i].script.getAddress(_network); outputs[i] = new TransactionDetails.Item(address, tx.outputs[i].value, false); } return new TransactionDetails( txid, tex.height, tex.time, inputs.toArray(new TransactionDetails.Item[] {}), outputs); }
/** * Determine whether a transaction was sent from one of our own addresses. * * <p>This is a costly operation as we have to lookup funding outputs of the transaction * * @param t the transaction to investigate * @return true iff one of the funding outputs were sent from one of our own addresses */ protected boolean isFromMe(Transaction t) { for (TransactionInput input : t.inputs) { TransactionOutputEx funding = _backing.getParentTransactionOutput(input.outPoint); if (funding == null || funding.isCoinBase) { continue; } ScriptOutput fundingScript = ScriptOutput.fromScriptBytes(funding.script); Address fundingAddress = fundingScript.getAddress(_network); if (isMine(fundingAddress)) { return true; } } return false; }
@Override public List<TransactionOutputSummary> getUnspentTransactionOutputSummary() { // Note that this method is not synchronized, and we might fetch the transaction history while // synchronizing // accounts. That should be ok as we write to the DB in a sane order. // Get all unspent outputs for this account Collection<TransactionOutputEx> outputs = _backing.getAllUnspentOutputs(); // Transform it to a list of summaries List<TransactionOutputSummary> list = new ArrayList<TransactionOutputSummary>(); int blockChainHeight = getBlockChainHeight(); for (TransactionOutputEx output : outputs) { ScriptOutput script = ScriptOutput.fromScriptBytes(output.script); Address address; if (script == null) { address = Address.getNullAddress(_network); // This never happens as we have parsed this script before } else { address = script.getAddress(_network); } int confirmations; if (output.height == -1) { confirmations = 0; } else { confirmations = Math.max(0, blockChainHeight - output.height + 1); } TransactionOutputSummary summary = new TransactionOutputSummary( output.outPoint, output.value, output.height, confirmations, address); list.add(summary); } // Sort & return Collections.sort(list); return list; }
/** * Determine whether an output script was created by one of our own addresses * * @param script the script to investigate * @return true iff the script was created by one of our own addresses */ protected boolean isMine(ScriptOutput script) { Address address = script.getAddress(_network); return isMine(address); }