Skip to content

Commit

Permalink
fix(core): correctly serialize/deserialize transactions with no valid…
Browse files Browse the repository at this point in the history
…ity interval
  • Loading branch information
mkazlauskas committed Dec 7, 2022
1 parent d7eb8d9 commit 5e6e3eb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
17 changes: 12 additions & 5 deletions packages/core/src/CML/cmlToCore/cmlToCore.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as Cardano from '../../Cardano';
import { CML } from '../CML';
import { ManagedFreeableScope, usingAutoFree } from '@cardano-sdk/util';
import { SerializationError, SerializationFailure } from '../../errors';
import { bytesToHex } from '../../util/misc/bytesToHex';
import { createAssetId } from '../../Asset/util/assetId';
import { createCertificate } from './certificate';
import { usingAutoFree } from '@cardano-sdk/util';

export const txRequiredExtraSignatures = (
signatures: CML.Ed25519KeyHashes | undefined
Expand Down Expand Up @@ -129,6 +129,16 @@ export const txMint = (assets?: CML.Mint): Cardano.TokenMap | undefined =>
return assetMap;
});

const validityInterval = (scope: ManagedFreeableScope, body: CML.TransactionBody) => {
const cmlInvalidBefore = body.validity_start_interval();
const cmlInvalidHereafter = body.ttl();
if (!cmlInvalidBefore && !cmlInvalidHereafter) return;
return {
invalidBefore: cmlInvalidBefore ? Cardano.Slot(Number(scope.manage(cmlInvalidBefore).to_str())) : undefined,
invalidHereafter: cmlInvalidHereafter ? Cardano.Slot(Number(scope.manage(cmlInvalidHereafter).to_str())) : undefined
};
};

export const txBody = (body: CML.TransactionBody): Cardano.TxBody =>
usingAutoFree((scope) => {
const cslScriptDataHash = scope.manage(body.script_data_hash());
Expand All @@ -144,10 +154,7 @@ export const txBody = (body: CML.TransactionBody): Cardano.TxBody =>
requiredExtraSignatures: txRequiredExtraSignatures(scope.manage(body.required_signers())),
scriptIntegrityHash:
cslScriptDataHash && Cardano.util.Hash32ByteBase16(Buffer.from(cslScriptDataHash.to_bytes()).toString('hex')),
validityInterval: {
invalidBefore: Cardano.Slot(Number(scope.manage(body.validity_start_interval())?.to_str())),
invalidHereafter: Cardano.Slot(Number(scope.manage(body.ttl())?.to_str()))
},
validityInterval: validityInterval(scope, body),
withdrawals: txWithdrawals(scope.manage(body.withdrawals()))
};
});
Expand Down
12 changes: 7 additions & 5 deletions packages/core/src/CML/coreToCml/coreToCml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,16 @@ export const txBody = (
txInputs(scope, inputs),
cslOutputs,
scope.manage(BigNum.from_str(fee.toString())),
BigNum.from_str(validityInterval?.invalidHereafter ? validityInterval.invalidHereafter.toString() : '0')
typeof validityInterval?.invalidHereafter !== 'undefined'
? // TransactionBody appears to be taking ownership of this object: do not scope.manage
// https://github.com/dcSpark/cardano-multiplatform-lib/blob/master/rust/src/lib.rs#L413
BigNum.from_str(validityInterval.invalidHereafter.toString())
: undefined
)
);

if (validityInterval?.invalidBefore) {
cslBody.set_validity_start_interval(
BigNum.from_str(validityInterval?.invalidBefore ? validityInterval.invalidBefore.toString() : '0')
);
if (typeof validityInterval?.invalidBefore !== 'undefined') {
cslBody.set_validity_start_interval(scope.manage(BigNum.from_str(validityInterval.invalidBefore.toString())));
}
if (mint) {
cslBody.set_mint(txMint(scope, mint));
Expand Down
29 changes: 29 additions & 0 deletions packages/core/test/CML/reserialization.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint-disable max-len */
import { CML, cmlToCore, coreToCml } from '../../src';
import { ManagedFreeableScope } from '@cardano-sdk/util';

describe('reserialization', () => {
let scope: ManagedFreeableScope;

const testReserialization = (testName: string, originalSerialized: string) =>
test(testName, () => {
const cmlTx = CML.Transaction.from_bytes(Buffer.from(originalSerialized, 'hex'));
const deserialized = cmlToCore.newTx(cmlTx);
const cmlTxReserialized = coreToCml.tx(scope, deserialized);
const reserialized = Buffer.from(cmlTxReserialized.to_bytes()).toString('hex');
expect(reserialized).toEqual(originalSerialized);
});

beforeEach(() => {
scope = new ManagedFreeableScope();
});

afterEach(() => {
scope.dispose();
});

testReserialization(
'simple tx',
'84a30081825820c2df24d66077bc77f5a566b211662fe1bd85820024b37fb1a3f13eca263f80c1010182825839003492aa317ad5a41ceaf03f0ff7ba16b4760c63614d75d02a3fec7d98b903affb7b9b39ecad90b3023e6154764e65e9e7a1567d8f7ff9a6251a002dc6c0825839005cf6c91279a859a072601779fb33bb07c34e1d641d45df51ff63b967f15db05f56035465bf8900a09bdaa16c3d8b8244fea686524408dd801b0000048c2708f81b021a00029125a10081825820368cf6a11ac7e29917568a366af62a596cb9cde8174bfe7f6e88393ecdb1dcc65840b2eb6ad0b749246dc9d6e8f6fca2793781688dadcf364a37c2e3713ede7bd40dd04c48289a0252be3b5ff413e8536879b8482a916e481b472e83169c51a1e508f5f6'
);
});

0 comments on commit 5e6e3eb

Please sign in to comment.