Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SessionKeys Module #17

Merged
merged 4 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog
## [1.1.2] - 2024-07-10
### New
- Added `SessionKeyValidator` module for ERC20 SessionKeyValidator.
- Added `enableSessionKey`, `rotateSessionKey`, `disableSessionKey`, and `getAssociatedSessionKeys` functions to `SessionKeyValidator`.

## [1.1.1] - 2024-07-08
### Feature Enhancement
Expand Down
65 changes: 65 additions & 0 deletions examples/13-enable-sessionkey-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { EtherspotBundler, ModularSdk, SessionKeyValidator } from '../src';
import * as dotenv from 'dotenv';
import { sleep } from '../src/sdk/common';
import { KeyStore } from '../src/sdk/SessionKeyValidator';

dotenv.config();

async function main() {
const bundlerApiKey = 'eyJvcmciOiI2NTIzZjY5MzUwOTBmNzAwMDFiYjJkZWIiLCJpZCI6IjMxMDZiOGY2NTRhZTRhZTM4MGVjYjJiN2Q2NDMzMjM4IiwiaCI6Im11cm11cjEyOCJ9';

// initializating sdk...
const modularSdk = new ModularSdk({ privateKey: process.env.WALLET_PRIVATE_KEY },
{
chainId: Number(process.env.CHAIN_ID),
bundlerProvider: new EtherspotBundler(Number(process.env.CHAIN_ID), bundlerApiKey)
})

console.log('address: ', modularSdk.state.EOAAddress);

// get address of EtherspotWallet
const address: string = await modularSdk.getCounterFactualAddress();

console.log('\x1b[33m%s\x1b[0m', `EtherspotWallet address: ${address}`);

const token = '0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238';
const functionSelector = '0xa9059cbb';
const spendingLimit = '100000';
const validAfter = new Date().getTime();
const validUntil = new Date().getTime() + 24 * 60 * 60 * 1000;

// get instance of SessionKeyValidator
const sessionKeyModule = new SessionKeyValidator(
modularSdk,
new EtherspotBundler(Number(process.env.CHAIN_ID), bundlerApiKey)
)

const response = await sessionKeyModule.enableSessionKey(
token,
functionSelector,
spendingLimit,
validAfter,
validUntil,
KeyStore.AWS
);

console.log('\x1b[33m%s\x1b[0m', `UserOpHash: `, response.userOpHash);
console.log('\x1b[33m%s\x1b[0m', `SessionKey: `, response.sessionKey);

// get transaction hash...
console.log('Waiting for transaction...');
let userOpsReceipt = null;
const timeout = Date.now() + 60000; // 1 minute timeout
while ((userOpsReceipt == null) && (Date.now() < timeout)) {
await sleep(2);
userOpsReceipt = await modularSdk.getUserOpReceipt(response.userOpHash);
}
console.log('\x1b[33m%s\x1b[0m', `Transaction Receipt: `, userOpsReceipt);

const sessionKeys = await sessionKeyModule.getAssociatedSessionKeys();
console.log('\x1b[33m%s\x1b[0m', `AssociatedSessionKeys: `, sessionKeys);
}

main()
.catch(console.error)
.finally(() => process.exit());
65 changes: 65 additions & 0 deletions examples/14-rotate-sessionkey-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { EtherspotBundler, ModularSdk, SessionKeyValidator } from '../src';
import * as dotenv from 'dotenv';
import { sleep } from '../src/sdk/common';
import { KeyStore } from '../src/sdk/SessionKeyValidator';

dotenv.config();

async function main() {
const bundlerApiKey = 'eyJvcmciOiI2NTIzZjY5MzUwOTBmNzAwMDFiYjJkZWIiLCJpZCI6IjMxMDZiOGY2NTRhZTRhZTM4MGVjYjJiN2Q2NDMzMjM4IiwiaCI6Im11cm11cjEyOCJ9';

// initializating sdk...
const modularSdk = new ModularSdk({ privateKey: process.env.WALLET_PRIVATE_KEY },
{
chainId: Number(process.env.CHAIN_ID),
bundlerProvider: new EtherspotBundler(Number(process.env.CHAIN_ID), bundlerApiKey)
})

console.log('address: ', modularSdk.state.EOAAddress);

// get address of EtherspotWallet
const address: string = await modularSdk.getCounterFactualAddress();

console.log('\x1b[33m%s\x1b[0m', `EtherspotWallet address: ${address}`);

const token = '0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238';
const functionSelector = '0xa9059cbb';
const spendingLimit = '100000';
const validAfter = new Date().getTime();
const validUntil = new Date().getTime() + 24 * 60 * 60 * 1000;

// get instance of SessionKeyValidator
const sessionKeyModule = new SessionKeyValidator(
modularSdk,
new EtherspotBundler(Number(process.env.CHAIN_ID), bundlerApiKey)
)

const response = await sessionKeyModule.rotateSessionKey(
token,
functionSelector,
spendingLimit,
validAfter,
validUntil,
KeyStore.AWS
);

console.log('\x1b[33m%s\x1b[0m', `UserOpHash: `, response.userOpHash);
console.log('\x1b[33m%s\x1b[0m', `SessionKey: `, response.sessionKey);

// get transaction hash...
console.log('Waiting for transaction...');
let userOpsReceipt = null;
const timeout = Date.now() + 60000; // 1 minute timeout
while ((userOpsReceipt == null) && (Date.now() < timeout)) {
await sleep(2);
userOpsReceipt = await modularSdk.getUserOpReceipt(response.userOpHash);
}
console.log('\x1b[33m%s\x1b[0m', `Transaction Receipt: `, userOpsReceipt);

const sessionKeys = await sessionKeyModule.getAssociatedSessionKeys();
console.log('\x1b[33m%s\x1b[0m', `AssociatedSessionKeys: `, sessionKeys);
}

main()
.catch(console.error)
.finally(() => process.exit());
51 changes: 51 additions & 0 deletions examples/15-disable-sessionkey-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { EtherspotBundler, ModularSdk, SessionKeyValidator } from '../src';
import * as dotenv from 'dotenv';
import { sleep } from '../src/sdk/common';

dotenv.config();

async function main() {
const bundlerApiKey = 'eyJvcmciOiI2NTIzZjY5MzUwOTBmNzAwMDFiYjJkZWIiLCJpZCI6IjMxMDZiOGY2NTRhZTRhZTM4MGVjYjJiN2Q2NDMzMjM4IiwiaCI6Im11cm11cjEyOCJ9';

// initializating sdk...
const modularSdk = new ModularSdk({ privateKey: process.env.WALLET_PRIVATE_KEY },
{
chainId: Number(process.env.CHAIN_ID),
bundlerProvider: new EtherspotBundler(Number(process.env.CHAIN_ID), bundlerApiKey)
})

console.log('address: ', modularSdk.state.EOAAddress);

// get address of EtherspotWallet
const address: string = await modularSdk.getCounterFactualAddress();

console.log('\x1b[33m%s\x1b[0m', `EtherspotWallet address: ${address}`);

// get instance of SessionKeyValidator
const sessionKeyModule = new SessionKeyValidator(
modularSdk,
new EtherspotBundler(Number(process.env.CHAIN_ID), bundlerApiKey)
)

const response = await sessionKeyModule.disableSessionKey();

console.log('\x1b[33m%s\x1b[0m', `UserOpHash: `, response.userOpHash);
console.log('\x1b[33m%s\x1b[0m', `SessionKey: `, response.sessionKey);

// get transaction hash...
console.log('Waiting for transaction...');
let userOpsReceipt = null;
const timeout = Date.now() + 60000; // 1 minute timeout
while ((userOpsReceipt == null) && (Date.now() < timeout)) {
await sleep(2);
userOpsReceipt = await modularSdk.getUserOpReceipt(response.userOpHash);
}
console.log('\x1b[33m%s\x1b[0m', `Transaction Receipt: `, userOpsReceipt);

const sessionKeys = await sessionKeyModule.getAssociatedSessionKeys();
console.log('\x1b[33m%s\x1b[0m', `AssociatedSessionKeys: `, sessionKeys);
}

main()
.catch(console.error)
.finally(() => process.exit());
4 changes: 2 additions & 2 deletions package-lock.json

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

17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@etherspot/modular-sdk",
"version": "1.1.1",
"version": "1.1.2",
"description": "Etherspot Modular SDK - build with ERC-7579 smart accounts modules",
"keywords": [
"ether",
Expand Down Expand Up @@ -31,13 +31,14 @@
"04-transfer-nft": "./node_modules/.bin/ts-node ./examples/04-transfer-nft",
"05-add-guardians": "./node_modules/.bin/ts-node ./examples/05-add-guardians",
"06-paymaster": "./node_modules/.bin/ts-node ./examples/06-paymaster",
"07-paymaster-arka": "./node_modules/.bin/ts-node ./examples/07-paymaster-arka",
"08-paymaster-validUntil-validAfter": "./node_modules/.bin/ts-node ./examples/08-paymaster-validUntil-validAfter",
"09-callGasLimit": "./node_modules/.bin/ts-node ./examples/09-callGasLimit",
"10-get-multiple-accounts": "./node_modules/.bin/ts-node ./examples/10-get-multiple-accounts",
"11-concurrent-userops": "./node_modules/.bin/ts-node ./examples/11-concurrent-userops",
"14-install-module": "./node_modules/.bin/ts-node ./examples/14-install-module",
"15-uninstall-module": "./node_modules/.bin/ts-node ./examples/15-uninstall-module",
"07-callGasLimit": "./node_modules/.bin/ts-node ./examples/07-callGasLimit",
"08-get-multiple-accounts": "./node_modules/.bin/ts-node ./examples/08-get-multiple-accounts",
"09-concurrent-userops": "./node_modules/.bin/ts-node ./examples/09-concurrent-userops",
"11-install-module": "./node_modules/.bin/ts-node ./examples/11-install-module",
"12-uninstall-module": "./node_modules/.bin/ts-node ./examples/12-uninstall-module",
"13-enable-sessionkey-module": "./node_modules/.bin/ts-node ./examples/13-enable-sessionkey-module",
"14-rotate-sessionkey-module": "./node_modules/.bin/ts-node ./examples/14-rotate-sessionkey-module",
"15-disable-sessionkey-module": "./node_modules/.bin/ts-node ./examples/15-disable-sessionkey-module",
"format": "prettier --write \"{src,test,examples}/**/*.ts\"",
"lint": "eslint \"{src,test,examples}/**/*.ts\"",
"lint-fix": "npm run lint -- --fix",
Expand Down
Loading
Loading