Skip to content

Commit

Permalink
Recalculate auxiliary data hash when transforming tx
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielKerekes committed Sep 12, 2022
1 parent b45786f commit c5018a7
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"typescript": "^4.4.3"
},
"dependencies": {
"blake2b": "^2.1.4",
"cbor": "^8.1.0"
},
"bugs": {
Expand Down
1 change: 1 addition & 0 deletions src/@types/blake2b.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'blake2b'
18 changes: 15 additions & 3 deletions src/txTransformers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type {
Amount,
Datum,
FixlenBuffer,
METADATA_HASH_LENGTH,
Multiasset,
RawTransaction,
ReferenceScript,
Expand All @@ -9,6 +11,7 @@ import type {
TransactionOutput,
} from './types'
import { AmountType, DatumType, TxOutputFormat } from './types'
import { blake2b256, encodeToCbor } from './utils'

const transformOptionalList = <T>(optionalList?: T[]): T[] | undefined =>
optionalList?.length === 0 ? undefined : optionalList
Expand Down Expand Up @@ -93,7 +96,15 @@ const transformTxOutput = (output: TransactionOutput): TransactionOutput => {
}
}

export const transformTxBody = (txBody: TransactionBody): TransactionBody => ({
const transformAuxiliaryDataHash = (
auxiliaryData: unknown | undefined,
): FixlenBuffer<typeof METADATA_HASH_LENGTH> | undefined =>
auxiliaryData ? blake2b256(encodeToCbor(auxiliaryData)) : undefined

export const transformTxBody = (
txBody: TransactionBody,
auxiliaryData: unknown,
): TransactionBody => ({
...txBody,
outputs: txBody.outputs.map(transformTxOutput),
certificates: transformOptionalList(txBody.certificates),
Expand All @@ -104,14 +115,15 @@ export const transformTxBody = (txBody: TransactionBody): TransactionBody => ({
txBody.collateralReturnOutput &&
transformTxOutput(txBody.collateralReturnOutput),
referenceInputs: transformOptionalList(txBody.referenceInputs),
metadataHash: transformAuxiliaryDataHash(auxiliaryData),
})

export const transformTx = (tx: Transaction): Transaction => ({
...tx,
body: transformTxBody(tx.body),
body: transformTxBody(tx.body, tx.auxiliaryData),
})

export const transformRawTx = (rawTx: RawTransaction): RawTransaction => ({
...rawTx,
body: transformTxBody(rawTx.body),
body: transformTxBody(rawTx.body, rawTx.auxiliaryData),
})
6 changes: 5 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import blake2b from 'blake2b'
import cbor from 'cbor'

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

export enum CborTag {
Expand Down Expand Up @@ -93,3 +94,6 @@ export enum TransactionBodyKeys {
TOTAL_COLLATERAL = 17,
REFERENCE_INPUTS = 18,
}

export const blake2b256 = (data: unknown): FixlenBuffer<32> =>
Buffer.from(blake2b(32).update(data).digest()) as FixlenBuffer<32>
39 changes: 39 additions & 0 deletions test/integration/__fixtures__/auxiliaryData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as cbor from 'cbor'

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

export const CanonicalAuxiliaryData = {
// 259({0: {721: {"ipfs": "some other text", "assetHash": "0931ae2de9aa46212e50adc1798f9071e7c6368b78ae7eb434c2ca0ed9d05370"}}})
data: cbor.decode(
Buffer.from(
'd90103a100a11902d1a264697066736f736f6d65206f74686572207465787469617373657448617368784030393331616532646539616134363231326535306164633137393866393037316537633633363862373861653765623433346332636130656439643035333730',
'hex',
),
),
hash: toFixlenBuffer(
'564702640848981c8b9996b84a13e34cfe4db95914694e0ad46a807da3d05bdb',
32,
),
transformedHash: toFixlenBuffer(
'564702640848981c8b9996b84a13e34cfe4db95914694e0ad46a807da3d05bdb',
32,
),
}

export const NonCanonicalAuxiliaryData = {
// 259({0: {721: {"assetHash": "0931ae2de9aa46212e50adc1798f9071e7c6368b78ae7eb434c2ca0ed9d05370", "ipfs": "some text"}}})
data: cbor.decode(
Buffer.from(
'd90103a100a11902d1a269617373657448617368784030393331616532646539616134363231326535306164633137393866393037316537633633363862373861653765623433346332636130656439643035333730646970667369736f6d652074657874',
'hex',
),
),
hash: toFixlenBuffer(
'fb7099a47afd6efb4f9cccf9d0f8745331a19eb8b3f50548ffadae9de8551743',
32,
),
transformedHash: toFixlenBuffer(
'ef10842a1a55e79310cba366c48b5bd8538039fbbadb01d9a69da74f0a5310f0',
32,
),
}
21 changes: 21 additions & 0 deletions test/integration/transform.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect } from 'chai'

import { decodeTxBody, transformTxBody } from '../../src/index'
import { TransformTransactionTestcases } from './__fixtures__/transactions'

describe('Transform', () => {
for (const {
testname,
cbor,
auxiliaryData,
txBody,
} of TransformTransactionTestcases) {
it(testname, () => {
const transformedTxBody = transformTxBody(
decodeTxBody(Buffer.from(cbor, 'hex')),
auxiliaryData,
)
expect(transformedTxBody).to.deep.equal(txBody)
})
}
})
10 changes: 8 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@
"target": "ES2019",
"typeRoots": ["./node_modules/@types", "./types"],
"noErrorTruncation": true,
"baseUrl": "src"
"baseUrl": "src",
"paths": {
"*": ["./src/@types/*"]
}
},
"include": ["./**/*.ts"]
"include": ["./**/*.ts"],
"ts-node": {
"files": true
}
}
26 changes: 26 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,11 @@ astral-regex@^2.0.0:
resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-2.0.0.tgz#483143c567aeed4785759c0865786dc77d7d2e31"
integrity sha512-Z7tMw1ytTXt5jqMcOP+OQteU1VuNK9Y02uuJtKQ1Sv69jXQKKg5cibLwGJow8yzZP+eAc18EmLGPal0bp36rvQ==

b4a@^1.0.1:
version "1.6.0"
resolved "https://registry.yarnpkg.com/b4a/-/b4a-1.6.0.tgz#5430a9cac1af388910dd1a1c1aa9d3a0a796ed68"
integrity sha512-fsTxXxj1081Yq5MOQ06gZ5+e2QcSyP2U6NofdOWyq+lrNI4IjkZ+fLVmoQ6uUCiNg1NWePMMVq93vOTdbJmErw==

balanced-match@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
Expand All @@ -446,6 +451,22 @@ binary-extensions@^2.0.0:
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.2.0.tgz#75f502eeaf9ffde42fc98829645be4ea76bd9e2d"
integrity sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==

blake2b-wasm@^2.4.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/blake2b-wasm/-/blake2b-wasm-2.4.0.tgz#9115649111edbbd87eb24ce7c04b427e4e2be5be"
integrity sha512-S1kwmW2ZhZFFFOghcx73+ZajEfKBqhP82JMssxtLVMxlaPea1p9uoLiUZ5WYyHn0KddwbLc+0vh4wR0KBNoT5w==
dependencies:
b4a "^1.0.1"
nanoassert "^2.0.0"

blake2b@^2.1.4:
version "2.1.4"
resolved "https://registry.yarnpkg.com/blake2b/-/blake2b-2.1.4.tgz#817d278526ddb4cd673bfb1af16d1ad61e393ba3"
integrity sha512-AyBuuJNI64gIvwx13qiICz6H6hpmjvYS5DGkG6jbXMOT8Z3WUJ3V1X0FlhIoT1b/5JtHE3ki+xjtMvu1nn+t9A==
dependencies:
blake2b-wasm "^2.4.0"
nanoassert "^2.0.0"

brace-expansion@^1.1.7:
version "1.1.11"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
Expand Down Expand Up @@ -1619,6 +1640,11 @@ [email protected], ms@^2.1.1:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==

nanoassert@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/nanoassert/-/nanoassert-2.0.0.tgz#a05f86de6c7a51618038a620f88878ed1e490c09"
integrity sha512-7vO7n28+aYO4J+8w96AzhmU8G+Y/xpPDJz/se19ICsqj/momRbb9mh9ZUtkoJ5X3nTnPdhEJyc0qnM6yAsHBaA==

[email protected]:
version "3.3.1"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.1.tgz#6347a18cac88af88f58af0b3594b723d5e99bb35"
Expand Down

0 comments on commit c5018a7

Please sign in to comment.