Skip to content

Commit

Permalink
update prettier config
Browse files Browse the repository at this point in the history
  • Loading branch information
janmazak authored and davidmisiak committed Feb 14, 2023
1 parent 7dbc20d commit 43b5cdb
Show file tree
Hide file tree
Showing 21 changed files with 104 additions and 108 deletions.
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.eslintrc.js
README.md
docs
.vscode
.circleci
3 changes: 2 additions & 1 deletion .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
semi: false,
tabWidth: 2,
singleQuote: true,
trailingComma: 'all',
bracketSpacing: false,
quoteProps: 'consistent'
}
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { ValidationError } from './errors'
import type {ValidationError} from './errors'
import * as parsers from './txParsers'
import * as serializers from './txSerializers'
import * as transformers from './txTransformers'
import { validateTxCommon } from './txValidators'
import type { Transaction, TransactionBody } from './types'
import { decodeCbor, encodeToCbor } from './utils'
import {validateTxCommon} from './txValidators'
import type {Transaction, TransactionBody} from './types'
import {decodeCbor, encodeToCbor} from './utils'

export type { ValidationError } from './errors'
export type {ValidationError} from './errors'
export * from './types'

/**
Expand Down
10 changes: 5 additions & 5 deletions src/parsers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Tagged } from 'cbor'
import {Tagged} from 'cbor'

import { ParseErrorReason, ParseError } from './errors'
import {ParseErrorReason, ParseError} from './errors'
import type {
FixLenBuffer,
Int,
Expand All @@ -9,7 +9,7 @@ import type {
MaxSizeUint,
Uint,
} from './types'
import { CborTag } from './utils'
import {CborTag} from './utils'

export function validate(
cond: boolean,
Expand Down Expand Up @@ -204,7 +204,7 @@ export const parseArray = <T>(
export const parseTuple = <T extends unknown[]>(
data: unknown,
errMsg: ParseErrorReason,
...parsers: { [K in keyof T]: Parser<T[K]> }
...parsers: {[K in keyof T]: Parser<T[K]>}
): T => {
validate(isArray(data), errMsg)
validate(data.length <= parsers.length, errMsg)
Expand All @@ -222,7 +222,7 @@ export const parseNullable = <T>(data: unknown, parser: Parser<T>): T | null =>

export type WithoutType<T> = Omit<T, 'type'>
type ArrayParser<T> = (data: unknown[]) => T
type ParsersWithoutType<T> = { [K in keyof T]: ArrayParser<WithoutType<T[K]>> }
type ParsersWithoutType<T> = {[K in keyof T]: ArrayParser<WithoutType<T[K]>>}

export const parseBasedOnType = <T extends number, U extends unknown[]>(
data: unknown,
Expand Down
16 changes: 8 additions & 8 deletions src/txParsers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ParseErrorReason } from './errors'
import type { Parser, WithoutType } from './parsers'
import {ParseErrorReason} from './errors'
import type {Parser, WithoutType} from './parsers'
import {
createParser,
isArray,
Expand Down Expand Up @@ -136,7 +136,7 @@ const parseTxInput = (unparsedTxInput: unknown): TransactionInput => {
createParser(parseUint, ParseErrorReason.INVALID_TX_INPUT_INDEX),
)

return { transactionId, index }
return {transactionId, index}
}

const parseAddress = createParser(
Expand All @@ -156,7 +156,7 @@ const parseAmountWithMultiasset = (unparsedAmount: unknown): Amount => {
),
)

return { type: AmountType.WITH_MULTIASSET, coin, multiasset }
return {type: AmountType.WITH_MULTIASSET, coin, multiasset}
}

const parseAmountWithoutMultiasset = (unparsedAmount: unknown): Amount => ({
Expand Down Expand Up @@ -426,7 +426,7 @@ const parsePoolMetadata = (unparsedPoolMetadata: unknown): PoolMetadata => {
),
)

return { url, metadataHash }
return {url, metadataHash}
}

const parsePoolParams = (unparsedPoolParams: unknown): PoolParams => {
Expand Down Expand Up @@ -564,7 +564,7 @@ const parseCollateralInput = (
createParser(parseUint, ParseErrorReason.INVALID_COLLATERAL_INPUT_INDEX),
)

return { transactionId, index }
return {transactionId, index}
}

const parseRequiredSigner = (unparsedRequiredSigner: unknown): RequiredSigner =>
Expand All @@ -588,7 +588,7 @@ const parseReferenceInput = (
createParser(parseUint, ParseErrorReason.INVALID_REFERENCE_INPUT_INDEX),
)

return { transactionId, index }
return {transactionId, index}
}

export const parseInputs = createParser(
Expand Down Expand Up @@ -734,7 +734,7 @@ export const parseTx = (unparsedTx: unknown): Transaction => {
// cardano-cli with --shelley-era, --allegra-era and --mary-era
// includes only txBody, witnessSet and auxiliaryData
if (presentItems.length === 2) {
return { body, witnessSet: presentItems[0], auxiliaryData: presentItems[1] }
return {body, witnessSet: presentItems[0], auxiliaryData: presentItems[1]}
}

// cardano-cli with --alonzo-era includes all tx items
Expand Down
10 changes: 4 additions & 6 deletions src/txSerializers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tagged } from 'cbor'
import {Tagged} from 'cbor'

import type {
Amount,
Expand Down Expand Up @@ -48,9 +48,9 @@ const serializeMultiasset = <T>(
multiasset: Multiasset<T>,
): Map<PolicyId, Map<AssetName, T>> =>
new Map(
multiasset.map(({ policyId, tokens }) => [
multiasset.map(({policyId, tokens}) => [
policyId,
new Map(tokens.map(({ assetName, amount }) => [assetName, amount])),
new Map(tokens.map(({assetName, amount}) => [assetName, amount])),
]),
)

Expand Down Expand Up @@ -113,9 +113,7 @@ const serializeTxOutput = (output: TransactionOutput) => {
const serializeWithdrawals = (
withdrawals: Withdrawal[],
): Map<RewardAccount, Coin> =>
new Map(
withdrawals.map(({ rewardAccount, amount }) => [rewardAccount, amount]),
)
new Map(withdrawals.map(({rewardAccount, amount}) => [rewardAccount, amount]))

const serializeRelay = (relay: Relay) => {
switch (relay.type) {
Expand Down
8 changes: 4 additions & 4 deletions src/txTransformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import type {
TransactionOutput,
Unparsed,
} from './types'
import { AmountType, DatumType, TxOutputFormat } from './types'
import { blake2b256, encodeToCbor, unreachable } from './utils'
import {AmountType, DatumType, TxOutputFormat} from './types'
import {blake2b256, encodeToCbor, unreachable} from './utils'

const transformOptionalList = <T>(optionalList?: T[]): T[] | undefined =>
optionalList?.length === 0 ? undefined : optionalList
Expand All @@ -23,11 +23,11 @@ const transformMultiasset = <T>(
? undefined
: transformOptionalList(
multiasset
.map(({ policyId, tokens }) => ({
.map(({policyId, tokens}) => ({
policyId,
tokens: transformOptionalList(tokens),
}))
.filter(({ tokens }) => tokens !== undefined) as Multiasset<T>,
.filter(({tokens}) => tokens !== undefined) as Multiasset<T>,
)

const transformAmount = (amount: Amount): Amount => {
Expand Down
27 changes: 13 additions & 14 deletions src/txValidators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ValidationError } from './errors'
import { err, ValidationErrorReason } from './errors'
import type {ValidationError} from './errors'
import {err, ValidationErrorReason} from './errors'
import type {
Amount,
Certificate,
Expand All @@ -17,8 +17,8 @@ import type {
Uint,
Withdrawal,
} from './types'
import { AmountType, CertificateType, DatumType, TxOutputFormat } from './types'
import { bind, getRewardAccountStakeCredentialType, unreachable } from './utils'
import {AmountType, CertificateType, DatumType, TxOutputFormat} from './types'
import {bind, getRewardAccountStakeCredentialType, unreachable} from './utils'

const UINT16_MAX = 65535
const MAX_UINT_64_STR = '18446744073709551615'
Expand All @@ -43,8 +43,7 @@ function* validateOptional<T>(
x: T | undefined | null,
validateFn: (x: T) => ValidatorReturnType,
): ValidatorReturnType {
if (x != null)
yield* validateFn(x)
if (x != null) yield* validateFn(x)
}

/**
Expand Down Expand Up @@ -99,14 +98,14 @@ function* validateMultiasset<T>(
): ValidatorReturnType {
yield* validateListConstraints(multiasset, position, false)

for (const { policyId, tokens } of multiasset) {
for (const {policyId, tokens} of multiasset) {
yield* validateListConstraints(
tokens,
`${position}[${policyId.toString('hex')}]`,
false,
)

for (const { assetName, amount } of tokens) {
for (const {assetName, amount} of tokens) {
yield* validateAmount(
amount,
`${position}[${policyId.toString('hex')}][${assetName.toString(
Expand Down Expand Up @@ -252,7 +251,7 @@ function* validateWithdrawals(withdrawals: Withdrawal[]): ValidatorReturnType {
false,
)

for (const { rewardAccount, amount } of withdrawals) {
for (const {rewardAccount, amount} of withdrawals) {
yield* validateUint64(
amount,
`transaction_body.withdrawals[${rewardAccount.toString('hex')}]`,
Expand All @@ -270,7 +269,7 @@ function* validateStakeCredentials(
if (certificates) {
// We must first filter out the certificates that contain stake credentials
const certificatesWithStakeCredentials = certificates.filter(
({ type }) =>
({type}) =>
type === CertificateType.STAKE_REGISTRATION ||
type === CertificateType.STAKE_DEREGISTRATION ||
type === CertificateType.STAKE_DELEGATION,
Expand All @@ -279,7 +278,7 @@ function* validateStakeCredentials(
| StakeDeregistrationCertificate
| StakeDelegationCertificate
)[]
certificatesWithStakeCredentials.forEach(({ stakeCredential }) =>
certificatesWithStakeCredentials.forEach(({stakeCredential}) =>
certificateStakeCredentialTypes.add(stakeCredential.type),
)
// We check the set of stake credential types to be less or equal to one,
Expand All @@ -295,7 +294,7 @@ function* validateStakeCredentials(
}

if (withdrawals) {
withdrawals.forEach(({ rewardAccount }) =>
withdrawals.forEach(({rewardAccount}) =>
withdrawalStakeCredentialTypes.add(
getRewardAccountStakeCredentialType(rewardAccount),
),
Expand Down Expand Up @@ -417,7 +416,7 @@ function* validatePoolRegistrationTransaction(
if (
!txBody.certificates ||
txBody.certificates.find(
({ type }) => type === CertificateType.POOL_REGISTRATION,
({type}) => type === CertificateType.POOL_REGISTRATION,
) === undefined
) {
return
Expand Down Expand Up @@ -448,7 +447,7 @@ function* validatePoolRegistrationTransaction(
yield* validate(
!txBody.mint ||
txBody.mint.length === 0 ||
txBody.mint.every(({ tokens }) => tokens.length === 0),
txBody.mint.every(({tokens}) => tokens.length === 0),
err(
ValidationErrorReason.POOL_REGISTRATION_CERTIFICATE_WITH_MINT_ENTRY,
'transaction_body.mint',
Expand Down
12 changes: 6 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export type Unparsed = unknown

export type Uint = (number | bigint) & { __type: 'uint' }
export type Int = (number | bigint) & { __type: 'int' }
export type Uint = (number | bigint) & {__type: 'uint'}
export type Int = (number | bigint) & {__type: 'int'}

export type MaxLenString<N> = string & { __maxLength: N }
export type FixLenBuffer<N> = Buffer & { __length: N }
export type MaxLenBuffer<N> = Buffer & { __maxLength: N }
export type MaxSizeUint<N> = Uint & { __maxSize: N }
export type MaxLenString<N> = string & {__maxLength: N}
export type FixLenBuffer<N> = Buffer & {__length: N}
export type MaxLenBuffer<N> = Buffer & {__maxLength: N}
export type MaxSizeUint<N> = Uint & {__maxSize: N}

export const KEY_HASH_LENGTH = 28
export const SCRIPT_HASH_LENGTH = 28
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import blake2b from 'blake2b'
import cbor from 'cbor'

import type { FixLenBuffer, RewardAccount } from './types'
import { StakeCredentialType } from './types'
import type {FixLenBuffer, RewardAccount} from './types'
import {StakeCredentialType} from './types'

export function assert(cond: boolean, errMsg: string): asserts cond {
const msg = errMsg ? `: ${errMsg}` : ''
Expand Down Expand Up @@ -34,7 +34,7 @@ export const decodeCbor = (buffer: Buffer) =>
},
})

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

export const bind =
<A, R, T extends unknown[]>(
Expand Down
2 changes: 1 addition & 1 deletion test/integration/__fixtures__/auxiliaryData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as cbor from 'cbor'

import { toFixLenBuffer } from '../../test_utils'
import {toFixLenBuffer} from '../../test_utils'

export const CanonicalAuxiliaryData = {
// 259({0: {721: {"ipfs": "some other text", "assetHash": "0931ae2de9aa46212e50adc1798f9071e7c6368b78ae7eb434c2ca0ed9d05370"}}})
Expand Down
2 changes: 1 addition & 1 deletion test/integration/__fixtures__/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ export const ValidTransactionTestCases: ValidTransactionTestCase[] = [
],
],
]),
auxiliaryData: [new Map([[1234, { foo: 'bar' }]]), []],
auxiliaryData: [new Map([[1234, {foo: 'bar'}]]), []],
},
},
{
Expand Down
10 changes: 5 additions & 5 deletions test/integration/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { expect } from 'chai'
import {expect} from 'chai'

import { ParseError, ParseErrorReason } from '../../src/errors'
import { decodeTx, decodeTxBody } from '../../src/index'
import {ParseError, ParseErrorReason} from '../../src/errors'
import {decodeTx, decodeTxBody} from '../../src/index'
import {
ValidTransactionBodyTestCases,
ValidTransactionTestCases,
} from './__fixtures__/transactions'

describe('Parse', () => {
describe('Valid transaction bodies', () => {
for (const { testName, cbor, txBody } of ValidTransactionBodyTestCases) {
for (const {testName, cbor, txBody} of ValidTransactionBodyTestCases) {
it(testName, () => {
const parsedTxBody = decodeTxBody(Buffer.from(cbor, 'hex'))
expect(parsedTxBody).to.deep.equal(txBody)
Expand All @@ -18,7 +18,7 @@ describe('Parse', () => {
})

describe('Valid transactions', () => {
for (const { testName, cbor, tx } of ValidTransactionTestCases) {
for (const {testName, cbor, tx} of ValidTransactionTestCases) {
it(testName, () => {
const parsedTx = decodeTx(Buffer.from(cbor, 'hex'))
expect(parsedTx).to.deep.equal(tx)
Expand Down
8 changes: 4 additions & 4 deletions test/integration/serialize.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { expect } from 'chai'
import {expect} from 'chai'

import { encodeTx, encodeTxBody } from '../../src/index'
import {encodeTx, encodeTxBody} from '../../src/index'
import {
ValidTransactionBodyTestCases,
ValidTransactionTestCases,
} from './__fixtures__/transactions'

describe('Serialize', () => {
describe('Transaction bodies', () => {
for (const { testName, txBody, cbor } of ValidTransactionBodyTestCases) {
for (const {testName, txBody, cbor} of ValidTransactionBodyTestCases) {
it(testName, () => {
const serializedCbor = encodeTxBody(txBody).toString('hex')
expect(serializedCbor).to.deep.equal(cbor)
Expand All @@ -17,7 +17,7 @@ describe('Serialize', () => {
})

describe('Transactions', () => {
for (const { testName, tx, cbor } of ValidTransactionTestCases) {
for (const {testName, tx, cbor} of ValidTransactionTestCases) {
it(testName, () => {
const serializedCbor = encodeTx(tx).toString('hex')
expect(serializedCbor).to.deep.equal(cbor)
Expand Down
Loading

0 comments on commit 43b5cdb

Please sign in to comment.