session-key
Session Key Contract Operations
Example
Section titled “Example”import * as SessionKey from '@filoz/synapse-core/session-key'import { privateKeyToAccount } from 'viem/accounts'import { type Hex } from 'viem'import { mainnet } from '@filoz/synapse-core/chains'
const rootAccount = privateKeyToAccount('0xaa14e25eaea762df1533e72394b85e56dd0c7aa61cf6df3b1f13a842ca0361e5' as Hex)
const sessionKey = SessionKey.fromSecp256k1({ privateKey: '0xaa14e25eaea762df1533e72394b85e56dd0c7aa61cf6df3b1f13a842ca0361e5' as Hex, root: rootAccount, chain: mainnet,})sessionKey.on('expirationsUpdated', (e) => {console.log(e.detail)})sessionKey.on('connected', (e) => {console.log(e.detail)})sessionKey.on('disconnected', () => {console.log('disconnected')})sessionKey.on('error', (e) => {console.log(e.detail)})
const { event: loginEvent } = await SessionKey.loginSync(client, { address: sessionKey.address, onHash(hash) { console.log(`Waiting for tx ${hash} to be mined...`) },})
console.log('event login:', loginEvent.args)
await sessionKey.connect()
if(sessionKey.hasPermission('CreateDataSet')) { const hash = await createDataSet(sessionKey.client, { payee: '0x1234567890123456789012345678901234567890', payer: sessionKey.rootAddress, serviceURL: 'https://example.com', }) console.log('event created dataset:', hash)}
const { event: revokeEvent } = await SessionKey.revokeSync(client, { address: sessionKey.address, onHash(hash) { console.log(`Waiting for tx ${hash} to be mined...`) },})console.log('event revoked:', revokeEvent.args)sessionKey.disconnect()Namespaces
Section titled “Namespaces”| Namespace | Description |
|---|---|
| authorizationExpiry | - |
| authorizationExpiryCall | - |
| getExpirations | - |
| isExpired | - |
| login | - |
| loginCall | - |
| loginSync | - |
| revoke | - |
| revokeCall | - |
| revokeSync | - |
Interfaces
Section titled “Interfaces”| Interface | Description |
|---|---|
| AccountFromSecp256k1Options | - |
| FromSecp256k1Options | - |
| SessionKey | - |
Type Aliases
Section titled “Type Aliases”| Type Alias | Description |
|---|---|
| SessionKeyAccount | - |
| SessionKeyEvents | - |
| SessionKeyPermissions | - |
| SessionKeyType | - |
Variables
Section titled “Variables”| Variable | Description |
|---|---|
| ALL_PERMISSIONS | - |
| EMPTY_EXPIRATIONS | - |
| SESSION_KEY_PERMISSIONS | Session key permissions type hash map |
| TYPE_HASH_TO_PERMISSION | - |
Functions
Section titled “Functions”| Function | Description |
|---|---|
| accountFromSecp256k1 | Create a session key account from a secp256k1 private key. |
| authorizationExpiry | Get the authorization expiry timestamp for a session key permission. |
| authorizationExpiryCall | Create a call to the authorizationExpiry function |
| extractLoginEvent | Extracts the AuthorizationsUpdated event from transaction logs. |
| extractRevokeEvent | Extracts the AuthorizationsUpdated event from transaction logs. |
| fromSecp256k1 | Create a session key from a secp256k1 private key. |
| getExpirations | Get the expirations for all permissions. |
| getPermissionFromTypeHash | - |
| isExpired | Check if the session key is expired. |
| login | Authorize a session key with permissions until expiry. |
| loginCall | Create a call to the login function. |
| loginSync | Authorize a session key and wait for confirmation. |
| revoke | Revoke session key permissions. |
| revokeCall | Create a call to the revoke function. |
| revokeSync | Revoke session key permissions and wait for confirmation. |