Skip to content

Commit

Permalink
added propeorties to the client on connect.
Browse files Browse the repository at this point in the history
updated some checks
  • Loading branch information
darkfriend77 committed Jan 29, 2024
1 parent 1f3b8c8 commit 8a99996
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 39 deletions.
10 changes: 6 additions & 4 deletions Substrate.NetApi.Test/Keys/Ed25519Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
14 changes: 8 additions & 6 deletions Substrate.NetApi.Test/Keys/Sr25519Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Schnorrkel.Keys;
using Substrate.NetApi.Model.Types;
using System.Threading.Tasks;
using System.Linq;

namespace Substrate.NetApi.Test.Keys
{
Expand Down Expand Up @@ -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));
}
}
}
5 changes: 3 additions & 2 deletions Substrate.NetApi/Model/Extrinsics/Extrinsic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public class Extrinsic
/// </summary>
/// <param name="str">The string.</param>
/// <param name="chargeType"></param>
public Extrinsic(string str, ChargeType chargeType) : this(Utils.HexToByteArray(str).AsMemory(), chargeType)
public Extrinsic(string str, ChargeType chargeType)
: this(Utils.HexToByteArray(str).AsMemory(), chargeType)
{
}

Expand Down Expand Up @@ -84,7 +85,7 @@ internal Extrinsic(Memory<byte> 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
Expand Down
12 changes: 9 additions & 3 deletions Substrate.NetApi/Model/Extrinsics/Payload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ namespace Substrate.NetApi.Model.Extrinsics
/// </summary>
public class Payload : IEncodable
{
public readonly Method Call;

public readonly SignedExtensions SignedExtension;
/// <summary>
/// The call
/// </summary>
public Method Call { get; }

/// <summary>
/// Signed extension
/// </summary>
public SignedExtensions SignedExtension { get; }

/// <summary>
/// Initializes a new instance of the <see cref="Payload"/> class.
Expand Down
37 changes: 29 additions & 8 deletions Substrate.NetApi/Model/Extrinsics/SignedExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,40 @@ namespace Substrate.NetApi.Model.Extrinsics
/// </summary>
public class SignedExtensions
{
public readonly uint SpecVersion;
/// <summary>
/// Specification Version
/// </summary>
public uint SpecVersion { get; }

public readonly uint TxVersion;
/// <summary>
/// Transaction Version
/// </summary>
public uint TxVersion { get; }

public readonly Hash Genesis;
/// <summary>
/// Genesis Hash
/// </summary>
public Hash Genesis { get; }

public readonly Hash StartEra;
/// <summary>
/// Start Era
/// </summary>
public Hash StartEra { get; }

public readonly Era Mortality;
/// <summary>
/// Mortality
/// </summary>
public Era Mortality { get; }

public readonly CompactInteger Nonce;
/// <summary>
/// Nonce
/// </summary>
public CompactInteger Nonce { get; }

public readonly ChargeType Charge;
/// <summary>
/// Charge
/// </summary>
public ChargeType Charge { get; }

/// <summary>
/// Initializes a new instance of the <see cref="SignedExtensions"/> class.
Expand All @@ -31,7 +52,7 @@ public class SignedExtensions
/// <param name="startEra">The start era.</param>
/// <param name="mortality">The mortality.</param>
/// <param name="nonce">The nonce.</param>
/// <param name="chargeTransactionPayment">The charge transaction payment.</param>
/// <param name="charge">The charge transaction payment.</param>
public SignedExtensions(uint specVersion, uint txVersion, Hash genesis, Hash startEra, Era mortality, CompactInteger nonce, ChargeType charge)
{
SpecVersion = specVersion;
Expand Down
25 changes: 17 additions & 8 deletions Substrate.NetApi/Model/Extrinsics/UnCheckedExtrinsic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@

namespace Substrate.NetApi.Model.Extrinsics
{
/// <summary>
/// Unchecked Extrinsic
/// </summary>
public class UnCheckedExtrinsic : Extrinsic
{
private readonly Hash _genesis;
/// <summary>
/// Genesis Hash
/// </summary>
private Hash Genesis { get; }

private readonly Hash _startEra;
/// <summary>
/// Start Era
/// </summary>
private Hash StartEra { get; }

/// <summary>
/// Initializes a new instance of the <see cref="UnCheckedExtrinsic"/> class.
Expand All @@ -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;
}

/// <summary>
Expand All @@ -37,7 +46,7 @@ public UnCheckedExtrinsic(bool signed, Account account, Method method, Era era,
/// <returns></returns>
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));
}

/// <summary>
Expand All @@ -50,11 +59,11 @@ public void AddPayloadSignature(byte[] signature)
}

/// <summary>
/// Encodes this instance.
/// Encode this instance, returns the encoded bytes.
/// </summary>
/// <returns></returns>
/// <exception cref="Exception">Missing payload signature for signed transaction.</exception>
public byte[] Encode()
/// <exception cref="NotSupportedException"></exception>
public new byte[] Encode()
{
if (Signed && Signature == null)
{
Expand Down
30 changes: 24 additions & 6 deletions Substrate.NetApi/Model/Types/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,19 @@ public interface IAccount
/// <summary>
/// Sign the specified message.
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
byte[] Sign(byte[] message);

/// <summary>
/// Asynchronouslys sign the specified message.
/// </summary>
/// <param name="message">The message bytes.</param>
/// <returns></returns>
Task<byte[]> SignRawAsync(byte[] message);
Task<byte[]> SignAsync(byte[] message);

/// <summary>
/// Sign the specified payload.
/// Asynchronouslys sign the specified payload.
/// </summary>
/// <param name="payload">The payload.</param>
/// <returns></returns>
Expand Down Expand Up @@ -146,15 +153,26 @@ public static Account Build(KeyType keyType, byte[] privateKey, byte[] publicKey
/// <param name="message"></param>
/// <returns></returns>
/// <exception cref="NotSupportedException"></exception>
public virtual async Task<byte[]> SignRawAsync(byte[] message)
public virtual async Task<byte[]> SignAsync(byte[] message)
{
return await Task.Run(() => Sign(message));
}

/// <summary>
/// Signs the specified message.
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
/// <exception cref="NotSupportedException"></exception>
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}'.");
Expand All @@ -169,7 +187,7 @@ public virtual async Task<byte[]> SignRawAsync(byte[] message)
/// <exception cref="NotImplementedException"></exception>
public virtual async Task<byte[]> SignPayloadAsync(Payload payload)
{
return await SignRawAsync(payload.Encode());
return await SignAsync(payload.Encode());
}

/// <summary>
Expand Down
13 changes: 11 additions & 2 deletions Substrate.NetApi/SubstrateClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,16 @@ public SubstrateClient(Uri uri, ChargeType chargeType, bool bypassRemoteCertific
/// <value> Information describing the meta. </value>
public MetaData MetaData { get; private set; }

/// <summary> Gets or sets information describing the runtime version. </summary>
/// <value> Information describing the runtime version. </value>
/// <summary>
/// Network runtime version
/// </summary>
public RuntimeVersion RuntimeVersion { get; private set; }

/// <summary>
/// Network propoerties
/// </summary>
public Properties Properties { get; private set; }

/// <summary> Gets or sets the genesis hash. </summary>
/// <value> The genesis hash. </value>
public Hash GenesisHash { get; private set; }
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 8a99996

Please sign in to comment.