@@ -475,6 +475,7 @@ function isAnyWebInstall() {
475475async function walletInitialized ( ) {
476476 // connect
477477 const connectButton = getElement ( 'connect' )
478+ const logoutButton = getElement ( 'logout' )
478479 const sendNativeTokenButton = getElement ( 'send_native_token' )
479480 const approveButton = getElement ( 'approve' )
480481 const transferFromButton = getElement ( 'transfer_from' )
@@ -491,36 +492,37 @@ async function walletInitialized() {
491492
492493 const deployContract = getElement ( 'deploy_contract' )
493494
494- async function authed ( address ) {
495+ function authed ( address , oauthCode = undefined ) {
495496 if ( ! address || address === '' ) {
496497 return unAuthed ( )
497498 }
498- const oauthCode = await provider . request ( {
499- method : 'anyweb_oauth' ,
500- } )
501499 getElement ( 'address' ) . innerHTML = address
502- getElement ( 'oauth_code' ) . innerHTML = oauthCode
500+ if ( oauthCode ) {
501+ getElement ( 'oauth_code' ) . innerHTML = oauthCode
502+ }
503503 console . log ( 'authed address: ' , address )
504- console . log ( 'OAuth Code: ' , oauthCode )
505504 sendNativeTokenButton . disabled = false
506505 approveButton . disabled = false
507506 transferFromButton . disabled = false
508507 deployContract . disabled = false
509508 getCFXButton . disabled = false
510509 connectButton . disabled = true
510+ logoutButton . disabled = false
511511 importAddressButton . disabled = false
512512 importPrivateKeyButton . disabled = false
513513 }
514514
515515 function unAuthed ( ) {
516516 getElement ( 'address' ) . innerHTML = 'N/A'
517+ getElement ( 'oauth_code' ) . innerHTML = 'N/A'
517518 console . log ( 'unauthed' )
518519 sendNativeTokenButton . disabled = true
519520 approveButton . disabled = true
520521 transferFromButton . disabled = true
521522 getCFXButton . disabled = true
522523 deployContract . disabled = true
523524 connectButton . disabled = false
525+ logoutButton . disabled = true
524526 importAddressButton . disabled = true
525527 importPrivateKeyButton . disabled = true
526528 }
@@ -547,18 +549,21 @@ async function walletInitialized() {
547549 getElement ( 'version' ) . innerHTML = version
548550 } )
549551
550- const [ chainId , networkId , alreadyAuthedAddresses ] = await Promise . all ( [
552+ const [ chainId , networkId , authResult ] = await Promise . all ( [
551553 provider . request ( { method : 'cfx_chainId' } ) ,
552554 provider . request ( { method : 'cfx_netVersion' } ) ,
553555 provider . request ( {
554556 method : 'cfx_accounts' ,
555557 params : [
556558 {
557559 availableNetwork : [ 1 , 1029 ] ,
560+ scopes : [ 'baseinfo' , 'phone' ] ,
558561 } ,
559562 ] ,
560563 } ) ,
561564 ] )
565+ const { address : alreadyAuthedAddresses , code, scopes } = authResult
566+ console . log ( 'DApp 获取到的授权结果' , authResult )
562567
563568 getElement ( 'initialized' ) . innerHTML = 'initialized'
564569 getElement ( 'chainId' ) . innerHTML = chainId
@@ -571,19 +576,32 @@ async function walletInitialized() {
571576 ) {
572577 unAuthed ( )
573578 } else {
574- authed ( alreadyAuthedAddresses [ 0 ] )
579+ authed ( alreadyAuthedAddresses [ 0 ] , code )
575580 }
576581 } catch ( e ) {
577582 unAuthed ( )
578583 console . error ( 'try 到错误了' , e )
579584 }
580585
581- connectButton . onclick = ( ) => {
586+ connectButton . onclick = async ( ) => {
587+ const { address, code } = await provider . request ( {
588+ method : 'cfx_accounts' ,
589+ params : [
590+ {
591+ availableNetwork : [ 1 , 1029 ] ,
592+ scopes : [ 'baseinfo' , 'phone' ] ,
593+ } ,
594+ ] ,
595+ } )
596+ authed ( address , code )
597+ }
598+
599+ logoutButton . onclick = ( ) => {
582600 provider
583601 . request ( {
584- method : 'cfx_requestAccounts ' ,
602+ method : 'anyweb_logout ' ,
585603 } )
586- . then ( authed )
604+ . then ( unAuthed )
587605 . catch ( console . error )
588606 }
589607
0 commit comments