-
Notifications
You must be signed in to change notification settings - Fork 44
refactor: align controller package with StarkNet wallet API spec #2053
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ import { | |
| ProfileContextTypeVariant, | ||
| ResponseCodes, | ||
| StarterPack, | ||
| createUserRejectedError, | ||
| } from "./types"; | ||
| import { parseChainId } from "./utils"; | ||
|
|
||
|
|
@@ -32,6 +33,7 @@ export default class ControllerProvider extends BaseProvider { | |
| private iframes: IFrames; | ||
| private selectedChain: ChainId; | ||
| private chains: Map<ChainId, Chain>; | ||
| private pendingConnectRejection?: (error: any) => void; | ||
|
|
||
| isReady(): boolean { | ||
| return !!this.keychain; | ||
|
|
@@ -164,8 +166,11 @@ export default class ControllerProvider extends BaseProvider { | |
|
|
||
| this.iframes.keychain.open(); | ||
|
|
||
| try { | ||
| let response = await this.keychain.connect( | ||
| return new Promise<WalletAccount | undefined>((resolve, reject) => { | ||
| // Track this pending connection for cancellation | ||
| this.pendingConnectRejection = reject; | ||
|
|
||
| this.keychain!.connect( | ||
| // Policy precedence logic: | ||
| // 1. If shouldOverridePresetPolicies is true and policies are provided, use policies | ||
| // 2. Otherwise, if preset is defined, use empty object (let preset take precedence) | ||
|
|
@@ -177,27 +182,38 @@ export default class ControllerProvider extends BaseProvider { | |
| : this.options.policies || {}, | ||
| this.rpcUrl(), | ||
| this.options.signupOptions, | ||
| ); | ||
| if (response.code !== ResponseCodes.SUCCESS) { | ||
| throw new Error(response.message); | ||
| } | ||
|
|
||
| response = response as ConnectReply; | ||
| this.account = new ControllerAccount( | ||
| this, | ||
| this.rpcUrl(), | ||
| response.address, | ||
| this.keychain, | ||
| this.options, | ||
| this.iframes.keychain, | ||
| ); | ||
| ) | ||
| .then((response) => { | ||
| // Clear the pending rejection handler | ||
| this.pendingConnectRejection = undefined; | ||
|
|
||
| if (response.code !== ResponseCodes.SUCCESS) { | ||
| this.iframes.keychain!.close(); | ||
| reject(new Error(response.message)); | ||
| return; | ||
| } | ||
|
|
||
| const connectReply = response as ConnectReply; | ||
| this.account = new ControllerAccount( | ||
| this, | ||
| this.rpcUrl(), | ||
| connectReply.address, | ||
| this.keychain!, | ||
| this.options, | ||
| this.iframes.keychain!, | ||
| ); | ||
|
|
||
| return this.account; | ||
| } catch (e) { | ||
| console.log(e); | ||
| } finally { | ||
| this.iframes.keychain.close(); | ||
| } | ||
| this.iframes.keychain!.close(); | ||
| resolve(this.account); | ||
| }) | ||
| .catch((error) => { | ||
| // Clear the pending rejection handler | ||
| this.pendingConnectRejection = undefined; | ||
| this.iframes.keychain!.close(); | ||
| console.log(error); | ||
| reject(error); | ||
| }); | ||
| }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Promise Rejection and Cancellation Race ConditionThe |
||
| } | ||
|
|
||
| async switchStarknetChain(chainId: string): Promise<boolean> { | ||
|
|
@@ -443,6 +459,13 @@ export default class ControllerProvider extends BaseProvider { | |
| return new KeychainIFrame({ | ||
| ...this.options, | ||
| onClose: this.keychain?.reset, | ||
| onCancel: () => { | ||
| // User cancelled by clicking outside - reject pending operations | ||
| if (this.pendingConnectRejection) { | ||
| this.pendingConnectRejection(createUserRejectedError()); | ||
| this.pendingConnectRejection = undefined; | ||
| } | ||
| }, | ||
| onConnect: (keychain) => { | ||
| this.keychain = keychain; | ||
| }, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ export class IFrame<CallSender extends {}> implements Modal { | |
| private iframe?: HTMLIFrameElement; | ||
| private container?: HTMLDivElement; | ||
| private onClose?: () => void; | ||
| private onCancel?: () => void; | ||
| private child?: AsyncMethodReturns<CallSender>; | ||
| private closeTimeout?: NodeJS.Timeout; | ||
|
|
||
|
|
@@ -22,12 +23,14 @@ export class IFrame<CallSender extends {}> implements Modal { | |
| url, | ||
| preset, | ||
| onClose, | ||
| onCancel, | ||
| onConnect, | ||
| methods = {}, | ||
| }: Pick<ControllerOptions, "preset"> & { | ||
| id: string; | ||
| url: URL; | ||
| onClose?: () => void; | ||
| onCancel?: () => void; | ||
| onConnect: (child: AsyncMethodReturns<CallSender>) => void; | ||
| methods?: { [key: string]: (...args: any[]) => void }; | ||
| }) { | ||
|
|
@@ -76,13 +79,16 @@ export class IFrame<CallSender extends {}> implements Modal { | |
| // Add click event listener to close iframe when clicking outside | ||
| container.addEventListener("click", (e) => { | ||
| if (e.target === container) { | ||
| // User clicked outside - this is a cancellation | ||
| // Attempting to reset(clear context) for keychain iframe (identified by ID) | ||
| if (id === "controller-keychain" && this.child) { | ||
| // Type assertion for keychain child only | ||
| (this.child as any) | ||
| .reset?.() | ||
| .catch((e: any) => console.error("Error resetting context:", e)); | ||
| } | ||
| // Call onCancel to notify about user cancellation | ||
| this.onCancel?.(); | ||
| this.close(); | ||
| } | ||
| }); | ||
|
|
@@ -129,6 +135,7 @@ export class IFrame<CallSender extends {}> implements Modal { | |
| } | ||
|
|
||
| this.onClose = onClose; | ||
| this.onCancel = onCancel; | ||
| } | ||
|
|
||
| open() { | ||
|
|
@@ -201,4 +208,24 @@ export class IFrame<CallSender extends {}> implements Modal { | |
| isOpen() { | ||
| return this.container?.style.display !== "none"; | ||
| } | ||
|
|
||
| // Register a one-time cancellation handler for the current operation | ||
| withCancellation<T>(operation: Promise<T>, onCancel: () => void): Promise<T> { | ||
| // Store the original onCancel | ||
| const originalOnCancel = this.onCancel; | ||
|
|
||
| // Set a one-time handler that includes the provided callback | ||
| this.onCancel = () => { | ||
| onCancel(); | ||
| // Restore the original handler | ||
| this.onCancel = originalOnCancel; | ||
| // Call the original if it exists | ||
| originalOnCancel?.(); | ||
| }; | ||
|
|
||
| // When the operation completes (success or failure), restore the original handler | ||
| return operation.finally(() => { | ||
| this.onCancel = originalOnCancel; | ||
| }); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Concurrent Operations Overwrite Cancellation HandlersThe |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.