From b5bfd0ba4f0d5a65662db07ee4e964baee0f34fc Mon Sep 17 00:00:00 2001 From: shotgunofdeath Date: Wed, 6 Mar 2024 12:21:22 +0100 Subject: [PATCH 1/3] feat(types): add custom types for authorities, plugins and asset --- clients/js/src/hooked/authorityHelpers.ts | 50 ++++++++++ .../src/hooked/fetchAssetWithPluginsTest.ts | 59 +++++++++++ clients/js/src/hooked/index.ts | 4 + clients/js/src/hooked/pluginHelpers.ts | 97 +++++++++++++++++++ clients/js/src/hooked/types.ts | 43 ++++++++ clients/js/src/hooked/utils.ts | 4 + clients/js/test/addAuthorityWithTestFetch.ts | 75 ++++++++++++++ 7 files changed, 332 insertions(+) create mode 100644 clients/js/src/hooked/authorityHelpers.ts create mode 100644 clients/js/src/hooked/fetchAssetWithPluginsTest.ts create mode 100644 clients/js/src/hooked/pluginHelpers.ts create mode 100644 clients/js/src/hooked/types.ts create mode 100644 clients/js/src/hooked/utils.ts create mode 100644 clients/js/test/addAuthorityWithTestFetch.ts diff --git a/clients/js/src/hooked/authorityHelpers.ts b/clients/js/src/hooked/authorityHelpers.ts new file mode 100644 index 00000000..c15eff4a --- /dev/null +++ b/clients/js/src/hooked/authorityHelpers.ts @@ -0,0 +1,50 @@ +import { PublicKey } from '@metaplex-foundation/umi'; +import { Authority, authority as authorityHelper } from '../generated'; +import { BaseAuthorities } from './types'; +import { toWords } from './utils'; + +// Authorities data helpers +export function getNoneAuthority() { + return authorityHelper('None'); +} + +export function getOwnerAuthority() { + return authorityHelper('Owner'); +} + +export function getUpdateAuthority() { + return authorityHelper('UpdateAuthority'); +} + +export function getPubkeyAuthority(address: PublicKey) { + return authorityHelper('Pubkey', { address }); +} + +export function getPermanentAuthority(address: PublicKey) { + return authorityHelper('Permanent', { address }); +} + +export function mapAuthorities(authorities: Authority[]) { + return authorities.reduce((acc: BaseAuthorities, authority: Authority) => { + const authorityKey = toWords(authority.__kind) + .split(' ')[0] + .toLowerCase() as keyof BaseAuthorities; + const authorityKeysLen = Object.keys(authority).length; + + if (authorityKeysLen === 1) { + acc = { ...acc, [authorityKey]: true }; + } + + if (authorityKeysLen > 1) { + acc = { + ...acc, + [authorityKey]: [ + ...(acc[authorityKey] ? (acc[authorityKey] as any[]) : []), + ...Object.values(authority).slice(1), + ], + }; + } + + return acc; + }, {}); +} diff --git a/clients/js/src/hooked/fetchAssetWithPluginsTest.ts b/clients/js/src/hooked/fetchAssetWithPluginsTest.ts new file mode 100644 index 00000000..16a0427f --- /dev/null +++ b/clients/js/src/hooked/fetchAssetWithPluginsTest.ts @@ -0,0 +1,59 @@ +import { + Context, + Pda, + PublicKey, + RpcGetAccountOptions, + assertAccountExists, + publicKey as toPublicKey, +} from '@metaplex-foundation/umi'; +import { + PluginHeaderAccountData, + PluginRegistryAccountData, + deserializeAsset, + getAssetAccountDataSerializer, + getPluginHeaderAccountDataSerializer, + getPluginRegistryAccountDataSerializer, +} from '../generated'; +import { AssetWithPluginsTest, PluginsList } from './types'; +import { registryRecordsToPluginsList } from './pluginHelpers'; + +export async function fetchAssetWithPluginsTest( + context: Pick, + publicKey: PublicKey | Pda, + options?: RpcGetAccountOptions +): Promise { + const maybeAccount = await context.rpc.getAccount( + toPublicKey(publicKey, false), + options + ); + assertAccountExists(maybeAccount, 'Asset'); + const asset = deserializeAsset(maybeAccount); + const assetData = getAssetAccountDataSerializer().serialize(asset); + + let pluginHeader: PluginHeaderAccountData | undefined; + let pluginRegistry: PluginRegistryAccountData | undefined; + let pluginsList: PluginsList | undefined; + + if (maybeAccount.data.length !== assetData.length) { + [pluginHeader] = getPluginHeaderAccountDataSerializer().deserialize( + maybeAccount.data, + assetData.length + ); + + [pluginRegistry] = getPluginRegistryAccountDataSerializer().deserialize( + maybeAccount.data, + Number(pluginHeader.pluginRegistryOffset) + ); + + pluginsList = registryRecordsToPluginsList( + pluginRegistry.registry, + maybeAccount.data + ); + } + + return { + pluginHeader, + ...pluginsList, + ...asset, + }; +} diff --git a/clients/js/src/hooked/index.ts b/clients/js/src/hooked/index.ts index b69f25ca..a2fbb778 100644 --- a/clients/js/src/hooked/index.ts +++ b/clients/js/src/hooked/index.ts @@ -1,7 +1,11 @@ import { Authority, PluginHeader, PluginRegistry, Plugin } from 'src/generated'; export * from './fetchAssetWithPlugins'; +export * from './fetchAssetWithPluginsTest'; export * from './fetchCollectionWithPlugins'; +export * from './authorityHelpers'; +export * from './pluginHelpers'; +export * from './types'; export type PluginWithAuthority = { plugin: Plugin; diff --git a/clients/js/src/hooked/pluginHelpers.ts b/clients/js/src/hooked/pluginHelpers.ts new file mode 100644 index 00000000..47ffc45b --- /dev/null +++ b/clients/js/src/hooked/pluginHelpers.ts @@ -0,0 +1,97 @@ +import { + Authority, + Key, + PluginHeader, + PluginRegistry, + PluginType, + Plugin, + getPluginSerializer, + RegistryRecord, +} from '../generated'; +import { BaseAuthorities, PluginsList } from './types'; +import { mapAuthorities } from './authorityHelpers'; +import { toWords } from './utils'; + +export function formPluginHeader( + pluginRegistryOffset: bigint +): Omit { + return { + key: Key.PluginHeader, + pluginRegistryOffset, + }; +} + +export function formPluginRegistry({ + pluginType, + offset, + authorities, +}: { + pluginType: PluginType; + offset: bigint; + authorities: Authority[]; +}): Omit { + return { + key: Key.PluginRegistry, + registry: [ + { + pluginType, + offset, + authorities, + }, + ], + }; +} + +export function formPluginWithAuthorities({ + authorities, + plugin, +}: { + authorities: Authority[]; + plugin: Plugin; +}) { + return { + authorities, + plugin, + }; +} + +export function mapPluginFields(fields: Array>) { + return fields.reduce((acc2, field) => ({ ...acc2, ...field }), {}); +} + +export function mapPlugin( + plugin: Plugin, + authorities: BaseAuthorities +): PluginsList { + const pluginKey = toWords(plugin.__kind) + .toLowerCase() + .split(' ') + .reduce((s, c) => s + (c.charAt(0).toUpperCase() + c.slice(1))); + + return { + [pluginKey]: { + authorities, + ...('fields' in plugin ? mapPluginFields(plugin.fields) : {}), + }, + }; +} + +export function registryRecordsToPluginsList( + registryRecords: RegistryRecord[], + accountData: Uint8Array +): PluginsList { + return registryRecords.reduce((acc, record) => { + const mappedAuthorities = mapAuthorities(record.authorities); + const deserializedPlugin = getPluginSerializer().deserialize( + accountData, + Number(record.offset) + )[0]; + + acc = { + ...acc, + ...mapPlugin(deserializedPlugin, mappedAuthorities), + }; + + return acc; + }, {}); +} diff --git a/clients/js/src/hooked/types.ts b/clients/js/src/hooked/types.ts new file mode 100644 index 00000000..3be1cbd2 --- /dev/null +++ b/clients/js/src/hooked/types.ts @@ -0,0 +1,43 @@ +import { PublicKey } from '@metaplex-foundation/umi'; +import { + Asset, + Burn, + Freeze, + PluginHeader, + Royalties, + Transfer, + UpdateDelegate, +} from '../generated'; + +export type BaseAuthorities = { + none?: boolean; + owner?: boolean; + update?: boolean; + pubkey?: Array; + permanent?: Array; +}; + +export type BasePlugin = { + authorities: BaseAuthorities; +}; + +export type ReservedPlugin = BasePlugin; +export type RoyaltiesPlugin = BasePlugin & Royalties; +export type FreezePlugin = BasePlugin & Freeze; +export type BurnPlugin = BasePlugin & Burn; +export type TransferPlugin = BasePlugin & Transfer; +export type UpdateDelegatePlugin = BasePlugin & UpdateDelegate; + +export type PluginsList = { + reserved?: ReservedPlugin; + royalties?: RoyaltiesPlugin; + freeze?: FreezePlugin; + burn?: BurnPlugin; + transfer?: TransferPlugin; + updateDelegate?: UpdateDelegatePlugin; +}; + +export type AssetWithPluginsTest = Asset & + PluginsList & { + pluginHeader?: Omit; + }; diff --git a/clients/js/src/hooked/utils.ts b/clients/js/src/hooked/utils.ts new file mode 100644 index 00000000..89787270 --- /dev/null +++ b/clients/js/src/hooked/utils.ts @@ -0,0 +1,4 @@ +export function toWords(str: string) { + const camelCaseRegex = /([a-z0-9])([A-Z])/g; + return str.replace(camelCaseRegex, '$1 $2'); +} diff --git a/clients/js/test/addAuthorityWithTestFetch.ts b/clients/js/test/addAuthorityWithTestFetch.ts new file mode 100644 index 00000000..99dc8d77 --- /dev/null +++ b/clients/js/test/addAuthorityWithTestFetch.ts @@ -0,0 +1,75 @@ +import { generateSigner } from '@metaplex-foundation/umi'; +import test from 'ava'; +// import { base58 } from '@metaplex-foundation/umi/serializers'; +import { + Asset, + DataState, + PluginType, + addPluginAuthority, + addPlugin, + create, + fetchAsset, + updateAuthority, + plugin, + fetchAssetWithPluginsTest, + AssetWithPluginsTest, + formPluginHeader, + getPubkeyAuthority, +} from '../src'; +import { createUmi } from './_setup'; + +test('TEST it can add an authority to a plugin TEST', async (t) => { + // Given a Umi instance and a new signer. + const umi = await createUmi(); + const assetAddress = generateSigner(umi); + const delegateAddress = generateSigner(umi); + + // When we create a new account. + await create(umi, { + dataState: DataState.AccountState, + asset: assetAddress, + name: 'Test Bread', + uri: 'https://example.com/bread', + plugins: [], + }).sendAndConfirm(umi); + + // Then an account was created with the correct data. + const asset = await fetchAsset(umi, assetAddress.publicKey); + t.like(asset, { + publicKey: assetAddress.publicKey, + updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), + owner: umi.identity.publicKey, + name: 'Test Bread', + uri: 'https://example.com/bread', + }); + + await addPlugin(umi, { + asset: assetAddress.publicKey, + plugin: plugin('Freeze', [{ frozen: false }]), + }) + .append( + addPluginAuthority(umi, { + asset: assetAddress.publicKey, + pluginType: PluginType.Freeze, + newAuthority: getPubkeyAuthority(delegateAddress.publicKey), + }) + ) + .sendAndConfirm(umi); + + const asset1 = await fetchAssetWithPluginsTest(umi, assetAddress.publicKey); + t.like(asset1, { + publicKey: assetAddress.publicKey, + updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), + owner: umi.identity.publicKey, + name: 'Test Bread', + uri: 'https://example.com/bread', + pluginHeader: formPluginHeader(BigInt(120)), + freeze: { + authorities: { + owner: true, + pubkey: [delegateAddress.publicKey], + }, + frozen: false, + }, + }); +}); From b1b41839e29b2e60444a6396babcf3639e3499b5 Mon Sep 17 00:00:00 2001 From: shotgunofdeath Date: Thu, 7 Mar 2024 11:43:50 +0100 Subject: [PATCH 2/3] fixup! feat(types): add custom types for authorities, plugins and asset --- clients/js/src/hooked/pluginHelpers.ts | 63 ++++++-------------- clients/js/src/hooked/types.ts | 3 +- clients/js/test/addAuthorityWithTestFetch.ts | 2 +- 3 files changed, 21 insertions(+), 47 deletions(-) diff --git a/clients/js/src/hooked/pluginHelpers.ts b/clients/js/src/hooked/pluginHelpers.ts index 47ffc45b..0da5890c 100644 --- a/clients/js/src/hooked/pluginHelpers.ts +++ b/clients/js/src/hooked/pluginHelpers.ts @@ -1,9 +1,6 @@ import { - Authority, Key, PluginHeader, - PluginRegistry, - PluginType, Plugin, getPluginSerializer, RegistryRecord, @@ -21,48 +18,19 @@ export function formPluginHeader( }; } -export function formPluginRegistry({ - pluginType, - offset, - authorities, -}: { - pluginType: PluginType; - offset: bigint; - authorities: Authority[]; -}): Omit { - return { - key: Key.PluginRegistry, - registry: [ - { - pluginType, - offset, - authorities, - }, - ], - }; -} - -export function formPluginWithAuthorities({ - authorities, - plugin, -}: { - authorities: Authority[]; - plugin: Plugin; -}) { - return { - authorities, - plugin, - }; -} - export function mapPluginFields(fields: Array>) { return fields.reduce((acc2, field) => ({ ...acc2, ...field }), {}); } -export function mapPlugin( - plugin: Plugin, - authorities: BaseAuthorities -): PluginsList { +export function mapPlugin({ + plugin, + authorities, + offset, +}: { + plugin: Exclude; + authorities: BaseAuthorities; + offset: bigint; +}): PluginsList { const pluginKey = toWords(plugin.__kind) .toLowerCase() .split(' ') @@ -71,6 +39,7 @@ export function mapPlugin( return { [pluginKey]: { authorities, + offset, ...('fields' in plugin ? mapPluginFields(plugin.fields) : {}), }, }; @@ -79,17 +48,23 @@ export function mapPlugin( export function registryRecordsToPluginsList( registryRecords: RegistryRecord[], accountData: Uint8Array -): PluginsList { - return registryRecords.reduce((acc, record) => { +) { + return registryRecords.reduce((acc: PluginsList, record) => { const mappedAuthorities = mapAuthorities(record.authorities); const deserializedPlugin = getPluginSerializer().deserialize( accountData, Number(record.offset) )[0]; + if (deserializedPlugin.__kind === 'Reserved') return acc; + acc = { ...acc, - ...mapPlugin(deserializedPlugin, mappedAuthorities), + ...mapPlugin({ + plugin: deserializedPlugin, + authorities: mappedAuthorities, + offset: record.offset, + }), }; return acc; diff --git a/clients/js/src/hooked/types.ts b/clients/js/src/hooked/types.ts index 3be1cbd2..6c6753d5 100644 --- a/clients/js/src/hooked/types.ts +++ b/clients/js/src/hooked/types.ts @@ -19,9 +19,9 @@ export type BaseAuthorities = { export type BasePlugin = { authorities: BaseAuthorities; + offset: bigint; }; -export type ReservedPlugin = BasePlugin; export type RoyaltiesPlugin = BasePlugin & Royalties; export type FreezePlugin = BasePlugin & Freeze; export type BurnPlugin = BasePlugin & Burn; @@ -29,7 +29,6 @@ export type TransferPlugin = BasePlugin & Transfer; export type UpdateDelegatePlugin = BasePlugin & UpdateDelegate; export type PluginsList = { - reserved?: ReservedPlugin; royalties?: RoyaltiesPlugin; freeze?: FreezePlugin; burn?: BurnPlugin; diff --git a/clients/js/test/addAuthorityWithTestFetch.ts b/clients/js/test/addAuthorityWithTestFetch.ts index 99dc8d77..ef117134 100644 --- a/clients/js/test/addAuthorityWithTestFetch.ts +++ b/clients/js/test/addAuthorityWithTestFetch.ts @@ -1,6 +1,5 @@ import { generateSigner } from '@metaplex-foundation/umi'; import test from 'ava'; -// import { base58 } from '@metaplex-foundation/umi/serializers'; import { Asset, DataState, @@ -70,6 +69,7 @@ test('TEST it can add an authority to a plugin TEST', async (t) => { pubkey: [delegateAddress.publicKey], }, frozen: false, + offset: BigInt(118), }, }); }); From 143e99be9028fddad527f86897867e3cf78ae3bc Mon Sep 17 00:00:00 2001 From: shotgunofdeath Date: Sat, 9 Mar 2024 13:34:44 +0100 Subject: [PATCH 3/3] fixup! feat(types): add custom types for authorities, plugins and asset --- clients/js/src/hooked/authorityHelpers.ts | 37 ++-- .../js/src/hooked/fetchAssetWithPlugins.ts | 30 ++- .../src/hooked/fetchAssetWithPluginsTest.ts | 59 ------ .../src/hooked/fetchCollectionWithPlugins.ts | 33 ++- clients/js/src/hooked/index.ts | 14 -- clients/js/src/hooked/pluginHelpers.ts | 14 +- clients/js/src/hooked/types.ts | 14 +- clients/js/test/_setup.ts | 115 +++++----- clients/js/test/addAuthorityWithTestFetch.ts | 75 ------- clients/js/test/addPlugin.test.ts | 56 +++-- clients/js/test/approveAuthority.test.ts | 44 ++-- clients/js/test/create.test.ts | 40 ++-- clients/js/test/createCollection.test.ts | 118 ++++------- .../js/test/plugins/asset/delegate.test.ts | 39 ++-- .../plugins/asset/delegateTransfer.test.ts | 20 +- clients/js/test/plugins/asset/freeze.test.ts | 40 ++-- .../js/test/plugins/asset/royalties.test.ts | 44 ++-- .../collectionUpdateDelegate.test.ts | 30 +-- clients/js/test/removePlugin.test.ts | 48 ++--- clients/js/test/revokeAuthority.test.ts | 199 ++++++------------ clients/js/test/update.test.ts | 74 ++----- 21 files changed, 425 insertions(+), 718 deletions(-) delete mode 100644 clients/js/src/hooked/fetchAssetWithPluginsTest.ts delete mode 100644 clients/js/test/addAuthorityWithTestFetch.ts diff --git a/clients/js/src/hooked/authorityHelpers.ts b/clients/js/src/hooked/authorityHelpers.ts index c15eff4a..4b54f409 100644 --- a/clients/js/src/hooked/authorityHelpers.ts +++ b/clients/js/src/hooked/authorityHelpers.ts @@ -1,6 +1,6 @@ import { PublicKey } from '@metaplex-foundation/umi'; import { Authority, authority as authorityHelper } from '../generated'; -import { BaseAuthorities } from './types'; +import { BaseAuthority } from './types'; import { toWords } from './utils'; // Authorities data helpers @@ -24,27 +24,16 @@ export function getPermanentAuthority(address: PublicKey) { return authorityHelper('Permanent', { address }); } -export function mapAuthorities(authorities: Authority[]) { - return authorities.reduce((acc: BaseAuthorities, authority: Authority) => { - const authorityKey = toWords(authority.__kind) - .split(' ')[0] - .toLowerCase() as keyof BaseAuthorities; - const authorityKeysLen = Object.keys(authority).length; - - if (authorityKeysLen === 1) { - acc = { ...acc, [authorityKey]: true }; - } - - if (authorityKeysLen > 1) { - acc = { - ...acc, - [authorityKey]: [ - ...(acc[authorityKey] ? (acc[authorityKey] as any[]) : []), - ...Object.values(authority).slice(1), - ], - }; - } - - return acc; - }, {}); +export function mapAuthority(authority: Authority): BaseAuthority { + const authorityKey = toWords(authority.__kind) + .split(' ')[0] + .toLowerCase() as keyof BaseAuthority; + + if (Object.keys(authority).length > 1) { + return { + [authorityKey]: Object.values(authority).slice(1), + }; + } + + return { [authorityKey]: true }; } diff --git a/clients/js/src/hooked/fetchAssetWithPlugins.ts b/clients/js/src/hooked/fetchAssetWithPlugins.ts index a09119e9..47a606e2 100644 --- a/clients/js/src/hooked/fetchAssetWithPlugins.ts +++ b/clients/js/src/hooked/fetchAssetWithPlugins.ts @@ -7,18 +7,15 @@ import { publicKey as toPublicKey, } from '@metaplex-foundation/umi'; import { - Asset, PluginHeaderAccountData, PluginRegistryAccountData, deserializeAsset, getAssetAccountDataSerializer, getPluginHeaderAccountDataSerializer, getPluginRegistryAccountDataSerializer, - getPluginSerializer, } from '../generated'; -import { PluginList, PluginWithAuthority } from '.'; - -export type AssetWithPlugins = Asset & PluginList; +import { AssetWithPlugins, PluginsList } from './types'; +import { registryRecordsToPluginsList } from './pluginHelpers'; export async function fetchAssetWithPlugins( context: Pick, @@ -35,31 +32,28 @@ export async function fetchAssetWithPlugins( let pluginHeader: PluginHeaderAccountData | undefined; let pluginRegistry: PluginRegistryAccountData | undefined; - let plugins: PluginWithAuthority[] | undefined; + let pluginsList: PluginsList | undefined; + if (maybeAccount.data.length !== assetData.length) { [pluginHeader] = getPluginHeaderAccountDataSerializer().deserialize( maybeAccount.data, assetData.length ); + [pluginRegistry] = getPluginRegistryAccountDataSerializer().deserialize( maybeAccount.data, Number(pluginHeader.pluginRegistryOffset) ); - plugins = pluginRegistry.registry.map((record) => ({ - plugin: getPluginSerializer().deserialize( - maybeAccount.data, - Number(record.offset) - )[0], - authority: record.authority, - })); + + pluginsList = registryRecordsToPluginsList( + pluginRegistry.registry, + maybeAccount.data + ); } - const assetWithPlugins: AssetWithPlugins = { + return { pluginHeader, - plugins, - pluginRegistry, + ...pluginsList, ...asset, }; - - return assetWithPlugins; } diff --git a/clients/js/src/hooked/fetchAssetWithPluginsTest.ts b/clients/js/src/hooked/fetchAssetWithPluginsTest.ts deleted file mode 100644 index 16a0427f..00000000 --- a/clients/js/src/hooked/fetchAssetWithPluginsTest.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { - Context, - Pda, - PublicKey, - RpcGetAccountOptions, - assertAccountExists, - publicKey as toPublicKey, -} from '@metaplex-foundation/umi'; -import { - PluginHeaderAccountData, - PluginRegistryAccountData, - deserializeAsset, - getAssetAccountDataSerializer, - getPluginHeaderAccountDataSerializer, - getPluginRegistryAccountDataSerializer, -} from '../generated'; -import { AssetWithPluginsTest, PluginsList } from './types'; -import { registryRecordsToPluginsList } from './pluginHelpers'; - -export async function fetchAssetWithPluginsTest( - context: Pick, - publicKey: PublicKey | Pda, - options?: RpcGetAccountOptions -): Promise { - const maybeAccount = await context.rpc.getAccount( - toPublicKey(publicKey, false), - options - ); - assertAccountExists(maybeAccount, 'Asset'); - const asset = deserializeAsset(maybeAccount); - const assetData = getAssetAccountDataSerializer().serialize(asset); - - let pluginHeader: PluginHeaderAccountData | undefined; - let pluginRegistry: PluginRegistryAccountData | undefined; - let pluginsList: PluginsList | undefined; - - if (maybeAccount.data.length !== assetData.length) { - [pluginHeader] = getPluginHeaderAccountDataSerializer().deserialize( - maybeAccount.data, - assetData.length - ); - - [pluginRegistry] = getPluginRegistryAccountDataSerializer().deserialize( - maybeAccount.data, - Number(pluginHeader.pluginRegistryOffset) - ); - - pluginsList = registryRecordsToPluginsList( - pluginRegistry.registry, - maybeAccount.data - ); - } - - return { - pluginHeader, - ...pluginsList, - ...asset, - }; -} diff --git a/clients/js/src/hooked/fetchCollectionWithPlugins.ts b/clients/js/src/hooked/fetchCollectionWithPlugins.ts index 4397935b..2bb2b771 100644 --- a/clients/js/src/hooked/fetchCollectionWithPlugins.ts +++ b/clients/js/src/hooked/fetchCollectionWithPlugins.ts @@ -7,18 +7,18 @@ import { publicKey as toPublicKey, } from '@metaplex-foundation/umi'; import { - Collection, PluginHeaderAccountData, PluginRegistryAccountData, deserializeCollection, getCollectionAccountDataSerializer, getPluginHeaderAccountDataSerializer, getPluginRegistryAccountDataSerializer, - getPluginSerializer, } from '../generated'; -import { PluginList, PluginWithAuthority } from '.'; - -export type CollectionWithPlugins = Collection & PluginList; +import { + CollectionWithPlugins, + PluginsList, + registryRecordsToPluginsList, +} from '.'; export async function fetchCollectionWithPlugins( context: Pick, @@ -36,31 +36,28 @@ export async function fetchCollectionWithPlugins( let pluginHeader: PluginHeaderAccountData | undefined; let pluginRegistry: PluginRegistryAccountData | undefined; - let plugins: PluginWithAuthority[] | undefined; + let pluginsList: PluginsList | undefined; + if (maybeAccount.data.length !== collectionData.length) { [pluginHeader] = getPluginHeaderAccountDataSerializer().deserialize( maybeAccount.data, collectionData.length ); + [pluginRegistry] = getPluginRegistryAccountDataSerializer().deserialize( maybeAccount.data, Number(pluginHeader.pluginRegistryOffset) ); - plugins = pluginRegistry.registry.map((record) => ({ - plugin: getPluginSerializer().deserialize( - maybeAccount.data, - Number(record.offset) - )[0], - authority: record.authority, - })); + + pluginsList = registryRecordsToPluginsList( + pluginRegistry.registry, + maybeAccount.data + ); } - const collectionWithPlugins: CollectionWithPlugins = { + return { pluginHeader, - plugins, - pluginRegistry, + ...pluginsList, ...collection, }; - - return collectionWithPlugins; } diff --git a/clients/js/src/hooked/index.ts b/clients/js/src/hooked/index.ts index a2fbb778..dc2b2f92 100644 --- a/clients/js/src/hooked/index.ts +++ b/clients/js/src/hooked/index.ts @@ -1,19 +1,5 @@ -import { Authority, PluginHeader, PluginRegistry, Plugin } from 'src/generated'; - export * from './fetchAssetWithPlugins'; -export * from './fetchAssetWithPluginsTest'; export * from './fetchCollectionWithPlugins'; export * from './authorityHelpers'; export * from './pluginHelpers'; export * from './types'; - -export type PluginWithAuthority = { - plugin: Plugin; - authority: Authority; -}; - -export type PluginList = { - pluginHeader?: Omit; - plugins?: PluginWithAuthority[]; - pluginRegistry?: Omit; -}; diff --git a/clients/js/src/hooked/pluginHelpers.ts b/clients/js/src/hooked/pluginHelpers.ts index 0da5890c..d87a3a34 100644 --- a/clients/js/src/hooked/pluginHelpers.ts +++ b/clients/js/src/hooked/pluginHelpers.ts @@ -5,8 +5,8 @@ import { getPluginSerializer, RegistryRecord, } from '../generated'; -import { BaseAuthorities, PluginsList } from './types'; -import { mapAuthorities } from './authorityHelpers'; +import { BaseAuthority, PluginsList } from './types'; +import { mapAuthority } from './authorityHelpers'; import { toWords } from './utils'; export function formPluginHeader( @@ -24,11 +24,11 @@ export function mapPluginFields(fields: Array>) { export function mapPlugin({ plugin, - authorities, + authority, offset, }: { plugin: Exclude; - authorities: BaseAuthorities; + authority: BaseAuthority; offset: bigint; }): PluginsList { const pluginKey = toWords(plugin.__kind) @@ -38,7 +38,7 @@ export function mapPlugin({ return { [pluginKey]: { - authorities, + authority, offset, ...('fields' in plugin ? mapPluginFields(plugin.fields) : {}), }, @@ -50,7 +50,7 @@ export function registryRecordsToPluginsList( accountData: Uint8Array ) { return registryRecords.reduce((acc: PluginsList, record) => { - const mappedAuthorities = mapAuthorities(record.authorities); + const mappedAuthority = mapAuthority(record.authority); const deserializedPlugin = getPluginSerializer().deserialize( accountData, Number(record.offset) @@ -62,7 +62,7 @@ export function registryRecordsToPluginsList( ...acc, ...mapPlugin({ plugin: deserializedPlugin, - authorities: mappedAuthorities, + authority: mappedAuthority, offset: record.offset, }), }; diff --git a/clients/js/src/hooked/types.ts b/clients/js/src/hooked/types.ts index 6c6753d5..ac712b67 100644 --- a/clients/js/src/hooked/types.ts +++ b/clients/js/src/hooked/types.ts @@ -2,6 +2,7 @@ import { PublicKey } from '@metaplex-foundation/umi'; import { Asset, Burn, + Collection, Freeze, PluginHeader, Royalties, @@ -9,7 +10,7 @@ import { UpdateDelegate, } from '../generated'; -export type BaseAuthorities = { +export type BaseAuthority = { none?: boolean; owner?: boolean; update?: boolean; @@ -18,8 +19,8 @@ export type BaseAuthorities = { }; export type BasePlugin = { - authorities: BaseAuthorities; - offset: bigint; + authority: BaseAuthority; + offset?: bigint; }; export type RoyaltiesPlugin = BasePlugin & Royalties; @@ -36,7 +37,12 @@ export type PluginsList = { updateDelegate?: UpdateDelegatePlugin; }; -export type AssetWithPluginsTest = Asset & +export type AssetWithPlugins = Asset & + PluginsList & { + pluginHeader?: Omit; + }; + +export type CollectionWithPlugins = Collection & PluginsList & { pluginHeader?: Omit; }; diff --git a/clients/js/test/_setup.ts b/clients/js/test/_setup.ts index 60b08eca..3608c29b 100644 --- a/clients/js/test/_setup.ts +++ b/clients/js/test/_setup.ts @@ -1,7 +1,13 @@ /* eslint-disable import/no-extraneous-dependencies */ import { createUmi as basecreateUmi } from '@metaplex-foundation/umi-bundle-tests'; import { Assertions } from 'ava'; -import { PublicKey, Signer, Umi, generateSigner, publicKey } from '@metaplex-foundation/umi'; +import { + PublicKey, + Signer, + Umi, + generateSigner, + publicKey, +} from '@metaplex-foundation/umi'; import { DataState, Key, @@ -14,7 +20,7 @@ import { CollectionWithPlugins, AssetWithPlugins, UpdateAuthority, - PluginWithAuthority, + PluginsList, } from '../src'; export const createUmi = async () => (await basecreateUmi()).use(mplCore()); @@ -22,35 +28,32 @@ export const createUmi = async () => (await basecreateUmi()).use(mplCore()); export type CreateAssetHelperArgs = { owner?: PublicKey | Signer; payer?: Signer; - asset?: Signer + asset?: Signer; dataState?: DataState; name?: string; uri?: string; authority?: Signer; - updateAuthority?: PublicKey | Signer - collection?: PublicKey + updateAuthority?: PublicKey | Signer; + collection?: PublicKey; // TODO use PluginList type here - plugins?: PluginArgs[] -} + plugins?: PluginArgs[]; +}; export const DEFAULT_ASSET = { name: 'Test Asset', uri: 'https://example.com/asset', -} +}; export const DEFAULT_COLLECTION = { name: 'Test Collection', uri: 'https://example.com/collection', -} +}; -export const createAsset = async ( - umi: Umi, - input: CreateAssetHelperArgs -) => { +export const createAsset = async (umi: Umi, input: CreateAssetHelperArgs) => { const payer = input.payer || umi.identity; const owner = publicKey(input.owner || input.payer || umi.identity); - const asset = input.asset || generateSigner(umi) - const updateAuthority = publicKey(input.updateAuthority || payer) + const asset = input.asset || generateSigner(umi); + const updateAuthority = publicKey(input.updateAuthority || payer); await create(umi, { owner, payer, @@ -61,7 +64,7 @@ export const createAsset = async ( uri: input.uri || DEFAULT_ASSET.uri, plugins: input.plugins || [], collection: input.collection, - authority: input.authority + authority: input.authority, }).sendAndConfirm(umi); return fetchAssetWithPlugins(umi, publicKey(asset)); @@ -69,15 +72,18 @@ export const createAsset = async ( export type CreateCollectionHelperArgs = { payer?: Signer; - collection?: Signer + collection?: Signer; name?: string; uri?: string; - updateAuthority?: PublicKey | Signer - // TODO use CollectionPluginList type here - plugins?: PluginArgs[] -} + updateAuthority?: PublicKey | Signer; + // TODO use CollectionPluginList type here + plugins?: PluginArgs[]; +}; -export const createCollection = async (umi: Umi, input:CreateCollectionHelperArgs) => { +export const createCollection = async ( + umi: Umi, + input: CreateCollectionHelperArgs +) => { const payer = input.payer || umi.identity; const collection = input.collection || generateSigner(umi); const updateAuthority = publicKey(input.updateAuthority || payer); @@ -86,29 +92,37 @@ export const createCollection = async (umi: Umi, input:CreateCollectionHelperArg collection, payer, updateAuthority, - plugins: input.plugins || [] + plugins: input.plugins || [], }).sendAndConfirm(umi); return fetchCollectionWithPlugins(umi, publicKey(collection)); -} +}; export const createAssetWithCollection: ( - umi: Umi, - assetInput: CreateAssetHelperArgs & { collection?: PublicKey | Signer}, + umi: Umi, + assetInput: CreateAssetHelperArgs & { collection?: PublicKey | Signer }, collectionInput?: CreateCollectionHelperArgs -) => Promise<{ asset: AssetWithPlugins; collection: CollectionWithPlugins }> = async (umi, assetInput, collectionInput = {}) => { - const collection = assetInput.collection ? await fetchCollectionWithPlugins(umi, publicKey(assetInput.collection)) : await createCollection(umi, { - payer: assetInput.payer, - updateAuthority: assetInput.updateAuthority, - ...collectionInput +) => Promise<{ + asset: AssetWithPlugins; + collection: CollectionWithPlugins; +}> = async (umi, assetInput, collectionInput = {}) => { + const collection = assetInput.collection + ? await fetchCollectionWithPlugins(umi, publicKey(assetInput.collection)) + : await createCollection(umi, { + payer: assetInput.payer, + updateAuthority: assetInput.updateAuthority, + ...collectionInput, + }); + + const asset = await createAsset(umi, { + ...assetInput, + collection: collection.publicKey, }); - const asset = await createAsset(umi, {...assetInput, collection: collection.publicKey}); - return { asset, - collection - } + collection, + }; }; export const assertAsset = async ( @@ -120,8 +134,7 @@ export const assertAsset = async ( updateAuthority?: UpdateAuthority; name?: string | RegExp; uri?: string | RegExp; - // TODO replace with remapped PluginList type - plugins?: PluginWithAuthority[] + plugins?: PluginsList; } ) => { const assetAddress = publicKey(input.asset); @@ -137,14 +150,14 @@ export const assertAsset = async ( if (typeof uri === 'string') t.is(asset.uri, uri); else if (uri !== undefined) t.regex(asset.uri, uri); - const testObj = { + let testObj = { key: Key.Asset, publicKey: assetAddress, owner, - } + }; if (plugins) { - testObj.plugins = plugins; + testObj = { ...testObj, ...plugins }; } if (updateAuthority) { @@ -152,7 +165,6 @@ export const assertAsset = async ( } t.like(asset, testObj); - }; export const assertCollection = async ( @@ -165,12 +177,11 @@ export const assertCollection = async ( uri?: string | RegExp; numMinted?: number; currentSize?: number; - // TODO replace with remapped PluginList type - plugins?: PluginWithAuthority[] + plugins?: PluginsList; } ) => { const collectionAddress = publicKey(input.collection); - const { name, uri, numMinted, currentSize, updateAuthority } = input; + const { name, uri, numMinted, currentSize, updateAuthority, plugins } = input; const collection = await fetchCollectionWithPlugins(umi, collectionAddress); // Name. @@ -181,25 +192,27 @@ export const assertCollection = async ( if (typeof uri === 'string') t.is(collection.uri, uri); else if (uri !== undefined) t.regex(collection.uri, uri); - const testObj = { + let testObj = { key: Key.Collection, publicKey: collectionAddress, updateAuthority, - plugins: input.plugins || [], + }; + + if (plugins) { + testObj = { ...testObj, ...plugins }; } - if(numMinted !== undefined) { + if (numMinted !== undefined) { testObj.numMinted = numMinted; - }; + } - if(currentSize !== undefined) { + if (currentSize !== undefined) { testObj.currentSize = currentSize; } - if(updateAuthority) { + if (updateAuthority) { testObj.updateAuthority = publicKey(updateAuthority); } t.like(collection, testObj); - }; diff --git a/clients/js/test/addAuthorityWithTestFetch.ts b/clients/js/test/addAuthorityWithTestFetch.ts deleted file mode 100644 index ef117134..00000000 --- a/clients/js/test/addAuthorityWithTestFetch.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { generateSigner } from '@metaplex-foundation/umi'; -import test from 'ava'; -import { - Asset, - DataState, - PluginType, - addPluginAuthority, - addPlugin, - create, - fetchAsset, - updateAuthority, - plugin, - fetchAssetWithPluginsTest, - AssetWithPluginsTest, - formPluginHeader, - getPubkeyAuthority, -} from '../src'; -import { createUmi } from './_setup'; - -test('TEST it can add an authority to a plugin TEST', async (t) => { - // Given a Umi instance and a new signer. - const umi = await createUmi(); - const assetAddress = generateSigner(umi); - const delegateAddress = generateSigner(umi); - - // When we create a new account. - await create(umi, { - dataState: DataState.AccountState, - asset: assetAddress, - name: 'Test Bread', - uri: 'https://example.com/bread', - plugins: [], - }).sendAndConfirm(umi); - - // Then an account was created with the correct data. - const asset = await fetchAsset(umi, assetAddress.publicKey); - t.like(asset, { - publicKey: assetAddress.publicKey, - updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), - owner: umi.identity.publicKey, - name: 'Test Bread', - uri: 'https://example.com/bread', - }); - - await addPlugin(umi, { - asset: assetAddress.publicKey, - plugin: plugin('Freeze', [{ frozen: false }]), - }) - .append( - addPluginAuthority(umi, { - asset: assetAddress.publicKey, - pluginType: PluginType.Freeze, - newAuthority: getPubkeyAuthority(delegateAddress.publicKey), - }) - ) - .sendAndConfirm(umi); - - const asset1 = await fetchAssetWithPluginsTest(umi, assetAddress.publicKey); - t.like(asset1, { - publicKey: assetAddress.publicKey, - updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), - owner: umi.identity.publicKey, - name: 'Test Bread', - uri: 'https://example.com/bread', - pluginHeader: formPluginHeader(BigInt(120)), - freeze: { - authorities: { - owner: true, - pubkey: [delegateAddress.publicKey], - }, - frozen: false, - offset: BigInt(118), - }, - }); -}); diff --git a/clients/js/test/addPlugin.test.ts b/clients/js/test/addPlugin.test.ts index 89bc121f..a23efcdd 100644 --- a/clients/js/test/addPlugin.test.ts +++ b/clients/js/test/addPlugin.test.ts @@ -8,13 +8,21 @@ import { plugin, updateAuthority, } from '../src'; -import { DEFAULT_ASSET, DEFAULT_COLLECTION, assertAsset, assertCollection, createAsset, createCollection, createUmi } from './_setup'; +import { + DEFAULT_ASSET, + DEFAULT_COLLECTION, + assertAsset, + assertCollection, + createAsset, + createCollection, + createUmi, +} from './_setup'; test('it can add a plugin to an asset', async (t) => { // Given a Umi instance and a new signer. const umi = await createUmi(); - - const asset = await createAsset(umi, {}) + + const asset = await createAsset(umi, {}); // Then an account was created with the correct data. await assertAsset(t, umi, { @@ -34,10 +42,14 @@ test('it can add a plugin to an asset', async (t) => { asset: asset.publicKey, owner: umi.identity.publicKey, updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), - plugins: [{ - authority: authority('Owner'), - plugin: plugin('Freeze', [{ frozen: false }]) - }], + plugins: { + freeze: { + authority: { + owner: true, + }, + frozen: false, + }, + }, }); }); @@ -46,7 +58,7 @@ test('it can add a plugin to an asset with a different authority than the defaul const umi = await createUmi(); const delegateAddress = generateSigner(umi); - const asset = await createAsset(umi, {}) + const asset = await createAsset(umi, {}); await assertAsset(t, umi, { ...DEFAULT_ASSET, @@ -66,18 +78,22 @@ test('it can add a plugin to an asset with a different authority than the defaul asset: asset.publicKey, owner: umi.identity.publicKey, updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), - plugins: [{ - authority: authority('Pubkey', { address: delegateAddress.publicKey }), - plugin: plugin('Freeze', [{ frozen: false }]) - }], + plugins: { + freeze: { + authority: { + pubkey: [delegateAddress.publicKey], + }, + frozen: false, + }, + }, }); }); test('it can add a plugin to a collection', async (t) => { // Given a Umi instance and a new signer. const umi = await createUmi(); - - const collection = await createCollection(umi, {}) + + const collection = await createCollection(umi, {}); await assertCollection(t, umi, { ...DEFAULT_COLLECTION, @@ -94,9 +110,13 @@ test('it can add a plugin to a collection', async (t) => { ...DEFAULT_COLLECTION, collection: collection.publicKey, updateAuthority: umi.identity.publicKey, - plugins: [{ - authority: authority('Owner'), - plugin: plugin('Freeze', [{ frozen: false }]) - }], + plugins: { + freeze: { + authority: { + owner: true, + }, + frozen: false, + }, + }, }); }); diff --git a/clients/js/test/approveAuthority.test.ts b/clients/js/test/approveAuthority.test.ts index 41b91028..f42cf98d 100644 --- a/clients/js/test/approveAuthority.test.ts +++ b/clients/js/test/approveAuthority.test.ts @@ -3,16 +3,18 @@ import test from 'ava'; // import { base58 } from '@metaplex-foundation/umi/serializers'; import { Asset, - AssetWithPlugins, DataState, PluginType, approvePluginAuthority, addPlugin, create, fetchAsset, - fetchAssetWithPlugins, updateAuthority, plugin, + fetchAssetWithPlugins, + formPluginHeader, + AssetWithPlugins, + getPubkeyAuthority, } from '../src'; import { createUmi } from './_setup'; @@ -45,16 +47,13 @@ test('it can add an authority to a plugin', async (t) => { await addPlugin(umi, { asset: assetAddress.publicKey, plugin: plugin('Freeze', [{ frozen: false }]), - initAuthority: null + initAuthority: null, }) .append( approvePluginAuthority(umi, { asset: assetAddress.publicKey, pluginType: PluginType.Freeze, - newAuthority: { - __kind: 'Pubkey', - address: delegateAddress.publicKey, - }, + newAuthority: getPubkeyAuthority(delegateAddress.publicKey), }) ) .sendAndConfirm(umi); @@ -67,30 +66,13 @@ test('it can add an authority to a plugin', async (t) => { owner: umi.identity.publicKey, name: 'Test Bread', uri: 'https://example.com/bread', - pluginHeader: { - key: 3, - pluginRegistryOffset: BigInt(120), - }, - pluginRegistry: { - key: 4, - registry: [ - { - pluginType: 2, - offset: BigInt(118), - authority: - { __kind: 'Pubkey', address: delegateAddress.publicKey }, - }, - ], - }, - plugins: [ - { - authority: - { __kind: 'Pubkey', address: delegateAddress.publicKey }, - plugin: { - __kind: 'Freeze', - fields: [{ frozen: false }], - }, + pluginHeader: formPluginHeader(BigInt(120)), + freeze: { + authority: { + pubkey: [delegateAddress.publicKey], }, - ], + offset: BigInt(118), + frozen: false, + }, }); }); diff --git a/clients/js/test/create.test.ts b/clients/js/test/create.test.ts index 8f4d1296..261d2f14 100644 --- a/clients/js/test/create.test.ts +++ b/clients/js/test/create.test.ts @@ -12,6 +12,8 @@ import { fetchHashedAsset, getAssetAccountDataSerializer, updateAuthority, + formPluginHeader, + plugin, } from '../src'; import { DEFAULT_ASSET, assertAsset, createAsset, createUmi } from './_setup'; @@ -22,14 +24,14 @@ test('it can create a new asset in account state', async (t) => { await createAsset(umi, { asset: assetAddress, - }) + }); await assertAsset(t, umi, { ...DEFAULT_ASSET, asset: assetAddress.publicKey, owner: umi.identity, updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), - }) + }); }); test('it cannot create a new asset with a different payer', async (t) => { @@ -54,7 +56,7 @@ test('it cannot create a new asset with a different payer', async (t) => { // console.log("Account State:", asset); t.like(asset, { publicKey: assetAddress.publicKey, - updateAuthority: updateAuthority("Address", [payer.publicKey]), + updateAuthority: updateAuthority('Address', [payer.publicKey]), owner: umi.identity.publicKey, name: 'Test Bread', uri: 'https://example.com/bread', @@ -117,7 +119,6 @@ test('it cannot create a new asset in ledger state because it is not available', await t.throwsAsync(result, { name: 'NotAvailable' }); }); - test('it can create a new asset in account state with plugins', async (t) => { // Given a Umi instance and a new signer. const umi = await createUmi(); @@ -129,7 +130,7 @@ test('it can create a new asset in account state with plugins', async (t) => { asset: assetAddress, name: 'Test Bread', uri: 'https://example.com/bread', - plugins: [{ __kind: 'Freeze', fields: [{ frozen: false }] }], + plugins: [plugin('Freeze', [{ frozen: false }])], }).sendAndConfirm(umi); // Then an account was created with the correct data. @@ -141,29 +142,14 @@ test('it can create a new asset in account state with plugins', async (t) => { owner: umi.identity.publicKey, name: 'Test Bread', uri: 'https://example.com/bread', - pluginHeader: { - key: 3, - pluginRegistryOffset: BigInt(120), - }, - pluginRegistry: { - key: 4, - registry: [ - { - pluginType: 2, - offset: BigInt(118), - authority: { __kind: 'Owner' }, - }, - ], - }, - plugins: [ - { - authority: { __kind: 'Owner' }, - plugin: { - __kind: 'Freeze', - fields: [{ frozen: false }], - }, + pluginHeader: formPluginHeader(BigInt(120)), + freeze: { + authority: { + owner: true, }, - ], + offset: BigInt(118), + frozen: false, + }, }); }); diff --git a/clients/js/test/createCollection.test.ts b/clients/js/test/createCollection.test.ts index 709b55c5..702b539c 100644 --- a/clients/js/test/createCollection.test.ts +++ b/clients/js/test/createCollection.test.ts @@ -14,8 +14,17 @@ import { fetchCollectionWithPlugins, plugin, updateAuthority, + formPluginHeader, } from '../src'; -import { DEFAULT_ASSET, DEFAULT_COLLECTION, assertAsset, assertCollection, createAssetWithCollection, createCollection, createUmi } from './_setup'; +import { + DEFAULT_ASSET, + DEFAULT_COLLECTION, + assertAsset, + assertCollection, + createAssetWithCollection, + createCollection, + createUmi, +} from './_setup'; test('it can create a new collection', async (t) => { // Given a Umi instance and a new signer. @@ -24,13 +33,13 @@ test('it can create a new collection', async (t) => { await createCollection(umi, { collection: collectionAddress, - }) + }); await assertCollection(t, umi, { ...DEFAULT_COLLECTION, collection: collectionAddress, updateAuthority: umi.identity.publicKey, - }) + }); }); test('it can create a new collection with plugins', async (t) => { @@ -43,7 +52,7 @@ test('it can create a new collection with plugins', async (t) => { collection: collectionAddress, name: 'Test Bread Collection', uri: 'https://example.com/bread', - plugins: [{ __kind: 'Freeze', fields: [{ frozen: false }] }], + plugins: [plugin('Freeze', [{ frozen: false }])], }).sendAndConfirm(umi); // Then an account was created with the correct data. @@ -57,59 +66,47 @@ test('it can create a new collection with plugins', async (t) => { updateAuthority: umi.identity.publicKey, name: 'Test Bread Collection', uri: 'https://example.com/bread', - pluginHeader: { - key: 3, - pluginRegistryOffset: BigInt(106), - }, - pluginRegistry: { - key: 4, - registry: [ - { - pluginType: 2, - offset: BigInt(104), - authority: { __kind: 'Owner' }, - }, - ], - }, - plugins: [ - { - authority: { __kind: 'Owner' }, - plugin: { - __kind: 'Freeze', - fields: [{ frozen: false }], - }, + pluginHeader: formPluginHeader(BigInt(106)), + freeze: { + authority: { + owner: true, }, - ], + offset: BigInt(104), + frozen: false, + }, }); }); test('it can create a new asset with a collection', async (t) => { // Given a Umi instance and a new signer. const umi = await createUmi(); - const { asset, collection} = await createAssetWithCollection(umi, { - }, { - plugins: [plugin('UpdateDelegate', [{}])], - }) + const { asset, collection } = await createAssetWithCollection( + umi, + {}, + { + plugins: [plugin('UpdateDelegate', [{}])], + } + ); await assertCollection(t, umi, { ...DEFAULT_COLLECTION, collection: collection.publicKey, updateAuthority: umi.identity.publicKey, - plugins: [{ - authority: authority('UpdateAuthority'), - plugin: plugin('UpdateDelegate', [{}]) - }] - }) + plugins: { + updateDelegate: { + authority: { + update: true, + }, + }, + }, + }); await assertAsset(t, umi, { ...DEFAULT_ASSET, asset: asset.publicKey, owner: umi.identity.publicKey, updateAuthority: updateAuthority('Collection', [collection.publicKey]), - }) - - t.assert(asset.pluginRegistry?.registry.length === 0); - t.assert(asset.plugins?.length === 0); + }); }); test('it can create a new asset with a collection with collection delegate', async (t) => { @@ -130,7 +127,7 @@ test('it can create a new asset with a collection with collection delegate', asy await addCollectionPlugin(umi, { collection: collectionAddress.publicKey, plugin: plugin('UpdateDelegate', [{}]), - initAuthority: null + initAuthority: null, }).sendAndConfirm(umi); await approveCollectionPluginAuthority(umi, { @@ -139,7 +136,6 @@ test('it can create a new asset with a collection with collection delegate', asy newAuthority: authority('Pubkey', { address: delegate.publicKey }), }).sendAndConfirm(umi); - // When we create a new account. await create(umi, { dataState: DataState.AccountState, @@ -162,17 +158,8 @@ test('it can create a new asset with a collection with collection delegate', asy owner: umi.identity.publicKey, name: 'Test Bread', uri: 'https://example.com/bread', - pluginHeader: { - key: 3, - pluginRegistryOffset: BigInt(118), - }, - pluginRegistry: { - key: 4, - }, + pluginHeader: formPluginHeader(BigInt(118)), }); - - t.assert(asset.pluginRegistry?.registry.length === 0); - t.assert(asset.plugins?.length === 0); }); // TODO: Add test @@ -206,29 +193,14 @@ test('it cannot create a new asset with a collection if it is not the collection updateAuthority: collectionAuth.publicKey, name: 'Test Bread Collection', uri: 'https://example.com/bread', - pluginHeader: { - key: 3, - pluginRegistryOffset: BigInt(106), - }, - pluginRegistry: { - key: 4, - registry: [ - { - pluginType: 2, - offset: BigInt(104), - authority: { __kind: 'Owner' }, - }, - ], - }, - plugins: [ - { - authority: { __kind: 'Owner' }, - plugin: { - __kind: 'Freeze', - fields: [{ frozen: false }], - }, + pluginHeader: formPluginHeader(BigInt(106)), + freeze: { + authority: { + owner: true, }, - ], + offset: BigInt(104), + frozen: false, + }, }); // When we create a new account. diff --git a/clients/js/test/plugins/asset/delegate.test.ts b/clients/js/test/plugins/asset/delegate.test.ts index e54a4986..a9a7a4ec 100644 --- a/clients/js/test/plugins/asset/delegate.test.ts +++ b/clients/js/test/plugins/asset/delegate.test.ts @@ -8,7 +8,12 @@ import { plugin, authority, } from '../../../src'; -import { DEFAULT_ASSET, assertAsset, createAsset, createUmi } from '../../_setup'; +import { + DEFAULT_ASSET, + assertAsset, + createAsset, + createUmi, +} from '../../_setup'; test('it can delegate a new authority', async (t) => { // Given a Umi instance and a new signer. @@ -17,7 +22,7 @@ test('it can delegate a new authority', async (t) => { const asset = await createAsset(umi, { plugins: [plugin('Freeze', [{ frozen: false }])], - }) + }); await approvePluginAuthority(umi, { asset: asset.publicKey, @@ -30,12 +35,16 @@ test('it can delegate a new authority', async (t) => { asset: asset.publicKey, owner: umi.identity.publicKey, updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), - plugins: [{ - authority: authority('Pubkey', { address: delegateAddress.publicKey }), - plugin: plugin('Freeze', [{ frozen: false }]) - }], + plugins: { + freeze: { + authority: { + pubkey: [delegateAddress.publicKey], + }, + frozen: false, + }, + }, }); -}) +}); test('a delegate can freeze the token', async (t) => { // Given a Umi instance and a new signer. @@ -44,7 +53,7 @@ test('a delegate can freeze the token', async (t) => { const asset = await createAsset(umi, { plugins: [plugin('Freeze', [{ frozen: false }])], - }) + }); await approvePluginAuthority(umi, { asset: asset.publicKey, @@ -52,7 +61,7 @@ test('a delegate can freeze the token', async (t) => { newAuthority: authority('Pubkey', { address: delegateAddress.publicKey }), }).sendAndConfirm(umi); - const umi2 = await createUmi() + const umi2 = await createUmi(); await updatePlugin(umi2, { asset: asset.publicKey, authority: delegateAddress, @@ -64,9 +73,13 @@ test('a delegate can freeze the token', async (t) => { asset: asset.publicKey, owner: umi.identity.publicKey, updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), - plugins: [{ - authority: authority('Pubkey', { address: delegateAddress.publicKey }), - plugin: plugin('Freeze', [{ frozen: true }]) - }], + plugins: { + freeze: { + authority: { + pubkey: [delegateAddress.publicKey], + }, + frozen: true, + }, + }, }); }); diff --git a/clients/js/test/plugins/asset/delegateTransfer.test.ts b/clients/js/test/plugins/asset/delegateTransfer.test.ts index 7957bc4b..6bfe6b0b 100644 --- a/clients/js/test/plugins/asset/delegateTransfer.test.ts +++ b/clients/js/test/plugins/asset/delegateTransfer.test.ts @@ -8,7 +8,12 @@ import { plugin, authority, } from '../../../src'; -import { DEFAULT_ASSET, assertAsset, createAsset, createUmi } from '../../_setup'; +import { + DEFAULT_ASSET, + assertAsset, + createAsset, + createUmi, +} from '../../_setup'; test('a delegate can transfer the asset', async (t) => { // Given a Umi instance and a new signer. @@ -18,7 +23,7 @@ test('a delegate can transfer the asset', async (t) => { const asset = await createAsset(umi, { plugins: [plugin('Transfer', [{}])], - }) + }); await approvePluginAuthority(umi, { asset: asset.publicKey, @@ -37,9 +42,12 @@ test('a delegate can transfer the asset', async (t) => { asset: asset.publicKey, owner: newOwnerAddress.publicKey, updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), - plugins: [{ - authority: authority('Pubkey', { address: delegateAddress.publicKey }), - plugin: plugin('Transfer', [{}]) - }], + plugins: { + transfer: { + authority: { + pubkey: [delegateAddress.publicKey], + }, + }, + }, }); }); diff --git a/clients/js/test/plugins/asset/freeze.test.ts b/clients/js/test/plugins/asset/freeze.test.ts index 0604f09e..1c669027 100644 --- a/clients/js/test/plugins/asset/freeze.test.ts +++ b/clients/js/test/plugins/asset/freeze.test.ts @@ -1,18 +1,17 @@ import test from 'ava'; +import { addPlugin, plugin, updateAuthority, updatePlugin } from '../../../src'; import { - addPlugin, - authority, - plugin, - updateAuthority, - updatePlugin, -} from '../../../src'; -import { DEFAULT_ASSET, assertAsset, createAsset, createUmi } from '../../_setup'; + DEFAULT_ASSET, + assertAsset, + createAsset, + createUmi, +} from '../../_setup'; test('it can freeze and unfreeze an asset', async (t) => { // Given a Umi instance and a new signer. const umi = await createUmi(); - const asset = await createAsset(umi, {}) + const asset = await createAsset(umi, {}); await addPlugin(umi, { asset: asset.publicKey, @@ -24,10 +23,14 @@ test('it can freeze and unfreeze an asset', async (t) => { asset: asset.publicKey, owner: umi.identity.publicKey, updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), - plugins: [{ - authority: authority('Owner'), - plugin: plugin('Freeze', [{ frozen: true }]) - }], + plugins: { + freeze: { + authority: { + owner: true, + }, + frozen: true, + }, + }, }); await updatePlugin(umi, { @@ -40,10 +43,13 @@ test('it can freeze and unfreeze an asset', async (t) => { asset: asset.publicKey, owner: umi.identity.publicKey, updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), - plugins: [{ - authority: authority('Owner'), - plugin: plugin('Freeze', [{ frozen: false }]) - }], + plugins: { + freeze: { + authority: { + owner: true, + }, + frozen: false, + }, + }, }); }); - diff --git a/clients/js/test/plugins/asset/royalties.test.ts b/clients/js/test/plugins/asset/royalties.test.ts index bfd95b0b..abdaf641 100644 --- a/clients/js/test/plugins/asset/royalties.test.ts +++ b/clients/js/test/plugins/asset/royalties.test.ts @@ -6,7 +6,6 @@ import { } from '@metaplex-foundation/mpl-toolbox'; import { MPL_CORE_PROGRAM_ID, - authority, plugin, ruleSet, transfer, @@ -43,18 +42,16 @@ test('it can transfer an asset with royalties', async (t) => { asset: asset.publicKey, owner: umi.identity.publicKey, updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), - plugins: [ - { - authority: authority('UpdateAuthority'), - plugin: plugin('Royalties', [ - { - percentage: 5, - creators: [{ address: umi.identity.publicKey, percentage: 100 }], - ruleSet: ruleSet('None'), - }, - ]), + plugins: { + royalties: { + authority: { + update: true, + }, + percentage: 5, + creators: [{ address: umi.identity.publicKey, percentage: 100 }], + ruleSet: ruleSet('None'), }, - ], + }, }); await transfer(umi, { @@ -93,18 +90,16 @@ test('it can transfer an asset with royalties to an allowlisted program address' asset: asset.publicKey, owner: umi.identity.publicKey, updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), - plugins: [ - { - authority: authority('UpdateAuthority'), - plugin: plugin('Royalties', [ - { - percentage: 5, - creators: [{ address: umi.identity.publicKey, percentage: 100 }], - ruleSet: ruleSet('ProgramAllowList', [[MPL_CORE_PROGRAM_ID]]), - }, - ]), + plugins: { + royalties: { + authority: { + update: true, + }, + percentage: 5, + creators: [{ address: umi.identity.publicKey, percentage: 100 }], + ruleSet: ruleSet('ProgramAllowList', [[MPL_CORE_PROGRAM_ID]]), }, - ], + }, }); await transfer(umi, { @@ -117,7 +112,6 @@ test('it can transfer an asset with royalties to an allowlisted program address' asset: asset.publicKey, owner: programOwned.publicKey, }); - }); test('it cannot transfer an asset with royalties to a program address not on the allowlist', async (t) => { @@ -212,7 +206,7 @@ test('it cannot transfer an asset with royalties to a denylisted program', async // Here we're creating a new owner that's program owned, so we're just going to use another asset. const programOwned = await createAsset(umi, {}); - + // Creating a new asset to transfer. const asset = await createAsset(umi, { plugins: [ diff --git a/clients/js/test/plugins/collection/collectionUpdateDelegate.test.ts b/clients/js/test/plugins/collection/collectionUpdateDelegate.test.ts index 05de2c69..c41c7b72 100644 --- a/clients/js/test/plugins/collection/collectionUpdateDelegate.test.ts +++ b/clients/js/test/plugins/collection/collectionUpdateDelegate.test.ts @@ -8,7 +8,15 @@ import { plugin, authority, } from '../../../src'; -import { DEFAULT_ASSET, DEFAULT_COLLECTION, assertAsset, assertCollection, createAsset, createCollection, createUmi } from '../../_setup'; +import { + DEFAULT_ASSET, + DEFAULT_COLLECTION, + assertAsset, + assertCollection, + createAsset, + createCollection, + createUmi, +} from '../../_setup'; test('it can create a new asset with a collection if it is the collection update delegate', async (t) => { // Given a Umi instance and a new signer. @@ -17,8 +25,8 @@ test('it can create a new asset with a collection if it is the collection update // When we create a new account. const collection = await createCollection(umi, { - plugins: [plugin('UpdateDelegate', [{}])] - }) + plugins: [plugin('UpdateDelegate', [{}])], + }); await approveCollectionPluginAuthority(umi, { collection: collection.publicKey, @@ -30,12 +38,13 @@ test('it can create a new asset with a collection if it is the collection update ...DEFAULT_COLLECTION, collection: collection.publicKey, updateAuthority: umi.identity.publicKey, - plugins: [ - { - authority: authority('Pubkey', { address: updateDelegate.publicKey }), - plugin: plugin('UpdateDelegate', [{}]), + plugins: { + updateDelegate: { + authority: { + pubkey: [updateDelegate.publicKey], + }, }, - ], + }, }); umi.identity = updateDelegate; @@ -46,7 +55,7 @@ test('it can create a new asset with a collection if it is the collection update collection: collection.publicKey, owner, authority: updateDelegate, - }) + }); await assertAsset(t, umi, { ...DEFAULT_ASSET, @@ -54,7 +63,4 @@ test('it can create a new asset with a collection if it is the collection update owner: owner.publicKey, updateAuthority: updateAuthority('Collection', [collection.publicKey]), }); - - t.assert(asset.pluginRegistry?.registry.length === 0); - t.assert(asset.plugins?.length === 0); }); diff --git a/clients/js/test/removePlugin.test.ts b/clients/js/test/removePlugin.test.ts index 89f24173..7399c883 100644 --- a/clients/js/test/removePlugin.test.ts +++ b/clients/js/test/removePlugin.test.ts @@ -12,6 +12,8 @@ import { fetchAssetWithPlugins, removePlugin, updateAuthority, + formPluginHeader, + plugin, } from '../src'; import { createUmi } from './_setup'; @@ -42,11 +44,8 @@ test('it can remove a plugin from an asset', async (t) => { await addPlugin(umi, { asset: assetAddress.publicKey, - plugin: { - __kind: 'Freeze', - fields: [{ frozen: false }], - }, - initAuthority: null + plugin: plugin('Freeze', [{ frozen: false }]), + initAuthority: null, }).sendAndConfirm(umi); const asset1 = await fetchAssetWithPlugins(umi, assetAddress.publicKey); @@ -57,29 +56,14 @@ test('it can remove a plugin from an asset', async (t) => { owner: umi.identity.publicKey, name: 'Test Bread', uri: 'https://example.com/bread', - pluginHeader: { - key: 3, - pluginRegistryOffset: BigInt(120), - }, - pluginRegistry: { - key: 4, - registry: [ - { - pluginType: 2, - offset: BigInt(118), - authority: { __kind: 'Owner' }, - }, - ], - }, - plugins: [ - { - authority: { __kind: 'Owner' }, - plugin: { - __kind: 'Freeze', - fields: [{ frozen: false }], - }, + pluginHeader: formPluginHeader(BigInt(120)), + freeze: { + authority: { + owner: true, }, - ], + offset: BigInt(118), + frozen: false, + }, }); await removePlugin(umi, { @@ -95,14 +79,6 @@ test('it can remove a plugin from an asset', async (t) => { owner: umi.identity.publicKey, name: 'Test Bread', uri: 'https://example.com/bread', - pluginHeader: { - key: 3, - pluginRegistryOffset: BigInt(118), - }, - pluginRegistry: { - key: 4, - registry: [], - }, - plugins: [], + pluginHeader: formPluginHeader(BigInt(118)), })); }); diff --git a/clients/js/test/revokeAuthority.test.ts b/clients/js/test/revokeAuthority.test.ts index 1eb98bff..c9257869 100644 --- a/clients/js/test/revokeAuthority.test.ts +++ b/clients/js/test/revokeAuthority.test.ts @@ -15,6 +15,9 @@ import { plugin, revokePluginAuthority, updateAuthority, + getPubkeyAuthority, + formPluginHeader, + getNoneAuthority, } from '../src'; import { createUmi } from './_setup'; @@ -46,20 +49,14 @@ test('it can remove an authority from a plugin', async (t) => { await addPlugin(umi, { asset: assetAddress.publicKey, - plugin: { - __kind: 'Freeze', - fields: [{ frozen: false }], - }, - initAuthority: null + plugin: plugin('Freeze', [{ frozen: false }]), + initAuthority: null, }) .append( approvePluginAuthority(umi, { asset: assetAddress.publicKey, pluginType: PluginType.Freeze, - newAuthority: { - __kind: 'Pubkey', - address: delegateAddress.publicKey, - }, + newAuthority: getPubkeyAuthority(delegateAddress.publicKey), }) ) .sendAndConfirm(umi); @@ -72,31 +69,14 @@ test('it can remove an authority from a plugin', async (t) => { owner: umi.identity.publicKey, name: 'Test Bread', uri: 'https://example.com/bread', - pluginHeader: { - key: 3, - pluginRegistryOffset: BigInt(120), - }, - pluginRegistry: { - key: 4, - registry: [ - { - pluginType: 2, - offset: BigInt(118), - authority: - { __kind: 'Pubkey', address: delegateAddress.publicKey }, - }, - ], - }, - plugins: [ - { - authority: - { __kind: 'Pubkey', address: delegateAddress.publicKey }, - plugin: { - __kind: 'Freeze', - fields: [{ frozen: false }], - }, + pluginHeader: formPluginHeader(BigInt(120)), + freeze: { + authority: { + pubkey: [delegateAddress.publicKey], }, - ], + offset: BigInt(118), + frozen: false, + }, }); await revokePluginAuthority(umi, { @@ -112,29 +92,14 @@ test('it can remove an authority from a plugin', async (t) => { owner: umi.identity.publicKey, name: 'Test Bread', uri: 'https://example.com/bread', - pluginHeader: { - key: 3, - pluginRegistryOffset: BigInt(120), - }, - pluginRegistry: { - key: 4, - registry: [ - { - pluginType: 2, - offset: BigInt(118), - authority: { __kind: 'Owner' }, - }, - ], - }, - plugins: [ - { - authority: { __kind: 'Owner' }, - plugin: { - __kind: 'Freeze', - fields: [{ frozen: false }], - }, + pluginHeader: formPluginHeader(BigInt(120)), + freeze: { + authority: { + owner: true, }, - ], + offset: BigInt(118), + frozen: false, + }, }); }); @@ -169,15 +134,13 @@ test('it can remove the default authority from a plugin to make it immutable', a __kind: 'Freeze', fields: [{ frozen: false }], }, - initAuthority: null + initAuthority: null, }).sendAndConfirm(umi); await approvePluginAuthority(umi, { asset: assetAddress.publicKey, pluginType: PluginType.Freeze, - newAuthority: { - __kind: 'None' - } + newAuthority: getNoneAuthority(), }).sendAndConfirm(umi); const asset1 = await fetchAssetWithPlugins(umi, assetAddress.publicKey); @@ -188,29 +151,14 @@ test('it can remove the default authority from a plugin to make it immutable', a owner: umi.identity.publicKey, name: 'Test Bread', uri: 'https://example.com/bread', - pluginHeader: { - key: 3, - pluginRegistryOffset: BigInt(120), - }, - pluginRegistry: { - key: 4, - registry: [ - { - pluginType: 2, - offset: BigInt(118), - authority: { __kind: 'None' }, - }, - ], - }, - plugins: [ - { - authority: { __kind: 'None' }, - plugin: { - __kind: 'Freeze', - fields: [{ frozen: false }], - }, + pluginHeader: formPluginHeader(BigInt(120)), + freeze: { + authority: { + none: true, }, - ], + offset: BigInt(118), + frozen: false, + }, }); }); @@ -232,16 +180,13 @@ test('it can remove a pubkey authority from a plugin if that pubkey is the signe await addPlugin(umi, { asset: assetAddress.publicKey, plugin: plugin('Freeze', [{ frozen: false }]), - initAuthority: null + initAuthority: null, }).sendAndConfirm(umi); await approvePluginAuthority(umi, { asset: assetAddress.publicKey, pluginType: PluginType.Freeze, - newAuthority: { - __kind: 'Pubkey', - address: pubkeyAuth.publicKey, - }, + newAuthority: getPubkeyAuthority(pubkeyAuth.publicKey), }).sendAndConfirm(umi); const asset1 = await fetchAssetWithPlugins(umi, assetAddress.publicKey); @@ -252,16 +197,12 @@ test('it can remove a pubkey authority from a plugin if that pubkey is the signe owner: umi.identity.publicKey, name: 'Test Bread', uri: 'https://example.com/bread', - plugins: [ - { - authority: - { __kind: 'Pubkey', address: pubkeyAuth.publicKey }, - plugin: { - __kind: 'Freeze', - fields: [{ frozen: false }], - }, + freeze: { + authority: { + pubkey: [pubkeyAuth.publicKey], }, - ], + frozen: false, + }, }); const umi2 = await createUmi(); @@ -281,15 +222,12 @@ test('it can remove a pubkey authority from a plugin if that pubkey is the signe owner: umi.identity.publicKey, name: 'Test Bread', uri: 'https://example.com/bread', - plugins: [ - { - authority: { __kind: 'Owner' }, - plugin: { - __kind: 'Freeze', - fields: [{ frozen: false }], - }, + freeze: { + authority: { + owner: true, }, - ], + frozen: false, + }, }); }); @@ -311,16 +249,13 @@ test('it can remove a owner authority from a plugin with other authority', async await addPlugin(umi, { asset: assetAddress.publicKey, plugin: plugin('Freeze', [{ frozen: false }]), - initAuthority: null + initAuthority: null, }).sendAndConfirm(umi); await approvePluginAuthority(umi, { asset: assetAddress.publicKey, pluginType: PluginType.Freeze, - newAuthority: { - __kind: 'Pubkey', - address: pubkeyAuth.publicKey, - }, + newAuthority: getPubkeyAuthority(pubkeyAuth.publicKey), }).sendAndConfirm(umi); const asset1 = await fetchAssetWithPlugins(umi, assetAddress.publicKey); @@ -331,16 +266,12 @@ test('it can remove a owner authority from a plugin with other authority', async owner: umi.identity.publicKey, name: 'Test Bread', uri: 'https://example.com/bread', - plugins: [ - { - authority: - { __kind: 'Pubkey', address: pubkeyAuth.publicKey }, - plugin: { - __kind: 'Freeze', - fields: [{ frozen: false }], - }, + freeze: { + authority: { + pubkey: [pubkeyAuth.publicKey], }, - ], + frozen: false, + }, }); await revokePluginAuthority(umi, { @@ -358,16 +289,12 @@ test('it can remove a owner authority from a plugin with other authority', async owner: umi.identity.publicKey, name: 'Test Bread', uri: 'https://example.com/bread', - plugins: [ - { - authority: - { __kind: 'Owner' }, - plugin: { - __kind: 'Freeze', - fields: [{ frozen: false }], - }, + freeze: { + authority: { + owner: true, }, - ], + frozen: false, + }, }); }); @@ -388,7 +315,7 @@ test('it cannot remove a none authority from a plugin', async (t) => { await addPlugin(umi, { asset: assetAddress.publicKey, plugin: plugin('Freeze', [{ frozen: false }]), - initAuthority: null + initAuthority: null, }).sendAndConfirm(umi); await approvePluginAuthority(umi, { @@ -396,17 +323,17 @@ test('it cannot remove a none authority from a plugin', async (t) => { asset: assetAddress.publicKey, authority: umi.identity, pluginType: PluginType.Freeze, - newAuthority: { - __kind: 'None' - } + newAuthority: getNoneAuthority(), }).sendAndConfirm(umi); - const err = await t.throwsAsync(() => revokePluginAuthority(umi, { - payer: umi.identity, - asset: assetAddress.publicKey, - authority: umi.identity, - pluginType: PluginType.Freeze, - }).sendAndConfirm(umi)); + const err = await t.throwsAsync(() => + revokePluginAuthority(umi, { + payer: umi.identity, + asset: assetAddress.publicKey, + authority: umi.identity, + pluginType: PluginType.Freeze, + }).sendAndConfirm(umi) + ); t.true(err?.message.startsWith('Invalid Authority')); }); diff --git a/clients/js/test/update.test.ts b/clients/js/test/update.test.ts index dafe8b40..9bbe885b 100644 --- a/clients/js/test/update.test.ts +++ b/clients/js/test/update.test.ts @@ -10,6 +10,8 @@ import { fetchAssetWithPlugins, update, updateAuthority, + plugin, + formPluginHeader, } from '../src'; import { createUmi } from './_setup'; @@ -91,11 +93,8 @@ test('it can update an asset with plugins to be larger', async (t) => { await addPlugin(umi, { asset: assetAddress.publicKey, - plugin: { - __kind: 'Freeze', - fields: [{ frozen: false }], - }, - initAuthority: null + plugin: plugin('Freeze', [{ frozen: false }]), + initAuthority: null, }).sendAndConfirm(umi); await update(umi, { @@ -113,29 +112,14 @@ test('it can update an asset with plugins to be larger', async (t) => { owner: umi.identity.publicKey, name: 'Test Bread 2', uri: 'https://example.com/bread2', - pluginHeader: { - key: 3, - pluginRegistryOffset: BigInt(123), - }, - pluginRegistry: { - key: 4, - registry: [ - { - pluginType: 2, - offset: BigInt(121), - authority: { __kind: 'Owner' }, - }, - ], - }, - plugins: [ - { - authority: { __kind: 'Owner' }, - plugin: { - __kind: 'Freeze', - fields: [{ frozen: false }], - }, + pluginHeader: formPluginHeader(BigInt(123)), + freeze: { + authority: { + owner: true, }, - ], + offset: BigInt(121), + frozen: false, + }, }); }); @@ -155,11 +139,8 @@ test('it can update an asset with plugins to be smaller', async (t) => { await addPlugin(umi, { asset: assetAddress.publicKey, - plugin: { - __kind: 'Freeze', - fields: [{ frozen: false }], - }, - initAuthority: null + plugin: plugin('Freeze', [{ frozen: false }]), + initAuthority: null, }).sendAndConfirm(umi); await update(umi, { @@ -176,28 +157,13 @@ test('it can update an asset with plugins to be smaller', async (t) => { owner: umi.identity.publicKey, name: '', uri: '', - pluginHeader: { - key: 3, - pluginRegistryOffset: BigInt(85), - }, - pluginRegistry: { - key: 4, - registry: [ - { - pluginType: 2, - offset: BigInt(83), - authority: { __kind: 'Owner' }, - }, - ], - }, - plugins: [ - { - authority: { __kind: 'Owner' }, - plugin: { - __kind: 'Freeze', - fields: [{ frozen: false }], - }, + pluginHeader: formPluginHeader(BigInt(85)), + freeze: { + authority: { + owner: true, }, - ], + offset: BigInt(83), + frozen: false, + }, }); });