Skip to content

Commit

Permalink
fix(coinjoin): add account index to coinjoin path
Browse files Browse the repository at this point in the history
  • Loading branch information
HashEngineering committed Dec 21, 2023
1 parent c6527ce commit b922cab
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,15 @@ public ImmutableList<ChildNumber> masternodeOperatorDerivationPath() {
}

/** mixed coins derivation path
* m/9'/5'/4' (mainnet)
* m/9'/1'/4' (testnet, devnets)
* m/9'/5'/4'/account' (mainnet)
* m/9'/1'/4'/account (testnet, devnets)
*/
public ImmutableList<ChildNumber> coinJoinDerivationPath() {
public ImmutableList<ChildNumber> coinJoinDerivationPath(int account) {
return ImmutableList.<ChildNumber>builder()
.add(FEATURE_PURPOSE)
.add(coinType)
.add(new ChildNumber(4, true))
.add(ChildNumber.ZERO_HARDENED)
.build();
}

Expand Down
12 changes: 8 additions & 4 deletions core/src/main/java/org/bitcoinj/wallet/WalletEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ public static WalletEx fromWatchingKeyB58(NetworkParameters params, String watch
* Creates a new keychain and activates it using the seed of the active key chain, if the path does not exist.
*/
@VisibleForTesting
public void initializeCoinJoin() {
getCoinJoin().addKeyChain(getKeyChainSeed(), derivationPathFactory.coinJoinDerivationPath());
public void initializeCoinJoin(int account) {
getCoinJoin().addKeyChain(getKeyChainSeed(), derivationPathFactory.coinJoinDerivationPath(account));
}

public Coin getDenominatedBalance() {
Expand Down Expand Up @@ -871,6 +871,10 @@ public boolean selectTxDSInsByDenomination(int nDenom, Coin nValueMax, List<Coin
}

log.info("coinjoin: -- setRecentTxIds.size(): {}", setRecentTxIds.size());
if (setRecentTxIds.isEmpty()) {
log.info("No results found for {}", CoinJoin.denominationToAmount(nDenom).toFriendlyString());
vCoins.forEach(output -> log.info(" output: {}", output));
}

return nValueTotal.isPositive();
}
Expand Down Expand Up @@ -950,8 +954,8 @@ List<TransactionOutput> getDenominatedOutputs() {
return result;
}

public void initializeCoinJoin(@Nullable KeyParameter keyParameter) {
ImmutableList<ChildNumber> path = DerivationPathFactory.get(getParams()).coinJoinDerivationPath();
public void initializeCoinJoin(@Nullable KeyParameter keyParameter, int account) {
ImmutableList<ChildNumber> path = DerivationPathFactory.get(getParams()).coinJoinDerivationPath(account);
if (keyParameter != null) {
getCoinJoin().addEncryptedKeyChain(getKeyChainSeed(), path, keyParameter);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void tearDown() {
@Ignore
public void sessionTest() throws Exception {
System.out.println("Session test started...");
mixingWallet.initializeCoinJoin();
mixingWallet.initializeCoinJoin(0);
CoinJoinClientOptions.reset();
CoinJoinClientOptions.setAmount(Coin.COIN);
CoinJoinClientOptions.setEnabled(true);
Expand Down Expand Up @@ -351,7 +351,7 @@ public void sessionTest() throws Exception {
@Test(timeout = 30000) // Exception: test timed out after 100 milliseconds
public void sessionTestTwo() throws Exception {
System.out.println("Session test started...");
mixingWallet.initializeCoinJoin();
mixingWallet.initializeCoinJoin(0);
CoinJoinClientOptions.reset();
CoinJoinClientOptions.setAmount(Coin.COIN);
CoinJoinClientOptions.setEnabled(true);
Expand Down Expand Up @@ -595,7 +595,7 @@ private void addBlock() throws PrunedException {

@Test
public void sessionAttemptWithEmptyWalletTest() throws Exception {
mixingWallet.initializeCoinJoin();
mixingWallet.initializeCoinJoin(0);
CoinJoinClientOptions.reset();
CoinJoinClientOptions.setAmount(Coin.COIN);
CoinJoinClientOptions.setEnabled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void tearDown() throws Exception {
}

public void setupWallet() {
walletEx.initializeCoinJoin();
walletEx.initializeCoinJoin(0);
DeterministicKey key = (DeterministicKey) walletEx.getCoinJoin().freshReceiveKey();

txDeposit = new Transaction(UNITTEST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void setUp() throws UnreadableWalletException {
.build();

wallet.addAndActivateHDChain(bip44);
wallet.initializeCoinJoin();
wallet.initializeCoinJoin(0);
wallet.coinjoin.getActiveKeyChain().getKeys(KeyChain.KeyPurpose.RECEIVE_FUNDS, 3000);
wallet.addWalletTransaction(new WalletTransaction(WalletTransaction.Pool.SPENT, new Transaction(TESTNET, inputTx)));
ArrayList<Transaction> unspentTxList = Lists.newArrayList();
Expand Down Expand Up @@ -113,11 +113,12 @@ public void setUp() throws UnreadableWalletException {

@Test
public void keyTest() {
ECKey key661 = ECKey.fromPublicOnly(HEX.decode("034382c0cd973ee81e6bf63f9f52bf892e451a4b67ca966de4d9974bc295244f77"));
System.out.println(wallet.toString(true, false, true, null));
ECKey key661 = ECKey.fromPublicOnly(HEX.decode("026097b3f8a2884383df50a9f48f2f5477c871e0c1c54732c8e22a4a9be9803ae5"));
ECKey key = (ECKey) wallet.coinjoin.findKeyFromPubKey(key661.getPubKey());
assertNotNull(key);

ECKey key2317 = wallet.findKeyFromPubKeyHash(HEX.decode("f6863a7856dbc547d26f03e6ebb8c3db3eb12f9d"), Script.ScriptType.P2PKH);
ECKey key2317 = wallet.findKeyFromPubKeyHash(HEX.decode("43a51a896f7197cdc539bc65a8f29eddc9571751"), Script.ScriptType.P2PKH);
assertNotNull(key2317);
}

Expand Down Expand Up @@ -155,7 +156,7 @@ public void encryptDecryptWalletWithArbitraryPathAndScriptTypeWithCoinJoin() thr
.build();
WalletEx encryptedWallet = new WalletEx(DEVNET, keyChainGroup);
DeterministicKey keyBip44 = encryptedWallet.currentKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
encryptedWallet.initializeCoinJoin();
encryptedWallet.initializeCoinJoin(0);
DeterministicKey coinJoinKey = (DeterministicKey) encryptedWallet.coinjoin.currentKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
encryptedWallet.encrypt(PASSWORD1);
encryptedWallet.decrypt(PASSWORD1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public class DerivationPathFactoryTest {

// m / 9' / 5' / 3' / 1' - Masternode Voting Path
public static final ImmutableList<ChildNumber> COINJOIN_PATH = ImmutableList.of(new ChildNumber(9, true),
ChildNumber.FIVE_HARDENED, new ChildNumber(4, true));
ChildNumber.FIVE_HARDENED, new ChildNumber(4, true), ChildNumber.ZERO_HARDENED);
public static final ImmutableList<ChildNumber> COINJOIN_PATH_TESTNET = ImmutableList.of(new ChildNumber(9, true),
ChildNumber.ONE_HARDENED, new ChildNumber(4, true));
ChildNumber.ONE_HARDENED, new ChildNumber(4, true), ChildNumber.ZERO_HARDENED);

@Test
public void validateMainNet() {
Expand All @@ -82,7 +82,7 @@ public void validateMainNet() {
assertEquals(BLOCKCHAIN_IDENTITY_FUNDING_PATH, factory.blockchainIdentityRegistrationFundingDerivationPath());

// coinjoin
assertEquals(COINJOIN_PATH, factory.coinJoinDerivationPath());
assertEquals(COINJOIN_PATH, factory.coinJoinDerivationPath(0));
}

@Test
Expand All @@ -103,6 +103,6 @@ public void validateTestNet() {
assertEquals(BLOCKCHAIN_IDENTITY_FUNDING_PATH_TESTNET, factory.blockchainIdentityRegistrationFundingDerivationPath());

// coinjoin
assertEquals(COINJOIN_PATH_TESTNET, factory.coinJoinDerivationPath());
assertEquals(COINJOIN_PATH_TESTNET, factory.coinJoinDerivationPath(0));
}
}
4 changes: 2 additions & 2 deletions tools/src/main/java/org/bitcoinj/tools/WalletTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,7 @@ private static void createWallet(OptionSet options, NetworkParameters params, Fi
throw new RuntimeException(e);
}
wallet = WalletEx.fromSeed(params, seed, outputScriptType);
wallet.initializeCoinJoin();
wallet.initializeCoinJoin(0);
} else if (options.has(watchFlag)) {
wallet = WalletEx.fromWatchingKeyB58(params, options.valueOf(watchFlag), creationTimeSecs);
} else {
Expand Down Expand Up @@ -1634,7 +1634,7 @@ private static void setCreationTime() {
}

private static void mix() {
wallet.getCoinJoin().addKeyChain(wallet.getKeyChainSeed(), DerivationPathFactory.get(wallet.getParams()).coinJoinDerivationPath());
wallet.getCoinJoin().addKeyChain(wallet.getKeyChainSeed(), DerivationPathFactory.get(wallet.getParams()).coinJoinDerivationPath(0));
syncChain();
// set defaults
CoinJoinReporter reporter = new CoinJoinReporter(params);
Expand Down

0 comments on commit b922cab

Please sign in to comment.