Skip to content

session-key

Session Key Contract Operations

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',
    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()
Namespace Description
authorizationExpiry -
authorizationExpiryCall -
getExpirations -
isExpired -
login -
loginCall -
loginSync -
revoke -
revokeCall -
revokeSync -
Interface Description
AccountFromSecp256k1Options -
FromSecp256k1Options -
SessionKey -
Type Alias Description
AddPiecesPermission -
CreateDataSetPermission -
Expirations -
Permission -
SchedulePieceRemovalsPermission -
SessionKeyAccount -
SessionKeyEvents -
SessionKeyType -
TerminateServicePermission -
Variable Description
AddPiecesPermission -
CreateDataSetPermission -
DefaultEmptyExpirations -
DefaultFwssPermissions -
extractRevokeEvent Extracts the AuthorizationsUpdated event from transaction logs.
PermissionNames Human-readable EIP-712 primary type names for each FWSS permission hash. Useful for building error messages and debug output that reference permissions by name instead of opaque bytes32 hashes.
SchedulePieceRemovalsPermission -
TerminateServicePermission -
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.
fromSecp256k1 Create a session key from a secp256k1 private key.
getExpirations Get the expirations for all FWSS permissions.
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.