Skip to content

Commit 88e3eb4

Browse files
Fix/tuple decoding (#200)
* Fix test * Fix tuple type parsing such that we include multi-dimensional arrays * Fix nested array decoding * Fix tests using inactive chains * increment package version * Reset sequence login authenticator during tests that use mocks * Fix decoding on PC - carriage return character handling * Fix tests on PC * Reset connector too
1 parent e6d674d commit 88e3eb4

File tree

10 files changed

+65
-31
lines changed

10 files changed

+65
-31
lines changed

Assets/SequenceSDK/Ethereum/Tests/DecodeABITests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,8 @@ public void TestDecodeNumber_throwsOnInvalidType<T>(T type)
337337
catch (ArgumentException ex)
338338
{
339339
Assert.AreEqual(
340-
$"Unable to decode to type \'{typeof(T)}\' when ABI expects to decode to type \'uint256\'. Supported types: {typeof(BigInteger)}, {typeof(int)}, {typeof(uint)}",
340+
$"Unable to decode to type \'{typeof(T)}\' when ABI expects to decode to type \'uint256\'. Supported types: {typeof(BigInteger)}, {typeof(int)}, {typeof(long)}, {typeof(uint)}, {typeof(ulong)}",
341+
341342
ex.Message);
342343
}
343344
catch (Exception ex)
@@ -401,7 +402,7 @@ public void TestDecodeBool_throwsOnInvalidType<T>(T type)
401402
{
402403
new object[] { "0x000000000000000000000000000000000000000000000000000000000000000e" + "53444b20627920486f72697a6f6e000000000000000000000000000000000000", Encoding.UTF8.GetBytes("SDK by Horizon") },
403404
new object[] { "0x0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000", Encoding.UTF8.GetBytes("")},
404-
new object[] { "0x0000000000000000000000000000000000000000000000000000000000000114" + "7468697320697320610a6d756c74692d6c696e6520737472696e670a7468617420697320616c736f20726174686572206c6f6e6720616e6420706f696e746c6573730a6275742069662049206a757374206b65657020747970696e67206d617962652069742077696c6c206d616b6520666f7220616e20696e746572657374696e67207465737420636173650a616e64206d617962652c0a6a757374206d617962652c0a796f752061732061207265616465722077696c6c20646576656c6f7020616e206576657220736f20736c6967687420736d696c65206f6e20796f757220666163650a616e6420696e207468617420636173652c206d79206d697373696f6e2077696c6c20626520636f6d706c6574652e000000000000000000000000", Encoding.UTF8.GetBytes(longMultiLineString) },
405+
new object[] { "0x0000000000000000000000000000000000000000000000000000000000000114" + "7468697320697320610a6d756c74692d6c696e6520737472696e670a7468617420697320616c736f20726174686572206c6f6e6720616e6420706f696e746c6573730a6275742069662049206a757374206b65657020747970696e67206d617962652069742077696c6c206d616b6520666f7220616e20696e746572657374696e67207465737420636173650a616e64206d617962652c0a6a757374206d617962652c0a796f752061732061207265616465722077696c6c20646576656c6f7020616e206576657220736f20736c6967687420736d696c65206f6e20796f757220666163650a616e6420696e207468617420636173652c206d79206d697373696f6e2077696c6c20626520636f6d706c6574652e000000000000000000000000", Encoding.UTF8.GetBytes(longMultiLineString.Replace("\r", "")) },
405406
};
406407

407408
[TestCaseSource(nameof(DecodeBytesTests))]

Assets/SequenceSDK/Ethereum/Tests/SequenceEthClientTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public async Task TestBlockRange()
135135
[TestCaseSource(nameof(chainIdCases))]
136136
public async Task TestChainId(Chain chain)
137137
{
138-
if (chain == Chain.None) return;
138+
if (!chain.IsActive()) return;
139139
try
140140
{
141141
var client = new SequenceEthClient(chain);

Assets/SequenceSDK/Indexer/Tests/ChainIndexerTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ChainIndexerTests
2020
[TestCaseSource(nameof(chainIdCases))]
2121
public void TestCreateChainIndexerForChain(Chain chain)
2222
{
23-
if (ChainIsInactive(chain)) return;
23+
if (!chain.IsActive()) return;
2424
try
2525
{
2626
ChainIndexer chainIndexer = new ChainIndexer(chain);
@@ -36,7 +36,7 @@ public void TestCreateChainIndexerForChain(Chain chain)
3636
[TestCaseSource(nameof(chainIdCases))]
3737
public async Task TestPingChain(Chain chain)
3838
{
39-
if (ChainIsInactive(chain)) return;
39+
if (!chain.IsActive()) return;
4040
try
4141
{
4242
IIndexer chainIndexer = new ChainIndexer(chain);
@@ -52,7 +52,7 @@ public async Task TestPingChain(Chain chain)
5252
[TestCaseSource(nameof(chainIdCases))]
5353
public async Task TestVersion(Chain chain)
5454
{
55-
if (ChainIsInactive(chain)) return;
55+
if (!chain.IsActive()) return;
5656
try
5757
{
5858
IIndexer indexer = new ChainIndexer(chain);
@@ -69,6 +69,7 @@ public async Task TestVersion(Chain chain)
6969
}
7070
}
7171

72+
7273
private bool ChainIsInactive(Chain chain)
7374
{
7475
return chain == Chain.None || chain == Chain.AstarZKEvm || chain == Chain.TestnetAstarZKyoto || chain == Chain.TestnetBorne;
@@ -77,7 +78,7 @@ private bool ChainIsInactive(Chain chain)
7778
[TestCaseSource(nameof(chainIdCases))]
7879
public async Task TestRuntimeStatus(Chain chain)
7980
{
80-
if (ChainIsInactive(chain)) return;
81+
if (!chain.IsActive()) return;
8182
RuntimeStatus result = null;
8283
try
8384
{

Assets/SequenceSDK/WaaS/Tests/EndToEndTestHarness.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ public EndToEndTestHarness(SequenceLogin login = null)
1616
{
1717
if (login == null)
1818
{
19-
_login = new SequenceLogin();
19+
_login = SequenceLogin.GetInstance();
2020
}
2121
else
2222
{
2323
_login = login;
2424
}
25+
_login.ResetLoginAfterTest();
2526
}
2627

2728
public async Task Login(Action<SequenceWallet> OnLogin, ILogin.OnLoginFailedHandler OnFailedLogin)

Assets/SequenceSDK/WaaS/Tests/InitiateEmailSignInUnitTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ public class InitiateEmailSignInUnitTests
88
{
99
private string _email = "[email protected]";
1010

11+
[TearDown]
12+
public void Cleanup()
13+
{
14+
SequenceLogin.GetInstance().ResetLoginAfterTest();
15+
}
16+
1117
[Test]
1218
public void TestLogin_Success()
1319
{

Assets/SequenceSDK/WaaS/Tests/WaaSSessionManagementTests.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -172,19 +172,9 @@ public async Task TestListAccounts()
172172
public async Task GetIdTokenTest()
173173
{
174174
var tcs = new TaskCompletionSource<bool>();
175+
EndToEndTestHarness testHarness = new EndToEndTestHarness();
175176

176-
177-
var login = SequenceLogin.GetInstance();
178-
179-
login.OnLoginFailed += (error, method, email, methods) =>
180-
{
181-
string errorMessage = "Login failed: " + error;
182-
tcs.TrySetException(new Exception(errorMessage));
183-
184-
Assert.Fail(errorMessage);
185-
};
186-
187-
SequenceWallet.OnWalletCreated += async wallet =>
177+
testHarness.Login(async wallet =>
188178
{
189179
try
190180
{
@@ -207,8 +197,13 @@ public async Task GetIdTokenTest()
207197
{
208198
tcs.TrySetException(e);
209199
}
210-
};
211-
login.GuestLogin();
200+
}, (error, method, email, methods) =>
201+
{
202+
string errorMessage = "Login failed: " + error;
203+
tcs.TrySetException(new Exception(errorMessage));
204+
205+
Assert.Fail(errorMessage);
206+
});
212207
await tcs.Task;
213208
}
214209
}

Packages/Sequence-Unity/Sequence/SequenceSDK/EmbeddedWallet/SequenceLogin.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ public void ResetSessionId()
114114
_emailConnector = new EmailConnector(_sessionId, _sessionWallet, _connector, _validator);
115115
}
116116

117+
/// <summary>
118+
/// Use this to reset the authenticator, validator, and other dependancies to new instances. Useful for when you're testing and using mock implementations
119+
/// </summary>
120+
public void ResetLoginAfterTest()
121+
{
122+
_connector = this;
123+
SetupAuthenticator();
124+
}
125+
117126
public void SetupAuthenticator(IValidator validator = null, IAuthenticator authenticator = null)
118127
{
119128
ConfigJwt configJwt = SequenceConfig.GetConfigJwt();

Packages/Sequence-Unity/Sequence/SequenceSDK/Ethereum/ABI/ABI.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public static string Pack(string method, params object[] parameters)
5656

5757
public static ABIType GetTypeFromEvmName(string typeName)
5858
{
59+
if (IsFixedArray(typeName))
60+
{
61+
return ABIType.FIXEDARRAY;
62+
}
63+
5964
if (typeName.EndsWith("[]"))
6065
{
6166
return ABIType.DYNAMICARRAY;
@@ -66,12 +71,6 @@ public static ABIType GetTypeFromEvmName(string typeName)
6671
return ABIType.TUPLE;
6772
}
6873

69-
if (IsFixedArray(typeName))
70-
{
71-
return ABIType.FIXEDARRAY;
72-
}
73-
74-
7574
if (typeName.StartsWith("bytes"))
7675
{
7776
if (typeName == "bytes")
@@ -112,6 +111,11 @@ public static ABIType GetTypeFromEvmName(string typeName)
112111
/// <returns></returns>
113112
private static bool IsFixedArray(string value)
114113
{
114+
if (value.StartsWith('('))
115+
{
116+
return false; // this is a tuple
117+
}
118+
115119
int start = value.IndexOf('[');
116120
if (start <= 0)
117121
{
@@ -225,9 +229,9 @@ private static string[] ExtractTypes(JArray array)
225229
}
226230
}
227231
tupleType.Append(")");
228-
if (result[i].EndsWith("[]"))
232+
if (result[i].Contains("["))
229233
{
230-
tupleType.Append("[]");
234+
tupleType.Append(result[i].Substring(result[i].IndexOf('['), result[i].Length - result[i].IndexOf('[')));
231235
}
232236
result[i] = tupleType.ToString();
233237
}
@@ -414,13 +418,23 @@ public static T Decode<T>(string value, string evmType)
414418
{
415419
return (T)(object)value.HexStringToInt();
416420
}
421+
422+
if (typeof(T) == typeof(long))
423+
{
424+
return (T)(object)(long)value.HexStringToBigInteger();
425+
}
417426

418427
if (typeof(T) == typeof(uint))
419428
{
420429
return (T)(object)(uint)value.HexStringToBigInteger();
421430
}
431+
432+
if (typeof(T) == typeof(ulong))
433+
{
434+
return (T)(object)(ulong)value.HexStringToBigInteger();
435+
}
422436

423-
ThrowDecodeException<T>(evmType, typeof(BigInteger).ToString(), typeof(int).ToString(), typeof(uint).ToString());
437+
ThrowDecodeException<T>(evmType, typeof(BigInteger).ToString(), typeof(int).ToString(), typeof(long).ToString(), typeof(uint).ToString(), typeof(ulong).ToString());
424438
break;
425439
case ABIType.BOOLEAN:
426440
if (typeof(T) != typeof(bool))

Packages/Sequence-Unity/Sequence/SequenceSDK/Ethereum/ABI/SequenceCoder.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ public static string HexStringToHumanReadable(string hexString)
212212
byte[] bytes = HexStringToByteArray(hexString);
213213
string result = Encoding.UTF8.GetString(bytes);
214214
string cleaned = RemoveControlCharactersExceptNewline(result); // Unity's encoding/decoding is a bit wonky and adds a bunch of \0 (null terminators) to the beginning and end of the string
215+
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
216+
cleaned = cleaned.Replace("\n", "\r\n");
217+
#endif
215218
return cleaned;
216219
}
217220

Packages/Sequence-Unity/Sequence/SequenceSDK/Ethereum/ChainId.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public static string GetChainId(this Chain chain)
2424
return ChainDictionaries.ChainIdOf[chain];
2525
}
2626

27+
public static bool IsActive(this Chain chain)
28+
{
29+
return !(chain == Chain.None || chain == Chain.AstarZKEvm || chain == Chain.TestnetAstarZKyoto);
30+
}
2731
}
2832

2933
public static class NativeTokenAddress

0 commit comments

Comments
 (0)