-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,451 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.cartridge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Cartridge Node.js Session Example | ||
|
||
This example demonstrates how to use the Cartridge session controller with Node.js | ||
|
||
## Setup | ||
|
||
1. Install dependencies: | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
2. Run the example: | ||
|
||
```bash | ||
npm start | ||
``` | ||
|
||
## What's happening? | ||
|
||
The example demonstrates: | ||
1. Setting up a filesystem backend for session storage | ||
2. Creating a session controller instance | ||
3. Creating a new session with specific policies | ||
4. Executing a transaction using the session | ||
|
||
## Customization | ||
|
||
To use this example with your own contract: | ||
|
||
1. Replace the contract address (`0x123...`) with your actual contract address | ||
2. Modify the policies to match your contract's methods | ||
3. Update the transaction parameters in the `execute` call to match your use case |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "cartridge-node-example", | ||
"version": "1.0.0", | ||
"description": "Example of using Cartridge session controller with Node.js", | ||
"type": "module", | ||
"scripts": { | ||
"build": "tsc", | ||
"start": "node --experimental-wasm-modules dist/session.js", | ||
"dev": "node --experimental-wasm-modules --import tsx src/session.ts" | ||
}, | ||
"dependencies": { | ||
"@cartridge/account-wasm": "workspace:*", | ||
"@cartridge/controller": "workspace:*", | ||
"starknet": "catalog:" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^5.3.3", | ||
"tsx": "^4.7.0", | ||
"@types/node": "^22.12.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import SessionProvider, { | ||
ControllerError, | ||
} from "@cartridge/controller/session/node"; | ||
import { constants } from "starknet"; | ||
import path from "path"; | ||
|
||
export const STRK_CONTRACT_ADDRESS = | ||
"0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"; | ||
|
||
async function main() { | ||
// Path to store session | ||
const storagePath = | ||
process.env.CARTRIDGE_STORAGE_PATH || | ||
path.join(process.cwd(), ".cartridge"); | ||
|
||
// Create a session provider | ||
const provider = new SessionProvider({ | ||
rpc: "https://api.cartridge.gg/x/starknet/sepolia", | ||
chainId: constants.StarknetChainId.SN_SEPOLIA, | ||
policies: { | ||
contracts: { | ||
[STRK_CONTRACT_ADDRESS]: { | ||
methods: [ | ||
{ | ||
name: "approve", | ||
entrypoint: "approve", | ||
description: "Approve spending of tokens", | ||
}, | ||
{ name: "transfer", entrypoint: "transfer" }, | ||
], | ||
}, | ||
}, | ||
}, | ||
basePath: storagePath, | ||
}); | ||
try { | ||
// Connect and create session | ||
const account = await provider.connect(); | ||
console.log("Session initialized!"); | ||
|
||
if (account) { | ||
console.log("Account address:", account.address); | ||
|
||
// Example: Transfer ETH | ||
const amount = "0x0"; | ||
const recipient = account.address; // Replace with actual recipient address | ||
|
||
const result = await account.execute([ | ||
{ | ||
contractAddress: STRK_CONTRACT_ADDRESS, | ||
entrypoint: "transfer", | ||
calldata: [recipient, amount, "0x0"], | ||
}, | ||
]); | ||
|
||
console.log("Transaction hash:", result.transaction_hash); | ||
} else { | ||
console.log("Please complete the session creation in your browser"); | ||
} | ||
} catch (error: unknown) { | ||
const controllerError = error as ControllerError; | ||
if (controllerError.code) { | ||
console.error("Session error:", { | ||
code: controllerError.code, | ||
message: controllerError.message, | ||
data: controllerError.data, | ||
}); | ||
} else { | ||
console.error("Session error:", error); | ||
} | ||
} | ||
} | ||
|
||
main().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"module": "NodeNext", | ||
"moduleResolution": "NodeNext", | ||
"esModuleInterop": true, | ||
"strict": true, | ||
"skipLibCheck": true, | ||
"outDir": "./dist", | ||
"rootDir": "./src", | ||
"declaration": true, | ||
"allowJs": true, | ||
"resolveJsonModule": true | ||
}, | ||
"include": ["src/**/*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.