-
Notifications
You must be signed in to change notification settings - Fork 35
refactor(wallets): extract shared quorum member matching (M4-4 part 1/4) #1997
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
panosinthezone
wants to merge
1
commit into
panos/wal-11291-m4-3-sdk-teach-the-approval-loop-the-nested-quorumapprovals
Choose a base branch
from
panos/wal-11292-m4-4a-quorum-member-matching-util
base: panos/wal-11291-m4-3-sdk-teach-the-approval-loop-the-nested-quorumapprovals
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import type { ResolvedQuorumMember } from "../signers/types"; | ||
| import { getQuorumMemberLocator, matchesQuorumMember } from "./quorum-members"; | ||
|
|
||
| const NO_SERVER_ADDRESSES = () => [] as string[]; | ||
|
|
||
| describe("getQuorumMemberLocator", () => { | ||
| it.each([ | ||
| [ | ||
| "an API-sourced locator", | ||
| { type: "email", email: "other@x.com", locator: "email:alice@gmail.com" }, | ||
| "email:alice@gmail.com", | ||
| ], | ||
| ["a server address fallback", { type: "server", address: "0xServer" }, "server:0xServer"], | ||
| [ | ||
| "an email fallback with normalization", | ||
| { type: "email", email: "A.l.i.c.e@GMAIL.com" }, | ||
| "email:alice@gmail.com", | ||
| ], | ||
| ["an external-wallet fallback", { type: "external-wallet", address: "0xExt" }, "external-wallet:0xExt"], | ||
| ])("uses %s", (_name, member, expected) => { | ||
| expect(getQuorumMemberLocator(member as ResolvedQuorumMember)).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| describe("matchesQuorumMember", () => { | ||
| it("rejects a candidate of a different type", () => { | ||
| expect( | ||
| matchesQuorumMember( | ||
| { type: "email", email: "alice@gmail.com" }, | ||
| { type: "phone", phone: "+15551234567" }, | ||
| NO_SERVER_ADDRESSES | ||
| ) | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| describe("when the member is an email", () => { | ||
| const member: ResolvedQuorumMember = { type: "email", email: "alice@gmail.com" }; | ||
|
|
||
| it.each([ | ||
| ["an exact address", "alice@gmail.com", true], | ||
| ["a denormalized Gmail address", "A.l.i.c.e@GMAIL.com", true], | ||
| ["a different address", "mallory@gmail.com", false], | ||
| ])("compares normalized emails: %s", (_name, email, expected) => { | ||
| expect(matchesQuorumMember({ type: "email", email }, member, NO_SERVER_ADDRESSES)).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| describe("when the member is a phone number", () => { | ||
| it.each([ | ||
| ["the same number", "+15551234567", true], | ||
| ["a different number", "+15550000000", false], | ||
| ])("compares exactly: %s", (_name, phone, expected) => { | ||
| expect( | ||
| matchesQuorumMember( | ||
| { type: "phone", phone }, | ||
| { type: "phone", phone: "+15551234567" }, | ||
| NO_SERVER_ADDRESSES | ||
| ) | ||
| ).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| describe("when the member is an external wallet", () => { | ||
| it.each([ | ||
| ["the same address", "0xAbC", true], | ||
| ["a different address", "0xDef", false], | ||
| ])("compares addresses exactly: %s", (_name, address, expected) => { | ||
| expect( | ||
| matchesQuorumMember( | ||
| { type: "external-wallet", address }, | ||
| { type: "external-wallet", address: "0xAbC" }, | ||
| NO_SERVER_ADDRESSES | ||
| ) | ||
| ).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| describe("when the member is a passkey", () => { | ||
| const member: ResolvedQuorumMember = { type: "passkey", id: "pk-1", name: "primary" }; | ||
|
|
||
| it.each([ | ||
| ["a matching id", { id: "pk-1" }, true], | ||
| ["a mismatching id even when the name matches", { id: "pk-9", name: "primary" }, false], | ||
| ["a matching name when no id is given", { name: "primary" }, true], | ||
| ["a mismatching name", { name: "backup" }, false], | ||
| ["no id and no name (permissive)", {}, true], | ||
| ])("matches by id, then name, then permissively: %s", (_name, fields, expected) => { | ||
| expect(matchesQuorumMember({ type: "passkey", ...fields }, member, NO_SERVER_ADDRESSES)).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| describe("when the member is a server signer", () => { | ||
| const member: ResolvedQuorumMember = { type: "server", address: "0xDerived" }; | ||
|
|
||
| it("matches when any derivable candidate address equals the member address", () => { | ||
| const candidates = vi.fn(() => ["0xPrimary", "0xDerived"]); | ||
| expect(matchesQuorumMember({ type: "server", secret: "s3cret" }, member, candidates)).toBe(true); | ||
| expect(candidates).toHaveBeenCalledWith({ type: "server", secret: "s3cret" }); | ||
| }); | ||
|
|
||
| it("rejects when no derivable candidate address matches", () => { | ||
| expect(matchesQuorumMember({ type: "server", secret: "s3cret" }, member, () => ["0xOther"])).toBe(false); | ||
| }); | ||
|
|
||
| it("rejects a member without an API address", () => { | ||
| expect( | ||
| matchesQuorumMember({ type: "server", secret: "s3cret" }, { type: "server" }, () => ["0xDerived"]) | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| it("rejects a candidate without a secret", () => { | ||
| const candidates = vi.fn(() => ["0xDerived"]); | ||
| expect(matchesQuorumMember({ type: "server", address: "0xDerived" }, member, candidates)).toBe(false); | ||
| expect(candidates).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| }); |
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,66 @@ | ||
| import type { Chain } from "../chains/chains"; | ||
| import type { ResolvedQuorumMember, ServerSignerConfig, SignerConfigForChain } from "../signers/types"; | ||
| import { getSignerLocator } from "./signer-locator"; | ||
| import { normalizeEmail } from "./signer-validation"; | ||
|
|
||
| /** | ||
| * Locator of a resolved quorum member. API-sourced members carry their locator; for members | ||
| * that don't (e.g. caller-supplied configs on the no-API-config fallback path), fall back to | ||
| * deriving one from the config fields. | ||
| */ | ||
| export function getQuorumMemberLocator(member: ResolvedQuorumMember): string { | ||
| if (typeof member.locator === "string") { | ||
| return member.locator; | ||
| } | ||
|
Comment on lines
+12
to
+14
Contributor
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. How can it not have a locator? |
||
| if (member.type === "server" && member.address != null) { | ||
| return `server:${member.address}`; | ||
| } | ||
| return getSignerLocator(member as SignerConfigForChain<Chain>) as string; | ||
| } | ||
|
|
||
| /** | ||
| * Whether a caller-supplied signer config identifies the given quorum member. | ||
| * | ||
| * Matching is per-type, mirroring the existing-wallet quorum validation: | ||
| * - server: the candidate's derivable addresses (primary and legacy) must include the member's | ||
| * API address — secrets are never compared. Derivation is injected so callers can plug their | ||
| * own provider (factory: fresh derivation; wallet: `ServerSignerResolver.candidateAddresses`). | ||
| * - passkey: by `id` when given, else by `name`, else permissive (callers layer count-based | ||
| * disambiguation on top). | ||
| * - email: normalized comparison; phone: exact; external-wallet: exact address. | ||
| * Null-tolerant on candidate fields to preserve the factory's permissive semantics. | ||
| */ | ||
| export function matchesQuorumMember( | ||
| candidate: { type: string } & Record<string, unknown>, | ||
| member: ResolvedQuorumMember, | ||
| serverCandidateAddresses: (config: ServerSignerConfig) => string[] | ||
| ): boolean { | ||
| if (candidate.type !== member.type) { | ||
| return false; | ||
| } | ||
| if (candidate.type === "server") { | ||
| if (member.address == null || typeof (candidate as { secret?: unknown }).secret !== "string") { | ||
| return false; | ||
| } | ||
| return serverCandidateAddresses(candidate as unknown as ServerSignerConfig).includes(member.address); | ||
| } | ||
| if (candidate.type === "passkey") { | ||
| if (candidate.id != null) { | ||
| return member.id === candidate.id; | ||
| } | ||
| if (candidate.name != null) { | ||
| return member.name === candidate.name; | ||
| } | ||
| return true; | ||
| } | ||
| if (candidate.type === "email") { | ||
| return ( | ||
| candidate.email == null || | ||
| normalizeEmail(String(candidate.email)) === normalizeEmail(String(member.email ?? "")) | ||
| ); | ||
| } | ||
| if (candidate.type === "phone") { | ||
| return candidate.phone == null || candidate.phone === member.phone; | ||
| } | ||
| return candidate.address === member.address; | ||
| } | ||
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.
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.
Shouldn't this be a discriminated union?