Skip to content

Commit

Permalink
Merge pull request #12 from metaplex-foundation/danenbm/compress-vali…
Browse files Browse the repository at this point in the history
…dation

Use validate_asset_permissions in compress/decompress
  • Loading branch information
danenbm authored Mar 8, 2024
2 parents 49a780b + 8c4ea7f commit 7deba35
Show file tree
Hide file tree
Showing 37 changed files with 712 additions and 467 deletions.
26 changes: 26 additions & 0 deletions clients/js/src/generated/errors/mplCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,32 @@ export class MissingNewOwnerError extends ProgramError {
codeToErrorMap.set(0x15, MissingNewOwnerError);
nameToErrorMap.set('MissingNewOwner', MissingNewOwnerError);

/** MissingSystemProgram: Missing system program */
export class MissingSystemProgramError extends ProgramError {
override readonly name: string = 'MissingSystemProgram';

readonly code: number = 0x16; // 22

constructor(program: Program, cause?: Error) {
super('Missing system program', program, cause);
}
}
codeToErrorMap.set(0x16, MissingSystemProgramError);
nameToErrorMap.set('MissingSystemProgram', MissingSystemProgramError);

/** NotImplemented: Not implemented */
export class NotImplementedError extends ProgramError {
override readonly name: string = 'NotImplemented';

readonly code: number = 0x17; // 23

constructor(program: Program, cause?: Error) {
super('Not implemented', program, cause);
}
}
codeToErrorMap.set(0x17, NotImplementedError);
nameToErrorMap.set('NotImplemented', NotImplementedError);

/**
* Attempts to resolve a custom program error from the provided error code.
* @category Errors
Expand Down
9 changes: 8 additions & 1 deletion clients/js/src/generated/instructions/burn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export type BurnInstructionAccounts = {
authority?: Signer;
/** The account paying for the storage fees */
payer?: Signer;
/** The system program */
systemProgram?: PublicKey | Pda;
/** The SPL Noop Program */
logWrapper?: PublicKey | Pda;
};
Expand Down Expand Up @@ -110,9 +112,14 @@ export function burn(
isWritable: true as boolean,
value: input.payer ?? null,
},
logWrapper: {
systemProgram: {
index: 4,
isWritable: false as boolean,
value: input.systemProgram ?? null,
},
logWrapper: {
index: 5,
isWritable: false as boolean,
value: input.logWrapper ?? null,
},
} satisfies ResolvedAccountsWithIndices;
Expand Down
11 changes: 7 additions & 4 deletions clients/js/src/generated/instructions/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type CompressInstructionAccounts = {
/** The collection to which the asset belongs */
collection?: PublicKey | Pda;
/** The owner or delegate of the asset */
owner: Signer;
authority?: Signer;
/** The account receiving the storage fees */
payer?: Signer;
/** The system program */
Expand Down Expand Up @@ -65,7 +65,7 @@ export function getCompressInstructionDataSerializer(): Serializer<

// Instruction.
export function compress(
context: Pick<Context, 'programs'>,
context: Pick<Context, 'identity' | 'programs'>,
input: CompressInstructionAccounts
): TransactionBuilder {
// Program ID.
Expand All @@ -86,10 +86,10 @@ export function compress(
isWritable: false as boolean,
value: input.collection ?? null,
},
owner: {
authority: {
index: 2,
isWritable: false as boolean,
value: input.owner ?? null,
value: input.authority ?? null,
},
payer: {
index: 3,
Expand All @@ -109,6 +109,9 @@ export function compress(
} satisfies ResolvedAccountsWithIndices;

// Default values.
if (!resolvedAccounts.authority.value) {
resolvedAccounts.authority.value = context.identity;
}
if (!resolvedAccounts.systemProgram.value) {
resolvedAccounts.systemProgram.value = context.programs.getPublicKey(
'splSystem',
Expand Down
11 changes: 7 additions & 4 deletions clients/js/src/generated/instructions/decompress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type DecompressInstructionAccounts = {
/** The collection to which the asset belongs */
collection?: PublicKey | Pda;
/** The owner or delegate of the asset */
owner: Signer;
authority?: Signer;
/** The account paying for the storage fees */
payer?: Signer;
/** The system program */
Expand Down Expand Up @@ -82,7 +82,7 @@ export type DecompressInstructionArgs = DecompressInstructionDataArgs;

// Instruction.
export function decompress(
context: Pick<Context, 'programs'>,
context: Pick<Context, 'identity' | 'programs'>,
input: DecompressInstructionAccounts & DecompressInstructionArgs
): TransactionBuilder {
// Program ID.
Expand All @@ -103,10 +103,10 @@ export function decompress(
isWritable: false as boolean,
value: input.collection ?? null,
},
owner: {
authority: {
index: 2,
isWritable: false as boolean,
value: input.owner ?? null,
value: input.authority ?? null,
},
payer: {
index: 3,
Expand All @@ -129,6 +129,9 @@ export function decompress(
const resolvedArgs: DecompressInstructionArgs = { ...input };

// Default values.
if (!resolvedAccounts.authority.value) {
resolvedAccounts.authority.value = context.identity;
}
if (!resolvedAccounts.systemProgram.value) {
resolvedAccounts.systemProgram.value = context.programs.getPublicKey(
'splSystem',
Expand Down
9 changes: 8 additions & 1 deletion clients/js/src/generated/instructions/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export type TransferInstructionAccounts = {
payer?: Signer;
/** The new owner to which to transfer the asset */
newOwner: PublicKey | Pda;
/** The system program */
systemProgram?: PublicKey | Pda;
/** The SPL Noop Program */
logWrapper?: PublicKey | Pda;
};
Expand Down Expand Up @@ -121,9 +123,14 @@ export function transfer(
isWritable: false as boolean,
value: input.newOwner ?? null,
},
logWrapper: {
systemProgram: {
index: 5,
isWritable: false as boolean,
value: input.systemProgram ?? null,
},
logWrapper: {
index: 6,
isWritable: false as boolean,
value: input.logWrapper ?? null,
},
} satisfies ResolvedAccountsWithIndices;
Expand Down
7 changes: 4 additions & 3 deletions clients/js/test/compress.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateSigner } from '@metaplex-foundation/umi';
import { generateSigner, publicKey } from '@metaplex-foundation/umi';
import test from 'ava';
import {
Asset,
Expand Down Expand Up @@ -44,7 +44,8 @@ test('it can compress an asset without any plugins as the owner', async (t) => {
// And when we compress the asset.
await compress(umi, {
asset: assetAddress.publicKey,
owner: umi.identity,
authority: umi.identity,
logWrapper: publicKey('noopb9bkMVfRPU8AsbpTUg8AQkHtKwMYZiFUjNRtMmV'),
}).sendAndConfirm(umi);
// console.log('Compress signature: ', bs58.encode(tx.signature));

Expand Down Expand Up @@ -92,7 +93,7 @@ test('it cannot compress an asset if not the owner', async (t) => {

const result = compress(umi, {
asset: assetAddress.publicKey,
owner: attacker,
authority: attacker,
}).sendAndConfirm(umi);

await t.throwsAsync(result, { name: 'InvalidAuthority' });
Expand Down
44 changes: 22 additions & 22 deletions clients/js/test/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {
create,
fetchAsset,
fetchAssetWithPlugins,
fetchHashedAsset,
getAssetAccountDataSerializer,
updateAuthority,
} from '../src';
import { DEFAULT_ASSET, assertAsset, createAsset, createUmi } from './_setup';
Expand Down Expand Up @@ -67,7 +65,7 @@ test('it can create a new asset in ledger state', async (t) => {
const assetAddress = generateSigner(umi);

// When we create a new account.
const txResult = await create(umi, {
const result = create(umi, {
dataState: DataState.LedgerState,
asset: assetAddress,
name: 'Test Bread',
Expand All @@ -76,27 +74,29 @@ test('it can create a new asset in ledger state', async (t) => {
plugins: [],
}).sendAndConfirm(umi);

await t.throwsAsync(result, { name: 'NotImplemented' });

// Then an account was created with the correct data.
const asset = await fetchHashedAsset(umi, assetAddress.publicKey);
//const asset = await fetchHashedAsset(umi, assetAddress.publicKey);
// console.log(asset);
t.like(asset, <Asset>{
publicKey: assetAddress.publicKey,
});

const tx = await umi.rpc.getTransaction(txResult.signature);
if (tx && tx.meta.innerInstructions) {
// console.log(tx.meta.innerInstructions[0].instructions);
const { data } = tx.meta.innerInstructions[0].instructions[0];
// console.log(base58.deserialize(data));
const parsed = getAssetAccountDataSerializer().deserialize(data)[0];
// console.log("Ledger State:", parsed);
t.like(parsed, <Asset>{
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
owner: umi.identity.publicKey,
name: 'Test Bread',
uri: 'https://example.com/bread',
});
}
// t.like(asset, <Asset>{
// publicKey: assetAddress.publicKey,
// });

// const tx = await umi.rpc.getTransaction(txResult.signature);
// if (tx && tx.meta.innerInstructions) {
// // console.log(tx.meta.innerInstructions[0].instructions);
// const { data } = tx.meta.innerInstructions[0].instructions[0];
// // console.log(base58.deserialize(data));
// const parsed = getAssetAccountDataSerializer().deserialize(data)[0];
// // console.log("Ledger State:", parsed);
// t.like(parsed, <Asset>{
// updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
// owner: umi.identity.publicKey,
// name: 'Test Bread',
// uri: 'https://example.com/bread',
// });
// }
});

test('it can create a new asset in account state with plugins', async (t) => {
Expand Down
7 changes: 4 additions & 3 deletions clients/js/test/decompress.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateSigner } from '@metaplex-foundation/umi';
import { generateSigner, publicKey } from '@metaplex-foundation/umi';
import test from 'ava';
import {
Asset,
Expand Down Expand Up @@ -44,7 +44,8 @@ test('it can decompress a previously compressed asset as the owner', async (t) =
// And when we compress the asset.
await compress(umi, {
asset: assetAddress.publicKey,
owner: umi.identity,
authority: umi.identity,
logWrapper: publicKey('noopb9bkMVfRPU8AsbpTUg8AQkHtKwMYZiFUjNRtMmV'),
}).sendAndConfirm(umi);

// And the asset is now compressed as a hashed asset.
Expand All @@ -67,7 +68,7 @@ test('it can decompress a previously compressed asset as the owner', async (t) =
// And when we decompress the asset.
await decompress(umi, {
asset: assetAddress.publicKey,
owner: umi.identity,
authority: umi.identity,
compressionProof: {
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
owner: umi.identity.publicKey,
Expand Down
12 changes: 9 additions & 3 deletions clients/js/test/info.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { generateSigner, publicKey } from '@metaplex-foundation/umi';
import test from 'ava';
import { DataState, create /* fetchAsset, fetchHashedAsset */ } from '../src';
import { DataState, create, compress /* fetchAsset, fetchHashedAsset */ } from '../src';
import { createUmi } from './_setup';

test('fetch account info for account state', async (t) => {
Expand Down Expand Up @@ -30,21 +30,27 @@ test('fetch account info for account state', async (t) => {
t.pass();
});

test('fetch account info for ledger state', async (t) => {
test('HELLO EHLO HLEOOO fetch account info for ledger state', async (t) => {
// Given a Umi instance and a new signer.
const umi = await createUmi();
const assetAddress = generateSigner(umi);

// When we create a new account.
await create(umi, {
dataState: DataState.LedgerState,
dataState: DataState.AccountState,
asset: assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
logWrapper: publicKey('noopb9bkMVfRPU8AsbpTUg8AQkHtKwMYZiFUjNRtMmV'),
plugins: [],
}).sendAndConfirm(umi);

// And when we compress the asset.
await compress(umi, {
asset: assetAddress.publicKey,
logWrapper: publicKey('noopb9bkMVfRPU8AsbpTUg8AQkHtKwMYZiFUjNRtMmV'),
}).sendAndConfirm(umi);

// Print the size of the account.
const account = await umi.rpc.getAccount(assetAddress.publicKey);
if (account.exists) {
Expand Down
6 changes: 6 additions & 0 deletions clients/rust/src/generated/errors/mpl_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ pub enum MplCoreError {
/// 21 (0x15) - Missing new owner
#[error("Missing new owner")]
MissingNewOwner,
/// 22 (0x16) - Missing system program
#[error("Missing system program")]
MissingSystemProgram,
/// 23 (0x17) - Not implemented
#[error("Not implemented")]
NotImplemented,
}

impl solana_program::program_error::PrintProgramError for MplCoreError {
Expand Down
Loading

0 comments on commit 7deba35

Please sign in to comment.