Skip to content

Commit a8e5cb2

Browse files
Merge pull request #1482 from input-output-hk/feat/lw-11065-take-over-external-contribution
Fix signed/unsigned bug in Value.fromCBOR
2 parents dc2d4f5 + d394341 commit a8e5cb2

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

packages/core/src/Serialization/TransactionBody/Value.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class Value {
8181
const reader = new CborReader(cbor);
8282

8383
if (reader.peekState() === CborReaderState.UnsignedInteger) {
84-
const coins = reader.readInt();
84+
const coins = reader.readUInt();
8585
return new Value(coins);
8686
}
8787

@@ -93,7 +93,7 @@ export class Value {
9393
`Expected an array of ${VALUE_ARRAY_SIZE} elements, but got an array of ${length} elements`
9494
);
9595

96-
const coins = reader.readInt();
96+
const coins = reader.readUInt();
9797
const multiassets = new Map<Crypto.Hash28ByteBase16, Map<Cardano.AssetName, bigint>>();
9898

9999
reader.readStartMap();
@@ -105,7 +105,7 @@ export class Value {
105105
reader.readStartMap();
106106
while (reader.peekState() !== CborReaderState.EndMap) {
107107
const assetName = Buffer.from(reader.readByteString()).toString('hex') as unknown as Cardano.AssetName;
108-
const quantity = reader.readInt();
108+
const quantity = reader.readUInt();
109109

110110
multiassets.get(scriptHash)!.set(assetName, quantity);
111111
}

packages/core/test/Serialization/TransactionBody/Value.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
/* eslint-disable sonarjs/no-duplicate-string */
22
import * as Cardano from '../../../src/Cardano';
3+
import { CborContentException, Value } from '../../../src/Serialization';
34
import { HexBlob } from '@cardano-sdk/util';
4-
import { Value } from '../../../src/Serialization';
55

66
// Test data used in the following tests was generated with the cardano-serialization-lib
77

88
const cbor = HexBlob(
99
'821a000f4240a2581c00000000000000000000000000000000000000000000000000000000a3443031323218644433343536186344404142420a581c11111111111111111111111111111111111111111111111111111111a3443031323218644433343536186344404142420a'
1010
);
1111

12+
const cborWithNegativeCoin = HexBlob(
13+
'823a000f423fa2581c00000000000000000000000000000000000000000000000000000000a3443031323218644433343536186344404142420a581c11111111111111111111111111111111111111111111111111111111a3443031323218644433343536186344404142420a'
14+
);
15+
1216
const unsortedCore = {
1317
assets: new Map([
1418
['1111111111111111111111111111111111111111111111111111111140414242' as unknown as Cardano.AssetId, 10n],
@@ -91,4 +95,8 @@ describe('Value', () => {
9195
const interval = Value.fromCbor(onlyLovelaceCbor);
9296
expect(interval.toCore()).toEqual(onlyLovelaceCore);
9397
});
98+
99+
it('can throws if Value CBOR contains negative numbers', () => {
100+
expect(() => Value.fromCbor(cborWithNegativeCoin)).toThrow(CborContentException);
101+
});
94102
});

0 commit comments

Comments
 (0)