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); }
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 ShutdowhainChainModule(Context context, ChainService chainSyncService, NodeConnectionService nodeConnectionService, ILoggerFactory loggerFactory, WalletStore walletStore) : base(context) { this.ChainIndex = context.ChainIndex; this.chainSyncService = chainSyncService; this.nodeConnectionService = nodeConnectionService; this.walletStore = walletStore; this.logger = loggerFactory.CreateLogger <ShutdowhainChainModule>(); }
private void PersistSmartContract(object sender, EventArgs e) { if (_viewModel == null) { return; } var authenticatedWallet = WalletStore.Instance().GetAuthenticatedWallet(); if (authenticatedWallet == null) { MainWindowStore.Instance().DisplayError("You're not authenticated"); return; } if (string.IsNullOrWhiteSpace(_viewModel.TransactionId)) { MainWindowStore.Instance().DisplayError("The transaction address must be filled in"); return; } if (_publishedSolidityContract == null) { MainWindowStore.Instance().DisplayError("The smart contract must be published"); return; } IEnumerable <byte> txId = null; try { txId = _viewModel.TransactionId.FromHexString(); } catch (Exception) { MainWindowStore.Instance().DisplayError("The transaction address is not a valid hex"); return; } var rpcClient = new RpcClient(authenticatedWallet.Network); rpcClient.GetTransactionReceipt(txId).ContinueWith((r) => { try { _publishedSolidityContract.Address = r.Result.ContractAddress; _solidityContractsRepository.Insert(_publishedSolidityContract).ContinueWith((t) => { try { if (t.Result) { Application.Current.Dispatcher.Invoke(() => { _viewModel.NewSmartContractAddress = r.Result.ContractAddress; _publishedSolidityContract = null; MainWindowStore.Instance().DisplayMessage("The transaction has been inserted"); }); } else { Application.Current.Dispatcher.Invoke(() => MainWindowStore.Instance().DisplayError("An error occured while trying to insert the smart contract")); } } catch { Application.Current.Dispatcher.Invoke(() => MainWindowStore.Instance().DisplayError("An error occured while trying to insert the smart contract")); } }); } catch (AggregateException) { Application.Current.Dispatcher.Invoke(() => MainWindowStore.Instance().DisplayError("An error occured while trying to insert the smart contract")); } }); }
private void PublishContract(object sender, EventArgs e) { if (_viewModel == null) { return; } var authenticatedWallet = WalletStore.Instance().GetAuthenticatedWallet(); if (authenticatedWallet == null) { MainWindowStore.Instance().DisplayError("You're not authenticated"); return; } if (string.IsNullOrWhiteSpace(_viewModel.SmartContract)) { MainWindowStore.Instance().DisplayError("The solidity contract must be filled in"); return; } var rpcClient = new RpcClient(authenticatedWallet.Network); _publishedSolidityContract = null; rpcClient.CompileSolidity(_viewModel.SmartContract).ContinueWith((t) => { try { var compilationResult = t.Result; if (compilationResult.Infos == null || !compilationResult.Infos.Any()) { return; } UpdateSmartContractDefinition(compilationResult); var newKey = _walletHelper.CreateNewAddress(); var fromAddr = newKey.GetPublicKeyHashed(); var smartContractTransaction = new SmartContractTransaction { From = fromAddr, Data = compilationResult.Infos.First().Code.FromHexString(), Nonce = NonceHelper.GetNonceInt32() }; rpcClient.SendRawTransaction(smartContractTransaction).ContinueWith((c) => { Application.Current.Dispatcher.Invoke(() => { _viewModel.TransactionId = c.Result; }); _publishedSolidityContract = new SolidityContractAggregate { Abi = compilationResult.Infos.First().AbiDefinition.ToString(), Code = compilationResult.Infos.First().Code }; }); } catch (AggregateException) { Application.Current.Dispatcher.Invoke(() => MainWindowStore.Instance().DisplayError("An error occured while trying to build the solidity contract")); } }); }
private void RefreshBlocks() { var authenticatedWallet = WalletStore.Instance().GetAuthenticatedWallet(); if (authenticatedWallet == null) { return; } var rpcClient = new RpcClient(authenticatedWallet.Network); var startIndex = 0; var lastIndex = WalletPageStore.Instance().NbBlocks; var waitBlockHashes = new List <Task <IEnumerable <byte> > >(); for (var i = startIndex; i < lastIndex; i++) { waitBlockHashes.Add(rpcClient.GetBlockHash(i)); } Task.WhenAll(waitBlockHashes.ToArray()).ContinueWith((r) => { var waitAllBlocks = new List <Task <Block> >(); try { var lstBHash = r.Result.ToList(); foreach (var bHash in lstBHash) { if (bHash == null) { continue; } waitAllBlocks.Add(rpcClient.GetBlock(bHash)); } } catch (AggregateException) { } Task.WhenAll(waitAllBlocks.ToArray()).ContinueWith((b) => { try { Application.Current.Dispatcher.Invoke(() => { _viewModel.Blocks.Clear(); }); var blocks = b.Result; foreach (var block in blocks) { if (block == null) { continue; } var blockHash = block.GetHashHeader().ToHexString(); var previousHash = block.BlockHeader.PreviousBlockHeader.ToHexString(); var nbTransactions = block.Transactions.Count(); var fees = block.GetTotalFees(); Application.Current.Dispatcher.Invoke(() => { _viewModel.Blocks.Add(new BlockViewModel { Fees = fees, Hash = blockHash, PreviousHash = previousHash }); }); } } catch (AggregateException) { } }); }); }
private void SendMoney(object sender, EventArgs e) { var authenticatedWallet = WalletStore.Instance().GetAuthenticatedWallet(); if (authenticatedWallet == null) { MainWindowStore.Instance().DisplayError("You're not authenticated"); return; } var receiverValue = _viewModel.SendValue; var addr = _viewModel.SendAddress; var selectedTransaction = _viewModel.SelectedTransaction; if (selectedTransaction == null) { MainWindowStore.Instance().DisplayError("A transaction must be selected"); return; } if (receiverValue > selectedTransaction.Amount) { return; } var txFee = _transactionHelper.GetMinFee(); var senderValue = selectedTransaction.Amount - receiverValue - txFee; var walletAddr = authenticatedWallet.Addresses.FirstOrDefault(a => a.Hash == selectedTransaction.Hash); if (walletAddr == null) { MainWindowStore.Instance().DisplayError("The selected tranasction is not linked to your wallet"); return; } BlockChainAddress bcAddr = null; try { bcAddr = BlockChainAddress.Deserialize(addr); } catch (Exception) { MainWindowStore.Instance().DisplayError("The address is not correct"); } if (bcAddr == null) { MainWindowStore.Instance().DisplayError("The address is not correct"); return; } var newKey = _walletHelper.CreateNewAddress(); var kh = new BigInteger(newKey.GetPublicKeyHashed()); var script = _scriptBuilder.New() .AddToStack(walletAddr.Key.GetSignature()) .AddToStack(walletAddr.Key.GetPublicKey()) .Build(); var pp = walletAddr.Key.GetPublicKeyHashed(); var senderSript = _scriptBuilder.New() // SEND MONEY TO MY WALLET. .AddOperation(OpCodes.OP_DUP) .AddOperation(OpCodes.OP_HASH160) .AddToStack(newKey.GetPublicKeyHashed()) .AddOperation(OpCodes.OP_EQUALVERIFY) .AddOperation(OpCodes.OP_CHECKSIG) .Build(); var receiverScript = _scriptBuilder.New() // SEND MONEY TO THE SELLER. .AddOperation(OpCodes.OP_DUP) .AddOperation(OpCodes.OP_HASH160) .AddToStack(bcAddr.PublicKeyHash) .AddOperation(OpCodes.OP_EQUALVERIFY) .AddOperation(OpCodes.OP_CHECKSIG) .Build(); var txBuilder = _transactionBuilder.NewNoneCoinbaseTransaction() .Spend(selectedTransaction.TxId.FromHexString(), (uint)selectedTransaction.Vout, script.Serialize()) .AddOutput((long)receiverValue, receiverScript); if (senderValue > 0) { txBuilder.AddOutput((long)senderValue, senderSript); } var tx = txBuilder.Build(); var s = tx.Serialize().Count(); var rpcClient = new RpcClient(authenticatedWallet.Network); rpcClient.SendRawTransaction(tx).ContinueWith((r) => { try { var res = r.Result; } catch (AggregateException ex) { var exx = ex.InnerExceptions; } }); }