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
29 changes: 0 additions & 29 deletions Substrate.NET.Wallet.Test/ArrayExtensionTest.cs

This file was deleted.

33 changes: 3 additions & 30 deletions Substrate.NET.Wallet.Test/KeyringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,33 +116,6 @@ public void GenerateNewAccount_Ed25519AndSr25519_ShouldHaveDifferentPublicAndSec
Assert.That(kp_Ed25519.Account.PrivateKey, Is.Not.EquivalentTo(kp_Sr25519.Account.PrivateKey));
}

[Test]
public void DecodeAddress()
{
var keyring = new Substrate.NET.Wallet.Keyring.Keyring();
var publicKey = new byte[] { 16, 178, 46, 190, 137, 179, 33, 55, 11, 238, 141, 57, 213, 197, 212, 17, 218, 241, 232, 252, 145, 201, 209, 83, 64, 68, 89, 15, 31, 150, 110, 188 };

Assert.That(keyring.DecodeAddress("5CSbZ7wG456oty4WoiX6a1J88VUbrCXLhrKVJ9q95BsYH4TZ"), Is.EqualTo(publicKey));
Assert.That(keyring.DecodeAddress("CxDDSH8gS7jecsxaRL9Txf8H5kqesLXAEAEgp76Yz632J9M"), Is.EqualTo(publicKey));
Assert.That(keyring.DecodeAddress("1NthTCKurNHLW52mMa6iA8Gz7UFYW5UnM3yTSpVdGu4Th7h"), Is.EqualTo(publicKey));
}

[Test]
public void EncodeAddress()
{
var keyring = new Substrate.NET.Wallet.Keyring.Keyring();
var publicKey = new byte[] { 16, 178, 46, 190, 137, 179, 33, 55, 11, 238, 141, 57, 213, 197, 212, 17, 218, 241, 232, 252, 145, 201, 209, 83, 64, 68, 89, 15, 31, 150, 110, 188 };

keyring.Ss58Format = 42;
Assert.That(keyring.EncodeAddress(publicKey), Is.EqualTo("5CSbZ7wG456oty4WoiX6a1J88VUbrCXLhrKVJ9q95BsYH4TZ"));

keyring.Ss58Format = 2;
Assert.That(keyring.EncodeAddress(publicKey), Is.EqualTo("CxDDSH8gS7jecsxaRL9Txf8H5kqesLXAEAEgp76Yz632J9M"));

keyring.Ss58Format = 0;
Assert.That(keyring.EncodeAddress(publicKey), Is.EqualTo("1NthTCKurNHLW52mMa6iA8Gz7UFYW5UnM3yTSpVdGu4Th7h"));
}

[Test]
[TestCase(NetApi.Model.Types.KeyType.Ed25519)]
[TestCase(NetApi.Model.Types.KeyType.Sr25519)]
Expand Down Expand Up @@ -208,10 +181,10 @@ public void KeyPairFromSeed()
var expected = Utils.HexToByteArray("46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a");

var keyPair = Keyring.Keyring.KeyPairFromSeed(NetApi.Model.Types.KeyType.Sr25519, seed);
Assert.That(keyPair.PublicKey.Length, Is.EqualTo(Keys.PUBLIC_KEY_LENGTH));
Assert.That(keyPair.SecretKey.Length, Is.EqualTo(Keys.SECRET_KEY_LENGTH));
Assert.That(keyPair.Bytes.Length, Is.EqualTo(Keys.PUBLIC_KEY_LENGTH));
Assert.That(keyPair.PrivateKey.Length, Is.EqualTo(Keys.SECRET_KEY_LENGTH));

Assert.That(keyPair.PublicKey, Is.EqualTo(expected));
Assert.That(keyPair.Bytes, Is.EqualTo(expected));
}
}
}
8 changes: 5 additions & 3 deletions Substrate.NET.Wallet.Test/Keyrings/KeypairSr25519Tests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using NUnit.Framework;
using Substrate.NET.Schnorrkel;
using Substrate.NET.Wallet.Derivation;
using Substrate.NET.Wallet.Extensions;
using Substrate.NET.Wallet.Keyring;
using Substrate.NetApi;
using Substrate.NetApi.Extensions;
using Substrate.NetApi.Model.Types;
using Substrate.NetApi.Sign;
using System;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -88,8 +88,10 @@ public void Sr25519_SignsAndVerifiesBothMethods()
// Just to check
// --
var concatenated_2 = miniSecret_Ed25519Bytes.GetPair().ToHalfEd25519Bytes();
var publicKey_2 = concatenated_2.SubArray(Keys.SECRET_KEY_LENGTH, Keys.SECRET_KEY_LENGTH + Keys.PUBLIC_KEY_LENGTH);
var secretKey_2 = concatenated_2.SubArray(0, Keys.SECRET_KEY_LENGTH);
Span<byte> concatenatedSpan = concatenated_2.AsSpan();
var publicKey_2 = concatenatedSpan.Slice(Keys.SECRET_KEY_LENGTH, Keys.PUBLIC_KEY_LENGTH).ToArray();
var secretKey_2 = concatenatedSpan.Slice(0, Keys.SECRET_KEY_LENGTH).ToArray();

var edBytes = miniSecret_Ed25519Bytes.ExpandToSecret().ToEd25519Bytes();
Assert.That(edBytes, Is.EquivalentTo(secretKey_2));
Assert.That(edBytes, Is.EquivalentTo(account_Ed25519Bytes.PrivateKey));
Expand Down
1 change: 0 additions & 1 deletion Substrate.NET.Wallet.Test/MainTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public Wallet GetWallet()
{
var keyType = KeyType.Sr25519;
return Pair.CreatePair(
new KeyringAddress(keyType),
Account.Build(keyType,
Utils.HexToByteArray(SecretKey),
Utils.HexToByteArray(PublickKey)));
Expand Down
24 changes: 0 additions & 24 deletions Substrate.NET.Wallet/Extensions/AccountExtension.cs

This file was deleted.

36 changes: 0 additions & 36 deletions Substrate.NET.Wallet/Extensions/ArrayExtension.cs

This file was deleted.

49 changes: 10 additions & 39 deletions Substrate.NET.Wallet/Keyring/Keyring.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Substrate.NET.Schnorrkel.Keys;
using Substrate.NET.Wallet.Derivation;
using Substrate.NET.Wallet.Extensions;
using Substrate.NetApi;
using Substrate.NetApi.Extensions;
using Substrate.NetApi.Model.Types;
Expand All @@ -13,46 +12,20 @@

namespace Substrate.NET.Wallet.Keyring
{
public class KeyringAddress
{
public KeyType KeyType { get; set; }
public Func<byte[], short, string> ToSS58 { get; set; }

public KeyringAddress(KeyType keyType)
{
KeyType = keyType;
ToSS58 = Utils.GetAddressFrom;
}

public KeyringAddress(KeyType keyType, Func<byte[], short, string> toSS58)
{
KeyType = keyType;
ToSS58 = toSS58;
}
}

/// <summary>
/// Keyring is a cryptographic key management tool or library used to manage cryptographic keys and perform key-related operations, such as key generation, storage, and signing.
/// </summary>
public class Keyring
{
public const int NONCE_LENGTH = 24;

public const int SCRYPT_LENGTH = 32 + 3 * 4;

public const short DEFAULT_SS58 = 42;

public IList<Wallet> Wallets { get; private set; } = new List<Wallet>();
public short Ss58Format { get; set; } = DEFAULT_SS58;

public byte[] DecodeAddress(string address)
{
short network;
return Utils.GetPublicKeyFrom(address, out network);
}

public string EncodeAddress(byte[] publicKey)
{
return Utils.GetAddressFrom(publicKey, Ss58Format);
}
public short Ss58Format { get; set; } = DEFAULT_SS58;

#region Get methods

Expand Down Expand Up @@ -80,8 +53,7 @@ public Wallet AddFromAddress(string address, Meta meta, byte[] encoded, KeyType
var publicKey = Utils.GetPublicKeyFrom(address);

var keyringPair = Pair.CreatePair(
new KeyringAddress(keyType),
Account.Build(keyType,null, publicKey),
Account.Build(keyType, null, publicKey),
meta, encoded, encryptedEncoding, Ss58Format);

AddWallet(keyringPair);
Expand Down Expand Up @@ -118,7 +90,7 @@ public Wallet AddFromUri(string uri, Meta meta, KeyType keyType)

public Wallet AddFromSeed(byte[] seed, Meta meta, KeyType keyType)
{
var pair = Pair.CreatePair(new KeyringAddress(keyType), Account.FromSeed(keyType, seed), meta, null, null, Ss58Format);
var pair = Pair.CreatePair(Account.FromSeed(keyType, seed), meta, null, null, Ss58Format);
AddWallet(pair);

return pair;
Expand All @@ -145,7 +117,6 @@ internal static Wallet CreateFromJson(WalletFile walletEncryption, short Ss58For
Convert.FromBase64String(walletEncryption.Encoded);

return Pair.CreatePair(
new KeyringAddress(keyType),
Account.Build(keyType, null, publicKey),
walletEncryption.Meta, encoded, encryptedEncoding, Ss58Format);
}
Expand All @@ -156,7 +127,7 @@ internal static Wallet CreateFromUri(string uri, Meta meta, KeyType keyType, sho

var derivedPair = Uri.KeyFromPath(Account.FromSeed(keyType, seed), extract.Path, keyType);

return Pair.CreatePair(new KeyringAddress(keyType), derivedPair, meta, null, null, Ss58Format);
return Pair.CreatePair(derivedPair, meta, null, null, Ss58Format);
}

internal static (KeyExtractResult, byte[]) CreateSeedFromUri(string uri)
Expand Down Expand Up @@ -222,7 +193,7 @@ public static byte[] JsonDecryptData(string password, byte[] encrypted, List<Wal
{
var scryptRes = Scrypt.FromBytes(encoded);
passwordBytes = Scrypt.ScryptEncode(password, scryptRes.Salt, scryptRes.Param).Password;
encrypted = encrypted.SubArray(SCRYPT_LENGTH);
encrypted = encrypted.AsSpan().Slice(SCRYPT_LENGTH).ToArray();
}

encoded = Chaos.NaCl.XSalsa20Poly1305.TryDecrypt(
Expand All @@ -237,7 +208,7 @@ public static byte[] JsonDecryptData(string password, byte[] encrypted, List<Wal
return encoded;
}

public static PairInfo KeyPairFromSeed(KeyType keyType, byte[] seed)
public static Account KeyPairFromSeed(KeyType keyType, byte[] seed)
{
if (seed.Length != 32)
throw new InvalidOperationException($"Seed is not 32 bytes (currently {seed.Length})");
Expand All @@ -246,11 +217,11 @@ public static PairInfo KeyPairFromSeed(KeyType keyType, byte[] seed)
{
case KeyType.Ed25519:
Chaos.NaCl.Ed25519.KeyPairFromSeed(out byte[] pubKey, out byte[] priKey, seed);
return new PairInfo(pubKey, priKey);
return Account.Build(keyType, priKey, pubKey);

case KeyType.Sr25519:
var miniSecret = new MiniSecret(seed, ExpandMode.Ed25519);
return new PairInfo(miniSecret.ExpandToPublic().Key, miniSecret.ExpandToSecret().ToEd25519Bytes());
return Account.Build(keyType, miniSecret.ExpandToSecret().ToEd25519Bytes(), miniSecret.ExpandToPublic().Key);

default:
throw new NotImplementedException($"KeyType {keyType} isn't implemented!");
Expand Down
Loading