Skip to content

Commit

Permalink
Merge pull request #47 from RemarkableTools/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
axenteoctavian authored Aug 10, 2023
2 parents 6cac560 + fc08841 commit 44ae531
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 129 deletions.
68 changes: 12 additions & 56 deletions src/Mx.NET.SDK.Core/Domain/Abi/AbiDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,72 +40,28 @@ public EventDefinition GetEventDefinition(string identifier)
return new EventDefinition(identifier, inputs.ToArray());
}

private TypeValue GetTypeValue(string rustType)
private TypeValue GetTypeValue(string type)
{
var optional = new Regex("^optional<(.*)>$");
var option = new Regex("^Option<(.*)>$");
var multi = new Regex("^multi<(.*)>$");
var tuple = new Regex("^tuple<(.*)>$");
var variadic = new Regex("^variadic<(.*)>$");
var list = new Regex("^List<(.*)>$");
var array = new Regex("^Array<(.*)>$");

if (optional.IsMatch(rustType))
{
var innerType = optional.Match(rustType).Groups[1].Value;
var innerTypeValue = GetTypeValue(innerType);
return TypeValue.OptionalValue(innerTypeValue);
}

if (option.IsMatch(rustType))
{
var innerType = option.Match(rustType).Groups[1].Value;
var innerTypeValue = GetTypeValue(innerType);
return TypeValue.OptionValue(innerTypeValue);
}

if (multi.IsMatch(rustType))
var pattern = new Regex("^(.*?)<(.*)>$");
if (pattern.IsMatch(type))
{
var innerTypes = multi.Match(rustType).Groups[1].Value.Split(',').Where(s => !string.IsNullOrEmpty(s));
var innerTypeValues = innerTypes.Select(GetTypeValue).ToArray();
return TypeValue.MultiValue(innerTypeValues);
}
var parentType = pattern.Match(type).Groups[1].Value;
var innerType = pattern.Match(type).Groups[2].Value;

if (tuple.IsMatch(rustType))
{
var innerTypes = tuple.Match(rustType).Groups[1].Value.Split(',').Where(s => !string.IsNullOrEmpty(s));
var innerTypes = pattern.IsMatch(innerType) ? new[] { innerType } : innerType.Split(',').Where(s => !string.IsNullOrEmpty(s));
var innerTypeValues = innerTypes.Select(GetTypeValue).ToArray();
return TypeValue.TupleValue(innerTypeValues);
}

if (variadic.IsMatch(rustType))
{
var innerType = variadic.Match(rustType).Groups[1].Value;
var innerTypeValue = GetTypeValue(innerType);
return TypeValue.VariadicValue(innerTypeValue);
}

if (list.IsMatch(rustType))
{
var innerType = list.Match(rustType).Groups[1].Value;
var innerTypeValue = GetTypeValue(innerType);
return TypeValue.ListValue(innerTypeValue);
}

if (array.IsMatch(rustType))
{
var innerType = array.Match(rustType).Groups[1].Value;
var innerTypeValue = GetTypeValue(innerType);
return TypeValue.ArrayValue(innerTypeValue);
var typeFromLearnedTypes = TypeValue.FromLearnedType(parentType, innerTypeValues);
if (typeFromLearnedTypes != null)
return typeFromLearnedTypes;
}

var typeFromBaseRustType = TypeValue.FromRustType(rustType);
var typeFromBaseRustType = TypeValue.FromRustType(type);
if (typeFromBaseRustType != null)
return typeFromBaseRustType;

if (Types.Keys.Contains(rustType))
if (Types.Keys.Contains(type))
{
var typeFromStruct = Types[rustType];
var typeFromStruct = Types[type];
if (typeFromStruct.Type == "enum")
{
return TypeValue.EnumValue(typeFromStruct.Type,
Expand Down
109 changes: 54 additions & 55 deletions src/Mx.NET.SDK.Core/Domain/Codec/ArrayBinaryCodec.cs
Original file line number Diff line number Diff line change
@@ -1,64 +1,63 @@
using System.Collections.Generic;
using System.Linq;
using Mx.NET.SDK.Core.Domain.Exceptions;
using Mx.NET.SDK.Core.Domain.Helper;
using Mx.NET.SDK.Core.Domain.Values;

namespace Mx.NET.SDK.Core.Domain.Codec
{
public class ArrayBinaryCodec : IBinaryCodec
{
private readonly BinaryCodec _binaryCodec;

public ArrayBinaryCodec(BinaryCodec binaryCodec)
{
_binaryCodec = binaryCodec;
}

public string Type => TypeValue.BinaryTypes.Array;

public (IBinaryType Value, int BytesLength) DecodeNested(byte[] data, TypeValue type)
{
var result = new List<IBinaryType>();
var originalBuffer = data;
var offset = 0;

while (data.Length > 0)
{
var (value, bytesLength) = _binaryCodec.DecodeNested(data, type.InnerType);
result.Add(value);
offset += bytesLength;
data = originalBuffer.Slice(offset);
}

var arrayValue = new ArrayValue(type, type.InnerType, result.ToArray());
return (arrayValue, offset);
}

public IBinaryType DecodeTopLevel(byte[] data, TypeValue type)
{
var (value, _) = DecodeNested(data, type);
return value;
}

public byte[] EncodeNested(IBinaryType value)
{
var arrayValueObject = value.ValueOf<ArrayValue>();
var buffers = new List<byte[]>();

foreach (var arrayValue in arrayValueObject.Values)
{
var fieldBuffer = _binaryCodec.EncodeNested(arrayValue);
buffers.Add(fieldBuffer);
}

var data = buffers.SelectMany(s => s);
return data.ToArray();
}

public byte[] EncodeTopLevel(IBinaryType value)
{
return EncodeNested(value);
}
}
{
private readonly BinaryCodec _binaryCodec;

public ArrayBinaryCodec(BinaryCodec binaryCodec)
{
_binaryCodec = binaryCodec;
}

public string Type => TypeValue.BinaryTypes.Array;

public (IBinaryType Value, int BytesLength) DecodeNested(byte[] data, TypeValue type)
{
var result = new List<IBinaryType>();
var originalBuffer = data;
var offset = 0;

for (int i = 0; i < type.Length; i++)
{
var (value, bytesLength) = _binaryCodec.DecodeNested(data, type.InnerType);
result.Add(value);
offset += bytesLength;
data = originalBuffer.Slice(offset);
}

var arrayValue = new ArrayValue(type, type.InnerType, result.ToArray());
return (arrayValue, offset);
}

public IBinaryType DecodeTopLevel(byte[] data, TypeValue type)
{
var (value, _) = DecodeNested(data, type);
return value;
}

public byte[] EncodeNested(IBinaryType value)
{
var arrayValueObject = value.ValueOf<ArrayValue>();
var buffers = new List<byte[]>();

foreach (var arrayValue in arrayValueObject.Values)
{
var fieldBuffer = _binaryCodec.EncodeNested(arrayValue);
buffers.Add(fieldBuffer);
}

var data = buffers.SelectMany(s => s);
return data.ToArray();
}

public byte[] EncodeTopLevel(IBinaryType value)
{
return EncodeNested(value);
}
}
}
3 changes: 1 addition & 2 deletions src/Mx.NET.SDK.Core/Domain/Values/ArrayValue.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Mx.NET.SDK.Core.Domain.Helper;
using System.Collections.Generic;
using System.Linq;
using System.Text;

Expand All @@ -18,7 +17,7 @@ public ArrayValue(TypeValue type, TypeValue innerType, IBinaryType[] values) : b

public static ArrayValue From(TypeValue type, params IBinaryType[] values)
{
return new ArrayValue(TypeValue.ArrayValue(type), type.InnerType, values);
return new ArrayValue(TypeValue.ArrayValue(type, values.Length), type.InnerType, values);
}

public override string ToString()
Expand Down
104 changes: 99 additions & 5 deletions src/Mx.NET.SDK.Core/Domain/Values/TypeValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class TypeValue
public string RustType { get; }
public TypeValue InnerType { get; }
public TypeValue[] MultiTypes { get; }
public int? Length { get; }

private readonly int? _sizeInBytes;
private readonly bool? _withSign;
Expand All @@ -31,10 +32,11 @@ public TypeValue(string binaryType, string rustType, FieldDefinition[] fieldDefi
_fieldDefinitions = fieldDefinitions;
}

public TypeValue(string binaryType, TypeValue innerType = null)
public TypeValue(string binaryType, TypeValue innerType = null, int? length = null)
{
BinaryType = binaryType;
InnerType = innerType;
Length = length;
}

public TypeValue(string binaryType, TypeValue[] multiTypes)
Expand Down Expand Up @@ -83,6 +85,39 @@ public static class BinaryTypes
public const string Enum = nameof(Enum);
}

public static class LearnedTypes
{
public const string Option = "Option";
public const string List = "List";
public const string VarArgs = "VarArgs";
public const string MultiResultVec = "MultiResultVec";
public const string Variadic = "variadic";
public const string OptionalArg = "OptionalArg";
public const string Optional = "optional";
public const string OptionalResult = "OptionalResult";
public const string Multi = "multi";
public const string MultiArg = "MultiArg";
public const string MultiResult = "MultiResult";
public const string Tuple = "tuple";
public const string Tuple2 = "tuple2";
public const string Tuple3 = "tuple3";
public const string Tuple4 = "tuple4";
public const string Tuple5 = "tuple5";
public const string Tuple6 = "tuple6";
public const string Tuple7 = "tuple7";
public const string Tuple8 = "tuple8";
public const string Array = "Array";
public const string Array2 = "array2";
public const string Array8 = "array8";
public const string Array16 = "array16";
public const string Array20 = "array20";
public const string Array32 = "array32";
public const string Array46 = "array46";
public const string Array64 = "array64";
public const string Array128 = "array128";
public const string Array256 = "array256";
}

public static class RustTypes
{
public const string U8 = "u8";
Expand Down Expand Up @@ -136,13 +171,72 @@ public static class RustTypes
public static TypeValue TupleValue(TypeValue[] tupleTypes) => new TypeValue(BinaryTypes.Tuple, tupleTypes);
public static TypeValue VariadicValue(TypeValue innerType) => new TypeValue(BinaryTypes.Variadic, innerType);
public static TypeValue ListValue(TypeValue innerType) => new TypeValue(BinaryTypes.List, innerType);
public static TypeValue ArrayValue(TypeValue innerType) => new TypeValue(BinaryTypes.Array, innerType);
public static TypeValue ArrayValue(TypeValue innerType, int length) => new TypeValue(BinaryTypes.Array, innerType, length);

public static TypeValue StructValue(string name, FieldDefinition[] fieldDefinitions) =>
new TypeValue(BinaryTypes.Struct, name, fieldDefinitions);
public static TypeValue EnumValue(string name, FieldDefinition[] fieldDefinitions) =>
new TypeValue(BinaryTypes.Enum, name, fieldDefinitions);

public static TypeValue FromLearnedType(string learnedType, TypeValue[] types)
{
switch (learnedType)
{
case LearnedTypes.Option:
return OptionValue(types[0]);

case LearnedTypes.List:
return ListValue(types[0]);

case LearnedTypes.VarArgs:
case LearnedTypes.MultiResultVec:
case LearnedTypes.Variadic:
return VariadicValue(types[0]);

case LearnedTypes.OptionalArg:
case LearnedTypes.Optional:
case LearnedTypes.OptionalResult:
return OptionalValue(types[0]);

case LearnedTypes.Multi:
case LearnedTypes.MultiArg:
case LearnedTypes.MultiResult:
return MultiValue(types);

case LearnedTypes.Tuple:
case LearnedTypes.Tuple2:
case LearnedTypes.Tuple3:
case LearnedTypes.Tuple4:
case LearnedTypes.Tuple5:
case LearnedTypes.Tuple6:
case LearnedTypes.Tuple7:
case LearnedTypes.Tuple8:
return TupleValue(types);

case LearnedTypes.Array2:
return ArrayValue(types[0], 2);
case LearnedTypes.Array8:
return ArrayValue(types[0], 8);
case LearnedTypes.Array16:
return ArrayValue(types[0], 16);
case LearnedTypes.Array20:
return ArrayValue(types[0], 20);
case LearnedTypes.Array32:
return ArrayValue(types[0], 32);
case LearnedTypes.Array46:
return ArrayValue(types[0], 46);
case LearnedTypes.Array64:
return ArrayValue(types[0], 64);
case LearnedTypes.Array128:
return ArrayValue(types[0], 128);
case LearnedTypes.Array256:
return ArrayValue(types[0], 256);

default:
return null;
}
}

public static TypeValue FromRustType(string rustType)
{
switch (rustType)
Expand All @@ -169,10 +263,10 @@ public static TypeValue FromRustType(string rustType)
case RustTypes.Bigint:
return BigIntTypeValue;

case RustTypes.Bytes:
return BytesValue;
case RustTypes.Bool:
return BooleanValue;
case RustTypes.Bytes:
return BytesValue;
case RustTypes.Address:
return AddressValue;
case RustTypes.TokenIdentifier:
Expand Down
2 changes: 1 addition & 1 deletion src/Mx.NET.SDK.Core/Mx.NET.SDK.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<RepositoryUrl>https://github.com/RemarkableTools/Mx.NET.SDK/tree/main/src/Mx.NET.SDK.Core</RepositoryUrl>
<RepositoryType>GitHub</RepositoryType>
<Company>Remarkable Tools</Company>
<Version>2.0.1</Version>
<Version>2.0.2</Version>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Title>RemarkableTools.Mx.Core</Title>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
2 changes: 1 addition & 1 deletion src/Mx.NET.SDK.Wallet/Mx.NET.SDK.Wallet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<RepositoryUrl>https://github.com/RemarkableTools/Mx.NET.SDK/tree/main/src/Mx.NET.SDK.Wallet</RepositoryUrl>
<RepositoryType>GitHub</RepositoryType>
<Company>Remarkable Tools</Company>
<Version>2.0.0</Version>
<Version>2.0.1</Version>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<Title>RemarkableTools.Mx.Wallet</Title>
Expand Down
Loading

0 comments on commit 44ae531

Please sign in to comment.