Skip to content

Commit

Permalink
fix(core)!: set tx body scriptIntegrityHash to correct length (32 byte)
Browse files Browse the repository at this point in the history
implement coreToCsl txBody collaterals, required signers and script data hash
  • Loading branch information
mkazlauskas committed Apr 1, 2022
1 parent 0e0b592 commit 37822ce
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 22 deletions.
73 changes: 55 additions & 18 deletions packages/core/src/CSL/coreToCsl/coreToCsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
AuxiliaryData,
BigNum,
Certificates,
Ed25519KeyHash,
Ed25519KeyHashes,
Ed25519Signature,
GeneralTransactionMetadata,
Int,
Expand All @@ -17,6 +19,7 @@ import {
MultiAsset,
PublicKey,
RewardAddress,
ScriptDataHash,
ScriptHash,
Transaction,
TransactionBody,
Expand Down Expand Up @@ -157,20 +160,56 @@ export const txAuxiliaryData = (auxiliaryData?: Cardano.AuxiliaryData): Auxiliar
return result;
};

export const txBody = (
{ inputs, outputs, fee, validityInterval, certificates, withdrawals, mint }: Cardano.TxBodyAlonzo,
auxiliaryData?: Cardano.AuxiliaryData
): TransactionBody => {
const txInputs = (coreInputs: Cardano.TxIn[]) => {
const cslInputs = TransactionInputs.new();
for (const input of inputs) {
for (const input of coreInputs) {
cslInputs.add(txIn(input));
}
return cslInputs;
};

const keyHashes = (coreHashes: Cardano.Ed25519KeyHash[]) => {
const cslKeyHashes = Ed25519KeyHashes.new();
for (const signature of coreHashes) {
cslKeyHashes.add(Ed25519KeyHash.from_bytes(Buffer.from(signature, 'hex')));
}
return cslKeyHashes;
};

const txWithdrawals = (coreWithdrawals: Cardano.Withdrawal[]) => {
const cslWithdrawals = Withdrawals.new();
for (const { stakeAddress, quantity } of coreWithdrawals) {
const cslAddress = RewardAddress.from_address(Address.from_bech32(stakeAddress.toString()));
if (!cslAddress) {
throw new SerializationError(SerializationFailure.InvalidAddress, `Invalid withdrawal address: ${stakeAddress}`);
}
cslWithdrawals.insert(cslAddress, BigNum.from_str(quantity.toString()));
}
return cslWithdrawals;
};

// eslint-disable-next-line complexity
export const txBody = (
{
inputs,
outputs,
fee,
validityInterval,
certificates,
withdrawals,
mint,
collaterals,
requiredExtraSignatures,
scriptIntegrityHash
}: Cardano.TxBodyAlonzo,
auxiliaryData?: Cardano.AuxiliaryData
): TransactionBody => {
const cslOutputs = TransactionOutputs.new();
for (const output of outputs) {
cslOutputs.add(txOut(output));
}
const cslBody = TransactionBody.new(
cslInputs,
txInputs(inputs),
cslOutputs,
BigNum.from_str(fee.toString()),
validityInterval.invalidHereafter
Expand All @@ -181,6 +220,15 @@ export const txBody = (
if (mint) {
cslBody.set_mint(txMint(mint));
}
if (collaterals) {
cslBody.set_collateral(txInputs(collaterals));
}
if (requiredExtraSignatures?.length) {
cslBody.set_required_signers(keyHashes(requiredExtraSignatures));
}
if (scriptIntegrityHash) {
cslBody.set_script_data_hash(ScriptDataHash.from_bytes(Buffer.from(scriptIntegrityHash, 'hex')));
}
if (certificates?.length) {
const certs = Certificates.new();
for (const cert of certificates) {
Expand All @@ -189,18 +237,7 @@ export const txBody = (
cslBody.set_certs(certs);
}
if (withdrawals?.length) {
const cslWithdrawals = Withdrawals.new();
for (const { stakeAddress, quantity } of withdrawals) {
const cslAddress = RewardAddress.from_address(Address.from_bech32(stakeAddress.toString()));
if (!cslAddress) {
throw new SerializationError(
SerializationFailure.InvalidAddress,
`Invalid withdrawal address: ${stakeAddress}`
);
}
cslWithdrawals.insert(cslAddress, BigNum.from_str(quantity.toString()));
}
cslBody.set_withdrawals(cslWithdrawals);
cslBody.set_withdrawals(txWithdrawals(withdrawals));
}
const cslAuxiliaryData = txAuxiliaryData(auxiliaryData);
if (cslAuxiliaryData) {
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/CSL/cslToCore/cslToCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ export const txMint = (assets?: CSL.Mint): Cardano.TokenMap | undefined => {
};

export const txBody = (body: CSL.TransactionBody): Cardano.TxBodyAlonzo => {
const { script_data_hash } = body;

const cslScriptDataHash = body.script_data_hash();
const cslCollaterals = body.collateral();

return {
Expand All @@ -132,7 +131,7 @@ export const txBody = (body: CSL.TransactionBody): Cardano.TxBodyAlonzo => {
outputs: txOutputs(body.outputs()),
requiredExtraSignatures: txRequiredExtraSignatures(body.required_signers()),
scriptIntegrityHash:
script_data_hash && Cardano.util.Hash28ByteBase16(Buffer.from(script_data_hash()!.to_bytes()).toString()),
cslScriptDataHash && Cardano.util.Hash28ByteBase16(Buffer.from(cslScriptDataHash.to_bytes()).toString('hex')),
validityInterval: {
invalidBefore: body.validity_start_interval(),
invalidHereafter: body.ttl()
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Cardano/types/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface TxBodyAlonzo {
withdrawals?: Withdrawal[];
certificates?: Cardano.Certificate[];
mint?: Cardano.TokenMap;
scriptIntegrityHash?: Cardano.Hash28ByteBase16;
scriptIntegrityHash?: Cardano.Hash32ByteBase16;
requiredExtraSignatures?: Cardano.Ed25519KeyHash[];
}

Expand Down
5 changes: 5 additions & 0 deletions packages/core/test/CSL/coreToCsl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,17 @@ describe('coreToCsl', () => {
expect(cslBody.withdrawals()?.get(cslBody.withdrawals()!.keys().get(0)!)?.to_str()).toBe(
txBody.withdrawals![0].quantity.toString()
);

const mint = cslBody.multiassets()!;
const scriptHashes = mint.keys();
const mintAssets1 = mint.get(scriptHashes.get(0))!;
const mintAssets2 = mint.get(scriptHashes.get(1))!;
expect(mintAssets1.get(mintAssets1.keys().get(0))!.as_positive()!.to_str()).toBe('20');
expect(mintAssets2.get(mintAssets2.keys().get(0))!.as_negative()!.to_str()).toBe('50');

expect(cslBody.collateral()!.len()).toBe(1);
expect(cslBody.required_signers()!.len()).toBe(1);
expect(cslBody.script_data_hash()).toBeTruthy();
});
it('tx', () => {
const cslTx = coreToCsl.tx(tx);
Expand Down
3 changes: 3 additions & 0 deletions packages/core/test/CSL/cslToCore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ describe('cslToCore', () => {
test.skip('newTx', () => {
expect(cslToCore.newTx(coreToCsl.tx(tx))).toEqual(tx);
});

it.todo('txScripts');
it.todo('txRequiredExtraSignatures');
});
3 changes: 3 additions & 0 deletions packages/core/test/CSL/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,13 @@ export const txBody: Cardano.TxBodyAlonzo = {
poolId: Cardano.PoolId('pool1y6chk7x7fup4ms9leesdr57r4qy9cwxuee0msan72x976a6u0nc')
}
],
collaterals: [{ ...txIn, index: txIn.index + 1 }],
fee: 10n,
inputs: [txInWithAddress],
mint: mintTokenMap,
outputs: [txOut],
requiredExtraSignatures: [Cardano.Ed25519KeyHash('6199186adb51974690d7247d2646097d2c62763b16fb7ed3f9f55d39')],
scriptIntegrityHash: Cardano.Hash32ByteBase16('6199186adb51974690d7247d2646097d2c62763b16fb7ed3f9f55d38abc123de'),
validityInterval: {
invalidBefore: 100,
invalidHereafter: 1000
Expand Down

0 comments on commit 37822ce

Please sign in to comment.