Skip to content

Commit 43352e3

Browse files
committed
feat(silent auth): support auth account with silent mode
1 parent c2e3eb2 commit 43352e3

File tree

5 files changed

+66
-72
lines changed

5 files changed

+66
-72
lines changed

example/basic-dapp/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<header>
4848
Connect
4949
</header>
50-
<button id="connect" >Connect
50+
<button id="connect" disabled="true">Connect
5151
Wallet
5252
</button>
5353
<button id="Deauthorize" disabled="true">

example/basic-dapp/index.js

Lines changed: 32 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,6 @@ async function walletInitialized() {
562562
// connect
563563
const connectButton = getElement('connect')
564564
const DeauthorizeButton = getElement('Deauthorize')
565-
const logoutButton = getElement('Logout')
566565
const sendNativeTokenButton = getElement('send_native_token')
567566
const approveButton = getElement('approve')
568567
const transferFromButton = getElement('transfer_from')
@@ -576,7 +575,6 @@ async function walletInitialized() {
576575
const transferToAccountInput = getElement('to-account')
577576
const importAddressInput = getElement('import_address_input')
578577
const importAddressNameInput = getElement('import_address_name_input')
579-
const identifyButton = getElement('identify_button')
580578

581579
const deployContract = getElement('deploy_contract')
582580

@@ -597,9 +595,7 @@ async function walletInitialized() {
597595
gatewayTestButton.disabled = false
598596
connectButton.disabled = true
599597
DeauthorizeButton.disabled = false
600-
logoutButton.disabled = false
601598
importAddressButton.disabled = false
602-
identifyButton.disabled = false
603599
}
604600

605601
function unAuthed() {
@@ -614,9 +610,7 @@ async function walletInitialized() {
614610
deployContract.disabled = true
615611
connectButton.disabled = false
616612
DeauthorizeButton.disabled = true
617-
logoutButton.disabled = true
618613
importAddressButton.disabled = true
619-
identifyButton.disabled = true
620614
}
621615

622616
provider.on('accountsChanged', (accounts) => {
@@ -636,37 +630,38 @@ async function walletInitialized() {
636630
provider.request({ method: 'anyweb_version' }).then((version) => {
637631
getElement('version').innerHTML = version
638632
})
639-
// const data = await provider.request({
640-
// method: 'cfx_accounts',
641-
// params: [
642-
// {
643-
// availableNetwork: [1, 1029],
644-
// scopes: ['baseInfo', 'identity'],
645-
// },
646-
// ],
647-
// })
648-
// const { chainId, networkId, address: alreadyAuthedAddresses, code } = data
649-
// console.log(
650-
// 'DApp 获取到的授权结果',
651-
// chainId,
652-
// networkId,
653-
// alreadyAuthedAddresses,
654-
// code
655-
// )
656-
// setState(data)
657-
// getElement('initialized').innerHTML = 'initialized'
658-
// getElement('chainId').innerHTML = chainId
659-
// getElement('networkId').innerHTML = networkId
660-
//
661-
// if (
662-
// !alreadyAuthedAddresses ||
663-
// !alreadyAuthedAddresses.length ||
664-
// alreadyAuthedAddresses.length === 0
665-
// ) {
666-
// unAuthed()
667-
// } else {
668-
// authed(alreadyAuthedAddresses[0], code)
669-
// }
633+
634+
const data = await provider.request({
635+
method: 'cfx_accounts',
636+
params: [
637+
{
638+
availableNetwork: [1, 1029],
639+
scopes: ['baseInfo', 'identity'],
640+
},
641+
],
642+
})
643+
const { chainId, networkId, address: alreadyAuthedAddresses, code } = data
644+
console.log(
645+
'DApp 获取到的授权结果',
646+
chainId,
647+
networkId,
648+
alreadyAuthedAddresses,
649+
code
650+
)
651+
setState(data)
652+
getElement('initialized').innerHTML = 'initialized'
653+
getElement('chainId').innerHTML = chainId
654+
getElement('networkId').innerHTML = networkId
655+
656+
if (
657+
!alreadyAuthedAddresses ||
658+
!alreadyAuthedAddresses.length ||
659+
alreadyAuthedAddresses.length === 0
660+
) {
661+
unAuthed()
662+
} else {
663+
authed(alreadyAuthedAddresses[0], code)
664+
}
670665
} catch (e) {
671666
unAuthed()
672667
console.error('try 到错误了', e)
@@ -714,14 +709,6 @@ async function walletInitialized() {
714709
.then(unAuthed)
715710
.catch(console.error)
716711
}
717-
logoutButton.onclick = () => {
718-
provider
719-
.request({
720-
method: 'anyweb_logout',
721-
})
722-
.then(unAuthed)
723-
.catch(console.error)
724-
}
725712

726713
// send native token to the connected address
727714
sendNativeTokenButton.onclick = async () => {
@@ -900,19 +887,6 @@ async function walletInitialized() {
900887
console.log('err', err)
901888
}
902889
}
903-
904-
identifyButton.onclick = async () => {
905-
try {
906-
provider
907-
.request({ method: 'anyweb_identify', params: [] })
908-
.then((result) => {
909-
getElement('identify_result').innerHTML = result
910-
console.log('result', result)
911-
})
912-
} catch (err) {
913-
console.log('err', err)
914-
}
915-
}
916890
}
917891

918892
window.addEventListener('load', async () => {

src/interface/provider.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export interface IIframeOptions {
8888
| 'importAccount'
8989
| 'exit_accounts'
9090
| 'logout'
91+
| 'check_auth'
9192
waitResult?: boolean
9293
silence?: boolean
9394
}

src/provider.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,36 @@ export class Provider implements IProvider {
131131
case 'cfx_accounts':
132132
console.debug('[AnyWeb]', { params })
133133
const scopes = params[0].scopes
134-
const result = (await callIframe(
135-
'pages/dapp/auth',
136-
{
137-
appId: this.appId,
138-
params: params ? JSON.stringify(params[0]) : '',
139-
chainId: this.chainId,
140-
authType: 'account',
141-
scopes: scopes,
142-
},
143-
this
144-
)) as IAuthResult
134+
let result: IAuthResult
135+
try {
136+
result = (await callIframe(
137+
'pages/dapp/auth',
138+
{
139+
appId: this.appId,
140+
params: params ? JSON.stringify(params[0]) : '',
141+
chainId: this.chainId,
142+
authType: 'check_auth',
143+
scopes: scopes,
144+
silence: true,
145+
},
146+
this
147+
)) as IAuthResult
148+
console.debug('[AnyWeb]', 'silent auth result', result)
149+
} catch (e) {
150+
console.debug('[AnyWeb]', 'need to auth', e)
151+
result = (await callIframe(
152+
'pages/dapp/auth',
153+
{
154+
appId: this.appId,
155+
params: params ? JSON.stringify(params[0]) : '',
156+
chainId: this.chainId,
157+
authType: 'account',
158+
scopes: scopes,
159+
},
160+
this
161+
)) as IAuthResult
162+
}
163+
145164
result.scopes = scopes
146165
this.events.onAccountsChanged &&
147166
this.events.onAccountsChanged(result.address)

src/utils/common.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ export const callIframe = async (
268268
)
269269
const timer = setTimeout(() => {
270270
close()
271-
reject(new Error('Timeout'))
271+
reject('Timeout')
272272
}, 10 * 60 * 1000)
273273

274274
// Set Listeners
@@ -294,7 +294,7 @@ export const callIframe = async (
294294
if (callback.success) {
295295
resolve(callback.data)
296296
} else {
297-
reject(new Error(callback.data as string))
297+
reject(callback.data as string)
298298
}
299299
} else if (callback.type === 'event') {
300300
const eventData = callback.data as IIframeEventData

0 commit comments

Comments
 (0)