From 015bae7964c3fa87c31612cdbab2c567ed384a7d Mon Sep 17 00:00:00 2001 From: gabrielkerekes Date: Thu, 8 Sep 2022 13:28:24 +0200 Subject: [PATCH] Rename metadata hash to auxiliary data hash --- src/errors/parseError.ts | 2 +- src/txParsers.ts | 17 +-- src/txSerializers.ts | 5 +- src/txTransformers.ts | 6 +- src/types.ts | 7 +- src/utils.ts | 2 +- test/integration/__fixtures__/transactions.ts | 127 ++++++++++++++++-- 7 files changed, 137 insertions(+), 29 deletions(-) diff --git a/src/errors/parseError.ts b/src/errors/parseError.ts index ab4b3a0..05516b5 100644 --- a/src/errors/parseError.ts +++ b/src/errors/parseError.ts @@ -64,7 +64,7 @@ export enum ParseErrorReason { INVALID_TTL = 'Invalid transaction ttl', INVALID_CERTIFICATES = 'Invalid transaction certificates', INVALID_WITHDRAWALS = 'Invalid transaction withdrawals', - INVALID_METADATA_HASH = 'Invalid transaction metadata hash', + INVALID_AUXILIARY_DATA_HASH = 'Invalid transaction auxiliary data hash', INVALID_VALIDITY_INTERVAL_START = 'Invalid transaction validity interval start', INVALID_MINT = 'Invalid transaction mint', INVALID_SCRIPT_DATA_HASH = 'Invalid transaction script data hash', diff --git a/src/txParsers.ts b/src/txParsers.ts index 120dc88..5b6c5c8 100644 --- a/src/txParsers.ts +++ b/src/txParsers.ts @@ -56,6 +56,7 @@ import type { import { AmountType, ASSET_NAME_MAX_LENGTH, + AUXILIARY_DATA_HASH_LENGTH, CertificateType, DATUM_HASH_LENGTH, DatumType, @@ -63,8 +64,8 @@ import { IPV4_LENGTH, IPV6_LENGTH, KEY_HASH_LENGTH, - METADATA_HASH_LENGTH, POOL_KEY_HASH_LENGTH, + POOL_METADATA_HASH_LENGTH, PORT_MAX_SIZE, RelayType, REWARD_ACCOUNT_LENGTH, @@ -422,7 +423,7 @@ const parsePoolMetadata = (unparsedPoolMetadata: unknown): PoolMetadata => { ), createParser( parseBufferOfLength, - METADATA_HASH_LENGTH, + POOL_METADATA_HASH_LENGTH, ParseErrorReason.INVALID_POOL_METADATA_METADATA_HASH, ), ) @@ -609,10 +610,10 @@ export const parseCertificates = createParser( parseCertificate, ParseErrorReason.INVALID_CERTIFICATES, ) -export const parseMetadataHash = createParser( +export const parseAuxiliaryDataHash = createParser( parseBufferOfLength, - METADATA_HASH_LENGTH, - ParseErrorReason.INVALID_METADATA_HASH, + AUXILIARY_DATA_HASH_LENGTH, + ParseErrorReason.INVALID_AUXILIARY_DATA_HASH, ) export const parseValidityIntervalStart = createParser( parseUint, @@ -672,9 +673,9 @@ export const parseTxBody = (unparsedTxBody: unknown): TransactionBody => { parseWithdrawals, ), update: unparsedTxBody.get(TransactionBodyKeys.UPDATE), - metadataHash: parseOptional( - unparsedTxBody.get(TransactionBodyKeys.METADATA_HASH), - parseMetadataHash, + auxiliaryDataHash: parseOptional( + unparsedTxBody.get(TransactionBodyKeys.AUXILIARY_DATA_HASH), + parseAuxiliaryDataHash, ), validityIntervalStart: parseOptional( unparsedTxBody.get(TransactionBodyKeys.VALIDITY_INTERVAL_START), diff --git a/src/txSerializers.ts b/src/txSerializers.ts index b848e32..cafaa80 100644 --- a/src/txSerializers.ts +++ b/src/txSerializers.ts @@ -189,7 +189,10 @@ export const serializeTxBody = (txBody: TransactionBody) => txBody.withdrawals && serializeWithdrawals(txBody.withdrawals), ], [TransactionBodyKeys.UPDATE, identity(txBody.update)], - [TransactionBodyKeys.METADATA_HASH, identity(txBody.metadataHash)], + [ + TransactionBodyKeys.AUXILIARY_DATA_HASH, + identity(txBody.auxiliaryDataHash), + ], [ TransactionBodyKeys.VALIDITY_INTERVAL_START, identity(txBody.validityIntervalStart), diff --git a/src/txTransformers.ts b/src/txTransformers.ts index c76ebf6..52bdae5 100644 --- a/src/txTransformers.ts +++ b/src/txTransformers.ts @@ -1,8 +1,8 @@ import type { Amount, + AUXILIARY_DATA_HASH_LENGTH, Datum, FixlenBuffer, - METADATA_HASH_LENGTH, Multiasset, RawTransaction, ReferenceScript, @@ -98,7 +98,7 @@ const transformTxOutput = (output: TransactionOutput): TransactionOutput => { const transformAuxiliaryDataHash = ( auxiliaryData: unknown | undefined, -): FixlenBuffer | undefined => +): FixlenBuffer | undefined => auxiliaryData ? blake2b256(encodeToCbor(auxiliaryData)) : undefined export const transformTxBody = ( @@ -115,7 +115,7 @@ export const transformTxBody = ( txBody.collateralReturnOutput && transformTxOutput(txBody.collateralReturnOutput), referenceInputs: transformOptionalList(txBody.referenceInputs), - metadataHash: transformAuxiliaryDataHash(auxiliaryData), + auxiliaryDataHash: transformAuxiliaryDataHash(auxiliaryData), }) export const transformTx = (tx: Transaction): Transaction => ({ diff --git a/src/types.ts b/src/types.ts index f75ae8f..ef80267 100644 --- a/src/types.ts +++ b/src/types.ts @@ -12,10 +12,11 @@ export const KEY_HASH_LENGTH = 28 export const SCRIPT_HASH_LENGTH = 28 export const GENESIS_DELEGATE_HASH_LENGTH = 28 export const POOL_KEY_HASH_LENGTH = 28 +export const POOL_METADATA_HASH_LENGTH = 32 export const GENESIS_HASH_LENGTH = 28 export const REWARD_ACCOUNT_LENGTH = 29 export const VRF_KEY_HASH_LENGTH = 32 -export const METADATA_HASH_LENGTH = 32 +export const AUXILIARY_DATA_HASH_LENGTH = 32 export const TX_ID_HASH_LENGTH = 32 export const DATUM_HASH_LENGTH = 32 export const SCRIPT_DATA_HASH_LENGTH = 32 @@ -198,7 +199,7 @@ export type Relay = export type PoolMetadata = { url: MaxlenString - metadataHash: FixlenBuffer + metadataHash: FixlenBuffer } export type PoolParams = { @@ -264,7 +265,7 @@ export type TransactionBody = { certificates?: Certificate[] withdrawals?: Withdrawal[] update?: Unparsed - metadataHash?: FixlenBuffer + auxiliaryDataHash?: FixlenBuffer validityIntervalStart?: Uint mint?: Mint scriptDataHash?: FixlenBuffer diff --git a/src/utils.ts b/src/utils.ts index 1ade2c8..181f6a4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -83,7 +83,7 @@ export enum TransactionBodyKeys { CERTIFICATES = 4, WITHDRAWALS = 5, UPDATE = 6, - METADATA_HASH = 7, + AUXILIARY_DATA_HASH = 7, VALIDITY_INTERVAL_START = 8, MINT = 9, SCRIPT_DATA_HASH = 11, diff --git a/test/integration/__fixtures__/transactions.ts b/test/integration/__fixtures__/transactions.ts index 5546e24..01014fc 100644 --- a/test/integration/__fixtures__/transactions.ts +++ b/test/integration/__fixtures__/transactions.ts @@ -23,6 +23,10 @@ import { toMaxLenString, toUint, } from '../../test_utils' +import { + CanonicalAuxiliaryData, + NonCanonicalAuxiliaryData, +} from './auxiliaryData' type ValidTransactionBodyTestcase = { testname: string @@ -62,7 +66,7 @@ export const ValidTransactionBodyTestcases: ValidTransactionBodyTestcase[] = [ certificates: undefined, withdrawals: undefined, update: undefined, - metadataHash: undefined, + auxiliaryDataHash: undefined, validityIntervalStart: undefined, mint: undefined, scriptDataHash: undefined, @@ -118,7 +122,7 @@ export const ValidTransactionBodyTestcases: ValidTransactionBodyTestcase[] = [ }, ], update: undefined, - metadataHash: undefined, + auxiliaryDataHash: undefined, validityIntervalStart: undefined, mint: undefined, scriptDataHash: undefined, @@ -204,7 +208,7 @@ export const ValidTransactionBodyTestcases: ValidTransactionBodyTestcase[] = [ certificates: undefined, withdrawals: undefined, update: undefined, - metadataHash: undefined, + auxiliaryDataHash: undefined, validityIntervalStart: undefined, mint: [ { @@ -366,7 +370,7 @@ export const ValidTransactionBodyTestcases: ValidTransactionBodyTestcase[] = [ ], withdrawals: undefined, update: undefined, - metadataHash: undefined, + auxiliaryDataHash: undefined, validityIntervalStart: undefined, mint: undefined, scriptDataHash: undefined, @@ -447,7 +451,7 @@ export const ValidTransactionBodyTestcases: ValidTransactionBodyTestcase[] = [ certificates: undefined, withdrawals: undefined, update: undefined, - metadataHash: undefined, + auxiliaryDataHash: undefined, validityIntervalStart: undefined, mint: undefined, scriptDataHash: toFixlenBuffer( @@ -518,7 +522,7 @@ export const ValidTransactionBodyTestcases: ValidTransactionBodyTestcase[] = [ certificates: undefined, withdrawals: undefined, update: undefined, - metadataHash: undefined, + auxiliaryDataHash: undefined, validityIntervalStart: undefined, mint: undefined, scriptDataHash: toFixlenBuffer( @@ -562,6 +566,105 @@ export const ValidTransactionBodyTestcases: ValidTransactionBodyTestcase[] = [ }, ] +type TransformTransactionBodyTestcase = { + testname: string + cbor: string + auxiliaryData?: unknown + txBody: TransactionBody +} + +export const TransformTransactionTestcases: TransformTransactionBodyTestcase[] = + [ + { + testname: 'Simple tx body with canonical auxiliary data', + cbor: 'a50081825820bc8bf52ea894fb8e442fe3eea628be87d0c9a37baef185b70eb00a5c8a849d3b000181825839000743d16cfe3c4fcc0c11c2403bbc10dbc7ecdd4477e053481a368e7a06e2ae44dff6770dc0f4ada3cf4cf2605008e27aecdb332ad349fda71a0023583c021a00029b75031a01a3bd8f075820fb7099a47afd6efb4f9cccf9d0f8745331a19eb8b3f50548ffadae9de8551743', + auxiliaryData: CanonicalAuxiliaryData.data, + txBody: { + inputs: [ + { + transactionId: toFixlenBuffer( + 'bc8bf52ea894fb8e442fe3eea628be87d0c9a37baef185b70eb00a5c8a849d3b', + 32, + ), + index: toUint(0), + }, + ], + outputs: [ + { + format: TxOutputFormat.ARRAY_LEGACY, + address: fromBech32( + 'addr_test1qqr585tvlc7ylnqvz8pyqwauzrdu0mxag3m7q56grgmgu7sxu2hyfhlkwuxupa9d5085eunq2qywy7hvmvej456flknswgndm3', + ), + amount: { + type: AmountType.WITHOUT_MULTIASSET, + coin: toUint(2316348), + }, + datumHash: undefined, + }, + ], + fee: toUint(170869), + ttl: toUint(27508111), + certificates: undefined, + withdrawals: undefined, + update: undefined, + auxiliaryDataHash: CanonicalAuxiliaryData.hash, + validityIntervalStart: undefined, + mint: undefined, + scriptDataHash: undefined, + collateralInputs: undefined, + requiredSigners: undefined, + networkId: undefined, + collateralReturnOutput: undefined, + totalCollateral: undefined, + referenceInputs: undefined, + }, + }, + { + testname: 'Simple tx body with non canonical auxiliary data', + cbor: 'a50081825820bc8bf52ea894fb8e442fe3eea628be87d0c9a37baef185b70eb00a5c8a849d3b000181825839000743d16cfe3c4fcc0c11c2403bbc10dbc7ecdd4477e053481a368e7a06e2ae44dff6770dc0f4ada3cf4cf2605008e27aecdb332ad349fda71a0023583c021a00029b75031a01a3bd8f075820fb7099a47afd6efb4f9cccf9d0f8745331a19eb8b3f50548ffadae9de8551743', + auxiliaryData: NonCanonicalAuxiliaryData.data, + txBody: { + inputs: [ + { + transactionId: toFixlenBuffer( + 'bc8bf52ea894fb8e442fe3eea628be87d0c9a37baef185b70eb00a5c8a849d3b', + 32, + ), + index: toUint(0), + }, + ], + outputs: [ + { + format: TxOutputFormat.ARRAY_LEGACY, + address: fromBech32( + 'addr_test1qqr585tvlc7ylnqvz8pyqwauzrdu0mxag3m7q56grgmgu7sxu2hyfhlkwuxupa9d5085eunq2qywy7hvmvej456flknswgndm3', + ), + amount: { + type: AmountType.WITHOUT_MULTIASSET, + coin: toUint(2316348), + }, + datumHash: undefined, + }, + ], + fee: toUint(170869), + ttl: toUint(27508111), + certificates: undefined, + withdrawals: undefined, + update: undefined, + auxiliaryDataHash: NonCanonicalAuxiliaryData.transformedHash, + validityIntervalStart: undefined, + mint: undefined, + scriptDataHash: undefined, + collateralInputs: undefined, + requiredSigners: undefined, + networkId: undefined, + collateralReturnOutput: undefined, + totalCollateral: undefined, + referenceInputs: undefined, + }, + }, + ] + type ValidRawTransactionTestcase = { testname: string cbor: string @@ -609,7 +712,7 @@ export const ValidRawTransactionTestcases: ValidRawTransactionTestcase[] = [ certificates: undefined, withdrawals: undefined, update: undefined, - metadataHash: undefined, + auxiliaryDataHash: undefined, validityIntervalStart: undefined, mint: undefined, scriptDataHash: undefined, @@ -865,7 +968,7 @@ export const ValidRawTransactionTestcases: ValidRawTransactionTestcase[] = [ }, ], update: undefined, - metadataHash: toFixlenBuffer( + auxiliaryDataHash: toFixlenBuffer( 'AF0C57B5A82F10A5D87E3145612B7D9A812D1470A932D182E46C396231072941', 32, ), @@ -981,7 +1084,7 @@ export const ValidRawTransactionTestcases: ValidRawTransactionTestcase[] = [ certificates: undefined, withdrawals: undefined, update: undefined, - metadataHash: undefined, + auxiliaryDataHash: undefined, validityIntervalStart: undefined, mint: undefined, scriptDataHash: toFixlenBuffer( @@ -1093,7 +1196,7 @@ export const ValidTransactionTestcases: ValidTransactionTestcase[] = [ certificates: undefined, withdrawals: undefined, update: undefined, - metadataHash: undefined, + auxiliaryDataHash: undefined, validityIntervalStart: undefined, mint: undefined, scriptDataHash: undefined, @@ -1163,7 +1266,7 @@ export const ValidTransactionTestcases: ValidTransactionTestcase[] = [ certificates: undefined, withdrawals: undefined, update: undefined, - metadataHash: undefined, + auxiliaryDataHash: undefined, validityIntervalStart: undefined, mint: undefined, scriptDataHash: undefined, @@ -1271,7 +1374,7 @@ export const ValidTransactionTestcases: ValidTransactionTestcase[] = [ certificates: undefined, withdrawals: undefined, update: undefined, - metadataHash: undefined, + auxiliaryDataHash: undefined, validityIntervalStart: undefined, mint: undefined, scriptDataHash: toFixlenBuffer(