From 140f62ddbf56b6aeca30198164a3b2dde26fa41b Mon Sep 17 00:00:00 2001
From: AhsanJavaid <ahsan.javaid@tintash.com>
Date: Mon, 8 Nov 2021 20:31:57 +0500
Subject: [PATCH] fix: remove circular dependencies form keychain package

---
 packages/keychain/src/common.ts   | 51 +++++++++++++++++++++++++++++++
 packages/keychain/src/identity.ts | 10 +++---
 packages/keychain/src/index.ts    |  1 +
 packages/keychain/src/profiles.ts | 29 +-----------------
 4 files changed, 58 insertions(+), 33 deletions(-)
 create mode 100644 packages/keychain/src/common.ts

diff --git a/packages/keychain/src/common.ts b/packages/keychain/src/common.ts
new file mode 100644
index 000000000..f5c0ff242
--- /dev/null
+++ b/packages/keychain/src/common.ts
@@ -0,0 +1,51 @@
+import { IdentityKeyPair } from './utils';
+
+interface RefreshOptions {
+  gaiaUrl: string;
+}
+
+export interface Identity {
+  keyPair: IdentityKeyPair;
+  address: string;
+  usernames: string[];
+  defaultUsername?: string;
+  profile?: Profile;
+  profileUrl(gaiaUrl: string): Promise<string>;
+  appPrivateKey(appDomain: string): string;
+  fetchNames(): Promise<string[]>;
+  refresh(opts: RefreshOptions): void;
+  makeAuthResponse(options: {
+    appDomain: string;
+    gaiaUrl: string;
+    transitPublicKey: string;
+    scopes: string[] | undefined;
+    stxAddress: string | undefined;
+  }): Promise<string>;
+}
+
+const PERSON_TYPE = 'Person';
+const CONTEXT = 'http://schema.org';
+const IMAGE_TYPE = 'ImageObject';
+
+export interface ProfileImage {
+  '@type': typeof IMAGE_TYPE;
+  name: string;
+  contentUrl: string;
+}
+
+export interface Profile {
+  '@type': typeof PERSON_TYPE;
+  '@context': typeof CONTEXT;
+  apps?: {
+    [origin: string]: string;
+  };
+  appsMeta?: {
+    [origin: string]: {
+      publicKey: string;
+      storage: string;
+    };
+  };
+  name?: string;
+  image?: ProfileImage[];
+  [key: string]: any;
+}
diff --git a/packages/keychain/src/identity.ts b/packages/keychain/src/identity.ts
index 37c3164a7..0607f7d2b 100644
--- a/packages/keychain/src/identity.ts
+++ b/packages/keychain/src/identity.ts
@@ -3,8 +3,8 @@ import { bip32, ECPair } from 'bitcoinjs-lib';
 import { getPublicKeyFromPrivate } from '@stacks/encryption';
 import { makeAuthResponse } from '@stacks/auth';
 import { getProfileURLFromZoneFile } from './utils';
-
-import { IdentityKeyPair } from './utils/index';
+import { Profile, Identity as IdentifyInterface } from './common';
+import { IdentityKeyPair } from './utils';
 import {
   makeGaiaAssociationToken,
   DEFAULT_GAIA_HUB,
@@ -12,7 +12,7 @@ import {
   connectToGaiaHubWithConfig,
 } from './utils/gaia';
 import IdentityAddressOwnerNode from './nodes/identity-address-owner-node';
-import { Profile, fetchProfile, DEFAULT_PROFILE, signAndUploadProfile } from './profiles';
+import { fetchProfile, DEFAULT_PROFILE, signAndUploadProfile } from './profiles';
 import { ecPairToAddress } from '@stacks/encryption';
 
 interface IdentityConstructorOptions {
@@ -27,7 +27,7 @@ interface RefreshOptions {
   gaiaUrl: string;
 }
 
-export class Identity {
+export class Identity implements IdentifyInterface {
   public keyPair: IdentityKeyPair;
   public address: string;
   public defaultUsername?: string;
@@ -64,7 +64,7 @@ export class Identity {
     const appPrivateKey = this.appPrivateKey(appDomain);
     const hubInfo = await getHubInfo(gaiaUrl);
     const profileUrl = await this.profileUrl(hubInfo.read_url_prefix);
-    const profile =
+    const profile: Profile =
       (await fetchProfile({ identity: this, gaiaUrl: hubInfo.read_url_prefix })) || DEFAULT_PROFILE;
     if (scopes.includes('publish_data')) {
       if (!profile.apps) {
diff --git a/packages/keychain/src/index.ts b/packages/keychain/src/index.ts
index f9ed0bc8c..ece6d096e 100644
--- a/packages/keychain/src/index.ts
+++ b/packages/keychain/src/index.ts
@@ -9,6 +9,7 @@ export { decrypt } from './encryption/decrypt';
 export { encrypt } from './encryption/encrypt';
 export * from './profiles';
 export * from './identity';
+export { Profile } from './common';
 
 export default {
   Wallet,
diff --git a/packages/keychain/src/profiles.ts b/packages/keychain/src/profiles.ts
index f3682ba38..57136f6ce 100644
--- a/packages/keychain/src/profiles.ts
+++ b/packages/keychain/src/profiles.ts
@@ -1,37 +1,10 @@
 import { connectToGaiaHub } from '@stacks/storage';
 import { signProfileToken, wrapProfileToken, makeProfileZoneFile } from '@stacks/profile';
+import { Identity, Profile } from './common';
 import { IdentityKeyPair } from './utils';
 import { uploadToGaiaHub } from './utils/gaia';
-import Identity from './identity';
 import { GaiaHubConfig } from '@stacks/storage';
 
-const PERSON_TYPE = 'Person';
-const CONTEXT = 'http://schema.org';
-const IMAGE_TYPE = 'ImageObject';
-
-export interface ProfileImage {
-  '@type': typeof IMAGE_TYPE;
-  name: string;
-  contentUrl: string;
-}
-
-export interface Profile {
-  '@type': typeof PERSON_TYPE;
-  '@context': typeof CONTEXT;
-  apps?: {
-    [origin: string]: string;
-  };
-  appsMeta?: {
-    [origin: string]: {
-      publicKey: string;
-      storage: string;
-    };
-  };
-  name?: string;
-  image?: ProfileImage[];
-  [key: string]: any;
-}
-
 export const DEFAULT_PROFILE: Profile = {
   '@type': 'Person',
   '@context': 'http://schema.org',