Skip to content

Commit

Permalink
added wallet docs
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfriend77 committed Feb 4, 2024
1 parent 09e3158 commit 16c26cf
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 35 deletions.
12 changes: 6 additions & 6 deletions Substrate.NET.Wallet.Test/KeyringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ public void GenerateNewAccount_WithPassword_AndExportToJson()
var keyring = new Keyring.Keyring();
var kp = keyring.AddFromMnemonic(Mnemonic.GenerateMnemonic(Mnemonic.MnemonicSize.Words12), new Meta()
{
genesisHash = "0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3",
isHardware = false,
name = "SubstrateAccount",
tags = null
GenesisHash = "0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3",
IsHardware = false,
Name = "SubstrateAccount",
Tags = null
}, NetApi.Model.Types.KeyType.Sr25519);

var walletResult = kp.ToWalletFile("walletName", "testPassword1");
Assert.That(walletResult.meta.name, Is.EqualTo("walletName"));
Assert.That(walletResult.meta.Name, Is.EqualTo("walletName"));
var jsonResult = walletResult.ToJson();

Assert.That(jsonResult, Is.Not.Null);
Expand Down Expand Up @@ -159,7 +159,7 @@ public void WikiExample_Test()
var existingMnemonicAccount = "entire material egg meadow latin bargain dutch coral blood melt acoustic thought";

// Import an account from mnemonic automatically unlock all feature
var firstWallet = keyring.AddFromMnemonic(existingMnemonicAccount, new Meta() { name = "My account name" }, NetApi.Model.Types.KeyType.Ed25519);
var firstWallet = keyring.AddFromMnemonic(existingMnemonicAccount, new Meta() { Name = "My account name" }, NetApi.Model.Types.KeyType.Ed25519);
firstWallet.PasswordPolicy = passwordLightPolicy;

// firstPair.IsLocked => false
Expand Down
8 changes: 4 additions & 4 deletions Substrate.NET.Wallet.Test/MainTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ protected MainTests()

protected Meta defaultMeta = new Meta()
{
genesisHash = "0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3",
isHardware = false,
name = "SubstrateAccount2",
tags = null
GenesisHash = "0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3",
IsHardware = false,
Name = "SubstrateAccount2",
Tags = null
};

public class PairDefTest
Expand Down
135 changes: 119 additions & 16 deletions Substrate.NET.Wallet/Keyring/WalletFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,38 @@

namespace Substrate.NET.Wallet.Keyring
{
/// <summary>
/// Wallet JSON
/// </summary>
public static class WalletJson
{
/// <summary>
/// Encrypted JSON encoding
/// </summary>
public enum EncryptedJsonEncoding
{
/// <summary>
/// None
/// </summary>
None,

/// <summary>
/// Scrypt
/// </summary>
Scrypt,

/// <summary>
/// Xsalsa20Poly1305
/// </summary>
Xsalsa20Poly1305
}

/// <summary>
/// Encrypted to string
/// </summary>
/// <param name="encrypt"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public static string EncryptedToString(EncryptedJsonEncoding encrypt)
{
switch (encrypt)
Expand All @@ -32,6 +55,12 @@ public static string EncryptedToString(EncryptedJsonEncoding encrypt)
}
}

/// <summary>
/// Encrypted from string
/// </summary>
/// <param name="encrypt"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public static EncryptedJsonEncoding EncryptedFromString(string encrypt)
{
switch (encrypt)
Expand All @@ -51,48 +80,122 @@ public static EncryptedJsonEncoding EncryptedFromString(string encrypt)
}
}

/// <summary>
/// Wallet file
/// </summary>
public class WalletFile
{
public string encoded { get; set; }
public Encoding encoding { get; set; }
public string address { get; set; }
public Meta meta { get; set; }

/// <summary>
/// Encoded
/// </summary>
[JsonPropertyName("encoded")]
public string Encoded { get; set; }

/// <summary>
/// Encoding
/// </summary>
[JsonPropertyName("encoding")]
public Encoding Encoding { get; set; }

/// <summary>
/// Address
/// </summary>
[JsonPropertyName("address")]
public string Address { get; set; }

/// <summary>
/// Meta
/// </summary>
[JsonPropertyName("meta")]
public Meta Meta { get; set; }

/// <summary>
/// Convert to json
/// </summary>
/// <returns></returns>
public string ToJson()
{
return System.Text.Json.JsonSerializer.Serialize(this);
}

/// <summary>
/// Get the key type
/// </summary>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public KeyType GetKeyType()
{
switch (encoding.content[1].ToLowerInvariant())
switch (encoding.Content[1].ToLowerInvariant())

Check failure on line 128 in Substrate.NET.Wallet/Keyring/WalletFile.cs

View workflow job for this annotation

GitHub Actions / build & test

The name 'encoding' does not exist in the current context
{
case "ed25519":

Check failure on line 130 in Substrate.NET.Wallet/Keyring/WalletFile.cs

View workflow job for this annotation

GitHub Actions / build & test

Cannot implicitly convert type 'string' to 'int'
return KeyType.Ed25519;

case "sr25519":

Check failure on line 133 in Substrate.NET.Wallet/Keyring/WalletFile.cs

View workflow job for this annotation

GitHub Actions / build & test

Cannot implicitly convert type 'string' to 'int'
return KeyType.Sr25519;

default: throw new InvalidOperationException($"{encoding.content[1]} type is not supported");
default: throw new InvalidOperationException($"{encoding.Content[1]} type is not supported");

Check failure on line 136 in Substrate.NET.Wallet/Keyring/WalletFile.cs

View workflow job for this annotation

GitHub Actions / build & test

The name 'encoding' does not exist in the current context
}
}
}

/// <summary>
/// Encoding for the wallet file
/// </summary>
public class Encoding
{
public List<string> content { get; set; }
public List<string> type { get; set; }

/// <summary>
/// Content
/// </summary>
[JsonPropertyName("content")]
public List<string> Content { get; set; }

/// <summary>
/// Type
/// </summary>
[JsonPropertyName("type")]
public List<string> Type { get; set; }

/// <summary>
/// Version
/// </summary>
[JsonPropertyName("version")]
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
public int version { get; set; }
public int Version { get; set; }
}

/// <summary>
/// Metadata for the wallet file
/// </summary>
public class Meta
{
public string genesisHash { get; set; }
public bool isHardware { get; set; }
public string name { get; set; }
public List<object> tags { get; set; }
public long whenCreated { get; set; }
/// <summary>
/// Genesis hash
/// </summary>
[JsonPropertyName("genesisHash")]
public string GenesisHash { get; set; }

/// <summary>
/// Is hardware wallet
/// </summary>
[JsonPropertyName("isHardware")]
public bool IsHardware { get; set; }

/// <summary>
/// Name of the wallet
/// </summary>
[JsonPropertyName("name")]
public string Name { get; set; }

/// <summary>
/// Tags
/// </summary>
[JsonPropertyName("tags")]
public List<object> Tags { get; set; }

/// <summary>
/// When created
/// </summary>
[JsonPropertyName("whenCreated")]
public long WhenCreated { get; set; }
}
}
58 changes: 49 additions & 9 deletions Substrate.NET.Wallet/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Wallet(string address, byte[] encoded, Meta meta, byte[] publicKey, byte[
Address = address;
Encoded = encoded;
Meta = meta;
FileName = meta?.name;
FileName = meta?.Name;
Account = new Account();
Account.Create(keyType, privateKey, publicKey);
EncryptedEncoding = encryptedEncoding;
Expand Down Expand Up @@ -139,6 +139,13 @@ public bool Lock()
return true;
}

/// <summary>
/// Transform to wallet file.
/// </summary>
/// <param name="walletName"></param>
/// <param name="password"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public WalletFile ToWalletFile(string walletName, string password)
{
if (!IsUnlocked)
Expand All @@ -158,11 +165,11 @@ public WalletFile ToWalletFile(string walletName, string password)

var generatedMeta = new Meta()
{
isHardware = false,
tags = new List<object>(),
whenCreated = DateTime.Now.Ticks,
name = walletName,
genesisHash = string.Empty
IsHardware = false,
Tags = new List<object>(),
WhenCreated = DateTime.Now.Ticks,
Name = walletName,
GenesisHash = string.Empty
};

return Pair.ToJsonPair(KeyType, Address, generatedMeta, Encoded, !string.IsNullOrEmpty(password));
Expand All @@ -179,6 +186,11 @@ public string ToJson(string walletName, string password)
return JsonSerializer.Serialize(ToWalletFile(walletName, password));
}

/// <summary>
/// Recode the account
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
public byte[] Recode(string password)
{
return Pair.EncodePair(password, Account.ToPair());
Expand Down Expand Up @@ -222,10 +234,10 @@ public Wallet Derive(string sUri, Meta meta)
/// <returns></returns>
public void Save(string walletName, string password)
{
if (string.IsNullOrEmpty(Meta.name))
throw new InvalidOperationException($"No wallet name has been specified. Please update {nameof(Meta)}.{nameof(Meta.name)} propery with account name");
if (string.IsNullOrEmpty(Meta.Name))
throw new InvalidOperationException($"No wallet name has been specified. Please update {nameof(Meta)}.{nameof(Meta.Name)} propery with account name");

Caching.Persist(Wallet.ConcatWalletFileType(Meta.name), ToJson(walletName, password));
Caching.Persist(Wallet.ConcatWalletFileType(Meta.Name), ToJson(walletName, password));
_isStored = true;
}

Expand Down Expand Up @@ -276,9 +288,22 @@ public static bool TryLoad(string walletName, WalletFile fileStore, out Wallet w

#region Sign

/// <summary>
/// Sign the message
/// </summary>
/// <param name="message"></param>
/// <param name="wrap"></param>
/// <returns></returns>
public byte[] Sign(string message, bool wrap = true)
=> Sign(message.ToBytes(), wrap);

/// <summary>
/// Sign the message
/// </summary>
/// <param name="message"></param>
/// <param name="wrap"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public byte[] Sign(byte[] message, bool wrap = true)
{
if (!IsUnlocked)
Expand Down Expand Up @@ -323,9 +348,24 @@ public static bool TrySignMessage(Account signer, byte[] message, out byte[] sig

#region Verify

/// <summary>
/// Verify the message
/// </summary>
/// <param name="signature"></param>
/// <param name="message"></param>
/// <param name="wrap"></param>
/// <returns></returns>
public bool Verify(byte[] signature, string message, bool wrap = true)
=> Verify(signature, message.ToBytes(), wrap);

/// <summary>
/// Verify the message
/// </summary>
/// <param name="signature"></param>
/// <param name="message"></param>
/// <param name="wrap"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public bool Verify(byte[] signature, byte[] message, bool wrap = true)
{
if (!IsUnlocked)
Expand Down

0 comments on commit 16c26cf

Please sign in to comment.