Skip to content

Commit 3ee6b8e

Browse files
authored
Merge pull request #32 from argentlabs/refactor/x-sessions
refactor: simplify code
2 parents 78d4955 + 97fc577 commit 3ee6b8e

18 files changed

+1337
-1070
lines changed

README.md

Lines changed: 83 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ A demo dapp using both sessions and offchain sessions can be found here [https:/
2222

2323
First you need to have a deployed account. This is the account that will authorise the session and interact with the contracts of your dapp.
2424

25-
To sign the session message the method needed is `openSession`. After the user sign the message, a session account can be created using `buildSessionAccount`.
25+
To sign the session message the method needed is `createSession`. After the user sign the message, a session account can be created using `buildSessionAccount`.
2626

2727
This example session will allow the dapp to execute an example endpoint on an example contract without asking the user to approve the transaction again. After signing the session the dapp can execute all transactions listed in `allowedMethods` whenever it wants and as many times as it wants.
2828

@@ -45,8 +45,8 @@ export type SessionMetadata = {
4545
projectSignature?: Signature
4646
}
4747

48-
type SessionParams = {
49-
dappKey?: Uint8Array // this is optional. This sdk generate a dappKey using ec.starkCurve.utils.randomPrivateKey() if not provided
48+
type CreateSessionParams = {
49+
sessionKey?: Uint8Array // this is optional. This sdk generate a sessionKey using ec.starkCurve.utils.randomPrivateKey() if not provided
5050
allowedMethods: AllowedMethod[]
5151
expiry: bigint
5252
metaData: SessionMetadata
@@ -58,13 +58,21 @@ The following snippet show how to create and use a session account
5858
```typescript
5959
import {
6060
SignSessionError,
61-
SessionParams,
62-
openSession,
61+
CreateSessionParams,
62+
createSession,
6363
buildSessionAccount
6464
} from "@argent/x-sessions"
6565
import { ec } from "starknet"
6666

67-
const sessionParams: SessionParams = {
67+
const privateKey = ec.starkCurve.utils.randomPrivateKey()
68+
69+
const sessionKey: SessionKey = {
70+
privateKey,
71+
publicKey: ec.starkCurve.getStarkKey(privateKey)
72+
}
73+
74+
const sessionParams: CreateSessionParams = {
75+
sessionKey,
6876
allowedMethods: [
6977
{
7078
"Contract Address": contractAddress,
@@ -74,7 +82,7 @@ const sessionParams: SessionParams = {
7482
expiry: Math.floor(
7583
(Date.now() + 1000 * 60 * 60 * 24) / 1000
7684
) as any, // ie: 1 day
77-
dappKey: ec.starkCurve.utils.randomPrivateKey(),
85+
sessionKey: ec.starkCurve.utils.randomPrivateKey(),
7886
metaData: {
7987
projectID: "test-dapp",
8088
txFees: [
@@ -87,33 +95,22 @@ const sessionParams: SessionParams = {
8795
}
8896

8997
// open session and sign message
90-
const accountSessionSignature = await openSession({
98+
const session = await createSession({
9199
wallet, // StarknetWindowObject
92-
sessionParams, // SessionParams
93-
chainId // StarknetChainId
100+
address, // Account address
101+
chainId, // StarknetChainId
102+
sessionParams // CreateSessionParams
94103
})
95104

96-
// create the session account from the current one that will be used to submit transactions
97-
const sessionRequest = createSessionRequest(
98-
sessionParams.allowedMethods,
99-
sessionParams.expiry,
100-
sessionParams.metaData,
101-
sessionParams.dappKey
102-
)
103-
104105
const sessionAccount = await buildSessionAccount({
105106
useCacheAuthorisation: false, // optional and defaulted to false, will be added in future developments
106-
accountSessionSignature: stark.formatSignature(
107-
accountSessionSignature
108-
),
109-
sessionRequest,
110-
chainId, // StarknetChainId
107+
session,
108+
sessionKey,
111109
provider: new RpcProvider({
112110
nodeUrl: "https://starknet-sepolia.public.blastapi.io/rpc/v0_7",
113111
chainId: constants.StarknetChainId.SN_SEPOLIA
114112
}),
115-
address, // account address
116-
dappKey
113+
argentSessionServiceBaseUrl: ARGENT_SESSION_SERVICE_BASE_URL // Optional: defaulted to mainnet url
117114
})
118115

119116
try {
@@ -138,24 +135,59 @@ Executing transactions “from outside” allows an account to submit transactio
138135
This package expose a method in order to get the Call required to perform an execution from outside.
139136

140137
```typescript
141-
// instantiate argent session service
142-
const beService = new ArgentSessionService(
143-
dappKey.publicKey,
144-
accountSessionSignature
145-
)
138+
const privateKey = ec.starkCurve.utils.randomPrivateKey()
146139

147-
// instantiate dapp session service
148-
const sessionDappService = new SessionDappService(
149-
beService,
150-
chainId,
151-
dappKey
152-
)
140+
const sessionKey: SessionKey = {
141+
privateKey,
142+
publicKey: ec.starkCurve.getStarkKey(privateKey)
143+
}
144+
145+
const sessionParams: CreateSessionParams = {
146+
sessionKey,
147+
allowedMethods: [
148+
{
149+
"Contract Address": contractAddress,
150+
selector: "method_selector"
151+
}
152+
],
153+
expiry: Math.floor(
154+
(Date.now() + 1000 * 60 * 60 * 24) / 1000
155+
) as any, // ie: 1 day
156+
sessionKey: ec.starkCurve.utils.randomPrivateKey(),
157+
metaData: {
158+
projectID: "test-dapp",
159+
txFees: [
160+
{
161+
tokenAddress: ETHTokenAddress,
162+
maxAmount: parseUnits("0.1", 18).value.toString()
163+
}
164+
]
165+
}
166+
}
167+
168+
const session = await createSession({
169+
wallet, // StarknetWindowObject
170+
address, // Account address
171+
chainId, // StarknetChainId
172+
sessionParams // CreateSessionParams
173+
})
174+
175+
const sessionAccount = await buildSessionAccount({
176+
useCacheAuthorisation: false, // optional and defaulted to false, will be added in future developments
177+
session,
178+
sessionKey,
179+
provider: new RpcProvider({
180+
nodeUrl: "https://starknet-sepolia.public.blastapi.io/rpc/v0_7",
181+
chainId: constants.StarknetChainId.SN_SEPOLIA
182+
}),
183+
argentSessionServiceBaseUrl: ARGENT_SESSION_SERVICE_BASE_URL // Optional: defaulted to mainnet url
184+
})
153185

154186
// example for creating the calldata
155187
const erc20Contract = new Contract(
156188
Erc20Abi as Abi,
157189
ETHTokenAddress,
158-
sessionAccount as any
190+
sessionAccount
159191
)
160192
const calldata = erc20Contract.populate("transfer", {
161193
recipient: address,
@@ -164,18 +196,18 @@ const calldata = erc20Contract.populate("transfer", {
164196

165197
// get execute from outside data
166198
const { contractAddress, entrypoint, calldata } =
167-
await sessionDappService.getOutsideExecutionCall(
168-
sessionRequest,
169-
stark.formatSignature(accountSessionSignature),
170-
false,
171-
[calldata],
172-
address, // the account address
173-
chainId,
174-
shortString.encodeShortString("ANY_CALLER"), // Optional: default value ANY_CALLER
175-
execute_after, // Optional: timestamp in seconds - this is the lower value in the range. Default value: 5 mins before Date.now()
176-
execute_before, // Optional: timestamp in seconds - this is the upper value in the range. Default value: 20 mins after Date.now()
177-
nonce: BigNumberish, // Optional: nonce, default value is a random nonce
178-
)
179-
```
199+
await createOutsideExecutionCall({
200+
session,
201+
sessionKey,
202+
calls: [transferCallData],
203+
argentSessionServiceUrl: ARGENT_SESSION_SERVICE_BASE_URL
204+
})
180205

181-
Another account can then use object `{ contractAddress, entrypoint, calldata }` to execute the transaction.
206+
const { signature, outsideExecutionTypedData } =
207+
await createOutsideExecutionTypedData({
208+
session,
209+
sessionKey,
210+
calls: [transferCallData],
211+
argentSessionServiceUrl: ARGENT_SESSION_SERVICE_BASE_URL
212+
})
213+
```

0 commit comments

Comments
 (0)