Skip to content

Commit f6192f3

Browse files
authored
Merge pull request #35 from argentlabs/refactor/create-session
refactor: create session
2 parents 96df23d + 063e421 commit f6192f3

File tree

3 files changed

+90
-30
lines changed

3 files changed

+90
-30
lines changed

README.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,24 @@ const sessionParams: CreateSessionParams = {
9494
}
9595
}
9696

97+
// create the session request to get the typed data to be signed
98+
const sessionRequest = createSessionRequest({
99+
sessionParams,
100+
chainId
101+
})
102+
103+
// wallet is a StarknetWindowObject
104+
const authorisationSignature = await wallet.request({
105+
type: "wallet_signTypedData",
106+
params: sessionRequest.sessionTypedData
107+
})
108+
97109
// open session and sign message
98110
const session = await createSession({
99-
wallet, // StarknetWindowObject
111+
sessionRequest, // SessionRequest
100112
address, // Account address
101113
chainId, // StarknetChainId
102-
sessionParams // CreateSessionParams
114+
authorisationSignature // Signature
103115
})
104116

105117
const sessionAccount = await buildSessionAccount({
@@ -165,11 +177,24 @@ const sessionParams: CreateSessionParams = {
165177
}
166178
}
167179

180+
// create the session request to get the typed data to be signed
181+
const sessionRequest = createSessionRequest({
182+
sessionParams,
183+
chainId
184+
})
185+
186+
// wallet is a StarknetWindowObject
187+
const authorisationSignature = await wallet.request({
188+
type: "wallet_signTypedData",
189+
params: sessionRequest.sessionTypedData
190+
})
191+
192+
// open session and sign message
168193
const session = await createSession({
169-
wallet, // StarknetWindowObject
194+
sessionRequest, // SessionRequest
170195
address, // Account address
171196
chainId, // StarknetChainId
172-
sessionParams // CreateSessionParams
197+
authorisationSignature // Signature
173198
})
174199

175200
const sessionAccount = await buildSessionAccount({

src/__tests__/utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ describe("Utils", () => {
161161

162162
const accountSessionSignature = await createSession({
163163
address: "0x1234567890abcdef",
164-
wallet: walletMock as StarknetWindowObject,
164+
authorisationSignature: ["0x123", "0x456"],
165165
sessionParams,
166166
chainId,
167167
})

src/utils.ts

Lines changed: 60 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1+
import { StarknetDomain, TypedData } from "@starknet-io/types-js"
12
import {
2-
StarknetDomain,
3-
StarknetWindowObject,
4-
TypedData,
5-
} from "@starknet-io/types-js"
6-
import { Account, constants, ec, hash, shortString, typedData } from "starknet"
3+
Account,
4+
constants,
5+
ec,
6+
hash,
7+
shortString,
8+
Signature,
9+
typedData,
10+
} from "starknet"
711
import { SessionAccount } from "./SessionAccount"
812
import {
913
AllowedMethod,
1014
BuildSessionAccountParams,
1115
CreateSessionParams,
1216
OffChainSession,
1317
Session,
18+
SessionKey,
1419
SessionMetadata,
1520
VerifySessionParams,
1621
} from "./session.types"
@@ -111,30 +116,29 @@ const buildSessionAccount = async ({
111116
})
112117
}
113118

114-
interface SignSessionMessageParams {
115-
address: string
116-
wallet: StarknetWindowObject
117-
sessionParams: CreateSessionParams
119+
interface CreateSessionRequestParams {
118120
chainId: constants.StarknetChainId
121+
sessionParams: CreateSessionParams
119122
}
120123

124+
interface SessionRequest {
125+
sessionTypedData: TypedData
126+
offchainSession: OffChainSession
127+
sessionKey: SessionKey
128+
}
121129
/**
122-
* Creates a new session.
130+
* Creates a new session request.
123131
*
124-
* @param {Object} params - The parameters for creating the session.
125-
* @param {string} params.address - The address of the user.
126-
* @param {StarknetWindowObject} params.wallet - The wallet object for signing the session.
127-
* @param {CreateSessionParams} params.sessionParams - The parameters for the session.
132+
* @param {Object} params - The parameters for creating the session request.
128133
* @param {constants.StarknetChainId} params.chainId - The chain ID for the session.
129-
* @returns {Promise<Session>} A promise that resolves to the created session.
134+
* @param {CreateSessionParams} params.sessionParams - The parameters for the session.
135+
* @returns {Object} The session typed data and the offchain session object.
130136
* @throws {Error} If the sessionPublicKey is not provided.
131137
*/
132-
const createSession = async ({
133-
address,
134-
wallet,
135-
sessionParams,
138+
const createSessionRequest = ({
136139
chainId,
137-
}: SignSessionMessageParams): Promise<Session> => {
140+
sessionParams,
141+
}: CreateSessionRequestParams): SessionRequest => {
138142
const {
139143
allowedMethods,
140144
expiry = BigInt(Date.now()) + 10000n,
@@ -153,12 +157,42 @@ const createSession = async ({
153157
sessionKey.publicKey,
154158
)
155159

156-
const sessionTypedData = getSessionTypedData(offchainSession, chainId)
160+
return {
161+
sessionTypedData: getSessionTypedData(offchainSession, chainId),
162+
offchainSession,
163+
sessionKey,
164+
}
165+
}
157166

158-
const authorisationSignature = await wallet.request({
159-
type: "wallet_signTypedData",
160-
params: sessionTypedData,
161-
})
167+
interface SignSessionMessageParams {
168+
address: string
169+
authorisationSignature: Signature
170+
sessionRequest: SessionRequest
171+
chainId: constants.StarknetChainId
172+
}
173+
174+
/**
175+
* Creates a new session.
176+
*
177+
* @param {Object} params - The parameters for creating the session.
178+
* @param {string} params.address - The address of the user.
179+
* @param {Signature} params.authorisationSignature - The session signature.
180+
* @param {SessionRequest} params.sessionRequest - The session request.
181+
* @param {constants.StarknetChainId} params.chainId - The chain ID for the session.
182+
* @returns {Promise<Session>} A promise that resolves to the created session.
183+
* @throws {Error} If the sessionPublicKey is not provided.
184+
*/
185+
const createSession = async ({
186+
address,
187+
authorisationSignature,
188+
sessionRequest,
189+
chainId,
190+
}: SignSessionMessageParams): Promise<Session> => {
191+
const { sessionKey, sessionTypedData, offchainSession } = sessionRequest
192+
193+
if (!sessionKey || !sessionKey.publicKey) {
194+
throw new Error("sessionPublicKey is required")
195+
}
162196

163197
const session: Session = {
164198
authorisationSignature,
@@ -208,6 +242,7 @@ export {
208242
buildSessionAccount,
209243
createOffchainSession,
210244
createSession,
245+
createSessionRequest,
211246
getSessionDomain,
212247
getSessionTypedData,
213248
sessionTypes,

0 commit comments

Comments
 (0)