PledgingWallet extends Wallet { public PendingPledge createPledge(Project project, long satoshis, @Nullable KeyParameter aesKey) throws InsufficientMoneyException { return createPledge(project, Coin.valueOf(satoshis), aesKey, LHProtos.PledgeDetails.getDefaultInstance()); } PledgingWallet(NetworkParameters params, KeyChainGroup keyChainGroup);  PledgingWallet(NetworkParameters params); org.bitcoinj.wallet.Protos.Wallet serialize(); static PledgingWallet deserialize(org.bitcoinj.wallet.Protos.Wallet proto); PendingPledge createPledge(Project project, long satoshis, @Nullable KeyParameter aesKey); PendingPledge createPledge(Project project, Coin value, @Nullable KeyParameter aesKey,
                                      LHProtos.PledgeDetails details); @Nullable synchronized LHProtos.Pledge getPledgeFor(Project project); long getPledgedAmountFor(Project project); synchronized Set<LHProtos.Pledge> getPledges(); synchronized boolean wasPledgeRevoked(LHProtos.Pledge pledge); Revocation revokePledge(LHProtos.Pledge proto, @Nullable KeyParameter aesKey); void completeContractWithFee(Project project, Set<LHProtos.Pledge> pledges, @Nullable KeyParameter aesKey,
                                        TransactionBroadcast.ProgressCallback progress, Consumer<Throwable> error,
                                        Executor callbackExecutor); boolean isProjectMine(Project project); void addOnPledgeHandler(Executor executor, Function2<Project, LHProtos.Pledge, Unit> onPledgeHandler); void addOnRevokeHandler(Executor executor, Function1<LHProtos.Pledge, Unit> onRevokeHandler); void addOnClaimHandler(Executor executor, Function2<LHProtos.Pledge, Transaction, Unit> onClaimHandler); DeterministicKey freshAuthKey(); @Nullable DeterministicKey getAuthKeyFromIndexOrPubKey(byte[] pubkey, int index);  }
@Test public void pledgePerfectSize() throws Exception { WalletTestObjects objects = new WalletTestObjects(() -> new PledgingWallet(UnitTestParams.get())); PledgingWallet wallet = (PledgingWallet) objects.wallet; objects.sendAmounts(100_000, 200_000, 300_000); assertEquals(600_000, objects.wallet.getBalance().longValue()); boolean[] flags = new boolean[2]; wallet.addEventListener(new AbstractWalletEventListener() { @Override public void onChange() { flags[0] = true; } @Override public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) { flags[1] = true; } }, Threading.SAME_THREAD); Project project = new Project(makeProject(wallet, 100_000)); PledgingWallet.PendingPledge pledge = wallet.createPledge(project, 100_000, null); assertNull(pledge.dependency); assertNotNull(pledge.pledge); final LHProtos.Pledge commit = pledge.commit(true); assertEquals(500_000, objects.wallet.getBalance().longValue()); assertTrue(flags[0]); assertTrue(flags[1]); Transaction contract = project.completeContract(ImmutableSet.of(commit)); assertEquals(contract.getInput(0).duplicateDetached(), pledge.pledge.getInput(0).duplicateDetached()); List<TransactionOutput> outputs = ImmutableList.of(checkNotNull(pledge.pledge.getInput(0).getConnectedOutput()).duplicateDetached()); project.verifyPledge(outpoint -> CompletableFuture.completedFuture(outputs), commit).get(); }
@Test public void pledgeImperfectSize() throws Exception { WalletTestObjects objects = new WalletTestObjects(() -> new PledgingWallet(UnitTestParams.get())); PledgingWallet wallet = (PledgingWallet) objects.wallet; objects.sendAmounts(1_000_000, 2_000_000); Project project = new Project(makeProject(wallet, 3_000_000)); PledgingWallet.PendingPledge pledge = wallet.createPledge(project, 2_500_000, null); assertNotNull(pledge.dependency); assertNotNull(pledge.pledge); assertEquals(2, pledge.dependency.getOutputs().size()); assertTrue(2_500_000 == pledge.dependency.getOutput(0).getValue().longValue() || 2_500_000 == pledge.dependency.getOutput(1).getValue().longValue()); pledge.commit(true); PledgingWallet.PendingPledge pledge2 = wallet.createPledge(project, 400_000, null); assertNotNull(pledge2.dependency); assertNotNull(pledge2.pledge); assertEquals(2, pledge2.dependency.getOutputs().size()); assertTrue(400_000 == pledge2.dependency.getOutput(0).getValue().longValue() || 400_000 == pledge2.dependency.getOutput(1).getValue().longValue()); assertEquals(pledge2.dependency.getInput(0).getOutpoint().getHash(), pledge.dependency.getHash()); pledge2.commit(true); System.out.println(roundTrip(wallet)); }
@Test(expected = InsufficientMoneyException.class) public void dontSpendStubs() throws Exception { WalletTestObjects objects = new WalletTestObjects(() -> new PledgingWallet(UnitTestParams.get())); PledgingWallet wallet = (PledgingWallet) objects.wallet; objects.sendAmounts(1_000_000); Project project = new Project(makeProject(wallet, 3_000_000)); PledgingWallet.PendingPledge pledge = wallet.createPledge(project, 1_000_000, null); pledge.commit(true); wallet = roundtripWallet(wallet); wallet.createPledge(project, 800_000, null); }