-
Notifications
You must be signed in to change notification settings - Fork 26
fix: miscellaneous bugs wrt stack upgrades and Jrpc V2 #390
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
30b693b
fix: fixed Ed25519 Key gen
lwin-kyaw 40925ed
fix: fixed PostMessageStream._postMessage override
lwin-kyaw 48df2e1
feat: Stream Middleware V2
lwin-kyaw aef8f4f
fix: addIdtoken from storage in user info
arch1995 b036c9c
chore: minor types updates
lwin-kyaw 6bb283f
Merge remote-tracking branch 'origin/fix/misc-bugs' into fix/misc-bugs
lwin-kyaw b9161ad
feat: updated demo
lwin-kyaw b3b1a90
feat: updated exports and fix postMessageStream
lwin-kyaw b2b7b80
fix: fixed duplex import
lwin-kyaw 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,69 @@ | ||
| import { Duplex } from "readable-stream"; | ||
|
|
||
| import { JRPCRequest, Json } from "../interfaces"; | ||
| import { SafeEventEmitter } from "../safeEventEmitter"; | ||
| import { JRPCMiddlewareV2 } from "./v2interfaces"; | ||
|
|
||
| /** | ||
| * Creates a V2-compatible client-side stream middleware for the dapp ↔ iframe | ||
| * transport layer. | ||
| * | ||
| * Replaces V1's `createStreamMiddleware` by providing: | ||
| * - A terminal middleware that sends outbound requests through the stream and | ||
| * resolves when the matching response arrives. | ||
| * - A Duplex object stream to pump through the ObjectMultiplex channel. | ||
| * - Inbound notification routing via the supplied `notificationEmitter`. | ||
| */ | ||
| export function createClientStreamMiddlewareV2({ notificationEmitter }: { notificationEmitter?: SafeEventEmitter } = {}): { | ||
| middleware: JRPCMiddlewareV2<JRPCRequest<unknown>, Json>; | ||
| stream: Duplex; | ||
| } { | ||
| const pendingRequests = new Map<number | string, { resolve: (result: Json) => void; reject: (error: unknown) => void }>(); | ||
|
|
||
| function noop() { | ||
| // noop | ||
| } | ||
|
|
||
| function write(this: Duplex, data: Record<string, unknown>, _encoding: BufferEncoding, cb: () => void) { | ||
| if (data.method !== undefined) { | ||
| // Inbound request or notification from remote — route to event emitter | ||
| // (matches V1 createStreamMiddleware behavior where all non-response | ||
| // messages are emitted as "notification" regardless of whether they | ||
| // carry an id) | ||
| notificationEmitter?.emit("notification", data); | ||
| } else { | ||
| // No method → this is a response to one of our pending outbound requests | ||
| const id = data.id as number | string | undefined; | ||
| if (id !== undefined && id !== null && pendingRequests.has(id)) { | ||
| const pending = pendingRequests.get(id)!; | ||
| pendingRequests.delete(id); | ||
|
|
||
| if (data.error) { | ||
| const errorObj = data.error as { code?: number; message?: string; data?: unknown }; | ||
| pending.reject(Object.assign(new Error(errorObj.message || "Internal JSON-RPC error"), { code: errorObj.code, data: errorObj.data })); | ||
| } else { | ||
| pending.resolve(data.result as Json); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| cb(); | ||
| } | ||
|
|
||
| const stream = new Duplex({ objectMode: true, read: noop, write }); | ||
|
|
||
| stream.once("close", () => { | ||
| const error = new Error("Stream closed"); | ||
| pendingRequests.forEach(({ reject }) => reject(error)); | ||
| pendingRequests.clear(); | ||
| }); | ||
|
|
||
| const middleware: JRPCMiddlewareV2<JRPCRequest<unknown>, Json> = ({ request }) => { | ||
| return new Promise<Json>((resolve, reject) => { | ||
| pendingRequests.set(request.id as number | string, { resolve, reject }); | ||
| stream.push(request); | ||
| }); | ||
| }; | ||
|
|
||
| return { middleware, stream }; | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| type PostMessagePayload = { | ||
| data: { | ||
| params: Array<Record<string, unknown>>; | ||
| }; | ||
| }; | ||
|
|
||
| describe("PostMessageStream", () => { | ||
| let targetWindow: { postMessage: ReturnType<typeof vi.fn> }; | ||
|
|
||
| async function createStream() { | ||
| const { PostMessageStream } = await import("../src/jrpc/postMessageStream"); | ||
| const stream = new PostMessageStream({ | ||
| name: "provider", | ||
| target: "auth", | ||
| targetWindow: targetWindow as unknown as Window, | ||
| }); | ||
|
|
||
| targetWindow.postMessage.mockClear(); | ||
|
|
||
| return stream; | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| vi.resetModules(); | ||
| targetWindow = { postMessage: vi.fn() }; | ||
|
|
||
| vi.stubGlobal("window", { | ||
| addEventListener: vi.fn(), | ||
| removeEventListener: vi.fn(), | ||
| location: { origin: "https://current.example" }, | ||
| postMessage: vi.fn(), | ||
| }); | ||
| vi.stubGlobal("MessageEvent", class MessageEventMock {}); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| it("writes the current origin into the posted payload params", async () => { | ||
| const stream = await createStream(); | ||
| const payload: PostMessagePayload = { data: { params: [{}] } }; | ||
|
|
||
| (stream as unknown as { _postMessage: (data: unknown) => void })._postMessage(payload); | ||
|
|
||
| expect(targetWindow.postMessage).toHaveBeenCalledOnce(); | ||
| const [message, originConstraint] = targetWindow.postMessage.mock.calls[0] as [{ target: string; data: PostMessagePayload }, string]; | ||
|
|
||
| expect(originConstraint).toBe("*"); | ||
| expect(message.target).toBe("auth"); | ||
| expect(message.data.data.params[0]._origin).toBe("https://current.example"); | ||
| // the original payload should not be mutated | ||
| expect(payload.data.params[0]._origin).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("uses the previous _origin as the postMessage origin constraint before rewriting it", async () => { | ||
| const stream = await createStream(); | ||
| const payload: PostMessagePayload = { | ||
| data: { | ||
| params: [{ _origin: "https://allowed.example" }], | ||
| }, | ||
| }; | ||
|
|
||
| (stream as unknown as { _postMessage: (data: unknown) => void })._postMessage(payload); | ||
|
|
||
| expect(targetWindow.postMessage).toHaveBeenCalledOnce(); | ||
| const [message, originConstraint] = targetWindow.postMessage.mock.calls[0] as [{ target: string; data: PostMessagePayload }, string]; | ||
|
|
||
| expect(originConstraint).toBe("https://allowed.example"); | ||
| expect(message.target).toBe("auth"); | ||
| expect(message.data.data.params[0]._origin).toBe("https://current.example"); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.