Skip to content

Commit

Permalink
Merge branch 'master' into feature/privatekey-build-account
Browse files Browse the repository at this point in the history
# Conflicts:
#	Substrate.NetApi.Test/Keys/Sr25519Tests.cs
#	Substrate.NetApi.TestNode/ExtrinsicsTest.cs
#	Substrate.NetApi/Mnemonic.cs
#	Substrate.NetApi/Model/Types/Account.cs
#	Substrate.NetApi/RequestGenerator.cs
  • Loading branch information
Apolixit committed Jan 31, 2024
2 parents 0c32cb8 + b4951b4 commit 5cdc9f1
Show file tree
Hide file tree
Showing 106 changed files with 72,880 additions and 72,022 deletions.
13 changes: 8 additions & 5 deletions Substrate.NetApi.Test/Keys/Ed25519Tests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using Substrate.NetApi.Model.Types;
using Substrate.NetApi.Model.Types.Base;
Expand Down Expand Up @@ -133,7 +134,7 @@ public void Ed25519SignatureTestComparePolkadotJs(string polkadotJsSignature)

[Test]
[TestCase("0xd2baabb61bcd0026e797136cb0938d55e3c3ea8825c163eb3d1738b3c79af8e8f4953ba4767dc5477202756d3fba97bc50fc3ac8355ff5acfba88a36311f2f0f")]
public void AccountEd25519SignatureTestComparePolkadotJs(string polkadotJsSignature)
public async Task AccountEd25519SignatureTestComparePolkadotJsAsync(string polkadotJsSignature)
{
var rawSeed = "0x70f93a75dbc6ad5b0c051210704a00a9937732d0c360792b0fea24efb8ea8465";

Expand All @@ -146,12 +147,14 @@ public void AccountEd25519SignatureTestComparePolkadotJs(string polkadotJsSignat
// According to https://github.com/polkadot-js/apps/blob/master/packages/page-signing/src/Sign.tsx#L93
var messageBytes = WrapMessage.Wrap(message);

var signature = account.Sign(messageBytes);
var singatureHexString = Utils.Bytes2HexString(signature);
var signature1 = await Task.Run(() => account.Sign(messageBytes));
var signature2 = await account.SignAsync(messageBytes);

// SIGn C#: 0x679FA7BC8B2A7C40B5ECD50CA041E961DB8971D2B454DB7DE64E543B3C1892A6D3F223DDA01C66B9878C149CFCC8B86ECF2B20F11F7610596F51479405776907
Assert.IsTrue(signature1.SequenceEqual(signature2));

Assert.True(account.Verify(signature1, account.Bytes, messageBytes));
Assert.True(account.Verify(signature2, account.Bytes, messageBytes));

// SIGn PolkaJS:0xd2baabb61bcd0026e797136cb0938d55e3c3ea8825c163eb3d1738b3c79af8e8f4953ba4767dc5477202756d3fba97bc50fc3ac8355ff5acfba88a36311f2f0f
Assert.True(account.Verify(Utils.HexToByteArray(polkadotJsSignature), account.Bytes, messageBytes));
}
}
Expand Down
27 changes: 15 additions & 12 deletions Substrate.NetApi.Test/Keys/Sr25519Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using NUnit.Framework;
using Substrate.NET.Schnorrkel.Keys;
using Substrate.NetApi.Model.Types;
using System.Threading.Tasks;
using System.Linq;

namespace Substrate.NetApi.Test.Keys
{
Expand Down Expand Up @@ -86,24 +88,25 @@ public void Sr25519SignatureTestComparePolkadotJs(string polkadotJsSignature)
Assert.True(Substrate.NET.Schnorrkel.Sr25519v091.Verify(simpleSign2, keyPairAlice.Public.Key, messageBytes));
}

//[Test]
//[TestCase("0x5c42ac4e2d55b8e59d9b255af370de03fe177f5545eecbbd784531cb2eb1f2553e0e2b91656f99fae930eb6ff8ac1a3eca4e19d307ecb39832a479a478a8608a")]
//public void AccountSr25519SignatureTestComparePolkadotJs(string polkadotJsSignature)
//{
// var miniSecretAlice = new MiniSecret(Utils.HexToByteArray("0xe5be9a5092b81bca64be81d212e7f2f9eba183bb7a90954f7b76361f6edb5c0a"), ExpandMode.Ed25519);
[Test]
[TestCase("0x5c42ac4e2d55b8e59d9b255af370de03fe177f5545eecbbd784531cb2eb1f2553e0e2b91656f99fae930eb6ff8ac1a3eca4e19d307ecb39832a479a478a8608a")]
public async Task AccountSr25519SignatureTestComparePolkadotJsAsync(string polkadotJsSignature)
{
var miniSecretAlice = new MiniSecret(Utils.HexToByteArray("0xe5be9a5092b81bca64be81d212e7f2f9eba183bb7a90954f7b76361f6edb5c0a"), ExpandMode.Ed25519);

// var account = Account.Build(KeyType.Sr25519, miniSecretAlice.ExpandToSecret().ToEd25519Bytes(), miniSecretAlice.GetPair().Public.Key);

// var message = "I test this signature!";
// var messageBytes = WrapMessage.Wrap(message);

// var simpleSign = account.Sign(messageBytes);
// var singatureHexString = Utils.Bytes2HexString(simpleSign);
// // SIGn C#: 0x2A6346A8707A9929B65167C448F719FE977F2EE04D2CB250685C98C79CCBF2458901F9B386D08422D9102FBD8BF7CFECDF7605F4CDC5FA8D121E2E9730F9098C
var signature1 = await Task.Run(() => account.Sign(messageBytes));
var signature2 = await account.SignAsync(messageBytes);

Assert.True(account.Verify(signature1, account.Bytes, messageBytes));
Assert.True(account.Verify(signature2, account.Bytes, messageBytes));

// // SIGn PolkaJS:0x5c42ac4e2d55b8e59d9b255af370de03fe177f5545eecbbd784531cb2eb1f2553e0e2b91656f99fae930eb6ff8ac1a3eca4e19d307ecb39832a479a478a8608a
// var simpleSign2 = Utils.HexToByteArray(polkadotJsSignature);
// Assert.True(account.Verify(simpleSign2, account.Bytes, messageBytes));
//}
var signature3 = Utils.HexToByteArray(polkadotJsSignature);
Assert.True(account.Verify(signature3, account.Bytes, messageBytes));
}
}
}
106 changes: 99 additions & 7 deletions Substrate.NetApi.Test/TypeConverters/BaseTypesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Substrate.NetApi.Model.Types.Base;
using Substrate.NetApi.Model.Types.Primitive;
using NUnit.Framework;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

namespace Substrate.NetApi.Test
{
Expand Down Expand Up @@ -90,6 +92,20 @@ public void BaseVecTest()
var baseVecCtor_2 = new BaseVec<U16>(
new uint[] { 4, 8, 15, 16, 23, 42, 100 }.Select(x => new U16((ushort)x)).ToArray());
Assert.IsFalse(baseVecCtor.Equals(baseVecCtor_2));

U16[] arrayFromBaseVec = baseVec;
for (int i = 0; i < vecUInt16.Length; i++)
{
Assert.AreEqual(vecUInt16[i], arrayFromBaseVec[i].Value);
}

BaseVec<U16> baseVecFromU16Array = (BaseVec<U16>) arrayFromBaseVec;
for (int i = 0; i < vecUInt16.Length; i++)
{
Assert.AreEqual(vecUInt16[i], baseVecFromU16Array.Value[i].Value);
}
Assert.AreEqual(baseVec.Bytes, baseVecFromU16Array.Bytes);
Assert.AreEqual(baseVec.TypeSize, baseVecFromU16Array.TypeSize);
}

[Test]
Expand Down Expand Up @@ -130,6 +146,45 @@ public void BaseOptTest()
Assert.That(baseOptFilledError.OptionFlag, Is.EqualTo(false));
Assert.That(baseOptFilledError.Bytes, Is.Not.Null);
Assert.That(baseOptFilledError.Bytes.Length, Is.EqualTo(1));

ulong value = 100;

// Testing implicit operator
BaseOpt<U64> implicitOpt = (U64)value;
Assert.AreEqual(value, implicitOpt.Value.Value);

// Testing explicit operator with a filled BaseOpt
BaseOpt<U64> explicitOptFilled = new BaseOpt<U64>((U64)value);
ulong explicitValueFilled = (U64)explicitOptFilled;
Assert.AreEqual(value, explicitValueFilled);

// Testing explicit operator with an empty BaseOpt
BaseOpt<U64> explicitOptEmpty = new BaseOpt<U64>(null);
bool exceptionThrown = false;
try
{
_ = (U64)explicitOptEmpty;
}
catch (InvalidOperationException ex)
{
exceptionThrown = true;
Assert.AreEqual("Option is None", ex.Message);
}
Assert.IsTrue(exceptionThrown, "Exception not thrown for explicit cast of empty BaseOpt");

// Testing explicit operator with a filled BaseOpt but OptionFlag = false (this is an unusual case)
explicitOptFilled.OptionFlag = false;
exceptionThrown = false;
try
{
_ = (U64)explicitOptFilled;
}
catch (InvalidOperationException ex)
{
exceptionThrown = true;
Assert.AreEqual("Option is None", ex.Message);
}
Assert.IsTrue(exceptionThrown, "Exception not thrown for explicit cast with OptionFlag = false");
}

[Test]
Expand Down Expand Up @@ -206,6 +261,18 @@ public void BaseComTest()
Assert.AreEqual(baseComFromValue.Bytes, new BaseCom<U128>(new CompactInteger(new U128(10))).Bytes);
Assert.AreEqual(baseComFromValue.Value, new BaseCom<U128>(new CompactInteger(new U128(10))).Value);
Assert.AreEqual(baseComFromValue.TypeSize, new BaseCom<U128>(new CompactInteger(new U128(10))).TypeSize);

// Test explicit conversion from CompactInteger to BaseCom
var compactInt = new CompactInteger(new U64(10));
var baseComFromExplicitConversion = (BaseCom<U64>)compactInt;
Assert.AreEqual(baseComFromValue.Bytes, baseComFromExplicitConversion.Bytes);
Assert.AreEqual(baseComFromValue.Value, baseComFromExplicitConversion.Value);
Assert.AreEqual(baseComFromValue.TypeSize, baseComFromExplicitConversion.TypeSize);

// Test implicit conversion from BaseCom to CompactInteger
CompactInteger compactIntFromImplicitConversion = baseComFromValue;
Assert.AreEqual(compactInt.Value, compactIntFromImplicitConversion.Value);
Assert.AreEqual(compactInt.Encode(), compactIntFromImplicitConversion.Encode());
}

public enum PartialBalanceEvents
Expand Down Expand Up @@ -238,6 +305,16 @@ public void BaseEnumTest()

Assert.AreNotEqual(baseEnumFromValue.Bytes, new BaseEnum<PartialBalanceEvents>(PartialBalanceEvents.BalanceSet).Bytes);
Assert.AreNotEqual(baseEnumFromValue.Bytes, new BaseEnum<PartialBalanceEvents>(PartialBalanceEvents.BalanceSet).Value);

// Test explicit conversion from Enum to BaseEnum
BaseEnum<PartialBalanceEvents> baseEnumFromImplicitConversion = (BaseEnum<PartialBalanceEvents>)PartialBalanceEvents.Transfer;
Assert.AreEqual(baseEnumFromValue.Bytes, baseEnumFromImplicitConversion.Bytes);
Assert.AreEqual(baseEnumFromValue.Value, baseEnumFromImplicitConversion.Value);
Assert.AreEqual(baseEnumFromValue.TypeSize, baseEnumFromImplicitConversion.TypeSize);

// Test implicit conversion from BaseEnum to Enum
PartialBalanceEvents enumValueFromImplicitConversion = baseEnumFromValue;
Assert.AreEqual(PartialBalanceEvents.Transfer, enumValueFromImplicitConversion);
}

[Test]
Expand All @@ -254,14 +331,29 @@ public void AccountIdTest()
[Test]
public void HashTest()
{
var blockHash = new byte[]
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
var hash = new Hash();
hash.Create(blockHash);

var hashPrim = new Hash(blockHash);
// Use a more meaningful byte array as test data
var value = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 };
var hexValue = Utils.Bytes2HexString(value);

Assert.AreEqual(hash.Bytes, hashPrim.Bytes);
// Create Hash object using Create method
var hash = new Hash();
hash.Create(value);
Assert.AreEqual(value, hash.Bytes);
Assert.AreEqual(hexValue, hash.Value);

// Create Hash object using constructor
var hashPrim = new Hash(value);
Assert.AreEqual(value, hashPrim.Bytes);
Assert.AreEqual(hexValue, hashPrim.Value);

// Test explicit conversion from byte[] to Hash
Hash hashExplicit = (Hash)value;
Assert.AreEqual(value, hashExplicit.Bytes);
Assert.AreEqual(hexValue, hashExplicit.Value);

// Test implicit conversion from Hash to byte[]
byte[] bytesImplicit = hash;
Assert.AreEqual(value, bytesImplicit);
}

[Test]
Expand Down
Loading

0 comments on commit 5cdc9f1

Please sign in to comment.