Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
58 changes: 58 additions & 0 deletions Substrate.NET.Wallet.Test/EncodingTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using NUnit.Framework;
using Substrate.NetApi;
using System;
using System.IO;

namespace Substrate.NET.Wallet.Test
{
public class EncodingTest
{
private Random _random;

[SetUp]
public void Setup()
{
SystemInteraction.ReadData = f => File.ReadAllText(Path.Combine(Environment.CurrentDirectory, f));
SystemInteraction.DataExists = f => File.Exists(Path.Combine(Environment.CurrentDirectory, f));
SystemInteraction.ReadPersistent = f => File.ReadAllText(Path.Combine(Environment.CurrentDirectory, f));
SystemInteraction.PersistentExists = f => File.Exists(Path.Combine(Environment.CurrentDirectory, f));
SystemInteraction.Persist = (f, c) => File.WriteAllText(Path.Combine(Environment.CurrentDirectory, f), c);

_random = new Random();
}

[Test]
public void EncryptionTest()
{
var origData = new byte[_random.Next(10, 500)];
_random.NextBytes(origData);

var salt = new byte[32];
_random.NextBytes(salt);

var encryptedData = Wallet.Encrypt(origData, "aA1234dd", salt);
var reprData = Wallet.Decrypt(encryptedData, "aA1234dd", salt);

Assert.AreEqual(origData, reprData);
}

[Test]
public void EncryptionSaltTest()
{
var address = "5CcaF7yE6YU67TyPHjSwd9DKiVBTAS2AktdxNG3DeLYs63gF";

var seed = new byte[16];
_random.NextBytes(seed);

var hash = new byte[16];
_random.NextBytes(hash);

var salt = Wallet.GetSalt(Utils.GetPublicKeyFrom(address), hash);

var encodedData = Wallet.Encrypt(seed, "aA1234dd", salt);
var reprData = Wallet.Decrypt(encodedData, "aA1234dd", salt);

Assert.AreEqual(seed, reprData);
}
}
}
6 changes: 6 additions & 0 deletions Substrate.NET.Wallet.Test/Substrate.NET.Wallet.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
<ProjectReference Include="..\Substrate.NET.Wallet\Substrate.NET.Wallet.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Update="EncodingTest.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
</ItemGroup>

<ItemGroup>
<None Update="dev_wallet1.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
Expand Down
29 changes: 1 addition & 28 deletions Substrate.NET.Wallet.Test/WalletTest.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using NUnit.Framework;
using Substrate.NET.Wallet;
using Substrate.NetApi;
using Substrate.NetApi.Model.Types;
using System;
using System.IO;
using System.Text;

namespace SubstrateNetWalletTest
namespace Substrate.NET.Wallet.Test
{
public class WalletTest
{
Expand Down Expand Up @@ -49,33 +48,15 @@ public void LoadWalletFromFileTest()
Wallet.Load(walletName1, out Wallet wallet1);
Assert.True(wallet1.IsStored);
Assert.False(wallet1.IsUnlocked);
Assert.AreEqual("Ed25519",
wallet1.FileStore.KeyType.ToString());
Assert.AreEqual("5FfzQe73TTQhmSQCgvYocrr6vh1jJXEKB8xUB6tExfpKVCEZ",
Utils.GetAddressFrom(wallet1.FileStore.PublicKey));
Assert.AreEqual("0x17E39AC65C894EC263396E9B8720D78A7A5FE0CB6C5C05DC32E756DF3D5D2D9622DBFDB41CE0C9067B810BB03E1DCE9C89CFC061FBB063B616FF91F3AA31498158632A35601C91DFEE5DA869D44FA8A4",
Utils.Bytes2HexString(wallet1.FileStore.EncryptedSeed));
Assert.AreEqual("0x34F0627DB7C9BF1B580A597122622E95",
Utils.Bytes2HexString(wallet1.FileStore.Salt));
wallet1.Unlock("aA1234dd");
Assert.True(wallet1.IsUnlocked);

var walletName2 = "dev_wallet2";
Wallet.Load(walletName2, out Wallet wallet2);
Assert.True(wallet2.IsStored);
Assert.False(wallet2.IsUnlocked);
Assert.AreEqual("Sr25519",
wallet2.FileStore.KeyType.ToString());
Assert.AreEqual("5Fe24e21Ff5vRtuWa4ZNPv1EGQz1zBq1VtT8ojqfmzo9k11P",
Utils.GetAddressFrom(wallet2.FileStore.PublicKey));
Assert.AreEqual("0xDA24A6B58BE083B58E3F011929B8A454B5FE9F1B91961DCC766D3E9F6AFE7AF96AAC1372DBA4537856F95C7E47A365C10590ACC092DB5AA95D6ECF5E06167B799AC6247178B7C51AC9B8F64C16602659",
Utils.Bytes2HexString(wallet2.FileStore.EncryptedSeed));
Assert.AreEqual("0xD048477FCAD42D83402CDE3B2AF369D4",
Utils.Bytes2HexString(wallet2.FileStore.Salt));
wallet2.Unlock("aA1234dd");
Assert.True(wallet2.IsUnlocked);
Assert.AreEqual("0x6BED04FEE1504A49825339A68F601F7739FA7CEBF3B5E6A4A2476979F53CF40A112F6ED717AE8E8F5134C784A07DE6F3B2F7DA51D8117C566547A5038D4B3C27",
Utils.Bytes2HexString(wallet2.Account.PrivateKey));
}

[Test]
Expand All @@ -89,14 +70,6 @@ public void LoadWalletFromFileStore()
var walletName2 = "dev_wallet3";
Wallet.Load(walletName2, wallet1.FileStore, out Wallet wallet2);

Assert.AreEqual("Ed25519",
wallet2.FileStore.KeyType.ToString());
Assert.AreEqual("5FfzQe73TTQhmSQCgvYocrr6vh1jJXEKB8xUB6tExfpKVCEZ",
Utils.GetAddressFrom(wallet2.FileStore.PublicKey));
Assert.AreEqual("0x17E39AC65C894EC263396E9B8720D78A7A5FE0CB6C5C05DC32E756DF3D5D2D9622DBFDB41CE0C9067B810BB03E1DCE9C89CFC061FBB063B616FF91F3AA31498158632A35601C91DFEE5DA869D44FA8A4",
Utils.Bytes2HexString(wallet2.FileStore.EncryptedSeed));
Assert.AreEqual("0x34F0627DB7C9BF1B580A597122622E95",
Utils.Bytes2HexString(wallet2.FileStore.Salt));
wallet2.Unlock("aA1234dd");
Assert.True(wallet2.IsUnlocked);
}
Expand Down
2 changes: 1 addition & 1 deletion Substrate.NET.Wallet.Test/dev_wallet1.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"KeyType":0,"PublicKey":"n54BhZZyCmeLir4glnLK+jIGHT9uBADZ1unbIo7hWk4=","EncryptedSeed":"F+OaxlyJTsJjOW6bhyDXinpf4MtsXAXcMudW3z1dLZYi2/20HODJBnuBC7A+Hc6cic/AYfuwY7YW/5HzqjFJgVhjKjVgHJHf7l2oadRPqKQ=","Salt":"NPBifbfJvxtYCllxImIulQ=="}
{"Encoded":"7Iy0sjfUMqXtZAXOIDJfkKxqZ3Pf0uo3frA6CgvrDVcvQ196eeFtOin2fknAwIzG/NdInwYLxGTGNTKN3kSsQhHexPWFcEaD","Encoding":{"Content":["pkcs8","ed25519"],"Type":["scrypt","xsalsa20-poly1305"],"Version":"3"},"Address":"5HUSXsYAg9h9KaWHmb6r5FXypyR8V7hsdRxsRAcy5M968PD4","Meta":{"GenesisHash":"0x3160D66BC1D86500DBAC8AA57FB845CE","IsHardware":false,"Name":"SUBSTRATE","Tags":[],"WhenCreated":1696592070618}}
2 changes: 1 addition & 1 deletion Substrate.NET.Wallet.Test/dev_wallet2.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"KeyType":1,"PublicKey":"nh0WPDIVJFrGLIO2ecRI2LYNKEyneZbpEvGsb3LQCF4=","EncryptedSeed":"2iSmtYvgg7WOPwEZKbikVLX+nxuRlh3Mdm0+n2r+evlqrBNy26RTeFb5XH5Ho2XBBZCswJLbWqldbs9eBhZ7eZrGJHF4t8Uaybj2TBZgJlk=","Salt":"0EhHf8rULYNALN47KvNp1A=="}
{"Encoded":"qVxFVCuaqvo/8ThK0uTpdxaqOaSuttmi333Q4xRMmEx6mdyo9WOmrrjf2/Tk+ZFB6vfD/ZK45BwdX2ALBHCHIP8y2p0ulP0Q","Encoding":{"Content":["pkcs8","sr25519"],"Type":["scrypt","xsalsa20-poly1305"],"Version":"3"},"Address":"5ELuscqHb44nDVyz5kHBLXQdBdp3hxJfGB8A6WQmpQtgf96z","Meta":{"GenesisHash":"0xEEF01B3AB74708E1F8ADC640B303C4FA","IsHardware":false,"Name":"SUBSTRATE","Tags":[],"WhenCreated":1696592071243}}
57 changes: 0 additions & 57 deletions Substrate.NET.Wallet/FileStore.cs

This file was deleted.

10 changes: 10 additions & 0 deletions Substrate.NET.Wallet/Model/EncodedData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Substrate.NET.Wallet.Model
{
public class EncodedData
{
public string Encoded { get; set; }
public EncodingInfo Encoding { get; set; }
public string Address { get; set; }
public Metadata Meta { get; set; }
}
}
11 changes: 11 additions & 0 deletions Substrate.NET.Wallet/Model/EncodingInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;

namespace Substrate.NET.Wallet.Model
{
public class EncodingInfo
{
public List<string> Content { get; set; } = new List<string>();
public List<string> Type { get; set; } = new List<string>();
public string Version { get; set; }
}
}
13 changes: 13 additions & 0 deletions Substrate.NET.Wallet/Model/Metadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;

namespace Substrate.NET.Wallet.Model
{
public class Metadata
{
public string GenesisHash { get; set; }
public bool IsHardware { get; set; }
public string Name { get; set; }
public List<string> Tags { get; set; } = new List<string>();
public long WhenCreated { get; set; } // This seems to be a Unix timestamp in milliseconds
}
}
3 changes: 2 additions & 1 deletion Substrate.NET.Wallet/Substrate.NET.Wallet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<PackageId>Substrate.NET.Wallet</PackageId>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
<Version>1.0.5</Version>
<Version>1.0.6</Version>
<Company>BloGa Tech AG</Company>
<Authors>Cedric Decoster</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand All @@ -22,6 +22,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Sodium.Core" Version="1.3.4" />
<PackageReference Include="Substrate.NET.API" Version="0.9.8" />
</ItemGroup>

Expand Down
Loading