Skip to content

Commit

Permalink
Add basic compress test
Browse files Browse the repository at this point in the history
  • Loading branch information
danenbm committed Feb 22, 2024
1 parent 3c293eb commit af2f47b
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 8 deletions.
4 changes: 3 additions & 1 deletion clients/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"@metaplex-foundation/mpl-toolbox": "^0.8.0"
},
"peerDependencies": {
"@metaplex-foundation/umi": "^0.8.2"
"@metaplex-foundation/umi": "^0.8.2",
"@noble/hashes": "^1.3.1"
},
"devDependencies": {
"@ava/typescript": "^3.0.1",
Expand All @@ -38,6 +39,7 @@
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.46.1",
"ava": "^5.1.0",
"bs58": "5.0.0",
"eslint": "^8.0.1",
"eslint-config-airbnb-typescript": "^17.0.0",
"eslint-config-prettier": "^8.5.0",
Expand Down
28 changes: 22 additions & 6 deletions clients/js/pnpm-lock.yaml

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

6 changes: 6 additions & 0 deletions clients/js/src/hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { mergeBytes } from '@metaplex-foundation/umi/serializers';
import { keccak_256 } from '@noble/hashes/sha3';

export function hash(input: Uint8Array | Uint8Array[]): Uint8Array {
return keccak_256(Array.isArray(input) ? mergeBytes(input) : input);
}
3 changes: 2 additions & 1 deletion clients/js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './generated';
export * from './plugin';
export * from './hooked';
export * from './hash';
export * from './hooked';
94 changes: 94 additions & 0 deletions clients/js/test/compress.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { generateSigner } from '@metaplex-foundation/umi';
import test from 'ava';
import { Asset, DataState, create, fetchAsset, fetchHashedAsset, getAssetAccountDataSerializer, compress, HashedAssetSchema, getHashedAssetSchemaSerializer } from '../src';
import { createUmi } from './_setup';
import bs58 from 'bs58';
import { hash } from '../src';

test('it can compress an asset without any plugins as the owner', 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.AccountState,
assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
}).sendAndConfirm(umi);

// Then an account was created with the correct data.
const beforeAsset = await fetchAsset(umi, assetAddress.publicKey);
//console.log("Account State:", beforeAsset);
t.like(beforeAsset, <Asset>{
publicKey: assetAddress.publicKey,
updateAuthority: umi.identity.publicKey,
owner: umi.identity.publicKey,
name: 'Test Bread',
uri: 'https://example.com/bread',
});

// And when we compress the asset.
let tx = await compress(umi, {
assetAddress: assetAddress.publicKey,
owner: umi.identity,
}).sendAndConfirm(umi);
console.log("Compress signature: ", bs58.encode(tx.signature));

// And the asset is now compressed as a hashed asset.
const afterAsset = await fetchHashedAsset(umi, assetAddress.publicKey);
//console.log("Account State:", afterAsset);

// And the hash matches the expected value.
let hashedAssetSchema: HashedAssetSchema = {
assetHash: hash(getAssetAccountDataSerializer().serialize(beforeAsset)),
pluginHashes: [],
};

let hashedAsset = hash(getHashedAssetSchemaSerializer().serialize(hashedAssetSchema));
t.deepEqual(afterAsset.hash, hashedAsset);
});

test('it cannot compress an asset if not the owner', async (t) => {
// Given a Umi instance and a new signer.
const umi = await createUmi();
const assetAddress = generateSigner(umi);
const attacker = generateSigner(umi);

// When we create a new account.
await create(umi, {
dataState: DataState.AccountState,
assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
}).sendAndConfirm(umi);

// Then an account was created with the correct data.
const beforeAsset = await fetchAsset(umi, assetAddress.publicKey);
// console.log("Account State:", beforeAsset);
t.like(beforeAsset, <Asset>{
publicKey: assetAddress.publicKey,
updateAuthority: umi.identity.publicKey,
owner: umi.identity.publicKey,
name: 'Test Bread',
uri: 'https://example.com/bread',
});

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

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

const afterAsset = await fetchAsset(umi, assetAddress.publicKey);
// console.log("Account State:", afterAsset);
t.like(afterAsset, <Asset>{
publicKey: assetAddress.publicKey,
updateAuthority: umi.identity.publicKey,
owner: umi.identity.publicKey,
name: 'Test Bread',
uri: 'https://example.com/bread',
});
});

0 comments on commit af2f47b

Please sign in to comment.