Skip to content

Commit

Permalink
Merge pull request #91 from phantasma-io/development
Browse files Browse the repository at this point in the history
Update to Version 2.8.11
  • Loading branch information
TeknoPT authored Feb 7, 2024
2 parents 4081451 + 51f2d9f commit 23a8dff
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Assets/Neo2Legacy/Core/NeoAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public static byte[] GenerateScript(UInt160 scriptHash, object[] args, bool addN

public void GenerateInputsOutputs(Action<EPHANTASMA_SDK_ERROR_TYPE, string> errorHandlingCallback, UnspentEntries unspent, NeoKeys key, string symbol, IEnumerable<Transaction.Output> targets, out List<Transaction.Input> inputs, out List<Transaction.Output> outputs, decimal system_fee = 0, bool allowSameSourceAndDest = false)
{
var from_script_hash = new UInt160(key.signatureHash.ToArray());
var from_script_hash = new UInt160(key.signatureScriptN2.ToArray());
var info = GetAssetsInfo();
var targetAssetID = NeoUtils.ReverseHex(info[symbol]).HexToBytes();
if (targets != null)
Expand Down
57 changes: 47 additions & 10 deletions Assets/Neo2Legacy/Core/NeoKeys.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
using Poltergeist.PhantasmaLegacy.Cryptography;
using Poltergeist.PhantasmaLegacy.Neo2;
using System;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using NBitcoin.DataEncoders;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Math.EC;
using Phantasma.Core.Cryptography.ECDsa;
using Phantasma.Core.Cryptography;
using Phantasma.Core.Numerics;
using Phantasma.Core.Utils;
using Poltergeist.Neo2.Utils;
using UnityEngine;
using ECCurve = System.Security.Cryptography.ECCurve;
using ECPoint = System.Security.Cryptography.ECPoint;

namespace Poltergeist.Neo2.Core
{
Expand All @@ -16,28 +26,37 @@ public class NeoKeys
public readonly byte[] CompressedPublicKey;
public readonly UInt160 PublicKeyHash;
public readonly string Address;
public readonly string AddressN3;
public readonly string WIF;

public readonly UInt160 signatureHash;
public readonly byte[] signatureScript;
public readonly UInt160 signatureHashN2;
public readonly UInt160 signatureHashN3;
public readonly byte[] signatureScriptN2;
public readonly byte[] signatureScriptN3;

public NeoKeys(byte[] privateKey)
{
if (privateKey.Length != 32 && privateKey.Length != 96 && privateKey.Length != 104)
throw new ArgumentException();
this.PrivateKey = new byte[32];
privateKey= privateKey[^32..];
Buffer.BlockCopy(privateKey, privateKey.Length - 32, PrivateKey, 0, 32);
this.PrivateKey = privateKey[^32..];

this.CompressedPublicKey = ECDsa.GetPublicKey(privateKey, true, ECDsaCurve.Secp256r1);

this.CompressedPublicKey = ECDsa.GetPublicKey(this.PrivateKey, true, ECDsaCurve.Secp256r1);
this.PublicKeyHash = NeoUtils.ToScriptHash(this.CompressedPublicKey);

this.signatureScript = CreateSignatureScript(this.CompressedPublicKey);
signatureHash = NeoUtils.ToScriptHash(signatureScript);

this.PublicKey = ECDsa.GetPublicKey(privateKey, false, ECDsaCurve.Secp256r1).Skip(1).ToArray();

this.Address = NeoUtils.ToAddress(signatureHash);
this.signatureScriptN2 = CreateSignatureScript(this.CompressedPublicKey);
signatureHashN2 = NeoUtils.ToScriptHash(signatureScriptN2);

this.signatureScriptN3 = CreateSignatureScriptN3(this.CompressedPublicKey);
signatureHashN3 = NeoUtils.ToScriptHash(signatureScriptN3);

this.PublicKey = ECDsa.GetPublicKey(this.PrivateKey, false, ECDsaCurve.Secp256r1).Skip(1).ToArray();

this.Address = NeoUtils.ToAddress(signatureHashN2);
this.AddressN3 = NeoUtils.ToAddressN3(signatureHashN3);
this.WIF = GetWIF();
}

Expand Down Expand Up @@ -105,6 +124,24 @@ public static byte[] CreateSignatureScript(byte[] bytes)

return script;
}

public static byte[] CreateSignatureScriptN3(byte[] bytes)
{
var sb = new ScriptBuilder();
sb.EmitPush(EncodePoint(bytes));
sb.Emit(OpCode.SYSCALL, BitConverter.GetBytes(666101590));
var endScript = sb.ToArray();

return endScript;
}

public static byte[] EncodePoint(byte[] bytes)
{
byte[] data = new byte[33];
Array.Copy(bytes, 0, data, 33 - bytes.Length, bytes.Length);
data[0] = (byte)0x03;
return data;
}

private string GetWIF()
{
Expand Down
8 changes: 4 additions & 4 deletions Assets/Neo2Legacy/Core/Opcodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public enum OpCode : byte
PUSHBYTES73 = 0x49,
PUSHBYTES74 = 0x4A,
PUSHBYTES75 = 0x4B,
PUSHDATA1 = 0x4C, // The next byte contains the number of bytes to be pushed onto the stack.
PUSHDATA2 = 0x4D, // The next two bytes contain the number of bytes to be pushed onto the stack.
PUSHDATA4 = 0x4E, // The next four bytes contain the number of bytes to be pushed onto the stack.
PUSHDATA1 = 0x0C, // The next byte contains the number of bytes to be pushed onto the stack.
PUSHDATA2 = 0x0D, // The next two bytes contain the number of bytes to be pushed onto the stack.
PUSHDATA4 = 0x0E, // The next four bytes contain the number of bytes to be pushed onto the stack.
PUSHM1 = 0x4F, // The number -1 is pushed onto the stack.
PUSH1 = 0x51, // The number 1 is pushed onto the stack.
PUSHT = PUSH1,
Expand Down Expand Up @@ -111,7 +111,7 @@ public enum OpCode : byte
CALL = 0x65,
RET = 0x66,
APPCALL = 0x67,
SYSCALL = 0x68,
SYSCALL = 0x41,
TAILCALL = 0x69,


Expand Down
4 changes: 2 additions & 2 deletions Assets/Neo2Legacy/Core/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,11 @@ public void Sign(NeoKeys key, IEnumerable<Witness> witnesses = null)
var signature = CryptoUtils.Sign(txdata, privkey, pubkey);

var invocationScript = new byte[] { (byte)OpCode.PUSHBYTES64 }.Concat(signature).ToArray();
var verificationScript = new byte[key.signatureScript.Length];
var verificationScript = new byte[key.signatureScriptN2.Length];

for (int i=0; i<verificationScript.Length; i++)
{
verificationScript[i] = key.signatureScript[i];
verificationScript[i] = key.signatureScriptN2[i];
}

witList.Add(new Witness() { invocationScript = invocationScript, verificationScript = verificationScript });
Expand Down
17 changes: 16 additions & 1 deletion Assets/Neo2Legacy/Utils/NeoUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Phantasma.Core.Cryptography;
using Phantasma.Core.Cryptography;
using Phantasma.Core.Cryptography.Hashing;
using Phantasma.Core.Numerics;
using Poltergeist.PhantasmaLegacy.Cryptography;
Expand Down Expand Up @@ -184,6 +184,21 @@ public static string ToAddress(this UInt160 scriptHash)
Buffer.BlockCopy(scriptHash.ToArray(), 0, data, 1, 20);
return data.Base58CheckEncode();
}

public static string ToAddressN3(this UInt160 scriptHash)
{
byte[] data = new byte[21];
data[0] = 53;
Buffer.BlockCopy(scriptHash.ToArray(), 0, data, 1, 20);
return data.Base58CheckEncode();
}
public static string ToAddress(this UInt160 scriptHash, byte version)
{
Span<byte> data = stackalloc byte[21];
data[0] = version;
scriptHash.ToArray().CopyTo(data[1..]);
return data.ToArray().Base58CheckEncode();
}

public static UInt160 ToScriptHash(this byte[] script)
{
Expand Down
8 changes: 2 additions & 6 deletions Assets/Neo2Legacy/Utils/Script.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,8 @@ public ScriptBuilder EmitPush(byte[] data)
{
if (data == null)
throw new ArgumentNullException();
if (data.Length <= (int)OpCode.PUSHBYTES75)
{
ms.WriteByte((byte)data.Length);
ms.Write(data, 0, data.Length);
}
else if (data.Length < 0x100)

if (data.Length < 0x100)
{
Emit(OpCode.PUSHDATA1);
ms.WriteByte((byte)data.Length);
Expand Down
18 changes: 18 additions & 0 deletions Assets/Phantasma/Phantasma.Core/src/Cryptography/ECDsa/ECDsa.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Security;
using Poltergeist.PhantasmaLegacy.Ethereum.Hex.HexConvertors.Extensions;

namespace Phantasma.Core.Cryptography.ECDsa
{
Expand Down Expand Up @@ -34,10 +35,27 @@ public static byte[] GetPublicKey(byte[] privateKey, bool compressed, ECDsaCurve

var d = new BigInteger(1, privateKey);
var q = dom.G.Multiply(d);


var publicParams = new ECPublicKeyParameters(q, dom);
return publicParams.Q.GetEncoded(compressed);
}

public static ECDomainParameters GetDomain(ECDsaCurve curve)
{
X9ECParameters ecCurve;
switch (curve)
{
case ECDsaCurve.Secp256k1:
ecCurve = SecNamedCurves.GetByName("secp256k1");
break;
default:
ecCurve = SecNamedCurves.GetByName("secp256r1");
break;
}

return new ECDomainParameters(ecCurve.Curve, ecCurve.G, ecCurve.N, ecCurve.H);
}

public static byte[] Sign(byte[] message, byte[] prikey, ECDsaCurve curve)
{
Expand Down
36 changes: 36 additions & 0 deletions Assets/Phantasma/Phantasma.Core/src/Domain/WalletLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ public struct NexusResult : IAPIResult
public string nexus;
}

public struct N3Address : IAPIResult
{
public string address;
}

public struct WalletVersion : IAPIResult
{
public string version;
Expand Down Expand Up @@ -154,6 +159,7 @@ private Connection ValidateRequest(string[] args)
protected abstract void GetPeer(Action<string> callback);

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

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

Expand Down Expand Up @@ -319,6 +325,30 @@ private void HandleGetNexus(string[] args, Connection connection, int id, Action
}
#endregion

#region N3 Address

private void HandleGetN3Address(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 = $"getN3Address: Invalid amount of arguments: {args.Length}" });
callback(id, answer, success);
_isPendingRequest = false;
return;
}

GetN3Address((address) => {
success = true;
answer = APIUtils.FromAPIResult(new N3Address() { address = address });
callback(id, answer, success);
_isPendingRequest = false;
});
}
#endregion

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

case "getN3Address":
{
HandleGetN3Address(args, connection, id, callback);
return;
}

case "getWalletVersion":
{
Expand Down
45 changes: 42 additions & 3 deletions Assets/Scripts/Wallet/AccountManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using UnityEngine;

using System;
using System.IO;
using System.Linq;
using Phantasma.SDK;
using Poltergeist.Neo2.Core;
Expand Down Expand Up @@ -737,7 +738,6 @@ void Start()
var walletVersion = PlayerPrefs.GetInt(WalletVersionTag, 1);

var wallets = PlayerPrefs.GetString(WalletTag, "");

Accounts = new List<Account>();

if (walletVersion == 1 && !string.IsNullOrEmpty(wallets))
Expand Down Expand Up @@ -808,7 +808,24 @@ void Start()
else if (!string.IsNullOrEmpty(wallets))
{
var bytes = Base16.Decode(wallets);
Accounts = Serialization.Unserialize<Account[]>(bytes).ToList();
try
{
List<Account >accountsTemp = new List<Account>();
var reader = new BinaryReader(new MemoryStream(bytes));
var size = reader.ReadVarInt();
for (int i = 0; i < (int)size; i++)
{
var account = new Account();
account.UnserializeData(reader);
accountsTemp.Add(account);
}

Accounts = accountsTemp; // = Serialization.Unserialize<Account[]>(bytes).ToList();
}
catch (Exception e)
{
Log.WriteError("Error deserializing accounts: " + e);
}
}

if (walletVersion == 2)
Expand All @@ -830,8 +847,17 @@ void Start()
public void SaveAccounts()
{
PlayerPrefs.SetInt(WalletVersionTag, 3);
MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(stream);
Accounts.ForEach(acc => acc.version = 3);

writer.WriteVarInt(Accounts.Count);
foreach (var account in Accounts)
{
account.SerializeData(writer);
}

var bytes = Serialization.Serialize(Accounts.ToArray());
var bytes = stream.ToArray();//Serialization.Serialize(Accounts.ToArray());
PlayerPrefs.SetString(WalletTag, Base16.Encode(bytes));
PlayerPrefs.Save();
}
Expand Down Expand Up @@ -1863,6 +1889,7 @@ public void RefreshBalances(bool force, PlatformKind platforms = PlatformKind.No
{
var keys = PhantasmaKeys.FromWIF(wif);
var ethKeys = EthereumKey.FromWIF(wif);
UpdateOpenAccount();
StartCoroutine(phantasmaApi.GetAccount(keys.Address.Text, (acc) =>
{
var balanceMap = new Dictionary<string, Balance>();
Expand Down Expand Up @@ -3236,6 +3263,7 @@ public int AddWallet(string name, string wif, string password, bool legacySeed)

var neoKeys = NeoKeys.FromWIF(wif);
account.neoAddress = neoKeys.Address.ToString();
account.neoAddress = neoKeys.AddressN3.ToString();

var ethereumAddressUtil = new Poltergeist.PhantasmaLegacy.Ethereum.Util.AddressUtil();
account.ethAddress = ethereumAddressUtil.ConvertToChecksumAddress(EthereumKey.FromWIF(wif).Address);
Expand Down Expand Up @@ -3982,5 +4010,16 @@ public void GetPhantasmaAddressInfo(string addressString, Action<string, string>
});
}

public void UpdateOpenAccount()
{
NeoKeys neoKeys = NeoKeys.FromWIF(CurrentWif);
var SelectedAccount = CurrentAccount;
SelectedAccount.neoAddressN3 = neoKeys.AddressN3;
SelectedAccount.neoAddress = neoKeys.Address;
SelectedAccount.version = 3;
Accounts[CurrentIndex] = SelectedAccount;
SaveAccounts();
}
}
}
Loading

0 comments on commit 23a8dff

Please sign in to comment.