public void WalletStore_Get_And_Set_And_Remove() { DataFolder dataFolder = CreateDataFolder(this); WalletStore store = new WalletStore(this.Network, dataFolder, new Types.Wallet { Name = "wallet1", EncryptedSeed = "EncryptedSeed1" }); var utxo = new OutPoint(new uint256(10), 1); var address = new Key().PubKey.GetAddress(this.Network).ScriptPubKey.ToString(); var trx = Create(utxo, address); // insert the document then fetch it and compare with source store.InsertOrUpdate(trx); var trxRes = store.GetForOutput(utxo); var jsonStringTrx = JsonConvert.SerializeObject(trx, new JsonSerializerSettings { Converters = new List <JsonConverter> { new MoneyJsonConverter(), new ScriptJsonConverter() } }); var jsonStringTrxRes = JsonConvert.SerializeObject(trxRes, new JsonSerializerSettings { Converters = new List <JsonConverter> { new MoneyJsonConverter(), new ScriptJsonConverter() } }); jsonStringTrx.Should().Be(jsonStringTrxRes); trx.BlockHash = null; trx.BlockHeight = null; trx.BlockIndex = null; // update the changed document then fetch it and compare with source store.InsertOrUpdate(trx); trxRes = store.GetForOutput(utxo); jsonStringTrx = JsonConvert.SerializeObject(trx, new JsonSerializerSettings { Converters = new List <JsonConverter> { new MoneyJsonConverter(), new ScriptJsonConverter() } }); jsonStringTrxRes = JsonConvert.SerializeObject(trxRes, new JsonSerializerSettings { Converters = new List <JsonConverter> { new MoneyJsonConverter(), new ScriptJsonConverter() } }); jsonStringTrx.Should().Be(jsonStringTrxRes); store.Remove(trx.OutPoint); var removed = store.GetForOutput(trx.OutPoint); removed.Should().BeNull(); }
public void WalletStore_GetForAddress() { DataFolder dataFolder = CreateDataFolder(this); WalletStore store = new WalletStore(this.Network, dataFolder, new Types.Wallet { Name = "wallet1", EncryptedSeed = "EncryptedSeed1" }); var scripts = new List <string>(); for (int indexAddress = 0; indexAddress < 3; indexAddress++) { var script = new Key().PubKey.GetAddress(this.Network).ScriptPubKey.ToString(); scripts.Add(script); for (int indexTrx = 0; indexTrx < 5; indexTrx++) { var utxo = new OutPoint(new uint256((ulong)indexTrx), indexAddress); var trx = Create(utxo, script); if (indexTrx > 2) { trx.SpendingDetails = null; } store.InsertOrUpdate(trx); } } var findforAddress = scripts[1]; var res = store.GetForAddress(findforAddress); res.Should().HaveCount(5); foreach (var item in res) { item.Address.Should().Be(findforAddress); } var count = store.CountForAddress(findforAddress); count.Should().Be(5); }
public void CreateWalletData() { string walltName = "wallet-with-funds"; string path = Path.Combine(WalletOutputDataPath + @"\txdb", walltName + ".db"); if (File.Exists(path)) { File.Delete(path); } var network = new BitcoinRegTest(); var folder = new DataFolder(WalletOutputDataPath); var wallet = new Features.Wallet.Types.Wallet() { Name = walltName }; var walletStore = new WalletStore(network, folder, wallet); string dataPath = Path.Combine(DataPath, "wallet-data-table.json"); var dataTable = JsonConvert.DeserializeObject <WalletDataItem[]>(File.ReadAllText(dataPath))[0]; WalletData walletData = new WalletData { Key = dataTable.Id, WalletName = dataTable.WalletName, EncryptedSeed = dataTable.EncryptedSeed, WalletTip = new HashHeightPair(uint256.Parse(dataTable.WalletTip.Split("-")[1]), int.Parse(dataTable.WalletTip.Split("-")[0])), BlockLocator = dataTable.BlockLocator.Select(uint256.Parse).ToList() }; walletStore.SetData(walletData); dataPath = Path.Combine(DataPath, "wallet-transactions-table.json"); var transactionsTable = JsonConvert.DeserializeObject <TransactionDataItem[]>(File.ReadAllText(dataPath)); foreach (var item in transactionsTable) { var trx = new TransactionOutputData { OutPoint = new OutPoint(uint256.Parse(item.OutPoint.Split("-")[0]), int.Parse(item.OutPoint.Split("-")[1])), Address = item.Address, Id = uint256.Parse(item.Id), Amount = new Money(item.Amount), Index = item.Index, BlockHeight = item.BlockHeight, BlockHash = item.BlockHash != null?uint256.Parse(item.BlockHash) : null, CreationTime = DateTimeOffset.FromUnixTimeSeconds(long.Parse(item.CreationTime)), ScriptPubKey = new Script(Encoders.Hex.DecodeData(item.ScriptPubKey)), IsPropagated = item.IsPropagated, AccountIndex = item.AccountIndex, IsCoinStake = item.IsCoinStake, IsCoinBase = item.IsCoinBase, IsColdCoinStake = item.IsColdCoinStake, }; if (item.SpendingDetails != null) { trx.SpendingDetails = new Features.Wallet.Database.SpendingDetails { BlockHeight = item.SpendingDetails.BlockHeight, IsCoinStake = item.SpendingDetails.IsCoinStake, CreationTime = DateTimeOffset.FromUnixTimeSeconds(long.Parse(item.SpendingDetails.CreationTime)), TransactionId = uint256.Parse(item.SpendingDetails.TransactionId) }; if (item.SpendingDetails.Payments != null) { foreach (PaymentDetails spendingDetailsPayment in item.SpendingDetails.Payments) { trx.SpendingDetails.Payments.Add(new Features.Wallet.Database.PaymentDetails { Amount = new Money(spendingDetailsPayment.Amount), DestinationAddress = spendingDetailsPayment.DestinationAddress, DestinationScriptPubKey = new Script(Encoders.Hex.DecodeData(spendingDetailsPayment.DestinationScriptPubKey)), OutputIndex = spendingDetailsPayment.OutputIndex, PayToSelf = spendingDetailsPayment.PayToSelf }); } } } walletStore.InsertOrUpdate(trx); } }
public void WalletStore_GetAccountHistory() { DataFolder dataFolder = CreateDataFolder(this); WalletStore store = new WalletStore(this.Network, dataFolder, new Types.Wallet { Name = "wallet1", EncryptedSeed = "EncryptedSeed1" }); string script = null; script = new Key().PubKey.GetAddress(this.Network).ScriptPubKey.ToString(); TransactionOutputData trx = null; ulong index = 20; ulong time = 2000; var dt = DateTimeOffset.Now; // unconfirmed spent trx = Create(new OutPoint(new uint256(index++), 10), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.BlockHeight = null; trx.SpendingDetails.BlockHeight = null; trx.IsColdCoinStake = true; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index++), 10), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.BlockHeight = null; trx.SpendingDetails.BlockHeight = null; trx.IsColdCoinStake = true; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index++), 10), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.BlockHeight = null; trx.SpendingDetails.BlockHeight = null; trx.IsColdCoinStake = false; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index++), 10), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.BlockHeight = null; trx.SpendingDetails.BlockHeight = null; trx.IsColdCoinStake = false; store.InsertOrUpdate(trx); // with 4 outputs trx = Create(new OutPoint(new uint256(index++), 00), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.BlockHeight = null; trx.SpendingDetails.BlockHeight = null; trx.IsColdCoinStake = false; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index), 0001), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.BlockHeight = null; trx.SpendingDetails.BlockHeight = null; trx.IsColdCoinStake = false; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index), 0002), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.BlockHeight = null; trx.SpendingDetails.BlockHeight = null; trx.IsColdCoinStake = false; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index), 0003), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.BlockHeight = null; trx.SpendingDetails.BlockHeight = null; trx.IsColdCoinStake = false; store.InsertOrUpdate(trx); // unconfirmed unspent trx = Create(new OutPoint(new uint256(index++), 10), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.BlockHeight = null; trx.SpendingDetails = null; trx.IsColdCoinStake = true; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index++), 10), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.BlockHeight = null; trx.SpendingDetails = null; trx.IsColdCoinStake = true; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index++), 10), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.BlockHeight = null; trx.SpendingDetails = null; trx.IsColdCoinStake = false; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index++), 10), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.BlockHeight = null; trx.SpendingDetails = null; trx.IsColdCoinStake = false; store.InsertOrUpdate(trx); // with 4 outputs trx = Create(new OutPoint(new uint256(index++), 00), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.BlockHeight = null; trx.SpendingDetails = null; trx.IsColdCoinStake = true; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index), 0001), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.BlockHeight = null; trx.SpendingDetails = null; trx.IsColdCoinStake = true; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index), 0002), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.BlockHeight = null; trx.SpendingDetails = null; trx.IsColdCoinStake = false; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index), 0003), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.BlockHeight = null; trx.SpendingDetails = null; trx.IsColdCoinStake = false; store.InsertOrUpdate(trx); // confirmed spent trx = Create(new OutPoint(new uint256(index++), 10), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.IsColdCoinStake = true; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index++), 10), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.IsColdCoinStake = true; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index++), 10), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.IsColdCoinStake = false; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index++), 10), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.IsColdCoinStake = false; store.InsertOrUpdate(trx); // with 4 outputs trx = Create(new OutPoint(new uint256(index++), 00), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.IsColdCoinStake = false; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index), 0001), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.IsColdCoinStake = false; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index), 0002), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.IsColdCoinStake = false; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index), 0003), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.IsColdCoinStake = false; store.InsertOrUpdate(trx); // confirmed unspent trx = Create(new OutPoint(new uint256(index++), 10), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.SpendingDetails = null; trx.IsColdCoinStake = true; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index++), 10), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.SpendingDetails = null; trx.IsColdCoinStake = true; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index++), 10), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.SpendingDetails = null; trx.IsColdCoinStake = false; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index++), 10), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.SpendingDetails = null; trx.IsColdCoinStake = false; store.InsertOrUpdate(trx); // with 4 outputs trx = Create(new OutPoint(new uint256(index++), 00), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.SpendingDetails = null; trx.IsColdCoinStake = true; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index), 0001), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.SpendingDetails = null; trx.IsColdCoinStake = true; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index), 0002), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.SpendingDetails = null; trx.IsColdCoinStake = false; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(index), 0003), script, 2); trx.SpendingDetails.CreationTime = dt.AddMinutes(time--); trx.SpendingDetails = null; trx.IsColdCoinStake = false; store.InsertOrUpdate(trx); var res = store.GetAccountHistory(2, false); res.Should().HaveCount(22); res = store.GetAccountHistory(2, true); res.Should().HaveCount(15); }
public void WalletStore_GetBalanceForAddress_And_GetBalanceForAccount() { DataFolder dataFolder = CreateDataFolder(this); WalletStore store = new WalletStore(this.Network, dataFolder, new Types.Wallet { Name = "wallet1", EncryptedSeed = "EncryptedSeed1" }); // Create some temp data for (int indexAddress = 0; indexAddress < 3; indexAddress++) { var scriptInsert = new Key().PubKey.GetAddress(this.Network).ScriptPubKey.ToString(); for (int indexTrx = 0; indexTrx < 5; indexTrx++) { store.InsertOrUpdate(Create(new OutPoint(new uint256((ulong)indexTrx), indexAddress), scriptInsert)); } } string script = null; for (int accountIndex = 0; accountIndex < 2; accountIndex++) { script = new Key().PubKey.GetAddress(this.Network).ScriptPubKey.ToString(); TransactionOutputData trx = null; // spent trx = Create(new OutPoint(new uint256(21), accountIndex * 10), script, 2); store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(22), accountIndex * 10), script, 2); trx.IsColdCoinStake = true; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(23), accountIndex * 10), script, 2); trx.IsColdCoinStake = true; trx.BlockHeight = null; store.InsertOrUpdate(trx); // cold stake unspent spend trx = Create(new OutPoint(new uint256(3), accountIndex * 10), script, 2); trx.IsColdCoinStake = true; trx.SpendingDetails = null; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(4), accountIndex * 10), script, 2); trx.IsColdCoinStake = true; trx.SpendingDetails = null; store.InsertOrUpdate(trx); // cold stake unspent spend unconfirmed trx = Create(new OutPoint(new uint256(5), accountIndex * 10), script, 2); trx.IsColdCoinStake = true; trx.BlockHeight = null; trx.SpendingDetails = null; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(6), accountIndex * 10), script, 2); trx.IsColdCoinStake = true; trx.BlockHeight = null; trx.SpendingDetails = null; store.InsertOrUpdate(trx); // unspent spend trx = Create(new OutPoint(new uint256(7), accountIndex * 10), script, 2); trx.IsColdCoinStake = false; trx.SpendingDetails = null; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(8), accountIndex * 10), script, 2); trx.IsColdCoinStake = null; trx.SpendingDetails = null; store.InsertOrUpdate(trx); // unspent spend unconfirmed trx = Create(new OutPoint(new uint256(9), accountIndex * 10), script, 2); trx.IsColdCoinStake = false; trx.BlockHeight = null; trx.SpendingDetails = null; store.InsertOrUpdate(trx); trx = Create(new OutPoint(new uint256(10), accountIndex * 10), script, 2); trx.IsColdCoinStake = null; trx.BlockHeight = null; trx.SpendingDetails = null; store.InsertOrUpdate(trx); } var findforAddress = script; var res = store.GetBalanceForAddress(findforAddress, false); res.AmountConfirmed.Should().Be(20); res.AmountUnconfirmed.Should().Be(20); res = store.GetBalanceForAddress(findforAddress, true); res.AmountConfirmed.Should().Be(10); res.AmountUnconfirmed.Should().Be(10); res = store.GetBalanceForAccount(2, false); res.AmountConfirmed.Should().Be(40); res.AmountUnconfirmed.Should().Be(40); res = store.GetBalanceForAccount(2, true); res.AmountConfirmed.Should().Be(20); res.AmountUnconfirmed.Should().Be(20); }