Skip to content

fix: plutus list now serializes using the correct encoding depending on its size #1322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions packages/core/src/Serialization/PlutusData/PlutusList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { bytesToHex, hexToBytes } from '../../util/misc';
/** A list of plutus data. */
export class PlutusList {
readonly #array = new Array<PlutusData>();
#useIndefiniteEncoding = false;

/**
* Serializes this PlutusList instance into its CBOR representation as a Uint8Array.
Expand All @@ -15,8 +14,9 @@ export class PlutusList {
*/
toCbor(): HexBlob {
const writer = new CborWriter();
const shouldUseIndefinite = this.#array.length > 0;

if (this.#useIndefiniteEncoding) {
if (shouldUseIndefinite) {
writer.writeStartArray();
} else {
writer.writeStartArray(this.#array.length);
Expand All @@ -26,7 +26,7 @@ export class PlutusList {
writer.writeEncodedValue(hexToBytes(elem.toCbor()));
}

if (this.#useIndefiniteEncoding) writer.writeEndArray();
if (shouldUseIndefinite) writer.writeEndArray();

return HexBlob.fromBytes(writer.encode());
}
Expand All @@ -41,9 +41,7 @@ export class PlutusList {
const list = new PlutusList();
const reader = new CborReader(cbor);

const length = reader.readStartArray();

if (length === null) list.#useIndefiniteEncoding = true;
reader.readStartArray();

while (reader.peekState() !== CborReaderState.EndArray) {
list.add(PlutusData.fromCbor(bytesToHex(reader.readEncodedValue())));
Expand Down Expand Up @@ -88,7 +86,6 @@ export class PlutusList {
* @returns true if objects are equals; otherwise false.
*/
equals(other: PlutusList): boolean {
if (this.#useIndefiniteEncoding !== other.#useIndefiniteEncoding) return false;
if (this.#array.length !== other.#array.length) return false;

for (let i = 0; i < this.#array.length; ++i) {
Expand Down
12 changes: 9 additions & 3 deletions packages/core/test/Serialization/PlutusData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ describe('PlutusData', () => {
});

describe('List', () => {
it('can encode an empty plutus list', () => {
const data = new Serialization.PlutusList();

expect(data.toCbor()).toEqual('80');
});

it('can encode simple plutus list', () => {
const data = new Serialization.PlutusList();

Expand All @@ -142,7 +148,7 @@ describe('PlutusData', () => {
data.add(Serialization.PlutusData.newInteger(4n));
data.add(Serialization.PlutusData.newInteger(5n));

expect(data.toCbor()).toEqual('850102030405');
expect(data.toCbor()).toEqual('9f0102030405ff');
});

it('can encode a list of plutus list', () => {
Expand All @@ -162,7 +168,7 @@ describe('PlutusData', () => {
outer.add(Serialization.PlutusData.newList(innerList));
outer.add(Serialization.PlutusData.newInteger(5n));

expect(outer.toCbor()).toEqual('85010285010203040585010203040505');
expect(outer.toCbor()).toEqual('9f01029f0102030405ff9f0102030405ff05ff');
});
});

Expand Down Expand Up @@ -292,7 +298,7 @@ describe('PlutusData', () => {

const data = new Serialization.ConstrPlutusData(0n, args);

expect(data.toCbor()).toEqual('d879850102030405');
expect(data.toCbor()).toEqual('d8799f0102030405ff');
});
});

Expand Down
Loading