Skip to content
Merged
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
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ solana-cli-version:
cargo-nightly:
cargo $(nightly) $(ARGS)

# zstd 0.13 is pinned by solana-account-decoder-client-types (via solana-rpc-client)
# Fixing requires zstd 0.14 / zstd-safe 8.x, which no Solana release depends on yet.
audit:
cargo audit $(ARGS)
cargo audit \
--ignore RUSTSEC-2026-0391 \
--ignore RUSTSEC-2026-0403 \
$(ARGS)

spellcheck:
cargo spellcheck --code 1 $(ARGS)
Expand Down
37 changes: 21 additions & 16 deletions clients/js/src/assertValidTransferSolDestination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,27 @@ import {

import { SYSTEM_PROGRAM_ADDRESS } from './generated';

/** Details of why a destination was rejected by {@link assertValidTransferSolDestination}. */
export type InvalidTransferSolDestinationDetails =
| {
reason: 'unexpected-owner';
/** The program that owns the destination account. */
owner: Address;
}
| { reason: 'unfunded-recipient' }
| { reason: 'off-curve' };

/** Distinguishes why a destination was rejected by {@link assertValidTransferSolDestination}. */
export type InvalidTransferSolDestinationReason = 'unexpected-owner' | 'unfunded-recipient' | 'off-curve';
export type InvalidTransferSolDestinationReason = InvalidTransferSolDestinationDetails['reason'];

/**
* Thrown by {@link assertValidTransferSolDestination} when a destination is rejected. `owner` is set only
* when `reason` is `'unexpected-owner'`.
*/
/** Thrown by {@link assertValidTransferSolDestination} when a destination is rejected. */
export class InvalidTransferSolDestinationError extends Error {
override readonly name = 'InvalidTransferSolDestinationError';

constructor(
message: string,
readonly destination: Address,
readonly reason: InvalidTransferSolDestinationReason,
readonly owner?: Address,
readonly details: InvalidTransferSolDestinationDetails,
) {
super(message);
}
Expand All @@ -46,7 +52,7 @@ export type AssertValidTransferSolDestinationConfig = FetchAccountConfig & {
/**
* Allow an off-curve (program-derived) destination, existing or not. Defaults to `true`, since
* program-controlled wallets such as multisigs are common, legitimate recipients; set to `false` to
* reject off-curve destinations until the recipient confirms they control them.
* reject off-curve destinations.
*/
allowOffCurve?: boolean;
/**
Expand All @@ -64,8 +70,8 @@ export type AssertValidTransferSolDestinationConfig = FetchAccountConfig & {
* default), e.g. an SPL token mint. SOL sent to such an account is typically unrecoverable.
* - `'unfunded-recipient'`: no account exists, which often means a mistyped address. Skipped when
* `allowUnfundedRecipient` is set.
* - `'off-curve'`: the address is off-curve, existing or not, and may be a smart wallet the recipient
* must confirm they control. Only checked when `allowOffCurve` is `false`.
* - `'off-curve'`: the address is off-curve, existing or not. Only checked when `allowOffCurve` is
* `false`.
*
* The owner is checked first, so an off-curve account owned by another program, such as an associated
* token account, reports `'unexpected-owner'`.
Expand All @@ -89,23 +95,22 @@ export async function assertValidTransferSolDestination(
throw new InvalidTransferSolDestinationError(
`Refusing to transfer SOL to ${destination}: it is owned by program ${account.programAddress}, not the expected program (${programOwner}).`,
destination,
'unexpected-owner',
account.programAddress,
{ reason: 'unexpected-owner', owner: account.programAddress },
);
}
} else if (!allowUnfundedRecipient) {
throw new InvalidTransferSolDestinationError(
`Refusing to transfer SOL to ${destination}: it has no account on-chain yet, which often means the address is mistyped. If you intend to fund a new account, pass { allowUnfundedRecipient: true }.`,
destination,
'unfunded-recipient',
{ reason: 'unfunded-recipient' },
);
}

if (isOffCurveAddress(destination) && !allowOffCurve) {
if (!allowOffCurve && isOffCurveAddress(destination)) {
throw new InvalidTransferSolDestinationError(
`Refusing to transfer SOL to ${destination}: it is an off-curve (program-derived) address, which no keypair controls. It may be a program-controlled account such as a multisig or smart wallet; once the recipient confirms they control it, pass { allowOffCurve: true }.`,
`Refusing to transfer SOL to ${destination}: it is an off-curve (program-derived) address, which no keypair controls. It may be a program-controlled account such as a multisig or smart wallet; to allow it, pass { allowOffCurve: true }.`,
destination,
'off-curve',
{ reason: 'off-curve' },
);
}
}
15 changes: 7 additions & 8 deletions clients/js/test/assertValidTransferSolDestination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ it('rejects an existing destination owned by another program', async () => {
await expect(promise).rejects.toMatchObject({
message: expect.stringContaining(`owned by program ${TOKEN_PROGRAM_ADDRESS}`),
destination,
reason: 'unexpected-owner',
owner: TOKEN_PROGRAM_ADDRESS,
details: { reason: 'unexpected-owner', owner: TOKEN_PROGRAM_ADDRESS },
});
});

Expand All @@ -85,7 +84,7 @@ it('rejects a System-Program-owned destination when programOwner expects another
// Then it is rejected when the Token program is the expected owner.
await expect(
assertValidTransferSolDestination(client.rpc, destination, { programOwner: TOKEN_PROGRAM_ADDRESS }),
).rejects.toMatchObject({ reason: 'unexpected-owner', owner: SYSTEM_PROGRAM_ADDRESS });
).rejects.toMatchObject({ details: { reason: 'unexpected-owner', owner: SYSTEM_PROGRAM_ADDRESS } });
});

it('accepts a HasAddress destination', async () => {
Expand All @@ -109,7 +108,7 @@ it('rejects a HasAddress destination owned by another program', async () => {
// Then it is rejected, reporting the wrapped address.
await expect(assertValidTransferSolDestination(client.rpc, destination)).rejects.toMatchObject({
destination: destination.address,
reason: 'unexpected-owner',
details: { reason: 'unexpected-owner' },
});
});

Expand All @@ -126,7 +125,7 @@ it('accepts a ProgramDerivedAddress destination', async () => {
await expect(assertValidTransferSolDestination(client.rpc, destination)).resolves.toBeUndefined();
await expect(
assertValidTransferSolDestination(client.rpc, destination, { allowOffCurve: false }),
).rejects.toMatchObject({ destination: destination[0], reason: 'off-curve' });
).rejects.toMatchObject({ destination: destination[0], details: { reason: 'off-curve' } });
});

it('rejects an unfunded recipient by default', async () => {
Expand All @@ -136,7 +135,7 @@ it('rejects an unfunded recipient by default', async () => {

// Then it is rejected as unfunded.
await expect(assertValidTransferSolDestination(client.rpc, destination)).rejects.toMatchObject({
reason: 'unfunded-recipient',
details: { reason: 'unfunded-recipient' },
});
});

Expand Down Expand Up @@ -173,7 +172,7 @@ it('rejects an unfunded off-curve recipient as off-curve when allowOffCurve is f
allowOffCurve: false,
allowUnfundedRecipient: true,
}),
).rejects.toMatchObject({ reason: 'off-curve' });
).rejects.toMatchObject({ details: { reason: 'off-curve' } });
});

it('accepts an existing off-curve System-Program-owned account unless allowOffCurve is false', async () => {
Expand All @@ -186,7 +185,7 @@ it('accepts an existing off-curve System-Program-owned account unless allowOffCu
await expect(assertValidTransferSolDestination(client.rpc, destination)).resolves.toBeUndefined();
await expect(
assertValidTransferSolDestination(client.rpc, destination, { allowOffCurve: false }),
).rejects.toMatchObject({ reason: 'off-curve' });
).rejects.toMatchObject({ details: { reason: 'off-curve' } });
});

it('does not forward guard options to getAccountInfo', async () => {
Expand Down
Loading