-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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: update metamask sdk and implement connectSign #4323
Changes from 3 commits
226aa91
b88db8e
940bdf4
4a74e2d
338e59d
1b43aaa
1bb833d
51f778a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -31,10 +31,27 @@ import { | |||||
withTimeout, | ||||||
} from 'viem' | ||||||
|
||||||
export type MetaMaskParameters = Compute< | ||||||
ExactPartial<Omit<MetaMaskSDKOptions, '_source' | 'readonlyRPCMap'>> | ||||||
type WagmiMetaMaskSDKOptions = Compute< | ||||||
ExactPartial< | ||||||
Omit< | ||||||
MetaMaskSDKOptions, | ||||||
| '_source' | ||||||
| 'readonlyRPCMap' | ||||||
| 'useDeeplink' | ||||||
| 'injectProvider' | ||||||
| 'forceInjectProvider' | ||||||
| 'forceDeleteProvider' | ||||||
> | ||||||
> | ||||||
> | ||||||
|
||||||
export type MetaMaskParameters = WagmiMetaMaskSDKOptions & { | ||||||
// Shortcut to connect and sign a message | ||||||
connectAndSign?: string | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// Allow connectWith any rpc method | ||||||
connectWith?: { method: string; params: unknown[] } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
What |
||||||
} | ||||||
|
||||||
metaMask.type = 'metaMask' as const | ||||||
export function metaMask(parameters: MetaMaskParameters = {}) { | ||||||
type Provider = SDKProvider | ||||||
|
@@ -76,11 +93,26 @@ export function metaMask(parameters: MetaMaskParameters = {}) { | |||||
if (isReconnecting) accounts = await this.getAccounts().catch(() => []) | ||||||
|
||||||
try { | ||||||
let signResponse: string | undefined | ||||||
let connectWithResponse: unknown | undefined | ||||||
if (!accounts?.length) { | ||||||
const requestedAccounts = (await sdk.connect()) as string[] | ||||||
accounts = requestedAccounts.map((x) => getAddress(x)) | ||||||
let requestedAccounts: `0x${string}`[] | ||||||
abretonc7s marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
if (parameters.connectAndSign) { | ||||||
signResponse = await sdk.connectAndSign({ | ||||||
msg: parameters.connectAndSign, | ||||||
}) | ||||||
accounts = await this.getAccounts() | ||||||
} else if (parameters.connectWith) { | ||||||
connectWithResponse = await sdk.connectWith({ | ||||||
method: parameters.connectWith.method, | ||||||
params: parameters.connectWith.params, | ||||||
}) | ||||||
accounts = await this.getAccounts() | ||||||
} else { | ||||||
requestedAccounts = await sdk.connect() | ||||||
accounts = requestedAccounts.map((x) => getAddress(x)) | ||||||
} | ||||||
} | ||||||
|
||||||
// Switch to chain if provided | ||||||
let currentChainId = (await this.getChainId()) as number | ||||||
if (chainId && currentChainId !== chainId) { | ||||||
|
@@ -96,6 +128,20 @@ export function metaMask(parameters: MetaMaskParameters = {}) { | |||||
displayUri = undefined | ||||||
} | ||||||
|
||||||
if (signResponse) { | ||||||
provider.emit('connectAndSign', { | ||||||
accounts, | ||||||
signResponse, | ||||||
chainId: currentChainId, | ||||||
}) | ||||||
} else if (connectWithResponse) { | ||||||
provider.emit('connectWith', { | ||||||
accounts, | ||||||
connectWithResponse, | ||||||
chainId: currentChainId, | ||||||
}) | ||||||
} | ||||||
|
||||||
// Manage EIP-1193 event listeners | ||||||
// https://eips.ethereum.org/EIPS/eip-1193#events | ||||||
if (connect) { | ||||||
|
@@ -168,11 +214,18 @@ export function metaMask(parameters: MetaMaskParameters = {}) { | |||||
// See: https://github.com/vitejs/vite/issues/9703 | ||||||
const MetaMaskSDK = await (async () => { | ||||||
const { default: SDK } = await import('@metamask/sdk') | ||||||
// @ts-ignore | ||||||
if (typeof SDK !== 'function' && typeof SDK.default === 'function') | ||||||
// @ts-ignore | ||||||
return SDK.default | ||||||
// @ts-ignore | ||||||
Comment on lines
+207
to
+211
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll take a crack on getting TS to play nice. |
||||||
return SDK as unknown as typeof SDK.default | ||||||
})() | ||||||
|
||||||
const dappMetadata: MetaMaskSDKOptions['dappMetadata'] = { | ||||||
url: `${window.location.protocol}//${window.location.host}`, | ||||||
} | ||||||
|
||||||
sdk = new MetaMaskSDK({ | ||||||
_source: 'wagmi', | ||||||
// Workaround cast since MetaMask SDK does not support `'exactOptionalPropertyTypes'` | ||||||
|
@@ -186,8 +239,11 @@ export function metaMask(parameters: MetaMaskParameters = {}) { | |||||
return [chain.id, url] | ||||||
}), | ||||||
), | ||||||
dappMetadata: parameters.dappMetadata ?? { name: 'wagmi' }, | ||||||
useDeeplink: parameters.useDeeplink ?? true, | ||||||
dappMetadata, | ||||||
abretonc7s marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
useDeeplink: true, | ||||||
injectProvider: false, | ||||||
forceInjectProvider: false, | ||||||
forceDeleteProvider: false, | ||||||
Comment on lines
+237
to
+240
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What were the defaults internally before? Just want to make sure changes these isn't too disruptive for folks. Probably should allow folks to override these manually so it's not a breaking change and remove that ability in the next major version. (Very unlikely anyone will override manually since people just use defaults, but want to make sure semver is respected.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We force inject provider but it can conflict with the injected provider by wagmi (see our other PR.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this PR dependent on that one? Haven't had a chance to wrap up our thinking on that quite yet. |
||||||
}) | ||||||
await sdk.init() | ||||||
return sdk.getProvider()! | ||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep these around since otherwise it's a breaking change.