web-dc-api 0.0.40 → 0.0.41
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/dc.min.js +2 -2
- package/dist/index.cjs.js +1 -1
- package/dist/index.d.ts +132 -24
- package/dist/index.esm.js +1 -1
- package/lib/common/define.ts +32 -22
- package/lib/common/module-system.ts +1 -0
- package/lib/common/service-worker.ts +2 -1
- package/lib/common/types/types.ts +7 -1
- package/lib/dc.ts +74 -17
- package/lib/implements/aiproxy/client.ts +17 -13
- package/lib/implements/file/client.ts +8 -8
- package/lib/implements/file/manager.ts +6 -2
- package/lib/implements/threaddb/core/logstore.ts +4 -4
- package/lib/implements/threaddb/lsstoreds/metadata.ts +23 -22
- package/lib/implements/threaddb/net/net.ts +0 -1
- package/lib/implements/wallet/manager.ts +287 -160
- package/lib/index.ts +15 -3
- package/lib/interfaces/file-interface.ts +1 -1
- package/lib/interfaces/util-interface.ts +1 -1
- package/lib/modules/aiproxy-module.ts +40 -15
- package/lib/modules/auth-module.ts +6 -6
- package/lib/modules/database-module.ts +51 -3
- package/lib/modules/file-module.ts +12 -7
- package/package.json +1 -1
|
@@ -1,14 +1,19 @@
|
|
|
1
|
-
|
|
2
1
|
import { walletOrigin, walletUrl, walletWindowName } from "../../common/define";
|
|
3
2
|
import { DCContext } from "../../../lib/interfaces/DCContext";
|
|
4
3
|
import { Ed25519PubKey } from "../../common/dc-key/ed25519";
|
|
5
|
-
import type {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
import type {
|
|
5
|
+
Account,
|
|
6
|
+
EIP712SignReqMessage,
|
|
7
|
+
SendMessage,
|
|
8
|
+
SignReqMessage,
|
|
9
|
+
SignResponseMessage,
|
|
10
|
+
} from "../../common/types/types";
|
|
10
11
|
|
|
12
|
+
const appOrigin = typeof window !== "undefined" ? window.location.origin : ""; //"http://localhost:3002"
|
|
13
|
+
const appUrl = typeof window !== "undefined" ? window.location.href : "";
|
|
11
14
|
|
|
15
|
+
const localStorageKey_dcwallet_opener = "dcwallet_opener";
|
|
16
|
+
const timeout = 30000;
|
|
12
17
|
// 错误定义
|
|
13
18
|
export class WalletError extends Error {
|
|
14
19
|
constructor(message: string) {
|
|
@@ -16,27 +21,28 @@ export class WalletError extends Error {
|
|
|
16
21
|
this.name = "WalletError";
|
|
17
22
|
}
|
|
18
23
|
}
|
|
19
|
-
export const Errors = {
|
|
20
|
-
};
|
|
24
|
+
export const Errors = {};
|
|
21
25
|
|
|
22
26
|
export class WalletManager {
|
|
23
27
|
private context: DCContext;
|
|
24
28
|
private walletWindow: Window | null = null;
|
|
25
|
-
private iframeId: string
|
|
26
|
-
private walletIframeId: string =
|
|
29
|
+
private iframeId: string = "dcIframeId";
|
|
30
|
+
private walletIframeId: string = "dcWalletIframeId";
|
|
27
31
|
private channelPort2: MessagePort | null = null;
|
|
28
32
|
constructor(context: DCContext) {
|
|
29
33
|
this.context = context;
|
|
30
34
|
}
|
|
31
35
|
|
|
32
|
-
async init():
|
|
36
|
+
async init(): Promise<boolean> {
|
|
33
37
|
console.log("========init walletManager", appOrigin, walletOrigin);
|
|
34
|
-
|
|
38
|
+
const walletOpenFlag = typeof globalThis !== "undefined" && typeof (globalThis as any).walletOpenType !== "undefined" ? true : false // 用于判断是否是直接打开;
|
|
39
|
+
if (walletOpenFlag || appOrigin.indexOf(walletOrigin) === -1) {
|
|
35
40
|
return new Promise((resolve, reject) => {
|
|
36
41
|
// html添加iframe标签,id是dcWalletIframe
|
|
37
42
|
const iframe = document.createElement("iframe");
|
|
38
43
|
iframe.id = this.iframeId;
|
|
39
44
|
iframe.src = `${walletUrl}/iframe?parentOrigin=${appOrigin}`;
|
|
45
|
+
(iframe as any).credentialless = true;
|
|
40
46
|
iframe.onload = async () => {
|
|
41
47
|
const bool = await this.initConfig(this);
|
|
42
48
|
resolve(bool);
|
|
@@ -47,34 +53,35 @@ export class WalletManager {
|
|
|
47
53
|
this.listenFromWallet(event);
|
|
48
54
|
});
|
|
49
55
|
});
|
|
50
|
-
}else {
|
|
56
|
+
} else {
|
|
51
57
|
return true;
|
|
52
58
|
}
|
|
53
59
|
}
|
|
54
60
|
// iframe加载完成后,发送初始化配置
|
|
55
|
-
async initConfig
|
|
56
|
-
return new Promise((resolve, reject) => {
|
|
61
|
+
async initConfig(that: WalletManager): Promise<boolean> {
|
|
62
|
+
return new Promise((resolve, reject) => {
|
|
57
63
|
const message = {
|
|
58
64
|
type: "init",
|
|
59
65
|
data: {
|
|
60
66
|
appId: this.context.appInfo.appId,
|
|
61
|
-
appName: this.context.appInfo.appName,
|
|
62
|
-
appIcon: this.context.appInfo.appIcon,
|
|
67
|
+
appName: this.context.appInfo.appName,
|
|
68
|
+
appIcon: this.context.appInfo.appIcon,
|
|
63
69
|
appVersion: this.context.appInfo.appVersion,
|
|
64
70
|
appUrl: appUrl,
|
|
65
71
|
},
|
|
66
72
|
};
|
|
67
|
-
that
|
|
73
|
+
that
|
|
74
|
+
.sendMessageToIframe(message, 5000 * 10)
|
|
68
75
|
.then((response) => {
|
|
69
76
|
console.log("initConfig response", response);
|
|
70
|
-
if(!response || !response.data || !response.data.data) {
|
|
77
|
+
if (!response || !response.data || !response.data.data) {
|
|
71
78
|
console.error("initConfig response is null");
|
|
72
79
|
resolve(false);
|
|
73
80
|
return;
|
|
74
81
|
}
|
|
75
82
|
const data = response.data?.data;
|
|
76
83
|
const messageData = data.message;
|
|
77
|
-
if(data.success === false || !messageData.publicKey) {
|
|
84
|
+
if (data.success === false || !messageData.publicKey) {
|
|
78
85
|
console.error("initConfig error", message);
|
|
79
86
|
resolve(false);
|
|
80
87
|
return;
|
|
@@ -87,28 +94,128 @@ export class WalletManager {
|
|
|
87
94
|
resolve(false);
|
|
88
95
|
});
|
|
89
96
|
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
//等待钱包页面加载完成
|
|
100
|
+
waitForWalletLoaded = async (
|
|
101
|
+
walletWindow: Window | null,
|
|
102
|
+
timeout: number
|
|
103
|
+
) => {
|
|
104
|
+
// localStorage中获取是否支持window.opener
|
|
105
|
+
const openerFlag = localStorage.getItem(localStorageKey_dcwallet_opener);
|
|
106
|
+
let waitTimeCount = 1;
|
|
107
|
+
if (openerFlag == "true") {
|
|
108
|
+
waitTimeCount = 3;
|
|
109
|
+
}
|
|
110
|
+
// 开启定时器500ms检查一次,第一次等待1.5秒,如果没有加载完成,则发送轮询请求
|
|
111
|
+
return new Promise((resolve) => {
|
|
112
|
+
let messageChannel = new MessageChannel();
|
|
113
|
+
const onMessage = (event: MessageEvent) => {
|
|
114
|
+
const message = event.data;
|
|
115
|
+
if (message.type === "walletLoaded") {
|
|
116
|
+
clearInterval(interval);
|
|
117
|
+
clearTimeout(timeoutHandle);
|
|
118
|
+
messageChannel.port1.close();
|
|
119
|
+
window.removeEventListener("message", listenForWalletLoaded);
|
|
120
|
+
resolve(true);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
messageChannel.port1.onmessage = onMessage;
|
|
124
|
+
const checkMessage = {
|
|
125
|
+
type: "checkWalletLoaded",
|
|
126
|
+
origin: appOrigin,
|
|
127
|
+
};
|
|
128
|
+
let walletLoadedFlag = false;
|
|
129
|
+
const listenForWalletLoaded = (event: MessageEvent) => {
|
|
130
|
+
//判断消息来源
|
|
131
|
+
if (event.origin !== walletOrigin) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (event.source != null && event.source != walletWindow) {
|
|
135
|
+
//非当前操作打开的窗口
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const data = event.data;
|
|
139
|
+
if (!data.type) {
|
|
140
|
+
//非钱包插件
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (data.type === "walletLoaded") {
|
|
144
|
+
//钱包加载完成
|
|
145
|
+
walletLoadedFlag = true;
|
|
146
|
+
localStorage.setItem(localStorageKey_dcwallet_opener, "true");
|
|
147
|
+
clearInterval(interval);
|
|
148
|
+
clearTimeout(timeoutHandle);
|
|
149
|
+
messageChannel.port1.close();
|
|
150
|
+
window.removeEventListener("message", listenForWalletLoaded);
|
|
151
|
+
resolve(true);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
//添加监听事件
|
|
156
|
+
window.addEventListener("message", listenForWalletLoaded);
|
|
157
|
+
const interval = setInterval(() => {
|
|
158
|
+
if (walletLoadedFlag) {
|
|
159
|
+
clearInterval(interval);
|
|
160
|
+
messageChannel.port1.close();
|
|
161
|
+
} else {
|
|
162
|
+
if (waitTimeCount >= 0) {
|
|
163
|
+
waitTimeCount--;
|
|
164
|
+
} else {
|
|
165
|
+
try {
|
|
166
|
+
walletWindow?.postMessage(checkMessage, walletOrigin, [
|
|
167
|
+
messageChannel.port2,
|
|
168
|
+
]);
|
|
169
|
+
} catch (e) {
|
|
170
|
+
//不做处理
|
|
171
|
+
// 提示错误
|
|
172
|
+
console.log("错误", e);
|
|
173
|
+
if (messageChannel) {
|
|
174
|
+
messageChannel.port1.close();
|
|
175
|
+
}
|
|
176
|
+
messageChannel = new MessageChannel();
|
|
177
|
+
messageChannel.port1.onmessage = onMessage;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}, 500);
|
|
182
|
+
//添加超时处理
|
|
183
|
+
const timeoutHandle = setTimeout(() => {
|
|
184
|
+
clearInterval(interval);
|
|
185
|
+
messageChannel.port1.close();
|
|
186
|
+
window.removeEventListener("message", listenForWalletLoaded);
|
|
187
|
+
resolve(false);
|
|
188
|
+
}, timeout);
|
|
189
|
+
});
|
|
90
190
|
};
|
|
91
191
|
|
|
92
|
-
//
|
|
93
|
-
private
|
|
192
|
+
// 判断是否iframe打开钱包
|
|
193
|
+
private isIframeOpen = (): boolean => {
|
|
194
|
+
const walletOpenType = typeof globalThis !== "undefined" ? (globalThis as any).walletOpenType : '' // 用于判断是否是直接打开;
|
|
195
|
+
if(walletOpenType == 'iframe') {
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
94
198
|
const ua = navigator.userAgent.toLowerCase();
|
|
95
199
|
return ua.indexOf("micromessenger") !== -1;
|
|
96
|
-
//
|
|
97
|
-
// return
|
|
200
|
+
// todo 临时测试
|
|
201
|
+
// return true
|
|
98
202
|
};
|
|
99
203
|
|
|
100
204
|
// 打开钱包iframe窗口
|
|
101
205
|
async openWalletIframe(): Promise<boolean> {
|
|
102
|
-
return new Promise((resolve, reject) => {
|
|
103
|
-
const walletIframe = document.getElementById(
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
206
|
+
return new Promise((resolve, reject) => {
|
|
207
|
+
const walletIframe = document.getElementById(
|
|
208
|
+
this.walletIframeId
|
|
209
|
+
) as HTMLIFrameElement;
|
|
210
|
+
if (walletIframe) {
|
|
211
|
+
resolve(true);
|
|
212
|
+
return;
|
|
107
213
|
}
|
|
108
214
|
// html添加iframe标签,id是dcWalletIframe
|
|
109
215
|
const iframe = document.createElement("iframe");
|
|
110
216
|
iframe.id = this.walletIframeId;
|
|
111
217
|
iframe.src = `${walletUrl}?origin=${appOrigin}`;
|
|
218
|
+
(iframe as any).credentialless = true;
|
|
112
219
|
iframe.onload = async () => {
|
|
113
220
|
resolve(true);
|
|
114
221
|
};
|
|
@@ -116,10 +223,12 @@ export class WalletManager {
|
|
|
116
223
|
console.error("openWallet error", error);
|
|
117
224
|
resolve(false);
|
|
118
225
|
};
|
|
119
|
-
iframe.sandbox
|
|
226
|
+
iframe.setAttribute('sandbox', 'allow-scripts allow-forms allow-same-origin');
|
|
227
|
+
|
|
228
|
+
// iframe.sandbox = "allow-scripts allow-forms allow-same-origin";
|
|
120
229
|
// 直接设置 iframe 的样式以覆盖整个页面
|
|
121
230
|
// 最大可能的 z-index
|
|
122
|
-
iframe.style = `
|
|
231
|
+
iframe.style.cssText = `
|
|
123
232
|
z-index: 2147483647;
|
|
124
233
|
position: fixed;
|
|
125
234
|
top: 0;
|
|
@@ -135,26 +244,28 @@ export class WalletManager {
|
|
|
135
244
|
isolation: isolate;
|
|
136
245
|
transform: translateZ(0);
|
|
137
246
|
`;
|
|
138
|
-
iframe.allow =
|
|
247
|
+
iframe.allow =
|
|
248
|
+
"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture";
|
|
139
249
|
iframe.allowFullscreen = true;
|
|
140
250
|
document.body.appendChild(iframe);
|
|
141
251
|
});
|
|
142
252
|
}
|
|
143
253
|
|
|
144
254
|
async removeWalletIframe() {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
255
|
+
const walletIframe = document.getElementById(
|
|
256
|
+
this.walletIframeId
|
|
257
|
+
) as HTMLIFrameElement;
|
|
258
|
+
if (walletIframe) {
|
|
259
|
+
document.body.removeChild(walletIframe);
|
|
260
|
+
}
|
|
149
261
|
}
|
|
150
262
|
|
|
151
|
-
|
|
152
263
|
async openConnect(): Promise<Account> {
|
|
153
264
|
return new Promise(async (resolve, reject) => {
|
|
154
|
-
if (this.
|
|
265
|
+
if (this.isIframeOpen()) {
|
|
155
266
|
// 微信窗口
|
|
156
267
|
const bool = await this.openWalletIframe();
|
|
157
|
-
if(!bool) {
|
|
268
|
+
if (!bool) {
|
|
158
269
|
console.error("openWalletIframe error");
|
|
159
270
|
reject(new WalletError("openWalletIframe error"));
|
|
160
271
|
return;
|
|
@@ -165,39 +276,43 @@ export class WalletManager {
|
|
|
165
276
|
this.walletWindow = window.open(urlWithOrigin, walletWindowName);
|
|
166
277
|
}
|
|
167
278
|
this.initCommChannel();
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
279
|
+
// this.waitForWalletLoaded(this.walletWindow, timeout).then((flag) => {
|
|
280
|
+
// if (flag) {
|
|
281
|
+
const message = {
|
|
282
|
+
type: "connect",
|
|
283
|
+
data: {
|
|
284
|
+
origin: appOrigin,
|
|
285
|
+
},
|
|
286
|
+
};
|
|
287
|
+
this.sendMessageToIframe(message, 600000)
|
|
288
|
+
.then((response) => {
|
|
289
|
+
console.log("openConnect response", response);
|
|
290
|
+
if (!response || !response.data || !response.data.data) {
|
|
291
|
+
console.error("openConnect response is null");
|
|
292
|
+
reject(new WalletError("openConnect response is null"));
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
const data = response.data?.data;
|
|
296
|
+
const messageData = data.message;
|
|
297
|
+
if (data.success === false || !messageData.appAccount) {
|
|
298
|
+
console.error("openConnect error", message);
|
|
299
|
+
reject(new WalletError("openConnect appAccount is null"));
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
console.log("openConnect success", messageData);
|
|
303
|
+
resolve(messageData);
|
|
304
|
+
})
|
|
305
|
+
.catch((error) => {
|
|
306
|
+
console.error("openConnect error", error);
|
|
307
|
+
reject(new WalletError("openConnect error"));
|
|
308
|
+
});
|
|
309
|
+
// }
|
|
310
|
+
// });
|
|
196
311
|
});
|
|
197
312
|
}
|
|
198
313
|
|
|
199
314
|
// 退出登录 清除iframe中的私钥和公钥
|
|
200
|
-
exitLogin = (): Promise<boolean>
|
|
315
|
+
exitLogin = (): Promise<boolean> => {
|
|
201
316
|
return new Promise((resolve, reject) => {
|
|
202
317
|
// 每100ms发送一次消息,直到钱包加载完成
|
|
203
318
|
const message = {
|
|
@@ -205,14 +320,14 @@ export class WalletManager {
|
|
|
205
320
|
};
|
|
206
321
|
this.sendMessageToIframe(message, 60000)
|
|
207
322
|
.then((response) => {
|
|
208
|
-
if(!response || !response.data || !response.data.data) {
|
|
323
|
+
if (!response || !response.data || !response.data.data) {
|
|
209
324
|
console.error("exitLogin response is null");
|
|
210
325
|
reject(new WalletError("exitLogin response is null"));
|
|
211
326
|
return;
|
|
212
327
|
}
|
|
213
328
|
const data = response.data?.data;
|
|
214
329
|
const messageData = data.message;
|
|
215
|
-
if(data.success === false) {
|
|
330
|
+
if (data.success === false) {
|
|
216
331
|
console.error("exitLogin error", message);
|
|
217
332
|
reject(new WalletError("exitLogin messageData is null"));
|
|
218
333
|
return;
|
|
@@ -226,11 +341,11 @@ export class WalletManager {
|
|
|
226
341
|
});
|
|
227
342
|
};
|
|
228
343
|
|
|
229
|
-
/**
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
344
|
+
/**
|
|
345
|
+
* 用私钥解密数据
|
|
346
|
+
* @param payload 需要解密的数据
|
|
347
|
+
* @returns 解密结果
|
|
348
|
+
*/
|
|
234
349
|
|
|
235
350
|
decrypt = (payload: Uint8Array): Promise<Uint8Array> => {
|
|
236
351
|
return new Promise((resolve, reject) => {
|
|
@@ -243,14 +358,14 @@ export class WalletManager {
|
|
|
243
358
|
this.sendMessageToIframe(message, 60000)
|
|
244
359
|
.then((response) => {
|
|
245
360
|
console.log("decrypt response", response);
|
|
246
|
-
if(!response || !response.data || !response.data.data) {
|
|
361
|
+
if (!response || !response.data || !response.data.data) {
|
|
247
362
|
console.error("decrypt response is null");
|
|
248
363
|
reject(new WalletError("decrypt response is null"));
|
|
249
364
|
return;
|
|
250
365
|
}
|
|
251
366
|
const data = response.data?.data;
|
|
252
367
|
const messageData = data.message;
|
|
253
|
-
if(data.success === false || !messageData) {
|
|
368
|
+
if (data.success === false || !messageData) {
|
|
254
369
|
console.error("decrypt error", message);
|
|
255
370
|
reject(new WalletError("decrypt messageData is null"));
|
|
256
371
|
return;
|
|
@@ -270,7 +385,7 @@ export class WalletManager {
|
|
|
270
385
|
* @param payload 需要签名的数据
|
|
271
386
|
* @returns 签名结果
|
|
272
387
|
*/
|
|
273
|
-
sign = (payload: Uint8Array): Promise<Uint8Array>
|
|
388
|
+
sign = (payload: Uint8Array): Promise<Uint8Array> => {
|
|
274
389
|
return new Promise((resolve, reject) => {
|
|
275
390
|
// 每100ms发送一次消息,直到钱包加载完成
|
|
276
391
|
const message = {
|
|
@@ -281,14 +396,14 @@ export class WalletManager {
|
|
|
281
396
|
};
|
|
282
397
|
this.sendMessageToIframe(message, 60000)
|
|
283
398
|
.then((response) => {
|
|
284
|
-
if(!response || !response.data || !response.data.data) {
|
|
399
|
+
if (!response || !response.data || !response.data.data) {
|
|
285
400
|
console.error("sign response is null");
|
|
286
401
|
reject(new WalletError("sign response is null"));
|
|
287
402
|
return;
|
|
288
403
|
}
|
|
289
404
|
const data = response.data?.data;
|
|
290
405
|
const messageData = data.message;
|
|
291
|
-
if(data.success === false || !messageData) {
|
|
406
|
+
if (data.success === false || !messageData) {
|
|
292
407
|
console.error("sign error", message);
|
|
293
408
|
reject(new WalletError("sign messageData is null"));
|
|
294
409
|
return;
|
|
@@ -302,19 +417,18 @@ export class WalletManager {
|
|
|
302
417
|
});
|
|
303
418
|
};
|
|
304
419
|
|
|
305
|
-
|
|
306
420
|
// 签名普通消息
|
|
307
|
-
async signMessage
|
|
421
|
+
async signMessage(data: SignReqMessage): Promise<SignResponseMessage | null> {
|
|
308
422
|
return new Promise(async (resolve, reject) => {
|
|
309
423
|
if (!this.context) {
|
|
310
424
|
console.log("未连接钱包");
|
|
311
425
|
reject(new WalletError("未连接钱包"));
|
|
312
426
|
return;
|
|
313
427
|
}
|
|
314
|
-
if (this.
|
|
428
|
+
if (this.isIframeOpen()) {
|
|
315
429
|
// 微信窗口
|
|
316
430
|
const bool = await this.openWalletIframe();
|
|
317
|
-
if(!bool) {
|
|
431
|
+
if (!bool) {
|
|
318
432
|
console.error("openWalletIframe error");
|
|
319
433
|
reject(new WalletError("openWalletIframe error"));
|
|
320
434
|
return;
|
|
@@ -325,49 +439,54 @@ export class WalletManager {
|
|
|
325
439
|
this.walletWindow = window.open(urlWithOrigin, walletWindowName);
|
|
326
440
|
}
|
|
327
441
|
this.initCommChannel();
|
|
328
|
-
//
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
442
|
+
// this.waitForWalletLoaded(this.walletWindow, timeout).then((flag) => {
|
|
443
|
+
// if (flag) {
|
|
444
|
+
// 每100ms发送一次消息,直到钱包加载完成
|
|
445
|
+
const message = {
|
|
446
|
+
type: "signMessage",
|
|
447
|
+
data,
|
|
448
|
+
};
|
|
449
|
+
this.sendMessageToIframe(message, 60000)
|
|
450
|
+
.then((response: MessageEvent | null) => {
|
|
451
|
+
console.log("signMessage response", response);
|
|
452
|
+
if (!response || !response.data || !response.data.data) {
|
|
453
|
+
console.error("signMessage response is null");
|
|
454
|
+
reject(new WalletError("signMessage response is null"));
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
const data = response.data?.data;
|
|
458
|
+
const messageData = data.message;
|
|
459
|
+
if (data.success === false || !messageData) {
|
|
460
|
+
console.error("signMessage error", message);
|
|
461
|
+
reject(new WalletError("signMessage messageData is null"));
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
console.log("signMessage success", messageData);
|
|
465
|
+
resolve(messageData);
|
|
466
|
+
})
|
|
467
|
+
.catch((error) => {
|
|
468
|
+
console.error("signMessage error", error);
|
|
469
|
+
reject(error);
|
|
470
|
+
});
|
|
471
|
+
// }
|
|
472
|
+
// });
|
|
473
|
+
});
|
|
474
|
+
}
|
|
358
475
|
|
|
359
476
|
// 签名EIP712消息
|
|
360
|
-
async signEIP712Message
|
|
477
|
+
async signEIP712Message(
|
|
478
|
+
data: EIP712SignReqMessage
|
|
479
|
+
): Promise<SignResponseMessage | null> {
|
|
361
480
|
return new Promise(async (resolve, reject) => {
|
|
362
481
|
if (!this.context) {
|
|
363
482
|
console.log("未连接钱包");
|
|
364
483
|
reject(new WalletError("未连接钱包"));
|
|
365
484
|
return;
|
|
366
485
|
}
|
|
367
|
-
if (this.
|
|
486
|
+
if (this.isIframeOpen()) {
|
|
368
487
|
// 微信窗口
|
|
369
488
|
const bool = await this.openWalletIframe();
|
|
370
|
-
if(!bool) {
|
|
489
|
+
if (!bool) {
|
|
371
490
|
console.error("openWalletIframe error");
|
|
372
491
|
reject(new WalletError("openWalletIframe error"));
|
|
373
492
|
return;
|
|
@@ -378,35 +497,39 @@ export class WalletManager {
|
|
|
378
497
|
this.walletWindow = window.open(urlWithOrigin, walletWindowName);
|
|
379
498
|
}
|
|
380
499
|
this.initCommChannel();
|
|
381
|
-
//
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
500
|
+
// this.waitForWalletLoaded(this.walletWindow, timeout).then((flag) => {
|
|
501
|
+
// if (flag) {
|
|
502
|
+
// port1 转移给iframe
|
|
503
|
+
const message = {
|
|
504
|
+
type: "signEIP712Message",
|
|
505
|
+
data: data,
|
|
506
|
+
};
|
|
507
|
+
this.sendMessageToIframe(message, 60000)
|
|
508
|
+
.then((response) => {
|
|
509
|
+
console.log("signEIP712Message response", response);
|
|
510
|
+
if (!response || !response.data || !response.data.data) {
|
|
511
|
+
console.error("signEIP712Message response is null");
|
|
512
|
+
reject(new WalletError("signEIP712Message response is null"));
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
const data = response.data?.data;
|
|
516
|
+
const messageData = data.message;
|
|
517
|
+
if (data.success === false || !messageData) {
|
|
518
|
+
console.error("signEIP712Message error", message);
|
|
519
|
+
reject(new WalletError("signEIP712Message messageData is null"));
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
console.log("messageData success", messageData);
|
|
523
|
+
resolve(messageData);
|
|
524
|
+
})
|
|
525
|
+
.catch((error) => {
|
|
526
|
+
console.error("signEIP712Message error", error);
|
|
527
|
+
reject(error);
|
|
528
|
+
});
|
|
529
|
+
// }
|
|
530
|
+
// });
|
|
531
|
+
});
|
|
532
|
+
}
|
|
410
533
|
|
|
411
534
|
private async listenFromWallet(event: MessageEvent): Promise<void> {
|
|
412
535
|
// if (event.origin !== "todo来源") return; // 可选:对源进行验证
|
|
@@ -417,9 +540,9 @@ export class WalletManager {
|
|
|
417
540
|
return;
|
|
418
541
|
}
|
|
419
542
|
if (message.type === "walletLoaded") {
|
|
420
|
-
console.log(
|
|
421
|
-
console.log(
|
|
422
|
-
console.log(
|
|
543
|
+
console.log("walletLoaded", message);
|
|
544
|
+
console.log("event.origin", event.origin);
|
|
545
|
+
console.log("walletOrigin", walletOrigin);
|
|
423
546
|
//钱包加载完成
|
|
424
547
|
if (event.origin !== walletOrigin) {
|
|
425
548
|
console.log("来源不匹配", event.origin, walletOrigin);
|
|
@@ -433,23 +556,28 @@ export class WalletManager {
|
|
|
433
556
|
type: "channelPort2",
|
|
434
557
|
origin: appOrigin,
|
|
435
558
|
};
|
|
436
|
-
if (this.walletWindow) {
|
|
559
|
+
if (this.walletWindow) {
|
|
560
|
+
// 如果钱包已经打开
|
|
437
561
|
try {
|
|
438
562
|
this.walletWindow.postMessage(message, walletOrigin, [this.channelPort2]);
|
|
439
563
|
} catch (error) {
|
|
440
564
|
console.error("postMessage error", error);
|
|
441
565
|
}
|
|
442
|
-
}else {
|
|
443
|
-
|
|
566
|
+
} else {
|
|
567
|
+
// 如果钱包iframe已经打开
|
|
568
|
+
const iframe = document.getElementById(
|
|
569
|
+
this.walletIframeId
|
|
570
|
+
) as HTMLIFrameElement;
|
|
444
571
|
// port1转移给iframe
|
|
445
572
|
if (iframe) {
|
|
446
573
|
try {
|
|
447
|
-
iframe.contentWindow?.postMessage(message, walletOrigin, [
|
|
574
|
+
iframe.contentWindow?.postMessage(message, walletOrigin, [
|
|
575
|
+
this.channelPort2,
|
|
576
|
+
]);
|
|
448
577
|
} catch (error) {
|
|
449
578
|
console.error("postMessage error", error);
|
|
450
579
|
}
|
|
451
580
|
}
|
|
452
|
-
|
|
453
581
|
}
|
|
454
582
|
}
|
|
455
583
|
}
|
|
@@ -481,7 +609,7 @@ export class WalletManager {
|
|
|
481
609
|
}
|
|
482
610
|
}
|
|
483
611
|
// 利用messageChannel通信
|
|
484
|
-
private async sendMessageToIframe
|
|
612
|
+
private async sendMessageToIframe(
|
|
485
613
|
message: SendMessage<any>,
|
|
486
614
|
timeout: number
|
|
487
615
|
): Promise<MessageEvent | null> {
|
|
@@ -492,7 +620,7 @@ export class WalletManager {
|
|
|
492
620
|
// 等待钱包iframe返回,并关闭channel,超时时间timeout
|
|
493
621
|
return new Promise((resolve, reject) => {
|
|
494
622
|
const timer = setTimeout(() => {
|
|
495
|
-
if (this.
|
|
623
|
+
if (this.isIframeOpen()) {
|
|
496
624
|
// 微信窗口
|
|
497
625
|
this.removeWalletIframe();
|
|
498
626
|
}
|
|
@@ -501,7 +629,7 @@ export class WalletManager {
|
|
|
501
629
|
messageChannel.port1.onmessage = (event) => {
|
|
502
630
|
clearTimeout(timer);
|
|
503
631
|
messageChannel.port1.close();
|
|
504
|
-
if (this.
|
|
632
|
+
if (this.isIframeOpen()) {
|
|
505
633
|
// 微信窗口
|
|
506
634
|
this.removeWalletIframe();
|
|
507
635
|
}
|
|
@@ -515,7 +643,7 @@ export class WalletManager {
|
|
|
515
643
|
console.error("sendMessageToIframe postMessage error", error);
|
|
516
644
|
clearTimeout(timer);
|
|
517
645
|
messageChannel.port1.close();
|
|
518
|
-
if (this.
|
|
646
|
+
if (this.isIframeOpen()) {
|
|
519
647
|
// 微信窗口
|
|
520
648
|
this.removeWalletIframe();
|
|
521
649
|
}
|
|
@@ -526,6 +654,5 @@ export class WalletManager {
|
|
|
526
654
|
console.error("iframe不存在");
|
|
527
655
|
return null;
|
|
528
656
|
}
|
|
529
|
-
}
|
|
530
|
-
|
|
657
|
+
}
|
|
531
658
|
}
|