Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Commit

Permalink
Moved test-related projects to separate directory, removed "Change Pa…
Browse files Browse the repository at this point in the history
…ssword" dialog, small bug fix (#115)

* Moved test-related projects to separate "Tests" directory. Cleaned up and renamed walletcontroller methods. Removed wallet password changing dialog as this functionality is no longer supported

* Fixed bug with incorrect asset URL format

* Moved IWalletController.GetAccountContract method call from AccountsViewModel to ViewContractViewModel
  • Loading branch information
justingaffney committed Dec 29, 2017
1 parent bb485a0 commit 4588181
Show file tree
Hide file tree
Showing 38 changed files with 130 additions and 350 deletions.
15 changes: 9 additions & 6 deletions Neo.Gui.Base/Controllers/IWalletController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public interface IWalletController : IDisposable

void CloseWallet();

bool ChangePassword(string oldPassword, string newPassword);

void CreateNewAccount();

void AddContract(Contract contract);
Expand Down Expand Up @@ -86,7 +84,12 @@ public interface IWalletController : IDisposable

Transaction GetTransaction(UInt256 hash, out int height);

AccountState GetAccountState(UInt160 scriptHash);
/// <summary>
/// Gets the public keys that the specified script hash have voted for.
/// </summary>
/// <param name="scriptHash">Script hash of the account that voted</param>
/// <returns>Enumerable collection of public keys</returns>
IEnumerable<ECPoint> GetVotes(UInt160 scriptHash);

ContractState GetContractState(UInt160 scriptHash);

Expand Down Expand Up @@ -140,10 +143,10 @@ public interface IWalletController : IDisposable

void ClaimAssets();

void ExecuteIssueTransaction(UInt256 assetId, IEnumerable<TransactionOutputItem> items);
void IssueAsset(UInt256 assetId, IEnumerable<TransactionOutputItem> items);

void ExecuteInvocationTransaction(InvocationTransaction transaction);
void InvokeContract(InvocationTransaction transaction);

void ExecuteTransferTransaction(IEnumerable<TransactionOutputItem> items, string remark, UInt160 changeAddress = null, Fixed8 fee = default(Fixed8));
void Transfer(IEnumerable<TransactionOutputItem> items, string remark, UInt160 changeAddress = null, Fixed8 fee = default(Fixed8));
}
}
72 changes: 42 additions & 30 deletions Neo.Gui.Base/Controllers/WalletController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,6 @@ public void CloseWallet()
this.SetCurrentWallet(null, null);
}

public bool ChangePassword(string oldPassword, string newPassword)
{
this.ThrowIfWalletIsNotOpen();

return false;//this.currentWallet.ChangePassword(oldPassword, newPassword);
}

public void CreateNewAccount()
{
this.ThrowIfWalletIsNotOpen();
Expand Down Expand Up @@ -433,9 +426,18 @@ public Transaction GetTransaction(UInt256 hash, out int height)
return this.blockchainController.GetTransaction(hash, out height);
}

public AccountState GetAccountState(UInt160 scriptHash)
public IEnumerable<ECPoint> GetVotes(UInt160 scriptHash)
{
return this.blockchainController.GetAccountState(scriptHash);
var accountState = this.blockchainController.GetAccountState(scriptHash);

if (accountState == null)
{
return Enumerable.Empty<ECPoint>();
}
else
{
return accountState.Votes;
}
}

public ContractState GetContractState(UInt160 scriptHash)
Expand Down Expand Up @@ -719,11 +721,11 @@ public void ClaimAssets()

if (claims.Length == 0) return;

var clainTransaction = this.MakeClaimTransaction(claims);
this.SignAndRelayTransaction(clainTransaction);
var claimTransaction = this.MakeClaimTransaction(claims);
this.SignAndRelayTransaction(claimTransaction);
}

public void ExecuteIssueTransaction(UInt256 assetId, IEnumerable<TransactionOutputItem> items)
public void IssueAsset(UInt256 assetId, IEnumerable<TransactionOutputItem> items)
{
var issueTransaction = this.MakeTransaction(new IssueTransaction
{
Expand All @@ -739,7 +741,7 @@ public void ExecuteIssueTransaction(UInt256 assetId, IEnumerable<TransactionOutp
this.SignAndRelayTransaction(issueTransaction);
}

public void ExecuteInvocationTransaction(InvocationTransaction transaction)
public void InvokeContract(InvocationTransaction transaction)
{
var transactionFee = transaction.Gas.Equals(Fixed8.Zero) ? NetworkFee : Fixed8.Zero;

Expand All @@ -756,7 +758,7 @@ public void ExecuteInvocationTransaction(InvocationTransaction transaction)
this.SignAndRelayTransaction(transactionWithFee);
}

public void ExecuteTransferTransaction(IEnumerable<TransactionOutputItem> items, string remark, UInt160 changeAddress = null, Fixed8 fee = default(Fixed8))
public void Transfer(IEnumerable<TransactionOutputItem> items, string remark, UInt160 changeAddress = null, Fixed8 fee = default(Fixed8))
{
var cOutputs = items.Where(p => p.AssetId is UInt160).GroupBy(p => new
{
Expand Down Expand Up @@ -869,8 +871,10 @@ public void ExecuteTransferTransaction(IEnumerable<TransactionOutputItem> items,
}

if (tx == null) return;

if (tx is InvocationTransaction invocationTransaction)
{
// TODO Remove IDialogManager dependency
this.dialogManager.ShowDialog<InvokeContractDialogResult, InvokeContractLoadParameters>(new InvokeContractLoadParameters(invocationTransaction));
}
else
Expand Down Expand Up @@ -1018,21 +1022,27 @@ private void SetCurrentWallet(Wallet wallet, IDisposable walletLocker)
}

// Load transactions
var walletTransactions = this.currentWallet.GetTransactions();
var walletTransactionHashes = this.currentWallet.GetTransactions();

foreach (var i in walletTransactions.Select(p => new
// Get transaction information from transaction hashes
var walletTransactions = new List<TransactionItem>();
foreach (var transactionHash in walletTransactionHashes)
{
Transaction = this.blockchainController.GetTransaction(p, out int height),
Height = (uint)height
}).Where(p => p.Transaction != null).Select(p => new
var transaction = this.blockchainController.GetTransaction(transactionHash, out var height);

if (transaction == null) continue;

var transactionTime = this.blockchainController.GetTimeOfBlock((uint) height);

walletTransactions.Add(new TransactionItem(transactionHash, transaction.Type, (uint) height, transactionTime));
}

// Add transactions to wallet info, ordered by time
var orderedTransactions = walletTransactions.OrderBy(item => item.Time);

foreach (var transactionItem in orderedTransactions)
{
p.Transaction,
p.Height,
Time = this.blockchainController.GetTimeOfBlock(p.Height)
}).OrderBy(p => p.Time))
{
this.AddTransaction(i.Transaction, i.Height, i.Time);
this.AddTransaction(transactionItem);
}

this.currentWallet.BalanceChanged += this.CurrentWalletBalanceChanged;
Expand All @@ -1046,10 +1056,14 @@ private void SetCurrentWallet(Wallet wallet, IDisposable walletLocker)

private void CurrentWalletBalanceChanged(object sender, BalanceEventArgs e)
{
var transaction = e.Transaction;

// TODO Check this logic is correct
var transactionHeight = e.Height ?? this.blockchainController.BlockHeight;

this.AddTransaction(e.Transaction, transactionHeight, TimeHelper.UnixTimestampToDateTime(e.Time));
var transactionItem = new TransactionItem(transaction.Hash, transaction.Type, transactionHeight, TimeHelper.UnixTimestampToDateTime(e.Time));

this.AddTransaction(transactionItem);

this.SetWalletBalanceChangedFlag();
}
Expand Down Expand Up @@ -1258,11 +1272,9 @@ private void UpdateNEP5TokenBalances(TimeSpan timeSinceLastBlock)
checkNep5Balance = false;
}

private void AddTransaction(Transaction transaction, uint blockHeight, DateTime transactionTime)
private void AddTransaction(TransactionItem transaction)
{
var transactionItem = new TransactionItem(transaction.Hash, transaction.Type, blockHeight, transactionTime);

this.currentWalletInfo.AddTransaction(transactionItem);
this.currentWalletInfo.AddTransaction(transaction);

// TODO Replace with a TransactionAddedMessage
this.messagePublisher.Publish(new TransactionsHaveChangedMessage(this.currentWalletInfo.GetTransactions()));
Expand Down
11 changes: 7 additions & 4 deletions Neo.Gui.Base/Data/TransactionItem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;

using Neo.Core;

using Neo.Gui.Globalization.Resources;

using Neo.Gui.Base.Helpers;
using Neo.Gui.Base.MVVM;

Expand All @@ -13,13 +16,13 @@ public class TransactionItem : BindableClass
#endregion

#region Public Properties
public UInt256 Hash { get; private set; }
public UInt256 Hash { get; }

public uint Height { get; private set; }
public uint Height { get; }

public DateTime Time { get; private set; }
public DateTime Time { get; }

public TransactionType Type { get; private set; }
public TransactionType Type { get; }

public string Confirmations => this.confirmations > 0 ? this.confirmations.ToString() : Strings.Unconfirmed;
#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using Neo.SmartContract;

namespace Neo.Gui.Base.Dialogs.LoadParameters.Accounts
namespace Neo.Gui.Base.Dialogs.LoadParameters.Accounts
{
public class ViewContractLoadParameters
{
public Contract Contract { get; }
public UInt160 ScriptHash { get; }

public ViewContractLoadParameters(Contract contract)
public ViewContractLoadParameters(UInt160 scriptHash)
{
this.Contract = contract;
this.ScriptHash = scriptHash;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
using Neo.Wallets;

namespace Neo.Gui.Base.Dialogs.LoadParameters.Accounts
namespace Neo.Gui.Base.Dialogs.LoadParameters.Accounts
{
public class ViewPrivateKeyLoadParameters
{
public UInt160 ScriptHash { get; }

public KeyPair Key { get; }

public ViewPrivateKeyLoadParameters(UInt160 scriptHash, KeyPair key)
public ViewPrivateKeyLoadParameters(UInt160 scriptHash)
{
this.ScriptHash = scriptHash;
this.Key = key;
}
}
}

This file was deleted.

2 changes: 1 addition & 1 deletion Neo.Gui.Base/Messages/TransactionsHaveChangedMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Neo.Gui.Base.Messages
{
public class TransactionsHaveChangedMessage
{
public IEnumerable<TransactionItem> Transactions { get; private set; }
public IEnumerable<TransactionItem> Transactions { get; }

public TransactionsHaveChangedMessage(IEnumerable<TransactionItem> transactions)
{
Expand Down
5 changes: 2 additions & 3 deletions Neo.Gui.ViewModels/Accounts/CreateLockAccountViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
using Neo.VM;
using Neo.Wallets;

using Neo.Gui.Base.Controllers;
using Neo.Gui.Base.Messages;
using Neo.Gui.Base.Messaging.Interfaces;
using Neo.Gui.Globalization.Resources;

using Neo.Gui.Base.Controllers;
using Neo.Gui.Base.Dialogs.Interfaces;
using Neo.Gui.Base.Dialogs.Results.Wallets;
using Neo.Gui.Base.Managers;
Expand Down
20 changes: 18 additions & 2 deletions Neo.Gui.ViewModels/Accounts/ViewContractViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using Neo.SmartContract;

using Neo.Gui.Base.Controllers;
using Neo.Gui.Base.Dialogs.Interfaces;
using Neo.Gui.Base.Dialogs.LoadParameters.Accounts;
using Neo.Gui.Base.Dialogs.Results.Wallets;
Expand All @@ -16,6 +17,19 @@ namespace Neo.Gui.ViewModels.Accounts
public class ViewContractViewModel : ViewModelBase,
ILoadableDialogViewModel<ViewContractDialogResult, ViewContractLoadParameters>
{
#region Private fields

private readonly IWalletController walletController;
#endregion

#region Constructor
public ViewContractViewModel(
IWalletController walletController)
{
this.walletController = walletController;
}
#endregion

#region Public Properties
public string Address { get; private set; }

Expand All @@ -37,9 +51,11 @@ public class ViewContractViewModel : ViewModelBase,

public void OnDialogLoad(ViewContractLoadParameters parameters)
{
if (parameters?.Contract == null) return;
if (parameters?.ScriptHash == null) return;

var contract = this.walletController.GetAccountContract(parameters.ScriptHash);

this.SetContract(parameters.Contract);
this.SetContract(contract);
}
#endregion

Expand Down
15 changes: 4 additions & 11 deletions Neo.Gui.ViewModels/Accounts/ViewPrivateKeyViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,24 @@ public class ViewPrivateKeyViewModel : ViewModelBase,

public void OnDialogLoad(ViewPrivateKeyLoadParameters parameters)
{
if (parameters == null || parameters.Key == null || parameters.ScriptHash == null) return;
if (parameters == null || parameters.ScriptHash == null) return;

this.SetAccountInfo(parameters.Key, parameters.ScriptHash);
}
#endregion

#region Private Methods
var key = this.walletController.GetAccountKey(parameters.ScriptHash);

private void SetAccountInfo(KeyPair key, UInt160 scriptHash)
{
this.Address = this.walletController.ToAddress(scriptHash);
this.Address = this.walletController.ToAddress(parameters.ScriptHash);
this.PublicKeyHex = key.PublicKey.EncodePoint(true).ToHexString();
using (key.Decrypt())
{
this.PrivateKeyHex = key.PrivateKey.ToHexString();
}
this.PrivateKeyWif = key.Export();

// Update properties
RaisePropertyChanged(nameof(this.Address));
RaisePropertyChanged(nameof(this.PublicKeyHex));
RaisePropertyChanged(nameof(this.PrivateKeyHex));
RaisePropertyChanged(nameof(this.PrivateKeyWif));
}

#endregion
}
}
2 changes: 1 addition & 1 deletion Neo.Gui.ViewModels/Assets/AssetDistributionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public bool DistributionEnabled

private void Confirm()
{
this.walletController.ExecuteIssueTransaction((UInt256)this.Asset.AssetId, this.Items);
this.walletController.IssueAsset((UInt256)this.Asset.AssetId, this.Items);

this.Close(this, EventArgs.Empty);
}
Expand Down
2 changes: 1 addition & 1 deletion Neo.Gui.ViewModels/Contracts/InvokeContractViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ private void Invoke()

if (this.transaction == null) return;

this.walletController.ExecuteInvocationTransaction(this.transaction);
this.walletController.InvokeContract(this.transaction);

this.Close(this, EventArgs.Empty);
}
Expand Down
Loading

0 comments on commit 4588181

Please sign in to comment.