Skip to content

Commit f1b42d1

Browse files
authored
Merge pull request #1630 from input-output-hk/refactor/bip32account-bip32ed25519-di
feat: bip32account bip32ed25519 dependency injection LW-12941
2 parents 06944bc + b56110c commit f1b42d1

File tree

62 files changed

+311
-293
lines changed

Some content is hidden

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

62 files changed

+311
-293
lines changed

packages/cardano-services/src/StakePool/HttpStakePoolMetadata/HttpStakePoolMetadataService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export const createHttpStakePoolMetadataService = (
6969
const { data } = await axiosClient.get<Uint8Array>(url, { responseType: 'arraybuffer' });
7070

7171
// Produce metadata hash
72-
const metadataHash = Crypto.blake2b(Crypto.blake2b.BYTES).update(data).digest('hex');
72+
const metadataHash = Crypto.blake2b.hash(HexBlob.fromBytes(data), 32);
7373

7474
// Verify base hashes
7575
if (metadataHash !== hash) {

packages/cardano-services/test/StakePool/HttpStakePoolMetadataService/HttpMetadataService.test.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Cardano } from '@cardano-sdk/core';
55
import { DataMocks } from '../../data-mocks';
66
import { ExtMetadataFormat } from '../../../src/StakePool/types';
77
import { Hash32ByteBase16 } from '@cardano-sdk/crypto';
8+
import { HexBlob } from '@cardano-sdk/util';
89
import { IncomingMessage } from 'http';
910
import {
1011
StakePoolMetadataServiceError,
@@ -90,15 +91,13 @@ describe('StakePoolMetadataService', () => {
9091

9192
// Since the extended metadata URL will change each run and its part of the metadata, we must
9293
// recalculate metadata the hash.
93-
const metadataHash = Crypto.blake2b(Crypto.blake2b.BYTES)
94-
.update(Buffer.from(JSON.stringify(metadata), 'ascii'))
95-
.digest('hex');
96-
97-
const resultMetadata = await metadataService.getStakePoolMetadata(
98-
metadataHash as Hash32ByteBase16,
99-
`${serverUrl}/metadata`
94+
const metadataHash = Crypto.blake2b.hash<Hash32ByteBase16>(
95+
HexBlob.fromBytes(Buffer.from(JSON.stringify(metadata), 'ascii')),
96+
32
10097
);
10198

99+
const resultMetadata = await metadataService.getStakePoolMetadata(metadataHash, `${serverUrl}/metadata`);
100+
102101
expect(resultMetadata).toEqual(metadata);
103102

104103
const resultExtendedMetadata = await metadataService.getValidateStakePoolExtendedMetadata(
@@ -249,14 +248,9 @@ describe('StakePoolMetadataService', () => {
249248
extVkey: INVALID_KEY
250249
};
251250

252-
const metadataHash = Crypto.blake2b(Crypto.blake2b.BYTES)
253-
.update(Buffer.from(JSON.stringify(metadata), 'ascii'))
254-
.digest('hex');
251+
const metadataHash = Crypto.blake2b.hash(HexBlob.fromBytes(Buffer.from(JSON.stringify(metadata), 'ascii')), 32);
255252

256-
const metadataResult = await metadataService.getStakePoolMetadata(
257-
metadataHash as Hash32ByteBase16,
258-
`${serverUrl}/metadata`
259-
);
253+
const metadataResult = await metadataService.getStakePoolMetadata(metadataHash, `${serverUrl}/metadata`);
260254

261255
const result2 = await metadataService.getValidateStakePoolExtendedMetadata(
262256
metadataResult as Cardano.StakePoolMetadata

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ export const AssetFingerprint = (value: string): AssetFingerprint => typedBech32
6464
AssetFingerprint.fromParts = (policyId: PolicyId, assetName: AssetName): AssetFingerprint => {
6565
const policyBuf = Buffer.from(policyId, 'hex');
6666
const assetNameBuf = Buffer.from(assetName, 'hex');
67-
const hexDigest = HexBlob(
68-
Crypto.blake2b(20)
69-
.update(new Uint8Array([...policyBuf, ...assetNameBuf]))
70-
.digest('hex')
71-
);
67+
const hexDigest = Crypto.blake2b.hash(HexBlob.fromBytes(new Uint8Array([...policyBuf, ...assetNameBuf])), 20);
7268

7369
return AssetFingerprint(HexBlob.toTypedBech32<string>('asset', hexDigest));
7470
};

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as Crypto from '@cardano-sdk/crypto';
22
import * as Serialization from '../../Serialization/AuxiliaryData';
33
import { Hash32ByteBase16 } from '@cardano-sdk/crypto';
44
import { Script } from './Script';
5-
import { hexToBytes } from '../../util/misc';
65

76
// eslint-disable-next-line no-use-before-define
87
export type MetadatumMap = Map<Metadatum, Metadatum>;
@@ -17,10 +16,4 @@ export interface AuxiliaryData {
1716
}
1817

1918
export const computeAuxiliaryDataHash = (data: AuxiliaryData | undefined): Hash32ByteBase16 | undefined =>
20-
data
21-
? Hash32ByteBase16(
22-
Crypto.blake2b(Crypto.blake2b.BYTES)
23-
.update(hexToBytes(Serialization.AuxiliaryData.fromCore(data).toCbor()))
24-
.digest('hex')
25-
)
26-
: undefined;
19+
data ? Crypto.blake2b.hash<Hash32ByteBase16>(Serialization.AuxiliaryData.fromCore(data).toCbor(), 32) : undefined;

packages/core/src/Cardano/types/StakePool/primitives.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const PoolId = (value: string): PoolId => typedBech32(value, 'pool', 45);
1818
* @param {string} value blake2b_224 digest of an operator verification key hash
1919
* @throws InvalidStringError
2020
*/
21-
PoolId.fromKeyHash = (value: Crypto.Ed25519KeyHashHex): PoolId => HexBlob.toTypedBech32('pool', HexBlob(value));
21+
PoolId.fromKeyHash = (value: Crypto.Ed25519KeyHashHex): PoolId => HexBlob.toTypedBech32('pool', value);
2222

2323
/** /** pool operator verification key hash as hex string */
2424
export type PoolIdHex = OpaqueString<'PoolIdHex'>;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { RewardAccount } from '../Address';
1313
import { Script } from './Script';
1414

1515
/** transaction hash as hex string */
16-
export type TransactionId = OpaqueString<'TransactionId'>;
16+
export type TransactionId = OpaqueString<'TransactionId'> & Crypto.Hash32ByteBase16;
1717

1818
/**
1919
* @param {string} value transaction hash as hex string

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ const computeConwayDeposits = (
102102
case Cardano.CertificateType.UnregisterDelegateRepresentative:
103103
if (
104104
!dRepKeyHash ||
105-
(cert.dRepCredential.type === Address.CredentialType.KeyHash &&
106-
cert.dRepCredential.hash === Crypto.Hash28ByteBase16.fromEd25519KeyHashHex(dRepKeyHash))
105+
(cert.dRepCredential.type === Address.CredentialType.KeyHash && cert.dRepCredential.hash === dRepKeyHash)
107106
) {
108107
cert.__typename === Cardano.CertificateType.RegisterDelegateRepresentative
109108
? (deposit += cert.deposit)

packages/core/src/Serialization/PlutusData/PlutusData.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,7 @@ export class PlutusData {
235235
* @returns the plutus data hash.
236236
*/
237237
hash(): Crypto.Hash32ByteBase16 {
238-
const hash = Crypto.blake2b(HASH_LENGTH_IN_BYTES).update(Buffer.from(this.toCbor(), 'hex')).digest();
239-
240-
return Crypto.Hash32ByteBase16(HexBlob.fromBytes(hash));
238+
return Crypto.blake2b.hash(this.toCbor(), HASH_LENGTH_IN_BYTES);
241239
}
242240

243241
/**

packages/core/src/Serialization/Scripts/NativeScript/NativeScript.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,9 @@ export class NativeScript {
182182
hash(): Crypto.Hash28ByteBase16 {
183183
// To compute a script hash, note that you must prepend a tag to the bytes of
184184
// the script before hashing. The tags in the Babbage era for native scripts is "\x00"
185-
const bytes = `00${this.toCbor()}`;
185+
const bytes = `00${this.toCbor()}` as HexBlob;
186186

187-
const hash = Crypto.blake2b(HASH_LENGTH_IN_BYTES).update(Buffer.from(bytes, 'hex')).digest();
188-
189-
return Crypto.Hash28ByteBase16(HexBlob.fromBytes(hash));
187+
return Crypto.blake2b.hash(bytes, HASH_LENGTH_IN_BYTES);
190188
}
191189

192190
/**

packages/core/src/Serialization/Scripts/PlutusScript/PlutusV1Script.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,9 @@ export class PlutusV1Script {
8989
hash(): Crypto.Hash28ByteBase16 {
9090
// To compute a script hash, note that you must prepend a tag to the bytes of
9191
// the script before hashing. The tags in the Babbage era for PlutusV1 is "\x01"
92-
const bytes = `01${this.rawBytes()}`;
92+
const bytes = `01${this.rawBytes()}` as HexBlob;
9393

94-
const hash = Crypto.blake2b(HASH_LENGTH_IN_BYTES).update(Buffer.from(bytes, 'hex')).digest();
95-
96-
return Crypto.Hash28ByteBase16(HexBlob.fromBytes(hash));
94+
return Crypto.blake2b.hash(bytes, HASH_LENGTH_IN_BYTES);
9795
}
9896

9997
/**

0 commit comments

Comments
 (0)