Skip to content

Commit

Permalink
Merge pull request #73 from SubstrateGaming/feat_next_release_0916
Browse files Browse the repository at this point in the history
Feat next release 0916
  • Loading branch information
RostislavLitovkin authored Jan 30, 2024
2 parents 27aa6a7 + cada944 commit b4951b4
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 99 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
17 changes: 10 additions & 7 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 Schnorrkel.Keys;
using Substrate.NetApi.Model.Types;
using System.Threading.Tasks;
using System.Linq;

namespace Substrate.NetApi.Test.Keys
{
Expand Down Expand Up @@ -88,7 +90,7 @@ public void Sr25519SignatureTestComparePolkadotJs(string polkadotJsSignature)

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

Expand All @@ -97,13 +99,14 @@ public void AccountSr25519SignatureTestComparePolkadotJs(string polkadotJsSignat
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);

// 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));
}
}
}
10 changes: 6 additions & 4 deletions Substrate.NetApi/Model/Extrinsics/Extrinsic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,23 @@ 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)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Extrinsic"/> class.
/// </summary>
/// <param name="memory">The memory.</param>
/// <param name="memory"></param>
/// <param name="chargeType"></param>
internal Extrinsic(Memory<byte> memory, ChargeType chargeType)
{
int p = 0;
int m;

// length
var length = CompactInteger.Decode(memory.ToArray(), ref p);
_ = CompactInteger.Decode(memory.ToArray(), ref p);

// signature version
m = 1;
Expand All @@ -83,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
33 changes: 24 additions & 9 deletions Substrate.NetApi/Model/Extrinsics/Payload.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
using System.Linq;
using Substrate.NetApi.Model.Types;
using System.Linq;

namespace Substrate.NetApi.Model.Extrinsics
{
/// <summary>
/// Payload
/// </summary>
public class Payload
public class Payload : IEncodable
{
private readonly Method _call;

private 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 All @@ -18,17 +25,25 @@ public class Payload
/// <param name="signedExtensions">The signed extensions.</param>
public Payload(Method call, SignedExtensions signedExtensions)
{
_call = call;
_signedExtension = signedExtensions;
Call = call;
SignedExtension = signedExtensions;
}

/// <summary>
/// Encodes this instance.
/// Encodes this instance, returns the encoded bytes. Additionally, if
/// the encoded bytes are longer than 256 bytes, they are hashed using `blake2_256`.
/// </summary>
/// <returns></returns>
public byte[] Encode()
{
byte[] bytes = _call.Encode().Concat(_signedExtension.Encode()).ToArray();
byte[] bytes = Call.Encode().Concat(SignedExtension.Encode()).ToArray();

// Payloads longer than 256 bytes are going to be `blake2_256`-hashed.
if (bytes.Length > 256)
{
bytes = HashExtension.Blake2(bytes, 256);
}

return bytes;
}
}
Expand Down
65 changes: 43 additions & 22 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
{
private readonly uint _specVersion;
/// <summary>
/// Specification Version
/// </summary>
public uint SpecVersion { get; }

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

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

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

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

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

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

/// <summary>
/// Initializes a new instance of the <see cref="SignedExtensions"/> class.
Expand All @@ -31,16 +52,16 @@ 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;
_txVersion = txVersion;
_genesis = genesis;
_startEra = startEra;
_mortality = mortality;
_nonce = nonce;
_charge = charge;
SpecVersion = specVersion;
TxVersion = txVersion;
Genesis = genesis;
StartEra = startEra;
Mortality = mortality;
Nonce = nonce;
Charge = charge;
}

/// <summary>
Expand All @@ -52,13 +73,13 @@ public byte[] GetExtra()
var bytes = new List<byte>();

// CheckMortality
bytes.AddRange(_mortality.Encode());
bytes.AddRange(Mortality.Encode());

// CheckNonce
bytes.AddRange(_nonce.Encode());
bytes.AddRange(Nonce.Encode());

// ChargeType
bytes.AddRange(_charge.Encode());
bytes.AddRange(Charge.Encode());

return bytes.ToArray();
}
Expand All @@ -72,16 +93,16 @@ public byte[] GetAdditionalSigned()
var bytes = new List<byte>();

// CheckSpecVersion
bytes.AddRange(Utils.Value2Bytes(_specVersion));
bytes.AddRange(Utils.Value2Bytes(SpecVersion));

// CheckTxVersion
bytes.AddRange(Utils.Value2Bytes(_txVersion));
bytes.AddRange(Utils.Value2Bytes(TxVersion));

// CheckGenesis
bytes.AddRange(_genesis.Bytes);
bytes.AddRange(Genesis.Bytes);

// CheckMortality, Additional Blockhash check. Immortal = genesis_hash, Mortal = logic
bytes.AddRange(_startEra.Bytes);
bytes.AddRange(StartEra.Bytes);

return bytes.ToArray();
}
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
Loading

0 comments on commit b4951b4

Please sign in to comment.