Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(coinjoin): fix private send and regular send #228

Merged
merged 12 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions core/src/main/java/org/bitcoinj/coinjoin/CoinJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,19 @@ public static String getStatusById(PoolStatus status) {
public static boolean hasDSTX(Sha256Hash hash) {
return mapDSTX.containsKey(hash);
}

public static String getRoundsString(int rounds) {
switch (rounds) {
case -4:
return "bad index";
case -3:
return "collateral";
case -2:
return "non-denominated";
case -1:
return "no such tx";
default:
return "coinjoin";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public static void setAmount(Coin amount) {

public static boolean removeDenomination(Coin amount) { return CoinJoinClientOptions.get().allowedDenominations.get().remove(amount); }

public static boolean removeDenomination(Denomination denomination) { return CoinJoinClientOptions.get().allowedDenominations.get().remove(denomination.value); }

public static void resetDenominations() { CoinJoinClientOptions.get().allowedDenominations.set(CoinJoin.getStandardDenominations()); }
private static CoinJoinClientOptions instance;
private static boolean onceFlag;
Expand Down
33 changes: 33 additions & 0 deletions core/src/main/java/org/bitcoinj/coinjoin/Denomination.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2023 Dash Core Group
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.bitcoinj.coinjoin;

import org.bitcoinj.core.Coin;

public enum Denomination {
TEN(Coin.COIN.multiply(10).add(Coin.valueOf(10000))),
ONE(Coin.COIN.add(Coin.valueOf(1000))),
TENTH(Coin.COIN.div(10).add(Coin.valueOf(100))),
HUNDREDTH(Coin.COIN.div(100).add(Coin.valueOf(10))),
THOUSANDTH(Coin.COIN.div(1000).add(Coin.valueOf(1))),
SMALLEST(THOUSANDTH.value);

final Coin value;
Denomination(Coin value) {
this.value = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,10 @@
import java.util.List;

public class UnmixedZeroConfCoinSelector extends ZeroConfCoinSelector {
//private final Wallet wallet;
private boolean onlyConfirmed = false;

private static final UnmixedZeroConfCoinSelector instance = new UnmixedZeroConfCoinSelector();

public static UnmixedZeroConfCoinSelector get() {
return instance;
}

protected UnmixedZeroConfCoinSelector() {
private final Wallet wallet;
public UnmixedZeroConfCoinSelector(Wallet wallet) {
super();
this.wallet = wallet;
}

@Override
Expand All @@ -58,10 +51,8 @@ public CoinSelection select(Coin target, List<TransactionOutput> candidates) {
protected boolean shouldSelect(Transaction tx) {
if (tx != null) {
for (TransactionOutput output : tx.getOutputs()) {
if (!output.isDenominated()) {
if (!output.isFullyMixed(wallet)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unmixed coins are not fully mixed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about partially mixed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This question will be answered later.

TransactionConfidence confidence = tx.getConfidence();
if (onlyConfirmed)
return confidence.getConfidenceType() == TransactionConfidence.ConfidenceType.BUILDING;
return super.shouldSelect(tx);
}
}
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/org/bitcoinj/core/TransactionBag.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,7 @@ public interface TransactionBag {

/** Returns transactions from a specific pool. */
Map<Sha256Hash, Transaction> getTransactionPool(WalletTransaction.Pool pool);

/** Returns true if this output is fully mixed **/
boolean isFullyMixed(TransactionOutput output);
}
8 changes: 7 additions & 1 deletion core/src/main/java/org/bitcoinj/core/TransactionOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,14 @@ else if (ScriptPattern.isP2PKH(script))
}

/**
* Returns true if this output is to a coinjoin related key
* Returns true if this output is to a coinjoin related key and is fully mixed
*/
public boolean isCoinJoin(TransactionBag transactionBag) {
try {
if(!isDenominated())
return false;
if(!transactionBag.isFullyMixed(this))
return false;
Comment on lines +329 to +330
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CoinJoin coins are fully mixed.


Script script = getScriptPubKey();
if (ScriptPattern.isP2PK(script))
Expand Down Expand Up @@ -445,4 +447,8 @@ public int hashCode() {
public boolean equalsWithoutParent(TransactionOutput output) {
return value == output.value && Arrays.equals(scriptBytes, output.scriptBytes);
}

public boolean isFullyMixed(TransactionBag transactionBag) {
return transactionBag.isFullyMixed(this);
}
}
24 changes: 23 additions & 1 deletion core/src/main/java/org/bitcoinj/wallet/CoinJoinExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import com.google.protobuf.CodedOutputStream;
import org.bitcoinj.coinjoin.CoinJoin;
import org.bitcoinj.coinjoin.CoinJoinClientOptions;
import org.bitcoinj.core.Address;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.TransactionOutPoint;
import org.bitcoinj.core.TransactionOutput;
import org.bitcoinj.crypto.ChildNumber;
import org.bitcoinj.crypto.factory.ECKeyFactory;
Expand Down Expand Up @@ -178,6 +180,7 @@ public void setRounds(int rounds) {
}

public TreeMap<Integer, List<TransactionOutput>> getOutputs() {
checkNotNull(wallet);
TreeMap<Integer, List<TransactionOutput>> outputs = Maps.newTreeMap();
for (Coin amount : CoinJoin.getStandardDenominations()) {
outputs.put(CoinJoin.amountToDenomination(amount), Lists.newArrayList());
Expand Down Expand Up @@ -205,8 +208,27 @@ public String toString(boolean includeLookahead, boolean includePrivateKeys, @Nu
int denom = entry.getKey();
List<TransactionOutput> outputs = entry.getValue();
Coin value = outputs.stream().map(TransactionOutput::getValue).reduce(Coin::add).orElse(Coin.ZERO);
builder.append(CoinJoin.denominationToString(denom)).append(" ").append(outputs.size()).append(" ")
builder.append(CoinJoin.denominationToString(denom)).append(" outputs:").append(outputs.size()).append(" total:")
.append(value.toFriendlyString()).append("\n");
outputs.forEach(output -> {
TransactionOutPoint outPoint = new TransactionOutPoint(output.getParams(), output.getIndex(), output.getParentTransactionHash());
builder.append(" addr:")
.append(Address.fromPubKeyHash(output.getParams(), ScriptPattern.extractHashFromP2PKH(output.getScriptPubKey())))
.append(" outpoint:")
.append(outPoint.toStringShort())
.append(" ");
int rounds = ((WalletEx) wallet).getRealOutpointCoinJoinRounds(outPoint);
builder.append(CoinJoin.getRoundsString(rounds));
if (rounds >= 0) {
builder.append(" ").append(rounds).append(" rounds");
if (((WalletEx) wallet).isFullyMixed(outPoint)) {
builder.append(" (fully mixed)");
}
} else {
builder.append(" ").append(output.getValue().toFriendlyString());
}
builder.append("\n");
});
Comment on lines +213 to +231
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change will update this section in a wallet dump to include each "output" and the rounds mixed

Outputs:
N/A outputs:11 total:0.00518944 DASH
  addr:ydEvmG7LMN8mne4LcGqraHV4v7KsDu5jZ7 outpoint:d7d6f127b9fb262baa49c397fa4f0e3afeba3534ce7e34c72d931bab7a039b58-0 collateral 0.0003 DASH
  addr:yQume8R9UgEn1ADfRqUmoqHeTRiDBXWyK4 outpoint:7fcea451f4b633197a0d5c69f651df23628b0c246c8b0dc6b51ab77ba857d6be-0 collateral 0.0004 DASH
  addr:yYXBfwGAFAj4R1Gpa7DUgrdHSd7vhpgyY4 outpoint:9745ffa61f8f8c8a8936bb5d59b8d29c4d8630e0d84ab01eb0cfbfc421ab953e-0 collateral 0.0004 DASH
  addr:yVCP88cjxHj8qko2fK1dFBdJQQ84Hkr4xV outpoint:713b579b0491feae451eae2a865dbbd7d3ffeef5155c1f579df5bb896498da26-0 collateral 0.0003 DASH
  addr:yUBvXJC9jXu8ac5Bm7xW4RSNQJi7sS9Z5Y outpoint:b7e3951e6567013a3b4b9342b766a028ab750de92935423d701cb14d25f26596-1 non-denominated 0.00079648 DASH
  addr:yUiFfkDWsAgunwcEYwnrwmmgr8rcP77hNY outpoint:65ffe13000550caa6bbe3e51589cb74611e132e20b72462dd50f2e51cfc4f185-1 non-denominated 0.00079648 DASH
  addr:yYrifCGm91rw1BtekrpH62kKck7j9ziCbA outpoint:d38def14895dc7c44f34692fca1f963e675f79f4842b75b68370c0f632a299c9-0 collateral 0.0003 DASH
  addr:yP6hq8bFkNozGZAffhhaHUCz2KXmpCPQeb outpoint:7fcea451f4b633197a0d5c69f651df23628b0c246c8b0dc6b51ab77ba857d6be-1 non-denominated 0.00079648 DASH
  addr:yMB1KM1XciP7RZ1tfsc2bEt6J8Sv65N5eo outpoint:1c9ede53abf7f28164f6cb567d9432295669e7e89c114af64eec8fd50f317894-1 collateral 0.0004 DASH
  addr:yNhrC82fPxV6zecXjtTR13aGPtiTSxKccE outpoint:65ffe13000550caa6bbe3e51589cb74611e132e20b72462dd50f2e51cfc4f185-0 collateral 0.0004 DASH
  addr:ybe6i8sg6oNX478mUxVk82VtuHcHspkdCE outpoint:cfcd443d79513b43faa0163d8698a4da2261241921009586a099d4b6e24429f2-0 collateral 0.0003 DASH
10.0001 outputs:0 total:0.00 DASH
1.00001 outputs:0 total:0.00 DASH
0.100001 outputs:0 total:0.00 DASH
0.0100001 outputs:2 total:0.0200002 DASH
  addr:ybMRc9tCRkCdxFk3Lcz3fGKkfEAUMEuALD outpoint:77b902728d9dc7d6ab9d75ea2a4fe0673f0f69ec478f948a6214136224a11a37-1 coinjoin 4 rounds
  addr:ydKqnTtLkq2xJijip997WS7WkBB22EDKse outpoint:07ee8cbce22bfeefaa92589ce082c28443310bd08596624a12e922f193e956d2-1 coinjoin 2 rounds
0.00100001 outputs:0 total:0.00 DASH

}
return builder.toString();
}
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/org/bitcoinj/wallet/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -6242,4 +6242,9 @@ public List<Address> getIssuedSendAddresses(EvolutionContact contact) {
addresses.add(Address.fromKey(getParams(), key));
return addresses;
}

@Override
public boolean isFullyMixed(TransactionOutput output) {
return false;
}
}
6 changes: 3 additions & 3 deletions core/src/main/java/org/bitcoinj/wallet/WalletEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ public Coin getBalance(BalanceType balanceType) {
List<TransactionOutput> all = calculateAllSpendCandidates(true, balanceType == BalanceType.COINJOIN_SPENDABLE);
Coin value = Coin.ZERO;
for (TransactionOutput out : all) {
// exclude non coinjoin outputs if isCoinJoinOnly is true
// exclude coinjoin outputs when isCoinJoinOnly is false
// coinjoin outputs must be denominated, using coinjoin keys and fully mixed
boolean isCoinJoin = out.isDenominated() && out.isCoinJoin(this) && isFullyMixed(out);

if (isCoinJoin)
Expand Down Expand Up @@ -312,7 +311,7 @@ public boolean isLockedCoin(TransactionOutPoint outPoint) {

HashMap<TransactionOutPoint, Integer> mapOutpointRoundsCache = new HashMap<>();
// Recursively determine the rounds of a given input (How deep is the CoinJoin chain for a given input)
int getRealOutpointCoinJoinRounds(TransactionOutPoint outPoint) {
public int getRealOutpointCoinJoinRounds(TransactionOutPoint outPoint) {
return getRealOutpointCoinJoinRounds(outPoint, 0);
}

Expand Down Expand Up @@ -439,6 +438,7 @@ int getRealOutpointCoinJoinRounds(TransactionOutPoint outpoint, int rounds) {

Sha256Hash coinJoinSalt = Sha256Hash.ZERO_HASH;

@Override
public boolean isFullyMixed(TransactionOutput output) {
return isFullyMixed(new TransactionOutPoint(params, output));
}
Expand Down
4 changes: 3 additions & 1 deletion tools/src/main/java/org/bitcoinj/tools/WalletTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.bitcoinj.coinjoin.CoinJoinClientManager;
import org.bitcoinj.coinjoin.CoinJoinClientOptions;
import org.bitcoinj.coinjoin.CoinJoinSendRequest;
import org.bitcoinj.coinjoin.Denomination;
import org.bitcoinj.coinjoin.UnmixedZeroConfCoinSelector;
import org.bitcoinj.coinjoin.utils.CoinJoinReporter;
import org.bitcoinj.coinjoin.utils.ProTxToOutpoint;
Expand Down Expand Up @@ -1638,7 +1639,8 @@ private static void mix() {
CoinJoinClientOptions.setEnabled(true);
CoinJoinClientOptions.setRounds(4);
CoinJoinClientOptions.setSessions(1);
CoinJoinClientOptions.removeDenomination(CoinJoin.getSmallestDenomination());
CoinJoinClientOptions.removeDenomination(Denomination.SMALLEST);
//CoinJoinClientOptions.removeDenomination(CoinJoinClientOptions.getDenominations().stream().min(Coin::compareTo).get());
Coin amountToMix = wallet.getBalance();

// set command line arguments
Expand Down
Loading