From 14f8781685c006c09e565a53294ecae34538d180 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Fri, 11 Oct 2024 13:58:28 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Correct=20agent=20`cal?= =?UTF-8?q?l`=20method=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/agent_dart_base/lib/agent/actor.dart | 5 +++-- packages/agent_dart_base/lib/agent/agent/api.dart | 2 +- .../lib/agent/agent/http/index.dart | 2 +- .../agent_dart_base/lib/agent/agent/proxy.dart | 8 ++++---- .../agent_dart_base/test/agent/http/http.dart | 15 ++++++--------- 5 files changed, 15 insertions(+), 17 deletions(-) diff --git a/packages/agent_dart_base/lib/agent/actor.dart b/packages/agent_dart_base/lib/agent/actor.dart index 0e60e37b..38c3f774 100644 --- a/packages/agent_dart_base/lib/agent/actor.dart +++ b/packages/agent_dart_base/lib/agent/actor.dart @@ -411,8 +411,9 @@ ActorMethod _createActorMethod(Actor actor, String methodName, Func func) { final ecid = effectiveCanisterId != null ? Principal.from(effectiveCanisterId) : cid; - // final { requestId, response } = - final result = await agent!.call( + final callSync = actor.metadata.config?.callSync ?? newOptions.callSync; + + final result = await agent!.callRequest( cid, CallOptions( methodName: methodName, diff --git a/packages/agent_dart_base/lib/agent/agent/api.dart b/packages/agent_dart_base/lib/agent/agent/api.dart index 439502db..06609a5b 100644 --- a/packages/agent_dart_base/lib/agent/agent/api.dart +++ b/packages/agent_dart_base/lib/agent/agent/api.dart @@ -157,7 +157,7 @@ abstract class Agent { Identity? identity, ); - Future call( + Future callRequest( Principal canisterId, CallOptions fields, Identity? identity, diff --git a/packages/agent_dart_base/lib/agent/agent/http/index.dart b/packages/agent_dart_base/lib/agent/agent/http/index.dart index 9fb0661e..c9f4e83f 100644 --- a/packages/agent_dart_base/lib/agent/agent/http/index.dart +++ b/packages/agent_dart_base/lib/agent/agent/http/index.dart @@ -210,7 +210,7 @@ class HttpAgent implements Agent { } @override - Future call( + Future callRequest( Principal canisterId, CallOptions fields, Identity? identity, diff --git a/packages/agent_dart_base/lib/agent/agent/proxy.dart b/packages/agent_dart_base/lib/agent/agent/proxy.dart index d81bd361..8a82a3a0 100644 --- a/packages/agent_dart_base/lib/agent/agent/proxy.dart +++ b/packages/agent_dart_base/lib/agent/agent/proxy.dart @@ -280,7 +280,7 @@ class ProxyStubAgent { final void Function(ProxyMessage msg) _frontend; final Agent _agent; - void onmessage(ProxyMessage msg) { + void onMessage(ProxyMessage msg) { switch (msg.type) { case ProxyMessageKind.getPrincipal: _agent.getPrincipal().then((response) { @@ -305,7 +305,7 @@ class ProxyStubAgent { }); break; case ProxyMessageKind.call: - _agent.call(msg.args?[0], msg.args?[1], msg.args?[2]).then((response) { + _agent.callRequest(msg.args?[0], msg.args?[1], msg.args?[2]).then((response) { _frontend( ProxyMessageCallResponse.fromJson({ 'id': msg.id, @@ -356,7 +356,7 @@ class ProxyAgent implements Agent { @override BinaryBlob? rootKey; - void onmessage(ProxyMessage msg) { + void onMessage(ProxyMessage msg) { final id = msg.id; final maybePromise = _pendingCalls[id]; @@ -417,7 +417,7 @@ class ProxyAgent implements Agent { } @override - Future call( + Future callRequest( Principal canisterId, CallOptions fields, Identity? identity, diff --git a/packages/agent_dart_base/test/agent/http/http.dart b/packages/agent_dart_base/test/agent/http/http.dart index 3bdc2b38..d392a58f 100644 --- a/packages/agent_dart_base/test/agent/http/http.dart +++ b/packages/agent_dart_base/test/agent/http/http.dart @@ -42,14 +42,12 @@ void httpTest() { }); }); - // ignore: unused_local_variable - final res = await agent.call( + final res = await agent.callRequest( canisterId, CallOptions(arg: arg, methodName: methodName), null, ); - - // print((res as CallResponseBody).toJson()); + print(res.toJson()); final mockPartialRequest = { 'request_type': SubmitRequestType.call, @@ -64,16 +62,15 @@ void httpTest() { }; final mockPartialsRequestId = requestIdOf(mockPartialRequest); - final expectedRequest = { 'content': mockPartialRequest, }; - - final expectedRequestId = - requestIdOf(expectedRequest['content'] as Map); - + final expectedRequestId = requestIdOf( + expectedRequest['content'] as Map, + ); expect(expectedRequestId, mockPartialsRequestId); }); }); + group('description 2 ', () {}); } From 72b7ae941eae8784c77d70eff697d42ffb6ac79a Mon Sep 17 00:00:00 2001 From: Alex Li Date: Fri, 11 Oct 2024 14:00:21 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Narrow=20`Certificate`?= =?UTF-8?q?=20fields?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../agent_dart_base/lib/agent/agent/http/index.dart | 4 ++-- packages/agent_dart_base/lib/agent/certificate.dart | 10 ++++++---- .../agent_dart_base/lib/agent/polling/polling.dart | 3 ++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/agent_dart_base/lib/agent/agent/http/index.dart b/packages/agent_dart_base/lib/agent/agent/http/index.dart index c9f4e83f..ec633b66 100644 --- a/packages/agent_dart_base/lib/agent/agent/http/index.dart +++ b/packages/agent_dart_base/lib/agent/agent/http/index.dart @@ -388,10 +388,10 @@ class HttpAgent implements Agent { } final buffer = response['arrayBuffer'] as Uint8List; - + final decoded = cbor.cborDecode(buffer); return ReadStateResponseResult( certificate: blobFromBuffer( - (cbor.cborDecode(buffer)['certificate'] as Uint8Buffer).buffer, + (decoded['certificate'] as Uint8Buffer).buffer, ), ); } diff --git a/packages/agent_dart_base/lib/agent/certificate.dart b/packages/agent_dart_base/lib/agent/certificate.dart index fdc247c4..330bb17d 100644 --- a/packages/agent_dart_base/lib/agent/certificate.dart +++ b/packages/agent_dart_base/lib/agent/certificate.dart @@ -52,7 +52,9 @@ class Cert { factory Cert.fromJson(Map json) { return Cert( delegation: json['delegation'] != null - ? CertDelegation.fromJson(Map.from(json['delegation'])) + ? CertDelegation.fromJson( + Map.from(json['delegation']), + ) : null, signature: json['signature'] != null ? (json['signature'] as Uint8Buffer).buffer.asUint8List() @@ -123,9 +125,9 @@ class CertDelegation extends ReadStateResponse { class Certificate { Certificate( - ReadStateResponse response, + BinaryBlob certificate, this._agent, - ) : cert = Cert.fromJson(cborDecode(response.certificate)); + ) : cert = Cert.fromJson(cborDecode(certificate)); final Agent _agent; final Cert cert; @@ -172,7 +174,7 @@ class Certificate { } return Future.value(_rootKey); } - final Certificate cert = Certificate(d, _agent); + final Certificate cert = Certificate(d.certificate, _agent); if (!(await cert.verify())) { throw StateError('Fail to verify certificate.'); } diff --git a/packages/agent_dart_base/lib/agent/polling/polling.dart b/packages/agent_dart_base/lib/agent/polling/polling.dart index a63ab8c5..dfaa2d94 100644 --- a/packages/agent_dart_base/lib/agent/polling/polling.dart +++ b/packages/agent_dart_base/lib/agent/polling/polling.dart @@ -28,11 +28,12 @@ Future pollForResponse( ReadStateOptions(paths: [path]), null, ); - final cert = Certificate(state, agent); + final cert = Certificate(state.certificate, agent); final verified = await cert.verify(); if (!verified) { throw StateError('Fail to verify certificate.'); } + final maybeBuf = cert.lookup([...path, blobFromText('status').buffer]); final RequestStatusResponseStatus status; if (maybeBuf == null) { From c9c50708a72de913917f6726cd7a7e1a032f184b Mon Sep 17 00:00:00 2001 From: Alex Li Date: Fri, 11 Oct 2024 14:02:03 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Allows=20`pollForRespo?= =?UTF-8?q?nse`=20accept=20override=20certificate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/agent/polling/polling.dart | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/agent_dart_base/lib/agent/polling/polling.dart b/packages/agent_dart_base/lib/agent/polling/polling.dart index dfaa2d94..45ce9fbb 100644 --- a/packages/agent_dart_base/lib/agent/polling/polling.dart +++ b/packages/agent_dart_base/lib/agent/polling/polling.dart @@ -14,21 +14,28 @@ Future pollForResponse( Principal canisterId, RequestId requestId, PollStrategy strategy, - String method, -) async { + String method, { + BinaryBlob? overrideCertificate, +}) async { final Principal? caller; if (agent is HttpAgent) { caller = agent.identity?.getPrincipal(); } else { caller = null; } + final path = [blobFromText('request_status'), requestId]; - final state = await agent.readState( - canisterId, - ReadStateOptions(paths: [path]), - null, - ); - final cert = Certificate(state.certificate, agent); + final Certificate cert; + if (overrideCertificate != null) { + cert = Certificate(overrideCertificate, agent); + } else { + final state = await agent.readState( + canisterId, + ReadStateOptions(paths: [path]), + null, + ); + cert = Certificate(state.certificate, agent); + } final verified = await cert.verify(); if (!verified) { throw StateError('Fail to verify certificate.'); @@ -51,6 +58,7 @@ Future pollForResponse( case RequestStatusResponseStatus.processing: // Execute the polling strategy, then retry. await strategy(canisterId, requestId, status); + // Passing the override certificate will cause infinite stacks. return pollForResponse(agent, canisterId, requestId, strategy, method); case RequestStatusResponseStatus.rejected: final rejectCode = cert.lookup( From 3122f00171045d8655d6dcdd3bad90ab0e029433 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Fri, 11 Oct 2024 14:33:25 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=F0=9F=9A=80=20Implement=20v3=20call=20(`ca?= =?UTF-8?q?llSync`)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/agent_dart_base/lib/agent/actor.dart | 25 ++++++++++++++ .../agent_dart_base/lib/agent/agent/api.dart | 4 +++ .../lib/agent/agent/http/index.dart | 34 +++++++++++++++---- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/packages/agent_dart_base/lib/agent/actor.dart b/packages/agent_dart_base/lib/agent/actor.dart index 38c3f774..e072626b 100644 --- a/packages/agent_dart_base/lib/agent/actor.dart +++ b/packages/agent_dart_base/lib/agent/actor.dart @@ -1,10 +1,14 @@ import 'dart:convert'; import 'dart:typed_data'; +import 'package:typed_data/typed_data.dart'; + import '../candid/idl.dart'; import '../principal/principal.dart'; import 'agent/api.dart'; +import 'agent/http/types.dart'; import 'canisters/management.dart'; +import 'cbor.dart' as cbor; import 'errors.dart'; import 'polling/polling.dart'; import 'request_id.dart'; @@ -72,6 +76,7 @@ class CallConfig { this.pollingStrategyFactory, this.canisterId, this.effectiveCanisterId, + this.callSync = true, }); factory CallConfig.fromJson(Map map) { @@ -80,6 +85,7 @@ class CallConfig { pollingStrategyFactory: map['pollingStrategyFactory'], canisterId: map['canisterId'], effectiveCanisterId: map['effectiveCanisterId'], + callSync: map['callSync'] ?? true, ); } @@ -97,12 +103,16 @@ class CallConfig { /// The effective canister ID. This should almost always be ignored. final Principal? effectiveCanisterId; + /// Whether to call the endpoint synchronously. + final bool callSync; + Map toJson() { return { 'agent': agent, 'pollingStrategyFactory': pollingStrategyFactory, 'canisterId': canisterId, 'effectiveCanisterId': effectiveCanisterId, + 'callSync': callSync, }; } } @@ -114,6 +124,7 @@ class ActorConfig extends CallConfig { super.pollingStrategyFactory, super.canisterId, super.effectiveCanisterId, + super.callSync, this.callTransform, this.queryTransform, }); @@ -126,6 +137,7 @@ class ActorConfig extends CallConfig { pollingStrategyFactory: map['pollingStrategyFactory'], canisterId: map['canisterId'], effectiveCanisterId: map['effectiveCanisterId'], + callSync: map['callSync'] ?? true, ); } @@ -419,6 +431,7 @@ ActorMethod _createActorMethod(Actor actor, String methodName, Func func) { methodName: methodName, arg: arg, effectiveCanisterId: ecid, + callSync: callSync, ), null, ); @@ -429,6 +442,17 @@ ActorMethod _createActorMethod(Actor actor, String methodName, Func func) { throw UpdateCallRejectedError(cid, methodName, result, requestId); } + BinaryBlob? certificate; + // Fall back to polling if we receive an "Accepted" response code, + // otherwise decode the certificate instantly. + if (result is CallResponseBody && result.response?.status != 202) { + final buffer = (result.response as HttpResponseBody).arrayBuffer!; + final decoded = cbor.cborDecode(buffer); + certificate = blobFromBuffer( + (decoded['certificate'] as Uint8Buffer).buffer, + ); + } + final pollStrategy = pollingStrategyFactory(); final responseBytes = await pollForResponse( agent, @@ -436,6 +460,7 @@ ActorMethod _createActorMethod(Actor actor, String methodName, Func func) { requestId, pollStrategy, methodName, + overrideCertificate: certificate, ); if (responseBytes.isNotEmpty) { diff --git a/packages/agent_dart_base/lib/agent/agent/api.dart b/packages/agent_dart_base/lib/agent/agent/api.dart index 06609a5b..b01d2eec 100644 --- a/packages/agent_dart_base/lib/agent/agent/api.dart +++ b/packages/agent_dart_base/lib/agent/agent/api.dart @@ -98,6 +98,7 @@ class CallOptions { required this.methodName, required this.arg, this.effectiveCanisterId, + this.callSync = true, }); /// The method name to call. @@ -109,6 +110,9 @@ class CallOptions { /// An effective canister ID, used for routing. This should only be mentioned /// if it's different from the canister ID. final Principal? effectiveCanisterId; + + /// Whether to call the endpoint synchronously. + final bool callSync; } @immutable diff --git a/packages/agent_dart_base/lib/agent/agent/http/index.dart b/packages/agent_dart_base/lib/agent/agent/http/index.dart index ec633b66..11b3b6ec 100644 --- a/packages/agent_dart_base/lib/agent/agent/http/index.dart +++ b/packages/agent_dart_base/lib/agent/agent/http/index.dart @@ -220,6 +220,7 @@ class HttpAgent implements Agent { final ecid = fields.effectiveCanisterId != null ? Principal.from(fields.effectiveCanisterId) : canister; + final callSync = fields.callSync; final sender = id != null ? id.getPrincipal() : Principal.anonymous(); final CallRequest submit = CallRequest( @@ -241,17 +242,38 @@ class HttpAgent implements Agent { body: submit, ); final transformedRequest = await _transform(rsRequest); - final newTransformed = await id!.transformRequest(transformedRequest); final body = cbor.cborEncode(newTransformed['body']); - final response = await withRetry( - () => _fetch!( - endpoint: '/api/v2/canister/${ecid.toText()}/call', + + Future> callV3() { + return _fetch!( + endpoint: '/api/v3/canister/${ecid.toText()}/call', method: FetchMethod.post, headers: newTransformed['request']['headers'], body: body, - ), - ); + ); + } + + Future> callV2() { + return withRetry( + () => _fetch!( + endpoint: '/api/v2/canister/${ecid.toText()}/call', + method: FetchMethod.post, + headers: newTransformed['request']['headers'], + body: body, + ), + ); + } + + Map response; + if (callSync) { + response = await callV3(); + if (response['statusCode'] == 404) { + response = await callV2(); + } + } else { + response = await callV2(); + } final requestId = requestIdOf(submit.toJson()); if (!(response['ok'] as bool)) { From 9f24519610f7e93cbd658638a6c100d8caad3059 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Fri, 11 Oct 2024 14:34:57 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=F0=9F=A7=AA=20Adds=20actor=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../agent_dart_base/test/agent/actor.dart | 33 ++++++++++++++++++- .../agent_dart_base/test/agent/agent.dart | 6 +++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/packages/agent_dart_base/test/agent/actor.dart b/packages/agent_dart_base/test/agent/actor.dart index cfe4b73f..c046205b 100644 --- a/packages/agent_dart_base/test/agent/actor.dart +++ b/packages/agent_dart_base/test/agent/actor.dart @@ -1,7 +1,38 @@ +import 'package:agent_dart_base/agent_dart_base.dart'; +import 'package:test/test.dart'; + void main() { actorTest(); } void actorTest() { - /// skip, see https://github.com/dfinity/agent-js/blob/main/packages/agent/src/actor.test.ts + test('actor', () async { + final agent = HttpAgent( + defaultHost: 'icp-api.io', + defaultPort: 443, + options: const HttpAgentOptions(identity: AnonymousIdentity()), + ); + final idl = IDL.Service({ + 'create_challenge': IDL.Func( + [], + [ + IDL.Record({ + 'png_base64': IDL.Text, + 'challenge_key': IDL.Text, + }), + ], + [], + ), + }); + final actor = CanisterActor( + ActorConfig( + canisterId: Principal.fromText('rdmx6-jaaaa-aaaaa-aaadq-cai'), + agent: agent, + ), + idl, + ); + final result = await actor.getFunc('create_challenge')!.call([]); + expect(result, isA()); + expect(result['challenge_key'], isA()); + }); } diff --git a/packages/agent_dart_base/test/agent/agent.dart b/packages/agent_dart_base/test/agent/agent.dart index 70a9bf4b..f46083b0 100644 --- a/packages/agent_dart_base/test/agent/agent.dart +++ b/packages/agent_dart_base/test/agent/agent.dart @@ -1,3 +1,5 @@ +import '../test_utils.dart'; +import 'actor.dart' as actor; import 'cbor.dart' as cbor; import 'certificate.dart' as certificate; import 'request_id.dart' as request_id; @@ -6,10 +8,12 @@ import 'utils/hash.dart' as hash; import 'utils/leb128.dart' as leb128; void main() { + ffiInit(); + actor.main(); + cbor.main(); certificate.main(); request_id.main(); bls.main(); hash.main(); leb128.main(); - cbor.main(); } From 6d53567657a74c5afcb6d4308e2450e5205f46bf Mon Sep 17 00:00:00 2001 From: Alex Li Date: Fri, 11 Oct 2024 14:35:23 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E2=9C=85=20Integrate=20tests=20as=20much?= =?UTF-8?q?=20as=20possible?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/agent_dart_base_test.dart | 8 +- .../test/authentication/authentication.dart | 3 + packages/agent_dart_base/test/candid/idl.dart | 2 +- .../test/identity/delegation.dart | 25 +++--- .../test/identity/identity.dart | 6 ++ .../agent_dart_base/test/identity/p256.dart | 2 - .../test/identity/schnorr.dart | 3 - .../agent_dart_base/test/principal/index.dart | 9 ++- packages/agent_dart_base/test/test_utils.dart | 77 ++++--------------- .../test/utils/utils_test.dart | 2 + .../agent_dart_base/test/wallet/index.dart | 11 +++ 11 files changed, 59 insertions(+), 89 deletions(-) create mode 100644 packages/agent_dart_base/test/wallet/index.dart diff --git a/packages/agent_dart_base/test/agent_dart_base_test.dart b/packages/agent_dart_base/test/agent_dart_base_test.dart index f4bec40d..45015b85 100644 --- a/packages/agent_dart_base/test/agent_dart_base_test.dart +++ b/packages/agent_dart_base/test/agent_dart_base_test.dart @@ -1,12 +1,11 @@ +import './test_utils.dart'; import 'agent/agent.dart' as agent; import 'authentication/authentication.dart' as auth; import 'candid/idl.dart' as candid; import 'identity/identity.dart' as identity; import 'principal/index.dart' as principal; -import 'test_utils.dart'; import 'utils/utils_test.dart' as utils; -import 'wallet/pem.dart' as pem; -import 'wallet/signer.dart' as signer; +import 'wallet/index.dart' as wallet; void main() async { ffiInit(); @@ -16,6 +15,5 @@ void main() async { identity.main(); principal.main(); utils.main(); - signer.main(); - pem.main(); + wallet.main(); } diff --git a/packages/agent_dart_base/test/authentication/authentication.dart b/packages/agent_dart_base/test/authentication/authentication.dart index 0dbdffc3..915e32ec 100644 --- a/packages/agent_dart_base/test/authentication/authentication.dart +++ b/packages/agent_dart_base/test/authentication/authentication.dart @@ -3,6 +3,8 @@ import 'dart:typed_data'; import 'package:agent_dart_base/agent_dart_base.dart'; import 'package:test/test.dart'; +import '../test_utils.dart'; + Future createIdentity(int seed) async { final seed1 = List.filled(32, 0); seed1[0] = seed; @@ -10,6 +12,7 @@ Future createIdentity(int seed) async { } void main() { + ffiInit(); authenticationTest(); } diff --git a/packages/agent_dart_base/test/candid/idl.dart b/packages/agent_dart_base/test/candid/idl.dart index c3f3a5a9..e37245a5 100644 --- a/packages/agent_dart_base/test/candid/idl.dart +++ b/packages/agent_dart_base/test/candid/idl.dart @@ -4,9 +4,9 @@ import 'package:agent_dart_base/agent_dart_base.dart'; import 'package:test/test.dart'; import '../test_utils.dart'; -// ignore: library_prefixes void main() { + ffiInit(); idlTest(); } diff --git a/packages/agent_dart_base/test/identity/delegation.dart b/packages/agent_dart_base/test/identity/delegation.dart index ac2301c8..c9e31a82 100644 --- a/packages/agent_dart_base/test/identity/delegation.dart +++ b/packages/agent_dart_base/test/identity/delegation.dart @@ -4,23 +4,20 @@ import 'dart:typed_data'; import 'package:agent_dart_base/agent_dart_base.dart'; import 'package:test/test.dart'; -Future createIdentity(int seed) { - final s = Uint8List.fromList([seed, ...List.filled(31, 0)]); - return Ed25519KeyIdentity.generate(s); -} - -// BinaryBlob h(String text) { -// return blobFromHex(text.codeUnits.); -// } void main() { delegationTest(); } +Future _createIdentity(int seed) { + final s = Uint8List.fromList([seed, ...List.filled(31, 0)]); + return Ed25519KeyIdentity.generate(s); +} + void delegationTest() { test('delegation signs with proper keys (3)', () async { - final root = await createIdentity(2); - final middle = await createIdentity(1); - final bottom = await createIdentity(0); + final root = await _createIdentity(2); + final middle = await _createIdentity(1); + final bottom = await _createIdentity(0); final rootToMiddle = await DelegationChain.create( root, @@ -66,9 +63,9 @@ void delegationTest() { expect(middleToBottom.toJson(), result); }); test('DelegationChain can be serialized to and from JSON', () async { - final root = await createIdentity(2); - final middle = await createIdentity(1); - final bottom = await createIdentity(0); + final root = await _createIdentity(2); + final middle = await _createIdentity(1); + final bottom = await _createIdentity(0); final rootToMiddle = await DelegationChain.create( root, diff --git a/packages/agent_dart_base/test/identity/identity.dart b/packages/agent_dart_base/test/identity/identity.dart index 2209e69a..1560ea1a 100644 --- a/packages/agent_dart_base/test/identity/identity.dart +++ b/packages/agent_dart_base/test/identity/identity.dart @@ -1,9 +1,15 @@ +import '../test_utils.dart'; import 'delegation.dart' as delegation; import 'ed25519.dart' as ed25519; +import 'p256.dart' as p256; +import 'schnorr.dart' as schnorr; import 'secp256k1.dart' as secp256k1; void main() { + ffiInit(); delegation.main(); ed25519.main(); + p256.main(); + schnorr.main(); secp256k1.main(); } diff --git a/packages/agent_dart_base/test/identity/p256.dart b/packages/agent_dart_base/test/identity/p256.dart index caf8e54c..86a6cc0f 100644 --- a/packages/agent_dart_base/test/identity/p256.dart +++ b/packages/agent_dart_base/test/identity/p256.dart @@ -3,8 +3,6 @@ import 'package:test/test.dart'; import '../test_utils.dart'; void main() { - ffiInit(); - // matchFFI(); p256Test(); } diff --git a/packages/agent_dart_base/test/identity/schnorr.dart b/packages/agent_dart_base/test/identity/schnorr.dart index 609d33f6..11bb3d65 100644 --- a/packages/agent_dart_base/test/identity/schnorr.dart +++ b/packages/agent_dart_base/test/identity/schnorr.dart @@ -2,11 +2,8 @@ import 'dart:typed_data'; import 'package:agent_dart_base/agent_dart_base.dart'; import 'package:test/test.dart'; -import '../test_utils.dart'; void main() { - ffiInit(); - // matchFFI(); schnorrTest(); } diff --git a/packages/agent_dart_base/test/principal/index.dart b/packages/agent_dart_base/test/principal/index.dart index d79b3f82..bd6d80a8 100644 --- a/packages/agent_dart_base/test/principal/index.dart +++ b/packages/agent_dart_base/test/principal/index.dart @@ -1,10 +1,11 @@ +import '../test_utils.dart'; import 'principal.dart' as principal; import 'utils/base32.dart' as base32; -// ignore: library_prefixes -import 'utils/get_crc.dart' as getCrc; +import 'utils/get_crc.dart' as get_crc; void main() { - base32.main(); - getCrc.main(); + ffiInit(); principal.main(); + base32.main(); + get_crc.main(); } diff --git a/packages/agent_dart_base/test/test_utils.dart b/packages/agent_dart_base/test/test_utils.dart index 3dbd422b..40d3cf45 100644 --- a/packages/agent_dart_base/test/test_utils.dart +++ b/packages/agent_dart_base/test/test_utils.dart @@ -3,6 +3,23 @@ import 'dart:ffi'; import 'package:agent_dart_ffi/agent_dart_ffi.dart'; import 'package:test/test.dart'; +Future ffiInit() async { + // ignore: invalid_use_of_internal_member + if (AgentDart.instance.initialized) { + return; + } + final [os, arch] = Abi.current().toString().split('_'); + final libName = switch ((os, arch)) { + ('macos', _) || ('linux', 'arm64') => 'libagent_dart.dylib', + ('linux', _) => 'libagent_dart.so', + ('windows', _) => 'agent_dart.dll', + _ => throw UnsupportedError('$os $arch is not a supported platform.'), + }; + return AgentDart.init( + externalLibrary: ExternalLibrary.open('../../target/debug/$libName'), + ); +} + const isAssertionError = TypeMatcher(); Matcher assertionThrowsContains(String str) { @@ -25,63 +42,3 @@ class ErrorMessageMatcher extends TypeMatcher { bool matches(Object? item, Map matchState) => item is T && (_message == null || (item as dynamic).message == _message); } - -Future ffiInit() { - final [os, arch] = Abi.current().toString().split('_'); - final libName = switch ((os, arch)) { - ('macos', _) || ('linux', 'arm64') => 'libagent_dart.dylib', - ('linux', _) => 'libagent_dart.so', - ('windows', _) => 'agent_dart.dll', - _ => throw UnsupportedError('$os $arch is not a supported platform.'), - }; - return AgentDart.init( - externalLibrary: ExternalLibrary.open('../../target/debug/$libName'), - ); -} - -// void matchFFI() { -// final architech = Abi.current().toString(); -// final arr = architech.split('_'); -// final os = arr[0]; -// final arc = arr[1]; -// const dyLib = 'libagent_dart.dylib'; -// const dySo = 'libagent_dart.so'; -// const dyDll = 'agent_dart.dll'; -// final String lib; -// switch (os) { -// case 'macos': -// { -// if (arc == 'arm64') { -// lib = '../../platform-build/dylib/aarch64-apple-darwin/$dyLib'; -// break; -// } else { -// lib = '../../platform-build/dylib/x86_64-apple-darwin/$dyLib'; -// break; -// } -// } -// case 'linux': -// { -// if (arc == 'arm64') { -// lib = '../../platform-build/dylib/aarch64-unknown-linux-gnu/$dyLib'; -// break; -// } else { -// lib = '../../platform-build/dylib/x86_64-unknown-linux-gnu/$dySo'; -// break; -// } -// } -// case 'windows': -// { -// if (arc == 'arm64') { -// lib = '../../platform-build/dylib/aarch64-pc-windows-msvc/$dyDll'; -// break; -// } else { -// lib = '../../platform-build/dylib/x86_64-pc-windows-msvc/$dyDll'; -// break; -// } -// } -// default: -// throw 'Unsupported OS: $os'; -// } -// print(lib); -// AgentDartFFI.setImpl(AgentDartImpl(DynamicLibrary.open(lib))); -// } diff --git a/packages/agent_dart_base/test/utils/utils_test.dart b/packages/agent_dart_base/test/utils/utils_test.dart index 884b6c3b..67120fbe 100644 --- a/packages/agent_dart_base/test/utils/utils_test.dart +++ b/packages/agent_dart_base/test/utils/utils_test.dart @@ -1,9 +1,11 @@ import 'package:agent_dart_base/agent_dart_base.dart'; import 'package:test/test.dart'; +import '../test_utils.dart'; import 'is.dart' as is_test; void main() { + ffiInit(); is_test.main(); test('Hex string to u8', () { diff --git a/packages/agent_dart_base/test/wallet/index.dart b/packages/agent_dart_base/test/wallet/index.dart new file mode 100644 index 00000000..55668b5d --- /dev/null +++ b/packages/agent_dart_base/test/wallet/index.dart @@ -0,0 +1,11 @@ +import '../test_utils.dart'; +import 'aes.dart' as aes; +import 'pem.dart' as pem; +import 'signer.dart' as signer; + +void main() { + ffiInit(); + aes.main(); + pem.main(); + signer.main(); +} From 4935f5459af9d7f14e6d8bc7a5c9cf221b921d2a Mon Sep 17 00:00:00 2001 From: Alex Li Date: Tue, 15 Oct 2024 10:09:18 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=F0=9F=94=96=201.0.0-dev.28?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 6 ++++++ packages/agent_dart/pubspec.yaml | 2 +- packages/agent_dart_base/pubspec.yaml | 2 +- packages/agent_dart_base/test/identity/p256.dart | 1 - packages/agent_dart_ffi/pubspec.yaml | 2 +- 5 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b3708931..3a850c55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ that can be found in the LICENSE file. --> # Changelog +## 1.0.0-dev.28 + +- Implements v3 synchronized call API in agent and actor. + - `pollForResponse` can override the certificate result. + - v3 calls will return to v2 if 202/404 status is returned. + ## 1.0.0-dev.27 - Support `flutter_rust_bridge` 2.5. diff --git a/packages/agent_dart/pubspec.yaml b/packages/agent_dart/pubspec.yaml index 29a18606..b35afce4 100644 --- a/packages/agent_dart/pubspec.yaml +++ b/packages/agent_dart/pubspec.yaml @@ -1,5 +1,5 @@ name: agent_dart -version: 1.0.0-dev.27 +version: 1.0.0-dev.28 description: | An agent library built for Internet Computer, diff --git a/packages/agent_dart_base/pubspec.yaml b/packages/agent_dart_base/pubspec.yaml index ef665f7b..4a896965 100644 --- a/packages/agent_dart_base/pubspec.yaml +++ b/packages/agent_dart_base/pubspec.yaml @@ -1,5 +1,5 @@ name: agent_dart_base -version: 1.0.0-dev.27 +version: 1.0.0-dev.28 description: The Dart plugin that bridges Rust implementation for agent_dart. repository: https://github.com/AstroxNetwork/agent_dart diff --git a/packages/agent_dart_base/test/identity/p256.dart b/packages/agent_dart_base/test/identity/p256.dart index 86a6cc0f..5fad29c4 100644 --- a/packages/agent_dart_base/test/identity/p256.dart +++ b/packages/agent_dart_base/test/identity/p256.dart @@ -1,6 +1,5 @@ import 'package:agent_dart_base/agent_dart_base.dart'; import 'package:test/test.dart'; -import '../test_utils.dart'; void main() { p256Test(); diff --git a/packages/agent_dart_ffi/pubspec.yaml b/packages/agent_dart_ffi/pubspec.yaml index 828f99c1..26ac1021 100644 --- a/packages/agent_dart_ffi/pubspec.yaml +++ b/packages/agent_dart_ffi/pubspec.yaml @@ -1,5 +1,5 @@ name: agent_dart_ffi -version: 1.0.0-dev.27 +version: 1.0.0-dev.28 description: The FFI plugin that bridges Rust implementation for agent_dart. repository: https://github.com/AstroxNetwork/agent_dart