Skip to content

Commit

Permalink
feat: wallet 新增悬浮按钮配置+更改默认chain+增加切换chain
Browse files Browse the repository at this point in the history
  • Loading branch information
chenweigh committed Sep 20, 2024
1 parent b296af5 commit 27fbc96
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
8 changes: 7 additions & 1 deletion xterio-wallet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ const config: IXterioWalletContextProps = {
client_id:'',
client_secret: '',
redirect_uri:'',
env: Env.Dev
env: Env.Dev,
//是否在wallet内部初始化auth,default: true
enableAuthInit = true,
//是否显示快捷按钮wallet,default: false
showOpenWalletIcon = false,
}
createRoot(document.getElementById('root')!).render(
<StrictMode>
Expand Down Expand Up @@ -179,6 +183,8 @@ aa wallet connection status

#### `signMessage()`

#### `switchChain(_id?: number)`

### 3.3 `useXterioTransaction(contract?, funcName?)`

#### `state`
Expand Down
4 changes: 2 additions & 2 deletions xterio-wallet/src/common/utils/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ export const EnvBaseURLConst: Record<Env, EnvItemType> = {
PN_APP_ID: '40ad8524-f844-496d-8de2-50a8a322d6ba'
},
[Env.Staging]: {
PN_CHAIN_ID: 11155111,
PN_CHAIN_ID: 1637450,
PN_PROJECT_ID: '6b4a0cd5-32c3-4641-a809-2bd9ac7a175f',
PN_CLIENT_KEY: 'cU43QlI3GMvPqW4Ep0Z8BK4mXDkaHd8kgpQlr8NG',
PN_APP_ID: '9cd6a325-3082-4e98-8803-82a66cd9e86f'
},
[Env.Production]: {
PN_CHAIN_ID: 56,
PN_CHAIN_ID: 112358,
PN_PROJECT_ID: 'fab00091-f966-437f-8ae9-12aa495f2828',
PN_CLIENT_KEY: 'cif8thrddJ9Iz46tecZ9UiEQmjxRaKy42AuutAZj',
PN_APP_ID: '926a55ad-1aad-4147-901e-ee66ff288e74'
Expand Down
22 changes: 14 additions & 8 deletions xterio-wallet/src/contexts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ const initState = {
disconnectWallet: () => {},
obtainWallet: () => {}
}
interface IWalletContextState extends Pick<IPnWalletState, 'signMessage' | 'signTypedData'> {
interface IWalletContextState extends Pick<IPnWalletState, 'signMessage' | 'signTypedData' | 'switchChain'> {
userinfo: IUserInfo | undefined
isLogin: boolean
login(mode?: LoginType): Promise<void>
logout(): Promise<void>
aaAddress: string
isConnect: boolean
openWallet(): void
connectWallet(): void
connectWallet(chainId?: number): void
disconnectWallet(): void
obtainWallet(): void
}
Expand All @@ -34,6 +34,7 @@ const WalletContextProvider: React.FC<PropsWithChildren<IXterioWalletContextProp
children,
env,
enableAuthInit = true,
showOpenWalletIcon = false,
...rest
}) => {
const [mounted, setMounted] = useState<boolean>()
Expand All @@ -47,6 +48,7 @@ const WalletContextProvider: React.FC<PropsWithChildren<IXterioWalletContextProp
connectPnAA,
connectPnEoA,
disconnectPnEoA,
switchChain,
pnUserInfo: _p,
isLogin: isPnLogin,
signMessage,
Expand Down Expand Up @@ -97,10 +99,13 @@ const WalletContextProvider: React.FC<PropsWithChildren<IXterioWalletContextProp
}
}, [_p, aaAddress, connectPnAA, connectPnEoA, isLogin, isPnLogin])

const connectWallet = useCallback(async () => {
log('connect wallet')
await connectPnEoAAndAA(XterioAuthTokensManager.idToken)
}, [connectPnEoAAndAA])
const connectWallet = useCallback(
async (chainId?: number) => {
log('connect wallet')
await connectPnEoAAndAA(XterioAuthTokensManager.idToken, chainId)
},
[connectPnEoAAndAA]
)

const disconnectWallet = useCallback(async () => {
log('disconnect wallet')
Expand Down Expand Up @@ -194,11 +199,12 @@ const WalletContextProvider: React.FC<PropsWithChildren<IXterioWalletContextProp
openWallet,
disconnectWallet,
signMessage,
signTypedData
signTypedData,
switchChain
}}
>
{children}
{!!isPnLogin && (
{showOpenWalletIcon && !!isPnLogin && (
<div id="xterio-wallet-btn" onClick={openWallet}>
Wallet
</div>
Expand Down
3 changes: 2 additions & 1 deletion xterio-wallet/src/contexts/pnWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const supportChains: [Chain, ...Chain[]] = [
]

export const usePnWallet = (init_address?: string, _env?: Env): IPnWalletState => {
const { chainInfo, address, provider, signMessage, signTypedData } = useEthereum()
const { chainInfo, address, provider, signMessage, signTypedData, switchChain } = useEthereum()
const { connect, connected, disconnect } = useConnect()
const { erc4337, setERC4337 } = useCustomize()
const { userInfo, getWalletIFrame: getWalletDom, openWallet: _openWallet } = useAuthCore()
Expand Down Expand Up @@ -200,6 +200,7 @@ export const usePnWallet = (init_address?: string, _env?: Env): IPnWalletState =
connectPnEoAAndAA,
getWalletIFrame,
openPnWallet,
switchChain,
eoaAddress: address || init_address || '',
pnAAWalletAddress,
pnUserInfo: userInfo,
Expand Down
2 changes: 2 additions & 0 deletions xterio-wallet/src/interfaces/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { Env, ISSoTokensParams } from '@xterio-sdk/auth'
export interface IXterioWalletContextProps extends Partial<ISSoTokensParams> {
env?: Env
enableAuthInit?: boolean
showOpenWalletIcon?: boolean
}

export type PnUserInfoType = import('@particle-network/auth-core').UserInfo | undefined
Expand All @@ -28,6 +29,7 @@ export interface IPnWalletState {
_eoaAddress?: string
) => Promise<{ aaAddress?: string; eoaAddress?: string; name?: string; version?: string }>
connectPnEoAAndAA: (jwt?: string, _chainId?: number) => Promise<void>
switchChain: (id: number | PrefixedHexString) => Promise<void>
getWalletIFrame: () => HTMLIFrameElement | null
openPnWallet: () => void
signMessage: (message: PrefixedHexString | string, uniq?: boolean) => Promise<string>
Expand Down

0 comments on commit 27fbc96

Please sign in to comment.