Skip to content

Commit

Permalink
feat: bump mm sdk (#4337)
Browse files Browse the repository at this point in the history
* feat: update metamask sdk and implement connectSign

* feat: cleanup

* feat: cleanup

* sdk 0.29.1

* feat: cleanup

* feat: url default

* feat: prevent breaking changes

* feat: cleanup

* chore: tweaks

* chore: tweaks

* chore: format

* chore: changeset

---------

Co-authored-by: abretonc7s <[email protected]>
Co-authored-by: tmm <[email protected]>
  • Loading branch information
3 people authored Oct 15, 2024
1 parent 7282143 commit 34a0c3b
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 187 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-hotels-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wagmi/connectors": minor
---

Added "Connect and Sign" behavior to MetaMask Connector.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"publint": "^0.2.11",
"sherif": "^1.0.0",
"simple-git-hooks": "^2.11.1",
"typescript": "5.5.2",
"typescript": "5.5.4",
"viem": "2.17.0",
"vitest": "^2.1.1"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/connectors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
},
"dependencies": {
"@coinbase/wallet-sdk": "4.0.4",
"@metamask/sdk": "0.28.4",
"@metamask/sdk": "0.29.2",
"@safe-global/safe-apps-provider": "0.18.3",
"@safe-global/safe-apps-sdk": "9.1.0",
"@walletconnect/ethereum-provider": "2.17.0",
Expand Down
83 changes: 77 additions & 6 deletions packages/connectors/src/metaMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import {
import type {
Compute,
ExactPartial,
OneOf,
RemoveUndefined,
UnionCompute,
} from '@wagmi/core/internal'
import {
type AddEthereumChainParameter,
Expand All @@ -31,8 +33,42 @@ import {
withTimeout,
} from 'viem'

export type MetaMaskParameters = Compute<
ExactPartial<Omit<MetaMaskSDKOptions, '_source' | 'readonlyRPCMap'>>
export type MetaMaskParameters = UnionCompute<
WagmiMetaMaskSDKOptions &
OneOf<
| {
/* Shortcut to connect and sign a message */
connectAndSign?: string | undefined
}
| {
// TODO: Strongly type `method` and `params`
/* Allow `connectWith` any rpc method */
connectWith?: { method: string; params: unknown[] } | undefined
}
>
>

type WagmiMetaMaskSDKOptions = Compute<
ExactPartial<
Omit<
MetaMaskSDKOptions,
| '_source'
| 'forceDeleteProvider'
| 'forceInjectProvider'
| 'injectProvider'
| 'useDeeplink'
| 'readonlyRPCMap'
>
> & {
/** @deprecated */
forceDeleteProvider?: MetaMaskSDKOptions['forceDeleteProvider']
/** @deprecated */
forceInjectProvider?: MetaMaskSDKOptions['forceInjectProvider']
/** @deprecated */
injectProvider?: MetaMaskSDKOptions['injectProvider']
/** @deprecated */
useDeeplink?: MetaMaskSDKOptions['useDeeplink']
}
>

metaMask.type = 'metaMask' as const
Expand Down Expand Up @@ -76,11 +112,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))
}
if (parameters.connectAndSign || parameters.connectWith) {
if (parameters.connectAndSign)
signResponse = await sdk.connectAndSign({
msg: parameters.connectAndSign,
})
else if (parameters.connectWith)
connectWithResponse = await sdk.connectWith({
method: parameters.connectWith.method,
params: parameters.connectWith.params,
})

accounts = await this.getAccounts()
} else {
const requestedAccounts = (await sdk.connect()) as string[]
accounts = requestedAccounts.map((x) => getAddress(x))
}
}
// Switch to chain if provided
let currentChainId = (await this.getChainId()) as number
if (chainId && currentChainId !== chainId) {
Expand All @@ -96,6 +147,19 @@ export function metaMask(parameters: MetaMaskParameters = {}) {
displayUri = undefined
}

if (signResponse)
provider.emit('connectAndSign', {
accounts,
chainId: currentChainId,
signResponse,
})
else if (connectWithResponse)
provider.emit('connectWith', {
accounts,
chainId: currentChainId,
connectWithResponse,
})

// Manage EIP-1193 event listeners
// https://eips.ethereum.org/EIPS/eip-1193#events
if (connect) {
Expand Down Expand Up @@ -175,6 +239,9 @@ export function metaMask(parameters: MetaMaskParameters = {}) {

sdk = new MetaMaskSDK({
_source: 'wagmi',
forceDeleteProvider: false,
forceInjectProvider: false,
injectProvider: false,
// Workaround cast since MetaMask SDK does not support `'exactOptionalPropertyTypes'`
...(parameters as RemoveUndefined<typeof parameters>),
readonlyRPCMap: Object.fromEntries(
Expand All @@ -186,7 +253,11 @@ export function metaMask(parameters: MetaMaskParameters = {}) {
return [chain.id, url]
}),
),
dappMetadata: parameters.dappMetadata ?? { name: 'wagmi' },
dappMetadata:
parameters.dappMetadata ??
(typeof window !== 'undefined'
? { url: window.location.origin }
: { name: 'wagmi' }),
useDeeplink: parameters.useDeeplink ?? true,
})
await sdk.init()
Expand Down
Loading

0 comments on commit 34a0c3b

Please sign in to comment.