Skip to content

Commit 44ae531

Browse files
Merge pull request #47 from RemarkableTools/dev
Dev
2 parents 6cac560 + fc08841 commit 44ae531

File tree

12 files changed

+216
-129
lines changed

12 files changed

+216
-129
lines changed

src/Mx.NET.SDK.Core/Domain/Abi/AbiDefinition.cs

Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -40,72 +40,28 @@ public EventDefinition GetEventDefinition(string identifier)
4040
return new EventDefinition(identifier, inputs.ToArray());
4141
}
4242

43-
private TypeValue GetTypeValue(string rustType)
43+
private TypeValue GetTypeValue(string type)
4444
{
45-
var optional = new Regex("^optional<(.*)>$");
46-
var option = new Regex("^Option<(.*)>$");
47-
var multi = new Regex("^multi<(.*)>$");
48-
var tuple = new Regex("^tuple<(.*)>$");
49-
var variadic = new Regex("^variadic<(.*)>$");
50-
var list = new Regex("^List<(.*)>$");
51-
var array = new Regex("^Array<(.*)>$");
52-
53-
if (optional.IsMatch(rustType))
54-
{
55-
var innerType = optional.Match(rustType).Groups[1].Value;
56-
var innerTypeValue = GetTypeValue(innerType);
57-
return TypeValue.OptionalValue(innerTypeValue);
58-
}
59-
60-
if (option.IsMatch(rustType))
61-
{
62-
var innerType = option.Match(rustType).Groups[1].Value;
63-
var innerTypeValue = GetTypeValue(innerType);
64-
return TypeValue.OptionValue(innerTypeValue);
65-
}
66-
67-
if (multi.IsMatch(rustType))
45+
var pattern = new Regex("^(.*?)<(.*)>$");
46+
if (pattern.IsMatch(type))
6847
{
69-
var innerTypes = multi.Match(rustType).Groups[1].Value.Split(',').Where(s => !string.IsNullOrEmpty(s));
70-
var innerTypeValues = innerTypes.Select(GetTypeValue).ToArray();
71-
return TypeValue.MultiValue(innerTypeValues);
72-
}
48+
var parentType = pattern.Match(type).Groups[1].Value;
49+
var innerType = pattern.Match(type).Groups[2].Value;
7350

74-
if (tuple.IsMatch(rustType))
75-
{
76-
var innerTypes = tuple.Match(rustType).Groups[1].Value.Split(',').Where(s => !string.IsNullOrEmpty(s));
51+
var innerTypes = pattern.IsMatch(innerType) ? new[] { innerType } : innerType.Split(',').Where(s => !string.IsNullOrEmpty(s));
7752
var innerTypeValues = innerTypes.Select(GetTypeValue).ToArray();
78-
return TypeValue.TupleValue(innerTypeValues);
79-
}
80-
81-
if (variadic.IsMatch(rustType))
82-
{
83-
var innerType = variadic.Match(rustType).Groups[1].Value;
84-
var innerTypeValue = GetTypeValue(innerType);
85-
return TypeValue.VariadicValue(innerTypeValue);
86-
}
87-
88-
if (list.IsMatch(rustType))
89-
{
90-
var innerType = list.Match(rustType).Groups[1].Value;
91-
var innerTypeValue = GetTypeValue(innerType);
92-
return TypeValue.ListValue(innerTypeValue);
93-
}
94-
95-
if (array.IsMatch(rustType))
96-
{
97-
var innerType = array.Match(rustType).Groups[1].Value;
98-
var innerTypeValue = GetTypeValue(innerType);
99-
return TypeValue.ArrayValue(innerTypeValue);
53+
var typeFromLearnedTypes = TypeValue.FromLearnedType(parentType, innerTypeValues);
54+
if (typeFromLearnedTypes != null)
55+
return typeFromLearnedTypes;
10056
}
10157

102-
var typeFromBaseRustType = TypeValue.FromRustType(rustType);
58+
var typeFromBaseRustType = TypeValue.FromRustType(type);
10359
if (typeFromBaseRustType != null)
10460
return typeFromBaseRustType;
10561

106-
if (Types.Keys.Contains(rustType))
62+
if (Types.Keys.Contains(type))
10763
{
108-
var typeFromStruct = Types[rustType];
64+
var typeFromStruct = Types[type];
10965
if (typeFromStruct.Type == "enum")
11066
{
11167
return TypeValue.EnumValue(typeFromStruct.Type,
Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,63 @@
11
using System.Collections.Generic;
22
using System.Linq;
3-
using Mx.NET.SDK.Core.Domain.Exceptions;
43
using Mx.NET.SDK.Core.Domain.Helper;
54
using Mx.NET.SDK.Core.Domain.Values;
65

76
namespace Mx.NET.SDK.Core.Domain.Codec
87
{
98
public class ArrayBinaryCodec : IBinaryCodec
10-
{
11-
private readonly BinaryCodec _binaryCodec;
12-
13-
public ArrayBinaryCodec(BinaryCodec binaryCodec)
14-
{
15-
_binaryCodec = binaryCodec;
16-
}
17-
18-
public string Type => TypeValue.BinaryTypes.Array;
19-
20-
public (IBinaryType Value, int BytesLength) DecodeNested(byte[] data, TypeValue type)
21-
{
22-
var result = new List<IBinaryType>();
23-
var originalBuffer = data;
24-
var offset = 0;
25-
26-
while (data.Length > 0)
27-
{
28-
var (value, bytesLength) = _binaryCodec.DecodeNested(data, type.InnerType);
29-
result.Add(value);
30-
offset += bytesLength;
31-
data = originalBuffer.Slice(offset);
32-
}
33-
34-
var arrayValue = new ArrayValue(type, type.InnerType, result.ToArray());
35-
return (arrayValue, offset);
36-
}
37-
38-
public IBinaryType DecodeTopLevel(byte[] data, TypeValue type)
39-
{
40-
var (value, _) = DecodeNested(data, type);
41-
return value;
42-
}
43-
44-
public byte[] EncodeNested(IBinaryType value)
45-
{
46-
var arrayValueObject = value.ValueOf<ArrayValue>();
47-
var buffers = new List<byte[]>();
48-
49-
foreach (var arrayValue in arrayValueObject.Values)
50-
{
51-
var fieldBuffer = _binaryCodec.EncodeNested(arrayValue);
52-
buffers.Add(fieldBuffer);
53-
}
54-
55-
var data = buffers.SelectMany(s => s);
56-
return data.ToArray();
57-
}
58-
59-
public byte[] EncodeTopLevel(IBinaryType value)
60-
{
61-
return EncodeNested(value);
62-
}
63-
}
9+
{
10+
private readonly BinaryCodec _binaryCodec;
11+
12+
public ArrayBinaryCodec(BinaryCodec binaryCodec)
13+
{
14+
_binaryCodec = binaryCodec;
15+
}
16+
17+
public string Type => TypeValue.BinaryTypes.Array;
18+
19+
public (IBinaryType Value, int BytesLength) DecodeNested(byte[] data, TypeValue type)
20+
{
21+
var result = new List<IBinaryType>();
22+
var originalBuffer = data;
23+
var offset = 0;
24+
25+
for (int i = 0; i < type.Length; i++)
26+
{
27+
var (value, bytesLength) = _binaryCodec.DecodeNested(data, type.InnerType);
28+
result.Add(value);
29+
offset += bytesLength;
30+
data = originalBuffer.Slice(offset);
31+
}
32+
33+
var arrayValue = new ArrayValue(type, type.InnerType, result.ToArray());
34+
return (arrayValue, offset);
35+
}
36+
37+
public IBinaryType DecodeTopLevel(byte[] data, TypeValue type)
38+
{
39+
var (value, _) = DecodeNested(data, type);
40+
return value;
41+
}
42+
43+
public byte[] EncodeNested(IBinaryType value)
44+
{
45+
var arrayValueObject = value.ValueOf<ArrayValue>();
46+
var buffers = new List<byte[]>();
47+
48+
foreach (var arrayValue in arrayValueObject.Values)
49+
{
50+
var fieldBuffer = _binaryCodec.EncodeNested(arrayValue);
51+
buffers.Add(fieldBuffer);
52+
}
53+
54+
var data = buffers.SelectMany(s => s);
55+
return data.ToArray();
56+
}
57+
58+
public byte[] EncodeTopLevel(IBinaryType value)
59+
{
60+
return EncodeNested(value);
61+
}
62+
}
6463
}

src/Mx.NET.SDK.Core/Domain/Values/ArrayValue.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Mx.NET.SDK.Core.Domain.Helper;
2-
using System.Collections.Generic;
32
using System.Linq;
43
using System.Text;
54

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

1918
public static ArrayValue From(TypeValue type, params IBinaryType[] values)
2019
{
21-
return new ArrayValue(TypeValue.ArrayValue(type), type.InnerType, values);
20+
return new ArrayValue(TypeValue.ArrayValue(type, values.Length), type.InnerType, values);
2221
}
2322

2423
public override string ToString()

src/Mx.NET.SDK.Core/Domain/Values/TypeValue.cs

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class TypeValue
1010
public string RustType { get; }
1111
public TypeValue InnerType { get; }
1212
public TypeValue[] MultiTypes { get; }
13+
public int? Length { get; }
1314

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

34-
public TypeValue(string binaryType, TypeValue innerType = null)
35+
public TypeValue(string binaryType, TypeValue innerType = null, int? length = null)
3536
{
3637
BinaryType = binaryType;
3738
InnerType = innerType;
39+
Length = length;
3840
}
3941

4042
public TypeValue(string binaryType, TypeValue[] multiTypes)
@@ -83,6 +85,39 @@ public static class BinaryTypes
8385
public const string Enum = nameof(Enum);
8486
}
8587

88+
public static class LearnedTypes
89+
{
90+
public const string Option = "Option";
91+
public const string List = "List";
92+
public const string VarArgs = "VarArgs";
93+
public const string MultiResultVec = "MultiResultVec";
94+
public const string Variadic = "variadic";
95+
public const string OptionalArg = "OptionalArg";
96+
public const string Optional = "optional";
97+
public const string OptionalResult = "OptionalResult";
98+
public const string Multi = "multi";
99+
public const string MultiArg = "MultiArg";
100+
public const string MultiResult = "MultiResult";
101+
public const string Tuple = "tuple";
102+
public const string Tuple2 = "tuple2";
103+
public const string Tuple3 = "tuple3";
104+
public const string Tuple4 = "tuple4";
105+
public const string Tuple5 = "tuple5";
106+
public const string Tuple6 = "tuple6";
107+
public const string Tuple7 = "tuple7";
108+
public const string Tuple8 = "tuple8";
109+
public const string Array = "Array";
110+
public const string Array2 = "array2";
111+
public const string Array8 = "array8";
112+
public const string Array16 = "array16";
113+
public const string Array20 = "array20";
114+
public const string Array32 = "array32";
115+
public const string Array46 = "array46";
116+
public const string Array64 = "array64";
117+
public const string Array128 = "array128";
118+
public const string Array256 = "array256";
119+
}
120+
86121
public static class RustTypes
87122
{
88123
public const string U8 = "u8";
@@ -136,13 +171,72 @@ public static class RustTypes
136171
public static TypeValue TupleValue(TypeValue[] tupleTypes) => new TypeValue(BinaryTypes.Tuple, tupleTypes);
137172
public static TypeValue VariadicValue(TypeValue innerType) => new TypeValue(BinaryTypes.Variadic, innerType);
138173
public static TypeValue ListValue(TypeValue innerType) => new TypeValue(BinaryTypes.List, innerType);
139-
public static TypeValue ArrayValue(TypeValue innerType) => new TypeValue(BinaryTypes.Array, innerType);
140-
174+
public static TypeValue ArrayValue(TypeValue innerType, int length) => new TypeValue(BinaryTypes.Array, innerType, length);
175+
141176
public static TypeValue StructValue(string name, FieldDefinition[] fieldDefinitions) =>
142177
new TypeValue(BinaryTypes.Struct, name, fieldDefinitions);
143178
public static TypeValue EnumValue(string name, FieldDefinition[] fieldDefinitions) =>
144179
new TypeValue(BinaryTypes.Enum, name, fieldDefinitions);
145180

181+
public static TypeValue FromLearnedType(string learnedType, TypeValue[] types)
182+
{
183+
switch (learnedType)
184+
{
185+
case LearnedTypes.Option:
186+
return OptionValue(types[0]);
187+
188+
case LearnedTypes.List:
189+
return ListValue(types[0]);
190+
191+
case LearnedTypes.VarArgs:
192+
case LearnedTypes.MultiResultVec:
193+
case LearnedTypes.Variadic:
194+
return VariadicValue(types[0]);
195+
196+
case LearnedTypes.OptionalArg:
197+
case LearnedTypes.Optional:
198+
case LearnedTypes.OptionalResult:
199+
return OptionalValue(types[0]);
200+
201+
case LearnedTypes.Multi:
202+
case LearnedTypes.MultiArg:
203+
case LearnedTypes.MultiResult:
204+
return MultiValue(types);
205+
206+
case LearnedTypes.Tuple:
207+
case LearnedTypes.Tuple2:
208+
case LearnedTypes.Tuple3:
209+
case LearnedTypes.Tuple4:
210+
case LearnedTypes.Tuple5:
211+
case LearnedTypes.Tuple6:
212+
case LearnedTypes.Tuple7:
213+
case LearnedTypes.Tuple8:
214+
return TupleValue(types);
215+
216+
case LearnedTypes.Array2:
217+
return ArrayValue(types[0], 2);
218+
case LearnedTypes.Array8:
219+
return ArrayValue(types[0], 8);
220+
case LearnedTypes.Array16:
221+
return ArrayValue(types[0], 16);
222+
case LearnedTypes.Array20:
223+
return ArrayValue(types[0], 20);
224+
case LearnedTypes.Array32:
225+
return ArrayValue(types[0], 32);
226+
case LearnedTypes.Array46:
227+
return ArrayValue(types[0], 46);
228+
case LearnedTypes.Array64:
229+
return ArrayValue(types[0], 64);
230+
case LearnedTypes.Array128:
231+
return ArrayValue(types[0], 128);
232+
case LearnedTypes.Array256:
233+
return ArrayValue(types[0], 256);
234+
235+
default:
236+
return null;
237+
}
238+
}
239+
146240
public static TypeValue FromRustType(string rustType)
147241
{
148242
switch (rustType)
@@ -169,10 +263,10 @@ public static TypeValue FromRustType(string rustType)
169263
case RustTypes.Bigint:
170264
return BigIntTypeValue;
171265

172-
case RustTypes.Bytes:
173-
return BytesValue;
174266
case RustTypes.Bool:
175267
return BooleanValue;
268+
case RustTypes.Bytes:
269+
return BytesValue;
176270
case RustTypes.Address:
177271
return AddressValue;
178272
case RustTypes.TokenIdentifier:

src/Mx.NET.SDK.Core/Mx.NET.SDK.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<RepositoryUrl>https://github.com/RemarkableTools/Mx.NET.SDK/tree/main/src/Mx.NET.SDK.Core</RepositoryUrl>
1212
<RepositoryType>GitHub</RepositoryType>
1313
<Company>Remarkable Tools</Company>
14-
<Version>2.0.1</Version>
14+
<Version>2.0.2</Version>
1515
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
1616
<Title>RemarkableTools.Mx.Core</Title>
1717
<PackageReadmeFile>README.md</PackageReadmeFile>

src/Mx.NET.SDK.Wallet/Mx.NET.SDK.Wallet.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<RepositoryUrl>https://github.com/RemarkableTools/Mx.NET.SDK/tree/main/src/Mx.NET.SDK.Wallet</RepositoryUrl>
1212
<RepositoryType>GitHub</RepositoryType>
1313
<Company>Remarkable Tools</Company>
14-
<Version>2.0.0</Version>
14+
<Version>2.0.1</Version>
1515
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
1616
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
1717
<Title>RemarkableTools.Mx.Wallet</Title>

0 commit comments

Comments
 (0)