diff --git a/InterlockLedger.Tags.UnitTests/TagHelpersTests.cs b/InterlockLedger.Tags.UnitTests/TagHelpersTests.cs new file mode 100644 index 0000000..719ef1c --- /dev/null +++ b/InterlockLedger.Tags.UnitTests/TagHelpersTests.cs @@ -0,0 +1,54 @@ +/****************************************************************************************************************************** + * + * Copyright (c) 2017-2019 InterlockLedger Network + * + ******************************************************************************************************************************/ + +using System; +using NUnit.Framework; + +namespace InterlockLedger.Tags +{ + [TestFixture] + public class TagHelpersTests + { + [Test] + public void DecodedTest() { + var tag = new ILTagILInt(14); + var decoded = tag.EncodedBytes.Decoded(); + Assert.AreEqual(tag, decoded); + } + + [Test] + public void ToBytesHelperTest() { + var tag = new ILTagILInt(23); + var bytes = TagHelpers.ToBytesHelper((s) => s.EncodeILInt(23)); + Assert.IsNotNull(bytes); + Assert.That(tag.EncodedBytes, Is.EquivalentTo(bytes)); + var e = Assert.Throws(() => TagHelpers.ToBytesHelper(null)); + Assert.AreEqual("serialize", e.ParamName); + } + + [Test] + public void ToTagArrayFromTest() { + var data = new string[] { "1", "2", "3", "5", "7", "11" }; + var tagArray = data.ToTagArrayFrom(v => new ILTagString(v)); + Assert.IsNotNull(tagArray); + Assert.AreEqual(data.Length, tagArray.Value.Length); + Assert.That(tagArray.GetValues(), Is.EquivalentTo(data)); + var e = Assert.Throws(() => data.ToTagArrayFrom(null)); + Assert.AreEqual("convert", e.ParamName); + } + + [Test] + public void ToTagArrayTest() { + var data = new ulong[] { 1, 2, 3, 5, 7, 11 }; + var tagArray = data.ToTagArray(v => new ILTagILInt(v)); + Assert.IsNotNull(tagArray); + Assert.AreEqual(data.Length, tagArray.Value.Length); + Assert.That(tagArray.GetValues(), Is.EquivalentTo(data)); + var e = Assert.Throws(() => data.ToTagArray(null)); + Assert.AreEqual("convert", e.ParamName); + } + } +} \ No newline at end of file diff --git a/InterlockLedger.Tags/Core/Tags/ILTagArrayOfILTag.cs b/InterlockLedger.Tags/Core/Tags/ILTagArrayOfILTag.cs index 4b1a1fa..c2c5017 100644 --- a/InterlockLedger.Tags/Core/Tags/ILTagArrayOfILTag.cs +++ b/InterlockLedger.Tags/Core/Tags/ILTagArrayOfILTag.cs @@ -49,6 +49,8 @@ public ILTagArrayOfILTag(object json) : base(ILTagId.ILTagArray, Elicit(json)) { public T this[int i] => Value?[i]; + public IEnumerable GetValues() => (Value ?? Enumerable.Empty()).Select(t => t is ILTagImplicit tv ? tv.Value : default); + internal ILTagArrayOfILTag(Stream s) : base(ILTagId.ILTagArray, s) { } diff --git a/InterlockLedger.Tags/Crypto/Types/Keys/RSA/RSACertificateSigningKey.cs b/InterlockLedger.Tags/Crypto/Types/Keys/RSA/RSACertificateSigningKey.cs index 96d7b5a..f47b134 100644 --- a/InterlockLedger.Tags/Crypto/Types/Keys/RSA/RSACertificateSigningKey.cs +++ b/InterlockLedger.Tags/Crypto/Types/Keys/RSA/RSACertificateSigningKey.cs @@ -29,8 +29,7 @@ public override byte[] AsSessionState { ms.EncodeTag(_value); ms.EncodeString(_password); ms.EncodeByteArray(_certificateBytes); - ms.Flush(); - return ms.GetBuffer(); + return ms.ToArray(); } } @@ -43,7 +42,7 @@ public override byte[] AsSessionState { } public override byte[] Decrypt(byte[] bytes) { - using RSA rsa = _certificateBytes.OpenCertificate(_password).GetRSAPrivateKey(); + using var rsa = _certificateBytes.OpenCertificate(_password).GetRSAPrivateKey(); return rsa.Decrypt(bytes, RSAEncryptionPadding.Pkcs1); } @@ -53,7 +52,7 @@ public override byte[] Decrypt(byte[] bytes) { private readonly string _password; private byte[] HashAndSign(byte[] dataToSign) { - using RSA rsa = _certificateBytes.OpenCertificate(_password).GetRSAPrivateKey(); + using var rsa = _certificateBytes.OpenCertificate(_password).GetRSAPrivateKey(); return rsa.SignData(dataToSign, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); } } diff --git a/InterlockLedger.Tags/Crypto/Types/Keys/RSA/RSAInterlockSigningKey.cs b/InterlockLedger.Tags/Crypto/Types/Keys/RSA/RSAInterlockSigningKey.cs index 91a2975..3e475f8 100644 --- a/InterlockLedger.Tags/Crypto/Types/Keys/RSA/RSAInterlockSigningKey.cs +++ b/InterlockLedger.Tags/Crypto/Types/Keys/RSA/RSAInterlockSigningKey.cs @@ -26,8 +26,7 @@ public override byte[] AsSessionState { using var ms = new MemoryStream(); ms.EncodeTag(_value); ms.EncodeTag(_keyParameters); - ms.Flush(); - return ms.GetBuffer(); + return ms.ToArray(); } } diff --git a/InterlockLedger.Tags/Extensions/IEnumerableOfTExtensions.cs b/InterlockLedger.Tags/Extensions/IEnumerableOfTExtensions.cs index f0d1e43..f4c5cd1 100644 --- a/InterlockLedger.Tags/Extensions/IEnumerableOfTExtensions.cs +++ b/InterlockLedger.Tags/Extensions/IEnumerableOfTExtensions.cs @@ -30,6 +30,8 @@ public static IEnumerable IfAnyDo(this IEnumerable values, Action actio return values; } + public static string JoinedBy(this IEnumerable list, string joiner) => list == null ? string.Empty : string.Join(joiner, list); + public static bool None(this IEnumerable items) => !items.SafeAny(); public static bool None(this IEnumerable items, Func predicate) => !items.SafeAny(predicate); @@ -50,6 +52,8 @@ public static IEnumerable SelectSkippingNulls(this IE public static IEnumerable SkipNulls(this IEnumerable values) where T : class => EmptyIfNull(values?.Where(item => item != null)); + public static string WithCommas(this IEnumerable list, bool noSpaces = false) => JoinedBy(list, noSpaces ? "," : ", "); + public static IEnumerable WithDefault(this IEnumerable values, Func> alternativeValues) => values.SafeAny() ? values : EmptyIfNull(alternativeValues?.Invoke()); @@ -69,4 +73,4 @@ private static IEnumerable InnerAppend(T item, IEnumerable remainingIte private static IEnumerable InnerConcat(IEnumerable items, IEnumerable remainingItems) => remainingItems.SafeAny() ? items.Concat(remainingItems) : items; } -} +} \ No newline at end of file diff --git a/InterlockLedger.Tags/Extensions/StringExtensions.cs b/InterlockLedger.Tags/Extensions/StringExtensions.cs index ce8ec69..40aa7b2 100644 --- a/InterlockLedger.Tags/Extensions/StringExtensions.cs +++ b/InterlockLedger.Tags/Extensions/StringExtensions.cs @@ -49,8 +49,6 @@ public static class StringExtensions public static bool IsEmptyOrMatches(this string s, params string[] matches) => string.IsNullOrWhiteSpace(s) || matches.Any(m => s.Trim().Equals(m, StringComparison.InvariantCultureIgnoreCase)); - public static string JoinedBy(this IEnumerable list, string joiner) => list == null ? string.Empty : string.Join(joiner, list); - public static string Parenthesize(this string s) => s.ParenthesizeIf(!s.Parenthesized()); public static bool Parenthesized(this string s) { @@ -96,8 +94,6 @@ public static string SimplifyAsFileName(this string name) { public static string ValidateNonEmpty(this string parameter, string parameterName) => string.IsNullOrWhiteSpace(parameter) ? throw new ArgumentNullException(parameterName) : parameter.Trim(); - public static string WithCommas(this IEnumerable list, bool noSpaces = false) => JoinedBy(list, noSpaces ? "," : ", "); - public static string WithDefault(this string s, string @default) => string.IsNullOrWhiteSpace(s) ? @default : s.Trim(); public static string WithDefault(this string s, Func resolver) { diff --git a/InterlockLedger.Tags/Extensions/TagHelpers.cs b/InterlockLedger.Tags/Extensions/TagHelpers.cs index 77de5da..37a1d08 100644 --- a/InterlockLedger.Tags/Extensions/TagHelpers.cs +++ b/InterlockLedger.Tags/Extensions/TagHelpers.cs @@ -1,5 +1,5 @@ /****************************************************************************************************************************** - + Copyright (c) 2018-2019 InterlockLedger Network All rights reserved. @@ -42,21 +42,17 @@ public static class TagHelpers public static ILTag Decoded(this byte[] buffer) => ILTag.DeserializeFrom(buffer); public static byte[] ToBytesHelper(Action serialize) { - if (serialize == null) { + if (serialize == null) throw new ArgumentNullException(nameof(serialize)); - } - using var s = new MemoryStream(); serialize(s); - s.Flush(); - return s.GetBuffer().PartOf((int)s.Position); + return s.ToArray(); } - public static ILTagArrayOfILTag ToTagArray(this IEnumerable list, Func convert) where TA : ILTag { - if (convert == null) { - throw new ArgumentNullException(nameof(convert)); - } - return new ILTagArrayOfILTag(list?.Select(st => convert(st)).ToArray()); - } + public static ILTagArrayOfILTag ToTagArray(this IEnumerable list, Func convert) where TA : ILTag + => convert == null ? throw new ArgumentNullException(nameof(convert)) : new ILTagArrayOfILTag(list?.Select(st => convert(st)).ToArray()); + + public static ILTagArrayOfILTag> ToTagArrayFrom(this IEnumerable list, Func> convert) + => ToTagArray>(list, convert); } } \ No newline at end of file diff --git a/InterlockLedger.Tags/InterlockLedger.Tags.csproj b/InterlockLedger.Tags/InterlockLedger.Tags.csproj index 2bd4876..9063907 100644 --- a/InterlockLedger.Tags/InterlockLedger.Tags.csproj +++ b/InterlockLedger.Tags/InterlockLedger.Tags.csproj @@ -10,11 +10,11 @@ This library implements the basic TLV encoding/decoding for core types. LICENSE https://interlockledger.network/ - Bring more extension methods and cleanup code + Refactor some extension methods InterlockLedger git https://github.com/interlockledger/interlockledger-tags.git - 2.2.2 + 2.2.3