Java 类org.bitcoinj.core.Wallet.SendRequest 实例源码

项目:namecoinj    文件:WalletTest.java   
private void spendUnconfirmedChange(Wallet wallet, Transaction t2, KeyParameter aesKey) throws Exception {
    if (wallet.getTransactionSigners().size() == 1)   // don't bother reconfiguring the p2sh wallet
        wallet = roundTrip(wallet);
    Coin v3 = valueOf(0, 49);
    assertEquals(v3, wallet.getBalance());
    Wallet.SendRequest req = Wallet.SendRequest.to(new ECKey().toAddress(params), valueOf(0, 48));
    req.aesKey = aesKey;
    req.ensureMinRequiredFee = false;
    req.shuffleOutputs = false;
    wallet.completeTx(req);
    Transaction t3 = req.tx;
    assertNotEquals(t2.getOutput(1).getScriptPubKey().getToAddress(params),
                    t3.getOutput(1).getScriptPubKey().getToAddress(params));
    assertNotNull(t3);
    wallet.commitTx(t3);
    assertTrue(wallet.isConsistent());
    // t2 and t3 gets confirmed in the same block.
    BlockPair bp = createFakeBlock(blockStore, t2, t3);
    wallet.receiveFromBlock(t2, bp.storedBlock, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
    wallet.receiveFromBlock(t3, bp.storedBlock, AbstractBlockChain.NewBlockType.BEST_CHAIN, 1);
    wallet.notifyNewBestBlock(bp.storedBlock);
    assertTrue(wallet.isConsistent());
}
项目:namecoinj    文件:WalletTest.java   
@Test
public void testCategory2WithChange() throws Exception {
    // Specifically target case 2 with significant change

    // Make sure TestWithWallet isnt doing anything crazy.
    assertEquals(0, wallet.getTransactions(true).size());

    Address notMyAddr = new ECKey().toAddress(params);

    // Generate a ton of small outputs
    StoredBlock block = new StoredBlock(makeSolvedTestBlock(blockStore, notMyAddr), BigInteger.ONE, 1);
    int i = 0;
    while (i <= CENT.divide(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.multiply(10))) {
        Transaction tx = createFakeTxWithChangeAddress(params, Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.multiply(10), myAddress, notMyAddr);
        tx.getInput(0).setSequenceNumber(i++); // Keep every transaction unique
        wallet.receiveFromBlock(tx, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, i);
    }

    // The selector will choose 2 with MIN_TX_FEE fee
    SendRequest request1 = SendRequest.to(notMyAddr, CENT.add(SATOSHI));
    wallet.completeTx(request1);
    assertEquals(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE, request1.tx.getFee());
    assertEquals(request1.tx.getInputs().size(), i); // We should have spent all inputs
    assertEquals(2, request1.tx.getOutputs().size()); // and gotten change back
}
项目:namecoinj    文件:WalletTest.java   
@Test
public void lowerThanDefaultFee() throws InsufficientMoneyException {
    Coin fee = Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.divide(10);
    receiveATransactionAmount(wallet, myAddress, Coin.COIN);
    SendRequest req = SendRequest.to(myAddress, Coin.CENT);
    req.feePerKb = fee;
    wallet.completeTx(req);
    assertEquals(fee, req.tx.getFee());
    wallet.commitTx(req.tx);
    SendRequest emptyReq = SendRequest.emptyWallet(myAddress);
    emptyReq.feePerKb = fee;
    emptyReq.emptyWallet = true;
    emptyReq.coinSelector = AllowUnconfirmedCoinSelector.get();
    wallet.completeTx(emptyReq);
    assertEquals(fee, emptyReq.tx.getFee());
    wallet.commitTx(emptyReq.tx);
}
项目:namecoinj    文件:WalletTest.java   
@Test
public void higherThanDefaultFee() throws InsufficientMoneyException {
    Coin fee = Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.multiply(10);
    receiveATransactionAmount(wallet, myAddress, Coin.COIN);
    SendRequest req = SendRequest.to(myAddress, Coin.CENT);
    req.feePerKb = fee;
    wallet.completeTx(req);
    assertEquals(fee, req.tx.getFee());
    wallet.commitTx(req.tx);
    SendRequest emptyReq = SendRequest.emptyWallet(myAddress);
    emptyReq.feePerKb = fee;
    emptyReq.emptyWallet = true;
    emptyReq.coinSelector = AllowUnconfirmedCoinSelector.get();
    wallet.completeTx(emptyReq);
    assertEquals(fee, emptyReq.tx.getFee());
    wallet.commitTx(emptyReq.tx);
}
项目:namecoinj    文件:WalletTest.java   
public void completeTxPartiallySignedMarried(Wallet.MissingSigsMode missSigMode, byte[] expectedSig) throws Exception {
    // create married wallet without signer
    createMarriedWallet(2, 2, false);
    myAddress = wallet.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    sendMoneyToWallet(wallet, COIN, myAddress, AbstractBlockChain.NewBlockType.BEST_CHAIN);

    ECKey dest = new ECKey();
    Wallet.SendRequest req = Wallet.SendRequest.emptyWallet(dest.toAddress(params));
    req.missingSigsMode = missSigMode;
    wallet.completeTx(req);
    TransactionInput input = req.tx.getInput(0);

    boolean firstSigIsMissing = Arrays.equals(expectedSig, input.getScriptSig().getChunks().get(1).data);
    boolean secondSigIsMissing = Arrays.equals(expectedSig, input.getScriptSig().getChunks().get(2).data);

    assertTrue("Only one of the signatures should be missing/dummy", firstSigIsMissing ^ secondSigIsMissing);
    int localSigIndex = firstSigIsMissing ? 2 : 1;
    int length = input.getScriptSig().getChunks().get(localSigIndex).data.length;
    assertTrue("Local sig should be present: " + length, length > 70);
}
项目:CoinJoin    文件:WalletTest.java   
private void spendUnconfirmedChange(Wallet wallet, Transaction t2, KeyParameter aesKey) throws Exception {
    Coin v3 = valueOf(0, 49);
    assertEquals(v3, wallet.getBalance());
    Wallet.SendRequest req = Wallet.SendRequest.to(new ECKey().toAddress(params), valueOf(0, 48));
    req.aesKey = aesKey;
    Address a = req.changeAddress = new ECKey().toAddress(params);
    req.ensureMinRequiredFee = false;
    req.shuffleOutputs = false;
    wallet.completeTx(req);
    Transaction t3 = req.tx;
    assertEquals(a, t3.getOutput(1).getScriptPubKey().getToAddress(params));
    assertNotNull(t3);
    wallet.commitTx(t3);
    assertTrue(wallet.isConsistent());
    // t2 and t3 gets confirmed in the same block.
    BlockPair bp = createFakeBlock(blockStore, t2, t3);
    wallet.receiveFromBlock(t2, bp.storedBlock, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
    wallet.receiveFromBlock(t3, bp.storedBlock, AbstractBlockChain.NewBlockType.BEST_CHAIN, 1);
    wallet.notifyNewBestBlock(bp.storedBlock);
    assertTrue(wallet.isConsistent());
}
项目:CoinJoin    文件:WalletTest.java   
@Test
public void testCategory2WithChange() throws Exception {
    // Specifically target case 2 with significant change

    // Make sure TestWithWallet isnt doing anything crazy.
    assertEquals(0, wallet.getTransactions(true).size());

    Address notMyAddr = new ECKey().toAddress(params);

    // Generate a ton of small outputs
    StoredBlock block = new StoredBlock(makeSolvedTestBlock(blockStore, notMyAddr), BigInteger.ONE, 1);
    int i = 0;
    while (i <= CENT.divide(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.multiply(10))) {
        Transaction tx = createFakeTxWithChangeAddress(params, Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.multiply(10), myAddress, notMyAddr);
        tx.getInput(0).setSequenceNumber(i++); // Keep every transaction unique
        wallet.receiveFromBlock(tx, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, i);
    }

    // The selector will choose 2 with MIN_TX_FEE fee
    SendRequest request1 = SendRequest.to(notMyAddr, CENT.add(SATOSHI));
    wallet.completeTx(request1);
    assertEquals(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE, request1.tx.getFee());
    assertEquals(request1.tx.getInputs().size(), i); // We should have spent all inputs
    assertEquals(2, request1.tx.getOutputs().size()); // and gotten change back
}
项目:CoinJoin    文件:WalletTest.java   
public void completeTxPartiallySignedMarried(Wallet.MissingSigsMode missSigMode, byte[] expectedSig) throws Exception {
    // create married wallet without signer
    createMarriedWallet(2, 2, false);
    myAddress = wallet.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    sendMoneyToWallet(wallet, COIN, myAddress, AbstractBlockChain.NewBlockType.BEST_CHAIN);

    ECKey dest = new ECKey();
    Wallet.SendRequest req = Wallet.SendRequest.emptyWallet(dest.toAddress(params));
    req.missingSigsMode = missSigMode;
    wallet.completeTx(req);
    TransactionInput input = req.tx.getInput(0);

    boolean firstSigIsMissing = Arrays.equals(expectedSig, input.getScriptSig().getChunks().get(1).data);
    boolean secondSigIsMissing = Arrays.equals(expectedSig, input.getScriptSig().getChunks().get(2).data);

    assertTrue("Only one of the signatures should be missing/dummy", firstSigIsMissing ^ secondSigIsMissing);
    int localSigIndex = firstSigIsMissing ? 2 : 1;
    int length = input.getScriptSig().getChunks().get(localSigIndex).data.length;
    assertTrue("Local sig should be present: " + length, length > 70);
}
项目:digibytej-alice    文件:WalletTest.java   
private void spendUnconfirmedChange(Wallet wallet, Transaction t2, KeyParameter aesKey) throws Exception {
    if (wallet.getTransactionSigners().size() == 1)   // don't bother reconfiguring the p2sh wallet
        wallet = roundTrip(wallet);
    Coin v3 = valueOf(0, 49);
    assertEquals(v3, wallet.getBalance());
    Wallet.SendRequest req = Wallet.SendRequest.to(new ECKey().toAddress(params), valueOf(0, 48));
    req.aesKey = aesKey;
    req.ensureMinRequiredFee = false;
    req.shuffleOutputs = false;
    wallet.completeTx(req);
    Transaction t3 = req.tx;
    assertNotEquals(t2.getOutput(1).getScriptPubKey().getToAddress(params),
                    t3.getOutput(1).getScriptPubKey().getToAddress(params));
    assertNotNull(t3);
    wallet.commitTx(t3);
    assertTrue(wallet.isConsistent());
    // t2 and t3 gets confirmed in the same block.
    BlockPair bp = createFakeBlock(blockStore, t2, t3);
    wallet.receiveFromBlock(t2, bp.storedBlock, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);
    wallet.receiveFromBlock(t3, bp.storedBlock, AbstractBlockChain.NewBlockType.BEST_CHAIN, 1);
    wallet.notifyNewBestBlock(bp.storedBlock);
    assertTrue(wallet.isConsistent());
}
项目:digibytej-alice    文件:WalletTest.java   
@Test
public void testCategory2WithChange() throws Exception {
    // Specifically target case 2 with significant change

    // Make sure TestWithWallet isnt doing anything crazy.
    assertEquals(0, wallet.getTransactions(true).size());

    Address notMyAddr = new ECKey().toAddress(params);

    // Generate a ton of small outputs
    StoredBlock block = new StoredBlock(makeSolvedTestBlock(blockStore, notMyAddr), BigInteger.ONE, 1);
    int i = 0;
    while (i <= CENT.divide(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.multiply(10))) {
        Transaction tx = createFakeTxWithChangeAddress(params, Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.multiply(10), myAddress, notMyAddr);
        tx.getInput(0).setSequenceNumber(i++); // Keep every transaction unique
        wallet.receiveFromBlock(tx, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, i);
    }

    // The selector will choose 2 with MIN_TX_FEE fee
    SendRequest request1 = SendRequest.to(notMyAddr, CENT.add(SATOSHI));
    wallet.completeTx(request1);
    assertEquals(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE, request1.tx.getFee());
    assertEquals(request1.tx.getInputs().size(), i); // We should have spent all inputs
    assertEquals(2, request1.tx.getOutputs().size()); // and gotten change back
}
项目:digibytej-alice    文件:WalletTest.java   
@Test
public void lowerThanDefaultFee() throws InsufficientMoneyException {
    Coin fee = Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.divide(10);
    receiveATransactionAmount(wallet, myAddress, Coin.COIN);
    SendRequest req = SendRequest.to(myAddress, Coin.CENT);
    req.feePerKb = fee;
    wallet.completeTx(req);
    assertEquals(fee, req.tx.getFee());
    wallet.commitTx(req.tx);
    SendRequest emptyReq = SendRequest.emptyWallet(myAddress);
    emptyReq.feePerKb = fee;
    emptyReq.emptyWallet = true;
    emptyReq.coinSelector = AllowUnconfirmedCoinSelector.get();
    wallet.completeTx(emptyReq);
    assertEquals(fee, emptyReq.tx.getFee());
    wallet.commitTx(emptyReq.tx);
}
项目:digibytej-alice    文件:WalletTest.java   
@Test
public void higherThanDefaultFee() throws InsufficientMoneyException {
    Coin fee = Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.multiply(10);
    receiveATransactionAmount(wallet, myAddress, Coin.COIN);
    SendRequest req = SendRequest.to(myAddress, Coin.CENT);
    req.feePerKb = fee;
    wallet.completeTx(req);
    assertEquals(fee, req.tx.getFee());
    wallet.commitTx(req.tx);
    SendRequest emptyReq = SendRequest.emptyWallet(myAddress);
    emptyReq.feePerKb = fee;
    emptyReq.emptyWallet = true;
    emptyReq.coinSelector = AllowUnconfirmedCoinSelector.get();
    wallet.completeTx(emptyReq);
    assertEquals(fee, emptyReq.tx.getFee());
    wallet.commitTx(emptyReq.tx);
}
项目:digibytej-alice    文件:WalletTest.java   
public void completeTxPartiallySignedMarried(Wallet.MissingSigsMode missSigMode, byte[] expectedSig) throws Exception {
    // create married wallet without signer
    createMarriedWallet(2, 2, false);
    myAddress = wallet.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    sendMoneyToWallet(wallet, COIN, myAddress, AbstractBlockChain.NewBlockType.BEST_CHAIN);

    ECKey dest = new ECKey();
    Wallet.SendRequest req = Wallet.SendRequest.emptyWallet(dest.toAddress(params));
    req.missingSigsMode = missSigMode;
    wallet.completeTx(req);
    TransactionInput input = req.tx.getInput(0);

    boolean firstSigIsMissing = Arrays.equals(expectedSig, input.getScriptSig().getChunks().get(1).data);
    boolean secondSigIsMissing = Arrays.equals(expectedSig, input.getScriptSig().getChunks().get(2).data);

    assertTrue("Only one of the signatures should be missing/dummy", firstSigIsMissing ^ secondSigIsMissing);
    int localSigIndex = firstSigIsMissing ? 2 : 1;
    int length = input.getScriptSig().getChunks().get(localSigIndex).data.length;
    assertTrue("Local sig should be present: " + length, length > 70);
}
项目:ombuds-android    文件:PaymentIntent.java   
public SendRequest toSendRequest()
{
    final Transaction transaction = new Transaction(Constants.NETWORK_PARAMETERS);
    for (final PaymentIntent.Output output : outputs)
        transaction.addOutput(output.amount, output.script);
    return SendRequest.forTx(transaction);
}
项目:ombuds-android    文件:RaiseFeeDialogFragment.java   
private void doRaiseFee(final KeyParameter encryptionKey)
{
    // construct child-pays-for-parent
    final TransactionOutput outputToSpend = checkNotNull(findSpendableOutput(wallet, transaction));
    final Transaction transactionToSend = new Transaction(Constants.NETWORK_PARAMETERS);
    transactionToSend.addInput(outputToSpend);
    transactionToSend.addOutput(outputToSpend.getValue().subtract(FEE_RAISE), wallet.freshAddress(KeyPurpose.CHANGE));
    transactionToSend.setPurpose(Transaction.Purpose.RAISE_FEE);

    final SendRequest sendRequest = SendRequest.forTx(transactionToSend);
    sendRequest.aesKey = encryptionKey;

    try
    {
        wallet.signTransaction(sendRequest);

        log.info("raise fee: cpfp {}", transactionToSend);

        wallet.commitTx(transactionToSend);
        application.broadcastTransaction(transactionToSend);

        state = State.DONE;
        updateView();

        dismiss();
    }
    catch (final KeyCrypterException x)
    {
        badPasswordView.setVisibility(View.VISIBLE);

        state = State.INPUT;
        updateView();

        passwordView.requestFocus();

        log.info("raise fee: bad spending password");
    }
}
项目:namecoinj    文件:WalletTest.java   
private Transaction cleanupCommon(Address destination) throws Exception {
    receiveATransaction(wallet, myAddress);

    Coin v2 = valueOf(0, 50);
    SendRequest req = SendRequest.to(destination, v2);
    req.fee = CENT;
    wallet.completeTx(req);

    Transaction t2 = req.tx;

    // Broadcast the transaction and commit.
    broadcastAndCommit(wallet, t2);

    // At this point we have one pending and one spent

    Coin v1 = valueOf(0, 10);
    Transaction t = sendMoneyToWallet(wallet, v1, myAddress, null);
    Threading.waitForUserCode();
    sendMoneyToWallet(wallet, t, null);
    assertEquals("Wrong number of PENDING.4", 2, wallet.getPoolSize(Pool.PENDING));
    assertEquals("Wrong number of UNSPENT.4", 0, wallet.getPoolSize(Pool.UNSPENT));
    assertEquals("Wrong number of ALL.4", 3, wallet.getTransactions(true).size());
    assertEquals(valueOf(0, 59), wallet.getBalance(Wallet.BalanceType.ESTIMATED));

    // Now we have another incoming pending
    return t;
}
项目:namecoinj    文件:WalletTest.java   
@Test
public void cleanupFailsDueToSpend() throws Exception {
    Address destination = new ECKey().toAddress(params);
    Transaction t = cleanupCommon(destination);

    // Now we have another incoming pending.  Spend everything.
    Coin v3 = valueOf(0, 58);
    SendRequest req = SendRequest.to(destination, v3);

    // Force selection of the incoming coin so that we can spend it
    req.coinSelector = new TestCoinSelector();

    req.fee = CENT;
    wallet.completeTx(req);
    wallet.commitTx(req.tx);

    assertEquals("Wrong number of PENDING.5", 3, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals("Wrong number of UNSPENT.5", 0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals("Wrong number of ALL.5", 4, wallet.getTransactions(true).size());

    // Consider the new pending as risky and try to remove it from the wallet
    wallet.setRiskAnalyzer(new TestRiskAnalysis.Analyzer(t));

    wallet.cleanup();
    assertTrue(wallet.isConsistent());

    // The removal should have failed
    assertEquals("Wrong number of PENDING.5", 3, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals("Wrong number of UNSPENT.5", 0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals("Wrong number of ALL.5", 4, wallet.getTransactions(true).size());
    assertEquals(ZERO, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
}
项目:namecoinj    文件:WalletTest.java   
@Test
@SuppressWarnings("deprecation")
// Having a test for deprecated method getFromAddress() is no evil so we suppress the warning here.
public void customTransactionSpending() throws Exception {
    // We'll set up a wallet that receives a coin, then sends a coin of lesser value and keeps the change.
    Coin v1 = valueOf(3, 0);
    sendMoneyToWallet(v1, AbstractBlockChain.NewBlockType.BEST_CHAIN);
    assertEquals(v1, wallet.getBalance());
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals(1, wallet.getTransactions(true).size());

    ECKey k2 = new ECKey();
    Address a2 = k2.toAddress(params);
    Coin v2 = valueOf(0, 50);
    Coin v3 = valueOf(0, 75);
    Coin v4 = valueOf(1, 25);

    Transaction t2 = new Transaction(params);
    t2.addOutput(v2, a2);
    t2.addOutput(v3, a2);
    t2.addOutput(v4, a2);
    SendRequest req = SendRequest.forTx(t2);
    req.ensureMinRequiredFee = false;
    wallet.completeTx(req);

    // Do some basic sanity checks.
    assertEquals(1, t2.getInputs().size());
    assertEquals(myAddress, t2.getInput(0).getScriptSig().getFromAddress(params));
    assertEquals(TransactionConfidence.ConfidenceType.UNKNOWN, t2.getConfidence().getConfidenceType());

    // We have NOT proven that the signature is correct!
    wallet.commitTx(t2);
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.SPENT));
    assertEquals(2, wallet.getTransactions(true).size());
}
项目:namecoinj    文件:WalletTest.java   
@Test
public void spendOutputFromPendingTransaction() throws Exception {
    // We'll set up a wallet that receives a coin, then sends a coin of lesser value and keeps the change.
    Coin v1 = COIN;
    sendMoneyToWallet(v1, AbstractBlockChain.NewBlockType.BEST_CHAIN);
    // First create our current transaction
    ECKey k2 = wallet.freshReceiveKey();
    Coin v2 = valueOf(0, 50);
    Transaction t2 = new Transaction(params);
    TransactionOutput o2 = new TransactionOutput(params, t2, v2, k2.toAddress(params));
    t2.addOutput(o2);
    SendRequest req = SendRequest.forTx(t2);
    req.ensureMinRequiredFee = false;
    wallet.completeTx(req);

    // Commit t2, so it is placed in the pending pool
    wallet.commitTx(t2);
    assertEquals(0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals(2, wallet.getTransactions(true).size());

    // Now try to the spend the output.
    ECKey k3 = new ECKey();
    Coin v3 = valueOf(0, 25);
    Transaction t3 = new Transaction(params);
    t3.addOutput(v3, k3.toAddress(params));
    t3.addInput(o2);
    wallet.signTransaction(SendRequest.forTx(t3));

    // Commit t3, so the coins from the pending t2 are spent
    wallet.commitTx(t3);
    assertEquals(0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals(2, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals(3, wallet.getTransactions(true).size());

    // Now the output of t2 must not be available for spending
    assertFalse(o2.isAvailableForSpending());
}
项目:namecoinj    文件:WalletTest.java   
@Test
public void importAndEncrypt() throws InsufficientMoneyException {
    final ECKey key = new ECKey();
    encryptedWallet.importKeysAndEncrypt(ImmutableList.of(key), PASSWORD1);
    assertEquals(1, encryptedWallet.getImportedKeys().size());
    assertEquals(key.getPubKeyPoint(), encryptedWallet.getImportedKeys().get(0).getPubKeyPoint());
    sendMoneyToWallet(encryptedWallet, Coin.COIN, key.toAddress(params), AbstractBlockChain.NewBlockType.BEST_CHAIN);
    assertEquals(Coin.COIN, encryptedWallet.getBalance());
    SendRequest req = Wallet.SendRequest.emptyWallet(new ECKey().toAddress(params));
    req.aesKey = checkNotNull(encryptedWallet.getKeyCrypter()).deriveKey(PASSWORD1);
    encryptedWallet.sendCoinsOffline(req);
}
项目:namecoinj    文件:WalletTest.java   
@Test(expected = Wallet.ExceededMaxTransactionSize.class)
public void respectMaxStandardSize() throws Exception {
    // Check that we won't create txns > 100kb. Average tx size is ~220 bytes so this would have to be enormous.
    sendMoneyToWallet(valueOf(100, 0), AbstractBlockChain.NewBlockType.BEST_CHAIN);
    Transaction tx = new Transaction(params);
    byte[] bits = new byte[20];
    new Random().nextBytes(bits);
    Coin v = CENT;
    // 3100 outputs to a random address.
    for (int i = 0; i < 3100; i++) {
        tx.addOutput(v, new Address(params, bits));
    }
    Wallet.SendRequest req = Wallet.SendRequest.forTx(tx);
    wallet.completeTx(req);
}
项目:namecoinj    文件:WalletTest.java   
@Test
public void opReturnOneOutputTest() throws Exception {
    // Tests basic send of transaction with one output that doesn't transfer any value but just writes OP_RETURN.
    receiveATransaction(wallet, myAddress);
    Transaction tx = new Transaction(params);
    Coin messagePrice = Coin.ZERO;
    Script script = ScriptBuilder.createOpReturnScript("hello world!".getBytes());
    tx.addOutput(messagePrice, script);
    SendRequest request = Wallet.SendRequest.forTx(tx);
    wallet.completeTx(request);
}
项目:namecoinj    文件:WalletTest.java   
@Test
public void opReturnOneOutputWithValueTest() throws Exception {
    // Tests basic send of transaction with one output that destroys coins and has an OP_RETURN.
    receiveATransaction(wallet, myAddress);
    Transaction tx = new Transaction(params);
    Coin messagePrice = CENT;
    Script script = ScriptBuilder.createOpReturnScript("hello world!".getBytes());
    tx.addOutput(messagePrice, script);
    SendRequest request = Wallet.SendRequest.forTx(tx);
    wallet.completeTx(request);
}
项目:namecoinj    文件:WalletTest.java   
@Test
public void opReturnTwoOutputsTest() throws Exception {
    // Tests sending transaction where one output transfers BTC, the other one writes OP_RETURN.
    receiveATransaction(wallet, myAddress);
    Address notMyAddr = new ECKey().toAddress(params);
    Transaction tx = new Transaction(params);
    Coin messagePrice = Coin.ZERO;
    Script script = ScriptBuilder.createOpReturnScript("hello world!".getBytes());
    tx.addOutput(CENT, notMyAddr);
    tx.addOutput(messagePrice, script);
    SendRequest request = Wallet.SendRequest.forTx(tx);
    wallet.completeTx(request);
}
项目:namecoinj    文件:WalletTest.java   
@Test(expected = Wallet.MultipleOpReturnRequested.class)
public void twoOpReturnsPerTransactionTest() throws Exception {
    // Tests sending transaction where there are 2 attempts to write OP_RETURN scripts - this should fail and throw MultipleOpReturnRequested.
    receiveATransaction(wallet, myAddress);
    Transaction tx = new Transaction(params);
    Coin messagePrice = Coin.ZERO;
    Script script1 = ScriptBuilder.createOpReturnScript("hello world 1!".getBytes());
    Script script2 = ScriptBuilder.createOpReturnScript("hello world 2!".getBytes());
    tx.addOutput(messagePrice, script1);
    tx.addOutput(messagePrice, script2);
    SendRequest request = Wallet.SendRequest.forTx(tx);
    wallet.completeTx(request);
}
项目:namecoinj    文件:WalletTest.java   
@Test(expected = Wallet.DustySendRequested.class)
public void sendDustTest() throws InsufficientMoneyException {
    // Tests sending dust, should throw DustySendRequested.
    Transaction tx = new Transaction(params);
    Address notMyAddr = new ECKey().toAddress(params);
    tx.addOutput(Transaction.MIN_NONDUST_OUTPUT.subtract(SATOSHI), notMyAddr);
    SendRequest request = Wallet.SendRequest.forTx(tx);
    wallet.completeTx(request);
}
项目:namecoinj    文件:WalletTest.java   
@Test
public void sendMultipleCentsTest() throws Exception {
    receiveATransactionAmount(wallet, myAddress, Coin.COIN);
    Transaction tx = new Transaction(params);
    Address notMyAddr = new ECKey().toAddress(params);
    tx.addOutput(COIN.CENT.subtract(SATOSHI), notMyAddr);
    tx.addOutput(COIN.CENT.subtract(SATOSHI), notMyAddr);
    tx.addOutput(COIN.CENT.subtract(SATOSHI), notMyAddr);
    tx.addOutput(COIN.CENT.subtract(SATOSHI), notMyAddr);
    SendRequest request = Wallet.SendRequest.forTx(tx);
    wallet.completeTx(request);
}
项目:namecoinj    文件:WalletTest.java   
@Test(expected = Wallet.DustySendRequested.class)
public void sendDustAndOpReturnWithoutValueTest() throws Exception {
    // Tests sending dust and OP_RETURN without value, should throw DustySendRequested because sending sending dust is not allowed in any case.
    receiveATransactionAmount(wallet, myAddress, Coin.COIN);
    Transaction tx = new Transaction(params);
    Address notMyAddr = new ECKey().toAddress(params);
    Script script = new ScriptBuilder().op(ScriptOpCodes.OP_RETURN).data("hello world!".getBytes()).build();
    tx.addOutput(Coin.ZERO, script);
    tx.addOutput(Coin.SATOSHI, notMyAddr);
    SendRequest request = Wallet.SendRequest.forTx(tx);
    wallet.completeTx(request);
}
项目:namecoinj    文件:WalletTest.java   
@Test(expected = Wallet.DustySendRequested.class)
public void sendDustAndMessageWithValueTest() throws Exception {
    //Tests sending dust and OP_RETURN with value, should throw DustySendRequested
    receiveATransaction(wallet, myAddress);
    Transaction tx = new Transaction(params);
    Address notMyAddr = new ECKey().toAddress(params);
    Script script = new ScriptBuilder().op(ScriptOpCodes.OP_RETURN).data("hello world!".getBytes()).build();
    tx.addOutput(Coin.CENT, script);
    tx.addOutput(Transaction.MIN_NONDUST_OUTPUT.subtract(SATOSHI), notMyAddr);
    SendRequest request = Wallet.SendRequest.forTx(tx);
    wallet.completeTx(request);
}
项目:namecoinj    文件:WalletTest.java   
@Test
public void transactionGetFeeTest() throws Exception {
    Address notMyAddr = new ECKey().toAddress(params);

    // Prepare wallet to spend
    StoredBlock block = new StoredBlock(makeSolvedTestBlock(blockStore, notMyAddr), BigInteger.ONE, 1);
    Transaction tx = createFakeTx(params, COIN, myAddress);
    wallet.receiveFromBlock(tx, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, 0);

    // Create a transaction
    SendRequest request = SendRequest.to(notMyAddr, CENT);
    request.feePerKb = Transaction.REFERENCE_DEFAULT_MIN_TX_FEE;
    wallet.completeTx(request);
    assertEquals(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE, request.tx.getFee());
}
项目:namecoinj    文件:WalletTest.java   
@Test
public void testEmptyRandomWallet() throws Exception {
    // Add a random set of outputs
    StoredBlock block = new StoredBlock(makeSolvedTestBlock(blockStore, new ECKey().toAddress(params)), BigInteger.ONE, 1);
    Random rng = new Random();
    for (int i = 0; i < rng.nextInt(100) + 1; i++) {
        Transaction tx = createFakeTx(params, Coin.valueOf(rng.nextInt((int) COIN.value)), myAddress);
        wallet.receiveFromBlock(tx, block, AbstractBlockChain.NewBlockType.BEST_CHAIN, i);
    }
    SendRequest request = SendRequest.emptyWallet(new ECKey().toAddress(params));
    wallet.completeTx(request);
    wallet.commitTx(request.tx);
    assertEquals(ZERO, wallet.getBalance());
}
项目:namecoinj    文件:WalletTest.java   
@Test (expected = TransactionSigner.MissingSignatureException.class)
public void completeTxPartiallySignedMarriedThrowsByDefault() throws Exception {
    createMarriedWallet(2, 2, false);
    myAddress = wallet.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    sendMoneyToWallet(wallet, COIN, myAddress, AbstractBlockChain.NewBlockType.BEST_CHAIN);

    Wallet.SendRequest req = Wallet.SendRequest.emptyWallet(new ECKey().toAddress(params));
    wallet.completeTx(req);
}
项目:namecoinj    文件:WalletTest.java   
@SuppressWarnings("ConstantConditions")
public void completeTxPartiallySigned(Wallet.MissingSigsMode missSigMode, byte[] expectedSig) throws Exception {
    // Check the wallet will write dummy scriptSigs for inputs that we have only pubkeys for without the privkey.
    ECKey priv = new ECKey();
    ECKey pub = ECKey.fromPublicOnly(priv.getPubKeyPoint());
    wallet.importKey(pub);
    ECKey priv2 = wallet.freshReceiveKey();
    // Send three transactions, with one being an address type and the other being a raw CHECKSIG type pubkey only,
    // and the final one being a key we do have. We expect the first two inputs to be dummy values and the last
    // to be signed correctly.
    Transaction t1 = sendMoneyToWallet(wallet, CENT, pub.toAddress(params), AbstractBlockChain.NewBlockType.BEST_CHAIN);
    Transaction t2 = sendMoneyToWallet(wallet, CENT, pub, AbstractBlockChain.NewBlockType.BEST_CHAIN);
    Transaction t3 = sendMoneyToWallet(wallet, CENT, priv2, AbstractBlockChain.NewBlockType.BEST_CHAIN);

    ECKey dest = new ECKey();
    Wallet.SendRequest req = Wallet.SendRequest.emptyWallet(dest.toAddress(params));
    req.missingSigsMode = missSigMode;
    wallet.completeTx(req);
    byte[] dummySig = TransactionSignature.dummy().encodeToBitcoin();
    // Selected inputs can be in any order.
    for (int i = 0; i < req.tx.getInputs().size(); i++) {
        TransactionInput input = req.tx.getInput(i);
        if (input.getConnectedOutput().getParentTransaction().equals(t1)) {
            assertArrayEquals(expectedSig, input.getScriptSig().getChunks().get(0).data);
        } else if (input.getConnectedOutput().getParentTransaction().equals(t2)) {
            assertArrayEquals(expectedSig, input.getScriptSig().getChunks().get(0).data);
        } else if (input.getConnectedOutput().getParentTransaction().equals(t3)) {
            input.getScriptSig().correctlySpends(req.tx, i, t3.getOutput(0).getScriptPubKey());
        }
    }
    assertTrue(TransactionSignature.isEncodingCanonical(dummySig));
}
项目:namecoinj    文件:WalletTest.java   
@Test
public void sendRequestExchangeRate() throws Exception {
    receiveATransaction(wallet, myAddress);
    SendRequest sendRequest = SendRequest.to(myAddress, Coin.COIN);
    sendRequest.exchangeRate = new ExchangeRate(Fiat.parseFiat("EUR", "500"));
    wallet.completeTx(sendRequest);
    assertEquals(sendRequest.exchangeRate, sendRequest.tx.getExchangeRate());
}
项目:namecoinj    文件:WalletTest.java   
@Test
public void sendRequestMemo() throws Exception {
    receiveATransaction(wallet, myAddress);
    SendRequest sendRequest = SendRequest.to(myAddress, Coin.COIN);
    sendRequest.memo = "memo";
    wallet.completeTx(sendRequest);
    assertEquals(sendRequest.memo, sendRequest.tx.getMemo());
}
项目:CoinJoin    文件:WalletTest.java   
private Transaction cleanupCommon(Address destination) throws Exception {
    receiveATransaction(wallet, myAddress);

    Coin v2 = valueOf(0, 50);
    SendRequest req = SendRequest.to(destination, v2);
    req.fee = CENT;
    wallet.completeTx(req);

    Transaction t2 = req.tx;

    // Broadcast the transaction and commit.
    broadcastAndCommit(wallet, t2);

    // At this point we have one pending and one spent

    Coin v1 = valueOf(0, 10);
    Transaction t = sendMoneyToWallet(wallet, v1, myAddress, null);
    Threading.waitForUserCode();
    sendMoneyToWallet(wallet, t, null);
    assertEquals("Wrong number of PENDING.4", 2, wallet.getPoolSize(Pool.PENDING));
    assertEquals("Wrong number of UNSPENT.4", 0, wallet.getPoolSize(Pool.UNSPENT));
    assertEquals("Wrong number of ALL.4", 3, wallet.getTransactions(true).size());
    assertEquals(valueOf(0, 59), wallet.getBalance(Wallet.BalanceType.ESTIMATED));

    // Now we have another incoming pending
    return t;
}
项目:CoinJoin    文件:WalletTest.java   
@Test
public void cleanupFailsDueToSpend() throws Exception {
    Address destination = new ECKey().toAddress(params);
    Transaction t = cleanupCommon(destination);

    // Now we have another incoming pending.  Spend everything.
    Coin v3 = valueOf(0, 58);
    SendRequest req = SendRequest.to(destination, v3);

    // Force selection of the incoming coin so that we can spend it
    req.coinSelector = new TestCoinSelector();

    req.fee = CENT;
    wallet.completeTx(req);
    wallet.commitTx(req.tx);

    assertEquals("Wrong number of PENDING.5", 3, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals("Wrong number of UNSPENT.5", 0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals("Wrong number of ALL.5", 4, wallet.getTransactions(true).size());

    // Consider the new pending as risky and try to remove it from the wallet
    wallet.setRiskAnalyzer(new TestRiskAnalysis.Analyzer(t));

    wallet.cleanup();
    assertTrue(wallet.isConsistent());

    // The removal should have failed
    assertEquals("Wrong number of PENDING.5", 3, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals("Wrong number of UNSPENT.5", 0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals("Wrong number of ALL.5", 4, wallet.getTransactions(true).size());
    assertEquals(ZERO, wallet.getBalance(Wallet.BalanceType.ESTIMATED));
}
项目:CoinJoin    文件:WalletTest.java   
@Test
@SuppressWarnings("deprecation")
// Having a test for deprecated method getFromAddress() is no evil so we suppress the warning here.
public void customTransactionSpending() throws Exception {
    // We'll set up a wallet that receives a coin, then sends a coin of lesser value and keeps the change.
    Coin v1 = valueOf(3, 0);
    sendMoneyToWallet(v1, AbstractBlockChain.NewBlockType.BEST_CHAIN);
    assertEquals(v1, wallet.getBalance());
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals(1, wallet.getTransactions(true).size());

    ECKey k2 = new ECKey();
    Address a2 = k2.toAddress(params);
    Coin v2 = valueOf(0, 50);
    Coin v3 = valueOf(0, 75);
    Coin v4 = valueOf(1, 25);

    Transaction t2 = new Transaction(params);
    t2.addOutput(v2, a2);
    t2.addOutput(v3, a2);
    t2.addOutput(v4, a2);
    SendRequest req = SendRequest.forTx(t2);
    req.ensureMinRequiredFee = false;
    wallet.completeTx(req);

    // Do some basic sanity checks.
    assertEquals(1, t2.getInputs().size());
    assertEquals(myAddress, t2.getInput(0).getScriptSig().getFromAddress(params));
    assertEquals(TransactionConfidence.ConfidenceType.UNKNOWN, t2.getConfidence().getConfidenceType());

    // We have NOT proven that the signature is correct!
    wallet.commitTx(t2);
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.SPENT));
    assertEquals(2, wallet.getTransactions(true).size());
}
项目:CoinJoin    文件:WalletTest.java   
@Test
public void spendOutputFromPendingTransaction() throws Exception {
    // We'll set up a wallet that receives a coin, then sends a coin of lesser value and keeps the change.
    Coin v1 = COIN;
    sendMoneyToWallet(v1, AbstractBlockChain.NewBlockType.BEST_CHAIN);
    // First create our current transaction
    ECKey k2 = wallet.freshReceiveKey();
    Coin v2 = valueOf(0, 50);
    Transaction t2 = new Transaction(params);
    TransactionOutput o2 = new TransactionOutput(params, t2, v2, k2.toAddress(params));
    t2.addOutput(o2);
    SendRequest req = SendRequest.forTx(t2);
    req.ensureMinRequiredFee = false;
    wallet.completeTx(req);

    // Commit t2, so it is placed in the pending pool
    wallet.commitTx(t2);
    assertEquals(0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals(2, wallet.getTransactions(true).size());

    // Now try to the spend the output.
    ECKey k3 = new ECKey();
    Coin v3 = valueOf(0, 25);
    Transaction t3 = new Transaction(params);
    t3.addOutput(v3, k3.toAddress(params));
    t3.addInput(o2);
    wallet.signTransaction(SendRequest.forTx(t3));

    // Commit t3, so the coins from the pending t2 are spent
    wallet.commitTx(t3);
    assertEquals(0, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals(2, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals(3, wallet.getTransactions(true).size());

    // Now the output of t2 must not be available for spending
    assertFalse(o2.isAvailableForSpending());
}
项目:CoinJoin    文件:WalletTest.java   
@Test
public void importAndEncrypt() throws IOException, InsufficientMoneyException {
    final ECKey key = new ECKey();
    encryptedWallet.importKeysAndEncrypt(ImmutableList.of(key), PASSWORD1);
    assertEquals(1, encryptedWallet.getImportedKeys().size());
    assertEquals(key.getPubKeyPoint(), encryptedWallet.getImportedKeys().get(0).getPubKeyPoint());
    sendMoneyToWallet(encryptedWallet, Coin.COIN, key.toAddress(params), AbstractBlockChain.NewBlockType.BEST_CHAIN);
    assertEquals(Coin.COIN, encryptedWallet.getBalance());
    SendRequest req = Wallet.SendRequest.emptyWallet(new ECKey().toAddress(params));
    req.aesKey = checkNotNull(encryptedWallet.getKeyCrypter()).deriveKey(PASSWORD1);
    encryptedWallet.sendCoinsOffline(req);
}