Skip to content

Commit

Permalink
Merge NET.Wallet Mnemonic to NET.Api Mnemonic (#62)
Browse files Browse the repository at this point in the history
* Merge NET.Wallet Mnemonic to NET.Api Mnemonic to stay coherent + bytes and string extension as dependencies

* Update StringExtension.cs

---------

Co-authored-by: Cedric Decoster <[email protected]>
  • Loading branch information
Apolixit and darkfriend77 authored Oct 22, 2023
1 parent 80bf304 commit 01a63d2
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 400 deletions.
62 changes: 62 additions & 0 deletions Substrate.NetApi.Test/Extensions/BytesExtensionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using NUnit.Framework;
using Substrate.NetApi.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Substrate.NetApi.Test.Extensions
{
public class BytesExtensionTest
{
[Test]
public void BytesFixLength_WhenNoChangeNeeded_ReturnsOriginalArray()
{
byte[] originalBytes = new byte[] { 0x01, 0x02, 0x03, 0x04 };
int bitLength = originalBytes.Length * 8;

byte[] result = originalBytes.BytesFixLength(bitLength);

Assert.That(originalBytes, Is.EqualTo(result));
}

[Test]
public void BytesFixLength_WhenShorter_WithAtStart_True_PrependsZeros()
{
byte[] originalBytes = new byte[] { 0x01, 0x02 };
int bitLength = 32; // 4 bytes
bool atStart = true;

byte[] result = originalBytes.BytesFixLength(bitLength, atStart);

byte[] expected = new byte[] { 0x01, 0x02, 0x00, 0x00 };
Assert.That(expected, Is.EqualTo(result));
}

[Test]
public void BytesFixLength_WhenShorter_WithAtStart_False_AppendsZeros()
{
byte[] originalBytes = new byte[] { 0x01, 0x02 };
int bitLength = 32; // 4 bytes
bool atStart = false;

byte[] result = originalBytes.BytesFixLength(bitLength, atStart);

byte[] expected = new byte[] { 0x00, 0x00, 0x01, 0x02 };
Assert.That(expected, Is.EqualTo(result));
}

[Test]
public void BytesFixLength_WhenLonger_TruncatesArray()
{
byte[] originalBytes = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 };
int bitLength = 24; // 3 bytes

byte[] result = originalBytes.BytesFixLength(bitLength);

byte[] expected = new byte[] { 0x01, 0x02, 0x03 };
Assert.That(expected, Is.EqualTo(result));
}
}
}
31 changes: 31 additions & 0 deletions Substrate.NetApi.Test/Extensions/HexadecimalValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using NUnit.Framework;
using Substrate.NetApi.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Substrate.NetApi.Test.Extensions
{
public class HexadecimalValidatorTests
{
[Test]
[TestCase("", false)]
[TestCase("1234567890ABCDEF", true)]
[TestCase("0x1234567890ABCDEF", true)]
[TestCase("0x1234567890ABCDEF", true)]
[TestCase("1A2F3D", true)]
[TestCase("abcdef", true)]
[TestCase("0Xabcdef", true)]
[TestCase("12G", false)]
[TestCase("1X2", false)]
[TestCase(" ", false)]
[TestCase(null, false)]
public void IsHexadecimal_ValidatingHexadecimalStrings(string input, bool expected)
{
bool result = input.IsHex();
Assert.That(expected, Is.EqualTo(result));
}
}
}
46 changes: 46 additions & 0 deletions Substrate.NetApi/Extensions/BytesExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace Substrate.NetApi.Extensions
{
public static class BytesExtension
{
private static readonly RandomNumberGenerator RandomGenerator = RandomNumberGenerator.Create();

/// <summary>
/// Load a byte array with random bytes
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] Populate(this byte[] data)
{
RandomGenerator.GetBytes(data);
return data;
}

public static byte[] BytesFixLength(this byte[] value, int bitLength = -1, bool atStart = false)
{
int byteLength = (bitLength == -1) ? value.Length : (int)Math.Ceiling(bitLength / 8.0);

if (value.Length == byteLength)
{
return value;
}

if (value.Length > byteLength)
{
return value.Take(byteLength).ToArray();
}

byte[] result = new byte[byteLength];
int copyIndex = atStart ? 0 : byteLength - value.Length;

Array.Copy(value, 0, result, copyIndex, value.Length);

return result;
}
}
}
42 changes: 42 additions & 0 deletions Substrate.NetApi/Extensions/StringExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Substrate.NetApi.Extensions
{
public static class StringExtension
{
public static byte[] ToBytes(this string value)
{
return Encoding.UTF8.GetBytes(value);
}

public static bool IsHex(this string value)
{
if (string.IsNullOrEmpty(value))
return false;

if (value.ToLower().StartsWith("0x"))
value = value.Remove(0, 2);

return value.Length % 2 == 0 && IsHexCharacters(value);
}

private static bool IsHexCharacters(string input)
{
foreach (char c in input)
{
if (!IsHexDigit(c))
{
return false;
}
}
return true;
}

private static bool IsHexDigit(char c)
{
return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
}
}
}
Loading

0 comments on commit 01a63d2

Please sign in to comment.