Skip to content

Commit

Permalink
fix: plutus list now serializes using the correct encoding depending …
Browse files Browse the repository at this point in the history
…on its size
  • Loading branch information
AngelCastilloB committed Jun 6, 2024
1 parent 9a7856a commit 3785092
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
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

0 comments on commit 3785092

Please sign in to comment.