Skip to content

Commit 110e26c

Browse files
chore(dapp-client): re-export network/util helpers and add explicit session config helper
1 parent 40c19ff commit 110e26c

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

packages/wallet/dapp-client/src/index.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,17 @@ export {
3535
SigningError,
3636
ModifyExplicitSessionError,
3737
} from './utils/errors.js'
38-
export { getExplorerUrl, jsonReplacers, jsonRevivers } from './utils/index.js'
38+
export {
39+
createExplicitSessionConfig,
40+
getExplorerUrl,
41+
getNetwork,
42+
getRelayerUrl,
43+
getRpcUrl,
44+
jsonReplacers,
45+
jsonRevivers,
46+
VALUE_FORWARDER_ADDRESS,
47+
} from './utils/index.js'
48+
export type { ExplicitSessionParams, NativeTokenSpending, SessionDuration } from './utils/index.js'
3949
export type {
4050
SequenceStorage,
4151
ExplicitSessionData,
@@ -46,6 +56,14 @@ export type {
4656
} from './utils/storage.js'
4757
export { WebStorage } from './utils/storage.js'
4858

49-
export { Attestation, Permission, Extensions, SessionConfig, Constants, Payload } from '@0xsequence/wallet-primitives'
59+
export {
60+
Attestation,
61+
Permission,
62+
Extensions,
63+
SessionConfig,
64+
Constants,
65+
Payload,
66+
Network,
67+
} from '@0xsequence/wallet-primitives'
5068
export type { ExplicitSessionConfig, ExplicitSession, ImplicitSession, Session } from '@0xsequence/wallet-core'
5169
export { Signers, Wallet, Utils, Envelope, State } from '@0xsequence/wallet-core'

packages/wallet/dapp-client/src/utils/index.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { Network } from '@0xsequence/wallet-primitives'
3-
import { Bytes, Hex } from 'ox'
2+
import type { ExplicitSessionConfig } from '@0xsequence/wallet-core'
3+
import { Network, Permission } from '@0xsequence/wallet-primitives'
4+
import { Bytes, Hex, type Address } from 'ox'
5+
export { VALUE_FORWARDER_ADDRESS } from './constants.js'
46

57
type JsonReplacer = (key: string, value: any) => any
68
type JsonReviver = (key: string, value: any) => any
@@ -124,6 +126,50 @@ const uint8ArrayReviver: JsonReviver = (key, value) => {
124126
export const jsonRevivers = chainRevivers([mapReviver, bigIntReviver, uint8ArrayReviver])
125127
export const jsonReplacers = chainReplacers([mapReplacer, bigIntReplacer, uint8ArrayReplacer])
126128

129+
export type SessionDuration = {
130+
days?: number
131+
hours?: number
132+
minutes?: number
133+
}
134+
135+
export type NativeTokenSpending = {
136+
valueLimit: bigint
137+
allowedRecipients?: Address.Address[]
138+
}
139+
140+
export type ExplicitSessionParams = {
141+
chainId: number
142+
expiresIn: SessionDuration
143+
permissions: Permission.Permission[]
144+
nativeTokenSpending?: NativeTokenSpending
145+
}
146+
147+
export const createExplicitSessionConfig = (params: ExplicitSessionParams): ExplicitSessionConfig => {
148+
const nowInSeconds = BigInt(Math.floor(Date.now() / 1000))
149+
const { days = 0, hours = 0, minutes = 0 } = params.expiresIn
150+
const sessionLifetimeSeconds = days * 24 * 60 * 60 + hours * 60 * 60 + minutes * 60
151+
const deadline = nowInSeconds + BigInt(sessionLifetimeSeconds)
152+
153+
if (params.permissions.length === 0) {
154+
throw new Error('createExplicitSessionConfig: At least one permission is required.')
155+
}
156+
157+
const nativeTokenSpending = params.nativeTokenSpending
158+
const valueLimit = nativeTokenSpending?.valueLimit ?? 0n
159+
const nativeTokenReceivers = [...(nativeTokenSpending?.allowedRecipients || [])]
160+
const nativeTokenSpendingPermissions = nativeTokenReceivers.map((receiver) => ({
161+
target: receiver,
162+
rules: [],
163+
}))
164+
165+
return {
166+
chainId: params.chainId,
167+
valueLimit,
168+
deadline,
169+
permissions: [...params.permissions, ...nativeTokenSpendingPermissions],
170+
}
171+
}
172+
127173
/**
128174
* Apply a template to a string.
129175
*

0 commit comments

Comments
 (0)