prefer scriptpubkey instead of bitcoinaddress
MODIFY
HiddenWallet.FullSpvWallet/ChaumianCoinJoin/CoinJoinService.cs
HiddenWallet.FullSpvWallet/ChaumianCoinJoin/CoinJoinService.cs
@@ -307,10 +307,10 @@ namespace HiddenWallet.FullSpvWallet.ChaumianCoinJoin
.OrderByDescending(x => x.Key.Amount) // look at the biggest amounts first, TODO: this is sub-optimal (CCJ unconfirmed change should be the same category as confirmed and not CCJ change unconfirmed should not be here)
.Select(x => x.Key))
{
- BitcoinAddress destinationAddress = coin.ScriptPubKey.GetDestinationAddress(WalletJob.CurrentNetwork);
+ Script destinationScriptPubKey = coin.ScriptPubKey;
try
{
- new BitcoinWitPubKeyAddress(destinationAddress.ToString(), WalletJob.CurrentNetwork);
+ new BitcoinWitPubKeyAddress(destinationScriptPubKey.GetDestinationAddress(WalletJob.CurrentNetwork).ToString(), WalletJob.CurrentNetwork);
}
catch
{
@@ -335,7 +335,7 @@ namespace HiddenWallet.FullSpvWallet.ChaumianCoinJoin
Inputs.Add(coin);
var input = coin.Outpoint.ToHex();
- BitcoinExtKey privateExtKey = WalletJob.Safe.FindPrivateKey(destinationAddress, (await WalletJob.GetTrackerAsync()).TrackedScriptPubKeys.Count, From);
+ BitcoinExtKey privateExtKey = WalletJob.Safe.FindPrivateKey(destinationScriptPubKey, (await WalletJob.GetTrackerAsync()).TrackedScriptPubKeys.Count, From);
SigningKeys.Add(privateExtKey);
var proof = privateExtKey.PrivateKey.SignMessage(blindedOutput);
inputs.Add(new InputProofModel { Input = input, Proof = proof });

MODIFY
HiddenWallet.FullSpvWallet/WalletJob.cs
HiddenWallet.FullSpvWallet/WalletJob.cs
@@ -424,7 +424,7 @@ namespace HiddenWallet.FullSpv
var cleanCount = 0;
while (true)
{
- Script scriptPubkey = account == null ? Safe.GetAddress(addressType, i, hdPathType).ScriptPubKey : Safe.GetAddress(addressType, i, hdPathType, account).ScriptPubKey;
+ Script scriptPubkey = account == null ? Safe.GetScriptPubKey(addressType, i, hdPathType) : Safe.GetScriptPubKey(addressType, i, hdPathType, account);
(await GetTrackerAsync()).TrackedScriptPubKeys.Add(scriptPubkey);
@@ -544,7 +544,7 @@ namespace HiddenWallet.FullSpv
public async Task<HashSet<Script>> GetTrackedScriptPubKeysBySafeAccountAsync(SafeAccount account = null)
{
var maxTracked = (await GetTrackerAsync()).TrackedScriptPubKeys.Count;
- var allPossiblyTrackedAddresses = new HashSet<BitcoinAddress>();
+ var allPossiblyTrackedScriptPubKeys = new HashSet<Script>();
var addressTypes = new HashSet<AddressType>
{
@@ -553,25 +553,25 @@ namespace HiddenWallet.FullSpv
};
foreach (AddressType addressType in addressTypes)
{
- foreach (var address in Safe.GetFirstNAddresses(addressType, maxTracked, HdPathType.Receive, account))
+ foreach (var address in Safe.GetFirstNScriptPubKey(addressType, maxTracked, HdPathType.Receive, account))
{
- allPossiblyTrackedAddresses.Add(address);
+ allPossiblyTrackedScriptPubKeys.Add(address);
}
- foreach (var address in Safe.GetFirstNAddresses(addressType, maxTracked, HdPathType.Change, account))
+ foreach (var address in Safe.GetFirstNScriptPubKey(addressType, maxTracked, HdPathType.Change, account))
{
- allPossiblyTrackedAddresses.Add(address);
+ allPossiblyTrackedScriptPubKeys.Add(address);
}
- foreach (var address in Safe.GetFirstNAddresses(addressType, maxTracked, HdPathType.NonHardened, account))
+ foreach (var address in Safe.GetFirstNScriptPubKey(addressType, maxTracked, HdPathType.NonHardened, account))
{
- allPossiblyTrackedAddresses.Add(address);
+ allPossiblyTrackedScriptPubKeys.Add(address);
}
}
var actuallyTrackedScriptPubKeys = new HashSet<Script>();
- foreach (var address in allPossiblyTrackedAddresses)
+ foreach (var scriptPubKey in allPossiblyTrackedScriptPubKeys)
{
- if ((await GetTrackerAsync()).TrackedScriptPubKeys.Any(x => x == address.ScriptPubKey))
- actuallyTrackedScriptPubKeys.Add(address.ScriptPubKey);
+ if ((await GetTrackerAsync()).TrackedScriptPubKeys.Contains(scriptPubKey))
+ actuallyTrackedScriptPubKeys.Add(scriptPubKey);
}
return actuallyTrackedScriptPubKeys;
@@ -1069,7 +1069,7 @@ namespace HiddenWallet.FullSpv
var signingKeys = new HashSet<ISecret>();
foreach (var coin in coinsToSpend)
{
- var signingKey = Safe.FindPrivateKey(coin.ScriptPubKey.GetDestinationAddress(Safe.Network), (await GetTrackerAsync()).TrackedScriptPubKeys.Count, account);
+ var signingKey = Safe.FindPrivateKey(coin.ScriptPubKey, (await GetTrackerAsync()).TrackedScriptPubKeys.Count, account);
signingKeys.Add(signingKey);
}
@@ -1145,7 +1145,7 @@ namespace HiddenWallet.FullSpv
int i = 0;
while (true)
{
- Script scriptPubkey = account == null ? Safe.GetAddress(type, i, hdPathType).ScriptPubKey : Safe.GetAddress(type, i, hdPathType, account).ScriptPubKey;
+ Script scriptPubkey = account == null ? Safe.GetScriptPubKey(type, i, hdPathType) : Safe.GetScriptPubKey(type, i, hdPathType, account);
if ((await GetTrackerAsync()).IsClean(scriptPubkey))
{
scriptPubKeys.Add(scriptPubkey);

MODIFY
HiddenWallet.Tests/FullSpvWalletTests.cs
HiddenWallet.Tests/FullSpvWalletTests.cs
@@ -165,9 +165,9 @@ namespace HiddenWallet.Tests
// 0. Query all operations, grouped our used safe addresses
int MinUnusedKeyNum = 74;
- Dictionary<BitcoinAddress, List<BalanceOperation>> operationsPerAddresses = await Helpers.QueryOperationsPerSafeAddressesAsync(new QBitNinjaClient(safe.Network), safe, MinUnusedKeyNum);
+ Dictionary<Script, List<BalanceOperation>> operationsPerScriptPubKeys = await Helpers.QueryOperationsPerSafeScriptPubKeysAsync(new QBitNinjaClient(safe.Network), safe, MinUnusedKeyNum);
- Dictionary<uint256, List<BalanceOperation>> operationsPerTransactions = QBitNinjaJutsus.GetOperationsPerTransactions(operationsPerAddresses);
+ Dictionary<uint256, List<BalanceOperation>> operationsPerTransactions = QBitNinjaJutsus.GetOperationsPerTransactions(operationsPerScriptPubKeys);
// 3. Create history records from the transactions
// History records is arbitrary data we want to show to the user

MODIFY
HiddenWallet.Tests/Helpers.cs
HiddenWallet.Tests/Helpers.cs
@@ -62,32 +62,32 @@ namespace HiddenWallet.Tests
}
}
- public static async Task<Dictionary<BitcoinAddress, List<BalanceOperation>>> QueryOperationsPerSafeAddressesAsync(QBitNinjaClient client, Safe safe, int minUnusedKeys = 7, HdPathType? hdPathType = null)
+ public static async Task<Dictionary<Script, List<BalanceOperation>>> QueryOperationsPerSafeScriptPubKeysAsync(QBitNinjaClient client, Safe safe, int minUnusedKeys = 7, HdPathType? hdPathType = null)
{
if (hdPathType == null)
{
- var t1 = QueryOperationsPerSafeAddressesAsync(client, safe, minUnusedKeys, HdPathType.Receive);
- var t2 = QueryOperationsPerSafeAddressesAsync(client, safe, minUnusedKeys, HdPathType.Change);
- var t3 = QueryOperationsPerSafeAddressesAsync(client, safe, minUnusedKeys, HdPathType.NonHardened);
+ var t1 = QueryOperationsPerSafeScriptPubKeysAsync(client, safe, minUnusedKeys, HdPathType.Receive);
+ var t2 = QueryOperationsPerSafeScriptPubKeysAsync(client, safe, minUnusedKeys, HdPathType.Change);
+ var t3 = QueryOperationsPerSafeScriptPubKeysAsync(client, safe, minUnusedKeys, HdPathType.NonHardened);
await Task.WhenAll(t1, t2, t3);
- Dictionary<BitcoinAddress, List<BalanceOperation>> operationsPerReceiveAddresses = await t1;
- Dictionary<BitcoinAddress, List<BalanceOperation>> operationsPerChangeAddresses = await t2;
- Dictionary<BitcoinAddress, List<BalanceOperation>> operationsPerNonHardenedAddresses = await t3;
+ Dictionary<Script, List<BalanceOperation>> operationsPerReceiveScriptPubKeys = await t1;
+ Dictionary<Script, List<BalanceOperation>> operationsPerChangeScriptPubKeys = await t2;
+ Dictionary<Script, List<BalanceOperation>> operationsPerNonHardenedScriptPubKeys = await t3;
- var operationsPerAllAddresses = new Dictionary<BitcoinAddress, List<BalanceOperation>>();
- foreach (var elem in operationsPerReceiveAddresses)
- operationsPerAllAddresses.Add(elem.Key, elem.Value);
- foreach (var elem in operationsPerChangeAddresses)
- operationsPerAllAddresses.Add(elem.Key, elem.Value);
- foreach (var elem in operationsPerNonHardenedAddresses)
- operationsPerAllAddresses.Add(elem.Key, elem.Value);
+ var operationsPerAllScriptPubKeys = new Dictionary<Script, List<BalanceOperation>>();
+ foreach (var elem in operationsPerReceiveScriptPubKeys)
+ operationsPerAllScriptPubKeys.Add(elem.Key, elem.Value);
+ foreach (var elem in operationsPerChangeScriptPubKeys)
+ operationsPerAllScriptPubKeys.Add(elem.Key, elem.Value);
+ foreach (var elem in operationsPerNonHardenedScriptPubKeys)
+ operationsPerAllScriptPubKeys.Add(elem.Key, elem.Value);
- return operationsPerAllAddresses;
+ return operationsPerAllScriptPubKeys;
}
- var addresses = new List<BitcoinAddress>();
+ var scriptPubKeys = new List<Script>();
var addressTypes = new HashSet<AddressType>
{
AddressType.Pay2PublicKeyHash,
@@ -95,27 +95,26 @@ namespace HiddenWallet.Tests
};
foreach (AddressType addressType in addressTypes)
{
- foreach(var address in safe.GetFirstNAddresses(addressType, minUnusedKeys, hdPathType.GetValueOrDefault()))
+ foreach(var scriptPubKey in safe.GetFirstNScriptPubKey(addressType, minUnusedKeys, hdPathType.GetValueOrDefault()))
{
- addresses.Add(address);
+ scriptPubKeys.Add(scriptPubKey);
}
}
- //var addresses = FakeData.FakeSafe.GetFirstNAddresses(minUnusedKeys);
- var operationsPerAddresses = new Dictionary<BitcoinAddress, List<BalanceOperation>>();
+ var operationsPerScriptPubKeys = new Dictionary<Script, List<BalanceOperation>>();
var unusedKeyCount = 0;
- foreach (var elem in await QueryOperationsPerAddressesAsync(client, addresses))
+ foreach (var elem in await QueryOperationsPerScriptPubKeysAsync(client, scriptPubKeys))
{
- operationsPerAddresses.Add(elem.Key, elem.Value);
+ operationsPerScriptPubKeys.Add(elem.Key, elem.Value);
if (elem.Value.Count == 0) unusedKeyCount++;
}
- Debug.WriteLine($"{operationsPerAddresses.Count} {hdPathType} keys are processed.");
+ Debug.WriteLine($"{operationsPerScriptPubKeys.Count} {hdPathType} keys are processed.");
var startIndex = minUnusedKeys;
while (unusedKeyCount < minUnusedKeys)
{
- addresses = new List<BitcoinAddress>();
+ scriptPubKeys = new List<Script>();
for (int i = startIndex; i < startIndex + minUnusedKeys; i++)
{
addressTypes = new HashSet<AddressType>
@@ -125,47 +124,46 @@ namespace HiddenWallet.Tests
};
foreach (AddressType addressType in addressTypes)
{
- addresses.Add(safe.GetAddress(addressType, i, hdPathType.GetValueOrDefault()));
+ scriptPubKeys.Add(safe.GetScriptPubKey(addressType, i, hdPathType.GetValueOrDefault()));
}
- //addresses.Add(FakeData.FakeSafe.GetAddress(i));
}
- foreach (var elem in await QueryOperationsPerAddressesAsync(client, addresses))
+ foreach (var elem in await QueryOperationsPerScriptPubKeysAsync(client, scriptPubKeys))
{
- operationsPerAddresses.Add(elem.Key, elem.Value);
+ operationsPerScriptPubKeys.Add(elem.Key, elem.Value);
if (elem.Value.Count == 0) unusedKeyCount++;
}
- Debug.WriteLine($"{operationsPerAddresses.Count} {hdPathType} keys are processed.");
+ Debug.WriteLine($"{operationsPerScriptPubKeys.Count} {hdPathType} keys are processed.");
startIndex += minUnusedKeys;
}
- return operationsPerAddresses;
+ return operationsPerScriptPubKeys;
}
- public static async Task<Dictionary<BitcoinAddress, List<BalanceOperation>>> QueryOperationsPerAddressesAsync(QBitNinjaClient client, IEnumerable<BitcoinAddress> addresses)
+ public static async Task<Dictionary<Script, List<BalanceOperation>>> QueryOperationsPerScriptPubKeysAsync(QBitNinjaClient client, IEnumerable<Script> scriptPubKeys)
{
- var operationsPerAddresses = new Dictionary<BitcoinAddress, List<BalanceOperation>>();
+ var operationsPerScriptPubKeysAsync = new Dictionary<Script, List<BalanceOperation>>();
- var addressList = addresses.ToList();
+ var scriptPubKeyList = scriptPubKeys.ToList();
var balanceModelList = new List<BalanceModel>();
- foreach (var balance in await GetBalancesAsync(client, addressList, unspentOnly: false))
+ foreach (var balance in await GetBalancesAsync(client, scriptPubKeyList, unspentOnly: false))
{
balanceModelList.Add(balance);
}
for (var i = 0; i < balanceModelList.Count; i++)
{
- operationsPerAddresses.Add(addressList[i], balanceModelList[i].Operations);
+ operationsPerScriptPubKeysAsync.Add(scriptPubKeyList[i], balanceModelList[i].Operations);
}
- return operationsPerAddresses;
+ return operationsPerScriptPubKeysAsync;
}
- public static async Task<IEnumerable<BalanceModel>> GetBalancesAsync(QBitNinjaClient client, IEnumerable<BitcoinAddress> addresses, bool unspentOnly)
+ public static async Task<IEnumerable<BalanceModel>> GetBalancesAsync(QBitNinjaClient client, IEnumerable<Script> scriptPubKeys, bool unspentOnly)
{
var results = new HashSet<BalanceModel>();
- foreach (var dest in addresses)
+ foreach (var dest in scriptPubKeys)
{
results.Add(await client.GetBalance(dest, unspentOnly));
}

MODIFY
HiddenWallet.Tests/QBitNinjaJutsus.cs
HiddenWallet.Tests/QBitNinjaJutsus.cs
@@ -44,11 +44,11 @@ namespace HiddenWallet.Tests
return haveEnough;
}
- public static Dictionary<uint256, List<BalanceOperation>> GetOperationsPerTransactions(Dictionary<BitcoinAddress, List<BalanceOperation>> operationsPerAddresses)
+ public static Dictionary<uint256, List<BalanceOperation>> GetOperationsPerTransactions(Dictionary<Script, List<BalanceOperation>> operationsPerScriptPubKeys)
{
// 1. Get all the unique operations
var opSet = new HashSet<BalanceOperation>();
- foreach (var elem in operationsPerAddresses)
+ foreach (var elem in operationsPerScriptPubKeys)
foreach (var op in elem.Value)
opSet.Add(op);
@@ -70,32 +70,32 @@ namespace HiddenWallet.Tests
return operationsPerTransactions;
}
- public static async Task<Dictionary<BitcoinAddress, List<BalanceOperation>>> QueryOperationsPerSafeAddressesAsync(QBitNinjaClient client, Safe safe, int minUnusedKeys = 7, HdPathType? hdPathType = null)
+ public static async Task<Dictionary<Script, List<BalanceOperation>>> QueryOperationsPerSafeScriptPubKeysAsync(QBitNinjaClient client, Safe safe, int minUnusedKeys = 7, HdPathType? hdPathType = null)
{
if (hdPathType == null)
{
- var t1 = QueryOperationsPerSafeAddressesAsync(client, safe, minUnusedKeys, HdPathType.Receive);
- var t2 = QueryOperationsPerSafeAddressesAsync(client, safe, minUnusedKeys, HdPathType.Change);
- var t3 = QueryOperationsPerSafeAddressesAsync(client, safe, minUnusedKeys, HdPathType.NonHardened);
+ var t1 = QueryOperationsPerSafeScriptPubKeysAsync(client, safe, minUnusedKeys, HdPathType.Receive);
+ var t2 = QueryOperationsPerSafeScriptPubKeysAsync(client, safe, minUnusedKeys, HdPathType.Change);
+ var t3 = QueryOperationsPerSafeScriptPubKeysAsync(client, safe, minUnusedKeys, HdPathType.NonHardened);
await Task.WhenAll(t1, t2, t3);
- Dictionary<BitcoinAddress, List<BalanceOperation>> operationsPerReceiveAddresses = await t1;
- Dictionary<BitcoinAddress, List<BalanceOperation>> operationsPerChangeAddresses = await t2;
- Dictionary<BitcoinAddress, List<BalanceOperation>> operationsPerNonHardenedAddresses = await t3;
+ Dictionary<Script, List<BalanceOperation>> operationsPerReceiveScriptPubKeys = await t1;
+ Dictionary<Script, List<BalanceOperation>> operationsPerChangeScriptPubKeys = await t2;
+ Dictionary<Script, List<BalanceOperation>> operationsPerNonHardenedScriptPubKeys = await t3;
- var operationsPerAllAddresses = new Dictionary<BitcoinAddress, List<BalanceOperation>>();
- foreach (var elem in operationsPerReceiveAddresses)
- operationsPerAllAddresses.Add(elem.Key, elem.Value);
- foreach (var elem in operationsPerChangeAddresses)
- operationsPerAllAddresses.Add(elem.Key, elem.Value);
- foreach (var elem in operationsPerNonHardenedAddresses)
- operationsPerAllAddresses.Add(elem.Key, elem.Value);
+ var operationsPerAllScriptPubKeys = new Dictionary<Script, List<BalanceOperation>>();
+ foreach (var elem in operationsPerReceiveScriptPubKeys)
+ operationsPerAllScriptPubKeys.Add(elem.Key, elem.Value);
+ foreach (var elem in operationsPerChangeScriptPubKeys)
+ operationsPerAllScriptPubKeys.Add(elem.Key, elem.Value);
+ foreach (var elem in operationsPerNonHardenedScriptPubKeys)
+ operationsPerAllScriptPubKeys.Add(elem.Key, elem.Value);
- return operationsPerAllAddresses;
+ return operationsPerAllScriptPubKeys;
}
- var addresses = new List<BitcoinAddress>();
+ var scriptPubKeys = new List<Script>();
var addressTypes = new HashSet<AddressType>
{
AddressType.Pay2PublicKeyHash,
@@ -103,27 +103,26 @@ namespace HiddenWallet.Tests
};
foreach (AddressType addressType in addressTypes)
{
- foreach (var address in safe.GetFirstNAddresses(addressType, minUnusedKeys, hdPathType.GetValueOrDefault()))
+ foreach (var scriptPubKey in safe.GetFirstNScriptPubKey(addressType, minUnusedKeys, hdPathType.GetValueOrDefault()))
{
- addresses.Add(address);
+ scriptPubKeys.Add(scriptPubKey);
}
}
- //var addresses = FakeData.FakeSafe.GetFirstNAddresses(minUnusedKeys);
- var operationsPerAddresses = new Dictionary<BitcoinAddress, List<BalanceOperation>>();
+ var operationsPerScriptPubKeys = new Dictionary<Script, List<BalanceOperation>>();
var unusedKeyCount = 0;
- foreach (var elem in await QueryOperationsPerAddressesAsync(client, addresses))
+ foreach (var elem in await QueryOperationsPerScriptPubKeysAsync(client, scriptPubKeys))
{
- operationsPerAddresses.Add(elem.Key, elem.Value);
+ operationsPerScriptPubKeys.Add(elem.Key, elem.Value);
if (elem.Value.Count == 0) unusedKeyCount++;
}
- Debug.WriteLine($"{operationsPerAddresses.Count} {hdPathType} keys are processed.");
+ Debug.WriteLine($"{operationsPerScriptPubKeys.Count} {hdPathType} keys are processed.");
var startIndex = minUnusedKeys;
while (unusedKeyCount < minUnusedKeys)
{
- addresses = new List<BitcoinAddress>();
+ scriptPubKeys = new List<Script>();
for (int i = startIndex; i < startIndex + minUnusedKeys; i++)
{
addressTypes = new HashSet<AddressType>
@@ -133,47 +132,46 @@ namespace HiddenWallet.Tests
};
foreach (AddressType addressType in addressTypes)
{
- addresses.Add(safe.GetAddress(addressType, i, hdPathType.GetValueOrDefault()));
+ scriptPubKeys.Add(safe.GetScriptPubKey(addressType, i, hdPathType.GetValueOrDefault()));
}
- //addresses.Add(FakeData.FakeSafe.GetAddress(i));
}
- foreach (var elem in await QueryOperationsPerAddressesAsync(client, addresses))
+ foreach (var elem in await QueryOperationsPerScriptPubKeysAsync(client, scriptPubKeys))
{
- operationsPerAddresses.Add(elem.Key, elem.Value);
+ operationsPerScriptPubKeys.Add(elem.Key, elem.Value);
if (elem.Value.Count == 0) unusedKeyCount++;
}
- Debug.WriteLine($"{operationsPerAddresses.Count} {hdPathType} keys are processed.");
+ Debug.WriteLine($"{operationsPerScriptPubKeys.Count} {hdPathType} keys are processed.");
startIndex += minUnusedKeys;
}
- return operationsPerAddresses;
+ return operationsPerScriptPubKeys;
}
- public static async Task<Dictionary<BitcoinAddress, List<BalanceOperation>>> QueryOperationsPerAddressesAsync(QBitNinjaClient client, IEnumerable<BitcoinAddress> addresses)
+ public static async Task<Dictionary<Script, List<BalanceOperation>>> QueryOperationsPerScriptPubKeysAsync(QBitNinjaClient client, IEnumerable<Script> scriptPubKeys)
{
- var operationsPerAddresses = new Dictionary<BitcoinAddress, List<BalanceOperation>>();
+ var operationsPerScriptPubKeys = new Dictionary<Script, List<BalanceOperation>>();
- var addressList = addresses.ToList();
+ var scriptPubKeyList = scriptPubKeys.ToList();
var balanceModelList = new List<BalanceModel>();
- foreach (var balance in await GetBalancesAsync(client, addressList, unspentOnly: false))
+ foreach (var balance in await GetBalancesAsync(client, scriptPubKeyList, unspentOnly: false))
{
balanceModelList.Add(balance);
}
for (var i = 0; i < balanceModelList.Count; i++)
{
- operationsPerAddresses.Add(addressList[i], balanceModelList[i].Operations);
+ operationsPerScriptPubKeys.Add(scriptPubKeyList[i], balanceModelList[i].Operations);
}
- return operationsPerAddresses;
+ return operationsPerScriptPubKeys;
}
- private static async Task<IEnumerable<BalanceModel>> GetBalancesAsync(QBitNinjaClient client, IEnumerable<BitcoinAddress> addresses, bool unspentOnly)
+ private static async Task<IEnumerable<BalanceModel>> GetBalancesAsync(QBitNinjaClient client, IEnumerable<Script> scriptPubKeys, bool unspentOnly)
{
var tasks = new HashSet<Task<BalanceModel>>();
- foreach (var dest in addresses)
+ foreach (var dest in scriptPubKeys)
{
var task = client.GetBalance(dest, unspentOnly);

MODIFY
HiddenWallet.Tests/SafeTests.cs
HiddenWallet.Tests/SafeTests.cs
@@ -39,12 +39,12 @@ namespace HiddenWallet.Tests
{
foreach (AddressType addressType in Enum.GetValues(typeof(AddressType)))
{
- Assert.Equal(safe.GetAddress(addressType, 3, account: alice), recoverdSafe.GetAddress(addressType, 3, account: alice));
- Assert.Equal(safe.GetAddress(addressType, 4, HdPathType.NonHardened, account: alice), recoverdSafe.GetAddress(addressType, 4, HdPathType.NonHardened, account: alice));
- Assert.NotEqual(safe.GetAddress(addressType, 4, HdPathType.Change, account: alice), recoverdSafe.GetAddress(addressType, 4, HdPathType.NonHardened, account: alice));
- Assert.NotEqual(safe.GetAddress(addressType, 3, HdPathType.NonHardened, account: alice), recoverdSafe.GetAddress(addressType, 4, HdPathType.NonHardened, account: alice));
- Assert.NotEqual(safe.GetAddress(addressType, 4, HdPathType.NonHardened, account: alice), recoverdSafe.GetAddress(addressType, 4, HdPathType.NonHardened, account: bob));
- Assert.NotEqual(safe.GetAddress(addressType, 4, account: alice), safe.GetAddress(addressType, 4));
+ Assert.Equal(safe.GetScriptPubKey(addressType, 3, account: alice), recoverdSafe.GetScriptPubKey(addressType, 3, account: alice));
+ Assert.Equal(safe.GetScriptPubKey(addressType, 4, HdPathType.NonHardened, account: alice), recoverdSafe.GetScriptPubKey(addressType, 4, HdPathType.NonHardened, account: alice));
+ Assert.NotEqual(safe.GetScriptPubKey(addressType, 4, HdPathType.Change, account: alice), recoverdSafe.GetScriptPubKey(addressType, 4, HdPathType.NonHardened, account: alice));
+ Assert.NotEqual(safe.GetScriptPubKey(addressType, 3, HdPathType.NonHardened, account: alice), recoverdSafe.GetScriptPubKey(addressType, 4, HdPathType.NonHardened, account: alice));
+ Assert.NotEqual(safe.GetScriptPubKey(addressType, 4, HdPathType.NonHardened, account: alice), recoverdSafe.GetScriptPubKey(addressType, 4, HdPathType.NonHardened, account: bob));
+ Assert.NotEqual(safe.GetScriptPubKey(addressType, 4, account: alice), safe.GetScriptPubKey(addressType, 4));
}
Assert.Equal(DateTimeOffset.UtcNow.Date, safe.CreationTime.Date);
@@ -87,7 +87,7 @@ namespace HiddenWallet.Tests
Assert.Equal(loadedSafe.GetBitcoinExtPubKey(index: null, hdPathType: HdPathType.NonHardened, account: new SafeAccount(1)), recoverdSafe.GetBitcoinExtPubKey(index: null, hdPathType: HdPathType.NonHardened, account: new SafeAccount(1)));
foreach (AddressType addressType in Enum.GetValues(typeof(AddressType)))
{
- Assert.Equal(loadedSafe.GetAddress(addressType, index), recoverdSafe.GetAddress(addressType, index));
+ Assert.Equal(loadedSafe.GetScriptPubKey(addressType, index), recoverdSafe.GetScriptPubKey(addressType, index));
}
Assert.Equal(loadedSafe.GetBitcoinExtKey(index), recoverdSafe.GetBitcoinExtKey(index));
}

MODIFY
HiddenWallet/KeyManagement/Safe.cs
HiddenWallet/KeyManagement/Safe.cs
@@ -28,45 +28,45 @@ namespace HiddenWallet.KeyManagement
return GetBitcoinExtKey(index, hdPathType, account).PrivateKey.PubKey;
}
- private ConcurrentDictionary<(AddressType Type, int Index, HdPathType HdPathType, SafeAccount Account), BitcoinAddress> _safeCache = new ConcurrentDictionary<(AddressType Type, int Index, HdPathType HdPathType, SafeAccount Account), BitcoinAddress>();
+ private ConcurrentDictionary<(AddressType Type, int Index, HdPathType HdPathType, SafeAccount Account), Script> _safeCache = new ConcurrentDictionary<(AddressType Type, int Index, HdPathType HdPathType, SafeAccount Account), Script>();
// GetP2wpkh pubKey.WitHash.ScriptPubKey.GetDestinationAddress(Network);
// GetP2pkhAddress pubKey.Hash.ScriptPubKey.GetDestinationAddress(Network);
// GetP2shOverP2wpkhAddress pubKey.WitHash.ScriptPubKey.Hash.ScriptPubKey.GetDestinationAddress(Network);
- public BitcoinAddress GetAddress(AddressType type, int index, HdPathType hdPathType = HdPathType.Receive, SafeAccount account = null)
+ public Script GetScriptPubKey(AddressType type, int index, HdPathType hdPathType = HdPathType.Receive, SafeAccount account = null)
{
- BitcoinAddress cachedAddress = _safeCache.FirstOrDefault(x => x.Key.Type == type && x.Key.Index == index && x.Key.HdPathType == hdPathType && x.Key.Account == account).Value;
- if (cachedAddress != null) return cachedAddress;
+ Script cachedScriptPubKey = _safeCache.FirstOrDefault(x => x.Key.Type == type && x.Key.Index == index && x.Key.HdPathType == hdPathType && x.Key.Account == account).Value;
+ if (cachedScriptPubKey != null) return cachedScriptPubKey;
PubKey pubKey = GetPubKey(index, hdPathType, account);
- BitcoinAddress address = null;
+ Script scriptPubKey = null;
if (type == AddressType.Pay2WitnessPublicKeyHash)
{
- address= pubKey.WitHash.ScriptPubKey.GetDestinationAddress(Network);
+ scriptPubKey= pubKey.WitHash.ScriptPubKey;
}
else if (type == AddressType.Pay2PublicKeyHash)
{
- address = pubKey.Hash.ScriptPubKey.GetDestinationAddress(Network);
+ scriptPubKey = pubKey.Hash.ScriptPubKey;
}
else if (type == AddressType.Pay2ScriptHashOverPay2WitnessPublicKeyHash)
{
- address = pubKey.WitHash.ScriptPubKey.Hash.ScriptPubKey.GetDestinationAddress(Network);
+ scriptPubKey = pubKey.WitHash.ScriptPubKey.Hash.ScriptPubKey;
}
else throw new NotSupportedException(type.ToString());
- _safeCache.TryAdd((type, index, hdPathType, account), address);
- return address;
+ _safeCache.TryAdd((type, index, hdPathType, account), scriptPubKey);
+ return scriptPubKey;
}
- public IList<BitcoinAddress> GetFirstNAddresses(AddressType type, int addressCount, HdPathType hdPathType = HdPathType.Receive, SafeAccount account = null)
+ public IList<Script> GetFirstNScriptPubKey(AddressType type, int addressCount, HdPathType hdPathType = HdPathType.Receive, SafeAccount account = null)
{
- var addresses = new List<BitcoinAddress>();
+ var scriptPubKeys = new List<Script>();
for (var i = 0; i < addressCount; i++)
{
- addresses.Add(GetAddress(type, i, hdPathType, account));
+ scriptPubKeys.Add(GetScriptPubKey(type, i, hdPathType, account));
}
- return addresses;
+ return scriptPubKeys;
}
// Let's generate a unique id from seedpublickey
@@ -213,22 +213,22 @@ namespace HiddenWallet.KeyManagement
return safe;
}
- public BitcoinExtKey FindPrivateKey(BitcoinAddress address, int stopSearchAfterIteration = 100000, SafeAccount account = null)
+ public BitcoinExtKey FindPrivateKey(Script scriptPubKey, int stopSearchAfterIteration = 100000, SafeAccount account = null)
{
foreach (AddressType type in Enum.GetValues(typeof(AddressType)))
{
for (int i = 0; i < stopSearchAfterIteration; i++)
{
- if (GetAddress(type, i, HdPathType.Receive, account) == address)
+ if (GetScriptPubKey(type, i, HdPathType.Receive, account) == scriptPubKey)
return GetBitcoinExtKey(i, HdPathType.Receive, account);
- if (GetAddress(type, i, HdPathType.Change, account) == address)
+ if (GetScriptPubKey(type, i, HdPathType.Change, account) == scriptPubKey)
return GetBitcoinExtKey(i, HdPathType.Change, account);
- if (GetAddress(type, i, HdPathType.NonHardened, account) == address)
+ if (GetScriptPubKey(type, i, HdPathType.NonHardened, account) == scriptPubKey)
return GetBitcoinExtKey(i, HdPathType.NonHardened, account);
}
}
- throw new KeyNotFoundException(address.ToString());
+ throw new KeyNotFoundException(scriptPubKey.ToString());
}
public BitcoinExtKey GetBitcoinExtKey(int? index = null, HdPathType hdPathType = HdPathType.Receive, SafeAccount account = null)

prefer scriptpubkey instead of bitcoinaddress