Skip to content

Commit 87aa26f

Browse files
committed
refactor: resolve circular references in core package
add missing 'circular-deps:check' script there are still some circular imports in Address and Serialization namespaces (see --exclude in package.json script). Resolving those would be a big refactor. They should not cause any issues at runtime, because there is no circular usage at the time of import BREAKING CHANGE: remove Cardano.TransactionId.fromTxBodyCbor - hoist getAssetNameAsText util to Asset.util namespace - hoist TxCBOR and TxBodyCBOR under Serialization namespace
1 parent 08dab8a commit 87aa26f

File tree

151 files changed

+681
-646
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

151 files changed

+681
-646
lines changed

packages/cardano-services-client/src/TxSubmitProvider/TxSubmitApiProvider.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { Cardano, ProviderError, ProviderFailure, SubmitTxArgs, TxBodyCBOR, TxSubmitProvider } from '@cardano-sdk/core';
1+
import {
2+
Cardano,
3+
ProviderError,
4+
ProviderFailure,
5+
Serialization,
6+
SubmitTxArgs,
7+
TxSubmitProvider
8+
} from '@cardano-sdk/core';
29
import { Logger } from 'ts-log';
310
import { hexStringToBuffer } from '@cardano-sdk/util';
411
import { mapCardanoTxSubmitError } from './cardanoTxSubmitErrorMapper';
@@ -35,7 +42,7 @@ export class TxSubmitApiProvider implements TxSubmitProvider {
3542
let txId: Cardano.TransactionId | undefined;
3643

3744
try {
38-
txId = Cardano.TransactionId.fromTxBodyCbor(TxBodyCBOR.fromTxCBOR(signedTransaction));
45+
txId = Serialization.TransactionBody.fromCbor(signedTransaction).hash();
3946

4047
this.#logger.debug(`Submitting tx ${txId} ...`);
4148

packages/core/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"scripts": {
2424
"build": "yarn build:cjs && tsc -p src/tsconfig.json --outDir ./dist/esm --module es2020 && tsc-alias -p src/tsconfig.json --outDir ./dist/esm && cp ../../build/esm-package.json ./dist/esm/package.json",
2525
"build:cjs": "tsc --build src && cp ../../build/cjs-package.json ./dist/cjs/package.json",
26+
"circular-deps:check": "madge --circular dist/cjs --exclude \"/(?:Address)|(?:Serialization/(?:PlutusData)|(?:Certificates)|(?:Scripts)|(?:AuxiliaryData))/*\"",
2627
"tscNoEmit": "echo typescript --noEmit command not implemented yet",
2728
"cleanup:dist": "rm -rf dist",
2829
"cleanup:nm": "rm -rf node_modules",

packages/core/src/Asset/NftMetadata/fromMetadatum.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
import { AssetId, AssetName } from '../../Cardano/types/Asset';
12
import { AssetInfo } from '../types';
2-
import { AssetName } from '../../Cardano';
3-
import { Cardano } from '../..';
43
import { ImageMediaType, MediaType, NftMetadata, NftMetadataFile, Uri } from './types';
54
import { InvalidFileError } from './errors';
65
import { Logger } from 'ts-log';
76
import { asMetadatumArray, asMetadatumMap } from '../../util/metadatum';
87
import { asString } from './util';
98
import { isNotNil } from '@cardano-sdk/util';
109
import difference from 'lodash/difference.js';
10+
import type { Cardano } from '../../';
1111

1212
const isString = (obj: unknown): obj is string => typeof obj === 'string';
1313
const VersionRegExp = /^\d+\.?\d?$/;
@@ -151,7 +151,7 @@ export const fromMetadatum = (
151151
if (!assetMetadata) return null;
152152
const name = getName(assetMetadata, version, asset, logger, true);
153153
const image = metadatumToString(assetMetadata.get('image'));
154-
const assetId = Cardano.AssetId.fromParts(asset.policyId, asset.name);
154+
const assetId = AssetId.fromParts(asset.policyId, asset.name);
155155

156156
if ((strict && !name) || !image) {
157157
logger.warn(missingFieldLogMessage(!name ? 'name' : 'image', assetId, true));

packages/core/src/Asset/NftMetadata/fromPlutusData.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Cardano } from '../..';
21
import { ImageMediaType, MediaType, NftMetadata, NftMetadataFile, Uri } from './types';
32
import { Logger } from 'ts-log';
43
import { asString } from './util';
@@ -11,6 +10,7 @@ import {
1110
isPlutusMap,
1211
tryConvertPlutusMapToUtf8Record
1312
} from '../../Cardano/util';
13+
import type { Cardano } from '../..';
1414

1515
const tryCoerce = <T>(
1616
value: string | Cardano.PlutusData | undefined,

packages/core/src/Asset/NftMetadata/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable wrap-regex */
22
import { InvalidStringError, OpaqueString } from '@cardano-sdk/util';
3-
import { Metadatum } from '../../Cardano';
3+
import type { Metadatum } from '../../Cardano';
44

55
export type Uri = OpaqueString<'Uri'>;
66
export const Uri = (uri: string) => {

packages/core/src/Asset/cip67.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as Cardano from '../Cardano';
1+
import { AssetName } from '../Cardano/types/Asset';
22
import { InvalidArgumentError, OpaqueNumber } from '@cardano-sdk/util';
33
import crc8 from './crc8';
44

@@ -18,7 +18,7 @@ export const AssetNameLabelNum = {
1818

1919
export interface DecodedAssetName {
2020
label: AssetNameLabel;
21-
content: Cardano.AssetName;
21+
content: AssetName;
2222
}
2323

2424
const assertLabelNumInterval = (labelNum: number) => {
@@ -55,7 +55,7 @@ const assetNameLabelHexToNum = (label: string): AssetNameLabel | null => {
5555
return AssetNameLabel(Number.parseInt(labelNumHex, 16));
5656
};
5757

58-
AssetNameLabel.decode = (assetName: Cardano.AssetName): DecodedAssetName | null => {
58+
AssetNameLabel.decode = (assetName: AssetName): DecodedAssetName | null => {
5959
const assetNameLabel = assetName.slice(0, ASSET_LABEL_LENGTH);
6060
const assetNameContent = assetName.slice(ASSET_LABEL_LENGTH);
6161
const assetNameLabelNum = assetNameLabelHexToNum(assetNameLabel);
@@ -65,15 +65,13 @@ AssetNameLabel.decode = (assetName: Cardano.AssetName): DecodedAssetName | null
6565
}
6666

6767
return {
68-
content: Cardano.AssetName(assetNameContent),
68+
content: AssetName(assetNameContent),
6969
label: assetNameLabelNum
7070
};
7171
};
7272

73-
AssetNameLabel.encode = (assetName: Cardano.AssetName, labelNum: AssetNameLabel): Cardano.AssetName => {
73+
AssetNameLabel.encode = (assetName: AssetName, labelNum: AssetNameLabel): AssetName => {
7474
assertLabelNumInterval(labelNum);
7575
const labelNumHex = labelNum.toString(16).padStart(4, ASSET_LABEL_BRACKET);
76-
return Cardano.AssetName(
77-
`${ASSET_LABEL_BRACKET}${labelNumHex}${checksum(labelNumHex)}${ASSET_LABEL_BRACKET}${assetName}`
78-
);
76+
return AssetName(`${ASSET_LABEL_BRACKET}${labelNumHex}${checksum(labelNumHex)}${ASSET_LABEL_BRACKET}${assetName}`);
7977
};

packages/core/src/Asset/types/AssetInfo.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { AssetFingerprint, AssetId, AssetName, PolicyId, TransactionId } from '../../Cardano';
21
import { NftMetadata } from '../NftMetadata';
32
import { TokenMetadata } from './TokenMetadata';
3+
import type { AssetFingerprint, AssetId, AssetName, PolicyId, TransactionId } from '../../Cardano';
44

55
export interface AssetMintOrBurn {
66
transactionId: TransactionId;

packages/core/src/Asset/util/coalesceTokenMaps.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { TokenMap } from '../../Cardano';
21
import { isNotNil } from '@cardano-sdk/util';
2+
import type { TokenMap } from '../../Cardano';
33

44
/** Sum asset quantities */
55
export const coalesceTokenMaps = (totals: (TokenMap | undefined)[]): TokenMap | undefined => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { AssetId } from '../../Cardano/types/Asset';
2+
import { AssetNameLabel } from '../cip67';
3+
4+
export const getAssetNameAsText = (id: AssetId) => {
5+
const assetName = AssetId.getAssetName(id);
6+
const assetNameContent = AssetNameLabel.decode(assetName)?.content;
7+
return Buffer.from(assetNameContent || assetName, 'hex').toString('utf8');
8+
};

packages/core/src/Asset/util/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './coalesceTokenMaps';
22
export * from './removeNegativesFromTokenMap';
33
export * from './subtractTokenMaps';
44
export * from './isValidHandle';
5+
export * from './getAssetNameAsText';

packages/core/src/Asset/util/removeNegativesFromTokenMap.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TokenMap } from '../../Cardano';
1+
import type { TokenMap } from '../../Cardano';
22

33
/**
44
* Remove all negative quantities from a TokenMap.

packages/core/src/Asset/util/subtractTokenMaps.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable complexity,sonarjs/cognitive-complexity */
2-
import * as Cardano from '../../Cardano';
32
import uniq from 'lodash/uniq.js';
3+
import type * as Cardano from '../../Cardano';
44

55
/**
66
* Given two Cardano.TokenMaps, compute a Cardano.TokenMap with the difference between the left-hand side and the right-hand side.

packages/core/src/CBOR/TxBodyCBOR.ts

-15
This file was deleted.

packages/core/src/CBOR/TxCBOR.ts

-31
This file was deleted.

packages/core/src/CBOR/index.ts

-2
This file was deleted.

packages/core/src/Cardano/Address/PaymentAddress.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import {
77
assertIsBech32WithPrefix,
88
assertIsHexString
99
} from '@cardano-sdk/util';
10-
import { HydratedTx, HydratedTxIn, Tx, TxIn, TxOut } from '../types';
11-
import { NetworkId } from '../ChainId';
12-
import { RewardAccount } from './RewardAccount';
10+
import type { HydratedTx, Tx } from '../types/Transaction';
11+
import type { HydratedTxIn, TxIn, TxOut } from '../types/Utxo';
12+
import type { NetworkId } from '../ChainId';
13+
import type { RewardAccount } from './RewardAccount';
1314

1415
/** mainnet or testnet address (Shelley as bech32 string, Byron as base58-encoded string) */
1516
export type PaymentAddress = OpaqueString<'PaymentAddress'>;

packages/core/src/Cardano/Address/PointerAddress.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Address, AddressProps, AddressType, Credential, CredentialType } from '
33
import { Hash28ByteBase16 } from '@cardano-sdk/crypto';
44
import { InvalidArgumentError, OpaqueNumber } from '@cardano-sdk/util';
55
import { NetworkId } from '../ChainId';
6-
import { Slot } from '../types';
6+
import { Slot } from '../types/Block';
77

88
/**
99
* Encodes the specified value with a variable number of bytes.

packages/core/src/Cardano/types/Asset.ts

-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as Crypto from '@cardano-sdk/crypto';
2-
import { AssetNameLabel } from '../../Asset';
32
import { HexBlob, InvalidStringError, OpaqueString, assertIsHexString, typedBech32 } from '@cardano-sdk/util';
43
import { TextDecoder } from 'web-encoding';
54

@@ -50,11 +49,6 @@ export const PolicyId = (value: string): PolicyId => Crypto.Hash28ByteBase16(val
5049
AssetId.getPolicyId = (id: AssetId) => PolicyId(id.slice(0, 56));
5150
AssetId.getAssetName = (id: AssetId) => AssetName(id.slice(56));
5251
AssetId.fromParts = (policyId: PolicyId, assetName: AssetName): AssetId => AssetId(policyId + assetName);
53-
AssetId.getAssetNameAsText = (id: AssetId) => {
54-
const assetName = AssetId.getAssetName(id);
55-
const assetNameContent = AssetNameLabel.decode(assetName)?.content;
56-
return Buffer.from(assetNameContent || assetName, 'hex').toString('utf8');
57-
};
5852

5953
/** Fingerprint of a native asset for human comparison See CIP-0014 */
6054
export type AssetFingerprint = OpaqueString<'AssetFingerprint'>;

packages/core/src/Cardano/types/AuxiliaryData.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Crypto from '@cardano-sdk/crypto';
2-
import * as Serialization from '../../Serialization';
2+
import * as Serialization from '../../Serialization/AuxiliaryData';
33
import { Hash32ByteBase16 } from '@cardano-sdk/crypto';
44
import { Script } from './Script';
55
import { hexToBytes } from '../../util/misc';

packages/core/src/Cardano/types/Certificate.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import * as Crypto from '@cardano-sdk/crypto';
2-
import { Anchor, DelegateRepresentative } from './Governance';
31
import { Credential, CredentialType, RewardAccount } from '../Address';
4-
import { EpochNo } from './Block';
52
import { Hash28ByteBase16 } from '@cardano-sdk/crypto';
6-
import { Lovelace } from './Value';
7-
import { PoolId, PoolParameters } from './StakePool';
83
import { isNotNil } from '@cardano-sdk/util';
4+
import type * as Crypto from '@cardano-sdk/crypto';
5+
import type { Anchor, DelegateRepresentative } from './Governance';
6+
import type { EpochNo } from './Block';
7+
import type { Lovelace } from './Value';
8+
import type { PoolId, PoolParameters } from './StakePool';
99

1010
export enum CertificateType {
1111
StakeRegistration = 'StakeRegistrationCertificate',

packages/core/src/Cardano/types/Transaction.ts

-8
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import { PlutusData } from './PlutusData';
1111
import { ProposalProcedure, VotingProcedures } from './Governance';
1212
import { RewardAccount } from '../Address';
1313
import { Script } from './Script';
14-
import { TxBodyCBOR } from '../../CBOR/TxBodyCBOR';
15-
import { bytesToHex, hexToBytes } from '../../util/misc';
1614

1715
/** transaction hash as hex string */
1816
export type TransactionId = OpaqueString<'TransactionId'>;
@@ -24,12 +22,6 @@ export type TransactionId = OpaqueString<'TransactionId'>;
2422
export const TransactionId = (value: string): TransactionId =>
2523
Crypto.Hash32ByteBase16(value) as unknown as TransactionId;
2624
TransactionId.fromHexBlob = (value: HexBlob) => Crypto.Hash32ByteBase16.fromHexBlob<TransactionId>(value);
27-
TransactionId.fromTxBodyCbor = (bodyCbor: TxBodyCBOR): TransactionId =>
28-
bytesToHex(
29-
Crypto.blake2b(Crypto.blake2b.BYTES)
30-
.update(hexToBytes(bodyCbor as unknown as HexBlob))
31-
.digest()
32-
) as unknown as TransactionId;
3325

3426
export interface Withdrawal {
3527
stakeAddress: RewardAccount;

packages/core/src/Cardano/util/addressesShareAnyKey.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Address, AddressType, Credential } from '../Address/Address';
2-
import { Hash28ByteBase16 } from '@cardano-sdk/crypto';
32
import { InvalidStringError } from '@cardano-sdk/util';
4-
import { PaymentAddress } from '../Address/PaymentAddress';
5-
import { Pointer } from '../Address/PointerAddress';
3+
import type { Hash28ByteBase16 } from '@cardano-sdk/crypto';
4+
import type { PaymentAddress } from '../Address/PaymentAddress';
5+
import type { Pointer } from '../Address/PointerAddress';
66

77
type PaymentId = { credential: Credential } | { byronRoot: Hash28ByteBase16 };
88
type StakeId = { credential: Credential } | { pointer: Pointer };

0 commit comments

Comments
 (0)