Skip to content

Commit

Permalink
feat(core): add coreToCsl.txAuxiliaryData
Browse files Browse the repository at this point in the history
  • Loading branch information
mkazlauskas committed Feb 17, 2022
1 parent 48c33e5 commit 8686610
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 12 deletions.
76 changes: 64 additions & 12 deletions packages/core/src/CSL/coreToCsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import * as Cardano from '../Cardano';
import {
Address,
Assets,
AuxiliaryData,
BigNum,
Certificates,
Ed25519Signature,
GeneralTransactionMetadata,
Int,
MetadataList,
MetadataMap,
MultiAsset,
PublicKey,
RewardAddress,
Expand All @@ -14,6 +19,7 @@ import {
TransactionHash,
TransactionInput,
TransactionInputs,
TransactionMetadatum,
TransactionOutput,
TransactionOutputs,
TransactionUnspentOutput,
Expand All @@ -22,13 +28,14 @@ import {
Vkey,
Vkeywitness,
Vkeywitnesses,
Withdrawals
Withdrawals,
hash_auxiliary_data
} from '@emurgo/cardano-serialization-lib-nodejs';

import * as certificate from './certificate';
import { SerializationError } from '../errors';
import { SerializationFailure } from '..';
import { coreToCsl, parseCslAddress } from '.';
import { parseCslAddress } from './parseCslAddress';
export * as certificate from './certificate';

export const tokenMap = (assets: Cardano.TokenMap) => {
Expand Down Expand Up @@ -63,14 +70,54 @@ export const txOut = (core: Cardano.TxOut): TransactionOutput =>
export const utxo = (core: Cardano.Utxo[]): TransactionUnspentOutput[] =>
core.map((item) => TransactionUnspentOutput.new(txIn(item[0]), txOut(item[1])));

export const txBody = ({
inputs,
outputs,
fee,
validityInterval,
certificates,
withdrawals
}: Cardano.TxBodyAlonzo): TransactionBody => {
export const txMetadatum = (metadatum: Cardano.Metadatum): TransactionMetadatum => {
switch (typeof metadatum) {
case 'bigint':
return TransactionMetadatum.new_int(Int.new(BigNum.from_str(metadatum.toString())));
case 'string':
return TransactionMetadatum.new_text(metadatum);
default: {
if (Array.isArray(metadatum)) {
const metadataList = MetadataList.new();
for (const metadataItem of metadatum) {
metadataList.add(txMetadatum(metadataItem));
}
return TransactionMetadatum.new_list(metadataList);
} else if (ArrayBuffer.isView(metadatum)) {
return TransactionMetadatum.new_bytes(metadatum);
}
const metadataMap = MetadataMap.new();
for (const [key, data] of metadatum.entries()) {
metadataMap.insert(txMetadatum(key), txMetadatum(data));
}
return TransactionMetadatum.new_map(metadataMap);
}
}
};

export const txMetadata = (blob: Map<bigint, Cardano.Metadatum>): GeneralTransactionMetadata => {
const metadata = GeneralTransactionMetadata.new();
for (const [key, data] of blob.entries()) {
metadata.insert(BigNum.from_str(key.toString()), txMetadatum(data));
}
return metadata;
};

export const txAuxiliaryData = (auxiliaryData?: Cardano.AuxiliaryData): AuxiliaryData | undefined => {
if (!auxiliaryData) return;
const result = AuxiliaryData.new();
// TODO: add support for auxiliaryData.scripts
const { blob } = auxiliaryData.body;
if (blob) {
result.set_metadata(txMetadata(blob));
}
return result;
};

export const txBody = (
{ inputs, outputs, fee, validityInterval, certificates, withdrawals }: Cardano.TxBodyAlonzo,
auxiliaryData?: Cardano.AuxiliaryData
): TransactionBody => {
const cslInputs = TransactionInputs.new();
for (const input of inputs) {
cslInputs.add(txIn(input));
Expand Down Expand Up @@ -109,10 +156,14 @@ export const txBody = ({
}
cslBody.set_withdrawals(cslWithdrawals);
}
const cslAuxiliaryData = txAuxiliaryData(auxiliaryData);
if (cslAuxiliaryData) {
cslBody.set_auxiliary_data_hash(hash_auxiliary_data(cslAuxiliaryData));
}
return cslBody;
};

export const tx = ({ body, witness }: Cardano.NewTxAlonzo): Transaction => {
export const tx = ({ body, witness, auxiliaryData }: Cardano.NewTxAlonzo): Transaction => {
const witnessSet = TransactionWitnessSet.new();
const vkeyWitnesses = Vkeywitnesses.new();
for (const [vkey, signature] of witness.signatures.entries()) {
Expand All @@ -121,5 +172,6 @@ export const tx = ({ body, witness }: Cardano.NewTxAlonzo): Transaction => {
vkeyWitnesses.add(vkeyWitness);
}
witnessSet.set_vkeys(vkeyWitnesses);
return Transaction.new(coreToCsl.txBody(body), witnessSet);
// Possible optimization: only convert auxiliary data once
return Transaction.new(txBody(body, auxiliaryData), witnessSet, txAuxiliaryData(auxiliaryData));
};
54 changes: 54 additions & 0 deletions packages/core/test/CSL/coreToCsl.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable max-len */
import { Asset, CSL, Cardano, coreToCsl } from '../../src';
import { BigNum } from '@emurgo/cardano-serialization-lib-nodejs';

const txIn: Cardano.TxIn = {
address: Cardano.Address(
Expand Down Expand Up @@ -122,4 +123,57 @@ describe('coreToCsl', () => {
expect(Buffer.from(witness.vkey().public_key().as_bytes()).toString('hex')).toBe(vkey);
expect(witness.signature().to_hex()).toBe(signature);
});
describe('txAuxiliaryData', () => {
it('returns undefined for undefined data', () => expect(coreToCsl.txAuxiliaryData()).toBeUndefined());

describe('txMetadata', () => {
// eslint-disable-next-line unicorn/consistent-function-scoping
const convertMetadatum = (metadatum: Cardano.Metadatum) => {
const label = 123n;
const auxiliaryData = coreToCsl.txAuxiliaryData({ body: { blob: new Map([[label, metadatum]]) } });
return auxiliaryData?.metadata()?.get(BigNum.from_str(label.toString()));
};

it('converts number', () => {
const number = 1234n;
const metadatum = convertMetadatum(number);
expect(metadatum?.as_int().as_positive()?.to_str()).toBe(number.toString());
});

it('converts text', () => {
const str = 'str';
const metadatum = convertMetadatum(str);
expect(metadatum?.as_text()).toBe(str);
});

it('converts list', () => {
const list = ['str1', 'str2'];
const metadatum = convertMetadatum(list);
const cslList = metadatum?.as_list();
expect(cslList?.len()).toBe(list.length);
expect(cslList?.get(0).as_text()).toBe(list[0]);
expect(cslList?.get(1).as_text()).toBe(list[1]);
});

test('converts bytes', () => {
const bytes = Buffer.from('str');
const metadatum = convertMetadatum(bytes);
expect(metadatum?.as_bytes().buffer).toEqual(bytes.buffer);
});

it('converts map', () => {
const map = new Map<Cardano.Metadatum, Cardano.Metadatum>([
[123n, 1234n],
['key', 'value']
]);
const metadatum = convertMetadatum(map);
const cslMap = metadatum?.as_map();
expect(cslMap?.len()).toBe(map.size);
expect(cslMap?.get(convertMetadatum(123n)!).as_int().as_positive()?.to_str()).toBe('1234');
expect(cslMap?.get(convertMetadatum('key')!).as_text()).toBe('value');
});
});

it.todo('txScripts');
});
});

0 comments on commit 8686610

Please sign in to comment.