Skip to content

Commit

Permalink
refactor plugin authoirty mapping, refactor revoke, remove plugin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nhanphan committed Mar 11, 2024
1 parent b66c20d commit 7741c09
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 314 deletions.
16 changes: 4 additions & 12 deletions clients/js/src/hooked/authorityHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { PublicKey } from '@metaplex-foundation/umi';
import { Authority, authority as authorityHelper } from '../generated';
import { BaseAuthority } from './types';
import { toWords } from './utils';

// Authorities data helpers
export function getNoneAuthority() {
Expand All @@ -25,15 +24,8 @@ export function getPermanentAuthority(address: PublicKey) {
}

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 };
return {
type: authority.__kind,
address: (authority as any).address,
};
}
10 changes: 5 additions & 5 deletions clients/js/src/hooked/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PublicKey } from '@metaplex-foundation/umi';
import {
Asset,
Authority,
Burn,
Collection,
Freeze,
Expand All @@ -11,13 +12,12 @@ import {
} from '../generated';

export type BaseAuthority = {
none?: boolean;
owner?: boolean;
update?: boolean;
pubkey?: Array<PublicKey>;
permanent?: Array<PublicKey>;
type: PluginAuthorityType
address?: PublicKey
};

export type PluginAuthorityType = Pick<Authority, '__kind'>['__kind'];

export type BasePlugin = {
authority: BaseAuthority;
offset?: bigint;
Expand Down
7 changes: 4 additions & 3 deletions clients/js/test/addPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test('it can add a plugin to an asset', async (t) => {
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
freeze: {
authority: {
owner: true,
type: 'Owner',
},
frozen: false,
},
Expand Down Expand Up @@ -79,7 +79,8 @@ test('it can add a plugin to an asset with a different authority than the defaul
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
freeze: {
authority: {
pubkey: [delegateAddress.publicKey],
type: 'Pubkey',
address: delegateAddress.publicKey,
},
frozen: false,
},
Expand Down Expand Up @@ -115,7 +116,7 @@ test('it can add a plugin to a collection', async (t) => {
updateAuthority: umi.identity.publicKey,
royalties: {
authority: {
update: true,
type: 'UpdateAuthority',
},
percentage: 5,
creators: [],
Expand Down
3 changes: 2 additions & 1 deletion clients/js/test/approveAuthority.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ test('it can add an authority to a plugin', async (t) => {
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
freeze: {
authority: {
pubkey: [delegateAddress.publicKey],
type: 'Pubkey',
address: delegateAddress.publicKey,
},
frozen: false,
},
Expand Down
4 changes: 2 additions & 2 deletions clients/js/test/burn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ test('it cannot burn an asset if it is frozen', async (t) => {
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
freeze: {
authority: {
owner: true,
type: 'Owner',
},
frozen: true,
},
Expand All @@ -97,7 +97,7 @@ test('it cannot burn an asset if it is frozen', async (t) => {
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
freeze: {
authority: {
owner: true,
type: 'Owner',
},
frozen: true,
},
Expand Down
2 changes: 1 addition & 1 deletion clients/js/test/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ test('it can create a new asset in account state with plugins', async (t) => {
// pluginHeader: formPluginHeader(BigInt(120)),
freeze: {
authority: {
owner: true,
type: 'Owner',
},
offset: BigInt(118),
frozen: false,
Expand Down
4 changes: 2 additions & 2 deletions clients/js/test/createCollection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test('it can create a new collection with plugins', async (t) => {
updateAuthority: umi.identity.publicKey,
freeze: {
authority: {
owner: true,
type: 'Owner',
},
frozen: false,
},
Expand All @@ -72,7 +72,7 @@ test('it can create a new asset with a collection', async (t) => {
updateAuthority: umi.identity.publicKey,
updateDelegate: {
authority: {
update: true,
type: 'UpdateAuthority'
},
},
});
Expand Down
6 changes: 4 additions & 2 deletions clients/js/test/plugins/asset/delegate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ test('it can delegate a new authority', async (t) => {
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
freeze: {
authority: {
pubkey: [delegateAddress.publicKey],
type: 'Pubkey',
address: delegateAddress.publicKey,
},
frozen: false,
},
Expand Down Expand Up @@ -73,7 +74,8 @@ test('a delegate can freeze the token', async (t) => {
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
freeze: {
authority: {
pubkey: [delegateAddress.publicKey],
type: 'Pubkey',
address: delegateAddress.publicKey,
},
frozen: true,
},
Expand Down
3 changes: 2 additions & 1 deletion clients/js/test/plugins/asset/delegateTransfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ test('a delegate can transfer the asset', async (t) => {
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
transfer: {
authority: {
pubkey: [delegateAddress.publicKey],
type: 'Pubkey',
address: delegateAddress.publicKey,
},
},
});
Expand Down
4 changes: 2 additions & 2 deletions clients/js/test/plugins/asset/freeze.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('it can freeze and unfreeze an asset', async (t) => {
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
freeze: {
authority: {
owner: true,
type: 'Owner',
},
frozen: true,
},
Expand All @@ -43,7 +43,7 @@ test('it can freeze and unfreeze an asset', async (t) => {
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
freeze: {
authority: {
owner: true,
type: 'Owner',
},
frozen: false,
},
Expand Down
4 changes: 2 additions & 2 deletions clients/js/test/plugins/asset/royalties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test('it can transfer an asset with royalties', async (t) => {

royalties: {
authority: {
update: true,
type: 'UpdateAuthority'
},
percentage: 5,
creators: [{ address: umi.identity.publicKey, percentage: 100 }],
Expand Down Expand Up @@ -91,7 +91,7 @@ test('it can transfer an asset with royalties to an allowlisted program address'
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
royalties: {
authority: {
update: true,
type: 'UpdateAuthority'
},
percentage: 5,
creators: [{ address: umi.identity.publicKey, percentage: 100 }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ test('it can create a new asset with a collection if it is the collection update
updateAuthority: umi.identity.publicKey,
updateDelegate: {
authority: {
pubkey: [updateDelegate.publicKey],
type: 'Pubkey',
address: updateDelegate.publicKey,
},
},
});
Expand Down
70 changes: 12 additions & 58 deletions clients/js/test/removePlugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,38 @@
import { generateSigner } from '@metaplex-foundation/umi';
import test from 'ava';
// import { base58 } from '@metaplex-foundation/umi/serializers';

import {
Asset,
AssetWithPlugins,
DataState,
PluginType,
addPlugin,
create,
fetchAsset,
fetchAssetWithPlugins,
removePlugin,
updateAuthority,
formPluginHeader,
plugin,
} from '../src';
import { createUmi } from './_setup';
import { assertAsset, createAsset, createUmi } from './_setup';

test('it can remove a plugin from an asset', 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,
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);
// console.log("Account State:", asset);
t.like(asset, <Asset>{
publicKey: assetAddress.publicKey,
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
owner: umi.identity.publicKey,
name: 'Test Bread',
uri: 'https://example.com/bread',
const asset = await createAsset(umi, {
plugins: [plugin('Freeze', [{ frozen: false }])],
});

await addPlugin(umi, {
asset: assetAddress.publicKey,
plugin: plugin('Freeze', [{ frozen: false }]),
initAuthority: null,
}).sendAndConfirm(umi);

const asset1 = await fetchAssetWithPlugins(umi, assetAddress.publicKey);
// console.log(JSON.stringify(asset1, (_, v) => typeof v === 'bigint' ? v.toString() : v, 2));
t.like(asset1, <AssetWithPlugins>{
publicKey: assetAddress.publicKey,
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
await assertAsset(t, umi, {
asset: asset.publicKey,
owner: umi.identity.publicKey,
name: 'Test Bread',
uri: 'https://example.com/bread',
pluginHeader: formPluginHeader(BigInt(120)),
freeze: {
authority: {
owner: true,
type: 'Owner',
},
offset: BigInt(118),
frozen: false,
},
});
})

await removePlugin(umi, {
asset: assetAddress.publicKey,
asset: asset.publicKey,
pluginType: PluginType.Freeze,
}).sendAndConfirm(umi);

const asset2 = await fetchAssetWithPlugins(umi, assetAddress.publicKey);
// console.log(JSON.stringify(asset2, (_, v) => typeof v === 'bigint' ? v.toString() : v, 2));
t.like(asset2, <AssetWithPlugins>(<unknown>{
publicKey: assetAddress.publicKey,
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
owner: umi.identity.publicKey,
name: 'Test Bread',
uri: 'https://example.com/bread',
pluginHeader: formPluginHeader(BigInt(118)),
}));
const asset2 = await fetchAssetWithPlugins(umi, asset.publicKey);

t.is(asset2.freeze, undefined);
});
Loading

0 comments on commit 7741c09

Please sign in to comment.