From 8a9999692a650172e612f270fa775862cf24ca5a Mon Sep 17 00:00:00 2001 From: "cedric.decoster" Date: Mon, 29 Jan 2024 12:57:46 +0100 Subject: [PATCH] added propeorties to the client on connect. updated some checks --- Substrate.NetApi.Test/Keys/Ed25519Tests.cs | 10 +++-- Substrate.NetApi.Test/Keys/Sr25519Tests.cs | 14 ++++--- .../Model/Extrinsics/Extrinsic.cs | 5 ++- Substrate.NetApi/Model/Extrinsics/Payload.cs | 12 ++++-- .../Model/Extrinsics/SignedExtensions.cs | 37 +++++++++++++++---- .../Model/Extrinsics/UnCheckedExtrinsic.cs | 25 +++++++++---- Substrate.NetApi/Model/Types/Account.cs | 30 ++++++++++++--- Substrate.NetApi/SubstrateClient.cs | 13 ++++++- 8 files changed, 107 insertions(+), 39 deletions(-) diff --git a/Substrate.NetApi.Test/Keys/Ed25519Tests.cs b/Substrate.NetApi.Test/Keys/Ed25519Tests.cs index e7c4304..a9dc9ef 100644 --- a/Substrate.NetApi.Test/Keys/Ed25519Tests.cs +++ b/Substrate.NetApi.Test/Keys/Ed25519Tests.cs @@ -147,12 +147,14 @@ public async Task AccountEd25519SignatureTestComparePolkadotJsAsync(string polka // According to https://github.com/polkadot-js/apps/blob/master/packages/page-signing/src/Sign.tsx#L93 var messageBytes = WrapMessage.Wrap(message); - var signature = await account.SignRawAsync(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)); } } diff --git a/Substrate.NetApi.Test/Keys/Sr25519Tests.cs b/Substrate.NetApi.Test/Keys/Sr25519Tests.cs index 9bd7f22..90b8a3a 100644 --- a/Substrate.NetApi.Test/Keys/Sr25519Tests.cs +++ b/Substrate.NetApi.Test/Keys/Sr25519Tests.cs @@ -5,6 +5,7 @@ using Schnorrkel.Keys; using Substrate.NetApi.Model.Types; using System.Threading.Tasks; +using System.Linq; namespace Substrate.NetApi.Test.Keys { @@ -98,13 +99,14 @@ public async Task AccountSr25519SignatureTestComparePolkadotJsAsync(string polka var message = "I test this signature!"; var messageBytes = WrapMessage.Wrap(message); - var simpleSign = await account.SignRawAsync(messageBytes); - var singatureHexString = Utils.Bytes2HexString(simpleSign); - // SIGn C#: 0x2A6346A8707A9929B65167C448F719FE977F2EE04D2CB250685C98C79CCBF2458901F9B386D08422D9102FBD8BF7CFECDF7605F4CDC5FA8D121E2E9730F9098C + var signature1 = await Task.Run(() => account.Sign(messageBytes)); + var signature2 = await account.SignAsync(messageBytes); - // SIGn PolkaJS:0x5c42ac4e2d55b8e59d9b255af370de03fe177f5545eecbbd784531cb2eb1f2553e0e2b91656f99fae930eb6ff8ac1a3eca4e19d307ecb39832a479a478a8608a - var simpleSign2 = Utils.HexToByteArray(polkadotJsSignature); - Assert.True(account.Verify(simpleSign2, account.Bytes, messageBytes)); + Assert.True(account.Verify(signature1, account.Bytes, messageBytes)); + Assert.True(account.Verify(signature2, account.Bytes, messageBytes)); + + var signature3 = Utils.HexToByteArray(polkadotJsSignature); + Assert.True(account.Verify(signature3, account.Bytes, messageBytes)); } } } \ No newline at end of file diff --git a/Substrate.NetApi/Model/Extrinsics/Extrinsic.cs b/Substrate.NetApi/Model/Extrinsics/Extrinsic.cs index 315fed2..fd9d3cc 100644 --- a/Substrate.NetApi/Model/Extrinsics/Extrinsic.cs +++ b/Substrate.NetApi/Model/Extrinsics/Extrinsic.cs @@ -55,7 +55,8 @@ public class Extrinsic /// /// The string. /// - public Extrinsic(string str, ChargeType chargeType) : this(Utils.HexToByteArray(str).AsMemory(), chargeType) + public Extrinsic(string str, ChargeType chargeType) + : this(Utils.HexToByteArray(str).AsMemory(), chargeType) { } @@ -84,7 +85,7 @@ internal Extrinsic(Memory memory, ChargeType chargeType) { // start bytes m = 1; - var _startBytes = memory.Slice(p, m).ToArray()[0]; + _ = memory.Slice(p, m).ToArray()[0]; p += m; // sender public key diff --git a/Substrate.NetApi/Model/Extrinsics/Payload.cs b/Substrate.NetApi/Model/Extrinsics/Payload.cs index 5a9ed89..6a1b84c 100644 --- a/Substrate.NetApi/Model/Extrinsics/Payload.cs +++ b/Substrate.NetApi/Model/Extrinsics/Payload.cs @@ -8,9 +8,15 @@ namespace Substrate.NetApi.Model.Extrinsics /// public class Payload : IEncodable { - public readonly Method Call; - - public readonly SignedExtensions SignedExtension; + /// + /// The call + /// + public Method Call { get; } + + /// + /// Signed extension + /// + public SignedExtensions SignedExtension { get; } /// /// Initializes a new instance of the class. diff --git a/Substrate.NetApi/Model/Extrinsics/SignedExtensions.cs b/Substrate.NetApi/Model/Extrinsics/SignedExtensions.cs index 6a92de9..bc6bc5c 100644 --- a/Substrate.NetApi/Model/Extrinsics/SignedExtensions.cs +++ b/Substrate.NetApi/Model/Extrinsics/SignedExtensions.cs @@ -8,19 +8,40 @@ namespace Substrate.NetApi.Model.Extrinsics /// public class SignedExtensions { - public readonly uint SpecVersion; + /// + /// Specification Version + /// + public uint SpecVersion { get; } - public readonly uint TxVersion; + /// + /// Transaction Version + /// + public uint TxVersion { get; } - public readonly Hash Genesis; + /// + /// Genesis Hash + /// + public Hash Genesis { get; } - public readonly Hash StartEra; + /// + /// Start Era + /// + public Hash StartEra { get; } - public readonly Era Mortality; + /// + /// Mortality + /// + public Era Mortality { get; } - public readonly CompactInteger Nonce; + /// + /// Nonce + /// + public CompactInteger Nonce { get; } - public readonly ChargeType Charge; + /// + /// Charge + /// + public ChargeType Charge { get; } /// /// Initializes a new instance of the class. @@ -31,7 +52,7 @@ public class SignedExtensions /// The start era. /// The mortality. /// The nonce. - /// The charge transaction payment. + /// The charge transaction payment. public SignedExtensions(uint specVersion, uint txVersion, Hash genesis, Hash startEra, Era mortality, CompactInteger nonce, ChargeType charge) { SpecVersion = specVersion; diff --git a/Substrate.NetApi/Model/Extrinsics/UnCheckedExtrinsic.cs b/Substrate.NetApi/Model/Extrinsics/UnCheckedExtrinsic.cs index f2d77ef..2175740 100644 --- a/Substrate.NetApi/Model/Extrinsics/UnCheckedExtrinsic.cs +++ b/Substrate.NetApi/Model/Extrinsics/UnCheckedExtrinsic.cs @@ -6,11 +6,20 @@ namespace Substrate.NetApi.Model.Extrinsics { + /// + /// Unchecked Extrinsic + /// public class UnCheckedExtrinsic : Extrinsic { - private readonly Hash _genesis; + /// + /// Genesis Hash + /// + private Hash Genesis { get; } - private readonly Hash _startEra; + /// + /// Start Era + /// + private Hash StartEra { get; } /// /// Initializes a new instance of the class. @@ -26,8 +35,8 @@ public class UnCheckedExtrinsic : Extrinsic public UnCheckedExtrinsic(bool signed, Account account, Method method, Era era, CompactInteger nonce, ChargeType charge, Hash genesis, Hash startEra) : base(signed, account, nonce, method, era, charge) { - _genesis = genesis; - _startEra = startEra; + Genesis = genesis; + StartEra = startEra; } /// @@ -37,7 +46,7 @@ public UnCheckedExtrinsic(bool signed, Account account, Method method, Era era, /// public Payload GetPayload(RuntimeVersion runtime) { - return new Payload(Method, new SignedExtensions(runtime.SpecVersion, runtime.TransactionVersion, _genesis, _startEra, Era, Nonce, Charge)); + return new Payload(Method, new SignedExtensions(runtime.SpecVersion, runtime.TransactionVersion, Genesis, StartEra, Era, Nonce, Charge)); } /// @@ -50,11 +59,11 @@ public void AddPayloadSignature(byte[] signature) } /// - /// Encodes this instance. + /// Encode this instance, returns the encoded bytes. /// /// - /// Missing payload signature for signed transaction. - public byte[] Encode() + /// + public new byte[] Encode() { if (Signed && Signature == null) { diff --git a/Substrate.NetApi/Model/Types/Account.cs b/Substrate.NetApi/Model/Types/Account.cs index 53cb0d6..a347e14 100644 --- a/Substrate.NetApi/Model/Types/Account.cs +++ b/Substrate.NetApi/Model/Types/Account.cs @@ -38,12 +38,19 @@ public interface IAccount /// /// Sign the specified message. /// + /// + /// + byte[] Sign(byte[] message); + + /// + /// Asynchronouslys sign the specified message. + /// /// The message bytes. /// - Task SignRawAsync(byte[] message); + Task SignAsync(byte[] message); /// - /// Sign the specified payload. + /// Asynchronouslys sign the specified payload. /// /// The payload. /// @@ -146,15 +153,26 @@ public static Account Build(KeyType keyType, byte[] privateKey, byte[] publicKey /// /// /// - public virtual async Task SignRawAsync(byte[] message) + public virtual async Task SignAsync(byte[] message) + { + return await Task.Run(() => Sign(message)); + } + + /// + /// Signs the specified message. + /// + /// + /// + /// + public byte[] Sign(byte[] message) { switch (KeyType) { case KeyType.Ed25519: - return await Task.Run(() => Ed25519.Sign(message, PrivateKey)); + return Ed25519.Sign(message, PrivateKey); case KeyType.Sr25519: - return await Task.Run(() => Sr25519v091.SignSimple(Bytes, PrivateKey, message)); + return Sr25519v091.SignSimple(Bytes, PrivateKey, message); default: throw new NotSupportedException($"Unknown key type found '{KeyType}'."); @@ -169,7 +187,7 @@ public virtual async Task SignRawAsync(byte[] message) /// public virtual async Task SignPayloadAsync(Payload payload) { - return await SignRawAsync(payload.Encode()); + return await SignAsync(payload.Encode()); } /// diff --git a/Substrate.NetApi/SubstrateClient.cs b/Substrate.NetApi/SubstrateClient.cs index 3b0fd59..b55dacb 100644 --- a/Substrate.NetApi/SubstrateClient.cs +++ b/Substrate.NetApi/SubstrateClient.cs @@ -87,10 +87,16 @@ public SubstrateClient(Uri uri, ChargeType chargeType, bool bypassRemoteCertific /// Information describing the meta. public MetaData MetaData { get; private set; } - /// Gets or sets information describing the runtime version. - /// Information describing the runtime version. + /// + /// Network runtime version + /// public RuntimeVersion RuntimeVersion { get; private set; } + /// + /// Network propoerties + /// + public Properties Properties { get; private set; } + /// Gets or sets the genesis hash. /// The genesis hash. public Hash GenesisHash { get; private set; } @@ -246,6 +252,9 @@ public async Task ConnectAsync(bool useMetaData, bool standardSubstrate, Cancell RuntimeVersion = await State.GetRuntimeVersionAsync(token); Logger.Debug("Runtime version parsed."); + + Properties = await System.PropertiesAsync(token); + Logger.Debug("Properties parsed."); } //_jsonRpc.TraceSource.Switch.Level = SourceLevels.All;