Skip to content

Commit

Permalink
Merge pull request #2 from argentlabs/fix/wallet-snip-offchain-sessions
Browse files Browse the repository at this point in the history
fix: offchain session account to work with wallet snip and webwallet
  • Loading branch information
bluecco authored Apr 2, 2024
2 parents b731c7e + a37a2e6 commit aa531c5
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 35 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: release
on:
push:
branches:
- develop
- main

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
persist-credentials: false

- uses: pnpm/action-setup@v2
name: install-pnpm
id: pnpm-install
with:
version: 8
run_install: false

- uses: actions/setup-node@v3
with:
node-version: 18

- run: pnpm install --frozen-lockfile
- run: pnpm run build

- name: semantic-release
run: npx semantic-release --debug true --dry-run false
env:
GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.CI_NPM_TOKEN }}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@commitlint/cli": "^19.2.1",
"@commitlint/config-conventional": "^19.1.0",
"@rollup/plugin-node-resolve": "^15.0.0",
"@semantic-release/git": "^10.0.1",
"@types/minimalistic-assert": "^1.0.1",
"@typescript-eslint/eslint-plugin": "^7.5.0",
"@vitest/browser": "1.3.1",
Expand Down
83 changes: 82 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { BigNumberish, Call, types } from "starknet"

export type OffchainSessionDetails = {
nonce: BigNumberish
maxFee: BigNumberish | undefined
version: `${types.RPC.ETransactionVersion}`
}

export type OffchainSessionCall = Call & {
offchainSessionDetails?: OffchainSessionDetails
}

export interface OffchainSessionAddInvokeTransactionParameters {
calls: OffchainSessionCall[]
}
77 changes: 43 additions & 34 deletions src/offchainSessionAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
stark,
} from "starknet"
import { ensureArray } from "./ensureArray"
import { OffchainSessionCall, OffchainSessionDetails } from "./interface"

const OFFCHAIN_SESSION_ENTRYPOINT = "use_offchain_session"

Expand All @@ -39,7 +40,10 @@ export class OffchainSessionAccount
super(providerOrOptions, address, pkOrSigner, "1")
}

private async sessionToCall(dappSignature: Signature): Promise<Call> {
private async sessionToCall(
dappSignature: Signature,
offchainSessionDetails: OffchainSessionDetails,
): Promise<OffchainSessionCall> {
const signature = stark.formatSignature(this.sessionSignature)
const formattedDappSignature = stark.formatSignature(dappSignature)

Expand All @@ -57,14 +61,19 @@ export class OffchainSessionAccount
token5: signature[2],
token6: signature[3],
}),
offchainSessionDetails,
}
}

private async extendCallsBySession(
calls: Call[],
dappSignature: Signature,
): Promise<Call[]> {
const sessionCall = await this.sessionToCall(dappSignature)
offchainSessionDetails: OffchainSessionDetails,
): Promise<OffchainSessionCall[]> {
const sessionCall = await this.sessionToCall(
dappSignature,
offchainSessionDetails,
)
return [sessionCall, ...calls]
}

Expand Down Expand Up @@ -92,47 +101,45 @@ export class OffchainSessionAccount

let maxFee = transactionsDetail.maxFee

if (!maxFee) {
try {
const sim = await this.simulateTransaction(
[
{
type: TransactionType.INVOKE,
payload: calls,
},
],
try {
const sim = await this.simulateTransaction(
[
{
type: TransactionType.INVOKE,
payload: calls,
},
],
{
skipValidate: true,
nonce,
},
)

const [estimation] = sim
const { fee_estimation } = estimation
const overall_fee = fee_estimation.overall_fee

maxFee = estimation.suggestedMaxFee ?? overall_fee
} catch (e) {
// fallback
maxFee = (
await this.getSuggestedFee(
{
type: TransactionType.INVOKE,
payload: calls,
},
{
skipValidate: true,
nonce,
},
)

const [estimation] = sim
const { fee_estimation } = estimation
const overall_fee = fee_estimation.overall_fee

maxFee = estimation.suggestedMaxFee ?? overall_fee
} catch (e) {
// fallback
maxFee = (
await this.getSuggestedFee(
{
type: TransactionType.INVOKE,
payload: calls,
},
{
skipValidate: true,
nonce,
},
)
).suggestedMaxFee
}
).suggestedMaxFee
}

const signerDetails: InvocationsSignerDetails = {
walletAddress: this.account.address,
nonce,
maxFee,
maxFee: maxFee,
version: "0x1" as const,
chainId,
cairoVersion: this.cairoVersion ?? "1",
Expand All @@ -147,13 +154,15 @@ export class OffchainSessionAccount
const transactionsWithSession = await this.extendCallsBySession(
transactions,
dappSignature,
{ nonce, maxFee: maxFee.toString(), version: "0x1" as const },
)

const invokeParams: AddInvokeTransactionParameters = {
calls: transactionsWithSession.map((t) => ({
contract_address: t.contractAddress,
entrypoint: t.entrypoint,
calldata: t.calldata as string[],
offchainSessionDetails: t.offchainSessionDetails,
})),
}

Expand Down

0 comments on commit aa531c5

Please sign in to comment.