diff --git a/src/errors/ErrorMessages.ts b/src/errors/ErrorMessages.ts index d92c9ec7..b710e144 100644 --- a/src/errors/ErrorMessages.ts +++ b/src/errors/ErrorMessages.ts @@ -26,5 +26,6 @@ export enum ERROR_MESSAGES { ROLE_PRECONDITION_NOT_MET = "Precondition not met, user not eligible to enrol for a role", ROLE_NOT_EXISTS = "Role you want to enroll to does not exists", CLAIM_PUBLISHER_NOT_REQUESTER = "Claim subject is not controlled by publisher", - ONCHAIN_ROLE_VERSION_NOT_SPECIFIED = "On-chain role version not specified" + ONCHAIN_ROLE_VERSION_NOT_SPECIFIED = "On-chain role version not specified", + CACHE_SERVER_NOT_REGISTERED = "Cache server for this chain is not registered" } diff --git a/src/iam.ts b/src/iam.ts index d2c251ae..06b6880d 100644 --- a/src/iam.ts +++ b/src/iam.ts @@ -170,10 +170,16 @@ export class IAM extends IAMBase { */ async initializeConnection({ walletProvider = this._providerType, - reinitializeMetamask - }: { walletProvider?: WalletProvider; reinitializeMetamask?: boolean } = {}): Promise< - InitializeData - > { + reinitializeMetamask, + initCacheServer = true, + initDID = true + }: { + walletProvider?: WalletProvider; + reinitializeMetamask?: boolean; + initCacheServer?: boolean, + initDID?: boolean + } = {} + ): Promise { const { privateKey } = this._connectionOptions; if (!walletProvider && !privateKey) { @@ -187,6 +193,12 @@ export class IAM extends IAMBase { initializeMetamask: reinitializeMetamask, walletProvider }); + if (initCacheServer) { + await this.connectToCacheServer(); + } + if (initDID) { + await this.connectToDIDRegistry(); + } } catch (err) { if (err.message === "User closed modal") { return { diff --git a/src/iam/iam-base.ts b/src/iam/iam-base.ts index 0e0576a7..5bd06b24 100644 --- a/src/iam/iam-base.ts +++ b/src/iam/iam-base.ts @@ -157,35 +157,15 @@ export class IAMBase { walletProvider?: WalletProvider; }) { await this.initSigner({ walletProvider, initializeMetamask }); + await this.setAddress(); + this.setDid(); await this.initChain(); this.initEventHandlers(); if (this._runningInBrowser) { await this.setupMessaging(); } - if (this._signer) { - const fromCacheLogin = await this.loginToCacheServer(); - this._publicKey = fromCacheLogin?.publicKey ?? this._publicKey; - this._identityToken = fromCacheLogin?.identityToken; - - // We need a pubKey to create DID document. - // So if didn't get one from cache server login and there wasn't one saved - // then need to request signature to compute one. - if (!this._publicKey) { - const { publicKey, identityToken } = await getPublicKeyAndIdentityToken(this._signer); - this._publicKey = publicKey; - this._identityToken = identityToken; - } - if (!this._publicKey) { - throw new Error(ERROR_MESSAGES.UNABLE_TO_OBTAIN_PUBLIC_KEY); - } - this._didSigner = new Owner(this._signer, this._provider, this._publicKey); - await this.setAddress(); - this.setDid(); - await this.setDocument(); - this.setClaims(); - } this.setResolver(); this.setJWT(); this.storeSession(); @@ -269,6 +249,41 @@ export class IAMBase { throw new Error(ERROR_MESSAGES.WALLET_TYPE_NOT_PROVIDED); } + /** + * @description established connection to cache server and logins in signing authentication token + */ + async connectToCacheServer() { + const { chainId } = await this._provider.getNetwork(); + const cacheOptions = cacheServerClientOptions[chainId]; + if (!this._signer) { + throw new Error(ERROR_MESSAGES.SIGNER_NOT_INITIALIZED); + } + if (!cacheOptions.url) { + throw new Error(ERROR_MESSAGES.CACHE_SERVER_NOT_REGISTERED); + } + this._cacheClient = new CacheServerClient(cacheOptions, this._signer); + const fromCacheLogin = await this.loginToCacheServer(); + this._publicKey = fromCacheLogin?.publicKey ?? this._publicKey; + this._identityToken = fromCacheLogin?.identityToken; + } + + /** + * @description creates users DID document if it is not yet exist + */ + async connectToDIDRegistry() { + if (!this._signer) { + throw new Error(ERROR_MESSAGES.SIGNER_NOT_INITIALIZED); + } + if (!this._publicKey) { + const { publicKey, identityToken } = await getPublicKeyAndIdentityToken(this._signer); + this._publicKey = publicKey; + this._identityToken = identityToken; + } + this._didSigner = new Owner(this._signer, this._provider, this._publicKey); + await this.setDocument(); + this.setClaims(); + } + /** * Check if session is active * @@ -413,8 +428,8 @@ export class IAMBase { } private setJWT() { - if (this._didSigner) { - this._jwt = new JWT(this._didSigner); + if (this._signer) { + this._jwt = new JWT(this._signer); return; } throw new Error(ERROR_MESSAGES.SIGNER_NOT_INITIALIZED); @@ -651,9 +666,7 @@ export class IAMBase { }); this._claimManager = ClaimManager__factory.connect(claimManagerAddress, this._signer); - const cacheOptions = cacheServerClientOptions[chainId]; - cacheOptions.url && (this._cacheClient = new CacheServerClient(cacheOptions, this._signer)); this._messagingOptions = messagingOptions[chainId]; }