Skip to content

Commit 7cfc75e

Browse files
authored
Merge pull request #36 from argentlabs/refactor/session-pk
refactor: update session pk type
2 parents 17dcfa2 + d51c146 commit 7cfc75e

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ import {
6060
SignSessionError,
6161
CreateSessionParams,
6262
createSession,
63-
buildSessionAccount
63+
buildSessionAccount,
64+
bytesToHexString
6465
} from "@argent/x-sessions"
6566
import { ec } from "starknet"
6667

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

6970
const sessionKey: SessionKey = {
70-
privateKey,
71+
privateKey: bytesToHexString(privateKey),
7172
publicKey: ec.starkCurve.getStarkKey(privateKey)
7273
}
7374

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

152153
const sessionKey: SessionKey = {
153-
privateKey,
154+
privateKey: bytesToHexString(privateKey),
154155
publicKey: ec.starkCurve.getStarkKey(privateKey)
155156
}
156157

src/outsideExecution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export const createOutsideExecutionCall = async ({
144144
const outsideExecutionTypedData = buildOutsideExecutionTypedData({
145145
outsideExecution,
146146
chainId: session.chainId,
147-
version: version || "1",
147+
version: version || "2", // version, set as "2" because of a bug in x-sessions where default was wrongly set to "1"
148148
})
149149

150150
const signature = await signOutsideExecution({

src/session.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export type CreateSessionParams = {
6969

7070
export type SessionKey = {
7171
publicKey: string
72-
privateKey: Uint8Array
72+
privateKey: string
7373
}
7474

7575
export type BuildSessionAccountParams = {

src/utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
Account,
44
constants,
55
ec,
6+
encode,
67
hash,
78
shortString,
89
Signature,
@@ -238,6 +239,29 @@ const verifySession = ({
238239
)
239240
}
240241

242+
/**
243+
* Converts a byte array to a hex string.
244+
* @param bytes The byte array to convert.
245+
* @returns The hex string.
246+
*/
247+
const bytesToHexString = (bytes: Uint8Array): string => {
248+
return encode.addHexPrefix(encode.buf2hex(bytes))
249+
}
250+
251+
/**
252+
* Converts a hex string to a byte array.
253+
* @param hexString The hex string to convert.
254+
* @returns The byte array.
255+
*/
256+
const hexStringToBytes = (hexString: string): Uint8Array => {
257+
const hex = encode.removeHexPrefix(hexString)
258+
const hexArray = hex.match(/.{1,2}/g)
259+
if (!hexArray) {
260+
throw new Error("Invalid hex string")
261+
}
262+
return Uint8Array.from(hexArray.map((byte) => parseInt(byte, 16)))
263+
}
264+
241265
export {
242266
buildSessionAccount,
243267
createOffchainSession,
@@ -247,4 +271,6 @@ export {
247271
getSessionTypedData,
248272
sessionTypes,
249273
verifySession,
274+
bytesToHexString,
275+
hexStringToBytes,
250276
}

0 commit comments

Comments
 (0)