org.bitcoinj.wallet.Wallet.createSend()方法的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(15.7k)|赞(0)|评价(0)|浏览(92)

本文整理了Java中org.bitcoinj.wallet.Wallet.createSend()方法的一些代码示例,展示了Wallet.createSend()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Wallet.createSend()方法的具体详情如下:
包路径:org.bitcoinj.wallet.Wallet
类名称:Wallet
方法名:createSend

Wallet.createSend介绍

[英]Statelessly creates a transaction that sends the given value to address. The change is sent to Wallet#currentChangeAddress(), so you must have added at least one key.

If you just want to send money quickly, you probably want Wallet#sendCoins(TransactionBroadcaster,Address,Coin) instead. That will create the sending transaction, commit to the wallet and broadcast it to the network all in one go. This method is lower level and lets you see the proposed transaction before anything is done with it.

This is a helper method that is equivalent to using SendRequest#to(Address,Coin)followed by Wallet#completeTx(SendRequest) and returning the requests transaction object. Note that this means a fee may be automatically added if required, if you want more control over the process, just do those two steps yourself.

IMPORTANT: This method does NOT update the wallet. If you call createSend again you may get two transactions that spend the same coins. You have to call Wallet#commitTx(Transaction) on the created transaction to prevent this, but that should only occur once the transaction has been accepted by the network. This implies you cannot have more than one outstanding sending tx at once.

You MUST ensure that the value is not smaller than Transaction#MIN_NONDUST_OUTPUT or the transaction will almost certainly be rejected by the network as dust.
[中]无状态创建将给定值发送到地址的事务。更改会发送到Wallet#currentChangeAddress(),因此您必须至少添加一个密钥。
如果你只是想快速汇款,你可能需要钱包#发送硬币(TransactionBroadcaster、地址、硬币)。这将创建发送交易,提交到钱包,并将其一次性广播到网络。这种方法级别较低,可以让您在完成任何操作之前查看提议的事务。
这是一种助手方法,相当于使用SendRequest#to(地址、硬币),然后使用Wallet#CompletEx(SendRequest)并返回请求事务对象。请注意,这意味着如果需要,可以自动添加费用。如果您想对流程进行更多控制,只需自己执行这两个步骤。
重要提示:此方法不会更新钱包。如果您再次致电createSend,您可能会收到两笔花费相同硬币的交易。您必须在创建的交易上致电Wallet#commitTx(交易),以防止出现这种情况,但只有在交易被网络接受后才会发生这种情况。这意味着一次不能有多个未完成的发送tx。
您必须确保该值不小于事务#MIN_NONDUST_输出,否则该事务几乎肯定会被网络拒绝。

代码示例

代码示例来源:origin: HashEngineering/dashj

Transaction tx1 = kit.wallet().createSend(Address.fromBase58(params, "muYPFNCv7KQEG2ZLM7Z3y96kJnNyXJ53wm"), CENT);
Transaction tx2 = kit.wallet().createSend(Address.fromBase58(params, "muYPFNCv7KQEG2ZLM7Z3y96kJnNyXJ53wm"), CENT.add(SATOSHI.multiply(10)));
final Peer peer = kit.peerGroup().getConnectedPeers().get(0);
peer.addPreMessageReceivedEventListener(Threading.SAME_THREAD,

代码示例来源:origin: greenaddress/GreenBits

@Test
public void testAddTransactionsDependingOn() throws Exception {
  CoinSelector originalCoinSelector = wallet.getCoinSelector();
  try {
    wallet.allowSpendingUnconfirmedTransactions();
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(2, 0));
    Transaction send1 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 0)));
    Transaction send2 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 20)));
    wallet.commitTx(send1);
    Transaction send1b = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 50)));
    wallet.commitTx(send1b);
    Transaction send1c = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 25)));
    wallet.commitTx(send1c);
    wallet.commitTx(send2);
    Set<Transaction> txns = new HashSet<>();
    txns.add(send1);
    wallet.addTransactionsDependingOn(txns, wallet.getTransactions(true));
    assertEquals(3, txns.size());
    assertTrue(txns.contains(send1));
    assertTrue(txns.contains(send1b));
    assertTrue(txns.contains(send1c));
  } finally {
    wallet.setCoinSelector(originalCoinSelector);
  }
}

代码示例来源:origin: greenaddress/GreenBits

@Test
public void txSpendingDeadTx() throws Exception {
  CoinSelector originalCoinSelector = wallet.getCoinSelector();
  try {
    wallet.allowSpendingUnconfirmedTransactions();
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(2, 0));
    Transaction send1 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 0)));
    Transaction send2 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 20)));
    wallet.commitTx(send1);
    assertPending(send1);
    Transaction send1b = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 50)));
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, send2);
    assertDead(send1);
    assertUnspent(send2);
    wallet.receivePending(send1b, null);
    assertDead(send1);
    assertUnspent(send2);
    assertDead(send1b);
  } finally {
    wallet.setCoinSelector(originalCoinSelector);
  }
}

代码示例来源:origin: greenaddress/GreenBits

@Test
public void doubleSpendForBuildingTx() throws Exception {
  CoinSelector originalCoinSelector = wallet.getCoinSelector();
  try {
    wallet.allowSpendingUnconfirmedTransactions();
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(2, 0));
    Transaction send1 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 0)));
    Transaction send2 = checkNotNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 20)));
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, send1);
    assertUnspent(send1);
    wallet.receivePending(send2, null);
    assertUnspent(send1);
    assertDead(send2);
  } finally {
    wallet.setCoinSelector(originalCoinSelector);
  }
}

代码示例来源:origin: greenaddress/GreenBits

@Test(expected = InsufficientMoneyException.class)
public void watchingScriptsConfirmed() throws Exception {
  Address watchedAddress = new ECKey().toAddress(PARAMS);
  wallet.addWatchedAddress(watchedAddress);
  sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, CENT, watchedAddress);
  assertEquals(CENT, wallet.getBalance());
  // We can't spend watched balances
  wallet.createSend(OTHER_ADDRESS, CENT);
}

代码示例来源:origin: greenaddress/GreenBits

Transaction tx3 = wallet.createSend(OTHER_ADDRESS, valueOf(0, 5));

代码示例来源:origin: greenaddress/GreenBits

Transaction send1 = checkNotNull(wallet.createSend(OTHER_ADDRESS, value2));
Transaction send2 = checkNotNull(wallet.createSend(OTHER_ADDRESS, value2));
byte[] buf = send1.bitcoinSerialize();
buf[43] = 0;  // Break the signature: bitcoinj won't check in SPV mode and this is easier than other mutations.
assertEquals(value, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
Transaction send3 = checkNotNull(wallet.createSend(OTHER_ADDRESS, value));
wallet.commitTx(send3);
assertEquals(ZERO, wallet.getBalance());

代码示例来源:origin: greenaddress/GreenBits

@Test
public void ageMattersDuringSelection() throws Exception {
  // Test that we prefer older coins to newer coins when building spends. This reduces required fees and improves
  // time to confirmation as the transaction will appear less spammy.
  final int ITERATIONS = 10;
  Transaction[] txns = new Transaction[ITERATIONS];
  for (int i = 0; i < ITERATIONS; i++) {
    txns[i] = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
  }
  // Check that we spend transactions in order of reception.
  for (int i = 0; i < ITERATIONS; i++) {
    Transaction spend = wallet.createSend(OTHER_ADDRESS, COIN);
    assertEquals(spend.getInputs().size(), 1);
    assertEquals("Failed on iteration " + i, spend.getInput(0).getOutpoint().getHash(), txns[i].getHash());
    wallet.commitTx(spend);
  }
}

代码示例来源:origin: greenaddress/GreenBits

@Test
public void orderingInsideBlock() throws Exception {
  // Test that transactions received in the same block have their ordering preserved when reorganising.
  // This covers issue 468.
  // Receive some money to the wallet.
  Transaction t1 = FakeTxBuilder.createFakeTx(PARAMS, COIN, coinsTo);
  final Block b1 = FakeTxBuilder.makeSolvedTestBlock(PARAMS.genesisBlock, t1);
  chain.add(b1);
  // Send a couple of payments one after the other (so the second depends on the change output of the first).
  wallet.allowSpendingUnconfirmedTransactions();
  Transaction t2 = checkNotNull(wallet.createSend(new ECKey().toAddress(PARAMS), CENT));
  wallet.commitTx(t2);
  Transaction t3 = checkNotNull(wallet.createSend(new ECKey().toAddress(PARAMS), CENT));
  wallet.commitTx(t3);
  chain.add(FakeTxBuilder.makeSolvedTestBlock(b1, t2, t3));
  final Coin coins0point98 = COIN.subtract(CENT).subtract(CENT);
  assertEquals(coins0point98, wallet.getBalance());
  // Now round trip the wallet and force a re-org.
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  wallet.saveToFileStream(bos);
  wallet = Wallet.loadFromFileStream(new ByteArrayInputStream(bos.toByteArray()));
  final Block b2 = FakeTxBuilder.makeSolvedTestBlock(b1, t2, t3);
  final Block b3 = FakeTxBuilder.makeSolvedTestBlock(b2);
  chain.add(b2);
  chain.add(b3);
  // And verify that the balance is as expected. Because new ECKey() is non-deterministic, if the order
  // isn't being stored correctly this should fail 50% of the time.
  assertEquals(coins0point98, wallet.getBalance());
}

代码示例来源:origin: greenaddress/GreenBits

@Test
public void bounce() throws Exception {
  // This test covers bug 64 (False double spends). Check that if we create a spend and it's immediately sent
  // back to us, this isn't considered as a double spend.
  Coin coin1 = COIN;
  sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, coin1);
  // Send half to some other guy. Sending only half then waiting for a confirm is important to ensure the tx is
  // in the unspent pool, not pending or spent.
  Coin coinHalf = valueOf(0, 50);
  assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
  assertEquals(1, wallet.getTransactions(true).size());
  Transaction outbound1 = wallet.createSend(OTHER_ADDRESS, coinHalf);
  wallet.commitTx(outbound1);
  sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, outbound1);
  assertTrue(outbound1.getWalletOutputs(wallet).size() <= 1); //the change address at most
  // That other guy gives us the coins right back.
  Transaction inbound2 = new Transaction(PARAMS);
  inbound2.addOutput(new TransactionOutput(PARAMS, inbound2, coinHalf, myAddress));
  assertTrue(outbound1.getWalletOutputs(wallet).size() >= 1);
  inbound2.addInput(outbound1.getOutputs().get(0));
  sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, inbound2);
  assertEquals(coin1, wallet.getBalance());
}

代码示例来源:origin: greenaddress/GreenBits

@Test
public void balances() throws Exception {
  Coin nanos = COIN;
  Transaction tx1 = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, nanos);
  assertEquals(nanos, tx1.getValueSentToMe(wallet));
  assertTrue(tx1.getWalletOutputs(wallet).size() >= 1);
  // Send 0.10 to somebody else.
  Transaction send1 = wallet.createSend(OTHER_ADDRESS, valueOf(0, 10));
  // Reserialize.
  Transaction send2 = PARAMS.getDefaultSerializer().makeTransaction(send1.bitcoinSerialize());
  assertEquals(nanos, send2.getValueSentFromMe(wallet));
  assertEquals(ZERO.subtract(valueOf(0, 10)), send2.getValue(wallet));
}

代码示例来源:origin: greenaddress/GreenBits

@Test
public void pubkeyOnlyScripts() throws Exception {
  // Verify that we support outputs like OP_PUBKEY and the corresponding inputs.
  ECKey key1 = wallet.freshReceiveKey();
  Coin value = valueOf(5, 0);
  Transaction t1 = createFakeTx(PARAMS, value, key1);
  if (wallet.isPendingTransactionRelevant(t1))
    wallet.receivePending(t1, null);
  // TX should have been seen as relevant.
  assertEquals(value, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
  assertEquals(ZERO, wallet.getBalance(Wallet.BalanceType.AVAILABLE));
  sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, t1);
  // TX should have been seen as relevant, extracted and processed.
  assertEquals(value, wallet.getBalance(Wallet.BalanceType.AVAILABLE));
  // Spend it and ensure we can spend the <key> OP_CHECKSIG output correctly.
  Transaction t2 = wallet.createSend(OTHER_ADDRESS, value);
  assertNotNull(t2);
  // TODO: This code is messy, improve the Script class and fixinate!
  assertEquals(t2.toString(), 1, t2.getInputs().get(0).getScriptSig().getChunks().size());
  assertTrue(t2.getInputs().get(0).getScriptSig().getChunks().get(0).data.length > 50);
}

代码示例来源:origin: greenaddress/GreenBits

@Test
public void spendToSameWallet() throws Exception {
  // Test that a spend to the same wallet is dealt with correctly.
  // It should appear in the wallet and confirm.
  // This is a bit of a silly thing to do in the real world as all it does is burn a fee but it is perfectly valid.
  Coin coin1 = COIN;
  Coin coinHalf = valueOf(0, 50);
  // Start by giving us 1 coin.
  sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, coin1);
  // Send half to ourselves. We should then have a balance available to spend of zero.
  assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
  assertEquals(1, wallet.getTransactions(true).size());
  Transaction outbound1 = wallet.createSend(myAddress, coinHalf);
  wallet.commitTx(outbound1);
  // We should have a zero available balance before the next block.
  assertEquals(ZERO, wallet.getBalance());
  sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, outbound1);
  // We should have a balance of 1 BTC after the block is received.
  assertEquals(coin1, wallet.getBalance());
}

代码示例来源:origin: greenaddress/GreenBits

@Test
public void doubleSpendUnspendsOtherInputs() throws Exception {
  // Test another Finney attack, but this time the killed transaction was also spending some other outputs in
  // our wallet which were not themselves double spent. This test ensures the death of the pending transaction
  // frees up the other outputs and makes them spendable again.
  // Receive 1 coin and then 2 coins in separate transactions.
  sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, COIN);
  sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(2, 0));
  // Create a send to a merchant of all our coins.
  Transaction send1 = wallet.createSend(OTHER_ADDRESS, valueOf(2, 90));
  // Create a double spend of just the first one.
  Address BAD_GUY = new ECKey().toAddress(PARAMS);
  Transaction send2 = wallet.createSend(BAD_GUY, COIN);
  send2 = PARAMS.getDefaultSerializer().makeTransaction(send2.bitcoinSerialize());
  // Broadcast send1, it's now pending.
  wallet.commitTx(send1);
  assertEquals(ZERO, wallet.getBalance()); // change of 10 cents is not yet mined so not included in the balance.
  // Receive a block that overrides the send1 using send2.
  sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, send2);
  // send1 got rolled back and replaced with a smaller send that only used one of our received coins, thus ...
  assertEquals(valueOf(2, 0), wallet.getBalance());
  assertTrue(wallet.isConsistent());
}

代码示例来源:origin: greenaddress/GreenBits

Transaction t2 = wallet.createSend(OTHER_ADDRESS, halfNanos);

代码示例来源:origin: greenaddress/GreenBits

chain.add(b1);
Transaction t1 = wallet.createSend(someOtherGuy, valueOf(10, 0));
Address yetAnotherGuy = new ECKey().toAddress(PARAMS);
Transaction t2 = wallet.createSend(yetAnotherGuy, valueOf(20, 0));
wallet.commitTx(t1);

代码示例来源:origin: greenaddress/GreenBits

@Test
public void balance() throws Exception {
  // Receive 5 coins then half a coin.
  Coin v1 = valueOf(5, 0);
  Coin v2 = valueOf(0, 50);
  Coin expected = valueOf(5, 50);
  assertEquals(0, wallet.getTransactions(true).size());
  sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, v1);
  assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
  sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, v2);
  assertEquals(2, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
  assertEquals(expected, wallet.getBalance());
  // Now spend one coin.
  Coin v3 = COIN;
  Transaction spend = wallet.createSend(OTHER_ADDRESS, v3);
  wallet.commitTx(spend);
  assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
  // Available and estimated balances should not be the same. We don't check the exact available balance here
  // because it depends on the coin selection algorithm.
  assertEquals(valueOf(4, 50), wallet.getBalance(Wallet.BalanceType.ESTIMATED));
  assertFalse(wallet.getBalance(Wallet.BalanceType.AVAILABLE).equals(
        wallet.getBalance(Wallet.BalanceType.ESTIMATED)));
  // Now confirm the transaction by including it into a block.
  sendMoneyToWallet(BlockChain.NewBlockType.BEST_CHAIN, spend);
  // Change is confirmed. We started with 5.50 so we should have 4.50 left.
  Coin v4 = valueOf(4, 50);
  assertEquals(v4, wallet.getBalance(Wallet.BalanceType.AVAILABLE));
}

代码示例来源:origin: greenaddress/GreenBits

assertEquals(FIFTY_COINS, wallet.getBalance());
Address dest = new ECKey().toAddress(PARAMS);
Transaction spend = wallet.createSend(dest, valueOf(10, 0));
wallet.commitTx(spend);

代码示例来源:origin: greenaddress/GreenBits

.isGreaterThan(txCoin.getOutput(0).getValue().multiply(txCoin.getConfidence().getDepthInBlocks())));
Transaction spend1 = wallet.createSend(OTHER_ADDRESS, CENT);
assertEquals(1, spend1.getInputs().size());
assertEquals(CENT, spend1.getInput(0).getValue());
    txCoin.getOutput(0).getValue().multiply(txCoin.getConfidence().getDepthInBlocks()));
Transaction spend2 = wallet.createSend(OTHER_ADDRESS, COIN);
assertEquals(1, spend2.getInputs().size());
assertEquals(COIN, spend2.getInput(0).getValue());
    .isLessThan(txCoin.getOutput(0).getValue().multiply(txCoin.getConfidence().getDepthInBlocks())));
Transaction spend3 = wallet.createSend(OTHER_ADDRESS, COIN);
assertEquals(1, spend3.getInputs().size());
assertEquals(COIN, spend3.getInput(0).getValue());

代码示例来源:origin: greenaddress/GreenBits

assertEquals(FIFTY_COINS, wallet.getBalance());
Address dest = new ECKey().toAddress(PARAMS);
Transaction spend = wallet.createSend(dest, FIFTY_COINS);

相关文章

微信公众号

最新文章

更多

Wallet类方法