Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib/libraries/intentFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function toCoreCreateIntentOptions(opts: AppCreateIntentOptions): CreateIntentOp
outputTokens: opts.outputTokens.map(toCoreTokenContext),
verifier: opts.verifier,
account,
outputRecipient: opts.outputRecipient,
lock: {
type: "compact",
resetPeriod: opts.lock.resetPeriod,
Expand All @@ -73,6 +74,7 @@ function toCoreCreateIntentOptions(opts: AppCreateIntentOptions): CreateIntentOp
outputTokens: opts.outputTokens.map(toCoreTokenContext),
verifier: opts.verifier,
account,
outputRecipient: opts.outputRecipient,
lock: {
type: "escrow"
}
Expand Down
27 changes: 18 additions & 9 deletions src/lib/screens/IssueIntent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@
const resolveExclusiveFor = (value: string): `0x${string}` | undefined =>
isAddress(value, { strict: false }) ? value : undefined;

const resolveRecipient = (value: string): `0x${string}` | undefined =>
isAddress(value, { strict: false }) ? value : undefined;

const intentOptions = $derived.by(
(): AppCreateIntentOptions => ({
exclusiveFor: resolveExclusiveFor(store.exclusiveFor),
inputTokens: store.inputTokens,
outputTokens: store.outputTokens,
verifier: store.verifier,
outputRecipient: resolveRecipient(store.recipient),
lock:
store.intentType === "compact"
? {
Expand Down Expand Up @@ -276,6 +280,19 @@

<SectionCard compact>
<div class="flex flex-col gap-2">
<div class="flex min-w-0 items-center gap-1">
<span class="text-[11px] font-semibold whitespace-nowrap text-gray-500">Recipient</span>
<FormControl
type="text"
size="sm"
className="flex-1"
placeholder="0x... (optional)"
state={store.recipient.length > 0 && !resolveRecipient(store.recipient)
? "error"
: "default"}
bind:value={store.recipient}
/>
</div>
<div class="flex items-center gap-1">
<span class="text-[11px] font-semibold text-gray-500">Verifier</span>
{#if sameChain}
Expand Down Expand Up @@ -313,15 +330,7 @@
</SectionCard>

<div class="mt-2 flex justify-center">
{#if !true}
<button
type="button"
class="h-8 rounded border border-gray-200 bg-white px-3 text-sm font-semibold text-gray-400"
disabled
>
Input must be exactly raw 100 USDC
</button>
{:else if !allowanceCheck}
{#if !allowanceCheck}
<AwaitButton buttonFunction={approveFunction}>
{#snippet name()}
Set allowance
Expand Down
1 change: 1 addition & 0 deletions src/lib/state.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ class Store {
allocatorId = $state<availableAllocators>(ALWAYS_OK_ALLOCATOR);
verifier = $state<Verifier>("polymer");
exclusiveFor: string = $state("");
recipient: string = $state("");
useExclusiveForQuoteRequest = $state(false);

invalidateWalletReadCache(scope: "all" | "balance" | "allowance" | "compact" = "all") {
Expand Down
54 changes: 54 additions & 0 deletions tests/unit/recipientField.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { describe, expect, it } from "bun:test";
import { isAddress } from "viem";

// Mirrors the resolveRecipient helper in IssueIntent.svelte
const resolveRecipient = (value: string): `0x${string}` | undefined =>
isAddress(value, { strict: false }) ? (value as `0x${string}`) : undefined;

describe("resolveRecipient", () => {
it("returns the address for a valid checksummed EVM address", () => {
const addr = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
expect(resolveRecipient(addr)).toBe(addr);
});

it("returns the address for a valid lowercase EVM address (strict: false)", () => {
const addr = "0xd8da6bf26964af9d7eed9e03e53415d37aa96045";
expect(resolveRecipient(addr)).toBe(addr);
});

it("returns undefined for an empty string", () => {
expect(resolveRecipient("")).toBeUndefined();
});

it("returns undefined for a partial address", () => {
expect(resolveRecipient("0x1234")).toBeUndefined();
});

it("returns undefined for arbitrary non-address text", () => {
expect(resolveRecipient("alice.eth")).toBeUndefined();
});

it("returns undefined for a hex string that is too long", () => {
expect(resolveRecipient("0x" + "a".repeat(42))).toBeUndefined();
});
});

describe("outputRecipient in AppCreateIntentOptions", () => {
it("is undefined when recipient field is empty", () => {
const recipient = "";
const outputRecipient = resolveRecipient(recipient);
expect(outputRecipient).toBeUndefined();
});

it("is set when a valid address is provided", () => {
const recipient = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
const outputRecipient = resolveRecipient(recipient);
expect(outputRecipient).toBe(recipient);
});

it("is undefined for an invalid address, so wallet default is used", () => {
const recipient = "not-an-address";
const outputRecipient = resolveRecipient(recipient);
expect(outputRecipient).toBeUndefined();
});
});
Loading