Skip to content

Commit

Permalink
chore: enable array of bytes only in v3
Browse files Browse the repository at this point in the history
  • Loading branch information
slowbackspace committed May 7, 2024
1 parent 650b19b commit 6aaec8f
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/cip68.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ export const getReferenceNFT = (
const convertDatumValue = (
decodedValue: unknown,
schema: PropertyScheme | Record<string, PropertyScheme> | null,
version: number,
): unknown => {
if (!schema) {
return null;
Expand All @@ -334,15 +335,19 @@ const convertDatumValue = (
return decodedValue;
} else if (schema.type === 'bytestring' && Buffer.isBuffer(decodedValue)) {
return toUTF8OrHex(decodedValue);
} else if (schema.type === 'bytestring' && Array.isArray(decodedValue)) {
} else if (
version === 3 &&
schema.type === 'bytestring' &&
Array.isArray(decodedValue)
) {
// bytestring, but encoded as array of bytes
// concat chunks and convert to utf-8 or return bytes as hex
return toUTF8OrHex(Buffer.concat(decodedValue));
} else if (Array.isArray(decodedValue)) {
const convertedArray = [];
for (const arrayItem of decodedValue) {
const arrayItemSchema = schema.items ?? null;
const v = convertDatumValue(arrayItem, arrayItemSchema);
const v = convertDatumValue(arrayItem, arrayItemSchema, version);
if (v === null) {
// One of the item has unsupported format which means we keep CBOR value instead
return null;
Expand All @@ -358,7 +363,7 @@ const convertDatumValue = (
const valueSchema =
// @ts-expect-error TODO
schema && convertedKey in schema ? schema[convertedKey] : null;
const convertedValue = convertDatumValue(mapValue, valueSchema);
const convertedValue = convertDatumValue(mapValue, valueSchema, version);
if (convertedValue === null) {
// Unsupported format
return null;
Expand Down Expand Up @@ -443,6 +448,7 @@ export const getMetadataFromOutputDatum = (
const convertedValue = convertDatumValue(
decodedValue,
metadataFormat[convertedKey],
datumVersion,
);

// use converted value if available, otherwise leave it as cbor
Expand Down

0 comments on commit 6aaec8f

Please sign in to comment.