Skip to content

Commit ab26ecf

Browse files
feat(core): add hasBabbageOutput method to TransactionBody
1 parent 17add5a commit ab26ecf

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

packages/core/src/Serialization/TransactionBody/TransactionBody.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,19 @@ export class TransactionBody {
947947
return reader.peekState() === CborReaderState.Tag && reader.peekTag() === CborTag.Set;
948948
}
949949

950+
/**
951+
* Checks if the transaction body has Babbage outputs.
952+
*
953+
* @returns true if the transaction body has Babbage outputs, false otherwise.
954+
*/
955+
hasBabbageOutput(): boolean {
956+
if (this.#outputs.length === 0) return false;
957+
958+
const reader = new CborReader(this.#outputs[0].toCbor());
959+
960+
return reader.peekState() === CborReaderState.StartMap;
961+
}
962+
950963
/**
951964
* Gets the size of the serialized map.
952965
*

packages/core/test/Serialization/TransactionBody/TransactionBody.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import * as Cardano from '../../../src/Cardano';
33
import * as Crypto from '@cardano-sdk/crypto';
44
import { HexBlob } from '@cardano-sdk/util';
5-
import { TransactionBody, TxBodyCBOR, TxCBOR } from '../../../src/Serialization';
5+
import { Transaction, TransactionBody, TxBodyCBOR, TxCBOR } from '../../../src/Serialization';
66
import { babbageTx } from '../testData';
77
import { mintTokenMap, params, txIn, txOut } from './testData';
88

@@ -273,6 +273,24 @@ describe('TransactionBody', () => {
273273
expect(body.toCore()).toEqual(expectedConwayCore);
274274
});
275275

276+
it('can encode identify transactions output format - Babbage outputs', () => {
277+
const tx = Transaction.fromCbor(
278+
TxCBOR(
279+
'84a500818258207aa1264bcd0c06f34a49ed1dd7307a2bdec5a97bdeb546498759ad5b8ed42fd5010182a200583930195bde3deacb613b7e9eb6280b14db4e353e475e96d19f3f7a5e2d66195bde3deacb613b7e9eb6280b14db4e353e475e96d19f3f7a5e2d66011a00e4e1c0a2005839003d3246dc0c50ab3c74a8ffdd8068313ff99d341c461a8fe31f416d0a8fba06d60d71edc077cc5ebcb8ff82137afbce68df98271909332348011b000000025106a838021a00029309031a04a07bc6081a04a07a40a0f5f6'
280+
)
281+
);
282+
expect(tx.body().hasBabbageOutput()).toBeTruthy();
283+
});
284+
285+
it('can encode identify transactions output format - Legacy outputs', () => {
286+
const tx = Transaction.fromCbor(
287+
TxCBOR(
288+
'84a500818258207aa1264bcd0c06f34a49ed1dd7307a2bdec5a97bdeb546498759ad5b8ed42fd501018282583930195bde3deacb613b7e9eb6280b14db4e353e475e96d19f3f7a5e2d66195bde3deacb613b7e9eb6280b14db4e353e475e96d19f3f7a5e2d661a00e4e1c0825839003d3246dc0c50ab3c74a8ffdd8068313ff99d341c461a8fe31f416d0a8fba06d60d71edc077cc5ebcb8ff82137afbce68df982719093323481b000000025106a838021a00029309031a04a07bc6081a04a07a40a0f5f6'
289+
)
290+
);
291+
expect(tx.body().hasBabbageOutput()).toBeFalsy();
292+
});
293+
276294
it('sorts withdrawals canonically', () => {
277295
const body = TransactionBody.fromCbor(cbor);
278296
const withdrawals = body.withdrawals();

0 commit comments

Comments
 (0)