-
Notifications
You must be signed in to change notification settings - Fork 125
Add EphemeralBaseAccountProvider for isolated payment flows #188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
spencerstock
wants to merge
5
commits into
master
Choose a base branch
from
spencer/ephemeral-provider-isolation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
84a8f20
Add EphemeralBaseAccountProvider and EphemeralSigner for isolated pay…
spencerstock a2c8486
fix: separate telemetry initialization to allow later SDK instances t…
spencerstock 794b951
felix feedback
spencerstock eabcfc4
add isEphemeral to analytic events
spencerstock 1e69f66
fix ci
spencerstock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
packages/account-sdk/src/interface/builder/core/EphemeralBaseAccountProvider.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import { Communicator } from ':core/communicator/Communicator.js'; | ||
| import { CB_WALLET_RPC_URL } from ':core/constants.js'; | ||
| import { standardErrorCodes } from ':core/error/constants.js'; | ||
| import { standardErrors } from ':core/error/errors.js'; | ||
| import { serializeError } from ':core/error/serialize.js'; | ||
| import { | ||
| ConstructorOptions, | ||
| ProviderEventEmitter, | ||
| ProviderInterface, | ||
| RequestArguments, | ||
| } from ':core/provider/interface.js'; | ||
| import { | ||
| logRequestError, | ||
| logRequestResponded, | ||
| logRequestStarted, | ||
| } from ':core/telemetry/events/provider.js'; | ||
| import { parseErrorMessageFromAny } from ':core/telemetry/utils.js'; | ||
| import { hexStringFromNumber } from ':core/type/util.js'; | ||
| import { EphemeralSigner } from ':sign/base-account/EphemeralSigner.js'; | ||
| import { correlationIds } from ':store/correlation-ids/store.js'; | ||
| import { createStoreInstance, type StoreInstance } from ':store/store.js'; | ||
| import { fetchRPCRequest } from ':util/provider.js'; | ||
|
|
||
| /** | ||
| * EphemeralBaseAccountProvider is a provider designed for single-use payment flows. | ||
| * | ||
| * Key differences from BaseAccountProvider: | ||
| * 1. Creates its own isolated store instance (no persistence, no global state pollution) | ||
| * 2. Uses EphemeralSigner with the isolated store to prevent concurrent operation interference | ||
| * 3. Cleanup clears the entire ephemeral store instance | ||
| * 4. Optimized for one-shot operations like pay() and subscribe() | ||
| * | ||
| * This prevents: | ||
| * - Race conditions when multiple ephemeral payment flows run concurrently | ||
| * - KeyManager interference (each instance has its own isolated keys) | ||
| * - Memory leaks (store instance is garbage collected after cleanup) | ||
| */ | ||
| export class EphemeralBaseAccountProvider | ||
| extends ProviderEventEmitter | ||
| implements ProviderInterface | ||
| { | ||
| private readonly communicator: Communicator; | ||
| private readonly signer: EphemeralSigner; | ||
| private readonly ephemeralStore: StoreInstance; | ||
|
|
||
| constructor({ | ||
| metadata, | ||
| preference: { walletUrl, ...preference }, | ||
| }: Readonly<ConstructorOptions>) { | ||
| super(); | ||
| this.communicator = new Communicator({ | ||
| url: walletUrl, | ||
| metadata, | ||
| preference, | ||
| }); | ||
| // Create an isolated ephemeral store for this provider instance | ||
| // persist: false means no localStorage persistence | ||
| this.ephemeralStore = createStoreInstance({ persist: false }); | ||
|
|
||
| this.signer = new EphemeralSigner({ | ||
| metadata, | ||
| communicator: this.communicator, | ||
| callback: this.emit.bind(this), | ||
| storeInstance: this.ephemeralStore, | ||
| }); | ||
| } | ||
|
|
||
| public async request<T>(args: RequestArguments): Promise<T> { | ||
| // correlation id across the entire request lifecycle | ||
| const correlationId = crypto.randomUUID(); | ||
| correlationIds.set(args, correlationId); | ||
| logRequestStarted({ method: args.method, correlationId }); | ||
|
|
||
| try { | ||
| const result = await this._request(args); | ||
| logRequestResponded({ | ||
| method: args.method, | ||
| correlationId, | ||
| }); | ||
| return result as T; | ||
| } catch (error) { | ||
| logRequestError({ | ||
| method: args.method, | ||
| correlationId, | ||
| errorMessage: parseErrorMessageFromAny(error), | ||
| }); | ||
| throw error; | ||
| } finally { | ||
| correlationIds.delete(args); | ||
| } | ||
| } | ||
|
|
||
| private async _request<T>(args: RequestArguments): Promise<T> { | ||
| try { | ||
| // For ephemeral providers, we only support a subset of methods | ||
| // that are needed for payment flows | ||
| switch (args.method) { | ||
| case 'wallet_sendCalls': | ||
| case 'wallet_sign': { | ||
| try { | ||
| await this.signer.handshake({ method: 'handshake' }); // exchange session keys | ||
| const result = await this.signer.request(args); // send diffie-hellman encrypted request | ||
| return result as T; | ||
| } finally { | ||
| await this.signer.cleanup(); // clean up (rotate) the ephemeral session keys | ||
| } | ||
| } | ||
| case 'wallet_getCallsStatus': { | ||
| const result = await fetchRPCRequest(args, CB_WALLET_RPC_URL); | ||
| return result as T; | ||
| } | ||
| case 'eth_accounts': { | ||
| return [] as T; | ||
| } | ||
| case 'net_version': { | ||
| const result = 1 as T; // default value | ||
| return result; | ||
| } | ||
| case 'eth_chainId': { | ||
| const result = hexStringFromNumber(1) as T; // default value | ||
| return result; | ||
| } | ||
| default: { | ||
| throw standardErrors.provider.unauthorized( | ||
| `Method '${args.method}' is not supported by ephemeral provider. Ephemeral providers only support: wallet_sendCalls, wallet_sign, wallet_getCallsStatus` | ||
| ); | ||
| } | ||
| } | ||
| } catch (error) { | ||
| const { code } = error as { code?: number }; | ||
| if (code === standardErrorCodes.provider.unauthorized) { | ||
| await this.disconnect(); | ||
| } | ||
| return Promise.reject(serializeError(error)); | ||
| } | ||
| } | ||
|
|
||
| async disconnect() { | ||
| // Cleanup ephemeral signer state and its isolated store | ||
| await this.signer.cleanup(); | ||
| // Note: The ephemeral store instance will be garbage collected | ||
| // when this provider instance is no longer referenced | ||
| this.emit('disconnect', standardErrors.provider.disconnected('User initiated disconnection')); | ||
| } | ||
|
|
||
| readonly isBaseAccount = true; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we get test coverage for this file?