@ic-reactor/core 1.16.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/classes/actor/index.js +194 -169
- package/dist/classes/actor/types.d.ts +59 -3
- package/dist/classes/adapter/index.d.ts +1 -1
- package/dist/classes/adapter/index.js +80 -101
- package/dist/classes/agent/index.d.ts +1 -1
- package/dist/classes/agent/index.js +169 -165
- package/dist/classes/agent/types.d.ts +61 -2
- package/dist/createReactorCore.js +14 -32
- package/dist/createReactorStore.js +5 -13
- package/dist/index.js +17 -7
- package/dist/types.d.ts +1 -2
- package/dist/utils/helper.d.ts +7 -2
- package/dist/utils/helper.js +41 -33
- package/dist/utils/index.js +17 -7
- package/package.json +23 -27
|
@@ -1,21 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.CandidAdapter = void 0;
|
|
13
4
|
const agent_1 = require("@dfinity/agent");
|
|
14
5
|
const constants_1 = require("../../utils/constants");
|
|
15
6
|
const utils_1 = require("../../utils");
|
|
16
7
|
class CandidAdapter {
|
|
8
|
+
agent;
|
|
9
|
+
didjsCanisterId;
|
|
10
|
+
parserModule;
|
|
11
|
+
unsubscribeAgent = utils_1.noop;
|
|
17
12
|
constructor({ agentManager, agent, didjsCanisterId, }) {
|
|
18
|
-
this.unsubscribeAgent = () => { };
|
|
19
13
|
if (agent) {
|
|
20
14
|
this.agent = agent;
|
|
21
15
|
}
|
|
@@ -31,119 +25,104 @@ class CandidAdapter {
|
|
|
31
25
|
}
|
|
32
26
|
this.didjsCanisterId = didjsCanisterId || this.getDefaultDidJsId();
|
|
33
27
|
}
|
|
34
|
-
initializeParser(module) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
yield this.parserModule.default();
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
catch (error) {
|
|
48
|
-
throw new Error(`Error initializing parser: ${error}`);
|
|
28
|
+
async initializeParser(module) {
|
|
29
|
+
if (module !== undefined) {
|
|
30
|
+
this.parserModule = module;
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
this.parserModule = require("@ic-reactor/parser");
|
|
35
|
+
if (typeof this.parserModule !== "undefined" &&
|
|
36
|
+
"default" in this.parserModule) {
|
|
37
|
+
await this.parserModule.default();
|
|
49
38
|
}
|
|
50
|
-
}
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
throw new Error(`Error initializing parser: ${error}`);
|
|
42
|
+
}
|
|
51
43
|
}
|
|
52
44
|
getDefaultDidJsId() {
|
|
53
|
-
|
|
54
|
-
return ((_b = (_a = this.agent).isLocal) === null || _b === void 0 ? void 0 : _b.call(_a)) === true
|
|
45
|
+
return this.agent.isLocal?.() === true
|
|
55
46
|
? constants_1.DEFAULT_LOCAL_DIDJS_ID
|
|
56
47
|
: constants_1.DEFAULT_IC_DIDJS_ID;
|
|
57
48
|
}
|
|
58
|
-
fetchCandidDefinition(canisterId) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
candidDef = yield this.getFromMetadata(canisterId);
|
|
64
|
-
if (!candidDef) {
|
|
65
|
-
throw new Error("Cannot retrieve Candid definition from metadata");
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
catch (error) {
|
|
69
|
-
// Second attempt: Try the temporary hack method
|
|
70
|
-
candidDef = yield this.getFromTmpHack(canisterId).catch(() => {
|
|
71
|
-
return undefined;
|
|
72
|
-
});
|
|
73
|
-
}
|
|
49
|
+
async fetchCandidDefinition(canisterId) {
|
|
50
|
+
let candidDef = "";
|
|
51
|
+
// First attempt: Try getting Candid definition from metadata
|
|
52
|
+
try {
|
|
53
|
+
candidDef = await this.getFromMetadata(canisterId);
|
|
74
54
|
if (!candidDef) {
|
|
75
|
-
throw new Error("
|
|
55
|
+
throw new Error("Cannot retrieve Candid definition from metadata");
|
|
76
56
|
}
|
|
77
|
-
|
|
78
|
-
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
// Second attempt: Try the temporary hack method
|
|
60
|
+
candidDef = await this.getFromTmpHack(canisterId).catch(() => {
|
|
61
|
+
return undefined;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
if (!candidDef) {
|
|
65
|
+
throw new Error("Failed to retrieve Candid definition by any method.");
|
|
66
|
+
}
|
|
67
|
+
return candidDef;
|
|
79
68
|
}
|
|
80
|
-
getCandidDefinition(canisterId) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
});
|
|
69
|
+
async getCandidDefinition(canisterId) {
|
|
70
|
+
try {
|
|
71
|
+
const candidDef = await this.fetchCandidDefinition(canisterId);
|
|
72
|
+
return this.evaluateCandidDefinition(candidDef);
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
throw new Error(`Error fetching canister ${canisterId}: ${error}`);
|
|
76
|
+
}
|
|
90
77
|
}
|
|
91
|
-
getFromMetadata(canisterId) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
paths: ["candid"],
|
|
97
|
-
});
|
|
98
|
-
return status.get("candid");
|
|
78
|
+
async getFromMetadata(canisterId) {
|
|
79
|
+
const status = await agent_1.CanisterStatus.request({
|
|
80
|
+
agent: this.agent,
|
|
81
|
+
canisterId: canisterId,
|
|
82
|
+
paths: ["candid"],
|
|
99
83
|
});
|
|
84
|
+
return status.get("candid");
|
|
100
85
|
}
|
|
101
|
-
getFromTmpHack(canisterId) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
canisterId,
|
|
109
|
-
});
|
|
110
|
-
return (yield actor.__get_candid_interface_tmp_hack());
|
|
86
|
+
async getFromTmpHack(canisterId) {
|
|
87
|
+
const commonInterface = ({ IDL }) => IDL.Service({
|
|
88
|
+
__get_candid_interface_tmp_hack: IDL.Func([], [IDL.Text], ["query"]),
|
|
89
|
+
});
|
|
90
|
+
const actor = agent_1.Actor.createActor(commonInterface, {
|
|
91
|
+
agent: this.agent,
|
|
92
|
+
canisterId,
|
|
111
93
|
});
|
|
94
|
+
return (await actor.__get_candid_interface_tmp_hack());
|
|
112
95
|
}
|
|
113
|
-
|
|
114
|
-
|
|
96
|
+
async evaluateCandidDefinition(data) {
|
|
97
|
+
try {
|
|
98
|
+
let candidDef = "";
|
|
115
99
|
try {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
candidDef = this.parseDidToJs(data);
|
|
119
|
-
if (candidDef === "") {
|
|
120
|
-
throw new Error("Cannot compile Candid to JavaScript");
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
catch (error) {
|
|
124
|
-
candidDef = (yield this.fetchDidTojs(data))[0];
|
|
125
|
-
}
|
|
126
|
-
if (JSON.stringify(candidDef) === JSON.stringify([])) {
|
|
100
|
+
candidDef = this.parseDidToJs(data);
|
|
101
|
+
if (candidDef === "") {
|
|
127
102
|
throw new Error("Cannot compile Candid to JavaScript");
|
|
128
103
|
}
|
|
129
|
-
return yield (0, utils_1.importCandidDefinition)(candidDef);
|
|
130
104
|
}
|
|
131
105
|
catch (error) {
|
|
132
|
-
|
|
106
|
+
candidDef = (await this.fetchDidTojs(data))[0];
|
|
133
107
|
}
|
|
134
|
-
|
|
108
|
+
if (JSON.stringify(candidDef) === JSON.stringify([])) {
|
|
109
|
+
throw new Error("Cannot compile Candid to JavaScript");
|
|
110
|
+
}
|
|
111
|
+
return await (0, utils_1.importCandidDefinition)(candidDef);
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
throw new Error(`Error evaluating Candid definition: ${error}`);
|
|
115
|
+
}
|
|
135
116
|
}
|
|
136
|
-
fetchDidTojs(candidSource, didjsCanisterId) {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
canisterId: didjsCanisterId || this.didjsCanisterId,
|
|
144
|
-
});
|
|
145
|
-
return didjs.did_to_js(candidSource);
|
|
117
|
+
async fetchDidTojs(candidSource, didjsCanisterId) {
|
|
118
|
+
const didjsInterface = ({ IDL }) => IDL.Service({
|
|
119
|
+
did_to_js: IDL.Func([IDL.Text], [IDL.Opt(IDL.Text)], ["query"]),
|
|
120
|
+
});
|
|
121
|
+
const didjs = agent_1.Actor.createActor(didjsInterface, {
|
|
122
|
+
agent: this.agent,
|
|
123
|
+
canisterId: didjsCanisterId || this.didjsCanisterId,
|
|
146
124
|
});
|
|
125
|
+
return didjs.did_to_js(candidSource);
|
|
147
126
|
}
|
|
148
127
|
parseDidToJs(candidSource) {
|
|
149
128
|
if (!this.parserModule) {
|
|
@@ -32,5 +32,5 @@ export declare class AgentManager {
|
|
|
32
32
|
subscribeAuthState: AuthStore["subscribe"];
|
|
33
33
|
getAuth: () => AuthClient | null;
|
|
34
34
|
getIdentity: () => import("@dfinity/agent").Identity | null;
|
|
35
|
-
getPrincipal: () => import("@dfinity/principal").Principal | null;
|
|
35
|
+
getPrincipal: () => import("@dfinity/principal/lib/esm/principal").Principal | null;
|
|
36
36
|
}
|
|
@@ -1,24 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
-
var t = {};
|
|
13
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
-
t[p] = s[p];
|
|
15
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
-
t[p[i]] = s[p[i]];
|
|
19
|
-
}
|
|
20
|
-
return t;
|
|
21
|
-
};
|
|
22
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
3
|
exports.AgentManager = void 0;
|
|
24
4
|
/* eslint-disable no-console */
|
|
@@ -28,162 +8,34 @@ const auth_client_1 = require("@dfinity/auth-client");
|
|
|
28
8
|
const constants_1 = require("../../utils/constants");
|
|
29
9
|
const AGENT_INITIAL_STATE = {
|
|
30
10
|
initialized: false,
|
|
11
|
+
isInitialized: false,
|
|
31
12
|
initializing: false,
|
|
13
|
+
isInitializing: false,
|
|
32
14
|
error: undefined,
|
|
33
15
|
network: undefined,
|
|
34
16
|
};
|
|
35
17
|
const AUTH_INITIAL_STATE = {
|
|
36
18
|
identity: null,
|
|
37
19
|
authenticating: false,
|
|
20
|
+
isAuthenticating: false,
|
|
38
21
|
authenticated: false,
|
|
22
|
+
isAuthenticated: false,
|
|
39
23
|
error: undefined,
|
|
40
24
|
};
|
|
41
25
|
class AgentManager {
|
|
26
|
+
_agent;
|
|
27
|
+
_auth = null;
|
|
28
|
+
_subscribers = [];
|
|
29
|
+
agentStore;
|
|
30
|
+
authStore;
|
|
31
|
+
updateAgentState = (newState, action) => {
|
|
32
|
+
this.agentStore.setState((state) => ({ ...state, ...newState }), false, action);
|
|
33
|
+
};
|
|
34
|
+
updateAuthState = (newState, action) => {
|
|
35
|
+
this.authStore.setState((state) => ({ ...state, ...newState }), false, action);
|
|
36
|
+
};
|
|
42
37
|
constructor(options) {
|
|
43
|
-
|
|
44
|
-
this._auth = null;
|
|
45
|
-
this._subscribers = [];
|
|
46
|
-
this.updateAgentState = (newState, action) => {
|
|
47
|
-
this.agentStore.setState((state) => (Object.assign(Object.assign({}, state), newState)), false, action);
|
|
48
|
-
};
|
|
49
|
-
this.updateAuthState = (newState, action) => {
|
|
50
|
-
this.authStore.setState((state) => (Object.assign(Object.assign({}, state), newState)), false, action);
|
|
51
|
-
};
|
|
52
|
-
this.initializeAgent = () => __awaiter(this, void 0, void 0, function* () {
|
|
53
|
-
const network = this.getNetwork();
|
|
54
|
-
this.updateAgentState({
|
|
55
|
-
initializing: true,
|
|
56
|
-
error: undefined,
|
|
57
|
-
network,
|
|
58
|
-
}, "initializing");
|
|
59
|
-
if (network !== "ic") {
|
|
60
|
-
try {
|
|
61
|
-
yield this._agent.fetchRootKey();
|
|
62
|
-
}
|
|
63
|
-
catch (error) {
|
|
64
|
-
this.updateAgentState({ error: error, initializing: false }, "error");
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
this.updateAgentState({ initialized: true, initializing: false }, "initialized");
|
|
68
|
-
});
|
|
69
|
-
this.subscribeAgent = (callback, initialize = true) => {
|
|
70
|
-
if (initialize) {
|
|
71
|
-
callback(this._agent);
|
|
72
|
-
}
|
|
73
|
-
this._subscribers.push(callback);
|
|
74
|
-
return () => this.unsubscribeAgent(callback);
|
|
75
|
-
};
|
|
76
|
-
this.unsubscribeAgent = (callback) => {
|
|
77
|
-
this._subscribers = this._subscribers.filter((sub) => sub !== callback);
|
|
78
|
-
};
|
|
79
|
-
this.notifySubscribers = () => __awaiter(this, void 0, void 0, function* () {
|
|
80
|
-
yield Promise.all(this._subscribers.map((callback) => __awaiter(this, void 0, void 0, function* () { return callback(this._agent); })));
|
|
81
|
-
});
|
|
82
|
-
this.updateAgent = (options) => __awaiter(this, void 0, void 0, function* () {
|
|
83
|
-
const { agent } = options || {};
|
|
84
|
-
if (agent) {
|
|
85
|
-
this._agent = agent;
|
|
86
|
-
}
|
|
87
|
-
else if (options) {
|
|
88
|
-
this._agent = agent_1.HttpAgent.createSync(options);
|
|
89
|
-
yield this.initializeAgent();
|
|
90
|
-
}
|
|
91
|
-
yield this.notifySubscribers();
|
|
92
|
-
});
|
|
93
|
-
this.authenticate = () => __awaiter(this, void 0, void 0, function* () {
|
|
94
|
-
console.log(`Authenticating on ${this.getNetwork()} network`);
|
|
95
|
-
this.updateAuthState({ authenticating: true }, "authenticating");
|
|
96
|
-
try {
|
|
97
|
-
this._auth = yield auth_client_1.AuthClient.create();
|
|
98
|
-
const authenticated = yield this._auth.isAuthenticated();
|
|
99
|
-
const identity = this._auth.getIdentity();
|
|
100
|
-
this._agent.replaceIdentity(identity);
|
|
101
|
-
this.notifySubscribers();
|
|
102
|
-
this.updateAuthState({
|
|
103
|
-
authenticated,
|
|
104
|
-
identity,
|
|
105
|
-
authenticating: false,
|
|
106
|
-
}, "authenticated");
|
|
107
|
-
return identity;
|
|
108
|
-
}
|
|
109
|
-
catch (error) {
|
|
110
|
-
this.updateAuthState({ error: error, authenticating: false }, "error");
|
|
111
|
-
throw error;
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
this.login = (options) => __awaiter(this, void 0, void 0, function* () {
|
|
115
|
-
this.updateAuthState({ authenticating: true }, "login");
|
|
116
|
-
if (!this._auth) {
|
|
117
|
-
yield this.authenticate();
|
|
118
|
-
}
|
|
119
|
-
if (!this._auth) {
|
|
120
|
-
throw new Error("Auth client not initialized");
|
|
121
|
-
}
|
|
122
|
-
yield this._auth.login(Object.assign(Object.assign({ identityProvider: this.getIsLocal()
|
|
123
|
-
? constants_1.LOCAL_INTERNET_IDENTITY_PROVIDER
|
|
124
|
-
: constants_1.IC_INTERNET_IDENTITY_PROVIDER }, options), { onSuccess: (msg) => __awaiter(this, void 0, void 0, function* () {
|
|
125
|
-
var _a;
|
|
126
|
-
yield this.authenticate();
|
|
127
|
-
(_a = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _a === void 0 ? void 0 : _a.call(options, msg);
|
|
128
|
-
}) }));
|
|
129
|
-
});
|
|
130
|
-
this.logout = (options) => __awaiter(this, void 0, void 0, function* () {
|
|
131
|
-
if (!this._auth) {
|
|
132
|
-
throw new Error("Auth client not initialized");
|
|
133
|
-
}
|
|
134
|
-
yield this._auth.logout(options);
|
|
135
|
-
yield this.authenticate();
|
|
136
|
-
});
|
|
137
|
-
// agent store
|
|
138
|
-
this.getAgent = () => {
|
|
139
|
-
return this._agent;
|
|
140
|
-
};
|
|
141
|
-
this.getAgentHost = () => {
|
|
142
|
-
return this._agent.host;
|
|
143
|
-
};
|
|
144
|
-
this.getAgentHostName = () => {
|
|
145
|
-
var _a;
|
|
146
|
-
return ((_a = this.getAgentHost()) === null || _a === void 0 ? void 0 : _a.hostname) || "";
|
|
147
|
-
};
|
|
148
|
-
this.getIsLocal = () => {
|
|
149
|
-
return this.getNetwork() !== "ic";
|
|
150
|
-
};
|
|
151
|
-
this.getNetwork = () => {
|
|
152
|
-
const hostname = this.getAgentHostName();
|
|
153
|
-
return (0, helper_1.getNetworkByHostname)(hostname);
|
|
154
|
-
};
|
|
155
|
-
this.getAgentState = () => {
|
|
156
|
-
return this.agentStore.getState();
|
|
157
|
-
};
|
|
158
|
-
// @ts-expect-error: Overrides subscribe method signature
|
|
159
|
-
this.subscribeAgentState = (selectorOrListener, listener, options) => {
|
|
160
|
-
if (listener) {
|
|
161
|
-
return this.agentStore.subscribe(selectorOrListener, listener, options);
|
|
162
|
-
}
|
|
163
|
-
return this.agentStore.subscribe(selectorOrListener);
|
|
164
|
-
};
|
|
165
|
-
// auth store
|
|
166
|
-
this.getAuthState = () => {
|
|
167
|
-
return this.authStore.getState();
|
|
168
|
-
};
|
|
169
|
-
// @ts-expect-error: Overrides subscribe method signature
|
|
170
|
-
this.subscribeAuthState = (selectorOrListener, listener, options) => {
|
|
171
|
-
if (listener) {
|
|
172
|
-
return this.authStore.subscribe(selectorOrListener, listener, options);
|
|
173
|
-
}
|
|
174
|
-
return this.authStore.subscribe(selectorOrListener);
|
|
175
|
-
};
|
|
176
|
-
this.getAuth = () => {
|
|
177
|
-
return this._auth;
|
|
178
|
-
};
|
|
179
|
-
this.getIdentity = () => {
|
|
180
|
-
return this.authStore.getState().identity;
|
|
181
|
-
};
|
|
182
|
-
this.getPrincipal = () => {
|
|
183
|
-
const identity = this.authStore.getState().identity;
|
|
184
|
-
return identity ? identity.getPrincipal() : null;
|
|
185
|
-
};
|
|
186
|
-
const _b = options || {}, { withDevtools, port = 4943, withLocalEnv, withProcessEnv, initializeOnCreate = true } = _b, agentOptions = __rest(_b, ["withDevtools", "port", "withLocalEnv", "withProcessEnv", "initializeOnCreate"]);
|
|
38
|
+
const { withDevtools, port = 4943, withLocalEnv, withProcessEnv, initializeOnCreate = true, ...agentOptions } = options || {};
|
|
187
39
|
if (withProcessEnv) {
|
|
188
40
|
const processNetwork = (0, helper_1.getProcessEnvNetwork)();
|
|
189
41
|
agentOptions.host =
|
|
@@ -193,7 +45,7 @@ class AgentManager {
|
|
|
193
45
|
agentOptions.host = `http://127.0.0.1:${port}`;
|
|
194
46
|
}
|
|
195
47
|
else {
|
|
196
|
-
agentOptions.host =
|
|
48
|
+
agentOptions.host = agentOptions.host ?? constants_1.IC_HOST_NETWORK_URI;
|
|
197
49
|
}
|
|
198
50
|
this.agentStore = (0, helper_1.createStoreWithOptionalDevtools)(AGENT_INITIAL_STATE, {
|
|
199
51
|
withDevtools,
|
|
@@ -210,5 +62,157 @@ class AgentManager {
|
|
|
210
62
|
this.initializeAgent();
|
|
211
63
|
}
|
|
212
64
|
}
|
|
65
|
+
initializeAgent = async () => {
|
|
66
|
+
const network = this.getNetwork();
|
|
67
|
+
this.updateAgentState({
|
|
68
|
+
initializing: true,
|
|
69
|
+
isInitializing: true,
|
|
70
|
+
error: undefined,
|
|
71
|
+
network,
|
|
72
|
+
}, "initializing");
|
|
73
|
+
if (network !== "ic") {
|
|
74
|
+
try {
|
|
75
|
+
await this._agent.fetchRootKey();
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
this.updateAgentState({
|
|
79
|
+
error: error,
|
|
80
|
+
initializing: false,
|
|
81
|
+
isInitializing: false,
|
|
82
|
+
}, "error");
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
this.updateAgentState({
|
|
86
|
+
initialized: true,
|
|
87
|
+
isInitialized: true,
|
|
88
|
+
initializing: false,
|
|
89
|
+
isInitializing: false,
|
|
90
|
+
}, "initialized");
|
|
91
|
+
};
|
|
92
|
+
subscribeAgent = (callback, initialize = true) => {
|
|
93
|
+
if (initialize) {
|
|
94
|
+
callback(this._agent);
|
|
95
|
+
}
|
|
96
|
+
this._subscribers.push(callback);
|
|
97
|
+
return () => this.unsubscribeAgent(callback);
|
|
98
|
+
};
|
|
99
|
+
unsubscribeAgent = (callback) => {
|
|
100
|
+
this._subscribers = this._subscribers.filter((sub) => sub !== callback);
|
|
101
|
+
};
|
|
102
|
+
notifySubscribers = async () => {
|
|
103
|
+
await Promise.all(this._subscribers.map(async (callback) => callback(this._agent)));
|
|
104
|
+
};
|
|
105
|
+
updateAgent = async (options) => {
|
|
106
|
+
const { agent } = options || {};
|
|
107
|
+
if (agent) {
|
|
108
|
+
this._agent = agent;
|
|
109
|
+
}
|
|
110
|
+
else if (options) {
|
|
111
|
+
this._agent = agent_1.HttpAgent.createSync(options);
|
|
112
|
+
await this.initializeAgent();
|
|
113
|
+
}
|
|
114
|
+
await this.notifySubscribers();
|
|
115
|
+
};
|
|
116
|
+
authenticate = async () => {
|
|
117
|
+
console.log(`Authenticating on ${this.getNetwork()} network`);
|
|
118
|
+
this.updateAuthState({ isAuthenticating: true, authenticating: true }, "authenticating");
|
|
119
|
+
try {
|
|
120
|
+
this._auth = await auth_client_1.AuthClient.create();
|
|
121
|
+
const isAuthenticated = await this._auth.isAuthenticated();
|
|
122
|
+
const identity = this._auth.getIdentity();
|
|
123
|
+
this._agent.replaceIdentity(identity);
|
|
124
|
+
this.notifySubscribers();
|
|
125
|
+
this.updateAuthState({
|
|
126
|
+
authenticated: isAuthenticated,
|
|
127
|
+
isAuthenticated,
|
|
128
|
+
identity,
|
|
129
|
+
authenticating: false,
|
|
130
|
+
isAuthenticating: false,
|
|
131
|
+
}, "authenticated");
|
|
132
|
+
return identity;
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
this.updateAuthState({
|
|
136
|
+
error: error,
|
|
137
|
+
isAuthenticating: false,
|
|
138
|
+
authenticating: false,
|
|
139
|
+
}, "error");
|
|
140
|
+
throw error;
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
login = async (options) => {
|
|
144
|
+
this.updateAuthState({ isAuthenticating: true, authenticating: true }, "login");
|
|
145
|
+
if (!this._auth) {
|
|
146
|
+
await this.authenticate();
|
|
147
|
+
}
|
|
148
|
+
if (!this._auth) {
|
|
149
|
+
throw new Error("Auth client not initialized");
|
|
150
|
+
}
|
|
151
|
+
await this._auth.login({
|
|
152
|
+
identityProvider: this.getIsLocal()
|
|
153
|
+
? constants_1.LOCAL_INTERNET_IDENTITY_PROVIDER
|
|
154
|
+
: constants_1.IC_INTERNET_IDENTITY_PROVIDER,
|
|
155
|
+
...options,
|
|
156
|
+
onSuccess: async (msg) => {
|
|
157
|
+
await this.authenticate();
|
|
158
|
+
options?.onSuccess?.(msg);
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
};
|
|
162
|
+
logout = async (options) => {
|
|
163
|
+
if (!this._auth) {
|
|
164
|
+
throw new Error("Auth client not initialized");
|
|
165
|
+
}
|
|
166
|
+
await this._auth.logout(options);
|
|
167
|
+
await this.authenticate();
|
|
168
|
+
};
|
|
169
|
+
// agent store
|
|
170
|
+
getAgent = () => {
|
|
171
|
+
return this._agent;
|
|
172
|
+
};
|
|
173
|
+
getAgentHost = () => {
|
|
174
|
+
return this._agent.host;
|
|
175
|
+
};
|
|
176
|
+
getAgentHostName = () => {
|
|
177
|
+
return this.getAgentHost()?.hostname || "";
|
|
178
|
+
};
|
|
179
|
+
getIsLocal = () => {
|
|
180
|
+
return this.getNetwork() !== "ic";
|
|
181
|
+
};
|
|
182
|
+
getNetwork = () => {
|
|
183
|
+
const hostname = this.getAgentHostName();
|
|
184
|
+
return (0, helper_1.getNetworkByHostname)(hostname);
|
|
185
|
+
};
|
|
186
|
+
getAgentState = () => {
|
|
187
|
+
return this.agentStore.getState();
|
|
188
|
+
};
|
|
189
|
+
// @ts-expect-error: Overrides subscribe method signature
|
|
190
|
+
subscribeAgentState = (selectorOrListener, listener, options) => {
|
|
191
|
+
if (listener) {
|
|
192
|
+
return this.agentStore.subscribe(selectorOrListener, listener, options);
|
|
193
|
+
}
|
|
194
|
+
return this.agentStore.subscribe(selectorOrListener);
|
|
195
|
+
};
|
|
196
|
+
// auth store
|
|
197
|
+
getAuthState = () => {
|
|
198
|
+
return this.authStore.getState();
|
|
199
|
+
};
|
|
200
|
+
// @ts-expect-error: Overrides subscribe method signature
|
|
201
|
+
subscribeAuthState = (selectorOrListener, listener, options) => {
|
|
202
|
+
if (listener) {
|
|
203
|
+
return this.authStore.subscribe(selectorOrListener, listener, options);
|
|
204
|
+
}
|
|
205
|
+
return this.authStore.subscribe(selectorOrListener);
|
|
206
|
+
};
|
|
207
|
+
getAuth = () => {
|
|
208
|
+
return this._auth;
|
|
209
|
+
};
|
|
210
|
+
getIdentity = () => {
|
|
211
|
+
return this.authStore.getState().identity;
|
|
212
|
+
};
|
|
213
|
+
getPrincipal = () => {
|
|
214
|
+
const identity = this.authStore.getState().identity;
|
|
215
|
+
return identity ? identity.getPrincipal() : null;
|
|
216
|
+
};
|
|
213
217
|
}
|
|
214
218
|
exports.AgentManager = AgentManager;
|
|
@@ -1,7 +1,19 @@
|
|
|
1
|
-
import type { HttpAgent, HttpAgentOptions, Identity } from "@dfinity/agent";
|
|
1
|
+
import type { AgentError, HttpAgent, HttpAgentOptions, Identity } from "@dfinity/agent";
|
|
2
2
|
import type { AuthClient } from "@dfinity/auth-client";
|
|
3
3
|
import type { StoreWithAllMiddleware } from "../../types";
|
|
4
4
|
export { HttpAgentOptions, AuthClient, Identity };
|
|
5
|
+
/**
|
|
6
|
+
* Parameters for configuring an AgentManager instance.
|
|
7
|
+
* Extends the options available in `HttpAgentOptions`.
|
|
8
|
+
*
|
|
9
|
+
* @extends HttpAgentOptions
|
|
10
|
+
*
|
|
11
|
+
* @property {number} [port] - The port number to be used by the agent.
|
|
12
|
+
* @property {boolean} [withLocalEnv] - Whether to include local environment variables.
|
|
13
|
+
* @property {boolean} [withDevtools] - Whether to enable developer tools integration.
|
|
14
|
+
* @property {boolean} [withProcessEnv] - Whether to include process environment variables.
|
|
15
|
+
* @property {boolean} [initializeOnCreate] - Whether to initialize the agent upon creation.
|
|
16
|
+
*/
|
|
5
17
|
export interface AgentManagerParameters extends HttpAgentOptions {
|
|
6
18
|
port?: number;
|
|
7
19
|
withLocalEnv?: boolean;
|
|
@@ -9,16 +21,63 @@ export interface AgentManagerParameters extends HttpAgentOptions {
|
|
|
9
21
|
withProcessEnv?: boolean;
|
|
10
22
|
initializeOnCreate?: boolean;
|
|
11
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Represents the state of an agent.
|
|
26
|
+
*/
|
|
12
27
|
export interface AgentState {
|
|
28
|
+
/**
|
|
29
|
+
* @deprecated Use `isInitialized` instead.
|
|
30
|
+
* Indicates whether the agent has been initialized.
|
|
31
|
+
*/
|
|
13
32
|
initialized: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Indicates whether the agent has been initialized.
|
|
35
|
+
*/
|
|
36
|
+
isInitialized: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* @deprecated Use `isInitializing` instead.
|
|
39
|
+
* Indicates whether the agent is in the process of initializing.
|
|
40
|
+
*/
|
|
14
41
|
initializing: boolean;
|
|
15
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Indicates whether the agent is in the process of initializing.
|
|
44
|
+
*/
|
|
45
|
+
isInitializing: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Represents an error associated with the agent, if any.
|
|
48
|
+
*/
|
|
49
|
+
error: AgentError | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* Represents the network associated with the agent, if any.
|
|
52
|
+
*/
|
|
16
53
|
network: string | undefined;
|
|
17
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Represents the authentication state of an agent.
|
|
57
|
+
*/
|
|
18
58
|
export interface AuthState {
|
|
19
59
|
identity: Identity | null;
|
|
60
|
+
/**
|
|
61
|
+
* @deprecated Use `isAuthenticating` instead.
|
|
62
|
+
* Indicates whether the authentication process is ongoing.
|
|
63
|
+
*/
|
|
20
64
|
authenticating: boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Indicates whether the authentication process is ongoing.
|
|
67
|
+
*/
|
|
68
|
+
isAuthenticating: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* @deprecated Use `isAuthenticated` instead.
|
|
71
|
+
* Indicates whether the agent is authenticated.
|
|
72
|
+
*/
|
|
21
73
|
authenticated: boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Indicates whether the agent is authenticated.
|
|
76
|
+
*/
|
|
77
|
+
isAuthenticated: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Represents any error that occurred during authentication.
|
|
80
|
+
*/
|
|
22
81
|
error: Error | undefined;
|
|
23
82
|
}
|
|
24
83
|
export interface UpdateAgentParameters extends HttpAgentOptions {
|