@bbachain/spl-token-metadata
Version:
110 lines (109 loc) • 4.72 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
exports.__esModule = true;
exports.deserialize = void 0;
var beet = __importStar(require("@bbachain/beet"));
var beetBBA = __importStar(require("@bbachain/beet-bbachain"));
var Metadata_1 = require("../accounts/Metadata");
var types_1 = require("../types");
var NONE_BYTE_SIZE = beet.coptionNone('').byteSize;
/**
* This is a custom deserializer for TokenMetadata in order to mitigate acounts with corrupted
* data on chain.
*
* Instead of failing the deserialization for the section that is possibly corrupt it just returns
* `null` for the fields that would normally be stored in that section.
*
* This deserializer matches the [fix implemented in the Rust program](https://github.com/metaplex-foundation/metaplex-program-library/blob/df36da5a78fb17e1690247b8041b761d27c83b1b/token-metadata/program/src/deser.rs#L6).
* Also @see ../../../program/src/deser.rs
*/
function deserialize(buf, offset) {
if (offset === void 0) { offset = 0; }
var cursor = offset;
// key
var key = types_1.keyBeet.read(buf, cursor);
cursor += types_1.keyBeet.byteSize;
// updateAuthority
var updateAuthority = beetBBA.publicKey.read(buf, cursor);
cursor += beetBBA.publicKey.byteSize;
// mint
var mint = beetBBA.publicKey.read(buf, cursor);
cursor += beetBBA.publicKey.byteSize;
// data
var _a = types_1.dataBeet.deserialize(buf, cursor), data = _a[0], dataDelta = _a[1];
cursor = dataDelta;
// primarySaleHappened
var primarySaleHappened = beet.bool.read(buf, cursor);
cursor += beet.bool.byteSize;
// isMutable
var isMutable = beet.bool.read(buf, cursor);
cursor += beet.bool.byteSize;
// tokenStandard
var _b = tryReadOption(beet.coption(types_1.tokenStandardBeet), buf, cursor), tokenStandard = _b[0], tokenDelta = _b[1], tokenCorrupted = _b[2];
cursor += tokenDelta;
// collection
var _c = tokenCorrupted
? [null, NONE_BYTE_SIZE, true]
: tryReadOption(beet.coption(types_1.collectionBeet), buf, cursor), collection = _c[0], collectionDelta = _c[1], collectionCorrupted = _c[2];
cursor += collectionDelta;
// collection_details
var _d = tokenCorrupted || collectionCorrupted
? [null, NONE_BYTE_SIZE, true]
: tryReadOption(beet.coption(types_1.collectionDetailsBeet), buf, cursor), collectionDetails = _d[0], collectionDetailsDelta = _d[1], collectionDetailsCorrupted = _d[2];
cursor += collectionDetailsDelta;
// uses
var _e = tokenCorrupted || collectionCorrupted || collectionDetailsCorrupted
? [null, NONE_BYTE_SIZE, true]
: tryReadOption(beet.coption(types_1.usesBeet), buf, cursor), uses = _e[0], usesDelta = _e[1], usesCorrupted = _e[2];
cursor += usesDelta;
var anyCorrupted = tokenCorrupted ||
collectionCorrupted ||
usesCorrupted ||
collectionDetailsCorrupted;
var args = {
key: key,
updateAuthority: updateAuthority,
mint: mint,
data: data,
primarySaleHappened: primarySaleHappened,
isMutable: isMutable,
tokenStandard: anyCorrupted ? null : tokenStandard,
collection: anyCorrupted ? null : collection,
collectionDetails: anyCorrupted ? null : collectionDetails,
uses: anyCorrupted ? null : uses
};
return [Metadata_1.Metadata.fromArgs(args), cursor];
}
exports.deserialize = deserialize;
function tryReadOption(optionBeet, buf, offset) {
try {
var fixed = optionBeet.toFixedFromData(buf, offset);
var value = fixed.read(buf, offset);
return [value, fixed.byteSize, false];
}
catch (e) {
return [null, NONE_BYTE_SIZE, true];
}
}