6.7.10 • Published 9 months ago

@argent/x-sessions v6.7.10

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Sessions

Sessions can be used to send transactions from a dapp on behalf of a user without requiring their confirmation with a wallet.

The user is guaranteed that the dapp can only execute transactions that comply to the policies of the session and until the session expires.

Installation

npm install @argent/x-sessions
# or
yarn add @argent/x-sessions
# or
pnpm add @argent/x-sessions

Creating a session as a dapp

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

A session kind of looks like this:

interface Policy {
  contractAddress: string
  selector: string
}

interface RequestSession {
  key: string
  expires: number
  policies: Policy[]
}

You can either get that by using the Account and Signer classes provided by starknet.js, or use an injected instance by a wallet.

const account = window.starknet.account

Next you need to come up with the permissions you would like for your session. You also need to generate the session keypair you want to grant these rights to.

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 policies whenever it wants and as many times as it wants.

import { Signer, ec } from "starknet"

// gets signer with random private key you need to store if you want to reuse the session
const sessionSigner = new Signer()

const requestSession: RequestSession = {
  key: await sessionSigner.getPublicKey(),
  expires: Math.floor((Date.now() + 1000 * 60 * 60 * 24) / 1000), // 1 day in seconds
  policies: [
    {
      contractAddress: "0x...",
      selector: "doAction"
    }
  ]
}

Now you can sign the session with the account you have. Depending on how your account works, the user may get asked to sign a message

import { createSession } from "@argent/x-sessions"

// calls account.signMessage internally
const signedSession = await createSession(requestSession, account)

Using established sessions

With your signed session you can now use it with your dapp to do transactions without the user having to approve again.

import { SessionAccount } from "@argent/x-sessions"

const sessionAccount = new SessionAccount(
  account,
  account.address,
  sessionSigner,
  signedSession
)

// this transaction should get executed without the user having to approve again
const tx = sessionAccount.execute({
  // lets assume this is a erc20 contract
  contractAddress: "0x...",
  selector: "transfer",
  calldata: [
    "0x...",
    // ...
  ]
})

You can also use the session when you dont have access to the main account (window.starknet.account). You only need access to the signedSession object and the sessionSigner.

const sessionAccount = new SessionAccount(
  providerWithCorrectNetwork,
  "0xaccountAddress",
  sessionSigner,
  signedSession
)

Congratulations, you can now use your dapp to execute transactions without the user having to approve!

Allowing a session as a user

Note: Sessions are an alpha feature and are not yet available on mainnet. You can use them on testnet, but expect bugs. That is also the reason why it is so inconvenient to activate them for use in Argent X right now.

Sessions can just be used with a new type of account, pioneered by Argent X. This guide will set you up with a new account that can use sessions, by deploying a Plugin Account and registering your first plugin (Sessions Plugin!).

Activate Plugin account in Argent X

You can activate session accounts in Argent X Settings, under the "Experimental features" tab.

Argent X SettingsExperimental SettingsActivate Plugin Account

Deploy a Plugin Account

We strongly recommend to use a new account for sessions. You can create a new account in Argent X by clicking the big + button in the account list.

Upgrade Account to use Plugin Account Implementation

You can upgrade your account to use the Plugin Account implementation by opening the account context menu and selecting "Use Plugins". It will prompt you to confirm a upgrade transaction.

Upgrade AccountUpgrade Transaction

Register Sessions Plugin

After the upgrade is done you can register the Sessions Plugin by clicking the new Plugins button in your Account. It will prompt you to confirm a register transaction.

Register PluginRegister Transaction

Test if Sessions work

To test if sessions work for your account you can head to the Wallet Playground. It should show supports sessions: true at the top and you should be able to test sessions in the playground.

Each "Use Session" click will send a transaction to mint 666 Test Tokens to the account.

6.7.10

9 months ago

6.7.9

9 months ago

6.7.8

10 months ago

6.7.7

10 months ago

6.6.3

1 year ago

6.7.0

1 year ago

6.7.2

1 year ago

6.7.1

1 year ago

6.7.4

12 months ago

6.7.3

1 year ago

6.7.6

11 months ago

6.7.5

12 months ago

6.6.2

1 year ago

6.6.1

1 year ago

6.6.0

1 year ago

6.5.5

1 year ago

6.5.4

1 year ago

1.0.0

1 year ago

6.5.3

1 year ago

6.5.2

2 years ago

6.5.1

2 years ago

6.5.0

2 years ago

6.4.0

2 years ago

6.3.1

2 years ago

6.3.0

2 years ago

6.2.0

2 years ago

5.3.21

2 years ago

5.3.20

2 years ago

5.3.1

2 years ago

5.3.0

2 years ago

6.0.2

2 years ago

5.3.17

2 years ago

5.3.16

2 years ago

5.3.15

2 years ago

5.3.14

2 years ago

5.3.13

2 years ago

5.3.12

2 years ago

5.3.11

2 years ago

5.3.6

2 years ago

5.3.4

2 years ago

5.2.0

3 years ago

5.0.6

3 years ago

5.0.5

3 years ago

5.1.3

3 years ago

5.1.2

3 years ago

5.0.3

3 years ago

5.1.1

3 years ago

5.0.2

3 years ago

5.1.0

3 years ago

5.0.1

3 years ago

4.8.8

3 years ago

5.0.10

3 years ago

5.0.11

3 years ago

5.0.12

3 years ago

4.8.5

3 years ago

6.0.1

3 years ago

4.8.7

3 years ago

6.0.0

3 years ago

4.8.6

3 years ago

4.8.4-noble.0

3 years ago

5.0.9

3 years ago

5.0.8

3 years ago

5.0.7

3 years ago

4.8.4

3 years ago

4.8.3

3 years ago

4.8.2

3 years ago

4.8.1

3 years ago

4.8.0

3 years ago