Hackathon Guide
RPC Endpoint
Base URL: http://54.163.51.119:3007
Note: please request a working API Key to ata@securitylabs.xyz
Overview
Functor Network’s RPC endpoint provides methods for creating and managing smart accounts and session keys. This guide explains:
- Creating Smart Accounts with
functor_createSmartAccount
. - Creating Session Keys with
functor_createSessionKey
. - The process flow for using these tools to manage accounts and sessions efficiently in the Functor Network.
1. Method: functor_createSmartAccount
This method creates a smart contract wallet, or “smart account,” based on the ERC-4337 standard. These accounts support features like multi-signature (multi-sig), social recovery, and more flexible gas payment options.
Parameters
- owner: Wallet address of the account owner.
- recoveryMechanism: List of guardian addresses for account recovery.
- paymaster: Address of a contract that sponsors gas payments.
Sample Request
Here’s a JavaScript example using fetch to create a smart account:
fetch('http://54.163.51.119:3007', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'x-api-key': '[Your API key goes here]' },
body: JSON.stringify({
"id": 1,
"jsonrpc": "2.0",
"method": "functor_createSmartAccount",
"params": [
"0x1234567890abcdef1234567890abcdef12345678",
[
"0xabcdef1234567890abcdef1234567890abcdef12",
"0xabcdef1234567890abcdef1234567890abcdef13"
],
"0xabcdef1234567890abcdef1234567890abcdef14"
]
})
})
.then(response => response.json())
.then(data => console.log(data));
Expected Response
{
"jsonrpc": "2.0",
"result": {
"smartAccountAddress": "0x4abd3dD0a1efccad89BE4855Ccdea32E8CCBF591",
"creationDate": 71773046
},
"id": 1
}
2. Method: functor_createSessionKey
This method creates a session key with specific permissions, allowing certain actions within a smart contract wallet based on the smart contract ABI.
Parameters
- walletAddress: Address of the wallet to which the session key is linked.
- permissions: Array of permissions defining which methods the session key can invoke.
- expiry: Duration (in seconds) until the session key expires.
- metadata: (Optional) Additional information, such as a label or restriction indicator.
Sample Request
This JavaScript example demonstrates creating a session key for a smart wallet:
fetch('http://54.163.51.119:3007', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'x-api-key': '[Your API key goes here]' },
body: JSON.stringify({
"jsonrpc": "2.0",
"method": "functor_createSessionKey",
"params": [
"0x1234567890abcdef1234567890abcdef12345678", // Smart contract wallet address
[
{ // Permissions Object
"contractAbi": "[{\"constant\":false,\"inputs\":[{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transfer\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"type\":\"function\"}]",
"allowedMethods": [
"transfer(address,uint256)"
]
}
],
3600, // Session duration in seconds
{ // Metadata
"label": "AI Agent Session Key",
"restricted": true
}
],
"id": 1
})
})
.then(response => response.json())
.then(data => console.log(data));
Expected Response
The response includes details of the generated session key:
{
"jsonrpc": "2.0",
"result": {
"sessionKey": "0xabc123456...",
"expiry": 3600,
"metadata": {
"label": "AI Agent Session Key",
"restricted": true
}
},
"id": 1
}
Process Flow for Account and Session Management
- Account Creation: Start by creating a smart account using
functor_createSmartAccount
. This establishes the user’s wallet on the network with the owner’s address and any optional recovery mechanisms. - Session Key Creation: Once the account is set up, create a session key with
functor_createSessionKey
. This key allows the user or AI agent to interact with the smart account within specified permissions and duration, enhancing security and control.
Usage and Architecture
- Session: The session key enables limited, time-bound interactions with the smart contract wallet, ideal for task automation and access delegation.
- Architecture: The Functor Network architecture separates account management from session authorization, utilizing session keys to interact securely with smart contract wallet accounts. This minimizes risk by granting restricted access rather than full wallet control. Functor provides an L2 that serves as a single source of truth to seamlessly verify Session Key permissions across all EVM compatible blockchains.
Working Example
The following are instructions to run a working example to get you started sending requests to our RPC Endpoints. Open your terminal and execute the following commands:
mkdir test_functor_rpc;
cd test_functor_rpc;
npm init -y;
touch request.js;
NOTE: This example was run using node version 20.18.0
Copy the following code into the request.js
file:
function createSmartAccount () {
return new Promise((resolve, reject) => {
const requestBody = JSON.stringify({
"id": 1,
"jsonrpc": "2.0",
"method": "functor_createSmartAccount",
"params": [
"0x1234567890abcdef1234567890abcdef12345678",
[
"0xabcdef1234567890abcdef1234567890abcdef12",
"0xabcdef1234567890abcdef1234567890abcdef13"
],
"0xabcdef1234567890abcdef1234567890abcdef14"
]
});
console.log('requestBody...:', requestBody);
fetch('http://54.163.51.119:3007', {
method: 'POST',
headers: { 'Content-Type': 'application/json' , 'x-api-key': '8c854746-20cc-4521-b67c-4306ff53c8ff' },
body: requestBody
})
.then(async response => {
console.log('response...:', response);
if (!response.ok) {
// If the response is not OK, try to read it as text
const errorText = await response.text();
throw new Error(`Request failed with status ${response.status}: ${errorText}`);
}
return response.json();
})
.then(data => {
console.log("Response data:", data);
resolve(data);
})
.catch(error => {
console.error('Error:', error);
reject(error);
});
});
}
function createSessionKey(smartAccountAddress) {
return new Promise((resolve, reject) => {
fetch('http://54.163.51.119:3007', {
method: 'POST',
headers: { 'Content-Type': 'application/json' , 'x-api-key': '8c854746-20cc-4521-b67c-4306ff53c8ff' },
body: JSON.stringify({
"jsonrpc": "2.0",
"method": "functor_createSessionKey",
"params": [
"0x1234567890abcdef1234567890abcdef12345678",
[
{
"contractAbi": "[{\"constant\":false,\"inputs\":[{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transfer\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"type\":\"function\"}]",
"allowedMethods": [
"transfer(address,uint256)"
]
}
],
3600,
{
"label": "AI Agent Session Key",
"restricted": true
}
],
"id": 1
})
})
.then(response => response.json())
.then(data => {
console.log(data)
resolve(data);
})
.catch(error => {
console.error('Error:', error);
reject(error);
});
});
}
createSmartAccount().then( (smartAccount) => {
console.log("smartAccount...:", smartAccount);
createSessionKey(smartAccount.result.smartAccountAddress)
.then( (sessionKey) => {
console.log("sessionKey...:", sessionKey);
})
.catch( (error) => {
console.error('Error creating sessionKey:', error);
});
});
Run the code:
node request.js
You should see the smartAccount
and sessionKey
responses printed to the terminal.