Skip to content

Commit

Permalink
refactor!: change redeemer script hash to data
Browse files Browse the repository at this point in the history
Interpret redeemer's `scriptHash` as data with `HexBlob` type in core.

BREAKING CHANGE:
- rename `redeemer.scriptHash` to `redeemer.data` in core
- change the type from `Hash28ByteBase16` to `HexBlob`
  • Loading branch information
Ivaylo Andonov committed Nov 30, 2022
1 parent ce687b3 commit 87452b9
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 21 deletions.
Expand Up @@ -96,13 +96,13 @@ export const mapWithdrawal = (withdrawalModel: WithdrawalModel): Cardano.Withdra
});

export const mapRedeemer = (redeemerModel: RedeemerModel): Cardano.Redeemer => ({
data: Cardano.util.HexBlob(redeemerModel.script_hash.toString('hex')),
executionUnits: {
memory: Number(redeemerModel.unit_mem),
steps: Number(redeemerModel.unit_steps)
},
index: redeemerModel.index,
purpose: redeemerModel.purpose as Cardano.RedeemerPurpose,
scriptHash: Cardano.util.Hash28ByteBase16(redeemerModel.script_hash.toString('hex'))
purpose: redeemerModel.purpose as Cardano.RedeemerPurpose
});

export const mapCertificate = (
Expand Down
2 changes: 1 addition & 1 deletion packages/cardano-services/src/ChainHistory/openApi.json
Expand Up @@ -361,7 +361,7 @@
"withdrawal"
]
},
"scriptHash": {
"data": {
"type": "string"
}
}
Expand Down
Expand Up @@ -286,13 +286,13 @@ describe('chain history mappers', () => {
test('map RedeemerModel to Cardano.Redeemer', () => {
const result = mappers.mapRedeemer(redeemerModel);
expect(result).toEqual<Cardano.Redeemer>({
data: Cardano.util.HexBlob(hash28ByteBase16),
executionUnits: {
memory: 2000,
steps: 5000
},
index: 1,
purpose: Cardano.RedeemerPurpose.mint,
scriptHash: Cardano.util.Hash28ByteBase16(hash28ByteBase16)
purpose: Cardano.RedeemerPurpose.mint
});
});
});
Expand All @@ -303,10 +303,10 @@ describe('chain history mappers', () => {
const outputs: Cardano.TxOut[] = [{ address: Cardano.Address(address), value: { assets, coins: 20_000_000n } }];
const redeemers: Cardano.Redeemer[] = [
{
data: Cardano.util.HexBlob(hash28ByteBase16),
executionUnits: { memory: 1, steps: 2 },
index: 1,
purpose: Cardano.RedeemerPurpose.spend,
scriptHash: Cardano.util.Hash28ByteBase16(hash28ByteBase16)
purpose: Cardano.RedeemerPurpose.spend
}
];
const withdrawals: Cardano.Withdrawal[] = [{ quantity: 200n, stakeAddress: Cardano.RewardAccount(stakeAddress) }];
Expand Down
8 changes: 4 additions & 4 deletions packages/cardano-services/test/data-mocks/tx.ts
Expand Up @@ -132,13 +132,13 @@ export const withWithdrawals: Cardano.TxAlonzo = merge(withAssets, {
export const witnessRedeemers = {
redeemers: [
{
data: '67f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656',
executionUnits: {
memory: 1700,
steps: 476_468
},
index: 0,
purpose: 'spend',
scriptHash: '67f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656'
purpose: 'spend'
}
],
signatures: {}
Expand Down Expand Up @@ -205,13 +205,13 @@ export const certificate = {
};

export const redeemer = {
data: '67f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656',
executionUnits: {
memory: 0,
steps: 0
},
index: 0,
purpose: 'spend',
scriptHash: '67f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656'
purpose: 'spend'
};

export const tx = [
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/CML/cmlToCore/cmlToCore.ts
Expand Up @@ -186,10 +186,10 @@ export const txWitnessRedeemers = (redeemers?: CML.Redeemers): Cardano.Redeemer[
const redeemerTagKind = scope.manage(reedeemer.tag()).kind();

result.push({
data: Cardano.util.HexBlob(Buffer.from(data.to_bytes()).toString()),
executionUnits: { memory: Number(scope.manage(exUnits.mem())), steps: Number(scope.manage(exUnits.steps())) },
index: Number(index),
purpose: Object.values(Cardano.RedeemerPurpose)[redeemerTagKind],
scriptHash: Cardano.util.Hash28ByteBase16(Buffer.from(data.to_bytes()).toString())
purpose: Object.values(Cardano.RedeemerPurpose)[redeemerTagKind]
});
}
return result;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/Cardano/types/Transaction.ts
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { AuxiliaryData } from './AuxiliaryData';
import { Base64Blob, Hash28ByteBase16, Hash32ByteBase16, HexBlob, OpaqueString, typedHex } from '../util/primitives';
import { Base64Blob, Hash32ByteBase16, HexBlob, OpaqueString, typedHex } from '../util/primitives';
import { Certificate } from './Certificate';
import { Datum, Script } from './Script';
import { Ed25519KeyHash, Ed25519PublicKey } from './Key';
Expand Down Expand Up @@ -66,7 +66,7 @@ export enum RedeemerPurpose {
export interface Redeemer {
index: number;
purpose: RedeemerPurpose;
scriptHash: Hash28ByteBase16;
data: HexBlob;
executionUnits: ExUnits;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/ogmios/src/ogmiosToCore/tx.ts
Expand Up @@ -218,10 +218,10 @@ const mapRedeemer = (key: string, redeemer: Schema.Redeemer): Cardano.Redeemer =
const purposeAndIndex = key.split(':');

return {
data: Cardano.util.HexBlob(redeemer.redeemer),
executionUnits: redeemer.executionUnits,
index: Number(purposeAndIndex[1]),
purpose: purposeAndIndex[0] as Cardano.RedeemerPurpose,
scriptHash: Cardano.util.Hash28ByteBase16(redeemer.redeemer)
purpose: purposeAndIndex[0] as Cardano.RedeemerPurpose
};
};

Expand Down
Expand Up @@ -254,7 +254,7 @@ Object {
"txId": "1a3380c51384d151b876de02b7a4c5ca7eda7b1d1d744660aee4647516fd9a81",
},
],
"mint": undefined,
"mint": Map {},
"outputs": Array [
Object {
"address": "addr_test1vpfwv0ezc5g8a4mkku8hhy3y3vp92t7s3ul8g778g5yegsgalc6gc",
Expand Down Expand Up @@ -426,13 +426,13 @@ Object {
"datums": Array [],
"redeemers": Array [
Object {
"data": "a34160029f20a3419c204386a48244e21bd936202004a0ffa3a4435aed90040242b07a210523409f414d429b3f0440ffa09f034001ffa222234041b4d8668218da9f2341440302413eff4204e1a1d866821901939f23ffa0",
"executionUnits": Object {
"memory": 6026711518256058000,
"steps": 4749668747002319000,
},
"index": 0,
"purpose": "spend",
"scriptHash": "034001ffa222234041b4d8668218da9f2341440302413eff4204e1a1",
},
],
"scripts": Array [
Expand Down
5 changes: 3 additions & 2 deletions packages/ogmios/test/ogmiosToCore/testData.ts
Expand Up @@ -109,7 +109,7 @@ export const mockAlonzoBlock: Ogmios.Schema.Alonzo = {
{ index: 0, txId: '1a3380c51384d151b876de02b7a4c5ca7eda7b1d1d744660aee4647516fd9a81' },
{ index: 1, txId: '1a3380c51384d151b876de02b7a4c5ca7eda7b1d1d744660aee4647516fd9a81' }
],
mint: { coins: 0n },
mint: { assets: {}, coins: 0n },
network: null,
outputs: [
{
Expand Down Expand Up @@ -652,7 +652,8 @@ export const mockBabbageBlock: Ogmios.Schema.Babbage = {
'spend:0': {
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
executionUnits: { memory: 6_026_711_518_256_058_765, steps: 4_749_668_747_002_318_509 },
redeemer: '034001ffa222234041b4d8668218da9f2341440302413eff4204e1a1'
redeemer:
'a34160029f20a3419c204386a48244e21bd936202004a0ffa3a4435aed90040242b07a210523409f414d429b3f0440ffa09f034001ffa222234041b4d8668218da9f2341440302413eff4204e1a1d866821901939f23ffa0'
}
},
scripts: {
Expand Down

0 comments on commit 87452b9

Please sign in to comment.