本文整理汇总了Java中org.bitcoinj.core.Transaction.getOutputs方法的典型用法代码示例。如果您正苦于以下问题:Java Transaction.getOutputs方法的具体用法?Java Transaction.getOutputs怎么用?Java Transaction.getOutputs使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bitcoinj.core.Transaction
的用法示例。
在下文中一共展示了Transaction.getOutputs方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getWalletAddressOfReceived
import org.bitcoinj.core.Transaction; //导入方法依赖的package包/类
@Nullable
public static Address getWalletAddressOfReceived(final Transaction tx, final Wallet wallet) {
for (final TransactionOutput output : tx.getOutputs()) {
try {
if (output.isMine(wallet)) {
final Script script = output.getScriptPubKey();
return script.getToAddress(Constants.NETWORK_PARAMETERS, true);
}
} catch (final ScriptException x) {
// swallow
}
}
return null;
}
示例2: isValidImprintingTransaction
import org.bitcoinj.core.Transaction; //导入方法依赖的package包/类
/**
* Return true if the transaction in input is a valid imprinting transaction and contains the specified address in
* one of its output, otherwise false.
* @param tx the transaction to check if is valid imprinting
* @param networkParameters the {@link NetworkParameters}
* @param imprintingAddress the address to check for
* @return true if it's an imprinting transaction otherwise false
*/
public static boolean isValidImprintingTransaction(Transaction tx, NetworkParameters networkParameters, Address imprintingAddress) {
// Retrieve sender
String sender = tx.getInput(0).getFromAddress().toBase58();
// Check output
List<TransactionOutput> transactionOutputs = tx.getOutputs();
for (TransactionOutput to : transactionOutputs) {
Address address = to.getAddressFromP2PKHScript(networkParameters);
if (address != null && address.equals(imprintingAddress)) {
return true;
}
}
return false;
}
示例3: markKeysAsUsed
import org.bitcoinj.core.Transaction; //导入方法依赖的package包/类
/**
* Marks all keys used in the transaction output as used in the wallet.
* See {@link org.bitcoinj.wallet.DeterministicKeyChain#markKeyAsUsed(DeterministicKey)} for more info on this.
*/
private void markKeysAsUsed(Transaction tx) {
keyChainGroupLock.lock();
try {
for (TransactionOutput o : tx.getOutputs()) {
try {
Script script = o.getScriptPubKey();
if (script.isSentToRawPubKey()) {
byte[] pubkey = script.getPubKey();
keyChainGroup.markPubKeyAsUsed(pubkey);
} else if (script.isSentToAddress()) {
byte[] pubkeyHash = script.getPubKeyHash();
keyChainGroup.markPubKeyHashAsUsed(pubkeyHash);
} else if (script.isPayToScriptHash()) {
Address a = Address.fromP2SHScript(tx.getParams(), script);
keyChainGroup.markP2SHAddressAsUsed(a);
}
} catch (ScriptException e) {
// Just means we didn't understand the output of this transaction: ignore it.
log.warn("Could not parse tx output script: {}", e.toString());
}
}
} finally {
keyChainGroupLock.unlock();
}
}
示例4: isTxConsistent
import org.bitcoinj.core.Transaction; //导入方法依赖的package包/类
@VisibleForTesting
boolean isTxConsistent(final Transaction tx, final boolean isSpent) {
boolean isActuallySpent = true;
for (TransactionOutput o : tx.getOutputs()) {
if (o.isAvailableForSpending()) {
if (o.isMineOrWatched(this)) isActuallySpent = false;
if (o.getSpentBy() != null) {
log.error("isAvailableForSpending != spentBy");
return false;
}
} else {
if (o.getSpentBy() == null) {
log.error("isAvailableForSpending != spentBy");
return false;
}
}
}
return isActuallySpent == isSpent;
}
示例5: getTotalReceived
import org.bitcoinj.core.Transaction; //导入方法依赖的package包/类
/**
* Returns the amount of bitcoin ever received via output. <b>This is not the balance!</b> If an output spends from a
* transaction whose inputs are also to our wallet, the input amounts are deducted from the outputs contribution, with a minimum of zero
* contribution. The idea behind this is we avoid double counting money sent to us.
* @return the total amount of satoshis received, regardless of whether it was spent or not.
*/
public Coin getTotalReceived() {
Coin total = Coin.ZERO;
// Include outputs to us if they were not just change outputs, ie the inputs to us summed to less
// than the outputs to us.
for (Transaction tx: transactions.values()) {
Coin txTotal = Coin.ZERO;
for (TransactionOutput output : tx.getOutputs()) {
if (output.isMine(this)) {
txTotal = txTotal.add(output.getValue());
}
}
for (TransactionInput in : tx.getInputs()) {
TransactionOutput prevOut = in.getConnectedOutput();
if (prevOut != null && prevOut.isMine(this)) {
txTotal = txTotal.subtract(prevOut.getValue());
}
}
if (txTotal.isPositive()) {
total = total.add(txTotal);
}
}
return total;
}
示例6: calcBloomOutPointsLocked
import org.bitcoinj.core.Transaction; //导入方法依赖的package包/类
private void calcBloomOutPointsLocked() {
// TODO: This could be done once and then kept up to date.
bloomOutPoints.clear();
Set<Transaction> all = new HashSet<>();
all.addAll(unspent.values());
all.addAll(spent.values());
all.addAll(pending.values());
for (Transaction tx : all) {
for (TransactionOutput out : tx.getOutputs()) {
try {
if (isTxOutputBloomFilterable(out))
bloomOutPoints.add(out.getOutPointFor());
} catch (ScriptException e) {
// If it is ours, we parsed the script correctly, so this shouldn't happen.
throw new RuntimeException(e);
}
}
}
}
示例7: childPaysForParent
import org.bitcoinj.core.Transaction; //导入方法依赖的package包/类
/**
* Construct a SendRequest for a CPFP (child-pays-for-parent) transaction. The resulting transaction is already
* completed, so you should directly proceed to signing and broadcasting/committing the transaction. CPFP is
* currently only supported by a few miners, so use with care.
*/
public static SendRequest childPaysForParent(Wallet wallet, Transaction parentTransaction, Coin feeRaise) {
TransactionOutput outputToSpend = null;
for (final TransactionOutput output : parentTransaction.getOutputs()) {
if (output.isMine(wallet) && output.isAvailableForSpending()
&& output.getValue().isGreaterThan(feeRaise)) {
outputToSpend = output;
break;
}
}
// TODO spend another confirmed output of own wallet if needed
checkNotNull(outputToSpend, "Can't find adequately sized output that spends to us");
final Transaction tx = new Transaction(parentTransaction.getParams());
tx.addInput(outputToSpend);
tx.addOutput(outputToSpend.getValue().subtract(feeRaise), wallet.freshAddress(KeyPurpose.CHANGE));
tx.setPurpose(Transaction.Purpose.RAISE_FEE);
final SendRequest req = forTx(tx);
req.completed = true;
return req;
}
示例8: getToAddressOfSent
import org.bitcoinj.core.Transaction; //导入方法依赖的package包/类
@Nullable
public static Address getToAddressOfSent(final Transaction tx, final Wallet wallet) {
for (final TransactionOutput output : tx.getOutputs()) {
try {
if (!output.isMine(wallet)) {
final Script script = output.getScriptPubKey();
return script.getToAddress(Constants.NETWORK_PARAMETERS, true);
}
} catch (final ScriptException x) {
// swallow
}
}
return null;
}
示例9: isEntirelySelf
import org.bitcoinj.core.Transaction; //导入方法依赖的package包/类
public static boolean isEntirelySelf(final Transaction tx, final Wallet wallet) {
for (final TransactionInput input : tx.getInputs()) {
final TransactionOutput connectedOutput = input.getConnectedOutput();
if (connectedOutput == null || !connectedOutput.isMine(wallet))
return false;
}
for (final TransactionOutput output : tx.getOutputs()) {
if (!output.isMine(wallet))
return false;
}
return true;
}
示例10: isTransactionBroadcastAnnouncement
import org.bitcoinj.core.Transaction; //导入方法依赖的package包/类
public boolean isTransactionBroadcastAnnouncement(Transaction tx) {
List<TransactionOutput> outputs = tx.getOutputs();
TransactionOutput scndOutput = outputs.get(1);
if(!scndOutput.getScriptPubKey().isOpReturn()) {
return false;
}
byte[] script = scndOutput.getScriptBytes();
//check magic numbers that are defined in BroadcastAnnouncement
return BroadcastAnnouncement.isBroadcastAnnouncementScript(script);
}
示例11: addWalletTransaction
import org.bitcoinj.core.Transaction; //导入方法依赖的package包/类
/**
* Adds the given transaction to the given pools and registers a confidence change listener on it.
*/
private void addWalletTransaction(Pool pool, Transaction tx) {
checkState(lock.isHeldByCurrentThread());
transactions.put(tx.getHash(), tx);
switch (pool) {
case UNSPENT:
checkState(unspent.put(tx.getHash(), tx) == null);
break;
case SPENT:
checkState(spent.put(tx.getHash(), tx) == null);
break;
case PENDING:
checkState(pending.put(tx.getHash(), tx) == null);
break;
case DEAD:
checkState(dead.put(tx.getHash(), tx) == null);
break;
default:
throw new RuntimeException("Unknown wallet transaction type " + pool);
}
if (pool == Pool.UNSPENT || pool == Pool.PENDING) {
for (TransactionOutput output : tx.getOutputs()) {
if (output.isAvailableForSpending() && output.isMineOrWatched(this))
myUnspents.add(output);
}
}
// This is safe even if the listener has been added before, as TransactionConfidence ignores duplicate
// registration requests. That makes the code in the wallet simpler.
tx.getConfidence().addEventListener(Threading.SAME_THREAD, txConfidenceListener);
}
示例12: getTotalSent
import org.bitcoinj.core.Transaction; //导入方法依赖的package包/类
/**
* Returns the amount of bitcoin ever sent via output. If an output is sent to our own wallet, because of change or
* rotating keys or whatever, we do not count it. If the wallet was
* involved in a shared transaction, i.e. there is some input to the transaction that we don't have the key for, then
* we multiply the sum of the output values by the proportion of satoshi coming in to our inputs. Essentially we treat
* inputs as pooling into the transaction, becoming fungible and being equally distributed to all outputs.
* @return the total amount of satoshis sent by us
*/
public Coin getTotalSent() {
Coin total = Coin.ZERO;
for (Transaction tx: transactions.values()) {
// Count spent outputs to only if they were not to us. This means we don't count change outputs.
Coin txOutputTotal = Coin.ZERO;
for (TransactionOutput out : tx.getOutputs()) {
if (out.isMine(this) == false) {
txOutputTotal = txOutputTotal.add(out.getValue());
}
}
// Count the input values to us
Coin txOwnedInputsTotal = Coin.ZERO;
for (TransactionInput in : tx.getInputs()) {
TransactionOutput prevOut = in.getConnectedOutput();
if (prevOut != null && prevOut.isMine(this)) {
txOwnedInputsTotal = txOwnedInputsTotal.add(prevOut.getValue());
}
}
// If there is an input that isn't from us, i.e. this is a shared transaction
Coin txInputsTotal = tx.getInputSum();
if (txOwnedInputsTotal != txInputsTotal) {
// multiply our output total by the appropriate proportion to account for the inputs that we don't own
BigInteger txOutputTotalNum = new BigInteger(txOutputTotal.toString());
txOutputTotalNum = txOutputTotalNum.multiply(new BigInteger(txOwnedInputsTotal.toString()));
txOutputTotalNum = txOutputTotalNum.divide(new BigInteger(txInputsTotal.toString()));
txOutputTotal = Coin.valueOf(txOutputTotalNum.longValue());
}
total = total.add(txOutputTotal);
}
return total;
}
示例13: findSpendableOutput
import org.bitcoinj.core.Transaction; //导入方法依赖的package包/类
private static @Nullable TransactionOutput findSpendableOutput(final Wallet wallet, final Transaction transaction,
final Coin minimumOutputValue) {
for (final TransactionOutput output : transaction.getOutputs()) {
if (output.isMine(wallet) && output.isAvailableForSpending()
&& output.getValue().isGreaterThan(minimumOutputValue))
return output;
}
return null;
}
示例14: appendApplicationInfo
import org.bitcoinj.core.Transaction; //导入方法依赖的package包/类
public static void appendApplicationInfo(final Appendable report, final WalletApplication application)
throws IOException {
final PackageInfo pi = application.packageInfo();
final Configuration configuration = application.getConfiguration();
final Calendar calendar = new GregorianCalendar(UTC);
report.append("Version: " + pi.versionName + " (" + pi.versionCode + ")\n");
report.append("Package: " + pi.packageName + "\n");
report.append("Installer: " + application.getPackageManager().getInstallerPackageName(pi.packageName) + "\n");
report.append("Test/Prod: " + (Constants.TEST ? "test" : "prod") + "\n");
report.append("Timezone: " + TimeZone.getDefault().getID() + "\n");
calendar.setTimeInMillis(System.currentTimeMillis());
report.append("Time: " + String.format(Locale.US, "%tF %tT %tZ", calendar, calendar, calendar) + "\n");
calendar.setTimeInMillis(WalletApplication.TIME_CREATE_APPLICATION);
report.append(
"Time of launch: " + String.format(Locale.US, "%tF %tT %tZ", calendar, calendar, calendar) + "\n");
calendar.setTimeInMillis(pi.lastUpdateTime);
report.append(
"Time of last update: " + String.format(Locale.US, "%tF %tT %tZ", calendar, calendar, calendar) + "\n");
calendar.setTimeInMillis(pi.firstInstallTime);
report.append("Time of first install: " + String.format(Locale.US, "%tF %tT %tZ", calendar, calendar, calendar)
+ "\n");
final long lastBackupTime = configuration.getLastBackupTime();
calendar.setTimeInMillis(lastBackupTime);
report.append("Time of backup: "
+ (lastBackupTime > 0 ? String.format(Locale.US, "%tF %tT %tZ", calendar, calendar, calendar) : "none")
+ "\n");
report.append("Network: " + Constants.NETWORK_PARAMETERS.getId() + "\n");
final Wallet wallet = application.getWallet();
report.append("Encrypted: " + wallet.isEncrypted() + "\n");
report.append("Keychain size: " + wallet.getKeyChainGroupSize() + "\n");
final Set<Transaction> transactions = wallet.getTransactions(true);
int numInputs = 0;
int numOutputs = 0;
int numSpentOutputs = 0;
for (final Transaction tx : transactions) {
numInputs += tx.getInputs().size();
final List<TransactionOutput> outputs = tx.getOutputs();
numOutputs += outputs.size();
for (final TransactionOutput txout : outputs) {
if (!txout.isAvailableForSpending())
numSpentOutputs++;
}
}
report.append("Transactions: " + transactions.size() + "\n");
report.append("Inputs: " + numInputs + "\n");
report.append("Outputs: " + numOutputs + " (spent: " + numSpentOutputs + ")\n");
report.append(
"Last block seen: " + wallet.getLastBlockSeenHeight() + " (" + wallet.getLastBlockSeenHash() + ")\n");
report.append("Databases:");
for (final String db : application.databaseList())
report.append(" " + db);
report.append("\n");
final File filesDir = application.getFilesDir();
report.append("\nContents of FilesDir " + filesDir + ":\n");
appendDir(report, filesDir, 0);
}
示例15: signTransaction
import org.bitcoinj.core.Transaction; //导入方法依赖的package包/类
/**
* <p>Given a send request containing transaction, attempts to sign it's inputs. This method expects transaction
* to have all necessary inputs connected or they will be ignored.</p>
* <p>Actual signing is done by pluggable {@link #signers} and it's not guaranteed that
* transaction will be complete in the end.</p>
*/
public void signTransaction(SendRequest req) {
lock.lock();
try {
Transaction tx = req.tx;
List<TransactionInput> inputs = tx.getInputs();
List<TransactionOutput> outputs = tx.getOutputs();
checkState(inputs.size() > 0);
checkState(outputs.size() > 0);
KeyBag maybeDecryptingKeyBag = new DecryptingKeyBag(this, req.aesKey);
int numInputs = tx.getInputs().size();
for (int i = 0; i < numInputs; i++) {
TransactionInput txIn = tx.getInput(i);
if (txIn.getConnectedOutput() == null) {
// Missing connected output, assuming already signed.
continue;
}
try {
// We assume if its already signed, its hopefully got a SIGHASH type that will not invalidate when
// we sign missing pieces (to check this would require either assuming any signatures are signing
// standard output types or a way to get processed signatures out of script execution)
txIn.getScriptSig().correctlySpends(tx, i, txIn.getConnectedOutput().getScriptPubKey());
log.warn("Input {} already correctly spends output, assuming SIGHASH type used will be safe and skipping signing.", i);
continue;
} catch (ScriptException e) {
log.debug("Input contained an incorrect signature", e);
// Expected.
}
Script scriptPubKey = txIn.getConnectedOutput().getScriptPubKey();
RedeemData redeemData = txIn.getConnectedRedeemData(maybeDecryptingKeyBag);
checkNotNull(redeemData, "Transaction exists in wallet that we cannot redeem: %s", txIn.getOutpoint().getHash());
txIn.setScriptSig(scriptPubKey.createEmptyInputScript(redeemData.keys.get(0), redeemData.redeemScript));
}
TransactionSigner.ProposedTransaction proposal = new TransactionSigner.ProposedTransaction(tx);
for (TransactionSigner signer : signers) {
if (!signer.signInputs(proposal, maybeDecryptingKeyBag))
log.info("{} returned false for the tx", signer.getClass().getName());
}
// resolve missing sigs if any
new MissingSigResolutionSigner(req.missingSigsMode).signInputs(proposal, maybeDecryptingKeyBag);
} finally {
lock.unlock();
}
}