Skip to content

Commit 420d8d9

Browse files
committed
fix(support uniapp): fix provider with uniapp
1 parent 44f5636 commit 420d8d9

File tree

9 files changed

+138
-41
lines changed

9 files changed

+138
-41
lines changed

dist/anyweb-js-sdk.umd.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/anyweb-js-sdk.umd.min.js.map

Lines changed: 12 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@idealight-labs/anyweb-js-sdk",
33
"description": "AnyWeb JavaScript Software Development Kit",
4-
"version": "1.1.6",
4+
"version": "1.2.0",
55
"license": "LGPL-3.0",
66
"author": "common@idealight.ltd",
77
"repository": {
@@ -14,7 +14,7 @@
1414
],
1515
"main": "dist/src/index.js",
1616
"types": "dist/src/index.d.ts",
17-
"browser": "dist/anyweb-js-sdk.umd.min.js",
17+
"browser": "dist/src/index.js",
1818
"browserify-browser": {
1919
"secp256k1": "secp256k1/elliptic"
2020
},

dist/src/interface/provider.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ export interface IIframeOptions {
6969
params: string;
7070
chainId: number;
7171
scopes?: string[];
72-
authType?: 'account' | 'createContract' | 'callContract' | 'createTransaction' | 'importAccount' | 'exit_accounts' | 'logout';
72+
authType?: 'account' | 'createContract' | 'callContract' | 'createTransaction' | 'importAccount' | 'exit_accounts' | 'logout' | 'check_auth' | 'check_identify' | 'identify' | 'check_login';
7373
waitResult?: boolean;
74+
silence?: boolean;
7475
}
7576
export interface IIframeData {
7677
type: 'event' | 'callback' | 'router' | 'default';

dist/src/provider.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@ export declare class Provider implements IProvider {
1616
logger: ConsoleLike;
1717
readonly appId: string;
1818
private chainId;
19+
private static instance;
20+
static ready: boolean;
1921
events: {
2022
onConnect?: (connectInfo: IProviderConnectInfo) => void;
2123
onDisconnect?: (error: IProviderRpcError) => void;
2224
onChainChanged?: (chainId: string) => void;
2325
onAccountsChanged?: (accounts: string[]) => void;
2426
onMessage?: (message: IProviderMessage) => void;
2527
onNetworkChanged?: (networkId: string) => void;
28+
onReady?: () => void;
2629
};
2730
constructor({ logger, appId }: IBaseProviderOptions);
31+
static getInstance(params?: IBaseProviderOptions): Provider;
2832
/**
2933
* Deprecated: use `request` instead
3034
* @param arg

dist/src/provider.js

Lines changed: 98 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class Provider {
3232
constructor({ logger, appId }) {
3333
this.chainId = 1;
3434
this.events = {};
35+
if (Provider.instance) {
36+
return Provider.instance;
37+
}
38+
Provider.instance = this;
3539
if (!logger) {
3640
logger = console;
3741
}
@@ -47,10 +51,38 @@ class Provider {
4751
// @ts-ignore
4852
window.anyweb = this;
4953
}
54+
const messageHandler = (event) => {
55+
if (event.data &&
56+
(0, common_1.isObject)(event.data) &&
57+
'type' in event.data &&
58+
event.data.type === 'anyweb') {
59+
const IframeData = event.data.data;
60+
if (IframeData.type == 'event' &&
61+
IframeData.data == 'ready' &&
62+
IframeData.success) {
63+
console.debug('[AnyWeb] SDK初始化完成');
64+
Provider.ready = true;
65+
this.events.onReady && this.events.onReady();
66+
window.removeEventListener('message', messageHandler);
67+
}
68+
}
69+
};
70+
window.addEventListener('message', messageHandler);
5071
(0, common_1.createIframe)('pages/index/home')
5172
.then()
5273
.catch((e) => console.error('[AnyWeb] createIframe error', e));
5374
}
75+
static getInstance(params) {
76+
if (!Provider.instance) {
77+
if (params) {
78+
Provider.instance = new Provider(params);
79+
}
80+
else {
81+
throw new Error('[AnyWeb] Provider is not initialized');
82+
}
83+
}
84+
return Provider.instance;
85+
}
5486
/**
5587
* Deprecated: use `request` instead
5688
* @param arg
@@ -119,19 +151,37 @@ class Provider {
119151
*/
120152
rawRequest(method, params) {
121153
return __awaiter(this, void 0, void 0, function* () {
154+
if (!Provider.ready) {
155+
throw new Error("[AnyWeb] Provider is not ready, please use on('ready', callback) to listen to ready event");
156+
}
122157
switch (method) {
123158
case 'cfx_requestAccounts':
124159
return this.rawRequest('cfx_accounts');
125160
case 'cfx_accounts':
126161
console.debug('[AnyWeb]', { params });
127162
const scopes = params[0].scopes;
128-
const result = (yield (0, common_1.callIframe)('pages/dapp/auth', {
129-
appId: this.appId,
130-
params: params ? JSON.stringify(params[0]) : '',
131-
chainId: this.chainId,
132-
authType: 'account',
133-
scopes: scopes,
134-
}, this));
163+
let result;
164+
try {
165+
result = (yield (0, common_1.callIframe)('pages/dapp/auth', {
166+
appId: this.appId,
167+
params: params ? JSON.stringify(params[0]) : '',
168+
chainId: this.chainId,
169+
authType: 'check_auth',
170+
scopes: scopes,
171+
silence: true,
172+
}, this));
173+
console.debug('[AnyWeb]', 'silent auth result', result);
174+
}
175+
catch (e) {
176+
console.debug('[AnyWeb]', 'need to auth', e);
177+
result = (yield (0, common_1.callIframe)('pages/dapp/auth', {
178+
appId: this.appId,
179+
params: params ? JSON.stringify(params[0]) : '',
180+
chainId: this.chainId,
181+
authType: 'account',
182+
scopes: scopes,
183+
}, this));
184+
}
135185
result.scopes = scopes;
136186
this.events.onAccountsChanged &&
137187
this.events.onAccountsChanged(result.address);
@@ -210,21 +260,53 @@ class Provider {
210260
chainId: this.chainId,
211261
params: params ? JSON.stringify(params) : '',
212262
authType: 'exit_accounts',
263+
silence: true,
213264
}, this);
214265
case 'anyweb_identify':
215-
return yield (0, common_1.callIframe)('pages/user/identify', {
216-
appId: this.appId,
217-
chainId: this.chainId,
218-
params: params ? JSON.stringify(params) : '',
219-
}, this);
266+
let identifyResult;
267+
try {
268+
identifyResult = yield (0, common_1.callIframe)('pages/user/identify', {
269+
appId: this.appId,
270+
chainId: this.chainId,
271+
params: params ? JSON.stringify(params) : '',
272+
authType: 'check_identify',
273+
silence: true,
274+
}, this);
275+
console.debug('[AnyWeb]', 'Check identify result', identifyResult);
276+
}
277+
catch (e) {
278+
console.debug('[AnyWeb]', 'need to identify', e);
279+
identifyResult = yield (0, common_1.callIframe)('pages/user/identify', {
280+
appId: this.appId,
281+
chainId: this.chainId,
282+
params: params ? JSON.stringify(params) : '',
283+
authType: 'identify',
284+
}, this);
285+
}
286+
return identifyResult;
220287
case 'anyweb_logout':
221288
// Logout the account of AnyWeb
222289
return yield (0, common_1.callIframe)('pages/dapp/auth', {
223290
appId: this.appId,
224291
chainId: this.chainId,
225292
params: params ? JSON.stringify(params) : '',
226293
authType: 'logout',
294+
silence: true,
227295
}, this);
296+
case 'anyweb_loginstate':
297+
try {
298+
return yield (0, common_1.callIframe)('pages/dapp/auth', {
299+
appId: this.appId,
300+
params: '',
301+
chainId: this.chainId,
302+
authType: 'check_login',
303+
silence: true,
304+
}, this);
305+
}
306+
catch (e) {
307+
console.debug('[AnyWeb]', 'need to login', e);
308+
return false;
309+
}
228310
default:
229311
return 'Unsupported method';
230312
}
@@ -261,9 +343,13 @@ class Provider {
261343
case 'networkChanged':
262344
this.events.onNetworkChanged = listener;
263345
break;
346+
case 'ready':
347+
this.events.onReady = listener;
348+
break;
264349
default:
265350
break;
266351
}
267352
}
268353
}
269354
exports.Provider = Provider;
355+
Provider.ready = false;

dist/src/utils/common.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export declare function sha512(str: string): string;
1414
export declare const isObject: (obj: unknown) => boolean;
1515
export declare const sendMessageToApp: ({ data, type, success, }: IIframeData) => void;
1616
export declare const createIframe: (url: string) => Promise<void>;
17-
export declare const getIframe: (url: string, onClose: () => void) => Promise<() => void>;
18-
export declare const callIframe: (path: string, { appId, params, chainId, scopes, authType, waitResult, }: IIframeOptions, provider: Provider) => Promise<unknown>;
17+
export declare const getIframe: (url: string, onClose: () => void, silence?: boolean) => Promise<() => void>;
18+
export declare const callIframe: (path: string, { appId, params, chainId, scopes, authType, waitResult, silence, }: IIframeOptions, provider: Provider) => Promise<unknown>;
1919
export declare const writeStorage: (key: string, content: Record<string, unknown>, expiresTime?: number) => void;
2020
export declare const isArrEqual: <T>(arr1: T[], arr2: T[]) => boolean;
2121
export declare const isIncluded: <T>(arr1: T[], arr2: T[]) => boolean;

dist/src/utils/common.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,8 @@ const isObject = (obj) => {
9595
exports.isObject = isObject;
9696
const closeIframe = (root) => {
9797
console.debug('[AnyWeb]', 'closeIframe', root.style);
98-
setTimeout(() => {
99-
setBodyScrollable();
100-
root.style.display = 'none';
101-
}, 100);
98+
setBodyScrollable();
99+
root.style.display = 'none';
102100
};
103101
const sendMessageToApp = ({ data, type, success = true, }) => {
104102
const iframe = document.getElementById('anyweb-iframe');
@@ -209,7 +207,7 @@ const createIframe = (url) => __awaiter(void 0, void 0, void 0, function* () {
209207
document.body.insertBefore(mask, document.body.firstElementChild);
210208
});
211209
exports.createIframe = createIframe;
212-
const getIframe = (url, onClose) => __awaiter(void 0, void 0, void 0, function* () {
210+
const getIframe = (url, onClose, silence = false) => __awaiter(void 0, void 0, void 0, function* () {
213211
if (!(document.getElementById('anyweb-iframe-mask') &&
214212
document.getElementById('anyweb-iframe'))) {
215213
console.warn('[AnyWeb] Something wrong with the iframe, recreating...');
@@ -223,28 +221,30 @@ const getIframe = (url, onClose) => __awaiter(void 0, void 0, void 0, function*
223221
},
224222
});
225223
const mask = document.getElementById('anyweb-iframe-mask');
226-
setTimeout(() => {
224+
if (!silence) {
227225
mask.style.display = 'block';
228226
setBodyNonScrollable();
229-
}, 100);
227+
}
230228
return () => {
231229
onClose();
232-
closeIframe(mask);
230+
if (!silence) {
231+
closeIframe(mask);
232+
}
233233
};
234234
});
235235
exports.getIframe = getIframe;
236-
const callIframe = (path, { appId, params, chainId, scopes = [], authType, waitResult = true, }, provider) => __awaiter(void 0, void 0, void 0, function* () {
236+
const callIframe = (path, { appId, params, chainId, scopes = [], authType, waitResult = true, silence = false, }, provider) => __awaiter(void 0, void 0, void 0, function* () {
237237
if (waitResult) {
238238
return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
239239
let callback = undefined;
240240
const close = yield (0, exports.getIframe)(`${path}?appId=${appId}&authType=${authType}&random=${Math.floor(Math.random() * 1000)}&chainId=${chainId}&params=${params}&scopes=${JSON.stringify(scopes)}`, () => {
241241
if (timer) {
242242
clearTimeout(timer);
243243
}
244-
});
244+
}, silence);
245245
const timer = setTimeout(() => {
246246
close();
247-
reject(new Error('Timeout'));
247+
reject('Timeout');
248248
}, 10 * 60 * 1000);
249249
// Set Listeners
250250
window.addEventListener('message', function receiveMessageFromIframePage(event) {
@@ -262,7 +262,7 @@ const callIframe = (path, { appId, params, chainId, scopes = [], authType, waitR
262262
resolve(callback.data);
263263
}
264264
else {
265-
reject(new Error(callback.data));
265+
reject(callback.data);
266266
}
267267
}
268268
else if (callback.type === 'event') {
@@ -287,7 +287,7 @@ const callIframe = (path, { appId, params, chainId, scopes = [], authType, waitR
287287
else {
288288
yield (0, exports.getIframe)(`${path}?appId=${appId}&authType=${authType}&random=${Math.floor(Math.random() * 1000)}&chainId=${chainId}&params=${params}&scopes=${JSON.stringify(scopes)}`, () => {
289289
return;
290-
});
290+
}, silence);
291291
return 'ok';
292292
}
293293
});

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@idealight-labs/anyweb-js-sdk",
33
"description": "AnyWeb JavaScript Software Development Kit",
4-
"version": "1.1.11",
4+
"version": "1.2.0",
55
"license": "LGPL-3.0",
66
"author": "common@idealight.ltd",
77
"repository": {
@@ -14,7 +14,7 @@
1414
],
1515
"main": "dist/src/index.js",
1616
"types": "dist/src/index.d.ts",
17-
"browser": "dist/anyweb-js-sdk.umd.min.js",
17+
"browser": "dist/src/index.js",
1818
"browserify-browser": {
1919
"secp256k1": "secp256k1/elliptic"
2020
},

0 commit comments

Comments
 (0)