|
| 1 | +/** |
| 2 | + * A spawned ANT's OWNER may differ from the wallet that pays for it. |
| 3 | + * |
| 4 | + * `buildSpawnAntInstructions({ owner })` separates the two roles the spawn |
| 5 | + * really has: `signer` funds the MPL Core asset's rent and signs `CreateV1`, |
| 6 | + * while `owner` receives the NFT and signs `ario_ant::initialize`. That is what |
| 7 | + * a sponsored spawn needs — a service pays, the end user owns — and the chain |
| 8 | + * already supports it (`CreateV1` takes `payer` and `owner` as separate |
| 9 | + * accounts, and `owner` is not a signer there). |
| 10 | + * |
| 11 | + * These tests pin the ACCOUNT ROLES rather than the byte layout, which |
| 12 | + * `spawn-ant.test.ts` already covers: the failure mode this guards against is a |
| 13 | + * silent swap of payer and owner, which would hand custody to the wrong wallet |
| 14 | + * while still producing a perfectly well-formed transaction. |
| 15 | + */ |
| 16 | +import { strict as assert } from 'node:assert'; |
| 17 | +import { describe, it } from 'node:test'; |
| 18 | + |
| 19 | +import { |
| 20 | + type Address, |
| 21 | + address, |
| 22 | + createNoopSigner, |
| 23 | + generateKeyPairSigner, |
| 24 | +} from '@solana/kit'; |
| 25 | + |
| 26 | +import { buildSpawnAntInstructions } from './spawn-ant.js'; |
| 27 | + |
| 28 | +const OWNER: Address = address('11111111111111111111111111111113'); |
| 29 | + |
| 30 | +/** MPL Core `CreateV1` account order: asset, collection, authority, payer, owner. */ |
| 31 | +const CREATE_V1_PAYER_INDEX = 3; |
| 32 | +const CREATE_V1_OWNER_INDEX = 4; |
| 33 | + |
| 34 | +async function build(withOwner: boolean) { |
| 35 | + const signer = await generateKeyPairSigner(); |
| 36 | + const { instructions } = await buildSpawnAntInstructions({ |
| 37 | + signer, |
| 38 | + state: { name: 'sponsored-name' }, |
| 39 | + ...(withOwner ? { owner: createNoopSigner(OWNER) } : {}), |
| 40 | + }); |
| 41 | + const [createIx, initIx] = instructions; |
| 42 | + assert.ok(createIx.accounts, 'CreateV1 must carry accounts'); |
| 43 | + assert.ok(initIx.accounts, 'initialize must carry accounts'); |
| 44 | + return { signer, createIx, initIx }; |
| 45 | +} |
| 46 | + |
| 47 | +describe('buildSpawnAntInstructions owner/payer separation', () => { |
| 48 | + it('defaults the owner to the signer, unchanged from before', async () => { |
| 49 | + const { signer, createIx, initIx } = await build(false); |
| 50 | + const accounts = createIx.accounts as readonly { address: Address }[]; |
| 51 | + |
| 52 | + assert.equal(accounts[CREATE_V1_PAYER_INDEX].address, signer.address); |
| 53 | + assert.equal(accounts[CREATE_V1_OWNER_INDEX].address, signer.address); |
| 54 | + assert.ok( |
| 55 | + (initIx.accounts as readonly { address: Address }[]).some( |
| 56 | + (a) => a.address === signer.address, |
| 57 | + ), |
| 58 | + 'initialize must be signed by the signer when no owner is given', |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + it('mints to `owner` while `signer` still pays', async () => { |
| 63 | + const { signer, createIx } = await build(true); |
| 64 | + const accounts = createIx.accounts as readonly { address: Address }[]; |
| 65 | + |
| 66 | + assert.equal( |
| 67 | + accounts[CREATE_V1_PAYER_INDEX].address, |
| 68 | + signer.address, |
| 69 | + 'the sponsor must remain the payer', |
| 70 | + ); |
| 71 | + assert.equal( |
| 72 | + accounts[CREATE_V1_OWNER_INDEX].address, |
| 73 | + OWNER, |
| 74 | + 'the ANT must be minted to the supplied owner, not the payer', |
| 75 | + ); |
| 76 | + assert.notEqual( |
| 77 | + accounts[CREATE_V1_OWNER_INDEX].address, |
| 78 | + signer.address, |
| 79 | + 'payer and owner must not collapse — that silently hands over custody', |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + // `ario_ant::initialize` declares `owner: Signer` and creates AntConfig, |
| 84 | + // AntControllers and the root AntRecord with `payer = owner`. Signing it with |
| 85 | + // the sponsor would both fail on chain and charge the wrong account. |
| 86 | + it('signs `initialize` as the OWNER, not the payer', async () => { |
| 87 | + const { signer, initIx } = await build(true); |
| 88 | + const addresses = (initIx.accounts as readonly { address: Address }[]).map( |
| 89 | + (a) => a.address, |
| 90 | + ); |
| 91 | + |
| 92 | + assert.ok(addresses.includes(OWNER), 'initialize must reference the owner'); |
| 93 | + assert.ok( |
| 94 | + !addresses.includes(signer.address), |
| 95 | + 'initialize must NOT reference the sponsor — it pins payer = owner', |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it('accepts a noop signer, so a sponsor can build for a remote owner', async () => { |
| 100 | + const { createIx } = await build(true); |
| 101 | + const accounts = createIx.accounts as readonly { address: Address }[]; |
| 102 | + // The point of a noop signer: the sponsor assembles and partially signs the |
| 103 | + // transaction without holding the owner's key, and the owner adds theirs. |
| 104 | + assert.equal(accounts[CREATE_V1_OWNER_INDEX].address, OWNER); |
| 105 | + }); |
| 106 | +}); |
0 commit comments