Skip to content

Commit

Permalink
Merge pull request #35 from argentlabs/refactor/create-session
Browse files Browse the repository at this point in the history
refactor: create session
  • Loading branch information
bluecco authored Dec 17, 2024
2 parents 96df23d + 063e421 commit f6192f3
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 30 deletions.
33 changes: 29 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,24 @@ const sessionParams: CreateSessionParams = {
}
}

// create the session request to get the typed data to be signed
const sessionRequest = createSessionRequest({
sessionParams,
chainId
})

// wallet is a StarknetWindowObject
const authorisationSignature = await wallet.request({
type: "wallet_signTypedData",
params: sessionRequest.sessionTypedData
})

// open session and sign message
const session = await createSession({
wallet, // StarknetWindowObject
sessionRequest, // SessionRequest
address, // Account address
chainId, // StarknetChainId
sessionParams // CreateSessionParams
authorisationSignature // Signature
})

const sessionAccount = await buildSessionAccount({
Expand Down Expand Up @@ -165,11 +177,24 @@ const sessionParams: CreateSessionParams = {
}
}

// create the session request to get the typed data to be signed
const sessionRequest = createSessionRequest({
sessionParams,
chainId
})

// wallet is a StarknetWindowObject
const authorisationSignature = await wallet.request({
type: "wallet_signTypedData",
params: sessionRequest.sessionTypedData
})

// open session and sign message
const session = await createSession({
wallet, // StarknetWindowObject
sessionRequest, // SessionRequest
address, // Account address
chainId, // StarknetChainId
sessionParams // CreateSessionParams
authorisationSignature // Signature
})

const sessionAccount = await buildSessionAccount({
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ describe("Utils", () => {

const accountSessionSignature = await createSession({
address: "0x1234567890abcdef",
wallet: walletMock as StarknetWindowObject,
authorisationSignature: ["0x123", "0x456"],
sessionParams,
chainId,
})
Expand Down
85 changes: 60 additions & 25 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { StarknetDomain, TypedData } from "@starknet-io/types-js"
import {
StarknetDomain,
StarknetWindowObject,
TypedData,
} from "@starknet-io/types-js"
import { Account, constants, ec, hash, shortString, typedData } from "starknet"
Account,
constants,
ec,
hash,
shortString,
Signature,
typedData,
} from "starknet"
import { SessionAccount } from "./SessionAccount"
import {
AllowedMethod,
BuildSessionAccountParams,
CreateSessionParams,
OffChainSession,
Session,
SessionKey,
SessionMetadata,
VerifySessionParams,
} from "./session.types"
Expand Down Expand Up @@ -111,30 +116,29 @@ const buildSessionAccount = async ({
})
}

interface SignSessionMessageParams {
address: string
wallet: StarknetWindowObject
sessionParams: CreateSessionParams
interface CreateSessionRequestParams {
chainId: constants.StarknetChainId
sessionParams: CreateSessionParams
}

interface SessionRequest {
sessionTypedData: TypedData
offchainSession: OffChainSession
sessionKey: SessionKey
}
/**
* Creates a new session.
* Creates a new session request.
*
* @param {Object} params - The parameters for creating the session.
* @param {string} params.address - The address of the user.
* @param {StarknetWindowObject} params.wallet - The wallet object for signing the session.
* @param {CreateSessionParams} params.sessionParams - The parameters for the session.
* @param {Object} params - The parameters for creating the session request.
* @param {constants.StarknetChainId} params.chainId - The chain ID for the session.
* @returns {Promise<Session>} A promise that resolves to the created session.
* @param {CreateSessionParams} params.sessionParams - The parameters for the session.
* @returns {Object} The session typed data and the offchain session object.
* @throws {Error} If the sessionPublicKey is not provided.
*/
const createSession = async ({
address,
wallet,
sessionParams,
const createSessionRequest = ({
chainId,
}: SignSessionMessageParams): Promise<Session> => {
sessionParams,
}: CreateSessionRequestParams): SessionRequest => {
const {
allowedMethods,
expiry = BigInt(Date.now()) + 10000n,
Expand All @@ -153,12 +157,42 @@ const createSession = async ({
sessionKey.publicKey,
)

const sessionTypedData = getSessionTypedData(offchainSession, chainId)
return {
sessionTypedData: getSessionTypedData(offchainSession, chainId),
offchainSession,
sessionKey,
}
}

const authorisationSignature = await wallet.request({
type: "wallet_signTypedData",
params: sessionTypedData,
})
interface SignSessionMessageParams {
address: string
authorisationSignature: Signature
sessionRequest: SessionRequest
chainId: constants.StarknetChainId
}

/**
* Creates a new session.
*
* @param {Object} params - The parameters for creating the session.
* @param {string} params.address - The address of the user.
* @param {Signature} params.authorisationSignature - The session signature.
* @param {SessionRequest} params.sessionRequest - The session request.
* @param {constants.StarknetChainId} params.chainId - The chain ID for the session.
* @returns {Promise<Session>} A promise that resolves to the created session.
* @throws {Error} If the sessionPublicKey is not provided.
*/
const createSession = async ({
address,
authorisationSignature,
sessionRequest,
chainId,
}: SignSessionMessageParams): Promise<Session> => {
const { sessionKey, sessionTypedData, offchainSession } = sessionRequest

Check failure on line 191 in src/utils.ts

View workflow job for this annotation

GitHub Actions / release

src/__tests__/utils.test.ts > Utils > createSession > should open a session using wallet rpc methods

TypeError: Cannot destructure property 'sessionKey' of 'sessionRequest' as it is undefined. ❯ Module.createSession src/utils.ts:191:11 ❯ src/__tests__/utils.test.ts:162:45

if (!sessionKey || !sessionKey.publicKey) {
throw new Error("sessionPublicKey is required")
}

const session: Session = {
authorisationSignature,
Expand Down Expand Up @@ -208,6 +242,7 @@ export {
buildSessionAccount,
createOffchainSession,
createSession,
createSessionRequest,
getSessionDomain,
getSessionTypedData,
sessionTypes,
Expand Down

0 comments on commit f6192f3

Please sign in to comment.