2026-03-17 18:08:53 +01:00
|
|
|
use nssa_core::{
|
fix(ata): lock down `ATA::Transfer` recipient contract
Enforce at the ATA layer that the recipient token holding is already
initialized, owned by the same token program as the sender ATA, decodes
to a valid `TokenHolding`, and points at the same token definition as
the sender. Align the core instruction doc and guest wrapper doc with
that contract, and cover the boundary with unit tests (default,
foreign-owned, malformed, mismatched-definition recipients, plus the
missing-owner-auth and happy paths) and end-to-end integration tests
(default and mismatched-definition recipients).
Without this, the downstream `token::Transfer` default-recipient
`Claim::Authorized` path was reachable through ATA, so integrators had
to reverse-engineer recipient semantics from token/runtime internals.
2026-05-11 12:44:46 -03:00
|
|
|
account::{Account, AccountWithMetadata},
|
2026-03-17 18:08:53 +01:00
|
|
|
program::{AccountPostState, ChainedCall, ProgramId},
|
|
|
|
|
};
|
|
|
|
|
use token_core::TokenHolding;
|
|
|
|
|
|
|
|
|
|
pub fn transfer_from_associated_token_account(
|
|
|
|
|
owner: AccountWithMetadata,
|
|
|
|
|
sender_ata: AccountWithMetadata,
|
|
|
|
|
recipient: AccountWithMetadata,
|
|
|
|
|
ata_program_id: ProgramId,
|
2026-05-13 17:24:11 -03:00
|
|
|
token_program_id: ProgramId,
|
2026-03-17 18:08:53 +01:00
|
|
|
amount: u128,
|
|
|
|
|
) -> (Vec<AccountPostState>, Vec<ChainedCall>) {
|
|
|
|
|
assert!(owner.is_authorized, "Owner authorization is missing");
|
2026-05-13 17:24:11 -03:00
|
|
|
assert_eq!(
|
|
|
|
|
sender_ata.account.program_owner, token_program_id,
|
|
|
|
|
"Sender ATA must be owned by expected token program"
|
|
|
|
|
);
|
fix(ata): lock down `ATA::Transfer` recipient contract
Enforce at the ATA layer that the recipient token holding is already
initialized, owned by the same token program as the sender ATA, decodes
to a valid `TokenHolding`, and points at the same token definition as
the sender. Align the core instruction doc and guest wrapper doc with
that contract, and cover the boundary with unit tests (default,
foreign-owned, malformed, mismatched-definition recipients, plus the
missing-owner-auth and happy paths) and end-to-end integration tests
(default and mismatched-definition recipients).
Without this, the downstream `token::Transfer` default-recipient
`Claim::Authorized` path was reachable through ATA, so integrators had
to reverse-engineer recipient semantics from token/runtime internals.
2026-05-11 12:44:46 -03:00
|
|
|
let sender_definition_id = TokenHolding::try_from(&sender_ata.account.data)
|
2026-03-17 18:08:53 +01:00
|
|
|
.expect("Sender ATA must hold a valid token")
|
|
|
|
|
.definition_id();
|
fix(ata): lock down `ATA::Transfer` recipient contract
Enforce at the ATA layer that the recipient token holding is already
initialized, owned by the same token program as the sender ATA, decodes
to a valid `TokenHolding`, and points at the same token definition as
the sender. Align the core instruction doc and guest wrapper doc with
that contract, and cover the boundary with unit tests (default,
foreign-owned, malformed, mismatched-definition recipients, plus the
missing-owner-auth and happy paths) and end-to-end integration tests
(default and mismatched-definition recipients).
Without this, the downstream `token::Transfer` default-recipient
`Claim::Authorized` path was reachable through ATA, so integrators had
to reverse-engineer recipient semantics from token/runtime internals.
2026-05-11 12:44:46 -03:00
|
|
|
let sender_seed = ata_core::verify_ata_and_get_seed(
|
|
|
|
|
&sender_ata,
|
|
|
|
|
&owner,
|
2026-05-13 17:24:11 -03:00
|
|
|
token_program_id,
|
fix(ata): lock down `ATA::Transfer` recipient contract
Enforce at the ATA layer that the recipient token holding is already
initialized, owned by the same token program as the sender ATA, decodes
to a valid `TokenHolding`, and points at the same token definition as
the sender. Align the core instruction doc and guest wrapper doc with
that contract, and cover the boundary with unit tests (default,
foreign-owned, malformed, mismatched-definition recipients, plus the
missing-owner-auth and happy paths) and end-to-end integration tests
(default and mismatched-definition recipients).
Without this, the downstream `token::Transfer` default-recipient
`Claim::Authorized` path was reachable through ATA, so integrators had
to reverse-engineer recipient semantics from token/runtime internals.
2026-05-11 12:44:46 -03:00
|
|
|
sender_definition_id,
|
|
|
|
|
ata_program_id,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// The recipient contract: ATA::Transfer requires a recipient token holding that is already
|
|
|
|
|
// initialized, owned by the same token program as the sender ATA, and that points at the same
|
|
|
|
|
// token definition as the sender. Anything else fails here rather than being silently
|
|
|
|
|
// materialized by the downstream token transfer (e.g. via `Claim::Authorized` on a default
|
|
|
|
|
// recipient), so integrators get an ATA-level failure rather than having to reverse-engineer
|
|
|
|
|
// token/runtime semantics.
|
|
|
|
|
assert_ne!(
|
|
|
|
|
recipient.account,
|
|
|
|
|
Account::default(),
|
|
|
|
|
"Recipient token holding must be initialized"
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
recipient.account.program_owner, token_program_id,
|
|
|
|
|
"Recipient must be owned by the same token program as the sender ATA"
|
|
|
|
|
);
|
|
|
|
|
let recipient_definition_id = TokenHolding::try_from(&recipient.account.data)
|
|
|
|
|
.expect("Recipient must hold a valid token")
|
|
|
|
|
.definition_id();
|
|
|
|
|
assert_eq!(
|
|
|
|
|
recipient_definition_id, sender_definition_id,
|
|
|
|
|
"Recipient and sender token definitions do not match"
|
|
|
|
|
);
|
2026-03-17 18:08:53 +01:00
|
|
|
|
|
|
|
|
let post_states = vec![
|
|
|
|
|
AccountPostState::new(owner.account.clone()),
|
|
|
|
|
AccountPostState::new(sender_ata.account.clone()),
|
|
|
|
|
AccountPostState::new(recipient.account.clone()),
|
|
|
|
|
];
|
|
|
|
|
let mut sender_ata_auth = sender_ata.clone();
|
|
|
|
|
sender_ata_auth.is_authorized = true;
|
|
|
|
|
|
|
|
|
|
let chained_call = ChainedCall::new(
|
|
|
|
|
token_program_id,
|
2026-04-15 14:55:04 -03:00
|
|
|
vec![sender_ata_auth, recipient],
|
2026-03-17 18:08:53 +01:00
|
|
|
&token_core::Instruction::Transfer {
|
|
|
|
|
amount_to_transfer: amount,
|
|
|
|
|
},
|
|
|
|
|
)
|
2026-04-15 14:55:04 -03:00
|
|
|
.with_pda_seeds(vec![sender_seed]);
|
2026-03-17 18:08:53 +01:00
|
|
|
(post_states, vec![chained_call])
|
|
|
|
|
}
|