web-dc-api 0.1.5 → 0.1.6
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/cjs/index.js +1 -1
- package/dist/dc.min.js +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/index.d.ts +934 -878
- package/package.json +4 -8
- package/dist/cjs/helia-core-B1Xqha7a.js +0 -1
- package/dist/cjs/helia-core-D8Uv1KjQ.js +0 -1
- package/dist/cjs/polkadot-api-7PhQf3ws.js +0 -1
- package/dist/cjs/polkadot-api-CtrJVWuZ.js +0 -1
- package/dist/esm/chunks/helia-core-BxMqyK2Y.js +0 -1
- package/dist/esm/chunks/helia-core-DMXRpcO-.js +0 -1
- package/dist/esm/chunks/polkadot-api-5Y9Bw8VT.js +0 -1
- package/dist/esm/chunks/polkadot-api-D69Ioun_.js +0 -1
- package/lib/common/blowfish/block.ts +0 -259
- package/lib/common/blowfish/cipher.ts +0 -144
- package/lib/common/blowfish/const.ts +0 -195
- package/lib/common/chain.ts +0 -469
- package/lib/common/commonclient.ts +0 -202
- package/lib/common/constants.ts +0 -55
- package/lib/common/dc-key/ed25519.ts +0 -343
- package/lib/common/dc-key/keyManager.ts +0 -424
- package/lib/common/dcapi.ts +0 -98
- package/lib/common/dcutil.ts +0 -627
- package/lib/common/define.ts +0 -70
- package/lib/common/error.ts +0 -67
- package/lib/common/grpc-dc.ts +0 -104
- package/lib/common/module-system.ts +0 -184
- package/lib/common/service-worker.ts +0 -234
- package/lib/common/types/types.ts +0 -344
- package/lib/dc.ts +0 -701
- package/lib/implements/account/client.ts +0 -185
- package/lib/implements/account/manager.ts +0 -683
- package/lib/implements/aiproxy/client.ts +0 -357
- package/lib/implements/aiproxy/manager.ts +0 -670
- package/lib/implements/cache/client.ts +0 -105
- package/lib/implements/cache/manager.ts +0 -127
- package/lib/implements/comment/client.ts +0 -982
- package/lib/implements/comment/manager.ts +0 -1151
- package/lib/implements/dc/client.ts +0 -51
- package/lib/implements/dc/manager.ts +0 -33
- package/lib/implements/file/client.ts +0 -253
- package/lib/implements/file/file-cache-manager.ts +0 -142
- package/lib/implements/file/manager.ts +0 -1240
- package/lib/implements/file/seekableFileStream.ts +0 -344
- package/lib/implements/file/streamwriter.ts +0 -322
- package/lib/implements/keyvalue/client.ts +0 -376
- package/lib/implements/keyvalue/manager.ts +0 -759
- package/lib/implements/message/client.ts +0 -250
- package/lib/implements/message/manager.ts +0 -215
- package/lib/implements/threaddb/cbor/coding.ts +0 -62
- package/lib/implements/threaddb/cbor/event.ts +0 -336
- package/lib/implements/threaddb/cbor/node.ts +0 -542
- package/lib/implements/threaddb/cbor/record.ts +0 -398
- package/lib/implements/threaddb/common/AsyncMutex.ts +0 -24
- package/lib/implements/threaddb/common/addrinfo.ts +0 -135
- package/lib/implements/threaddb/common/dispatcher.ts +0 -81
- package/lib/implements/threaddb/common/idbstore-adapter.ts +0 -260
- package/lib/implements/threaddb/common/json-patcher.ts +0 -204
- package/lib/implements/threaddb/common/key.ts +0 -290
- package/lib/implements/threaddb/common/level-adapter.ts +0 -235
- package/lib/implements/threaddb/common/lineReader.ts +0 -79
- package/lib/implements/threaddb/common/logstore.ts +0 -215
- package/lib/implements/threaddb/common/transformed-datastore.ts +0 -308
- package/lib/implements/threaddb/core/app.ts +0 -206
- package/lib/implements/threaddb/core/core.ts +0 -230
- package/lib/implements/threaddb/core/db.ts +0 -249
- package/lib/implements/threaddb/core/event.ts +0 -54
- package/lib/implements/threaddb/core/head.ts +0 -89
- package/lib/implements/threaddb/core/identity.ts +0 -171
- package/lib/implements/threaddb/core/logstore.ts +0 -137
- package/lib/implements/threaddb/core/options.ts +0 -14
- package/lib/implements/threaddb/core/record.ts +0 -54
- package/lib/implements/threaddb/db/collection.ts +0 -1910
- package/lib/implements/threaddb/db/db.ts +0 -698
- package/lib/implements/threaddb/db/json2Query.ts +0 -192
- package/lib/implements/threaddb/db/query.ts +0 -524
- package/lib/implements/threaddb/dbclient.ts +0 -543
- package/lib/implements/threaddb/dbmanager.ts +0 -1906
- package/lib/implements/threaddb/lsstoreds/addr_book.ts +0 -549
- package/lib/implements/threaddb/lsstoreds/cache.ts +0 -36
- package/lib/implements/threaddb/lsstoreds/cyclic_batch.ts +0 -87
- package/lib/implements/threaddb/lsstoreds/global.ts +0 -151
- package/lib/implements/threaddb/lsstoreds/headbook.ts +0 -373
- package/lib/implements/threaddb/lsstoreds/keybook.ts +0 -297
- package/lib/implements/threaddb/lsstoreds/logstore.ts +0 -29
- package/lib/implements/threaddb/lsstoreds/metadata.ts +0 -223
- package/lib/implements/threaddb/net/define.ts +0 -149
- package/lib/implements/threaddb/net/grpcClient.ts +0 -589
- package/lib/implements/threaddb/net/grpcserver.ts +0 -146
- package/lib/implements/threaddb/net/net.ts +0 -2047
- package/lib/implements/threaddb/pb/lstore.proto +0 -38
- package/lib/implements/threaddb/pb/lstore.ts +0 -393
- package/lib/implements/threaddb/pb/lstore_pb.d.ts +0 -433
- package/lib/implements/threaddb/pb/lstore_pb.js +0 -1085
- package/lib/implements/threaddb/pb/net.proto +0 -194
- package/lib/implements/threaddb/pb/net_pb.d.ts +0 -2349
- package/lib/implements/threaddb/pb/net_pb.js +0 -5525
- package/lib/implements/threaddb/pb/proto-custom-types.ts +0 -212
- package/lib/implements/util/client.ts +0 -72
- package/lib/implements/util/manager.ts +0 -146
- package/lib/implements/wallet/manager.ts +0 -671
- package/lib/index.ts +0 -57
- package/lib/interfaces/DCContext.ts +0 -51
- package/lib/interfaces/aiproxy-interface.ts +0 -145
- package/lib/interfaces/auth-interface.ts +0 -118
- package/lib/interfaces/cache-interface.ts +0 -22
- package/lib/interfaces/client-interface.ts +0 -11
- package/lib/interfaces/comment-interface.ts +0 -167
- package/lib/interfaces/components/news-component.ts +0 -0
- package/lib/interfaces/database-interface.ts +0 -169
- package/lib/interfaces/file-interface.ts +0 -120
- package/lib/interfaces/index.ts +0 -10
- package/lib/interfaces/keyvalue-interface.ts +0 -156
- package/lib/interfaces/message-interface.ts +0 -22
- package/lib/interfaces/util-interface.ts +0 -31
- package/lib/modules/aiproxy-module.ts +0 -246
- package/lib/modules/auth-module.ts +0 -753
- package/lib/modules/cache-module.ts +0 -99
- package/lib/modules/client-module.ts +0 -71
- package/lib/modules/comment-module.ts +0 -429
- package/lib/modules/components/news-components.ts +0 -390
- package/lib/modules/database-module.ts +0 -598
- package/lib/modules/file-module.ts +0 -291
- package/lib/modules/index.ts +0 -13
- package/lib/modules/keyvalue-module.ts +0 -379
- package/lib/modules/message-module.ts +0 -107
- package/lib/modules/util-module.ts +0 -148
- package/lib/polyfills/process-env-browser.ts +0 -1
- package/lib/proto/datasource.ts +0 -93
- package/lib/proto/dcnet.proto +0 -1601
- package/lib/proto/dcnet_proto.d.ts +0 -22857
- package/lib/proto/dcnet_proto.js +0 -55204
- package/lib/proto/dcnet_proto_sparse.js +0 -55166
- package/lib/proto/oidfetch.proto +0 -25
- package/lib/proto/oidfetch_proto.d.ts +0 -585
- package/lib/proto/oidfetch_proto.js +0 -1247
- package/lib/serverless/babel-browser.ts +0 -39
- package/lib/serverless/base_entity.ts +0 -78
- package/lib/serverless/base_repository.ts +0 -414
- package/lib/serverless/browser_schema_extractor.ts +0 -283
- package/lib/serverless/decorator_factory.ts +0 -322
- package/lib/util/BrowserLineReader.ts +0 -73
- package/lib/util/base64.ts +0 -105
- package/lib/util/bcrypt.ts +0 -206
- package/lib/util/curve25519Encryption.ts +0 -418
- package/lib/util/dccrypt.ts +0 -73
- package/lib/util/logger.ts +0 -104
- package/lib/util/utils.ts +0 -289
|
@@ -1,671 +0,0 @@
|
|
|
1
|
-
import { walletOrigin, walletUrl, walletWindowName } from "../../common/define";
|
|
2
|
-
import { DCContext } from "../../../lib/interfaces/DCContext";
|
|
3
|
-
import { Ed25519PubKey } from "../../common/dc-key/ed25519";
|
|
4
|
-
import type {
|
|
5
|
-
Account,
|
|
6
|
-
AccountInfo,
|
|
7
|
-
EIP712SignReqMessage,
|
|
8
|
-
SendMessage,
|
|
9
|
-
SignReqMessage,
|
|
10
|
-
SignResponseMessage,
|
|
11
|
-
} from "../../common/types/types";
|
|
12
|
-
|
|
13
|
-
const appOrigin = typeof window !== "undefined" ? window.location.origin : ""; //"http://localhost:3002"
|
|
14
|
-
const appUrl = typeof window !== "undefined" ? window.location.href : "";
|
|
15
|
-
|
|
16
|
-
const localStorageKey_dcwallet_opener = "dcwallet_opener";
|
|
17
|
-
const timeout = 30000;
|
|
18
|
-
// 错误定义
|
|
19
|
-
export class WalletError extends Error {
|
|
20
|
-
constructor(message: string) {
|
|
21
|
-
super(message);
|
|
22
|
-
this.name = "WalletError";
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
export const Errors = {};
|
|
26
|
-
|
|
27
|
-
export class WalletManager {
|
|
28
|
-
private context: DCContext;
|
|
29
|
-
private walletWindow: Window | null = null;
|
|
30
|
-
private iframeId: string = "dcIframeId";
|
|
31
|
-
private walletIframeId: string = "dcWalletIframeId";
|
|
32
|
-
private channelPort2: MessagePort | null = null;
|
|
33
|
-
constructor(context: DCContext) {
|
|
34
|
-
this.context = context;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
async init(): Promise<boolean> {
|
|
38
|
-
console.log("========init walletManager", appOrigin, walletOrigin);
|
|
39
|
-
const walletOpenFlag = typeof globalThis !== "undefined" && typeof (globalThis as any).walletOpenType !== "undefined" ? true : false // 用于判断是否是直接打开;
|
|
40
|
-
if (walletOpenFlag || appOrigin.indexOf(walletOrigin) === -1) {
|
|
41
|
-
return new Promise((resolve, reject) => {
|
|
42
|
-
// html添加iframe标签,id是dcWalletIframe
|
|
43
|
-
const iframe = document.createElement("iframe");
|
|
44
|
-
iframe.id = this.iframeId;
|
|
45
|
-
iframe.src = `${walletUrl}/iframe?parentOrigin=${appOrigin}`;
|
|
46
|
-
(iframe as any).credentialless = true;// 去掉可以加快加载
|
|
47
|
-
iframe.onload = async () => {
|
|
48
|
-
const bool = await this.initConfig(this);
|
|
49
|
-
resolve(bool);
|
|
50
|
-
};
|
|
51
|
-
iframe.style.display = "none";
|
|
52
|
-
document.body.appendChild(iframe);
|
|
53
|
-
window.addEventListener("message", (event) => {
|
|
54
|
-
this.listenFromWallet(event);
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
} else {
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
// iframe加载完成后,发送初始化配置
|
|
62
|
-
async initConfig(that: WalletManager): Promise<boolean> {
|
|
63
|
-
return new Promise((resolve, reject) => {
|
|
64
|
-
const message = {
|
|
65
|
-
type: "init",
|
|
66
|
-
data: {
|
|
67
|
-
appId: this.context.appInfo.appId,
|
|
68
|
-
appName: this.context.appInfo.appName,
|
|
69
|
-
appIcon: this.context.appInfo.appIcon,
|
|
70
|
-
appVersion: this.context.appInfo.appVersion,
|
|
71
|
-
appUrl: appUrl,
|
|
72
|
-
},
|
|
73
|
-
};
|
|
74
|
-
that
|
|
75
|
-
.sendMessageToIframe(message, 5000 * 10)
|
|
76
|
-
.then((response) => {
|
|
77
|
-
console.log("initConfig response", response);
|
|
78
|
-
if (!response || !response.data || !response.data.data) {
|
|
79
|
-
console.error("initConfig response is null");
|
|
80
|
-
resolve(false);
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
const data = response.data?.data;
|
|
84
|
-
if (data.success === false) {
|
|
85
|
-
console.error("initConfig error", message);
|
|
86
|
-
resolve(false);
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
resolve(true);
|
|
90
|
-
})
|
|
91
|
-
.catch((error) => {
|
|
92
|
-
console.error("initConfig error", error);
|
|
93
|
-
resolve(false);
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
//等待钱包页面加载完成
|
|
99
|
-
waitForWalletLoaded = async (
|
|
100
|
-
walletWindow: Window | null,
|
|
101
|
-
timeout: number
|
|
102
|
-
) => {
|
|
103
|
-
// localStorage中获取是否支持window.opener
|
|
104
|
-
const openerFlag = localStorage.getItem(localStorageKey_dcwallet_opener);
|
|
105
|
-
let waitTimeCount = 1;
|
|
106
|
-
if (openerFlag == "true") {
|
|
107
|
-
waitTimeCount = 3;
|
|
108
|
-
}
|
|
109
|
-
// 开启定时器500ms检查一次,第一次等待1.5秒,如果没有加载完成,则发送轮询请求
|
|
110
|
-
return new Promise((resolve) => {
|
|
111
|
-
let messageChannel = new MessageChannel();
|
|
112
|
-
const onMessage = (event: MessageEvent) => {
|
|
113
|
-
const message = event.data;
|
|
114
|
-
if (message.type === "walletLoaded") {
|
|
115
|
-
clearInterval(interval);
|
|
116
|
-
clearTimeout(timeoutHandle);
|
|
117
|
-
messageChannel.port1.close();
|
|
118
|
-
window.removeEventListener("message", listenForWalletLoaded);
|
|
119
|
-
resolve(true);
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
messageChannel.port1.onmessage = onMessage;
|
|
123
|
-
const checkMessage = {
|
|
124
|
-
type: "checkWalletLoaded",
|
|
125
|
-
origin: appOrigin,
|
|
126
|
-
};
|
|
127
|
-
let walletLoadedFlag = false;
|
|
128
|
-
const listenForWalletLoaded = (event: MessageEvent) => {
|
|
129
|
-
//判断消息来源
|
|
130
|
-
if (event.origin !== walletOrigin) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
if (event.source != null && event.source != walletWindow) {
|
|
134
|
-
//非当前操作打开的窗口
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
const data = event.data;
|
|
138
|
-
if (!data.type) {
|
|
139
|
-
//非钱包插件
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
console.log("debug================listenForWalletLoaded", new Date());
|
|
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
|
-
});
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
// 判断是否iframe打开钱包
|
|
193
|
-
private isIframeOpen = (): boolean => {
|
|
194
|
-
const walletOpenType = typeof globalThis !== "undefined" ? (globalThis as any).walletOpenType : '' // 用于判断是否是直接打开;
|
|
195
|
-
if(walletOpenType == 'iframe') {
|
|
196
|
-
return true;
|
|
197
|
-
}
|
|
198
|
-
const ua = navigator.userAgent.toLowerCase();
|
|
199
|
-
return ua.indexOf("micromessenger") !== -1;
|
|
200
|
-
// todo 临时测试
|
|
201
|
-
// return true
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
// 打开钱包iframe窗口
|
|
205
|
-
async openWalletIframe(): Promise<boolean> {
|
|
206
|
-
console.log("debug================openWalletIframe", new Date());
|
|
207
|
-
return new Promise((resolve, reject) => {
|
|
208
|
-
const walletIframe = document.getElementById(
|
|
209
|
-
this.walletIframeId
|
|
210
|
-
) as HTMLIFrameElement;
|
|
211
|
-
if (walletIframe) {
|
|
212
|
-
resolve(true);
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
// html添加iframe标签,id是dcWalletIframe
|
|
216
|
-
const iframe = document.createElement("iframe");
|
|
217
|
-
iframe.id = this.walletIframeId;
|
|
218
|
-
iframe.src = `${walletUrl}?origin=${appOrigin}`;
|
|
219
|
-
(iframe as any).credentialless = true; // 去掉可以加快加载
|
|
220
|
-
iframe.onload = async () => {
|
|
221
|
-
console.log("debug================onload", new Date());
|
|
222
|
-
};
|
|
223
|
-
iframe.onerror = (error) => {
|
|
224
|
-
console.error("openWallet error", error);
|
|
225
|
-
resolve(false);
|
|
226
|
-
};
|
|
227
|
-
iframe.setAttribute('sandbox', 'allow-scripts allow-forms allow-same-origin');
|
|
228
|
-
|
|
229
|
-
// iframe.sandbox = "allow-scripts allow-forms allow-same-origin";
|
|
230
|
-
// 直接设置 iframe 的样式以覆盖整个页面
|
|
231
|
-
// 最大可能的 z-index
|
|
232
|
-
iframe.style.cssText = `
|
|
233
|
-
z-index: 2147483647;
|
|
234
|
-
position: fixed;
|
|
235
|
-
top: 0;
|
|
236
|
-
right: 0;
|
|
237
|
-
bottom: 0;
|
|
238
|
-
left: 0;
|
|
239
|
-
width: 100%;
|
|
240
|
-
height: 100%;
|
|
241
|
-
background-color: #fff;
|
|
242
|
-
border: none;
|
|
243
|
-
display: block;
|
|
244
|
-
|
|
245
|
-
isolation: isolate;
|
|
246
|
-
transform: translateZ(0);
|
|
247
|
-
`;
|
|
248
|
-
iframe.allow =
|
|
249
|
-
"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture";
|
|
250
|
-
iframe.allowFullscreen = true;
|
|
251
|
-
document.body.appendChild(iframe);
|
|
252
|
-
console.log("debug================appendChild", new Date());
|
|
253
|
-
resolve(true);
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
async removeWalletIframe() {
|
|
258
|
-
const walletIframe = document.getElementById(
|
|
259
|
-
this.walletIframeId
|
|
260
|
-
) as HTMLIFrameElement;
|
|
261
|
-
if (walletIframe) {
|
|
262
|
-
document.body.removeChild(walletIframe);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
async openConnect(accountInfo: AccountInfo = {} as AccountInfo): Promise<Account> {
|
|
267
|
-
return new Promise(async (resolve, reject) => {
|
|
268
|
-
if (this.isIframeOpen()) {
|
|
269
|
-
// 微信窗口
|
|
270
|
-
const bool = await this.openWalletIframe();
|
|
271
|
-
if (!bool) {
|
|
272
|
-
console.error("openWalletIframe error");
|
|
273
|
-
reject(new WalletError("openWalletIframe error"));
|
|
274
|
-
return;
|
|
275
|
-
}
|
|
276
|
-
} else {
|
|
277
|
-
// 普通窗口
|
|
278
|
-
const urlWithOrigin = walletUrl + "?origin=" + appOrigin;
|
|
279
|
-
this.walletWindow = window.open(urlWithOrigin, walletWindowName);
|
|
280
|
-
}
|
|
281
|
-
console.log("debug================initCommChannel", new Date());
|
|
282
|
-
this.initCommChannel();
|
|
283
|
-
const shouldReturnUserInfo = typeof globalThis !== "undefined" && typeof (globalThis as any).shouldReturnUserInfo !== "undefined" ? true : false // 用于判断需要返回用户信息;
|
|
284
|
-
// this.waitForWalletLoaded(this.walletWindow, timeout).then((flag) => {
|
|
285
|
-
// if (flag) {
|
|
286
|
-
const message = {
|
|
287
|
-
type: "connect",
|
|
288
|
-
data: {
|
|
289
|
-
origin: appOrigin,
|
|
290
|
-
accountInfo: accountInfo || {},
|
|
291
|
-
shouldReturnUserInfo: shouldReturnUserInfo || false,
|
|
292
|
-
attach: '', // 附加参数,以后可以用来传递一些参数
|
|
293
|
-
},
|
|
294
|
-
};
|
|
295
|
-
console.log("debug================sendMessageToIframe", new Date());
|
|
296
|
-
this.sendMessageToIframe(message, 600000)
|
|
297
|
-
.then((response) => {
|
|
298
|
-
console.log("debug================response", new Date());
|
|
299
|
-
console.log("openConnect response", response);
|
|
300
|
-
if (!response || !response.data || !response.data.data) {
|
|
301
|
-
console.error("openConnect response is null");
|
|
302
|
-
reject(new WalletError("openConnect response is null"));
|
|
303
|
-
return;
|
|
304
|
-
}
|
|
305
|
-
const data = response.data?.data;
|
|
306
|
-
const messageData = data.message;
|
|
307
|
-
if (data.success === false || !messageData.appAccount) {
|
|
308
|
-
console.error("openConnect error", message);
|
|
309
|
-
reject(new WalletError("openConnect appAccount is null"));
|
|
310
|
-
return;
|
|
311
|
-
}
|
|
312
|
-
console.log("openConnect success", messageData);
|
|
313
|
-
resolve(messageData);
|
|
314
|
-
})
|
|
315
|
-
.catch((error) => {
|
|
316
|
-
console.error("openConnect error", error);
|
|
317
|
-
reject(new WalletError("openConnect error"));
|
|
318
|
-
});
|
|
319
|
-
// }
|
|
320
|
-
// });
|
|
321
|
-
});
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
// 退出登录 清除iframe中的私钥和公钥
|
|
325
|
-
exitLogin = (): Promise<boolean> => {
|
|
326
|
-
return new Promise((resolve, reject) => {
|
|
327
|
-
// 每100ms发送一次消息,直到钱包加载完成
|
|
328
|
-
const message = {
|
|
329
|
-
type: "exit",
|
|
330
|
-
};
|
|
331
|
-
this.sendMessageToIframe(message, 60000)
|
|
332
|
-
.then((response) => {
|
|
333
|
-
if (!response || !response.data || !response.data.data) {
|
|
334
|
-
console.error("exitLogin response is null");
|
|
335
|
-
reject(new WalletError("exitLogin response is null"));
|
|
336
|
-
return;
|
|
337
|
-
}
|
|
338
|
-
const data = response.data?.data;
|
|
339
|
-
const messageData = data.message;
|
|
340
|
-
if (data.success === false) {
|
|
341
|
-
console.error("exitLogin error", message);
|
|
342
|
-
reject(new WalletError("exitLogin messageData is null"));
|
|
343
|
-
return;
|
|
344
|
-
}
|
|
345
|
-
resolve(messageData);
|
|
346
|
-
})
|
|
347
|
-
.catch((error) => {
|
|
348
|
-
console.error("exitLogin error", error);
|
|
349
|
-
reject(error);
|
|
350
|
-
});
|
|
351
|
-
});
|
|
352
|
-
};
|
|
353
|
-
|
|
354
|
-
/**
|
|
355
|
-
* 用私钥解密数据
|
|
356
|
-
* @param payload 需要解密的数据
|
|
357
|
-
* @returns 解密结果
|
|
358
|
-
*/
|
|
359
|
-
|
|
360
|
-
decrypt = (payload: Uint8Array): Promise<Uint8Array> => {
|
|
361
|
-
return new Promise((resolve, reject) => {
|
|
362
|
-
const message = {
|
|
363
|
-
type: "decrypt",
|
|
364
|
-
data: {
|
|
365
|
-
message: payload,
|
|
366
|
-
},
|
|
367
|
-
};
|
|
368
|
-
this.sendMessageToIframe(message, 60000)
|
|
369
|
-
.then((response) => {
|
|
370
|
-
console.log("decrypt response", response);
|
|
371
|
-
if (!response || !response.data || !response.data.data) {
|
|
372
|
-
console.error("decrypt response is null");
|
|
373
|
-
reject(new WalletError("decrypt response is null"));
|
|
374
|
-
return;
|
|
375
|
-
}
|
|
376
|
-
const data = response.data?.data;
|
|
377
|
-
const messageData = data.message;
|
|
378
|
-
if (data.success === false || !messageData) {
|
|
379
|
-
console.error("decrypt error", message);
|
|
380
|
-
reject(new WalletError("decrypt messageData is null"));
|
|
381
|
-
return;
|
|
382
|
-
}
|
|
383
|
-
console.log("decrypt success", messageData);
|
|
384
|
-
resolve(messageData);
|
|
385
|
-
})
|
|
386
|
-
.catch((error) => {
|
|
387
|
-
console.error("decrypt error", error);
|
|
388
|
-
reject(error);
|
|
389
|
-
});
|
|
390
|
-
});
|
|
391
|
-
};
|
|
392
|
-
|
|
393
|
-
/**
|
|
394
|
-
* 签名方法
|
|
395
|
-
* @param payload 需要签名的数据
|
|
396
|
-
* @returns 签名结果
|
|
397
|
-
*/
|
|
398
|
-
sign = (payload: Uint8Array): Promise<Uint8Array> => {
|
|
399
|
-
return new Promise((resolve, reject) => {
|
|
400
|
-
// 每100ms发送一次消息,直到钱包加载完成
|
|
401
|
-
const message = {
|
|
402
|
-
type: "sign",
|
|
403
|
-
data: {
|
|
404
|
-
message: payload,
|
|
405
|
-
},
|
|
406
|
-
};
|
|
407
|
-
this.sendMessageToIframe(message, 60000)
|
|
408
|
-
.then((response) => {
|
|
409
|
-
if (!response || !response.data || !response.data.data) {
|
|
410
|
-
console.error("sign response is null");
|
|
411
|
-
reject(new WalletError("sign response is null"));
|
|
412
|
-
return;
|
|
413
|
-
}
|
|
414
|
-
const data = response.data?.data;
|
|
415
|
-
const messageData = data.message;
|
|
416
|
-
if (data.success === false || !messageData) {
|
|
417
|
-
console.error("sign error", message);
|
|
418
|
-
reject(new WalletError("sign messageData is null"));
|
|
419
|
-
return;
|
|
420
|
-
}
|
|
421
|
-
resolve(messageData);
|
|
422
|
-
})
|
|
423
|
-
.catch((error) => {
|
|
424
|
-
console.error("sign error", error);
|
|
425
|
-
reject(error);
|
|
426
|
-
});
|
|
427
|
-
});
|
|
428
|
-
};
|
|
429
|
-
|
|
430
|
-
// 签名普通消息
|
|
431
|
-
async signMessage(data: SignReqMessage): Promise<SignResponseMessage | null> {
|
|
432
|
-
return new Promise(async (resolve, reject) => {
|
|
433
|
-
if (!this.context) {
|
|
434
|
-
console.log("未连接钱包");
|
|
435
|
-
reject(new WalletError("未连接钱包"));
|
|
436
|
-
return;
|
|
437
|
-
}
|
|
438
|
-
if (this.isIframeOpen()) {
|
|
439
|
-
// 微信窗口
|
|
440
|
-
const bool = await this.openWalletIframe();
|
|
441
|
-
if (!bool) {
|
|
442
|
-
console.error("openWalletIframe error");
|
|
443
|
-
reject(new WalletError("openWalletIframe error"));
|
|
444
|
-
return;
|
|
445
|
-
}
|
|
446
|
-
} else {
|
|
447
|
-
// 普通窗口
|
|
448
|
-
const urlWithOrigin = walletUrl + "?origin=" + appOrigin;
|
|
449
|
-
this.walletWindow = window.open(urlWithOrigin, walletWindowName);
|
|
450
|
-
}
|
|
451
|
-
this.initCommChannel();
|
|
452
|
-
// this.waitForWalletLoaded(this.walletWindow, timeout).then((flag) => {
|
|
453
|
-
// if (flag) {
|
|
454
|
-
// 每100ms发送一次消息,直到钱包加载完成
|
|
455
|
-
const message = {
|
|
456
|
-
type: "signMessage",
|
|
457
|
-
data,
|
|
458
|
-
};
|
|
459
|
-
this.sendMessageToIframe(message, 60000)
|
|
460
|
-
.then((response: MessageEvent | null) => {
|
|
461
|
-
console.log("signMessage response", response);
|
|
462
|
-
if (!response || !response.data || !response.data.data) {
|
|
463
|
-
console.error("signMessage response is null");
|
|
464
|
-
reject(new WalletError("signMessage response is null"));
|
|
465
|
-
return;
|
|
466
|
-
}
|
|
467
|
-
const data = response.data?.data;
|
|
468
|
-
const messageData = data.message;
|
|
469
|
-
if (data.success === false || !messageData) {
|
|
470
|
-
console.error("signMessage error", message);
|
|
471
|
-
reject(new WalletError("signMessage messageData is null"));
|
|
472
|
-
return;
|
|
473
|
-
}
|
|
474
|
-
console.log("signMessage success", messageData);
|
|
475
|
-
resolve(messageData);
|
|
476
|
-
})
|
|
477
|
-
.catch((error) => {
|
|
478
|
-
console.error("signMessage error", error);
|
|
479
|
-
reject(error);
|
|
480
|
-
});
|
|
481
|
-
// }
|
|
482
|
-
// });
|
|
483
|
-
});
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
// 签名EIP712消息
|
|
487
|
-
async signEIP712Message(
|
|
488
|
-
data: EIP712SignReqMessage
|
|
489
|
-
): Promise<SignResponseMessage | null> {
|
|
490
|
-
return new Promise(async (resolve, reject) => {
|
|
491
|
-
if (!this.context) {
|
|
492
|
-
console.log("未连接钱包");
|
|
493
|
-
reject(new WalletError("未连接钱包"));
|
|
494
|
-
return;
|
|
495
|
-
}
|
|
496
|
-
if (this.isIframeOpen()) {
|
|
497
|
-
// 微信窗口
|
|
498
|
-
const bool = await this.openWalletIframe();
|
|
499
|
-
if (!bool) {
|
|
500
|
-
console.error("openWalletIframe error");
|
|
501
|
-
reject(new WalletError("openWalletIframe error"));
|
|
502
|
-
return;
|
|
503
|
-
}
|
|
504
|
-
} else {
|
|
505
|
-
// 普通窗口
|
|
506
|
-
const urlWithOrigin = walletUrl + "?origin=" + appOrigin;
|
|
507
|
-
this.walletWindow = window.open(urlWithOrigin, walletWindowName);
|
|
508
|
-
}
|
|
509
|
-
this.initCommChannel();
|
|
510
|
-
// this.waitForWalletLoaded(this.walletWindow, timeout).then((flag) => {
|
|
511
|
-
// if (flag) {
|
|
512
|
-
// port1 转移给iframe
|
|
513
|
-
const message = {
|
|
514
|
-
type: "signEIP712Message",
|
|
515
|
-
data: data,
|
|
516
|
-
};
|
|
517
|
-
this.sendMessageToIframe(message, 60000)
|
|
518
|
-
.then((response) => {
|
|
519
|
-
console.log("signEIP712Message response", response);
|
|
520
|
-
if (!response || !response.data || !response.data.data) {
|
|
521
|
-
console.error("signEIP712Message response is null");
|
|
522
|
-
reject(new WalletError("signEIP712Message response is null"));
|
|
523
|
-
return;
|
|
524
|
-
}
|
|
525
|
-
const data = response.data?.data;
|
|
526
|
-
const messageData = data.message;
|
|
527
|
-
if (data.success === false || !messageData) {
|
|
528
|
-
console.error("signEIP712Message error", message);
|
|
529
|
-
reject(new WalletError("signEIP712Message messageData is null"));
|
|
530
|
-
return;
|
|
531
|
-
}
|
|
532
|
-
console.log("messageData success", messageData);
|
|
533
|
-
resolve(messageData);
|
|
534
|
-
})
|
|
535
|
-
.catch((error) => {
|
|
536
|
-
console.error("signEIP712Message error", error);
|
|
537
|
-
reject(error);
|
|
538
|
-
});
|
|
539
|
-
// }
|
|
540
|
-
// });
|
|
541
|
-
});
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
private async listenFromWallet(event: MessageEvent): Promise<void> {
|
|
545
|
-
// if (event.origin !== "todo来源") return; // 可选:对源进行验证
|
|
546
|
-
try {
|
|
547
|
-
const message = event.data;
|
|
548
|
-
if (!message.type) {
|
|
549
|
-
//非钱包插件
|
|
550
|
-
return;
|
|
551
|
-
}
|
|
552
|
-
if (message.type === "walletLoaded") {
|
|
553
|
-
console.log("debug================walletLoaded", new Date());
|
|
554
|
-
console.log("walletLoaded", message);
|
|
555
|
-
console.log("event.origin", event.origin);
|
|
556
|
-
console.log("walletOrigin", walletOrigin);
|
|
557
|
-
//钱包加载完成
|
|
558
|
-
if (event.origin !== walletOrigin) {
|
|
559
|
-
console.log("来源不匹配", event.origin, walletOrigin);
|
|
560
|
-
return;
|
|
561
|
-
}
|
|
562
|
-
// 钱包打开成功
|
|
563
|
-
console.log("钱包打开成功", message.data);
|
|
564
|
-
if (this.channelPort2) {
|
|
565
|
-
//port2转移给钱包
|
|
566
|
-
const message = {
|
|
567
|
-
type: "channelPort2",
|
|
568
|
-
origin: appOrigin,
|
|
569
|
-
};
|
|
570
|
-
if (this.walletWindow) {
|
|
571
|
-
// 如果钱包已经打开
|
|
572
|
-
try {
|
|
573
|
-
this.walletWindow.postMessage(message, walletOrigin, [this.channelPort2]);
|
|
574
|
-
} catch (error) {
|
|
575
|
-
console.error("postMessage error", error);
|
|
576
|
-
}
|
|
577
|
-
} else {
|
|
578
|
-
// 如果钱包iframe已经打开
|
|
579
|
-
const iframe = document.getElementById(
|
|
580
|
-
this.walletIframeId
|
|
581
|
-
) as HTMLIFrameElement;
|
|
582
|
-
// port1转移给iframe
|
|
583
|
-
if (iframe) {
|
|
584
|
-
try {
|
|
585
|
-
iframe.contentWindow?.postMessage(message, walletOrigin, [
|
|
586
|
-
this.channelPort2,
|
|
587
|
-
]);
|
|
588
|
-
} catch (error) {
|
|
589
|
-
console.error("postMessage error", error);
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
} catch (error) {
|
|
596
|
-
console.error("message error", error);
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
private async initCommChannel(): Promise<void> {
|
|
601
|
-
const iframe = document.getElementById(this.iframeId) as HTMLIFrameElement;
|
|
602
|
-
// port1转移给iframe
|
|
603
|
-
if (iframe) {
|
|
604
|
-
const message = {
|
|
605
|
-
code: "0",
|
|
606
|
-
type: "channelPort1",
|
|
607
|
-
};
|
|
608
|
-
try {
|
|
609
|
-
const messageChannel = new MessageChannel();
|
|
610
|
-
iframe.contentWindow?.postMessage(message, walletOrigin, [
|
|
611
|
-
messageChannel.port1,
|
|
612
|
-
]);
|
|
613
|
-
this.channelPort2 = messageChannel.port2;
|
|
614
|
-
console.log("initCommChannel success");
|
|
615
|
-
} catch (error) {
|
|
616
|
-
console.error("initCommChannel error", error);
|
|
617
|
-
}
|
|
618
|
-
} else {
|
|
619
|
-
console.error("iframe不存在");
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
|
-
// 利用messageChannel通信
|
|
623
|
-
private async sendMessageToIframe(
|
|
624
|
-
message: SendMessage<any>,
|
|
625
|
-
timeout: number
|
|
626
|
-
): Promise<MessageEvent | null> {
|
|
627
|
-
const iframe = document.getElementById(this.iframeId) as HTMLIFrameElement;
|
|
628
|
-
// port2转移给iframe
|
|
629
|
-
if (iframe) {
|
|
630
|
-
const messageChannel = new MessageChannel();
|
|
631
|
-
// 等待钱包iframe返回,并关闭channel,超时时间timeout
|
|
632
|
-
return new Promise((resolve, reject) => {
|
|
633
|
-
const timer = setTimeout(() => {
|
|
634
|
-
if (this.isIframeOpen()) {
|
|
635
|
-
// 微信窗口
|
|
636
|
-
this.removeWalletIframe();
|
|
637
|
-
}
|
|
638
|
-
reject("timeout");
|
|
639
|
-
}, timeout);
|
|
640
|
-
messageChannel.port1.onmessage = (event) => {
|
|
641
|
-
clearTimeout(timer);
|
|
642
|
-
messageChannel.port1.close();
|
|
643
|
-
if (event.data && event.data.type !== "initConfigResponse") {
|
|
644
|
-
if (this.isIframeOpen()) {
|
|
645
|
-
// 微信窗口
|
|
646
|
-
this.removeWalletIframe();
|
|
647
|
-
}
|
|
648
|
-
}
|
|
649
|
-
resolve(event);
|
|
650
|
-
};
|
|
651
|
-
try {
|
|
652
|
-
iframe.contentWindow?.postMessage(message, walletOrigin, [
|
|
653
|
-
messageChannel.port2,
|
|
654
|
-
]);
|
|
655
|
-
} catch (error) {
|
|
656
|
-
console.error("sendMessageToIframe postMessage error", error);
|
|
657
|
-
clearTimeout(timer);
|
|
658
|
-
messageChannel.port1.close();
|
|
659
|
-
if (this.isIframeOpen()) {
|
|
660
|
-
// 微信窗口
|
|
661
|
-
this.removeWalletIframe();
|
|
662
|
-
}
|
|
663
|
-
reject(error);
|
|
664
|
-
}
|
|
665
|
-
});
|
|
666
|
-
} else {
|
|
667
|
-
console.error("iframe不存在");
|
|
668
|
-
return null;
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
}
|