11import { MsgAddLAK , MsgSignWithFAK , MsgSignIn , Permissions , MsgSignInQueryParams , Codec } from "@one-click-connect/core" ;
22import { BaseDAppClientConfig } from "./base.client.config" ;
3- import { Account } from "../../common" ;
3+ import { Account , ErrorCodes } from "../../common" ;
44import { AccountStore , Callbacks , PendingTransactionStore , Provider } from "../../common" ;
55
66export abstract class BaseDAppClient < Transaction , TransactionResult , Config extends BaseDAppClientConfig = BaseDAppClientConfig > {
@@ -21,18 +21,18 @@ export abstract class BaseDAppClient<Transaction, TransactionResult, Config exte
2121 }
2222
2323 get accountId ( ) {
24- if ( ! this . connected ) throw new Error ( "Not connected" ) ;
24+ if ( ! this . connected ) throw new Error ( ErrorCodes . NOT_CONNECTED ) ;
2525 return this . account ! . accountId ;
2626 }
2727
2828 get accessKey ( ) {
29- if ( ! this . connected ) throw new Error ( "Not connected" ) ;
29+ if ( ! this . connected ) throw new Error ( ErrorCodes . NOT_CONNECTED ) ;
3030 return this . account ! . accessKey ;
3131 }
3232
3333 get publicKey ( ) {
34- if ( ! this . connected ) throw new Error ( "Not connected" ) ;
35- if ( ! this . accessKey ) throw new Error ( "Access key undefined" ) ;
34+ if ( ! this . connected ) throw new Error ( ErrorCodes . NOT_CONNECTED ) ;
35+ if ( ! this . accessKey ) throw new Error ( ErrorCodes . NO_ACCESS_KEY ) ;
3636 return this . provider . derivePublicKey ( this . accessKey ) ;
3737 }
3838
@@ -55,7 +55,7 @@ export abstract class BaseDAppClient<Transaction, TransactionResult, Config exte
5555 account = await this . accountStore . get ( ) ;
5656 }
5757 if ( ! account ) {
58- throw new Error ( "Could not sign in using One Click Connect, no msgSignIn detected and no account stored" ) ;
58+ throw new Error ( ErrorCodes . COULD_NOT_CONNECT ) ;
5959 }
6060 this . account = account ;
6161 if ( executePendingTransactions ) await this . executePendingTransactions ( ) ;
@@ -68,7 +68,7 @@ export abstract class BaseDAppClient<Transaction, TransactionResult, Config exte
6868 * @returns The transaction result or void if executed a callback.
6969 */
7070 async signAndSendTransaction ( transaction : Transaction ) : Promise < TransactionResult | void > {
71- if ( ! this . connected ) throw new Error ( "Not connected" ) ;
71+ if ( ! this . connected ) throw new Error ( ErrorCodes . NOT_CONNECTED ) ;
7272 const hasAccessKey = this . account ! . accessKey !== undefined ;
7373 const canExecute = await this . provider . canExecute ( this . permissions , transaction ) ;
7474 const hasPermissions =
@@ -100,20 +100,20 @@ export abstract class BaseDAppClient<Transaction, TransactionResult, Config exte
100100 * @returns A promise that resolves to the signed message as a string.
101101 */
102102 async signMessage ( message : string ) : Promise < string > {
103- if ( ! this . connected ) throw new Error ( "Not connected" ) ;
104- if ( ! this . accessKey ) throw new Error ( "Access key undefined" ) ;
103+ if ( ! this . connected ) throw new Error ( ErrorCodes . NOT_CONNECTED ) ;
104+ if ( ! this . accessKey ) throw new Error ( ErrorCodes . NO_ACCESS_KEY ) ;
105105 return this . provider . signMessage ( this . accessKey , message ) ;
106106 }
107107
108108 /**
109109 * Executes any pending transactions stored in the pending transaction store.
110110 */
111111 async executePendingTransactions ( ) : Promise < void > {
112- if ( ! this . connected ) throw new Error ( "Not connected" ) ;
112+ if ( ! this . connected ) throw new Error ( ErrorCodes . NOT_CONNECTED ) ;
113113 if ( ! this . pendingTransactionStore ) return ;
114114 let pendingTransaction : Transaction | null = await this . pendingTransactionStore . pop ( this . account ! . accountId ) ;
115115 while ( pendingTransaction ) {
116- if ( ! this . account ?. accessKey ) throw new Error ( "Access key undefined" ) ;
116+ if ( ! this . account ?. accessKey ) throw new Error ( ErrorCodes . NO_ACCESS_KEY ) ;
117117 // NOTE: We wait for certain seconds to make sure transaction gets included in the blockchain
118118 await new Promise ( ( resolve ) => setTimeout ( resolve , 1500 ) ) ;
119119 await this . signAndSendTransaction ( pendingTransaction ) ;
0 commit comments