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: requestPermissions #4338

Open
wants to merge 3 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
5 changes: 5 additions & 0 deletions .changeset/four-pants-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wagmi": minor
---

Added `useRequestPermissions` hook.
5 changes: 5 additions & 0 deletions .changeset/seven-terms-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wagmi/core": minor
---

Added `requestPermissions` action.
31 changes: 31 additions & 0 deletions packages/core/src/actions/requestPermissions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { config } from '@wagmi/test'
import { expect, test } from 'vitest'

import { connect } from './connect.js'
import { disconnect } from './disconnect.js'
import { requestPermissions } from './requestPermissions.js'

const connector = config.connectors[0]!

test('default', async () => {
await connect(config, { connector })
await expect(
requestPermissions(config, { eth_accounts: {} }),
).resolves.toMatchInlineSnapshot(`
[
{
"caveats": [
{
"type": "filterResponse",
"value": [
"0x0c54fccd2e384b4bb6f2e405bf5cbc15a017aafb",
],
},
],
"invoker": "https://example.com",
"parentCapability": "eth_accounts",
},
]
`)
await disconnect(config, { connector })
})
48 changes: 48 additions & 0 deletions packages/core/src/actions/requestPermissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
type RequestPermissionsErrorType as viem_RequestPermissionsErrorType,
type RequestPermissionsParameters as viem_RequestPermissionsParameters,
type RequestPermissionsReturnType as viem_RequestPermissionsReturnType,
requestPermissions as viem_requestPermissions,
} from 'viem/actions'

import type { Config } from '../createConfig.js'
import type { BaseErrorType, ErrorType } from '../errors/base.js'
import type { ConnectorParameter } from '../types/properties.js'
import type { Compute } from '../types/utils.js'
import { getAction } from '../utils/getAction.js'
import {
type GetConnectorClientErrorType,
getConnectorClient,
} from './getConnectorClient.js'

export type RequestPermissionsParameters = Compute<
viem_RequestPermissionsParameters & ConnectorParameter
>

export type RequestPermissionsReturnType = viem_RequestPermissionsReturnType

export type RequestPermissionsErrorType =
// getConnectorClient()
| GetConnectorClientErrorType
// base
| BaseErrorType
| ErrorType
// viem
| viem_RequestPermissionsErrorType

/** https://wagmi.sh/core/api/actions/requestPermissions */
export async function requestPermissions(
config: Config,
parameters: RequestPermissionsParameters,
): Promise<RequestPermissionsReturnType> {
const { connector, ...rest } = parameters

const client = await getConnectorClient(config, { connector })

const action = getAction(
client,
viem_requestPermissions,
'requestPermissions',
)
return action(rest)
}
23 changes: 23 additions & 0 deletions packages/core/src/connectors/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type MockParameters = {
signMessageError?: boolean | Error | undefined
signTypedDataError?: boolean | Error | undefined
reconnect?: boolean | undefined
requestPermissionsError?: boolean | Error | undefined
watchAssetError?: boolean | Error | undefined
}
| undefined
Expand Down Expand Up @@ -250,6 +251,28 @@ export function mock(parameters: MockParameters) {

if (method === 'wallet_showCallsStatus') return

if (method === 'wallet_requestPermissions') {
if (features.requestPermissionsError) {
if (typeof features.requestPermissionsError === 'boolean')
throw new UserRejectedRequestError(
new Error('Failed to request permissions.'),
)
throw features.requestPermissionsError
}
return [
{
invoker: 'https://example.com',
parentCapability: 'eth_accounts',
caveats: [
{
type: 'filterResponse',
value: ['0x0c54fccd2e384b4bb6f2e405bf5cbc15a017aafb'],
},
],
},
]
}

// other methods
if (method === 'personal_sign') {
if (features.signMessageError) {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/exports/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ test('exports', () => {
"readContract",
"readContracts",
"reconnect",
"requestPermissions",
"sendTransaction",
"signMessage",
"signTypedData",
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/exports/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,13 @@ export {
reconnect,
} from '../actions/reconnect.js'

export {
type RequestPermissionsErrorType,
type RequestPermissionsParameters,
type RequestPermissionsReturnType,
requestPermissions,
} from '../actions/requestPermissions.js'

export {
type SendTransactionErrorType,
type SendTransactionParameters,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/exports/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ test('exports', () => {
"readContract",
"readContracts",
"reconnect",
"requestPermissions",
"sendTransaction",
"signMessage",
"signTypedData",
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/exports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,13 @@ export {
reconnect,
} from '../actions/reconnect.js'

export {
type RequestPermissionsErrorType,
type RequestPermissionsParameters,
type RequestPermissionsReturnType,
requestPermissions,
} from '../actions/requestPermissions.js'

export {
type SendTransactionErrorType,
type SendTransactionParameters,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/exports/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ test('exports', () => {
"readContractsQueryKey",
"readContractsQueryOptions",
"reconnectMutationOptions",
"requestPermissionsMutationOptions",
"sendTransactionMutationOptions",
"signMessageMutationOptions",
"signTypedDataMutationOptions",
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/exports/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,14 @@ export {
reconnectMutationOptions,
} from '../query/reconnect.js'

export {
type RequestPermissionsData,
type RequestPermissionsVariables,
type RequestPermissionsMutate,
type RequestPermissionsMutateAsync,
requestPermissionsMutationOptions,
} from '../query/requestPermissions.js'

export {
type SendTransactionData,
type SendTransactionVariables,
Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/query/requestPermissions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { config } from '@wagmi/test'
import { expect, test } from 'vitest'

import { requestPermissionsMutationOptions } from './requestPermissions.js'

test('default', () => {
expect(requestPermissionsMutationOptions(config)).toMatchInlineSnapshot(`
{
"mutationFn": [Function],
"mutationKey": [
"requestPermissions",
],
}
`)
})
41 changes: 41 additions & 0 deletions packages/core/src/query/requestPermissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { MutationOptions } from '@tanstack/query-core'

import {
type RequestPermissionsErrorType,
type RequestPermissionsParameters,
type RequestPermissionsReturnType,
requestPermissions,
} from '../actions/requestPermissions.js'
import type { Config } from '../createConfig.js'
import type { Mutate, MutateAsync } from './types.js'

export function requestPermissionsMutationOptions(config: Config) {
return {
mutationFn(variables) {
return requestPermissions(config, variables)
},
mutationKey: ['requestPermissions'],
} as const satisfies MutationOptions<
RequestPermissionsData,
RequestPermissionsErrorType,
RequestPermissionsVariables
>
}

export type RequestPermissionsData = RequestPermissionsReturnType

export type RequestPermissionsVariables = RequestPermissionsParameters

export type RequestPermissionsMutate<context = unknown> = Mutate<
RequestPermissionsData,
RequestPermissionsErrorType,
RequestPermissionsVariables,
context
>

export type RequestPermissionsMutateAsync<context = unknown> = MutateAsync<
RequestPermissionsData,
RequestPermissionsErrorType,
RequestPermissionsVariables,
context
>
1 change: 1 addition & 0 deletions packages/react/src/exports/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ test('exports', () => {
"readContract",
"readContracts",
"reconnect",
"requestPermissions",
"sendTransaction",
"signMessage",
"signTypedData",
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/exports/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ test('exports', () => {
"useReadContracts",
"useContractReads",
"useReconnect",
"useRequestPermissions",
"useSendTransaction",
"useSignMessage",
"useSignTypedData",
Expand Down
6 changes: 6 additions & 0 deletions packages/react/src/exports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ export {
useReconnect,
} from '../hooks/useReconnect.js'

export {
type UseRequestPermissionsParameters,
type UseRequestPermissionsReturnType,
useRequestPermissions,
} from '../hooks/useRequestPermissions.js'

export {
type UseSendTransactionParameters,
type UseSendTransactionReturnType,
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/exports/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ test('exports', () => {
"readContractsQueryKey",
"readContractsQueryOptions",
"reconnectMutationOptions",
"requestPermissionsMutationOptions",
"sendTransactionMutationOptions",
"signMessageMutationOptions",
"signTypedDataMutationOptions",
Expand Down
67 changes: 67 additions & 0 deletions packages/react/src/hooks/useRequestPermissions.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type { RequestPermissionsErrorType } from '@wagmi/core'
import type { RequestPermissionsVariables } from '@wagmi/core/query'
import { expectTypeOf, test } from 'vitest'

import type { WalletPermission } from 'viem'
import { useRequestPermissions } from './useRequestPermissions.js'

const contextValue = { foo: 'bar' } as const

test('context', () => {
const { context, data, error, requestPermissions, variables } =
useRequestPermissions({
mutation: {
onMutate(variables) {
expectTypeOf(variables).toEqualTypeOf<RequestPermissionsVariables>()
return contextValue
},
onError(error, variables, context) {
expectTypeOf(variables).toEqualTypeOf<RequestPermissionsVariables>()
expectTypeOf(error).toEqualTypeOf<RequestPermissionsErrorType>()
expectTypeOf(context).toEqualTypeOf<typeof contextValue | undefined>()
},
onSuccess(data, variables, context) {
expectTypeOf(variables).toEqualTypeOf<RequestPermissionsVariables>()
expectTypeOf(data).toEqualTypeOf<WalletPermission[]>()
expectTypeOf(context).toEqualTypeOf<typeof contextValue>()
},
onSettled(data, error, variables, context) {
expectTypeOf(data).toEqualTypeOf<WalletPermission[] | undefined>()
expectTypeOf(
error,
).toEqualTypeOf<RequestPermissionsErrorType | null>()
expectTypeOf(variables).toEqualTypeOf<RequestPermissionsVariables>()
expectTypeOf(context).toEqualTypeOf<typeof contextValue | undefined>()
},
},
})

expectTypeOf(data).toEqualTypeOf<WalletPermission[] | undefined>()
expectTypeOf(error).toEqualTypeOf<RequestPermissionsErrorType | null>()
expectTypeOf(variables).toEqualTypeOf<
RequestPermissionsVariables | undefined
>()
expectTypeOf(context).toEqualTypeOf<typeof contextValue | undefined>()

requestPermissions(
{ eth_accounts: {} },
{
onError(error, variables, context) {
expectTypeOf(variables).toEqualTypeOf<RequestPermissionsVariables>()
expectTypeOf(error).toEqualTypeOf<RequestPermissionsErrorType>()
expectTypeOf(context).toEqualTypeOf<typeof contextValue | undefined>()
},
onSuccess(data, variables, context) {
expectTypeOf(variables).toEqualTypeOf<RequestPermissionsVariables>()
expectTypeOf(data).toEqualTypeOf<WalletPermission[]>()
expectTypeOf(context).toEqualTypeOf<typeof contextValue>()
},
onSettled(data, error, variables, context) {
expectTypeOf(data).toEqualTypeOf<WalletPermission[] | undefined>()
expectTypeOf(error).toEqualTypeOf<RequestPermissionsErrorType | null>()
expectTypeOf(variables).toEqualTypeOf<RequestPermissionsVariables>()
expectTypeOf(context).toEqualTypeOf<typeof contextValue | undefined>()
},
},
)
})
36 changes: 36 additions & 0 deletions packages/react/src/hooks/useRequestPermissions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { connect, disconnect } from '@wagmi/core'
import { config } from '@wagmi/test'
import { renderHook, waitFor } from '@wagmi/test/react'
import { expect, test } from 'vitest'

import { useRequestPermissions } from './useRequestPermissions.js'

const connector = config.connectors[0]!

test('default', async () => {
await connect(config, { connector })

const { result } = renderHook(() => useRequestPermissions())

result.current.requestPermissions({ eth_accounts: {} })
await waitFor(() => expect(result.current.isSuccess).toBeTruthy())

expect(result.current.data).toMatchInlineSnapshot(`
[
{
"caveats": [
{
"type": "filterResponse",
"value": [
"0x0c54fccd2e384b4bb6f2e405bf5cbc15a017aafb",
],
},
],
"invoker": "https://example.com",
"parentCapability": "eth_accounts",
},
]
`)

await disconnect(config, { connector })
})
Loading
Loading