Skip to content

Commit

Permalink
Merge pull request #181 from metaplex-foundation/feat/msgpack-decoding
Browse files Browse the repository at this point in the history
Automatic msgpack decoding in SDK
  • Loading branch information
blockiosaurus authored Aug 1, 2024
2 parents 2a72c64 + d63fca1 commit 1b28658
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 35 deletions.
6 changes: 4 additions & 2 deletions clients/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -65,4 +67,4 @@
}
},
"packageManager": "[email protected]"
}
}
8 changes: 4 additions & 4 deletions clients/js/pnpm-lock.yaml

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

11 changes: 6 additions & 5 deletions clients/js/src/plugins/lib.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isSome, none, Option, some } from '@metaplex-foundation/umi';

import { decode } from '@msgpack/msgpack';
import {
Key,
PluginHeaderV1,
Expand Down Expand Up @@ -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;
}
Expand Down
22 changes: 10 additions & 12 deletions clients/js/test/externalPlugins/appData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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());
}
Expand Down
22 changes: 10 additions & 12 deletions clients/js/test/externalPlugins/linkedAppData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit 1b28658

Please sign in to comment.