Skip to content

Commit 80d736f

Browse files
fix: plutus list now serializes using the correct encoding depending on its size
1 parent 9a7856a commit 80d736f

File tree

4 files changed

+23
-20
lines changed

4 files changed

+23
-20
lines changed

packages/core/src/Serialization/PlutusData/PlutusList.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { bytesToHex, hexToBytes } from '../../util/misc';
66
/** A list of plutus data. */
77
export class PlutusList {
88
readonly #array = new Array<PlutusData>();
9-
#useIndefiniteEncoding = false;
109

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

19-
if (this.#useIndefiniteEncoding) {
19+
if (shouldUseIndefinite) {
2020
writer.writeStartArray();
2121
} else {
2222
writer.writeStartArray(this.#array.length);
@@ -26,7 +26,7 @@ export class PlutusList {
2626
writer.writeEncodedValue(hexToBytes(elem.toCbor()));
2727
}
2828

29-
if (this.#useIndefiniteEncoding) writer.writeEndArray();
29+
if (shouldUseIndefinite) writer.writeEndArray();
3030

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

44-
const length = reader.readStartArray();
45-
46-
if (length === null) list.#useIndefiniteEncoding = true;
44+
reader.readStartArray();
4745

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

9491
for (let i = 0; i < this.#array.length; ++i) {

packages/core/test/Serialization/PlutusData.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ describe('PlutusData', () => {
133133
});
134134

135135
describe('List', () => {
136+
it('can encode an empty plutus list', () => {
137+
const data = new Serialization.PlutusList();
138+
139+
expect(data.toCbor()).toEqual('80');
140+
});
141+
136142
it('can encode simple plutus list', () => {
137143
const data = new Serialization.PlutusList();
138144

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

145-
expect(data.toCbor()).toEqual('850102030405');
151+
expect(data.toCbor()).toEqual('9f0102030405ff');
146152
});
147153

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

165-
expect(outer.toCbor()).toEqual('85010285010203040585010203040505');
171+
expect(outer.toCbor()).toEqual('9f01029f0102030405ff9f0102030405ff05ff');
166172
});
167173
});
168174

@@ -292,7 +298,7 @@ describe('PlutusData', () => {
292298

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

295-
expect(data.toCbor()).toEqual('d879850102030405');
301+
expect(data.toCbor()).toEqual('d8799f0102030405ff');
296302
});
297303
});
298304

packages/ogmios/test/CardanoNode/__snapshots__/ObservableOgmiosCardanoNode.test.ts.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,19 @@ Array [
9191
},
9292
Object {
9393
"data": Object {
94-
"cbor": "9fd866821901739fd866821902dd9f2341e14478c0fcf5ff22d866821903e39f438002ab0104ffa1448858788900ff029fa040ffff",
94+
"cbor": "83d866821901739fd866821902dd9f2341e14478c0fcf5ff22d866821903e39f438002ab0104ffa1448858788900ff029fa040ff",
9595
"items": Array [
9696
Object {
9797
"cbor": "d866821901739fd866821902dd9f2341e14478c0fcf5ff22d866821903e39f438002ab0104ffa1448858788900ff",
9898
"constructor": 371n,
9999
"fields": Object {
100-
"cbor": "9fd866821902dd9f2341e14478c0fcf5ff22d866821903e39f438002ab0104ffa1448858788900ff",
100+
"cbor": "84d866821902dd9f2341e14478c0fcf5ff22d866821903e39f438002ab0104ffa1448858788900",
101101
"items": Array [
102102
Object {
103103
"cbor": "d866821902dd9f2341e14478c0fcf5ff",
104104
"constructor": 733n,
105105
"fields": Object {
106-
"cbor": "9f2341e14478c0fcf5ff",
106+
"cbor": "832341e14478c0fcf5",
107107
"items": Array [
108108
-4n,
109109
Uint8Array [
@@ -123,7 +123,7 @@ Array [
123123
"cbor": "d866821903e39f438002ab0104ff",
124124
"constructor": 995n,
125125
"fields": Object {
126-
"cbor": "9f438002ab0104ff",
126+
"cbor": "83438002ab0104",
127127
"items": Array [
128128
Uint8Array [
129129
128,
@@ -151,7 +151,7 @@ Array [
151151
},
152152
2n,
153153
Object {
154-
"cbor": "9fa040ff",
154+
"cbor": "82a040",
155155
"items": Array [
156156
Object {
157157
"cbor": "a0",

packages/ogmios/test/ogmiosToCore/__snapshots__/block.test.ts.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Object {
142142
96,
143143
] => 2n,
144144
Object {
145-
"cbor": "9f20a3419c204386a48244e21bd936202004a0ff",
145+
"cbor": "8420a3419c204386a48244e21bd936202004a0",
146146
"items": Array [
147147
-1n,
148148
Object {
@@ -189,7 +189,7 @@ Object {
189189
-4n => Uint8Array [],
190190
},
191191
} => Object {
192-
"cbor": "9f414d429b3f0440ff",
192+
"cbor": "84414d429b3f0440",
193193
"items": Array [
194194
Uint8Array [
195195
77,
@@ -206,7 +206,7 @@ Object {
206206
"cbor": "a0",
207207
"data": Map {},
208208
} => Object {
209-
"cbor": "9f034001ff",
209+
"cbor": "83034001",
210210
"items": Array [
211211
3n,
212212
Uint8Array [],
@@ -225,7 +225,7 @@ Object {
225225
"cbor": "d8668218da9f2341440302413eff",
226226
"constructor": 218n,
227227
"fields": Object {
228-
"cbor": "9f2341440302413eff",
228+
"cbor": "852341440302413e",
229229
"items": Array [
230230
-4n,
231231
Uint8Array [
@@ -251,7 +251,7 @@ Object {
251251
"cbor": "d866821901939f23ff",
252252
"constructor": 403n,
253253
"fields": Object {
254-
"cbor": "9f23ff",
254+
"cbor": "8123",
255255
"items": Array [
256256
-4n,
257257
],

0 commit comments

Comments
 (0)