Skip to content

Commit

Permalink
fix tag 30
Browse files Browse the repository at this point in the history
  • Loading branch information
janmazak committed Dec 18, 2023
1 parent b172ab5 commit 22f6318
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
15 changes: 13 additions & 2 deletions src/txParsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,15 @@ import {
ProposalProcedure,
Int,
Uint,
UnitInterval,
} from './types'
import {
BabbageTransactionOutputKeys,
CborTag,
TransactionBodyKeys,
undefinedOnlyAtTheEnd,
} from './utils'
import { Tagged } from 'cbor'

// ======================== universal parsers / parsers for CDDL data types

Expand Down Expand Up @@ -383,13 +386,21 @@ const parsePoolKeyHash = createParser(
ParseErrorReason.INVALID_POOL_KEY_HASH,
)

const parseUnitInterval = createParser(
parseTupleWithUndefined,
const parseUnitIntervalData = createParser(
parseTuple,
ParseErrorReason.INVALID_UNIT_INTERVAL,
createParser(parseUint, ParseErrorReason.INVALID_UNIT_INTERVAL_START),
createParser(parseUint, ParseErrorReason.INVALID_UNIT_INTERVAL_END),
)

const parseUnitInterval = (
data: unknown,
): UnitInterval => {
validate(data instanceof Tagged, ParseErrorReason.INVALID_UNIT_INTERVAL)
validate(data.tag === CborTag.UNIT_INTERVAL, ParseErrorReason.INVALID_UNIT_INTERVAL)
return parseUnitIntervalData(data.value)
}

const parsePort = (data: unknown): Port => {
validate(
isUintOfMaxSize(data, PORT_MAX_SIZE),
Expand Down
2 changes: 1 addition & 1 deletion src/txSerializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ const serializePoolParams = (poolParams: PoolParams) => [
poolParams.vrfKeyHash,
poolParams.pledge,
poolParams.cost,
new Tagged(CborTag.TUPLE, poolParams.margin),
new Tagged(CborTag.UNIT_INTERVAL, poolParams.margin),
poolParams.rewardAccount,
serializeCddlSetBase(poolParams.poolOwners, identity),
poolParams.relays.map(serializeRelay),
Expand Down
1 change: 1 addition & 0 deletions src/txValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ function* validateStakeCredentials(
const withdrawalStakeCredentialTypes: Set<CredentialType> = new Set()

if (certificates) {
// TODO add Conway?
// We must first filter out the certificates that contain stake credentials
const certificatesWithStakeCredentials = certificates.filter(
({type}) =>
Expand Down
15 changes: 3 additions & 12 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export function unreachable(_val: never): never {

export enum CborTag {
ENCODED_CBOR = 24,
TUPLE = 30,

// called unit interval in the CDDL, but means a fraction from [0, 1]
UNIT_INTERVAL = 30,

// used for arrays interpreted as sets
SET = 258,
Expand All @@ -24,17 +26,6 @@ export enum CborTag {
export const decodeCbor = (buffer: Buffer) =>
cbor.decode(buffer, {
preventDuplicateKeys: true,
tags: {
// Specifies that tag 30 should be parsed only as a tuple. For example
// the CDDL specifies unit_interval as:
// unit_interval = #6.30([uint, uint])
[CborTag.TUPLE]: (v: unknown) => {
if (!Array.isArray(v) || v.length !== 2) {
throw new Error('Invalid tuple')
}
return v
},
},
})

export const encodeToCbor = (x: unknown) => cbor.encodeOne(x, {canonical: true})
Expand Down

0 comments on commit 22f6318

Please sign in to comment.