Skip to content

Commit

Permalink
feat(core): add hasTaggedSets function to TransactionBody
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelCastilloB committed Sep 4, 2024
1 parent a0fa7c7 commit c785428
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable sonarjs/cognitive-complexity, complexity, sonarjs/cognitive-complexity, max-statements */
import * as Crypto from '@cardano-sdk/crypto';
import { Address, RewardAddress } from '../../Cardano/Address';
import { CborReader, CborReaderState, CborWriter } from '../CBOR';
import { CborReader, CborReaderState, CborTag, CborWriter } from '../CBOR';
import { CborSet, Hash } from '../Common';
import { Certificate } from '../Certificates';
import { HexBlob } from '@cardano-sdk/util';
Expand Down Expand Up @@ -924,11 +924,26 @@ export class TransactionBody {
return this.#donation;
}

/**
* Computes the hash of the transaction body.
*
* @returns The hash of the transaction body.
*/
hash() {
const hash = Crypto.blake2b(Crypto.blake2b.BYTES).update(hexToBytes(this.toCbor())).digest();
return TransactionId.fromHexBlob(HexBlob.fromBytes(hash));
}

/**
* Checks if the transaction body has tagged sets.
*
* @returns true if the transaction body has tagged sets, false otherwise.
*/
hasTaggedSets() {
const reader = new CborReader(this.#inputs.toCbor());
return reader.peekState() === CborReaderState.Tag && reader.peekTag() === CborTag.Set;
}

/**
* Gets the size of the serialized map.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,14 @@ describe('TransactionBody', () => {
// hex characters only
expect(Buffer.from(txId, 'hex').toString('hex')).toEqual(txId);
});

it('hasTaggedSets() returns true for transaction bodies with 6.258 tags', () => {
const body = TransactionBody.fromCbor(conwayCborWithSets);
expect(body.hasTaggedSets()).toBe(true);
});

it('hasTaggedSets() returns false for transaction bodies without 6.258 tags', () => {
const body = TransactionBody.fromCbor(cbor);
expect(body.hasTaggedSets()).toBe(false);
});
});

0 comments on commit c785428

Please sign in to comment.