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: add siwe #1214

Open
wants to merge 19 commits into
base: main
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
7 changes: 7 additions & 0 deletions .changeset/friendly-pandas-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@ant-design/web3-common': minor
'@ant-design/web3-wagmi': minor
'@ant-design/web3': minor
---

feat: wagmi add siwe config
1 change: 1 addition & 0 deletions packages/common/src/locale/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const localeValues: RequiredLocale = {
copied: 'Copied!',
LCJove marked this conversation as resolved.
Show resolved Hide resolved
walletAddress: 'Wallet address',
moreWallets: 'More Wallets',
sign: 'Sign',
},
ConnectModal: {
title: 'Connect Wallet',
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/locale/zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const localeValues: RequiredLocale = {
copied: '复制成功!',
walletAddress: '钱包地址',
moreWallets: '更多钱包',
sign: '签名',
},
ConnectModal: {
title: '连接钱包',
Expand Down
23 changes: 22 additions & 1 deletion packages/common/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
export const enum ConnectStatus {
Connected = 'connected',
Disconnected = 'disconnected',
Signed = 'signed',
}

export interface Account {
address: string;
name?: string;
avatar?: string;
addresses?: readonly [`0x${string}`, ...`0x${string}`[]];
addresses?: [`0x${string}`, ...`0x${string}`[]] | readonly `0x${string}`[];
status?: ConnectStatus;
}

export enum ChainIds {
Expand Down Expand Up @@ -119,6 +126,9 @@ export interface UniversalWeb3ProviderInterface {

// For Bitcoin, tokenId is undefined.
getNFTMetadata?: (params: { address: string; tokenId?: bigint }) => Promise<NFTMetadata>;

// For Sign
sign?: SignConfig;
}

export interface Wallet extends WalletMetadata {
Expand Down Expand Up @@ -247,6 +257,7 @@ export interface RequiredLocale {
copied: string;
walletAddress: string;
moreWallets: string;
sign: string;
};
ConnectModal: {
title: string;
Expand Down Expand Up @@ -328,3 +339,13 @@ export type Token = {
contract?: string;
}[];
};

export interface SignConfig {
// required
signIn: (address: string) => Promise<void>;
signOut?: () => Promise<void>;

// signOutOnDisconnect?: boolean; // defaults true
// signOutOnAccountChange?: boolean; // defaults true
// signOutOnNetworkChange?: boolean; // defaults true
}
1 change: 0 additions & 1 deletion packages/common/src/web3-config-provider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const ProviderChildren: React.FC<
ConfigConsumerProps & { children?: React.ReactNode; parentContext?: ConfigConsumerProps }
> = (props) => {
const { children, parentContext, ...rest } = props;

const config = { ...parentContext };

Object.keys(rest).forEach((key) => {
Expand Down
7 changes: 7 additions & 0 deletions packages/wagmi/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
WalletMetadata,
} from '@ant-design/web3-common';
import type { Chain as WagmiChain } from 'viem';
import type { CreateSiweMessageParameters } from 'viem/siwe';
import type { Connector, CreateConnectorFn } from 'wagmi';

export interface WalletUseInWagmiAdapter extends Wallet {
Expand All @@ -31,3 +32,9 @@ export interface WalletFactory {
export type EIP6963Config = boolean | UniversalEIP6963Config;

export type ChainAssetWithWagmiChain = Chain & { wagmiChain?: WagmiChain };

export interface SIWEConfig {
getNonce: (address: string, chainId?: number) => Promise<string>;
createMessage: (args: CreateSiweMessageParameters) => string;
verifyMessage: (message: string, signature: string) => Promise<boolean>;
}
28 changes: 28 additions & 0 deletions packages/wagmi/src/wagmi-provider/__mocks__/wagmiBaseMock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { mainnet } from 'wagmi/chains';

const mockConnector = {
name: 'MetaMask',
};

export const wagmiBaseMock = {
useAccount: () => {
return {
chain: mainnet,
address: '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B',
connector: mockConnector,
};
},
useConfig: () => ({}),
useBalance: () => ({ data: {} }),
useSwitchChain: () => ({ switchChain: () => {} }),
useConnect: () => ({
connectors: [mockConnector],
connectAsync: async () => ({}),
}),
useDisconnect: () => ({
disconnectAsync: () => {},
}),
useEnsName: () => ({ data: null }),
useEnsAvatar: () => ({ data: null }),
useSignMessage: () => ({ signMessageAsync: async () => 'signMessage' }),
};
29 changes: 2 additions & 27 deletions packages/wagmi/src/wagmi-provider/__tests__/balance.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { describe, expect, it, vi } from 'vitest';
import type * as Wagmi from 'wagmi';
import { mainnet } from 'wagmi/chains';

import { wagmiBaseMock } from '../__mocks__/wagmiBaseMock';
import { MetaMask } from '../../wallets';
import { AntDesignWeb3ConfigProvider } from '../config-provider';

Expand All @@ -21,32 +22,8 @@ vi.mock('wagmi', async (importOriginal) => {
const actual = await importOriginal<typeof Wagmi>();
return {
...actual,
useConfig: () => {
return {};
},
...wagmiBaseMock,
// https://wagmi.sh/react/hooks/useAccount
useAccount: () => {
return {
chain: mainnet,
address: '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B',
connector: mockConnector,
};
},
useConnect: () => {
return {
connectors: [mockConnector],
};
},
useDisconnect: () => {
return {
disconnectAsync: () => {},
};
},
useSwitchChain: () => {
return {
switchChain: () => {},
};
},
useBalance: () => {
return {
data: {
Expand All @@ -56,8 +33,6 @@ vi.mock('wagmi', async (importOriginal) => {
},
};
},
useEnsName: () => ({}),
useEnsAvatar: () => ({}),
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { Connector, Config as WagmiConfig } from 'wagmi';
import type * as Wagmi from 'wagmi';
import { mainnet } from 'wagmi/chains';

import { wagmiBaseMock } from '../__mocks__/wagmiBaseMock';
import { TokenPocket } from '../../wallets';
import { AntDesignWeb3ConfigProvider } from '../config-provider';

Expand Down Expand Up @@ -37,9 +38,7 @@ vi.mock('wagmi', async (importOriginal) => {
const actual = await importOriginal<typeof Wagmi>();
return {
...actual,
useConfig: () => {
return {};
},
...wagmiBaseMock,
useAccount: () => {
const [connected, setConnected] = React.useState(false);
useEffect(() => {
Expand Down Expand Up @@ -72,16 +71,6 @@ vi.mock('wagmi', async (importOriginal) => {
},
};
},
useSwitchChain: () => {
return {
switchChain: () => {},
};
},
useBalance: () => {
return { data: {} };
},
useEnsName: () => ({ data: null }),
useEnsAvatar: () => ({ data: null }),
};
});

Expand Down
15 changes: 2 additions & 13 deletions packages/wagmi/src/wagmi-provider/__tests__/connect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { Connector, Config as WagmiConfig } from 'wagmi';
import type * as Wagmi from 'wagmi';
import { mainnet } from 'wagmi/chains';

import { wagmiBaseMock } from '../__mocks__/wagmiBaseMock';
import { MetaMask } from '../../wallets';
import { AntDesignWeb3ConfigProvider } from '../config-provider';

Expand All @@ -32,9 +33,7 @@ vi.mock('wagmi', async (importOriginal) => {
const actual = await importOriginal<typeof Wagmi>();
return {
...actual,
useConfig: () => {
return {};
},
...wagmiBaseMock,
// https://wagmi.sh/react/hooks/useAccount
useAccount: () => {
const [connected, setConnected] = React.useState(false);
Expand Down Expand Up @@ -73,16 +72,6 @@ vi.mock('wagmi', async (importOriginal) => {
},
};
},
useSwitchChain: () => {
return {
switchChain: () => {},
};
},
useBalance: () => {
return {};
},
useEnsName: () => ({}),
useEnsAvatar: () => ({}),
};
});

Expand Down
26 changes: 2 additions & 24 deletions packages/wagmi/src/wagmi-provider/__tests__/ens.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { describe, expect, it, vi } from 'vitest';
import type * as Wagmi from 'wagmi';
import { mainnet } from 'wagmi/chains';

import { wagmiBaseMock } from '../__mocks__/wagmiBaseMock';
import { MetaMask } from '../../wallets';
import { AntDesignWeb3ConfigProvider } from '../config-provider';

Expand All @@ -19,9 +20,7 @@ vi.mock('wagmi', async (importOriginal) => {
const actual = await importOriginal<typeof Wagmi>();
return {
...actual,
useConfig: () => {
return {};
},
...wagmiBaseMock,
// https://wagmi.sh/react/hooks/useAccount
useAccount: () => {
return {
Expand All @@ -30,27 +29,6 @@ vi.mock('wagmi', async (importOriginal) => {
address: '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B',
};
},
useConnect: () => {
return {
connectors: [],
connectAsync: async () => {
return {};
},
};
},
useDisconnect: () => {
return {
disconnectAsync: () => {},
};
},
useSwitchChain: () => {
return {
switchChain: () => {},
};
},
useBalance: () => {
return {};
},
useEnsName: ({ address }: { address: string }) => {
if (address === '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B') {
return { data: 'wanderingearth.eth' };
Expand Down
28 changes: 2 additions & 26 deletions packages/wagmi/src/wagmi-provider/__tests__/nft.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { describe, expect, it, vi } from 'vitest';
import type * as Wagmi from 'wagmi';
import { mainnet } from 'wagmi/chains';

import { wagmiBaseMock } from '../__mocks__/wagmiBaseMock';
import { MetaMask } from '../../wallets';
import { AntDesignWeb3ConfigProvider } from '../config-provider';

Expand All @@ -20,9 +21,7 @@ vi.mock('wagmi', async (importOriginal) => {
const actual = await importOriginal<typeof Wagmi>();
return {
...actual,
useConfig: () => {
return {};
},
...wagmiBaseMock,
// https://wagmi.sh/react/hooks/useAccount
useAccount: () => {
return {
Expand All @@ -31,29 +30,6 @@ vi.mock('wagmi', async (importOriginal) => {
address: '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B',
};
},
useConnect: () => {
return {
connectors: [],
connectAsync: async () => {
return {};
},
};
},
useDisconnect: () => {
return {
disconnectAsync: () => {},
};
},
useSwitchChain: () => {
return {
switchChain: () => {},
};
},
useBalance: () => {
return {};
},
useEnsName: () => ({}),
useEnsAvatar: () => ({}),
};
});

Expand Down
Loading
Loading