Skip to content

Commit

Permalink
feat: separate conn to cache and DID reg
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiter committed Jul 12, 2021
1 parent 613ccf8 commit 06b990b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 31 deletions.
3 changes: 2 additions & 1 deletion src/errors/ErrorMessages.ts
Expand Up @@ -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"
}
20 changes: 16 additions & 4 deletions src/iam.ts
Expand Up @@ -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<InitializeData> {
const { privateKey } = this._connectionOptions;

if (!walletProvider && !privateKey) {
Expand All @@ -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 {
Expand Down
65 changes: 39 additions & 26 deletions src/iam/iam-base.ts
Expand Up @@ -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();
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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];
}
Expand Down

0 comments on commit 06b990b

Please sign in to comment.