Skip to content

Commit

Permalink
Merging main.
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Feb 23, 2024
2 parents cf1388b + 2348653 commit 3490b23
Show file tree
Hide file tree
Showing 22 changed files with 571 additions and 251 deletions.
36 changes: 29 additions & 7 deletions clients/js/src/generated/instructions/decompress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import {
ResolvedAccountsWithIndices,
getAccountMetasAndSigners,
} from '../shared';
import {
CompressionProof,
CompressionProofArgs,
getCompressionProofSerializer,
} from '../types';

// Accounts.
export type DecompressInstructionAccounts = {
Expand All @@ -41,9 +46,14 @@ export type DecompressInstructionAccounts = {
};

// Data.
export type DecompressInstructionData = { discriminator: number };
export type DecompressInstructionData = {
discriminator: number;
compressionProof: CompressionProof;
};

export type DecompressInstructionDataArgs = {};
export type DecompressInstructionDataArgs = {
compressionProof: CompressionProofArgs;
};

export function getDecompressInstructionDataSerializer(): Serializer<
DecompressInstructionDataArgs,
Expand All @@ -54,17 +64,24 @@ export function getDecompressInstructionDataSerializer(): Serializer<
any,
DecompressInstructionData
>(
struct<DecompressInstructionData>([['discriminator', u8()]], {
description: 'DecompressInstructionData',
}),
struct<DecompressInstructionData>(
[
['discriminator', u8()],
['compressionProof', getCompressionProofSerializer()],
],
{ description: 'DecompressInstructionData' }
),
(value) => ({ ...value, discriminator: 10 })
) as Serializer<DecompressInstructionDataArgs, DecompressInstructionData>;
}

// Args.
export type DecompressInstructionArgs = DecompressInstructionDataArgs;

// Instruction.
export function decompress(
context: Pick<Context, 'programs'>,
input: DecompressInstructionAccounts
input: DecompressInstructionAccounts & DecompressInstructionArgs
): TransactionBuilder {
// Program ID.
const programId = context.programs.getPublicKey(
Expand Down Expand Up @@ -93,6 +110,9 @@ export function decompress(
},
};

// Arguments.
const resolvedArgs: DecompressInstructionArgs = { ...input };

// Default values.
if (!resolvedAccounts.systemProgram.value) {
resolvedAccounts.systemProgram.value = context.programs.getPublicKey(
Expand All @@ -115,7 +135,9 @@ export function decompress(
);

// Data.
const data = getDecompressInstructionDataSerializer().serialize({});
const data = getDecompressInstructionDataSerializer().serialize(
resolvedArgs as DecompressInstructionDataArgs
);

// Bytes Created On Chain.
const bytesCreatedOnChain = 0;
Expand Down
4 changes: 2 additions & 2 deletions clients/js/src/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ 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);
}
return keccak_256(Array.isArray(input) ? mergeBytes(input) : input);
}
9 changes: 6 additions & 3 deletions clients/js/src/hooked/fetchAssetWithPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import {
getPluginSerializer
} from "../generated";

export type PluginWithAuthorities = { plugin: Plugin, authorities: Authority[] };
export type PluginWithAuthorities = {
plugin: Plugin;
authorities: Authority[];
};

export type PluginList = {
pluginHeader?: Omit<PluginHeader, 'publicKey' | 'header'>,
Expand Down Expand Up @@ -59,8 +62,8 @@ export async function fetchAssetWithPlugins(
pluginHeader,
plugins,
pluginRegistry,
...asset
...asset,
};

return assetWithPlugins;
}
}
2 changes: 1 addition & 1 deletion clients/js/src/hooked/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './fetchAssetWithPlugins';
export * from './fetchAssetWithPlugins';
3 changes: 1 addition & 2 deletions clients/js/test/_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
import { createUmi as basecreateUmi } from '@metaplex-foundation/umi-bundle-tests';
import { mplAsset } from '../src';

export const createUmi = async () =>
(await basecreateUmi()).use(mplAsset());
export const createUmi = async () => (await basecreateUmi()).use(mplAsset());
78 changes: 47 additions & 31 deletions clients/js/test/addAuthority.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { generateSigner } from '@metaplex-foundation/umi';
import test from 'ava';
// import { base58 } from '@metaplex-foundation/umi/serializers';
import { Asset, AssetWithPlugins, DataState, PluginType, addAuthority, addPlugin, create, fetchAsset, fetchAssetWithPlugins } from '../src';
import {
Asset,
AssetWithPlugins,
DataState,
PluginType,
addAuthority,
addPlugin,
create,
fetchAsset,
fetchAssetWithPlugins,
} from '../src';
import { createUmi } from './_setup';

test('it can add an authority to a plugin', async (t) => {
Expand Down Expand Up @@ -34,17 +44,19 @@ test('it can add an authority to a plugin', async (t) => {
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
}
}).append(
addAuthority(umi, {
assetAddress: assetAddress.publicKey,
pluginType: PluginType.Freeze,
newAuthority: {
__kind: 'Pubkey',
address: delegateAddress.publicKey,
}
})
).sendAndConfirm(umi);
},
})
.append(
addAuthority(umi, {
assetAddress: assetAddress.publicKey,
pluginType: PluginType.Freeze,
newAuthority: {
__kind: 'Pubkey',
address: delegateAddress.publicKey,
},
})
)
.sendAndConfirm(umi);

const asset1 = await fetchAssetWithPlugins(umi, assetAddress.publicKey);
// console.log(JSON.stringify(asset1, (_, v) => typeof v === 'bigint' ? v.toString() : v, 2));
Expand All @@ -60,26 +72,30 @@ test('it can add an authority to a plugin', async (t) => {
},
pluginRegistry: {
key: 4,
registry: [{
pluginType: 2,
data: {
offset: BigInt(117),
authorities: [
{ __kind: "Owner" },
{ __kind: "Pubkey", address: delegateAddress.publicKey }
]
}
}],
},
plugins: [{
authorities: [
{ __kind: "Owner" },
{ __kind: "Pubkey", address: delegateAddress.publicKey }
registry: [
{
pluginType: 2,
data: {
offset: BigInt(117),
authorities: [
{ __kind: 'Owner' },
{ __kind: 'Pubkey', address: delegateAddress.publicKey },
],
},
},
],
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
},
plugins: [
{
authorities: [
{ __kind: 'Owner' },
{ __kind: 'Pubkey', address: delegateAddress.publicKey },
],
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
},
},
}],
],
});
});
42 changes: 27 additions & 15 deletions clients/js/test/addPlugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { generateSigner } from '@metaplex-foundation/umi';
import test from 'ava';
// import { base58 } from '@metaplex-foundation/umi/serializers';
import { Asset, AssetWithPlugins, DataState, addPlugin, create, fetchAsset, fetchAssetWithPlugins } from '../src';
import {
Asset,
AssetWithPlugins,
DataState,
addPlugin,
create,
fetchAsset,
fetchAssetWithPlugins,
} from '../src';
import { createUmi } from './_setup';

test('it can add a plugin to an asset', async (t) => {
Expand Down Expand Up @@ -33,7 +41,7 @@ test('it can add a plugin to an asset', async (t) => {
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
}
},
}).sendAndConfirm(umi);

const asset1 = await fetchAssetWithPlugins(umi, assetAddress.publicKey);
Expand All @@ -50,20 +58,24 @@ test('it can add a plugin to an asset', async (t) => {
},
pluginRegistry: {
key: 4,
registry: [{
pluginType: 2,
data: {
offset: BigInt(117),
authorities: [{ __kind: "Owner" }]
}
}],
registry: [
{
pluginType: 2,
data: {
offset: BigInt(117),
authorities: [{ __kind: 'Owner' }],
},
},
],
},
plugins: [{
authorities: [{ __kind: "Owner" }],
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
plugins: [
{
authorities: [{ __kind: 'Owner' }],
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
},
},
}],
],
});
});
10 changes: 7 additions & 3 deletions clients/js/test/burn.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { assertAccountExists, generateSigner, sol} from '@metaplex-foundation/umi';
import {
assertAccountExists,
generateSigner,
sol,
} from '@metaplex-foundation/umi';
import test from 'ava';
// import { base58 } from '@metaplex-foundation/umi/serializers';
import { Asset, DataState, create, fetchAsset, burn, Key } from '../src';
Expand Down Expand Up @@ -30,7 +34,7 @@ test('it can burn an asset as the owner', async (t) => {

await burn(umi, {
assetAddress: assetAddress.publicKey,
compressionProof: null
compressionProof: null,
}).sendAndConfirm(umi);

// And the asset address still exists but was resized to 1.
Expand Down Expand Up @@ -73,7 +77,7 @@ test('it cannot burn an asset if not the owner', async (t) => {
authority: attacker,
}).sendAndConfirm(umi);

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

const afterAsset = await fetchAsset(umi, assetAddress.publicKey);
// console.log("Account State:", afterAsset);
Expand Down
24 changes: 18 additions & 6 deletions clients/js/test/compress.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { generateSigner } from '@metaplex-foundation/umi';
import test from 'ava';
import { Asset, DataState, create, fetchAsset, fetchHashedAsset, getAssetAccountDataSerializer, compress, HashedAssetSchema, getHashedAssetSchemaSerializer } from '../src';
import {
Asset,
compress,
create,
DataState,
fetchAsset,
fetchHashedAsset,
getAssetAccountDataSerializer,
getHashedAssetSchemaSerializer,
HashedAssetSchema,
} from '../src';
import { createUmi } from './_setup';
import bs58 from 'bs58';
//import bs58 from 'bs58';
import { hash } from '../src';

test('it can compress an asset without any plugins as the owner', async (t) => {
Expand Down Expand Up @@ -30,11 +40,11 @@ test('it can compress an asset without any plugins as the owner', async (t) => {
});

// And when we compress the asset.
let tx = await compress(umi, {
await compress(umi, {
assetAddress: assetAddress.publicKey,
owner: umi.identity,
}).sendAndConfirm(umi);
console.log("Compress signature: ", bs58.encode(tx.signature));
//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);
Expand All @@ -46,7 +56,9 @@ test('it can compress an asset without any plugins as the owner', async (t) => {
pluginHashes: [],
};

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

Expand Down Expand Up @@ -80,7 +92,7 @@ test('it cannot compress an asset if not the owner', async (t) => {
owner: attacker,
}).sendAndConfirm(umi);

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

const afterAsset = await fetchAsset(umi, assetAddress.publicKey);
// console.log("Account State:", afterAsset);
Expand Down
Loading

0 comments on commit 3490b23

Please sign in to comment.