Skip to content

Commit 41f2fd5

Browse files
committed
feat(add some feat): improve SDK
1 parent dd266ab commit 41f2fd5

File tree

8 files changed

+83
-152
lines changed

8 files changed

+83
-152
lines changed

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

Lines changed: 1 addition & 1 deletion
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: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/basic-dapp/index.html

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -98,39 +98,13 @@
9898
<form>
9999
<label>test</label>
100100
</form>
101-
<button id="sign_button" disabled="true">Gateway Test
101+
<button id="sign_button" disabled="true">签名
102102
</button>
103103
<footer>
104104
Result: <span id="sign_result">N/A</span>
105105
</footer>
106106
</article>
107107

108-
<article>
109-
<header>
110-
Approve (cUSDT)
111-
</header>
112-
<form>
113-
<label>Spender Address <input type="text" id=
114-
"approve-account"></label>
115-
</form>
116-
<button id="approve" disabled=
117-
"true">approve 100 cUSDT limit
118-
</button>
119-
</article>
120-
<article>
121-
<header>
122-
Transfer from (cUSDT)
123-
</header>
124-
<form>
125-
<label>From <input type="text" id="from-account"></label>
126-
127-
<label>To</label> <input type="text" id=
128-
"to-account">
129-
</form>
130-
<button id="transfer_from" disabled="true">transfer 10
131-
cUSDT
132-
</button>
133-
</article>
134108
<article>
135109
<header>
136110
Deploy Contract

example/basic-dapp/index.js

Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// import js-conflux-sdk
22
// more info about js-conflux-sdk
33
// https://github.com/Conflux-Chain/js-conflux-sdk#readme
4-
import { Conflux, Drip } from './js-conflux-sdk.js'
4+
import { Conflux } from './js-conflux-sdk.js'
55

6-
const cusdtAddress = 'cfxtest:acepe88unk7fvs18436178up33hb4zkuf62a9dk1gv'
76
const dappAddress = 'cfxtest:aca8paka2w86tpgmmh7ufdv005u2cheb76578khwd2'
87
const onlineAddress = 'cfx:acgtf56ceymsvw2r0nsffveafatu5v6jz6me8m4m85'
98

@@ -653,16 +652,9 @@ async function walletInitialized() {
653652
const connectButton = getElement('connect')
654653
const DeauthorizeButton = getElement('Deauthorize')
655654
const logoutButton = getElement('Logout')
656-
const approveButton = getElement('approve')
657-
const transferFromButton = getElement('transfer_from')
658655
const getCFXButton = getElement('get-cfx')
659656
const gatewayTestButton = getElement('gateway_test')
660657
const importAddressButton = getElement('import_address_button')
661-
const nativeReceiverAddressInput = getElement('native-receiver')
662-
const countInput = getElement('native-count')
663-
const approveAccountInput = getElement('approve-account')
664-
const transferFromAccountInput = getElement('from-account')
665-
const transferToAccountInput = getElement('to-account')
666658
const importAddressInput = getElement('import_address_input')
667659
const importAddressNameInput = getElement('import_address_name_input')
668660
const identifyButton = getElement('identify_button')
@@ -680,8 +672,6 @@ async function walletInitialized() {
680672
getElement('oauth_code').innerHTML = oauthCode
681673
}
682674
console.log('authed address: ', address)
683-
approveButton.disabled = false
684-
transferFromButton.disabled = false
685675
deployContract.disabled = false
686676
getCFXButton.disabled = false
687677
gatewayTestButton.disabled = false
@@ -697,8 +687,6 @@ async function walletInitialized() {
697687
getElement('address').innerHTML = 'N/A'
698688
getElement('oauth_code').innerHTML = 'N/A'
699689
console.log('unauthed')
700-
approveButton.disabled = true
701-
transferFromButton.disabled = true
702690
getCFXButton.disabled = true
703691
gatewayTestButton.disabled = true
704692
deployContract.disabled = true
@@ -831,6 +819,11 @@ async function walletInitialized() {
831819
console.log('result', result)
832820
})
833821
.catch((e) => {
822+
getElement('get_cfx_result').innerHTML = `
823+
Code: ${e.code},\n
824+
Message: ${e.message},\n
825+
Data: ${JSON.stringify(e.data)}\n
826+
`
834827
console.error('获取CFX失败', e)
835828
})
836829
} catch (err) {
@@ -903,52 +896,6 @@ async function walletInitialized() {
903896
}
904897
}
905898

906-
// approve spender
907-
approveButton.onclick = async () => {
908-
try {
909-
const connectedAddress = address[0]
910-
const tx = {
911-
from: connectedAddress,
912-
to: cusdtAddress,
913-
data: exampleContract.approve(
914-
approveAccountInput.value,
915-
100000000000000000000
916-
).data,
917-
gasPrice: 2,
918-
}
919-
provider
920-
.request({ method: 'cfx_sendTransaction', params: [tx] })
921-
.then((result) => {
922-
getElement('approve_result').innerHTML = result
923-
console.log('result', result)
924-
})
925-
} catch (err) {
926-
console.log('err', err)
927-
}
928-
}
929-
// transfer from
930-
transferFromButton.onclick = async () => {
931-
try {
932-
const connectedAddress = address[0]
933-
const tx = {
934-
from: connectedAddress,
935-
to: cusdtAddress,
936-
data: exampleContract.transferFrom(
937-
transferFromAccountInput.value,
938-
transferToAccountInput.value,
939-
10000000000000000000
940-
).data,
941-
}
942-
provider
943-
.request({ method: 'cfx_sendTransaction', params: [tx] })
944-
.then((result) => {
945-
console.log('result', result)
946-
})
947-
} catch (err) {
948-
console.log('err', err)
949-
}
950-
}
951-
952899
deployContract.onclick = async () => {
953900
try {
954901
const connectedAddress = address[0]

package.json

Lines changed: 1 addition & 1 deletion
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.2.3",
4+
"version": "1.2.4",
55
"license": "LGPL-3.0",
66
"author": "common@idealight.ltd",
77
"repository": {

src/interface/provider.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,27 @@ export interface IProviderMessage {
6666
readonly data: unknown
6767
}
6868

69+
export type IMethodType =
70+
| 'account'
71+
| 'createContract'
72+
| 'callContract'
73+
| 'createTransaction'
74+
| 'importAccount'
75+
| 'exitAccounts'
76+
| 'signTypedData'
77+
| 'logout'
78+
| 'checkAuth'
79+
| 'checkLogin'
80+
| 'home'
81+
| 'unknownType'
82+
| 'identify'
83+
| 'checkIdentify'
84+
6985
export interface IIframeOptions {
7086
appId: string
7187
params: string
7288
chainId: number
7389
scopes?: string[]
74-
authType?:
75-
| 'account'
76-
| 'createContract'
77-
| 'callContract'
78-
| 'createTransaction'
79-
| 'importAccount'
80-
| 'exit_accounts'
81-
| 'logout'
82-
| 'check_auth'
83-
| 'check_identify'
84-
| 'identify'
85-
| 'check_login'
86-
| 'signTypedData'
8790
waitResult?: boolean
8891
silence?: boolean
8992
}

src/provider.ts

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
import {
77
IAuthResult,
8+
IMethodType,
89
IBaseProviderOptions,
910
IIframeData,
10-
IIframeOptions,
1111
IProvider,
1212
IProviderConnectInfo,
1313
IProviderMessage,
@@ -209,12 +209,11 @@ export class Provider implements IProvider {
209209
let result: IAuthResult
210210
try {
211211
result = (await callIframe(
212-
'pages/dapp/auth',
212+
'checkAuth',
213213
{
214214
appId: this.appId,
215-
params: params ? JSON.stringify(params[0]) : '',
215+
params: params ? params[0] : {},
216216
chainId: this.chainId,
217-
authType: 'check_auth',
218217
scopes: scopes,
219218
silence: true,
220219
},
@@ -224,12 +223,11 @@ export class Provider implements IProvider {
224223
} catch (e) {
225224
this.logger?.debug('[AnyWeb]', 'need to auth', e)
226225
result = (await callIframe(
227-
'pages/dapp/auth',
226+
'account',
228227
{
229228
appId: this.appId,
230-
params: params ? JSON.stringify(params[0]) : '',
229+
params: params ? params[0] : {},
231230
chainId: this.chainId,
232-
authType: 'account',
233231
scopes: scopes,
234232
},
235233
this
@@ -259,7 +257,7 @@ export class Provider implements IProvider {
259257
const data = params[1]
260258
const isRsv = !!params[2]
261259
return await callIframe(
262-
'pages/dapp/auth',
260+
'signTypedData',
263261
{
264262
appId: this.appId,
265263
chainId: this.chainId,
@@ -270,12 +268,11 @@ export class Provider implements IProvider {
270268
isRsv,
271269
})
272270
: '',
273-
authType: 'signTypedData',
274271
},
275272
this
276273
)
277274
case 'cfx_sendTransaction':
278-
let authType: IIframeOptions['authType']
275+
let authType: IMethodType
279276
const payload = params[0]
280277
const to = payload.to
281278
if (to) {
@@ -288,7 +285,7 @@ export class Provider implements IProvider {
288285
}
289286

290287
return await callIframe(
291-
'pages/dapp/auth',
288+
authType,
292289
{
293290
appId: this.appId,
294291
chainId: this.chainId,
@@ -298,26 +295,24 @@ export class Provider implements IProvider {
298295
gatewayPayload: params[1],
299296
})
300297
: '',
301-
authType: authType,
302298
},
303299
this
304300
)
305301
case 'anyweb_importAccount':
306302
return await callIframe(
307-
'pages/dapp/auth',
303+
'importAccount',
308304
{
309305
appId: this.appId,
310306
chainId: this.chainId,
311-
params: params ? JSON.stringify(params[0]) : JSON.stringify({}),
312-
authType: 'importAccount',
307+
params: params ? params[0] : {},
313308
},
314309
this
315310
)
316311
case 'anyweb_version':
317312
return config.version
318313
case 'anyweb_home':
319314
return await callIframe(
320-
'pages/index/home',
315+
'home',
321316
{
322317
appId: this.appId,
323318
chainId: this.chainId,
@@ -330,12 +325,11 @@ export class Provider implements IProvider {
330325
return this.rawRequest('anyweb_revoke', params)
331326
case 'anyweb_revoke':
332327
return await callIframe(
333-
'pages/dapp/auth',
328+
'exitAccounts',
334329
{
335330
appId: this.appId,
336331
chainId: this.chainId,
337332
params: params ? JSON.stringify(params) : '',
338-
authType: 'exit_accounts',
339333
silence: true,
340334
},
341335
this
@@ -344,12 +338,11 @@ export class Provider implements IProvider {
344338
let identifyResult
345339
try {
346340
identifyResult = await callIframe(
347-
'pages/user/identify',
341+
'checkIdentify',
348342
{
349343
appId: this.appId,
350344
chainId: this.chainId,
351345
params: params ? JSON.stringify(params) : '',
352-
authType: 'check_identify',
353346
silence: true,
354347
},
355348
this
@@ -362,12 +355,11 @@ export class Provider implements IProvider {
362355
} catch (e) {
363356
this.logger?.debug('[AnyWeb]', 'need to identify', e)
364357
identifyResult = await callIframe(
365-
'pages/user/identify',
358+
'identify',
366359
{
367360
appId: this.appId,
368361
chainId: this.chainId,
369362
params: params ? JSON.stringify(params) : '',
370-
authType: 'identify',
371363
},
372364
this
373365
)
@@ -376,25 +368,23 @@ export class Provider implements IProvider {
376368
case 'anyweb_logout':
377369
// Logout the account of AnyWeb
378370
return await callIframe(
379-
'pages/dapp/auth',
371+
'logout',
380372
{
381373
appId: this.appId,
382374
chainId: this.chainId,
383375
params: params ? JSON.stringify(params) : '',
384-
authType: 'logout',
385376
silence: true,
386377
},
387378
this
388379
)
389380
case 'anyweb_loginstate':
390381
try {
391382
return await callIframe(
392-
'pages/dapp/auth',
383+
'checkLogin',
393384
{
394385
appId: this.appId,
395386
params: '',
396387
chainId: this.chainId,
397-
authType: 'check_login',
398388
silence: true,
399389
},
400390
this

0 commit comments

Comments
 (0)