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: support fix devMode through sdk url #202

Merged
merged 1 commit into from
Nov 4, 2024
Merged
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
40 changes: 35 additions & 5 deletions apps/wallet/src/stores/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { makeAutoObservable, reaction } from "mobx";
import { ChainWalletPool } from "../utils/chain/chain-wallets";
import { ChainId, ChainInfo } from "../utils/basicTypes";
import { Ethereum, EthereumSepolia, Ton, TonTestnet } from "../utils/chain/chain-list";
import { IS_TELEGRAM_MINI_APP, RUNTIME_ENV, RUNTIME_LANG } from "../utils/runtime";
import { IS_TELEGRAM_MINI_APP, RUNTIME_ENV, RUNTIME_FIX_DEV_MODE, RUNTIME_LANG } from "../utils/runtime";
import { HibitEnv, RuntimeEnv } from "../utils/basicEnums";
import rpcManager from "./rpc";
import { WalletAccount } from "@delandlabs/hibit-id-sdk";
Expand Down Expand Up @@ -33,13 +33,20 @@ export class HibitIdSession {
public chainInfo: ChainInfo
public config: SessionConfig = {
lastChainId: '',
devMode: HIBIT_ENV === HibitEnv.PROD ? false : true,
devMode: RUNTIME_FIX_DEV_MODE === 'on'
? true
: RUNTIME_FIX_DEV_MODE === 'off'
? false
: HIBIT_ENV === HibitEnv.PROD
? false
: true,
lang: getSystemLang(),
}

private _mnemonic: GetMnemonicResult | null = null
private _password: string | null = null
private _account: WalletAccount | null = null
private _initialStoredDevMode: boolean = this.config.devMode

constructor() {
makeAutoObservable(this)
Expand All @@ -52,10 +59,16 @@ export class HibitIdSession {
const configString = localStorage.getItem(SESSION_CONFIG_KEY)
if (configString) {
const config = JSON.parse(configString) as SessionConfig
this._initialStoredDevMode = config.devMode
this.config = {
...this.config,
...config,
lang: RUNTIME_LANG || config.lang,
devMode: RUNTIME_FIX_DEV_MODE === 'on'
? true
: RUNTIME_FIX_DEV_MODE === 'off'
? false
: config.devMode
}
i18n.changeLanguage(this.config.lang)
const chainId = ChainId.fromString(this.config.lastChainId)
Expand Down Expand Up @@ -119,11 +132,19 @@ export class HibitIdSession {
public setChainInfo = (chainInfo: ChainInfo) => {
this.chainInfo = chainInfo
this.config.lastChainId = chainInfo.chainId.toString()
localStorage.setItem(SESSION_CONFIG_KEY, JSON.stringify(this.config))
this.persistConfig(this.config)
}

public setDevMode = (devMode: boolean) => {
if (this.config.devMode === devMode) return
if (RUNTIME_FIX_DEV_MODE === 'on' && !devMode) {
toaster.error(t('page_settings_devModeOnlyTestnet'))
return
}
if (RUNTIME_FIX_DEV_MODE === 'off' && devMode) {
toaster.error(t('page_settings_devModeOnlyMainnet'))
return
}
const newChain = getDevModeSwitchChain(!devMode, this.chainInfo.chainId)
if (!newChain) {
toaster.error(devMode ? t('page_settings_devModeOnlyMainnet') : t('page_settings_devModeOnlyTestnet'))
Expand All @@ -137,7 +158,7 @@ export class HibitIdSession {
console.error(e)
this.config.devMode = !devMode
} finally {
localStorage.setItem(SESSION_CONFIG_KEY, JSON.stringify(this.config))
this.persistConfig(this.config)
}
})
}
Expand All @@ -146,7 +167,7 @@ export class HibitIdSession {
if (this.config.lang === lang) return
await i18n.changeLanguage(lang)
this.config.lang = lang
localStorage.setItem(SESSION_CONFIG_KEY, JSON.stringify(this.config))
this.persistConfig(this.config)
}

public getValidAddress = async () => {
Expand Down Expand Up @@ -255,6 +276,15 @@ export class HibitIdSession {
const pool = new ChainWalletPool(phrase)
return pool
}

private persistConfig = (config: SessionConfig) => {
localStorage.setItem(SESSION_CONFIG_KEY, JSON.stringify({
...config,
devMode: RUNTIME_ENV !== RuntimeEnv.WEB
? this._initialStoredDevMode
: config.devMode
} as SessionConfig))
}
}

const hibitIdSession = new HibitIdSession()
Expand Down
6 changes: 6 additions & 0 deletions apps/wallet/src/utils/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { retrieveLaunchParams } from '@telegram-apps/sdk';
import { ChainId } from "./basicTypes";
import { Language } from "./lang";
import { Dfinity } from "./chain/chain-list";
import { FixDevMode } from "@delandlabs/hibit-id-sdk";

export const IS_IN_IFRAME = window.top !== window.self;

Expand All @@ -13,6 +14,7 @@ let runtimeParamsRaw: string | undefined = undefined;
let runtimeParams: unknown = undefined;
let runtimeSupportedChainIds: ChainId[] = [];
let runtimeLang: Language | undefined = undefined;
let runtimeFixDevMode: FixDevMode | undefined = undefined;

const urlParams = new URLSearchParams(window.location.search);

Expand Down Expand Up @@ -56,6 +58,8 @@ urlParams.get('chains')?.split(',').forEach((idStr) => {
}
})
runtimeLang = urlParams.get('lang') as Language || undefined
runtimeFixDevMode = urlParams.get('fixDevMode') as FixDevMode || undefined


export const IS_TELEGRAM_MINI_APP = isTelegramMiniApp;
export const RUNTIME_ENV = runtimeEnv
Expand All @@ -65,10 +69,12 @@ export const RUNTIME_PARAMS_RAW = runtimeParamsRaw
export const RUNTIME_PARAMS = runtimeParams
export const RUNTIME_SUPPORTED_CHAIN_IDS = runtimeSupportedChainIds
export const RUNTIME_LANG = runtimeLang
export const RUNTIME_FIX_DEV_MODE = runtimeFixDevMode

console.debug('[runtime env]', RUNTIME_ENV)
console.debug('[runtime icrc host]', RUNTIME_ICRC_HOST)
console.debug('[runtime icrc dev]', RUNTIME_ICRC_DEV)
console.debug('[runtime params]', RUNTIME_PARAMS)
console.debug('[runtime supported chains]', RUNTIME_SUPPORTED_CHAIN_IDS)
console.debug('[runtime lang]', RUNTIME_LANG)
console.debug('[runtime fixDevMode]', RUNTIME_FIX_DEV_MODE)
6 changes: 3 additions & 3 deletions packages/sdk/src/lib/dom/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CONTROLLER_CONTAINER_ID, IFRAME_CONTAINER_ID } from "../constants"
import { HibitIdChainId } from "../enums"
import { HibitEnv, Language } from "../types"
import { FixDevMode, HibitEnv, Language } from "../types"
import { clamp, getHibitIdUrl } from "../utils"
import './index.css'

Expand Down Expand Up @@ -156,7 +156,7 @@ export class HibitIdIframe {
private container: HTMLDivElement
private _visible = false

constructor(env: HibitEnv, chains: HibitIdChainId[] = [], urlAppendix: string = '', lang: Language | '' = '') {
constructor(env: HibitEnv, chains: HibitIdChainId[] = [], urlAppendix: string = '', lang: Language | '' = '', fixDevMode: FixDevMode = 'unset' ) {
this.isDesktop = window.innerWidth > 576
const existed = document.getElementById(IFRAME_CONTAINER_ID)
if (existed) {
Expand All @@ -167,7 +167,7 @@ export class HibitIdIframe {
const container = document.createElement('div')
container.id = IFRAME_CONTAINER_ID
const iframe = document.createElement('iframe')
iframe.src = `${getHibitIdUrl(env)}?chains=${chains.join(',')}&lang=${lang}${urlAppendix}`
iframe.src = `${getHibitIdUrl(env)}?chains=${chains.join(',')}&lang=${lang}&fixDevMode=${fixDevMode}${urlAppendix}`
iframe.allow='clipboard-write; publickey-credentials-get *; publickey-credentials-create *'
container.appendChild(iframe)
document.body.appendChild(container)
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export { BridgePromise } from './types'
export type {
HibitEnv,
Language,
FixDevMode,
HibitIdWalletOptions,
WalletAccount,
ConnectRequest,
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { TonConnectSignDataPayload, TonConnectSignDataResult, TonConnectTransact
export type HibitEnv = 'dev' | 'test' | 'prod'

export type Language = 'en' | 'cnt' | 'ja' | 'ru'
export type FixDevMode = 'on' | 'off' | 'unset'

export interface HibitIdWalletOptions {
env: HibitEnv
chains: HibitIdChainId[]
defaultChain: HibitIdChainId
lang?: Language
fixDevMode?: FixDevMode
iframeUrlAppendix?: string
}

Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/lib/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ export class HibitIdWallet {
this._options.chains,
this._options.iframeUrlAppendix,
this._options.lang,
this._options.fixDevMode ?? 'unset',
)

const rpc = new RPC({
Expand Down