Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎸 service load storage token #1650

Open
wants to merge 2 commits into
base: feature/contract-1.4.13
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions packages/api/api-did/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import { sleep } from '@portkey-wallet/utils';
import im from '@portkey-wallet/im';
import { IM_TOKEN_ERROR_ARRAY } from '@portkey-wallet/im/constant';
import signalrFCM from '@portkey-wallet/socket/socket-fcm';
import { IStorageSuite } from '@portkey/types';

const C_T_EVENT_NAME = 'connectTokenChange';
const C_T_STORAGE_KEY = 'DidServiceConnectTokenStorageKey';
export class DidService extends ServiceInit {
protected refreshTokenConfig?: RefreshTokenConfig;
protected onLockApp?: (expired?: boolean) => void;
protected storage?: IStorageSuite;
locked?: boolean;
exceptionManager?: IExceptionManager;
constructor() {
Expand All @@ -34,9 +37,8 @@ export class DidService extends ServiceInit {
try {
if (!this.refreshTokenConfig || !isValidRefreshTokenConfig(this.refreshTokenConfig)) return;
const authorization = await queryAuthorization(this.refreshTokenConfig);
this.set('headers', { Authorization: authorization });
this.emitConnectTokenChange(authorization);

this.saveAuthorization(authorization);
this.setAuthorization(authorization);
this.locked = false;
return authorization;
} catch (error) {
Expand All @@ -52,6 +54,7 @@ export class DidService extends ServiceInit {
...this.defaultConfig.headers,
Authorization: '',
};
this.storage?.removeItem(C_T_STORAGE_KEY);
};

setRefreshTokenConfig = (config: RefreshTokenConfig) => {
Expand Down Expand Up @@ -133,6 +136,26 @@ export class DidService extends ServiceInit {
rep: fetchResult,
});
};

initStorageAuthorization = async () => {
try {
const authorization = await this.storage?.getItem(C_T_STORAGE_KEY);
if (authorization) this.setAuthorization(authorization);
} catch (error) {
this.exceptionManager?.reportError(error, Severity.Fatal);
}
};
setStorage = (storage: IStorageSuite) => {
this.storage = storage;
this.initStorageAuthorization();
};
setAuthorization = (authorization: string) => {
this.set('headers', { Authorization: authorization });
this.emitConnectTokenChange(authorization);
};
saveAuthorization = (authorization: string) => {
this.storage?.setItem(C_T_STORAGE_KEY, authorization);
};
}

const didServer = new DidService();
Expand Down
2 changes: 2 additions & 0 deletions packages/mobile-app-did/js/components/Updater/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import s3Instance from '@portkey-wallet/utils/s3';
import Config from 'react-native-config';
import { useCheckContactMap } from '@portkey-wallet/hooks/hooks-ca/contact';
import { useAppEntrance } from 'hooks/cms';
import { baseStore } from '@portkey-wallet/utils/mobile/storage';

request.setExceptionManager(exceptionManager);
export default function Updater() {
Expand All @@ -55,6 +56,7 @@ export default function Updater() {
useFetchTxFee();
useMemo(() => {
request.set('baseURL', apiUrl);
request.setStorage(baseStore);
if (service.defaults.baseURL !== apiUrl) {
service.defaults.baseURL = apiUrl;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { request } from '@portkey-wallet/api/api-did';
import BigNumber from 'bignumber.js';
import { localStorage } from 'redux-persist-webextension-storage';

export function initConfig() {
BigNumber.set({ ROUNDING_MODE: BigNumber.ROUND_DOWN });
}

export function initRequest() {
request.set('headers', { version: `${process.env.SDK_VERSION}` });
request.setStorage(localStorage);
}