Skip to content

Commit

Permalink
Merge pull request #36 from argentlabs/refactor/session-pk
Browse files Browse the repository at this point in the history
refactor: update session pk type
  • Loading branch information
vladutjs authored Dec 19, 2024
2 parents 17dcfa2 + d51c146 commit 7cfc75e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ import {
SignSessionError,
CreateSessionParams,
createSession,
buildSessionAccount
buildSessionAccount,
bytesToHexString
} from "@argent/x-sessions"
import { ec } from "starknet"

const privateKey = ec.starkCurve.utils.randomPrivateKey()

const sessionKey: SessionKey = {
privateKey,
privateKey: bytesToHexString(privateKey),
publicKey: ec.starkCurve.getStarkKey(privateKey)
}

Expand Down Expand Up @@ -150,7 +151,7 @@ This package expose a method in order to get the Call required to perform an exe
const privateKey = ec.starkCurve.utils.randomPrivateKey()

const sessionKey: SessionKey = {
privateKey,
privateKey: bytesToHexString(privateKey),
publicKey: ec.starkCurve.getStarkKey(privateKey)
}

Expand Down
2 changes: 1 addition & 1 deletion src/outsideExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export const createOutsideExecutionCall = async ({
const outsideExecutionTypedData = buildOutsideExecutionTypedData({
outsideExecution,
chainId: session.chainId,
version: version || "1",
version: version || "2", // version, set as "2" because of a bug in x-sessions where default was wrongly set to "1"
})

const signature = await signOutsideExecution({
Expand Down
2 changes: 1 addition & 1 deletion src/session.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export type CreateSessionParams = {

export type SessionKey = {
publicKey: string
privateKey: Uint8Array
privateKey: string
}

export type BuildSessionAccountParams = {
Expand Down
26 changes: 26 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Account,
constants,
ec,
encode,
hash,
shortString,
Signature,
Expand Down Expand Up @@ -238,6 +239,29 @@ const verifySession = ({
)
}

/**
* Converts a byte array to a hex string.
* @param bytes The byte array to convert.
* @returns The hex string.
*/
const bytesToHexString = (bytes: Uint8Array): string => {
return encode.addHexPrefix(encode.buf2hex(bytes))
}

/**
* Converts a hex string to a byte array.
* @param hexString The hex string to convert.
* @returns The byte array.
*/
const hexStringToBytes = (hexString: string): Uint8Array => {
const hex = encode.removeHexPrefix(hexString)
const hexArray = hex.match(/.{1,2}/g)
if (!hexArray) {
throw new Error("Invalid hex string")
}
return Uint8Array.from(hexArray.map((byte) => parseInt(byte, 16)))
}

export {
buildSessionAccount,
createOffchainSession,
Expand All @@ -247,4 +271,6 @@ export {
getSessionTypedData,
sessionTypes,
verifySession,
bytesToHexString,
hexStringToBytes,
}

0 comments on commit 7cfc75e

Please sign in to comment.