@metamask/connect-multichain 0.15.0 → 1.1.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/CHANGELOG.md +36 -1
- package/README.md +0 -1
- package/dist/browser/es/connect-multichain.d.mts +18 -8
- package/dist/browser/es/connect-multichain.mjs +558 -378
- package/dist/browser/es/connect-multichain.mjs.map +1 -1
- package/dist/browser/es/metafile-esm.json +1 -1
- package/dist/browser/iife/connect-multichain.d.ts +18 -8
- package/dist/browser/iife/connect-multichain.js +571 -378
- package/dist/browser/iife/connect-multichain.js.map +1 -1
- package/dist/browser/iife/metafile-iife.json +1 -1
- package/dist/browser/umd/connect-multichain.d.ts +18 -8
- package/dist/browser/umd/connect-multichain.js +558 -378
- package/dist/browser/umd/connect-multichain.js.map +1 -1
- package/dist/browser/umd/metafile-cjs.json +1 -1
- package/dist/node/cjs/connect-multichain.d.ts +18 -8
- package/dist/node/cjs/connect-multichain.js +560 -376
- package/dist/node/cjs/connect-multichain.js.map +1 -1
- package/dist/node/cjs/metafile-cjs.json +1 -1
- package/dist/node/es/connect-multichain.d.mts +18 -8
- package/dist/node/es/connect-multichain.mjs +557 -375
- package/dist/node/es/connect-multichain.mjs.map +1 -1
- package/dist/node/es/metafile-esm.json +1 -1
- package/dist/react-native/es/connect-multichain.d.mts +18 -8
- package/dist/react-native/es/connect-multichain.mjs +556 -374
- package/dist/react-native/es/connect-multichain.mjs.map +1 -1
- package/dist/react-native/es/metafile-esm.json +1 -1
- package/dist/src/domain/errors/rpc.d.ts +11 -1
- package/dist/src/domain/errors/rpc.d.ts.map +1 -1
- package/dist/src/domain/errors/rpc.js +10 -2
- package/dist/src/domain/errors/rpc.js.map +1 -1
- package/dist/src/domain/multichain/api/constants.d.ts +1 -0
- package/dist/src/domain/multichain/api/constants.d.ts.map +1 -1
- package/dist/src/domain/multichain/api/constants.js +13 -0
- package/dist/src/domain/multichain/api/constants.js.map +1 -1
- package/dist/src/domain/multichain/index.d.ts +2 -2
- package/dist/src/domain/multichain/index.d.ts.map +1 -1
- package/dist/src/domain/multichain/index.js.map +1 -1
- package/dist/src/domain/multichain/types.d.ts +0 -1
- package/dist/src/domain/multichain/types.d.ts.map +1 -1
- package/dist/src/domain/platform/index.d.ts.map +1 -1
- package/dist/src/domain/platform/index.js +27 -5
- package/dist/src/domain/platform/index.js.map +1 -1
- package/dist/src/domain/store/client.d.ts +3 -3
- package/dist/src/domain/store/client.d.ts.map +1 -1
- package/dist/src/domain/utils/index.d.ts +1 -0
- package/dist/src/domain/utils/index.d.ts.map +1 -1
- package/dist/src/domain/utils/index.js +5 -1
- package/dist/src/domain/utils/index.js.map +1 -1
- package/dist/src/multichain/index.d.ts +2 -3
- package/dist/src/multichain/index.d.ts.map +1 -1
- package/dist/src/multichain/index.js +142 -147
- package/dist/src/multichain/index.js.map +1 -1
- package/dist/src/multichain/rpc/invocationError.d.ts +9 -0
- package/dist/src/multichain/rpc/invocationError.d.ts.map +1 -0
- package/dist/src/multichain/rpc/invocationError.js +99 -0
- package/dist/src/multichain/rpc/invocationError.js.map +1 -0
- package/dist/src/multichain/rpc/requestRouter.d.ts +15 -0
- package/dist/src/multichain/rpc/requestRouter.d.ts.map +1 -1
- package/dist/src/multichain/rpc/requestRouter.js +38 -20
- package/dist/src/multichain/rpc/requestRouter.js.map +1 -1
- package/dist/src/multichain/transports/default/index.d.ts.map +1 -1
- package/dist/src/multichain/transports/default/index.js +25 -10
- package/dist/src/multichain/transports/default/index.js.map +1 -1
- package/dist/src/multichain/transports/multichainApiClientWrapper/index.d.ts +2 -1
- package/dist/src/multichain/transports/multichainApiClientWrapper/index.d.ts.map +1 -1
- package/dist/src/multichain/transports/multichainApiClientWrapper/index.js +25 -17
- package/dist/src/multichain/transports/multichainApiClientWrapper/index.js.map +1 -1
- package/dist/src/multichain/transports/mwp/index.d.ts +3 -1
- package/dist/src/multichain/transports/mwp/index.d.ts.map +1 -1
- package/dist/src/multichain/transports/mwp/index.js +235 -172
- package/dist/src/multichain/transports/mwp/index.js.map +1 -1
- package/dist/src/store/index.d.ts +3 -3
- package/dist/src/store/index.d.ts.map +1 -1
- package/dist/src/store/index.js +8 -8
- package/dist/src/store/index.js.map +1 -1
- package/dist/src/ui/ModalFactory.d.ts.map +1 -1
- package/dist/src/ui/ModalFactory.js +5 -1
- package/dist/src/ui/ModalFactory.js.map +1 -1
- package/dist/src/ui/index.js +1 -1
- package/dist/src/ui/index.js.map +1 -1
- package/dist/src/ui/modals/web/install.d.ts.map +1 -1
- package/dist/src/ui/modals/web/install.js.map +1 -1
- package/dist/types/connect-multichain.d.ts +18 -8
- package/package.json +2 -2
|
@@ -10,31 +10,34 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
11
11
|
});
|
|
12
12
|
};
|
|
13
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
14
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
15
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
16
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
17
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
18
|
+
};
|
|
13
19
|
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
14
20
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
15
21
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
16
22
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
17
23
|
};
|
|
18
|
-
var _MultichainApiClientWrapperTransport_instances, _MultichainApiClientWrapperTransport_notificationCallbacks, _MultichainApiClientWrapperTransport_walletCreateSession, _MultichainApiClientWrapperTransport_walletGetSession, _MultichainApiClientWrapperTransport_walletRevokeSession, _MultichainApiClientWrapperTransport_walletInvokeMethod;
|
|
24
|
+
var _MultichainApiClientWrapperTransport_instances, _MultichainApiClientWrapperTransport_notificationCallbacks, _MultichainApiClientWrapperTransport_getTransport, _MultichainApiClientWrapperTransport_walletCreateSession, _MultichainApiClientWrapperTransport_walletGetSession, _MultichainApiClientWrapperTransport_walletRevokeSession, _MultichainApiClientWrapperTransport_walletInvokeMethod;
|
|
19
25
|
import { providerErrors } from '@metamask/rpc-errors';
|
|
20
26
|
import { getUniqueRequestId } from '../../utils';
|
|
21
27
|
export class MultichainApiClientWrapperTransport {
|
|
22
|
-
constructor(metamaskConnectMultichain) {
|
|
28
|
+
constructor(metamaskConnectMultichain, getTransport) {
|
|
23
29
|
_MultichainApiClientWrapperTransport_instances.add(this);
|
|
24
30
|
this.metamaskConnectMultichain = metamaskConnectMultichain;
|
|
25
31
|
_MultichainApiClientWrapperTransport_notificationCallbacks.set(this, new Set());
|
|
32
|
+
_MultichainApiClientWrapperTransport_getTransport.set(this, void 0);
|
|
33
|
+
__classPrivateFieldSet(this, _MultichainApiClientWrapperTransport_getTransport, getTransport, "f");
|
|
26
34
|
}
|
|
27
35
|
isTransportDefined() {
|
|
28
|
-
|
|
29
|
-
return Boolean(this.metamaskConnectMultichain.transport);
|
|
30
|
-
}
|
|
31
|
-
catch (_error) {
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
36
|
+
return __classPrivateFieldGet(this, _MultichainApiClientWrapperTransport_getTransport, "f").call(this) !== undefined;
|
|
34
37
|
}
|
|
35
38
|
isTransportConnected() {
|
|
36
|
-
|
|
37
|
-
|
|
39
|
+
var _a, _b;
|
|
40
|
+
return (_b = (_a = __classPrivateFieldGet(this, _MultichainApiClientWrapperTransport_getTransport, "f").call(this)) === null || _a === void 0 ? void 0 : _a.isConnected()) !== null && _b !== void 0 ? _b : false;
|
|
38
41
|
}
|
|
39
42
|
clearNotificationCallbacks() {
|
|
40
43
|
__classPrivateFieldGet(this, _MultichainApiClientWrapperTransport_notificationCallbacks, "f").clear();
|
|
@@ -50,11 +53,11 @@ export class MultichainApiClientWrapperTransport {
|
|
|
50
53
|
this.notificationListener = undefined;
|
|
51
54
|
}
|
|
52
55
|
setupTransportNotificationListener() {
|
|
53
|
-
|
|
56
|
+
const transport = __classPrivateFieldGet(this, _MultichainApiClientWrapperTransport_getTransport, "f").call(this);
|
|
57
|
+
if (!transport || this.notificationListener) {
|
|
54
58
|
return;
|
|
55
59
|
}
|
|
56
|
-
this.notificationListener =
|
|
57
|
-
this.metamaskConnectMultichain.transport.onNotification(this.notifyCallbacks.bind(this));
|
|
60
|
+
this.notificationListener = transport.onNotification(this.notifyCallbacks.bind(this));
|
|
58
61
|
}
|
|
59
62
|
// Purposely noop, resolves successfully. Actual connection is handled by the underlying client/transport.
|
|
60
63
|
connect() {
|
|
@@ -98,7 +101,7 @@ export class MultichainApiClientWrapperTransport {
|
|
|
98
101
|
};
|
|
99
102
|
}
|
|
100
103
|
}
|
|
101
|
-
_MultichainApiClientWrapperTransport_notificationCallbacks = new WeakMap(), _MultichainApiClientWrapperTransport_instances = new WeakSet(), _MultichainApiClientWrapperTransport_walletCreateSession = function _MultichainApiClientWrapperTransport_walletCreateSession(request) {
|
|
104
|
+
_MultichainApiClientWrapperTransport_notificationCallbacks = new WeakMap(), _MultichainApiClientWrapperTransport_getTransport = new WeakMap(), _MultichainApiClientWrapperTransport_instances = new WeakSet(), _MultichainApiClientWrapperTransport_walletCreateSession = function _MultichainApiClientWrapperTransport_walletCreateSession(request) {
|
|
102
105
|
return __awaiter(this, void 0, void 0, function* () {
|
|
103
106
|
const createSessionParams = request.params;
|
|
104
107
|
const scopes = Object.keys(Object.assign(Object.assign({}, createSessionParams.optionalScopes), createSessionParams.requiredScopes));
|
|
@@ -116,13 +119,18 @@ _MultichainApiClientWrapperTransport_notificationCallbacks = new WeakMap(), _Mul
|
|
|
116
119
|
});
|
|
117
120
|
const accounts = [...new Set(scopeAccounts)];
|
|
118
121
|
yield this.metamaskConnectMultichain.connect(scopes, accounts, createSessionParams.sessionProperties);
|
|
119
|
-
|
|
122
|
+
const transport = __classPrivateFieldGet(this, _MultichainApiClientWrapperTransport_getTransport, "f").call(this);
|
|
123
|
+
if (!transport) {
|
|
124
|
+
throw new Error('Transport not initialized after connect');
|
|
125
|
+
}
|
|
126
|
+
return transport.request({
|
|
120
127
|
method: 'wallet_getSession',
|
|
121
128
|
});
|
|
122
129
|
});
|
|
123
130
|
}, _MultichainApiClientWrapperTransport_walletGetSession = function _MultichainApiClientWrapperTransport_walletGetSession(request) {
|
|
124
131
|
return __awaiter(this, void 0, void 0, function* () {
|
|
125
|
-
|
|
132
|
+
const transport = __classPrivateFieldGet(this, _MultichainApiClientWrapperTransport_getTransport, "f").call(this);
|
|
133
|
+
if (!(transport === null || transport === void 0 ? void 0 : transport.isConnected())) {
|
|
126
134
|
return {
|
|
127
135
|
jsonrpc: '2.0',
|
|
128
136
|
id: request.id,
|
|
@@ -131,7 +139,7 @@ _MultichainApiClientWrapperTransport_notificationCallbacks = new WeakMap(), _Mul
|
|
|
131
139
|
},
|
|
132
140
|
};
|
|
133
141
|
}
|
|
134
|
-
return
|
|
142
|
+
return transport.request({
|
|
135
143
|
method: 'wallet_getSession',
|
|
136
144
|
});
|
|
137
145
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/multichain/transports/multichainApiClientWrapper/index.ts"],"names":[],"mappings":"AAAA,8FAA8F;AAC9F,sGAAsG;AACtG,oGAAoG
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/multichain/transports/multichainApiClientWrapper/index.ts"],"names":[],"mappings":"AAAA,8FAA8F;AAC9F,sGAAsG;AACtG,oGAAoG;;;;;;;;;;;;;;;;;;;;;;AASpG,OAAO,EAAE,cAAc,EAAE,M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AS,CAAE,WAAW,EAAE,CAAA,EAAE,CAAC;YAC9B,OAAO;gBACL,OAAO,EAAE,KAAK;gBACd,EAAE,EAAE,OAAO,CAAC,EAAE;gBACd,MAAM,EAAE;oBACN,aAAa,EAAE,EAAE;iBAClB;aACF,CAAC;QACJ,CAAC;QACD,OAAO,SAAS,CAAC,OAAO,CAAC;YACvB,MAAM,EAAE,mBAAmB;SAC5B,CAAC,CAAC;IACL,CAAC;gIAE0B,OAA+B;;;QACxD,MAAM,mBAAmB,GAAG,OAAO,CAAC,MAEvB,CAAC;QACd,MAAM,MAAM,GAAG,MAAA,mBAAmB,aAAnB,mBAAmB,uBAAnB,mBAAmB,CAAE,MAAM,mCAAI,EAAE,CAAC;QAEjD,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,yBAAyB,CAAC,UAAU,CAAC,MAAiB,CAAC,CAAC;YACnE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,OAAO,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;QAC1D,CAAC;QAAC,OAAO,MAAM,EAAE,CAAC;YAChB,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,OAAO,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;QAC3D,CAAC;IACH,CAAC;8HAEyB,OAA+B;;QACvD,IAAI,CAAC,IAAI,CAAC,oBAAoB,EAAE,EAAE,CAAC;YACjC,OAAO,EAAE,KAAK,EAAE,cAAc,CAAC,YAAY,EAAE,EAAE,CAAC;QAClD,CAAC;QACD,MAAM,MAAM,GAAG,IAAI,CAAC,yBAAyB,CAAC,YAAY,CACxD,OAAO,CAAC,MAA6B,CACtC,CAAC;QAEF,OAAO;YACL,MAAM;SACP,CAAC;IACJ,CAAC"}
|
|
@@ -18,6 +18,7 @@ type PendingRequests = {
|
|
|
18
18
|
* Bridges the MWP DappClient with the multichain API client Transport interface
|
|
19
19
|
*/
|
|
20
20
|
export declare class MWPTransport implements ExtendedTransport {
|
|
21
|
+
#private;
|
|
21
22
|
private dappClient;
|
|
22
23
|
private kvstore;
|
|
23
24
|
private options;
|
|
@@ -49,8 +50,9 @@ export declare class MWPTransport implements ExtendedTransport {
|
|
|
49
50
|
private notifyCallbacks;
|
|
50
51
|
private rejectRequest;
|
|
51
52
|
private parseWalletError;
|
|
53
|
+
private getResponseError;
|
|
54
|
+
private isErrorPayload;
|
|
52
55
|
private handleMessage;
|
|
53
|
-
private onResumeSuccess;
|
|
54
56
|
init(): Promise<void>;
|
|
55
57
|
sendEip1193Message<TRequest extends TransportRequest, TResponse extends TransportResponse>(payload: TRequest, options?: {
|
|
56
58
|
timeout?: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/multichain/transports/mwp/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/multichain/transports/mwp/index.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EACV,OAAO,EACP,cAAc,EACf,MAAM,uCAAuC,CAAC;AAC/C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8CAA8C,CAAC;AAC/E,OAAO,EACL,KAAK,iBAAiB,EAEtB,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EAEvB,MAAM,iCAAiC,CAAC;AAEzC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAGrD,OAAO,EAIL,KAAK,iBAAiB,EAEtB,KAAK,KAAK,EAEV,KAAK,YAAY,EAClB,MAAM,iBAAiB,CAAC;AA8BzB,KAAK,eAAe,GAAG;IACrB,OAAO,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,EAAE,EAAE,MAAM,CAAA;KAAE,GAAG,gBAAgB,CAAC;IAC5D,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAC;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,CAAC;IAsClE,OAAO,CAAC,OAAO,CAAC,EAAE;QACtB,MAAM,EAAE,KAAK,EAAE,CAAC;QAChB,cAAc,EAAE,aAAa,EAAE,CAAC;QAChC,iBAAiB,CAAC,EAAE,iBAAiB,CAAC;QACtC,YAAY,CAAC,EAAE,OAAO,CAAC;KACxB,GAAG,OAAO,CAAC,IAAI,CAAC;IA0BjB;;;;;OAKG;IACG,UAAU,CAAC,MAAM,GAAE,KAAK,EAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAyErD;;;;OAIG;IACH,WAAW,IAAI,OAAO;IAItB;;;;OAIG;YAGW,oBAAoB;YA0BpB,iBAAiB;YAqCjB,kBAAkB;IAuB1B,OAAO,CACX,QAAQ,SAAS,gBAAgB,EACjC,SAAS,SAAS,iBAAiB,EACnC,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,EAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,SAAS,CAAC;IA0CxE,cAAc,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,GAAG,MAAM,IAAI;IAOvD,gBAAgB,IAAI,OAAO,CAAC,OAAO,GAAG,SAAS,CAAC;YAyBxC,+BAA+B;CAkC9C"}
|
|
@@ -7,8 +7,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
11
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
12
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
13
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
14
|
+
};
|
|
15
|
+
var _MWPTransport_instances, _MWPTransport_onResumeHandler, _MWPTransport_resumeSession, _MWPTransport_startSession;
|
|
10
16
|
import { TransportTimeoutError, } from '@metamask/multichain-api-client';
|
|
11
17
|
import { JsonRpcError, providerErrors, rpcErrors } from '@metamask/rpc-errors';
|
|
18
|
+
import { createDeferredPromise, isValidJson } from '@metamask/utils';
|
|
12
19
|
import { createLogger, getPlatformType, PlatformType, } from '../../../domain';
|
|
13
20
|
import { addValidAccounts, getOptionalScopes, getUniqueRequestId, getValidAccounts, isSameScopesAndAccounts, } from '../../utils';
|
|
14
21
|
import { MULTICHAIN_PROVIDER_STREAM_NAME } from '../constants';
|
|
@@ -49,6 +56,7 @@ export class MWPTransport {
|
|
|
49
56
|
connectionTimeout: DEFAULT_CONNECTION_TIMEOUT,
|
|
50
57
|
resumeTimeout: DEFAULT_RESUME_TIMEOUT,
|
|
51
58
|
}) {
|
|
59
|
+
_MWPTransport_instances.add(this);
|
|
52
60
|
this.dappClient = dappClient;
|
|
53
61
|
this.kvstore = kvstore;
|
|
54
62
|
this.options = options;
|
|
@@ -125,16 +133,44 @@ export class MWPTransport {
|
|
|
125
133
|
if (typeof errorData.code === 'number' &&
|
|
126
134
|
typeof errorData.message === 'string') {
|
|
127
135
|
const { code, message } = errorData;
|
|
136
|
+
// Preserve the wallet's JSON-RPC error `data` (e.g. revert reason bytes /
|
|
137
|
+
// custom-error payloads) so it survives to the RequestRouter and can
|
|
138
|
+
// reach dapps via `error.data`. Without this, the data is dropped here
|
|
139
|
+
// and the normalized `RPCInvokeMethodErr.rpcData` is always unset.
|
|
140
|
+
const rawData = errorData.data;
|
|
141
|
+
const data = isValidJson(rawData) ? rawData : undefined;
|
|
128
142
|
if (code >= 1000 && code <= 4999) {
|
|
129
|
-
return providerErrors.custom({ code, message });
|
|
143
|
+
return providerErrors.custom({ code, message, data });
|
|
130
144
|
}
|
|
131
|
-
return new JsonRpcError(code, message);
|
|
145
|
+
return new JsonRpcError(code, message, data);
|
|
132
146
|
}
|
|
133
147
|
const message = errorPayload instanceof Error
|
|
134
148
|
? errorPayload.message
|
|
135
149
|
: JSON.stringify(errorPayload);
|
|
136
150
|
return rpcErrors.internal({ message });
|
|
137
151
|
}
|
|
152
|
+
getResponseError(messagePayload) {
|
|
153
|
+
if ('error' in messagePayload && messagePayload.error) {
|
|
154
|
+
return messagePayload.error;
|
|
155
|
+
}
|
|
156
|
+
const { result } = messagePayload;
|
|
157
|
+
if (typeof result === 'object' &&
|
|
158
|
+
result !== null &&
|
|
159
|
+
'error' in result &&
|
|
160
|
+
result.error &&
|
|
161
|
+
this.isErrorPayload(result.error)) {
|
|
162
|
+
return result.error;
|
|
163
|
+
}
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
isErrorPayload(errorPayload) {
|
|
167
|
+
if (errorPayload instanceof Error) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
const errorData = errorPayload;
|
|
171
|
+
return (typeof (errorData === null || errorData === void 0 ? void 0 : errorData.code) === 'number' &&
|
|
172
|
+
typeof (errorData === null || errorData === void 0 ? void 0 : errorData.message) === 'string');
|
|
173
|
+
}
|
|
138
174
|
handleMessage(message) {
|
|
139
175
|
if (typeof message === 'object' && message !== null) {
|
|
140
176
|
if ('data' in message) {
|
|
@@ -143,10 +179,10 @@ export class MWPTransport {
|
|
|
143
179
|
const request = this.pendingRequests.get(messagePayload.id);
|
|
144
180
|
if (request) {
|
|
145
181
|
clearTimeout(request.timeout);
|
|
146
|
-
|
|
147
|
-
if (
|
|
182
|
+
const responseError = this.getResponseError(messagePayload);
|
|
183
|
+
if (responseError) {
|
|
148
184
|
this.pendingRequests.delete(messagePayload.id);
|
|
149
|
-
request.reject(this.parseWalletError(
|
|
185
|
+
request.reject(this.parseWalletError(responseError));
|
|
150
186
|
return;
|
|
151
187
|
}
|
|
152
188
|
// Success case - resolve the promise
|
|
@@ -187,67 +223,6 @@ export class MWPTransport {
|
|
|
187
223
|
}
|
|
188
224
|
}
|
|
189
225
|
}
|
|
190
|
-
onResumeSuccess(resumeResolve, resumeReject, options) {
|
|
191
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
192
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
193
|
-
try {
|
|
194
|
-
yield this.waitForWalletSessionIfNotCached();
|
|
195
|
-
const sessionRequest = yield this.request({
|
|
196
|
-
method: 'wallet_getSession',
|
|
197
|
-
});
|
|
198
|
-
// TODO: verify if this branching logic can ever be hit
|
|
199
|
-
if (sessionRequest.error) {
|
|
200
|
-
return resumeReject(new Error(sessionRequest.error.message));
|
|
201
|
-
}
|
|
202
|
-
let walletSession = sessionRequest.result;
|
|
203
|
-
if (walletSession && options) {
|
|
204
|
-
const currentScopes = Object.keys((_a = walletSession === null || walletSession === void 0 ? void 0 : walletSession.sessionScopes) !== null && _a !== void 0 ? _a : {});
|
|
205
|
-
const proposedScopes = (_b = options === null || options === void 0 ? void 0 : options.scopes) !== null && _b !== void 0 ? _b : [];
|
|
206
|
-
const proposedCaipAccountIds = (_c = options === null || options === void 0 ? void 0 : options.caipAccountIds) !== null && _c !== void 0 ? _c : [];
|
|
207
|
-
const hasSameScopesAndAccounts = isSameScopesAndAccounts(currentScopes, proposedScopes, walletSession, proposedCaipAccountIds);
|
|
208
|
-
if (options.forceRequest || !hasSameScopesAndAccounts) {
|
|
209
|
-
const optionalScopes = addValidAccounts(getOptionalScopes((_d = options === null || options === void 0 ? void 0 : options.scopes) !== null && _d !== void 0 ? _d : []), getValidAccounts((_e = options === null || options === void 0 ? void 0 : options.caipAccountIds) !== null && _e !== void 0 ? _e : []));
|
|
210
|
-
const sessionRequest = {
|
|
211
|
-
optionalScopes,
|
|
212
|
-
};
|
|
213
|
-
const response = yield this.request({
|
|
214
|
-
method: 'wallet_createSession',
|
|
215
|
-
params: sessionRequest,
|
|
216
|
-
});
|
|
217
|
-
if (response.error) {
|
|
218
|
-
return resumeReject(new Error(response.error.message));
|
|
219
|
-
}
|
|
220
|
-
// TODO: Maybe find a better way to revoke sessions on wallet without triggering an empty notification
|
|
221
|
-
// Issue of this is it will send a session update event with an empty session and right after we may get the session recovered
|
|
222
|
-
// await this.request({ method: 'wallet_revokeSession', params: walletSession });
|
|
223
|
-
walletSession = response.result;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
else if (!walletSession) {
|
|
227
|
-
// TODO: verify if this branching logic can ever be hit
|
|
228
|
-
const optionalScopes = addValidAccounts(getOptionalScopes((_f = options === null || options === void 0 ? void 0 : options.scopes) !== null && _f !== void 0 ? _f : []), getValidAccounts((_g = options === null || options === void 0 ? void 0 : options.caipAccountIds) !== null && _g !== void 0 ? _g : []));
|
|
229
|
-
const sessionRequest = { optionalScopes };
|
|
230
|
-
const response = yield this.request({
|
|
231
|
-
method: 'wallet_createSession',
|
|
232
|
-
params: sessionRequest,
|
|
233
|
-
});
|
|
234
|
-
if (response.error) {
|
|
235
|
-
return resumeReject(new Error(response.error.message));
|
|
236
|
-
}
|
|
237
|
-
walletSession = response.result;
|
|
238
|
-
}
|
|
239
|
-
yield this.removeStoredPendingSessionRequest();
|
|
240
|
-
this.notifyCallbacks({
|
|
241
|
-
method: 'wallet_sessionChanged',
|
|
242
|
-
params: walletSession,
|
|
243
|
-
});
|
|
244
|
-
return resumeResolve();
|
|
245
|
-
}
|
|
246
|
-
catch (err) {
|
|
247
|
-
return resumeReject(err);
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
|
-
}
|
|
251
226
|
init() {
|
|
252
227
|
return __awaiter(this, void 0, void 0, function* () {
|
|
253
228
|
// no-op for MWP — passive init is only relevant for DefaultTransport
|
|
@@ -288,7 +263,6 @@ export class MWPTransport {
|
|
|
288
263
|
}
|
|
289
264
|
connect(options) {
|
|
290
265
|
return __awaiter(this, void 0, void 0, function* () {
|
|
291
|
-
const { dappClient } = this;
|
|
292
266
|
const session = yield this.getActiveSession();
|
|
293
267
|
if (session) {
|
|
294
268
|
logger('active session found', {
|
|
@@ -297,103 +271,10 @@ export class MWPTransport {
|
|
|
297
271
|
expiresAt: session.expiresAt,
|
|
298
272
|
});
|
|
299
273
|
}
|
|
300
|
-
const
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
let connection;
|
|
305
|
-
if (session) {
|
|
306
|
-
connection = new Promise((resumeResolve, resumeReject) => {
|
|
307
|
-
var _a;
|
|
308
|
-
if (this.dappClient.state === 'CONNECTED') {
|
|
309
|
-
this.onResumeSuccess(resumeResolve, resumeReject, options);
|
|
310
|
-
}
|
|
311
|
-
else {
|
|
312
|
-
this.dappClient.once('connected', () => __awaiter(this, void 0, void 0, function* () {
|
|
313
|
-
this.onResumeSuccess(resumeResolve, resumeReject, options);
|
|
314
|
-
}));
|
|
315
|
-
dappClient.resume((_a = session === null || session === void 0 ? void 0 : session.id) !== null && _a !== void 0 ? _a : '');
|
|
316
|
-
}
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
else {
|
|
320
|
-
connection = new Promise((resolveConnection, rejectConnection) => {
|
|
321
|
-
var _a, _b;
|
|
322
|
-
const optionalScopes = addValidAccounts(getOptionalScopes((_a = options === null || options === void 0 ? void 0 : options.scopes) !== null && _a !== void 0 ? _a : []), getValidAccounts((_b = options === null || options === void 0 ? void 0 : options.caipAccountIds) !== null && _b !== void 0 ? _b : []));
|
|
323
|
-
const sessionRequest = {
|
|
324
|
-
optionalScopes,
|
|
325
|
-
sessionProperties: options === null || options === void 0 ? void 0 : options.sessionProperties,
|
|
326
|
-
};
|
|
327
|
-
const request = {
|
|
328
|
-
jsonrpc: '2.0',
|
|
329
|
-
id: String(getUniqueRequestId()),
|
|
330
|
-
method: 'wallet_createSession',
|
|
331
|
-
params: sessionRequest,
|
|
332
|
-
};
|
|
333
|
-
// Handler for initial connection messages - checks for error responses
|
|
334
|
-
// and properly rejects the connection promise with EIP-1193 error codes
|
|
335
|
-
initialConnectionMessageHandler = (message) => __awaiter(this, void 0, void 0, function* () {
|
|
336
|
-
if (typeof message !== 'object' || message === null) {
|
|
337
|
-
return;
|
|
338
|
-
}
|
|
339
|
-
if (!('data' in message)) {
|
|
340
|
-
return;
|
|
341
|
-
}
|
|
342
|
-
const messagePayload = message.data;
|
|
343
|
-
// Match by ID (preferred) or by method (backward compatibility for notifications without ID)
|
|
344
|
-
const isMatchingId = messagePayload.id === request.id;
|
|
345
|
-
const isMatchingMethod = messagePayload.method === 'wallet_createSession' ||
|
|
346
|
-
messagePayload.method === 'wallet_sessionChanged';
|
|
347
|
-
if (!isMatchingId && !isMatchingMethod) {
|
|
348
|
-
return;
|
|
349
|
-
}
|
|
350
|
-
// Handle error response (e.g., user rejected the connection)
|
|
351
|
-
if (messagePayload.error) {
|
|
352
|
-
return rejectConnection(this.parseWalletError(messagePayload.error));
|
|
353
|
-
}
|
|
354
|
-
// Success case - store session, notify, and resolve
|
|
355
|
-
yield this.storeWalletSession(request, messagePayload);
|
|
356
|
-
yield this.removeStoredPendingSessionRequest();
|
|
357
|
-
this.notifyCallbacks(messagePayload);
|
|
358
|
-
return resolveConnection();
|
|
359
|
-
});
|
|
360
|
-
this.dappClient.on('message', initialConnectionMessageHandler);
|
|
361
|
-
const platformType = getPlatformType();
|
|
362
|
-
const isQRCodeFlow = [
|
|
363
|
-
PlatformType.DesktopWeb,
|
|
364
|
-
PlatformType.NonBrowser,
|
|
365
|
-
].includes(platformType);
|
|
366
|
-
const initialPayload = {
|
|
367
|
-
name: MULTICHAIN_PROVIDER_STREAM_NAME,
|
|
368
|
-
data: request,
|
|
369
|
-
};
|
|
370
|
-
dappClient
|
|
371
|
-
.connect({
|
|
372
|
-
mode: 'trusted',
|
|
373
|
-
initialPayload: isQRCodeFlow ? undefined : initialPayload,
|
|
374
|
-
})
|
|
375
|
-
.then(() => __awaiter(this, void 0, void 0, function* () {
|
|
376
|
-
if (isQRCodeFlow) {
|
|
377
|
-
return dappClient.sendRequest(initialPayload);
|
|
378
|
-
}
|
|
379
|
-
return undefined;
|
|
380
|
-
}))
|
|
381
|
-
.catch((error) => {
|
|
382
|
-
if (initialConnectionMessageHandler) {
|
|
383
|
-
this.dappClient.off('message', initialConnectionMessageHandler);
|
|
384
|
-
}
|
|
385
|
-
rejectConnection(error);
|
|
386
|
-
});
|
|
387
|
-
});
|
|
388
|
-
}
|
|
389
|
-
timeout = setTimeout(() => {
|
|
390
|
-
reject(new TransportTimeoutError());
|
|
391
|
-
}, storedSessionRequestBeforeConnectionAttempt
|
|
392
|
-
? this.options.resumeTimeout
|
|
393
|
-
: this.options.connectionTimeout);
|
|
394
|
-
connection.then(resolve).catch(reject);
|
|
395
|
-
}));
|
|
396
|
-
return connectionPromise
|
|
274
|
+
const connection = session
|
|
275
|
+
? __classPrivateFieldGet(this, _MWPTransport_instances, "m", _MWPTransport_resumeSession).call(this, session, options)
|
|
276
|
+
: __classPrivateFieldGet(this, _MWPTransport_instances, "m", _MWPTransport_startSession).call(this, options);
|
|
277
|
+
return connection
|
|
397
278
|
.catch((error) => __awaiter(this, void 0, void 0, function* () {
|
|
398
279
|
// Clean up the MWP session from the KVStore so stale sessions
|
|
399
280
|
// don't cause subsequent connect attempts to enter the resume path
|
|
@@ -401,13 +282,6 @@ export class MWPTransport {
|
|
|
401
282
|
throw error;
|
|
402
283
|
}))
|
|
403
284
|
.finally(() => {
|
|
404
|
-
if (timeout) {
|
|
405
|
-
clearTimeout(timeout);
|
|
406
|
-
}
|
|
407
|
-
if (initialConnectionMessageHandler) {
|
|
408
|
-
this.dappClient.off('message', initialConnectionMessageHandler);
|
|
409
|
-
initialConnectionMessageHandler = undefined;
|
|
410
|
-
}
|
|
411
285
|
this.removeStoredPendingSessionRequest();
|
|
412
286
|
});
|
|
413
287
|
});
|
|
@@ -475,7 +349,6 @@ export class MWPTransport {
|
|
|
475
349
|
* @returns True if transport is connected, false otherwise
|
|
476
350
|
*/
|
|
477
351
|
isConnected() {
|
|
478
|
-
// biome-ignore lint/suspicious/noExplicitAny: required if state is not made public in dappClient
|
|
479
352
|
return this.dappClient.state === 'CONNECTED';
|
|
480
353
|
}
|
|
481
354
|
/**
|
|
@@ -668,4 +541,194 @@ export class MWPTransport {
|
|
|
668
541
|
});
|
|
669
542
|
}
|
|
670
543
|
}
|
|
544
|
+
_MWPTransport_instances = new WeakSet(), _MWPTransport_onResumeHandler = function _MWPTransport_onResumeHandler(options) {
|
|
545
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
546
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
547
|
+
yield this.waitForWalletSessionIfNotCached();
|
|
548
|
+
const sessionResponse = yield this.request({ method: 'wallet_getSession' });
|
|
549
|
+
if (sessionResponse.error) {
|
|
550
|
+
throw new Error(sessionResponse.error.message);
|
|
551
|
+
}
|
|
552
|
+
let walletSession = sessionResponse.result;
|
|
553
|
+
if (walletSession && options) {
|
|
554
|
+
const currentScopes = Object.keys((_a = walletSession === null || walletSession === void 0 ? void 0 : walletSession.sessionScopes) !== null && _a !== void 0 ? _a : {});
|
|
555
|
+
const proposedScopes = (_b = options === null || options === void 0 ? void 0 : options.scopes) !== null && _b !== void 0 ? _b : [];
|
|
556
|
+
const proposedCaipAccountIds = (_c = options === null || options === void 0 ? void 0 : options.caipAccountIds) !== null && _c !== void 0 ? _c : [];
|
|
557
|
+
const hasSameScopesAndAccounts = isSameScopesAndAccounts(currentScopes, proposedScopes, walletSession, proposedCaipAccountIds);
|
|
558
|
+
if (options.forceRequest || !hasSameScopesAndAccounts) {
|
|
559
|
+
const optionalScopes = addValidAccounts(getOptionalScopes((_d = options === null || options === void 0 ? void 0 : options.scopes) !== null && _d !== void 0 ? _d : []), getValidAccounts((_e = options === null || options === void 0 ? void 0 : options.caipAccountIds) !== null && _e !== void 0 ? _e : []));
|
|
560
|
+
const sessionRequest = {
|
|
561
|
+
optionalScopes,
|
|
562
|
+
};
|
|
563
|
+
const response = yield this.request({
|
|
564
|
+
method: 'wallet_createSession',
|
|
565
|
+
params: sessionRequest,
|
|
566
|
+
});
|
|
567
|
+
if (response.error) {
|
|
568
|
+
throw new Error(response.error.message);
|
|
569
|
+
}
|
|
570
|
+
// TODO: Maybe find a better way to revoke sessions on wallet without triggering an empty notification
|
|
571
|
+
// Issue of this is it will send a session update event with an empty session and right after we may get the session recovered
|
|
572
|
+
// await this.request({ method: 'wallet_revokeSession', params: walletSession });
|
|
573
|
+
walletSession = response.result;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
else if (!walletSession) {
|
|
577
|
+
// Hitting this branch implies that the MWP session was established,
|
|
578
|
+
// but the user has not yet accepted the initial wallet_createSession approval,
|
|
579
|
+
// but the page has refreshed and we've lost that previous context and so we
|
|
580
|
+
// are trying to recover by making a new wallet_createSession request.
|
|
581
|
+
const optionalScopes = addValidAccounts(getOptionalScopes((_f = options === null || options === void 0 ? void 0 : options.scopes) !== null && _f !== void 0 ? _f : []), getValidAccounts((_g = options === null || options === void 0 ? void 0 : options.caipAccountIds) !== null && _g !== void 0 ? _g : []));
|
|
582
|
+
const sessionRequest = {
|
|
583
|
+
optionalScopes,
|
|
584
|
+
};
|
|
585
|
+
const response = yield this.request({
|
|
586
|
+
method: 'wallet_createSession',
|
|
587
|
+
params: sessionRequest,
|
|
588
|
+
});
|
|
589
|
+
if (response.error) {
|
|
590
|
+
throw new Error(response.error.message);
|
|
591
|
+
}
|
|
592
|
+
walletSession = response.result;
|
|
593
|
+
}
|
|
594
|
+
yield this.removeStoredPendingSessionRequest();
|
|
595
|
+
this.notifyCallbacks({
|
|
596
|
+
method: 'wallet_sessionChanged',
|
|
597
|
+
params: walletSession,
|
|
598
|
+
});
|
|
599
|
+
});
|
|
600
|
+
}, _MWPTransport_resumeSession = function _MWPTransport_resumeSession(session, options) {
|
|
601
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
602
|
+
var _a;
|
|
603
|
+
// Captured before any work begins so that a `session_request` event fired
|
|
604
|
+
// during resume (e.g. when the resume path falls through to
|
|
605
|
+
// `wallet_createSession`) doesn't skew the timeout decision.
|
|
606
|
+
const isContinuingPriorAttempt = (yield this.getStoredPendingSessionRequest()) !== null;
|
|
607
|
+
const resumeDeferred = createDeferredPromise();
|
|
608
|
+
const runOnResumeHandler = () => __awaiter(this, void 0, void 0, function* () {
|
|
609
|
+
try {
|
|
610
|
+
resumeDeferred.resolve(yield __classPrivateFieldGet(this, _MWPTransport_instances, "m", _MWPTransport_onResumeHandler).call(this, options));
|
|
611
|
+
}
|
|
612
|
+
catch (err) {
|
|
613
|
+
resumeDeferred.reject(err);
|
|
614
|
+
}
|
|
615
|
+
});
|
|
616
|
+
if (this.dappClient.state === 'CONNECTED') {
|
|
617
|
+
runOnResumeHandler();
|
|
618
|
+
}
|
|
619
|
+
else {
|
|
620
|
+
this.dappClient.once('connected', runOnResumeHandler);
|
|
621
|
+
this.dappClient
|
|
622
|
+
.resume((_a = session.id) !== null && _a !== void 0 ? _a : '')
|
|
623
|
+
.catch((err) => resumeDeferred.reject(err));
|
|
624
|
+
}
|
|
625
|
+
// The resume path can fall through to `wallet_createSession` (forceRequest,
|
|
626
|
+
// recovery from a missing wallet session, or a scope/account change), which
|
|
627
|
+
// requires a human to approve in the wallet. Use the longer
|
|
628
|
+
// `connectionTimeout` for those flows; only use the shorter `resumeTimeout`
|
|
629
|
+
// when we're continuing an in-flight prior attempt.
|
|
630
|
+
const timeoutDeferred = createDeferredPromise();
|
|
631
|
+
const timeout = setTimeout(() => timeoutDeferred.reject(new TransportTimeoutError()), isContinuingPriorAttempt
|
|
632
|
+
? this.options.resumeTimeout
|
|
633
|
+
: this.options.connectionTimeout);
|
|
634
|
+
const cleanup = () => this.dappClient.off('connected', runOnResumeHandler);
|
|
635
|
+
return Promise.race([
|
|
636
|
+
resumeDeferred.promise,
|
|
637
|
+
timeoutDeferred.promise,
|
|
638
|
+
]).finally(() => {
|
|
639
|
+
clearTimeout(timeout);
|
|
640
|
+
cleanup();
|
|
641
|
+
});
|
|
642
|
+
});
|
|
643
|
+
}, _MWPTransport_startSession = function _MWPTransport_startSession(options) {
|
|
644
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
645
|
+
var _a, _b;
|
|
646
|
+
const { dappClient } = this;
|
|
647
|
+
// Captured before any work begins so that the session_request event fired
|
|
648
|
+
// by `dappClient.connect()` (which overwrites the stored value) doesn't
|
|
649
|
+
// skew the timeout decision.
|
|
650
|
+
const isContinuingPriorAttempt = (yield this.getStoredPendingSessionRequest()) !== null;
|
|
651
|
+
const connDeferred = createDeferredPromise();
|
|
652
|
+
const optionalScopes = addValidAccounts(getOptionalScopes((_a = options === null || options === void 0 ? void 0 : options.scopes) !== null && _a !== void 0 ? _a : []), getValidAccounts((_b = options === null || options === void 0 ? void 0 : options.caipAccountIds) !== null && _b !== void 0 ? _b : []));
|
|
653
|
+
const sessionRequest = {
|
|
654
|
+
optionalScopes,
|
|
655
|
+
sessionProperties: options === null || options === void 0 ? void 0 : options.sessionProperties,
|
|
656
|
+
};
|
|
657
|
+
const request = {
|
|
658
|
+
jsonrpc: '2.0',
|
|
659
|
+
id: String(getUniqueRequestId()),
|
|
660
|
+
method: 'wallet_createSession',
|
|
661
|
+
params: sessionRequest,
|
|
662
|
+
};
|
|
663
|
+
let handler;
|
|
664
|
+
const removeHandler = () => {
|
|
665
|
+
if (handler) {
|
|
666
|
+
this.dappClient.off('message', handler);
|
|
667
|
+
handler = undefined;
|
|
668
|
+
}
|
|
669
|
+
};
|
|
670
|
+
// Handler for initial connection messages — checks for error responses
|
|
671
|
+
// and properly rejects the connection promise with EIP-1193 error codes
|
|
672
|
+
handler = (message) => __awaiter(this, void 0, void 0, function* () {
|
|
673
|
+
if (typeof message !== 'object' || message === null) {
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
676
|
+
if (!('data' in message)) {
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
const messagePayload = message.data;
|
|
680
|
+
// Match by ID (preferred) or by method (backward compatibility for notifications without ID)
|
|
681
|
+
const isMatchingId = messagePayload.id === request.id;
|
|
682
|
+
const isMatchingMethod = messagePayload.method === 'wallet_createSession' ||
|
|
683
|
+
messagePayload.method === 'wallet_sessionChanged';
|
|
684
|
+
if (!isMatchingId && !isMatchingMethod) {
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
const responseError = this.getResponseError(messagePayload);
|
|
688
|
+
// Handle error response (e.g., user rejected the connection)
|
|
689
|
+
if (responseError) {
|
|
690
|
+
connDeferred.reject(this.parseWalletError(responseError));
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
// Success case — store session, notify, and resolve
|
|
694
|
+
yield this.storeWalletSession(request, messagePayload);
|
|
695
|
+
yield this.removeStoredPendingSessionRequest();
|
|
696
|
+
this.notifyCallbacks(messagePayload);
|
|
697
|
+
connDeferred.resolve();
|
|
698
|
+
});
|
|
699
|
+
this.dappClient.on('message', handler);
|
|
700
|
+
const platformType = getPlatformType();
|
|
701
|
+
const isQRCodeFlow = [
|
|
702
|
+
PlatformType.DesktopWeb,
|
|
703
|
+
PlatformType.NonBrowser,
|
|
704
|
+
].includes(platformType);
|
|
705
|
+
const initialPayload = {
|
|
706
|
+
name: MULTICHAIN_PROVIDER_STREAM_NAME,
|
|
707
|
+
data: request,
|
|
708
|
+
};
|
|
709
|
+
dappClient
|
|
710
|
+
.connect({
|
|
711
|
+
mode: 'trusted',
|
|
712
|
+
initialPayload: isQRCodeFlow ? undefined : initialPayload,
|
|
713
|
+
})
|
|
714
|
+
.then(() => __awaiter(this, void 0, void 0, function* () {
|
|
715
|
+
if (isQRCodeFlow) {
|
|
716
|
+
return dappClient.sendRequest(initialPayload);
|
|
717
|
+
}
|
|
718
|
+
return undefined;
|
|
719
|
+
}))
|
|
720
|
+
.catch((error) => connDeferred.reject(error));
|
|
721
|
+
const timeoutDeferred = createDeferredPromise();
|
|
722
|
+
const timeout = setTimeout(() => timeoutDeferred.reject(new TransportTimeoutError()), isContinuingPriorAttempt
|
|
723
|
+
? this.options.resumeTimeout
|
|
724
|
+
: this.options.connectionTimeout);
|
|
725
|
+
return Promise.race([
|
|
726
|
+
connDeferred.promise,
|
|
727
|
+
timeoutDeferred.promise,
|
|
728
|
+
]).finally(() => {
|
|
729
|
+
clearTimeout(timeout);
|
|
730
|
+
removeHandler();
|
|
731
|
+
});
|
|
732
|
+
});
|
|
733
|
+
};
|
|
671
734
|
//# sourceMappingURL=index.js.map
|