Skip to content

feat: new session keys with viem#618

Merged
hugomrdias merged 6 commits intomasterfrom
hugomrdias/session-keys
Feb 25, 2026
Merged

feat: new session keys with viem#618
hugomrdias merged 6 commits intomasterfrom
hugomrdias/session-keys

Conversation

@hugomrdias
Copy link
Copy Markdown
Member

@hugomrdias hugomrdias commented Feb 19, 2026

  • still needs wiring in react and sdk
  • proper e2e testing

Example usage:

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(SessionKey.CreateDataSetPermission)) {
  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()

@github-project-automation github-project-automation bot moved this to 📌 Triage in FOC Feb 19, 2026
@hugomrdias hugomrdias changed the title Hugomrdias/session-keys feat: new session keys with viem Feb 19, 2026
@hugomrdias hugomrdias self-assigned this Feb 19, 2026
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages bot commented Feb 19, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
synapse-dev 7fdc04d Commit Preview URL

Branch Preview URL
Feb 25 2026, 11:59 AM

const permission = getPermissionFromTypeHash(hash)
this.expirations[permission] = event.args.expiry
}
this.emit('expirationsUpdated', this.#expirations)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and if it fails and doesn't even get here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? it will throw an error. User still gets the connected event if syncExpirations doesnt fail.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean watchContractEvent, not a big deal but this is lost here: https://v1.viem.sh/docs/contract/watchContractEvent.html#onerror-optional

@rvagg
Copy link
Copy Markdown
Collaborator

rvagg commented Feb 20, 2026

This might be a good opportunity to deal with type SessionKeyPermissions being a closed union and ALL_PERMISSIONS being a bit misleading in its name. This came up this week in filecoin-project/curio#1028, noting that (a) SessionKeyRegistry doesn't care what these strings are, it's not exclusive, and (b) DeleteDataSet isn't even standard anymore, it's gone, so our existing set isn't even accurate (but as per that issue we're talking about repurposing it to do Client<>Curio auth, not touching FWSS).

Perhaps:

  • ALL_PERMISSIONS -> FWSS_PERMISSIONS
  • EMPTY_PERMISSIONS has the same problem
  • Make SessionKeyPermissions open, or rename it to FWSSPermissions and do a type SessionKeyPermission = FWSSPermissions | Hex.
  • login(), revoke() should expand their permissions? as per above.
  • getExpirations() and hasPermission() is harder, but we could clearly document that they are dealing specifically with FWSS only

Not a blocker, but the current session key code gives the impression this is all canonical and a closed set of permissions. But developers should be able to use the registry to do non-FWSS things and having an SDK that makes that easy would be nice.

@rvagg
Copy link
Copy Markdown
Collaborator

rvagg commented Feb 20, 2026

Will I be able to pass sessionKey.client to the SDK in the end of this? For the signing operations, will we need to update them to differentiate between a SessionKeyAccount client and a standard one so we can pick out rootAddress when it exists?

@hugomrdias
Copy link
Copy Markdown
Member Author

Will I be able to pass sessionKey.client to the SDK in the end of this? For the signing operations, will we need to update them to differentiate between a SessionKeyAccount client and a standard one so we can pick out rootAddress when it exists?

Yeah not sure yet, because session keys only works for those 4 mutations (maybe just 3 if delete dataset is gone), some of the other services do other mutations that need the root client.

I think for now I'm just adding a new constructor option to pass a session keys and internally use if available and has permission.

There's one subtle thing in this design, that I want your opinion.

The connect/disconnect are actually optional, they are there if you want realtime tracking of the permissions, if you don't you can just pass whatever expirations you got from login and YOLO the mutations.

I will be cleaning up with your comments and finish testing hopefully sps work today.

@rvagg
Copy link
Copy Markdown
Collaborator

rvagg commented Feb 23, 2026

you can just pass whatever expirations you got from login and YOLO the mutations

Pass to where? I don't think we care in any of the places we use it, so this isn't an internal concern. I think the answer here is to just document how expirations work and for long-lived session key instances advise that the developer should be sure to refresh expiries occasionally to be sure to perform login() as needed. Otherwise, not our problem (except for the fact that errors arising from expired permissions will be opaque about the cause!).

@hugomrdias
Copy link
Copy Markdown
Member Author

you can just pass whatever expirations you got from login and YOLO the mutations

Pass to where? I don't think we care in any of the places we use it, so this isn't an internal concern. I think the answer here is to just document how expirations work and for long-lived session key instances advise that the developer should be sure to refresh expiries occasionally to be sure to perform login() as needed. Otherwise, not our problem (except for the fact that errors arising from expired permissions will be opaque about the cause!).

to fromSecp256k1 and the SessionKey instance will have whatever expirations you want, if you dont connect nor syncExpirations it will just use the initial ones.

but if you want to track expirations and get events to logout user or something you can.

@hugomrdias hugomrdias force-pushed the hugomrdias/session-keys branch from aa80d34 to 59913f0 Compare February 23, 2026 21:37
@hugomrdias hugomrdias marked this pull request as ready for review February 23, 2026 21:38
@BigLep BigLep moved this from ⌨️ In Progress to 🔎 Awaiting review in FOC Feb 23, 2026
@hugomrdias hugomrdias force-pushed the hugomrdias/session-keys branch from 59913f0 to 20c8483 Compare February 24, 2026 10:32
@hugomrdias hugomrdias force-pushed the hugomrdias/session-keys branch from b63ee2f to 8ba72b0 Compare February 24, 2026 13:21
@hugomrdias hugomrdias requested a review from rvagg February 24, 2026 13:22
Copy link
Copy Markdown
Collaborator

@rvagg rvagg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With some suggestions, and some slightly more firm requests which are probably: missing onError handling, unless you want to defend not doing that? The Synapse.create could do with some attention while you're at it. I also wouldn't mind hearing more justification on connect() / disconnect() being in core and not a react specialty (I'm imagining "there are other long-running uses out there" .. but .. really, it's pretty much only going to be React right?).

I'm going to follow up with a docs PR to this.

@hugomrdias hugomrdias merged commit ad9ebe7 into master Feb 25, 2026
11 checks passed
@hugomrdias hugomrdias deleted the hugomrdias/session-keys branch February 25, 2026 12:54
@github-project-automation github-project-automation bot moved this from 🔎 Awaiting review to 🎉 Done in FOC Feb 25, 2026
lordshashank pushed a commit to lordshashank/synapse-sdk that referenced this pull request Mar 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 🎉 Done

Development

Successfully merging this pull request may close these issues.

hook in session key properly, rethink session key client and inherit transport properly

4 participants