Skip to content

Support isolated SequenceLogin instances for dual sessions - #351

Open
tolgahan-arikan wants to merge 5 commits into
masterfrom
feature/dual-session-support
Open

tolgahan-arikan wants to merge 5 commits into
masterfrom
feature/dual-session-support

Conversation

@tolgahan-arikan

@tolgahan-arikan tolgahan-arikan commented Nov 5, 2026 •

Copy link
Copy Markdown

Adds opt-in support for holding a second embedded-wallet session alongside the default one, so an existing account can be authenticated while the current guest wallet stays usable (TapNation guest-reward merge).

Related: 0xsequence/issue-tracker#7344, 0xsequence/issue-tracker#7348

Changes

  • SequenceLogin.CreateIsolatedInstance(string storageNamespace, ...): non-singleton instance.
    • Session stored under {company}-{product}-{namespace}-SessionWallet.
    • Raises per-instance OnSessionWalletCreated / OnSessionRecoveryFailed; does not raise the static SequenceWallet.OnWalletCreated / OnFailedToRecoverSession, so EmbeddedWalletAdapter and SessionManager stay on the default session.
    • No auto-federation; no writes to WaaSLoginMethod / LoginEmail PlayerPrefs.
  • PromoteToDefaultSession(SequenceWallet): writes the session to the default key, clears the namespaced key, clears federation mode on the default instance, raises SequenceWallet.OnWalletCreated. Closing the previous (guest) session stays with the caller, after transfers confirm.
  • ClearStoredSession(): removes an instance's stored session.
  • TryToRestoreSessionAsync listens to the per-instance events (default instance still raises the static ones).
  • SequenceWallet keeps its session key and owning login. On account federation it persists its own key under its owner's namespace; previously it stored the singleton's current session key, which is wrong once two sessions exist.
  • Social sign-in callbacks go to the instance that started the sign-in (OpenIdAuthenticator, IosBrowser). Before, with two instances:
    • iOS: IosBrowser was a singleton bound to the last created authenticator; a sign-in started on the default instance was delivered to the isolated one and failed with "State token mismatch".
    • Android / macOS / WebGL: both instances receive Application.deepLinkActivated; the one that did not start the sign-in raised OnLoginFailed.
    • Windows: the IPC listener runs only on the first authenticator, so links for the isolated instance went to the default one.
    • Now: authenticators register the state they issue; a link with another authenticator's state is ignored; single-receiver callbacks use OpenIdAuthenticator.RouteDeepLink (public). Links with a state nobody issued fail as before.
  • Default GetInstance() behaviour unchanged.
  • Test-only files outside the package: Assets/Editor/DualSessionVerification (batchmode harness), Assets/DualSessionDeviceTest (device test app + APK build), 3 tests added to DeepLinkHandlerTests.

Verification

Batchmode harness Assets/Editor/DualSessionVerification (Unity 2022.3.62f2, dev WaaS, each phase a separate editor process; not NUnit because StoreSessionKey() disables storage during test runs):

  • setup 13/13, restart 8/8, final 2/2: guest on default + guest on isolated instance, backend sessions, storage keys, adapter/event isolation, transfer from guest, restore after restart, guest sign-out isolation, promotion, restore of promoted account.
  • email-create 2/2, email-merge 15/15: guest live, existing email account authenticated on isolated instance, wrong OTP rejected without side effects, auth returns the existing wallet, transfer guest → existing wallet, guest closed, promotion, adapter switched.
  • social-routing 9/9 (0 network sign-ins; link delivered through Unity's Application.InvokeDeepLinkActivated and through RouteDeepLink): before the routing fix 3 checks fail (spurious OnLoginFailed on the default instance, single-receiver link not reaching the isolated instance).
  • iOS Simulator (iPhone 17 Pro, IL2CPP, real IosBrowser / ASWebAuthenticationSession, local page redirecting to the callback scheme): before the fix, a sign-in started on the default instance reached the isolated one ("State token mismatch"); after, each sign-in reaches its starter, no failures on the other instance.
  • Android emulator (Pixel 9, Android 16, IL2CPP, Android Keystore storage), test app Assets/DualSessionDeviceTest: guest on default, existing email account on isolated instance returned the existing wallet, adapter stayed on guest, separate stored keys, 0-value tx guest → email wallet, both sessions restored after force-stop, guest sign-out, promotion (default key holds email wallet, isolated key cleared), promoted account restored after force-stop, sign-out.
  • EditMode tests (DeepLinkHandlerTests, SequenceLoginTests, WaaSWalletUnitTests, InitiateEmailSignInUnitTests, WaaSToWalletAdapterUnitTests, IntentSenderTests, JwtHelperTests): all 70 existing tests have the same per-test result as before the change (22 pass; failures are environment/config). 3 new DeepLinkHandlerTests for two authenticators pass. TestStateTokenMismatch now uses a fresh state: the fixed "123456" it used is issued by another test's authenticator.

Not covered

  • Asset transfer logic (balances, ERC-20/721/1155, fees, retry reconciliation) is app code; only zero-value native txs were sent here.
  • AccountAlreadyLinked path (federating an email that already has a wallet into the guest) and the new-user federation path were not re-run; the default instance's federation code is unchanged.
  • iOS Keychain (iOS run was routing only; Android Keystore covered on emulator).
  • Real Google / Apple / Facebook / Discord sign-in on device; Android and Windows routing tested in the editor only.

Integration notes (TapNation, 0xsequence/issue-tracker#7344)

Flow: try federating the email into the guest; on conflict, authenticate the existing account on SequenceLogin.CreateIsolatedInstance("user"), transfer from the guest to the wallet that authentication returned, guest.DropThisSession(), then PromoteToDefaultSession(user).

  • Second email code: email federation verifies the code before it checks for an existing account, then returns AccountAlreadyLinked (7001) through SequenceWallet.OnAccountFederationFailed as a string. The code is bound to the guest's session (email;sessionID verifier), so the isolated instance needs a new code. Every returning player gets two codes if federation is always tried first. Using the email→wallet table only to choose the path (not to authorize the destination) limits two codes to missing or stale table entries. Removing it entirely needs a backend change.
  • Before DropThisSession(): check the result is SuccessfulTransactionReturn and its receipt.status (SendTransaction waits for the receipt by default).
  • Always drop the guest session after the switch; sessions have a 100-year TTL.
  • Cancelled or failed authentication: call ClearStoredSession() on the isolated instance. An isolated session that restores at startup means a migration was interrupted: re-read guest balances and send only what remains.
  • The isolated instance raises its own OnSessionWalletCreated / OnSessionRecoveryFailed; the adapter and static SequenceWallet events follow the default session until promotion.
  • Create one isolated instance and reuse it; each instance subscribes to deep link events for its lifetime.
  • Upgrade from 4.6.7: the embedded-wallet API is unchanged; boilerplate UI and HttpClient were renamed/moved in 5.x.

Version Increment

Please ensure you have incremented the package version in the package.json as necessary.

  • I have incremented the package.json according to semantic versioning
  • No version increment is needed; the change does not impact SDK or Sample code/assets

Docs Checklist

Please ensure you have addressed documentation updates if needed as part of this PR:

  • I have created a separate PR on the sequence docs repository for documentation updates: Link to docs PR
  • No documentation update is needed for this change.

Adds SequenceLogin.CreateIsolatedInstance(namespace) so a second wallet
session (e.g. an existing email account) can be authenticated while the
default instance keeps the current guest session.

- Isolated instances store their session under a namespaced secure
  storage key, raise per-instance OnSessionWalletCreated /
  OnSessionRecoveryFailed instead of the static SequenceWallet events,
  skip auto-federation and do not write the last-login PlayerPrefs.
- PromoteToDefaultSession(wallet) moves the session to the default key,
  clears federation mode and raises SequenceWallet.OnWalletCreated so
  EmbeddedWalletAdapter / SessionManager switch to it.
- ClearStoredSession() removes an instance's stored session.
- SequenceWallet keeps its own session key and owning login; on account
  federation it persists its own key instead of the singleton's current
  session key.
- Default GetInstance() behaviour is unchanged.

Adds a batchmode verification harness (Assets/Editor/DualSessionVerification)
that runs the guest + second-account flow across separate editor processes.
@tolgahan-arikan
tolgahan-arikan requested a review from a team November 5, 2026 15:32
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Deploying sequence-unity with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0eb4301
Status: ✅  Deploy successful!
Preview URL: https://ea231f73.sequence-unity.pages.dev
Branch Preview URL: https://feature-dual-session-support.sequence-unity.pages.dev

View logs

With an isolated SequenceLogin alongside the default one there are two
OpenIdAuthenticators, and sign-in callbacks reached the wrong one:

- iOS: IosBrowser was a process-wide singleton bound to the most recently
  created authenticator, so a sign-in started on the default instance was
  delivered to the isolated one and failed with "State token mismatch".
- Android/macOS/WebGL: every instance receives Application.deepLinkActivated;
  the instance that did not start the sign-in raised OnLoginFailed.
- Windows: the IPC listener only runs on the first authenticator, so links for
  the isolated instance were handed to the default one.

Authenticators now register the state token they issue. A link carrying another
authenticator's state is ignored; single-receiver callbacks (Windows IPC, iOS
authentication session) go through OpenIdAuthenticator.RouteDeepLink to the
issuer. IosBrowser is per authenticator, with the native callback going to the
browser that started the session. Links whose state nobody issued fail as before.
DualSessionDeviceTest: guest on the default SequenceLogin, existing account
(Google or email) on an isolated instance, transfer, sign-out, promotion, with
session restore on every launch. Shows both instances, the adapter wallet,
both stored keys and every login failure from either instance.
BuildDualSessionDeviceTest.Android builds it as an APK (run in a project copy:
it writes keys into the config and switches to debug signing).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants