Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements and Bug fix's #88

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 79 additions & 6 deletions Assets/Phantasma/Phantasma.Core/src/Domain/WalletLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ public struct Signature : IAPIResult
public string signature;
public string random;
}

public struct Peer : IAPIResult
{
public string peer;
}

public struct NexusResult : IAPIResult
{
public string nexus;
}

public struct WalletVersion : IAPIResult
{
public string version;
}

public class Connection
{
Expand Down Expand Up @@ -139,7 +154,9 @@ private Connection ValidateRequest(string[] args)
protected abstract void GetPeer(Action<string> callback);

protected abstract void GetNexus(Action<string> callback);


protected abstract void GetWalletVersion(Action<string> callback);

protected abstract void InvokeScript(string chain, byte[] script, int id, Action<string[], string> callback);

// NOTE for security, signData should not be usable as a way of signing transaction. That way the wallet is responsible for appending random bytes to the message, and return those in callback
Expand Down Expand Up @@ -278,13 +295,37 @@ private void HandleGetAccount(string[] args, Connection connection, int id, Acti
}
#endregion

#region Nexus

private void HandleGetNexus(string[] args, Connection connection, int id, Action<int, DataNode, bool> callback)
{
DataNode answer;
bool success = false;

if (args.Length > 1)
{
answer = APIUtils.FromAPIResult(new Error() { message = $"getNexus: Invalid amount of arguments: {args.Length}" });
callback(id, answer, success);
_isPendingRequest = false;
return;
}

GetNexus((nexus) => {
success = true;
answer = APIUtils.FromAPIResult(new NexusResult { nexus = nexus });
callback(id, answer, success);
_isPendingRequest = false;
});
}
#endregion

#region Peer
private void HandleGetPeer(string[] args, Connection connection, int id, Action<int, DataNode, bool> callback)
{
DataNode answer;
bool success = false;

if (args.Length != 0)
if (args.Length > 1)
{
answer = APIUtils.FromAPIResult(new Error() { message = $"getPeer: Invalid amount of arguments: {args.Length}" });
callback(id, answer, success);
Expand All @@ -294,16 +335,36 @@ private void HandleGetPeer(string[] args, Connection connection, int id, Action<

GetPeer((peer) => {
success = true;
SingleResult result;
result.value = peer;
answer = APIUtils.FromAPIResult(result);

answer = APIUtils.FromAPIResult(new Peer() { peer = peer });
callback(id, answer, success);
_isPendingRequest = false;
});
}
#endregion

#region Wallet Version
private void HandleGetWalletVersion(string[] args, Connection connection, int id, Action<int, DataNode, bool> callback)
{
DataNode answer;
bool success = false;

if (args.Length > 1)
{
answer = APIUtils.FromAPIResult(new Error() { message = $"getWalletVersion: Invalid amount of arguments: {args.Length}" });
callback(id, answer, success);
_isPendingRequest = false;
return;
}

GetWalletVersion((version) => {
success = true;
answer = APIUtils.FromAPIResult(new WalletVersion() { version = version });
callback(id, answer, success);
_isPendingRequest = false;
});
}
#endregion

#region Sign Data

private void HandleSignData(string[] args, Connection connection, int id, Action<int, DataNode, bool> callback)
Expand Down Expand Up @@ -786,6 +847,12 @@ public void Execute(string cmd, Action<int, DataNode, bool> callback)
HandleGetPeer(args, connection, id, callback);
return;
}

case "getWalletVersion":
{
HandleGetWalletVersion(args, connection, id, callback);
return;
}

case "signData":
{
Expand Down Expand Up @@ -817,6 +884,12 @@ public void Execute(string cmd, Action<int, DataNode, bool> callback)
return;
}

case "getNexus":
{
HandleGetNexus(args, connection, id, callback);
return;
}

case "writeArchive":
{
HandleWriteArchive(args, connection, id, callback);
Expand Down
38 changes: 36 additions & 2 deletions Assets/Scripts/Wallet/AccountManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private void Awake()

var platforms = new List<PlatformKind>();
platforms.Add(PlatformKind.Phantasma);
//platforms.Add(PlatformKind.Neo);
platforms.Add(PlatformKind.Neo);
platforms.Add(PlatformKind.Ethereum);
platforms.Add(PlatformKind.BSC);

Expand Down Expand Up @@ -1568,6 +1568,26 @@ public void SelectAccount(int index)
platforms.Add(PlatformKind.BSC);
}

if (!platforms.Contains(PlatformKind.Neo))
{
var account = Accounts[_selectedAccountIndex];
account.platforms |= PlatformKind.Neo;
Accounts[_selectedAccountIndex] = account;

_states[PlatformKind.Neo] = new AccountState()
{
platform = PlatformKind.Neo,
address = GetAddress(CurrentIndex, PlatformKind.Neo),
balances = new Balance[0],
flags = AccountFlags.None,
name = ValidationUtils.ANONYMOUS_NAME,
};

SaveAccounts();

platforms.Add(PlatformKind.Neo);
}

CurrentPlatform = platforms.FirstOrDefault();
_states.Clear();

Expand Down Expand Up @@ -2057,7 +2077,21 @@ public void RefreshBalances(bool force, PlatformKind platforms = PlatformKind.No

case PlatformKind.Neo:
{
ReportWalletBalance(platform, null);
var keys = NeoKeys.FromWIF(wif);
var state = new AccountState()
{
platform = platform,
address = keys.Address,
name = "Anonymous",
balances = new Balance[0],
flags = AccountFlags.None,
archives = new Archive[0],
avatarData = "",
usedStorage = 0,
availableStorage = 0,
stakeTime = 0,
};
ReportWalletBalance(platform, state);
/*var keys = NeoKeys.FromWIF(wif);

var url = GetNeoscanAPIUrl($"get_balance/{keys.Address}");
Expand Down
5 changes: 5 additions & 0 deletions Assets/Scripts/Wallet/AccountManagerClasses/AccountState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public class AccountState

public decimal GetAvailableAmount(string symbol)
{
if (balances == null)
{
return 0;
}

for (int i = 0; i < balances.Length; i++)
{
var entry = balances[i];
Expand Down
7 changes: 7 additions & 0 deletions Assets/Scripts/Wallet/WalletGUIFolder/WalletGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2386,6 +2386,7 @@ private void DoBalanceScreen()
{
accountManager.RefreshBalances(false, accountManager.CurrentPlatform);
});

var endY = DoBottomMenu();

if (accountManager.BalanceRefreshing)
Expand All @@ -2410,6 +2411,12 @@ private void DoBalanceScreen()

decimal feeBalance = state.GetAvailableAmount("KCAL");

if (state.balances == null)
{
DrawCenteredText($"No assets found in this {accountManager.CurrentPlatform} account.");
return;
}

var balanceCount = DoScrollArea<Balance>(ref balanceScroll, startY, endY, VerticalLayout ? Units(7) : Units(6), state.balances.Where(x => x.Total >= 0.001m),
DoBalanceEntry);

Expand Down
37 changes: 31 additions & 6 deletions Assets/Scripts/Wallet/WalletLink/WalletConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
using Phantasma.Core.Numerics;
using Phantasma.Core.Utils;
using Phantasma.SDK;
using Poltergeist.Neo2.Core;
using Poltergeist.PhantasmaLegacy.Ethereum;
using UnityEngine.Device;

namespace Poltergeist
{
Expand Down Expand Up @@ -144,6 +146,11 @@ protected override void GetAccount(string platform, int version, Action<Account,
}
else
{
if (state.balances == null)
{
state.balances = new Poltergeist.Balance[0];
}

balances = state.balances.Select(x => new Balance()
{
symbol = x.Symbol,
Expand Down Expand Up @@ -174,7 +181,14 @@ protected override void GetNexus(Action<string> callback)
{
callback(AccountManager.Instance.Settings.nexusName);
}


protected override void GetWalletVersion(Action<string> callback)
{
callback(Application.version.StartsWith("v")
? Application.version.Substring(1)
: Application.version);
}


protected override void InvokeScript(string chain, byte[] script, int id, Action<string[], string> callback)
{
Expand Down Expand Up @@ -451,7 +465,7 @@ protected override void SignData(string platform, SignatureKind kind, byte[] dat
var accountManager = AccountManager.Instance;

var targetPlatform = RequestPlatform(platform);
if (targetPlatform == PlatformKind.None || targetPlatform == PlatformKind.Neo)
if (targetPlatform == PlatformKind.None)
{
callback(null, null, "Unsupported platform: " + platform);
return;
Expand Down Expand Up @@ -484,18 +498,29 @@ protected override void SignData(string platform, SignatureKind kind, byte[] dat
Phantasma.Core.Cryptography.Signature signature;

var wif = account.GetWif(AccountManager.Instance.CurrentPasswordHash);
var phantasmaKeys = PhantasmaKeys.FromWIF(wif);

switch (kind)
{
case SignatureKind.Ed25519:
var phantasmaKeys = PhantasmaKeys.FromWIF(wif);
signature = phantasmaKeys.Sign(msg);
break;

case SignatureKind.ECDSA:
var ethKeys = EthereumKey.FromWIF(wif);
var signatureBytes = Poltergeist.PhantasmaLegacy.Cryptography.CryptoUtils.Sign(msg, ethKeys.PrivateKey, ethKeys.PublicKey, ECDsaCurve.Secp256k1);
signature = new ECDsaSignature(signatureBytes, ECDsaCurve.Secp256k1);

if ( targetPlatform == PlatformKind.Ethereum || targetPlatform == PlatformKind.BSC)
{
var ethKeys = EthereumKey.FromWIF(wif);

var signatureBytes = Poltergeist.PhantasmaLegacy.Cryptography.CryptoUtils.Sign(msg, ethKeys.PrivateKey, ethKeys.PublicKey, ECDsaCurve.Secp256k1);
signature = new ECDsaSignature(signatureBytes, ECDsaCurve.Secp256k1);
}
else
{
var neoKeys = NeoKeys.FromWIF(wif);
var signatureBytes = Poltergeist.PhantasmaLegacy.Cryptography.CryptoUtils.Sign(msg, neoKeys.PrivateKey, neoKeys.CompressedPublicKey, ECDsaCurve.Secp256k1);
signature = new ECDsaSignature(signatureBytes, ECDsaCurve.Secp256k1);
}
break;

default:
Expand Down
4 changes: 2 additions & 2 deletions ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ PlayerSettings:
vulkanEnableLateAcquireNextImage: 0
vulkanEnableCommandBufferRecycling: 1
loadStoreDebugModeEnabled: 0
bundleVersion: 2.8.8
bundleVersion: 2.8.9
preloadedAssets: []
metroInputSource: 0
wsaTransparentSwapchain: 0
Expand Down Expand Up @@ -164,7 +164,7 @@ PlayerSettings:
iPhone: 0
tvOS: 0
overrideDefaultApplicationIdentifier: 1
AndroidBundleVersionCode: 36
AndroidBundleVersionCode: 37
AndroidMinSdkVersion: 22
AndroidTargetSdkVersion: 33
AndroidPreferredInstallLocation: 1
Expand Down