1+ import { StarknetDomain , TypedData } from "@starknet-io/types-js"
12import {
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"
711import { SessionAccount } from "./SessionAccount"
812import {
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