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',
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()
NamespaceDescription
authorizationExpiry-
authorizationExpiryCall-
getExpirations-
isExpired-
login-
loginCall-
loginSync-
revoke-
revokeCall-
revokeSync-
InterfaceDescription
AccountFromSecp256k1Options-
FromSecp256k1Options-
SessionKey-
Type AliasDescription
SessionKeyAccount-
SessionKeyEvents-
SessionKeyPermissions-
SessionKeyType-
VariableDescription
ALL_PERMISSIONS-
EMPTY_EXPIRATIONS-
SESSION_KEY_PERMISSIONSSession key permissions type hash map
TYPE_HASH_TO_PERMISSION-
FunctionDescription
accountFromSecp256k1Create a session key account from a secp256k1 private key.
authorizationExpiryGet the authorization expiry timestamp for a session key permission.
authorizationExpiryCallCreate a call to the authorizationExpiry function
extractLoginEventExtracts the AuthorizationsUpdated event from transaction logs.
extractRevokeEventExtracts the AuthorizationsUpdated event from transaction logs.
fromSecp256k1Create a session key from a secp256k1 private key.
getExpirationsGet the expirations for all permissions.
getPermissionFromTypeHash-
isExpiredCheck if the session key is expired.
loginAuthorize a session key with permissions until expiry.
loginCallCreate a call to the login function.
loginSyncAuthorize a session key and wait for confirmation.
revokeRevoke session key permissions.
revokeCallCreate a call to the revoke function.
revokeSyncRevoke session key permissions and wait for confirmation.