From d63fca198a99ee1e50519b95de609c5be1308470 Mon Sep 17 00:00:00 2001 From: Blockiosaurus Date: Thu, 1 Aug 2024 16:13:18 -0400 Subject: [PATCH] Adding automatic msgpack decoding. --- clients/js/package.json | 6 +++-- clients/js/pnpm-lock.yaml | 8 +++---- clients/js/src/plugins/lib.ts | 11 +++++----- .../js/test/externalPlugins/appData.test.ts | 22 +++++++++---------- .../externalPlugins/linkedAppData.test.ts | 22 +++++++++---------- 5 files changed, 34 insertions(+), 35 deletions(-) diff --git a/clients/js/package.json b/clients/js/package.json index d1c85b4d..6cfc288a 100644 --- a/clients/js/package.json +++ b/clients/js/package.json @@ -29,13 +29,15 @@ "@metaplex-foundation/umi": ">=0.8.2 < 1", "@noble/hashes": "^1.3.1" }, + "dependencies": { + "@msgpack/msgpack": "^3.0.0-beta2" + }, "devDependencies": { "@ava/typescript": "^5.0.0", "@metaplex-foundation/mpl-core-oracle-example": "^0.0.2", "@metaplex-foundation/mpl-toolbox": "^0.8.0", "@metaplex-foundation/umi": "^0.8.10", "@metaplex-foundation/umi-bundle-tests": "^0.8.10", - "@msgpack/msgpack": "3.0.0-beta2", "@solana/web3.js": "^1.73.0", "@typescript-eslint/eslint-plugin": "^5.0.0", "@typescript-eslint/parser": "^5.46.1", @@ -65,4 +67,4 @@ } }, "packageManager": "pnpm@8.2.0" -} +} \ No newline at end of file diff --git a/clients/js/pnpm-lock.yaml b/clients/js/pnpm-lock.yaml index 040fbff9..7ebde4b4 100644 --- a/clients/js/pnpm-lock.yaml +++ b/clients/js/pnpm-lock.yaml @@ -5,6 +5,9 @@ settings: excludeLinksFromLockfile: false dependencies: + '@msgpack/msgpack': + specifier: ^3.0.0-beta2 + version: 3.0.0-beta2 '@noble/hashes': specifier: ^1.3.1 version: 1.4.0 @@ -25,9 +28,6 @@ devDependencies: '@metaplex-foundation/umi-bundle-tests': specifier: ^0.8.10 version: 0.8.10(@metaplex-foundation/umi@0.8.10)(@solana/web3.js@1.93.0) - '@msgpack/msgpack': - specifier: 3.0.0-beta2 - version: 3.0.0-beta2 '@solana/web3.js': specifier: ^1.73.0 version: 1.93.0 @@ -2333,7 +2333,7 @@ packages: /@msgpack/msgpack@3.0.0-beta2: resolution: {integrity: sha512-y+l1PNV0XDyY8sM3YtuMLK5vE3/hkfId+Do8pLo/OPxfxuFAUwcGz3oiiUuV46/aBpwTzZ+mRWVMtlSKbradhw==} engines: {node: '>= 14'} - dev: true + dev: false /@noble/curves@1.4.0: resolution: {integrity: sha512-p+4cb332SFCrReJkCYe8Xzm0OWi4Jji5jVdIZRL/PmacmDkFNw6MrrV+gGpiPxLHbV+zKFRywUWbaseT+tZRXg==} diff --git a/clients/js/src/plugins/lib.ts b/clients/js/src/plugins/lib.ts index e8345249..b91e9541 100644 --- a/clients/js/src/plugins/lib.ts +++ b/clients/js/src/plugins/lib.ts @@ -1,5 +1,6 @@ import { isSome, none, Option, some } from '@metaplex-foundation/umi'; +import { decode } from '@msgpack/msgpack'; import { Key, PluginHeaderV1, @@ -234,11 +235,11 @@ export function parseExternalPluginAdapterData( } } } else if (plugin.schema === ExternalPluginAdapterSchema.MsgPack) { - // eslint-disable-next-line no-console - console.warn( - 'MsgPack schema currently not supported, falling back to binary' - ); - data = dataSlice; + if (dataSlice.length === 0) { + data = null; + } else { + data = decode(dataSlice); + } } return data; } diff --git a/clients/js/test/externalPlugins/appData.test.ts b/clients/js/test/externalPlugins/appData.test.ts index b1e4eb91..6a131250 100644 --- a/clients/js/test/externalPlugins/appData.test.ts +++ b/clients/js/test/externalPlugins/appData.test.ts @@ -91,6 +91,7 @@ async function generateTestContext( otherData = Uint8Array.from(Buffer.from(JSON.stringify(otherDataJson))); } else if (schema === ExternalPluginAdapterSchema.MsgPack) { data = msgpack.encode({ message: 'Hello', target: 'msgpack' }); + otherData = msgpack.encode({ message: 'Hello hello', target: 'msgpack' }); } if (!dataAuthoritySigner) { @@ -188,11 +189,10 @@ DATA_AUTHORITIES.forEach((dataAuthorityType) => { }).sendAndConfirm(umi); let assertData = null; - if ( - schema === ExternalPluginAdapterSchema.Binary || - schema === ExternalPluginAdapterSchema.MsgPack - ) { + if (schema === ExternalPluginAdapterSchema.Binary) { assertData = data; + } else if (schema === ExternalPluginAdapterSchema.MsgPack) { + assertData = msgpack.decode(data); } else if (schema === ExternalPluginAdapterSchema.Json) { assertData = JSON.parse(Buffer.from(data).toString()); } @@ -260,11 +260,10 @@ DATA_AUTHORITIES.forEach((dataAuthorityType) => { }).sendAndConfirm(umi); let assertData = null; - if ( - schema === ExternalPluginAdapterSchema.Binary || - schema === ExternalPluginAdapterSchema.MsgPack - ) { + if (schema === ExternalPluginAdapterSchema.Binary) { assertData = data; + } else if (schema === ExternalPluginAdapterSchema.MsgPack) { + assertData = msgpack.decode(data); } else if (schema === ExternalPluginAdapterSchema.Json) { assertData = JSON.parse(Buffer.from(data).toString()); } @@ -294,11 +293,10 @@ DATA_AUTHORITIES.forEach((dataAuthorityType) => { asset: asset.publicKey, }).sendAndConfirm(umi); - if ( - schema === ExternalPluginAdapterSchema.Binary || - schema === ExternalPluginAdapterSchema.MsgPack - ) { + if (schema === ExternalPluginAdapterSchema.Binary) { assertData = otherData; + } else if (schema === ExternalPluginAdapterSchema.MsgPack) { + assertData = msgpack.decode(otherData); } else if (schema === ExternalPluginAdapterSchema.Json) { assertData = JSON.parse(Buffer.from(otherData).toString()); } diff --git a/clients/js/test/externalPlugins/linkedAppData.test.ts b/clients/js/test/externalPlugins/linkedAppData.test.ts index 593de476..a0277da9 100644 --- a/clients/js/test/externalPlugins/linkedAppData.test.ts +++ b/clients/js/test/externalPlugins/linkedAppData.test.ts @@ -97,6 +97,7 @@ async function generateTestContext( otherData = Uint8Array.from(Buffer.from(JSON.stringify(otherDataJson))); } else if (schema === ExternalPluginAdapterSchema.MsgPack) { data = msgpack.encode({ message: 'Hello', target: 'msgpack' }); + otherData = msgpack.encode({ message: 'Hello hello', target: 'msgpack' }); } if (!dataAuthoritySigner) { @@ -203,11 +204,10 @@ DATA_AUTHORITIES.forEach((dataAuthorityType) => { }).sendAndConfirm(umi); let assertData = null; - if ( - schema === ExternalPluginAdapterSchema.Binary || - schema === ExternalPluginAdapterSchema.MsgPack - ) { + if (schema === ExternalPluginAdapterSchema.Binary) { assertData = data; + } else if (schema === ExternalPluginAdapterSchema.MsgPack) { + assertData = msgpack.decode(data); } else if (schema === ExternalPluginAdapterSchema.Json) { assertData = JSON.parse(Buffer.from(data).toString()); } @@ -300,11 +300,10 @@ DATA_AUTHORITIES.forEach((dataAuthorityType) => { }).sendAndConfirm(umi); let assertData = null; - if ( - schema === ExternalPluginAdapterSchema.Binary || - schema === ExternalPluginAdapterSchema.MsgPack - ) { + if (schema === ExternalPluginAdapterSchema.Binary) { assertData = data; + } else if (schema === ExternalPluginAdapterSchema.MsgPack) { + assertData = msgpack.decode(data); } else if (schema === ExternalPluginAdapterSchema.Json) { assertData = JSON.parse(Buffer.from(data).toString()); } @@ -355,11 +354,10 @@ DATA_AUTHORITIES.forEach((dataAuthorityType) => { asset: asset.publicKey, }).sendAndConfirm(umi); - if ( - schema === ExternalPluginAdapterSchema.Binary || - schema === ExternalPluginAdapterSchema.MsgPack - ) { + if (schema === ExternalPluginAdapterSchema.Binary) { assertData = otherData; + } else if (schema === ExternalPluginAdapterSchema.MsgPack) { + assertData = msgpack.decode(otherData); } else if (schema === ExternalPluginAdapterSchema.Json) { assertData = JSON.parse(Buffer.from(otherData).toString()); }