Skip to content

Commit

Permalink
Rename metadata hash to auxiliary data hash
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielKerekes committed Sep 12, 2022
1 parent c5018a7 commit 015bae7
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/errors/parseError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
17 changes: 9 additions & 8 deletions src/txParsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@ import type {
import {
AmountType,
ASSET_NAME_MAX_LENGTH,
AUXILIARY_DATA_HASH_LENGTH,
CertificateType,
DATUM_HASH_LENGTH,
DatumType,
DNS_NAME_MAX_LENGTH,
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,
Expand Down Expand Up @@ -422,7 +423,7 @@ const parsePoolMetadata = (unparsedPoolMetadata: unknown): PoolMetadata => {
),
createParser(
parseBufferOfLength,
METADATA_HASH_LENGTH,
POOL_METADATA_HASH_LENGTH,
ParseErrorReason.INVALID_POOL_METADATA_METADATA_HASH,
),
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
5 changes: 4 additions & 1 deletion src/txSerializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions src/txTransformers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {
Amount,
AUXILIARY_DATA_HASH_LENGTH,
Datum,
FixlenBuffer,
METADATA_HASH_LENGTH,
Multiasset,
RawTransaction,
ReferenceScript,
Expand Down Expand Up @@ -98,7 +98,7 @@ const transformTxOutput = (output: TransactionOutput): TransactionOutput => {

const transformAuxiliaryDataHash = (
auxiliaryData: unknown | undefined,
): FixlenBuffer<typeof METADATA_HASH_LENGTH> | undefined =>
): FixlenBuffer<typeof AUXILIARY_DATA_HASH_LENGTH> | undefined =>
auxiliaryData ? blake2b256(encodeToCbor(auxiliaryData)) : undefined

export const transformTxBody = (
Expand All @@ -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 => ({
Expand Down
7 changes: 4 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -198,7 +199,7 @@ export type Relay =

export type PoolMetadata = {
url: MaxlenString<typeof URL_MAX_LENGTH>
metadataHash: FixlenBuffer<typeof METADATA_HASH_LENGTH>
metadataHash: FixlenBuffer<typeof POOL_METADATA_HASH_LENGTH>
}

export type PoolParams = {
Expand Down Expand Up @@ -264,7 +265,7 @@ export type TransactionBody = {
certificates?: Certificate[]
withdrawals?: Withdrawal[]
update?: Unparsed
metadataHash?: FixlenBuffer<typeof METADATA_HASH_LENGTH>
auxiliaryDataHash?: FixlenBuffer<typeof AUXILIARY_DATA_HASH_LENGTH>
validityIntervalStart?: Uint
mint?: Mint
scriptDataHash?: FixlenBuffer<typeof SCRIPT_DATA_HASH_LENGTH>
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
127 changes: 115 additions & 12 deletions test/integration/__fixtures__/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import {
toMaxLenString,
toUint,
} from '../../test_utils'
import {
CanonicalAuxiliaryData,
NonCanonicalAuxiliaryData,
} from './auxiliaryData'

type ValidTransactionBodyTestcase = {
testname: string
Expand Down Expand Up @@ -62,7 +66,7 @@ export const ValidTransactionBodyTestcases: ValidTransactionBodyTestcase[] = [
certificates: undefined,
withdrawals: undefined,
update: undefined,
metadataHash: undefined,
auxiliaryDataHash: undefined,
validityIntervalStart: undefined,
mint: undefined,
scriptDataHash: undefined,
Expand Down Expand Up @@ -118,7 +122,7 @@ export const ValidTransactionBodyTestcases: ValidTransactionBodyTestcase[] = [
},
],
update: undefined,
metadataHash: undefined,
auxiliaryDataHash: undefined,
validityIntervalStart: undefined,
mint: undefined,
scriptDataHash: undefined,
Expand Down Expand Up @@ -204,7 +208,7 @@ export const ValidTransactionBodyTestcases: ValidTransactionBodyTestcase[] = [
certificates: undefined,
withdrawals: undefined,
update: undefined,
metadataHash: undefined,
auxiliaryDataHash: undefined,
validityIntervalStart: undefined,
mint: [
{
Expand Down Expand Up @@ -366,7 +370,7 @@ export const ValidTransactionBodyTestcases: ValidTransactionBodyTestcase[] = [
],
withdrawals: undefined,
update: undefined,
metadataHash: undefined,
auxiliaryDataHash: undefined,
validityIntervalStart: undefined,
mint: undefined,
scriptDataHash: undefined,
Expand Down Expand Up @@ -447,7 +451,7 @@ export const ValidTransactionBodyTestcases: ValidTransactionBodyTestcase[] = [
certificates: undefined,
withdrawals: undefined,
update: undefined,
metadataHash: undefined,
auxiliaryDataHash: undefined,
validityIntervalStart: undefined,
mint: undefined,
scriptDataHash: toFixlenBuffer(
Expand Down Expand Up @@ -518,7 +522,7 @@ export const ValidTransactionBodyTestcases: ValidTransactionBodyTestcase[] = [
certificates: undefined,
withdrawals: undefined,
update: undefined,
metadataHash: undefined,
auxiliaryDataHash: undefined,
validityIntervalStart: undefined,
mint: undefined,
scriptDataHash: toFixlenBuffer(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -609,7 +712,7 @@ export const ValidRawTransactionTestcases: ValidRawTransactionTestcase[] = [
certificates: undefined,
withdrawals: undefined,
update: undefined,
metadataHash: undefined,
auxiliaryDataHash: undefined,
validityIntervalStart: undefined,
mint: undefined,
scriptDataHash: undefined,
Expand Down Expand Up @@ -865,7 +968,7 @@ export const ValidRawTransactionTestcases: ValidRawTransactionTestcase[] = [
},
],
update: undefined,
metadataHash: toFixlenBuffer(
auxiliaryDataHash: toFixlenBuffer(
'AF0C57B5A82F10A5D87E3145612B7D9A812D1470A932D182E46C396231072941',
32,
),
Expand Down Expand Up @@ -981,7 +1084,7 @@ export const ValidRawTransactionTestcases: ValidRawTransactionTestcase[] = [
certificates: undefined,
withdrawals: undefined,
update: undefined,
metadataHash: undefined,
auxiliaryDataHash: undefined,
validityIntervalStart: undefined,
mint: undefined,
scriptDataHash: toFixlenBuffer(
Expand Down Expand Up @@ -1093,7 +1196,7 @@ export const ValidTransactionTestcases: ValidTransactionTestcase[] = [
certificates: undefined,
withdrawals: undefined,
update: undefined,
metadataHash: undefined,
auxiliaryDataHash: undefined,
validityIntervalStart: undefined,
mint: undefined,
scriptDataHash: undefined,
Expand Down Expand Up @@ -1163,7 +1266,7 @@ export const ValidTransactionTestcases: ValidTransactionTestcase[] = [
certificates: undefined,
withdrawals: undefined,
update: undefined,
metadataHash: undefined,
auxiliaryDataHash: undefined,
validityIntervalStart: undefined,
mint: undefined,
scriptDataHash: undefined,
Expand Down Expand Up @@ -1271,7 +1374,7 @@ export const ValidTransactionTestcases: ValidTransactionTestcase[] = [
certificates: undefined,
withdrawals: undefined,
update: undefined,
metadataHash: undefined,
auxiliaryDataHash: undefined,
validityIntervalStart: undefined,
mint: undefined,
scriptDataHash: toFixlenBuffer(
Expand Down

0 comments on commit 015bae7

Please sign in to comment.