Skip to content

Commit 1651e25

Browse files
authored
Nat encode improvement (#87)
- Treat parsable big number string as a covariant of the `Nat` class. - Allow parsable big number string for `lebEncode` and `slebEncode`.
1 parent 360e74c commit 1651e25

File tree

9 files changed

+29
-18
lines changed

9 files changed

+29
-18
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ that can be found in the LICENSE file. -->
44

55
# Changelog
66

7+
## 1.0.0-dev.31
8+
9+
- Treat parsable big number string as a covariant of the `Nat` class.
10+
- Allow parsable big number string for `lebEncode` and `slebEncode`.
11+
712
## 1.0.0-dev.30
813

914
- Make pod shell respect configured profile on Darwin.

packages/agent_dart/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: agent_dart
2-
version: 1.0.0-dev.30
2+
version: 1.0.0-dev.31
33

44
description: |
55
An agent library built for Internet Computer,

packages/agent_dart_base/lib/agent/utils/leb128.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ List<T> safeRead<T>(BufferPipe<T> pipe, int ref) {
2222
/// nearest integer.
2323
/// @param value The number to encode.
2424
Uint8List lebEncode(dynamic value) {
25-
BigInt bn = value is BigInt ? value : BigInt.from(value);
25+
BigInt bn = switch (value) {
26+
BigInt() => value,
27+
num() => BigInt.from(value),
28+
String() => BigInt.parse(value),
29+
_ => throw ArgumentError('Invalid big number: $value', 'lebEncode'),
30+
};
2631
if (bn < BigInt.zero) {
2732
throw StateError('Cannot leb-encode negative values.');
2833
}
@@ -58,9 +63,13 @@ BigInt lebDecode<T>(BufferPipe<T> pipe) {
5863
/// Encode a number (or bigint) into a Buffer, with support for negative numbers.
5964
/// The number will be floored to the nearest integer.
6065
/// @param value The number to encode.
61-
Uint8List slebEncode(Comparable value) {
62-
BigInt bn = value is BigInt ? value : BigInt.from(value as num);
63-
66+
Uint8List slebEncode(dynamic value) {
67+
BigInt bn = switch (value) {
68+
BigInt() => value,
69+
num() => BigInt.from(value),
70+
String() => BigInt.parse(value),
71+
_ => throw ArgumentError('Invalid big number: $value', 'slebEncode'),
72+
};
6473
final isNeg = bn < BigInt.zero;
6574
if (isNeg) {
6675
bn = -bn - BigInt.one;

packages/agent_dart_base/lib/candid/idl.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,9 @@ class NatClass extends PrimitiveType {
528528

529529
@override
530530
bool covariant(x) {
531-
return (x is BigInt && x >= BigInt.zero) || (x is int && x >= 0);
531+
return (x is BigInt && x >= BigInt.zero) ||
532+
(x is int && x >= 0) ||
533+
(x is String && BigInt.parse(x) >= BigInt.zero);
532534
}
533535

534536
@override

packages/agent_dart_base/lib/wallet/signer.dart

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,6 @@ class ICPSigner extends BaseSigner<ICPAccount, ConstructionPayloadsResponse,
263263
curveType: curveType,
264264
))
265265
..setSourceType(SourceType.base);
266-
default:
267-
return (await ICPSigner.fromPhrase(
268-
phrase,
269-
index: hardened,
270-
icPath: icDerivationPath,
271-
curveType: curveType,
272-
))
273-
..setSourceType(SourceType.ii);
274266
}
275267
}
276268

packages/agent_dart_base/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: agent_dart_base
2-
version: 1.0.0-dev.30
2+
version: 1.0.0-dev.31
33

44
description: The Dart plugin that bridges Rust implementation for agent_dart.
55
repository: https://github.com/AstroxNetwork/agent_dart

packages/agent_dart_base/test/agent/utils/leb128.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ void leb128Test() {
2424
lebEncode(BigInt.from(60000000000000000)).toHex(),
2525
'808098f4e9b5ca6a',
2626
);
27+
expect(lebEncode('1').toHex(), '01');
2728

2829
expect(lebDecode(BufferPipe<int>(Uint8List.fromList([0]))), BigInt.zero);
2930
expect(lebDecode(BufferPipe<int>(Uint8List.fromList([1]))), BigInt.one);
@@ -59,6 +60,7 @@ void leb128Test() {
5960
slebEncode(BigInt.parse('60000000000000000')).toHex(),
6061
'808098f4e9b5caea00',
6162
);
63+
expect(slebEncode('1').toHex(), '01');
6264

6365
expect(
6466
slebDecode(BufferPipe<int>(Uint8List.fromList([0x7f]))),

packages/agent_dart_base/test/wallet/signer.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ void main() {
4141
stopwatch.stop();
4242
final acc22TimePeriod = stopwatch.elapsed;
4343

44-
expect(acc21TimePeriod < allCurvesDuration, true);
45-
expect(acc22TimePeriod < allCurvesDuration, true);
44+
print([allCurvesDuration, acc21TimePeriod, acc22TimePeriod]);
45+
expect(acc21TimePeriod <= allCurvesDuration, true);
46+
expect(acc22TimePeriod <= allCurvesDuration, true);
4647
expect(acc2.account.identity != null, true);
4748
expect(acc2.account.ecIdentity != null, true);
4849
expect(acc21.account.ecIdentity, equals(null));

packages/agent_dart_ffi/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: agent_dart_ffi
2-
version: 1.0.0-dev.30
2+
version: 1.0.0-dev.31
33

44
description: The FFI plugin that bridges Rust implementation for agent_dart.
55
repository: https://github.com/AstroxNetwork/agent_dart

0 commit comments

Comments
 (0)