Skip to content

Commit 890f935

Browse files
authored
Merge pull request #1685 from input-output-hk/feat/strict-deserialization
feat(core): opt-in strict CBOR deserialization mode
2 parents 3cbc64f + 042eaee commit 890f935

34 files changed

Lines changed: 239 additions & 35 deletions

packages/core/src/Serialization/Certificates/AuthCommitteeHot.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CborReader, CborWriter } from '../CBOR';
33
import { CertificateKind } from './CertificateKind';
44
import { CertificateType } from '../../Cardano/types/Certificate';
55
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
6+
import { readCredentialType } from '../Common/Credential';
67
import type * as Cardano from '../../Cardano';
78

89
const EMBEDDED_GROUP_SIZE = 2;
@@ -86,7 +87,7 @@ export class AuthCommitteeHot {
8687
`Expected an array of ${EMBEDDED_GROUP_SIZE} elements, but got an array of ${length} elements`
8788
);
8889

89-
const coldType = Number(reader.readInt()) as Cardano.CredentialType;
90+
const coldType = readCredentialType(reader);
9091
const coldHash = Crypto.Hash28ByteBase16(HexBlob.fromBytes(reader.readByteString()));
9192

9293
reader.readEndArray();
@@ -99,7 +100,7 @@ export class AuthCommitteeHot {
99100
`Expected an array of ${EMBEDDED_GROUP_SIZE} elements, but got an array of ${length} elements`
100101
);
101102

102-
const hotType = Number(reader.readInt()) as Cardano.CredentialType;
103+
const hotType = readCredentialType(reader);
103104
const hotHash = Crypto.Hash28ByteBase16(HexBlob.fromBytes(reader.readByteString()));
104105

105106
reader.readEndArray();

packages/core/src/Serialization/Certificates/DRep/DRep.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Crypto from '@cardano-sdk/crypto';
22
import { CborReader, CborWriter } from '../../CBOR';
33
import { CredentialType } from '../../../Cardano/Address/Address';
44
import { DRepKind } from './DRepKind';
5-
import { HexBlob } from '@cardano-sdk/util';
5+
import { HexBlob, InvalidStateError } from '@cardano-sdk/util';
66
import { isDRepAlwaysAbstain, isDRepAlwaysNoConfidence } from '../../../Cardano/types/Governance';
77
import type * as Cardano from '../../../Cardano';
88

@@ -91,8 +91,9 @@ export class DRep {
9191
reader.readEndArray();
9292

9393
if (kind === DRepKind.Abstain) return DRep.newAlwaysAbstain();
94+
if (kind === DRepKind.NoConfidence) return DRep.newAlwaysNoConfidence();
9495

95-
return DRep.newAlwaysNoConfidence();
96+
throw new InvalidStateError(`Unexpected kind value: ${kind}`);
9697
}
9798

9899
/**

packages/core/src/Serialization/Certificates/RegisterDelegateRepresentative.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { CertificateKind } from './CertificateKind';
55
import { CertificateType } from '../../Cardano/types/Certificate';
66
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
77
import { hexToBytes } from '../../util/misc';
8+
import { readCredentialType } from '../Common/Credential';
89
import type * as Cardano from '../../Cardano';
910

1011
const EMBEDDED_GROUP_SIZE = 2;
@@ -100,7 +101,7 @@ export class RegisterDelegateRepresentative {
100101
`Expected an array of ${EMBEDDED_GROUP_SIZE} elements, but got an array of ${length} elements`
101102
);
102103

103-
const type = Number(reader.readInt()) as Cardano.CredentialType;
104+
const type = readCredentialType(reader);
104105
const hash = Crypto.Hash28ByteBase16(HexBlob.fromBytes(reader.readByteString()));
105106

106107
reader.readEndArray();

packages/core/src/Serialization/Certificates/Registration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CborReader, CborWriter } from '../CBOR';
33
import { CertificateKind } from './CertificateKind';
44
import { CertificateType } from '../../Cardano/types/Certificate';
55
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
6+
import { readCredentialType } from '../Common/Credential';
67
import type * as Cardano from '../../Cardano';
78

89
const EMBEDDED_GROUP_SIZE = 2;
@@ -95,7 +96,7 @@ export class Registration {
9596
`Expected an array of ${EMBEDDED_GROUP_SIZE} elements, but got an array of ${length} elements`
9697
);
9798

98-
const type = Number(reader.readInt()) as Cardano.CredentialType;
99+
const type = readCredentialType(reader);
99100
const hash = Crypto.Hash28ByteBase16(HexBlob.fromBytes(reader.readByteString()));
100101

101102
reader.readEndArray();

packages/core/src/Serialization/Certificates/ResignCommitteeCold.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { CertificateKind } from './CertificateKind';
55
import { CertificateType } from '../../Cardano/types/Certificate';
66
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
77
import { hexToBytes } from '../../util/misc';
8+
import { readCredentialType } from '../Common/Credential';
89
import type * as Cardano from '../../Cardano';
910

1011
const EMBEDDED_GROUP_SIZE = 2;
@@ -90,7 +91,7 @@ export class ResignCommitteeCold {
9091
`Expected an array of ${EMBEDDED_GROUP_SIZE} elements, but got an array of ${length} elements`
9192
);
9293

93-
const coldType = Number(reader.readInt()) as Cardano.CredentialType;
94+
const coldType = readCredentialType(reader);
9495
const coldHash = Crypto.Hash28ByteBase16(HexBlob.fromBytes(reader.readByteString()));
9596
reader.readEndArray();
9697

packages/core/src/Serialization/Certificates/StakeDelegation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { CertificateKind } from './CertificateKind';
44
import { CertificateType } from '../../Cardano/types/Certificate';
55
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
66
import { PoolId } from '../../Cardano/types/StakePool';
7+
import { readCredentialType } from '../Common/Credential';
78
import type * as Cardano from '../../Cardano';
89

910
const EMBEDDED_GROUP_SIZE = 3;
@@ -92,7 +93,7 @@ export class StakeDelegation {
9293
`Expected an array of ${CREDENTIAL_SIZE} elements, but got an array of ${length} elements`
9394
);
9495

95-
const type = Number(reader.readInt()) as Cardano.CredentialType;
96+
const type = readCredentialType(reader);
9697
const hash = Crypto.Hash28ByteBase16(HexBlob.fromBytes(reader.readByteString()));
9798

9899
reader.readEndArray();

packages/core/src/Serialization/Certificates/StakeDeregistration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CborReader, CborWriter } from '../CBOR';
33
import { CertificateKind } from './CertificateKind';
44
import { CertificateType } from '../../Cardano/types/Certificate';
55
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
6+
import { readCredentialType } from '../Common/Credential';
67
import type * as Cardano from '../../Cardano';
78

89
const EMBEDDED_GROUP_SIZE = 2;
@@ -86,7 +87,7 @@ export class StakeDeregistration {
8687
`Expected an array of ${EMBEDDED_GROUP_SIZE} elements, but got an array of ${length} elements`
8788
);
8889

89-
const type = Number(reader.readInt()) as Cardano.CredentialType;
90+
const type = readCredentialType(reader);
9091
const hash = Crypto.Hash28ByteBase16(HexBlob.fromBytes(reader.readByteString()));
9192

9293
reader.readEndArray();

packages/core/src/Serialization/Certificates/StakeRegistration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CborReader, CborWriter } from '../CBOR';
33
import { CertificateKind } from './CertificateKind';
44
import { CertificateType } from '../../Cardano/types/Certificate';
55
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
6+
import { readCredentialType } from '../Common/Credential';
67
import type * as Cardano from '../../Cardano';
78

89
const EMBEDDED_GROUP_SIZE = 2;
@@ -86,7 +87,7 @@ export class StakeRegistration {
8687
`Expected an array of ${EMBEDDED_GROUP_SIZE} elements, but got an array of ${length} elements`
8788
);
8889

89-
const type = Number(reader.readInt()) as Cardano.CredentialType;
90+
const type = readCredentialType(reader);
9091
const hash = Crypto.Hash28ByteBase16(HexBlob.fromBytes(reader.readByteString()));
9192

9293
reader.readEndArray();

packages/core/src/Serialization/Certificates/StakeRegistrationDelegation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { CertificateKind } from './CertificateKind';
44
import { CertificateType } from '../../Cardano/types/Certificate';
55
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
66
import { PoolId } from '../../Cardano/types/StakePool';
7+
import { readCredentialType } from '../Common/Credential';
78
import type * as Cardano from '../../Cardano';
89

910
const EMBEDDED_GROUP_SIZE = 2;
@@ -90,7 +91,7 @@ export class StakeRegistrationDelegation {
9091
`Expected an array of ${EMBEDDED_GROUP_SIZE} elements, but got an array of ${length} elements`
9192
);
9293

93-
const type = Number(reader.readInt()) as Cardano.CredentialType;
94+
const type = readCredentialType(reader);
9495
const hash = Crypto.Hash28ByteBase16(HexBlob.fromBytes(reader.readByteString()));
9596

9697
reader.readEndArray();

packages/core/src/Serialization/Certificates/StakeVoteDelegation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { DRep } from './DRep';
66
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
77
import { PoolId } from '../../Cardano/types/StakePool';
88
import { hexToBytes } from '../../util/misc';
9+
import { readCredentialType } from '../Common/Credential';
910
import type * as Cardano from '../../Cardano';
1011

1112
const EMBEDDED_GROUP_SIZE = 2;
@@ -95,7 +96,7 @@ export class StakeVoteDelegation {
9596
`Expected an array of ${EMBEDDED_GROUP_SIZE} elements, but got an array of ${length} elements`
9697
);
9798

98-
const type = Number(reader.readInt()) as Cardano.CredentialType;
99+
const type = readCredentialType(reader);
99100
const hash = Crypto.Hash28ByteBase16(HexBlob.fromBytes(reader.readByteString()));
100101

101102
reader.readEndArray();

0 commit comments

Comments
 (0)