1- import { MsgAddLAK , MsgSignWithFAK , MsgSignIn , Permissions , MsgSignInQueryParams } from "@one-click-connect/core" ;
1+ import { MsgAddLAK , MsgSignWithFAK , MsgSignIn , Permissions , MsgSignInQueryParams , Codec } from "@one-click-connect/core" ;
22import { BaseDAppClientConfig } from "./base.client.config" ;
33import { Account } from "../../common" ;
4- import { AccountStore , Callbacks , PendingTransactionStore , Wallet } from "../../common" ;
4+ import { AccountStore , Callbacks , PendingTransactionStore , Provider } from "../../common" ;
55
66export abstract class BaseDAppClient < Transaction , TransactionResult , Config extends BaseDAppClientConfig = BaseDAppClientConfig > {
77 protected account ?: Account ;
88
99 protected constructor (
1010 protected config : Config ,
11+ protected codec : Codec < Transaction , string > ,
1112 protected permissions : Permissions ,
1213 protected callbacks : Callbacks ,
13- protected wallet : Wallet < Transaction , TransactionResult > ,
14+ protected provider : Provider < Transaction , TransactionResult > ,
1415 protected accountStore : AccountStore ,
1516 protected pendingTransactionStore ?: PendingTransactionStore < Transaction > ,
1617 ) { }
@@ -32,7 +33,7 @@ export abstract class BaseDAppClient<Transaction, TransactionResult, Config exte
3233 get publicKey ( ) {
3334 if ( ! this . connected ) throw new Error ( "Not connected" ) ;
3435 if ( ! this . accessKey ) throw new Error ( "Access key undefined" ) ;
35- return this . wallet . derivePublicKey ( this . accessKey ) ;
36+ return this . provider . derivePublicKey ( this . accessKey ) ;
3637 }
3738
3839 /**
@@ -68,21 +69,27 @@ export abstract class BaseDAppClient<Transaction, TransactionResult, Config exte
6869 */
6970 async signAndSendTransaction ( transaction : Transaction ) : Promise < TransactionResult | void > {
7071 if ( ! this . connected ) throw new Error ( "Not connected" ) ;
71- if ( ! this . account ! . accessKey ) {
72- this . account ! . accessKey = await this . wallet . generateAccessKey ( ) ;
73- await this . accountStore . set ( this . account ! ) ;
74- if ( this . pendingTransactionStore ) await this . pendingTransactionStore . push ( this . account ! . accountId , transaction ) ;
75- const publicKey = await this . wallet . derivePublicKey ( this . account ! . accessKey ) ;
72+ const hasAccessKey = this . account ! . accessKey !== undefined ;
73+ const canExecute = await this . provider . canExecute ( this . permissions , transaction ) ;
74+ const hasPermissions =
75+ hasAccessKey && ( await this . provider . canAccessKeyExecute ( this . account ! . accountId , this . account ! . accessKey ! , transaction ) ) ;
7676
77- const msgAddLAK = new MsgAddLAK ( this . config . redirectURL , this . permissions , publicKey ) ;
78- return await this . callbacks . onRequestAddLAK ( msgAddLAK . toURL ( this . account ! . signingURL ) ) ;
77+ if ( ! canExecute ) {
78+ // Can't execute transaction with the established permissions
79+ const msgSignWithFAK = new MsgSignWithFAK ( this . codec , [ transaction ] , this . config . redirectURL ) ;
80+ return await this . callbacks . onRequestSignWithFAK ( msgSignWithFAK . toURL ( this . account ! . signingURL ) ) ;
7981 } else {
80- const hasPermissions = await this . wallet . canAccessKeyExecute ( this . account ! . accountId , this . account ! . accessKey , transaction ) ;
81- if ( hasPermissions ) {
82- return await this . wallet . signAndSendTransaction ( this . account ! . accountId , this . account ! . accessKey , transaction ) ;
82+ if ( ! hasAccessKey || ( hasAccessKey && ! hasPermissions ) ) {
83+ // Access key is not stored or access key is stored but doesn't have permissions (removed or badly created)
84+ this . account ! . accessKey = await this . provider . generateAccessKey ( ) ;
85+ await this . accountStore . set ( this . account ! ) ;
86+ if ( this . pendingTransactionStore ) await this . pendingTransactionStore . push ( this . account ! . accountId , transaction ) ;
87+ const publicKey = this . provider . derivePublicKey ( this . account ! . accessKey ) ;
88+ const msgAddLAK = new MsgAddLAK ( this . config . redirectURL , this . permissions , publicKey ) ;
89+ return await this . callbacks . onRequestAddLAK ( msgAddLAK . toURL ( this . account ! . signingURL ) ) ;
8390 } else {
84- const msgSignWithFAK = new MsgSignWithFAK ( [ transaction ] , this . config . redirectURL ) ;
85- return await this . callbacks . onRequestSignWithFAK ( msgSignWithFAK . toURL ( this . account ! . signingURL ) ) ;
91+ // Access key is stored and has permissions
92+ return await this . provider . signAndSendTransaction ( this . account ! . accountId , this . account ! . accessKey ! , transaction ) ;
8693 }
8794 }
8895 }
@@ -95,7 +102,7 @@ export abstract class BaseDAppClient<Transaction, TransactionResult, Config exte
95102 async signMessage ( message : string ) : Promise < string > {
96103 if ( ! this . connected ) throw new Error ( "Not connected" ) ;
97104 if ( ! this . accessKey ) throw new Error ( "Access key undefined" ) ;
98- return this . wallet . signMessage ( this . accessKey , message ) ;
105+ return this . provider . signMessage ( this . accessKey , message ) ;
99106 }
100107
101108 /**
0 commit comments