Anastasia-Labs / lucid-evolution

https://anastasia-labs.github.io/lucid-evolution/
44 stars 19 forks source link

Incorrect Parameter Application to Script CBOR #244

Closed keyan-m closed 2 months ago

keyan-m commented 2 months ago

The Problem

applyParamsToScript seems to generate incorrect CBOR.

How to Reproduce

Clone the test/lucid-evolution branch of the direct-offer-offchain repo and run the following commands:

npm install
npm test

This should log the following CBORs, comparing one generated using @anastasia-labs/lucid-cardano-fork, and the other using Lucid Evolution:

======================= LUCID =======================
590bef590bec01000032222323232325333573466e1d200200213232325333573466e1d2000002153335734664646460044660040040024600446600400400244a666aae7c0045280a999ab9a3375e6ae8400400c528898011aba2001357420026eb0d5d09aba23574400e2930b0a99ab9c491455061747465726e206d61746368206661696c75726520696e207175616c69666965642027646f2720626c6f636b206174207372632f4f666665722e68733a37343a372d3536001635573c0046aae74004dd5191aba13235573c6ea8004004d5d0991aab9e375400264646464016a666ae68cdc3a4000004264646464646464646464646464932999aab9f001149854cd5ce2481317074727946726f6d2850446174615265636f72645b5d293a206c697374206973206c6f6e676572207468616e207a65726f00163574400c64a666ae68cc88cc8c8c8c0088cc0080080048c0088cc008008004894ccd55cf8008a5115332233357340040022940c00cd5d0800898011aba20012300337566aae780040048cc88cc8c8c8c0088cc0080080048c0088cc008008004894ccd55cf8008a5115332233357340040022940c00cd5d0800898011aba200123003375a6aae780040048cdc42400000200200220022a66ae712401184e6567617469766520616d6f756e7420696e2056616c7565001637560086466646460044660040040024600446600400400244a666aae7c004480104c894ccd5cd18008010a99ab9c4910c756e736f72746564206d6170001613300435744006466e4000400cdd71aab9d357420020024940dd58019991919180111980100100091801119801001000912999aab9f00114bd6f7b630099aba030033574200260046ae880048cdd819192999ab9a53323357340022944cdc3800a4000266e1c0052038135573a0062a66ae71241387074727946726f6d2843757272656e637953796d626f6c293a206d757374206265203238206279746573206c6f6e67206f7220656d7074790016371a0026eb8d55ce8009919191aab9e004323332323002233002002001230022330020020012253335573e002240082644a666ae68c00400854cd5ce24810c756e736f72746564206d6170001613300435744006466e4000400cdd71aab9d357420020024940dd59aab9e00333232323002233002002001230022330020020012253335573e002297adef6c60133574060066ae84004c008d5d1000919bb0325333573466e24dc6800a408026aae7400854cd5ce2481327074727946726f6d28546f6b656e4e616d65293a206d757374206265206174206d6f7374203332204279746573206c6f6e670016375c6aae74004c8d55cf0011bad35573c0020026eacd55cf0008009bab001357420026ae880194ccd5cd19b87480000084c8c8c8c8c8c8c8c8c8c8c8c8c92653335573e0022930a99ab9c491317074727946726f6d2850446174615265636f72645b5d293a206c697374206973206c6f6e676572207468616e207a65726f00163574400ca666ae68cdc3a4000004264646464646464932999aab9f001149854cd5ce2481317074727946726f6d2850446174615265636f72645b5d293a206c697374206973206c6f6e676572207468616e207a65726f00163574400ca666ae68cdc3a4000004264646464646464932999aab9f001149854cd5ce2481317074727946726f6d2850446174615265636f72645b5d293a206c697374206973206c6f6e676572207468616e207a65726f00163574400ca666ae68cdc3a400000426464a666ae68cdc39b8d001480e04c8c92653335573e0022930a99ab9c491317074727946726f6d2850446174615265636f72645b5d293a206c697374206973206c6f6e676572207468616e207a65726f0016357440062a66ae7124012c7074727946726f6d28505075624b657948617368293a206d757374206265203238206279746573206c6f6e670016375c0026ae8400454ccd5cd19b87480080084c8c94ccd5cd19b87371a002901c0991924ca666aae7c00452615335738921317074727946726f6d2850446174615265636f72645b5d293a206c697374206973206c6f6e676572207468616e207a65726f0016357440062a66ae7124012c7074727946726f6d285053637269707448617368293a206d757374206265203238206279746573206c6f6e670016375c0026ae8400454cd5ce24813f7265616368656420656e64206f662073756d207768696c65207374696c6c206e6f7420686176696e6720666f756e642074686520636f6e7374727563746f72001635573c0046aae74004dd50009aba100115333573466e1d20020021323232323232323232324994ccd55cf8008a4c2a66ae712401317074727946726f6d2850446174615265636f72645b5d293a206c697374206973206c6f6e676572207468616e207a65726f0016357440066eb4004d5d08009aba2003375a0026ae84004d5d10019bad001357420022a66ae712413f7265616368656420656e64206f662073756d207768696c65207374696c6c206e6f7420686176696e6720666f756e642074686520636f6e7374727563746f72001635573c0046aae74004dd50009aba100115333573466e1d20020021324994ccd55cf8008a4c2a66ae712401317074727946726f6d2850446174615265636f72645b5d293a206c697374206973206c6f6e676572207468616e207a65726f00161533573892013f7265616368656420656e64206f662073756d207768696c65207374696c6c206e6f7420686176696e6720666f756e642074686520636f6e7374727563746f72001635573c0046aae74004dd50009aba10013574400ca666ae68cdc3a400000426464a666ae68cdc39b8d001480e04c8c92653335573e0022930a99ab9c491317074727946726f6d2850446174615265636f72645b5d293a206c697374206973206c6f6e676572207468616e207a65726f0016357440062a66ae7124012c7074727946726f6d28505075624b657948617368293a206d757374206265203238206279746573206c6f6e670016375c0026ae8400454ccd5cd19b87480080084c8c94ccd5cd19b87371a002901c0991924ca666aae7c00452615335738921317074727946726f6d2850446174615265636f72645b5d293a206c697374206973206c6f6e676572207468616e207a65726f0016357440062a66ae7124012c7074727946726f6d285053637269707448617368293a206d757374206265203238206279746573206c6f6e670016375c0026ae8400454cd5ce24813f7265616368656420656e64206f662073756d207768696c65207374696c6c206e6f7420686176696e6720666f756e642074686520636f6e7374727563746f72001635573c0046aae74004dd50009aba10011533573892013f7265616368656420656e64206f662073756d207768696c65207374696c6c206e6f7420686176696e6720666f756e642074686520636f6e7374727563746f72001635573c0046aae74004dd50009aba10011533573892013f7265616368656420656e64206f662073756d207768696c65207374696c6c206e6f7420686176696e6720666f756e642074686520636f6e7374727563746f72001635573c0046aae74004dd500389999991119919180111980100100091801119801001000912999aab9f001122500115333573466ebcd55ce9aba10010041300535742002260046ae880040048c888c00800cdd69aab9e00100837566ae840109261635573c0046aae74004dd519191919003a999ab9a3370e900000109924ca666aae7c004526153357389201317074727946726f6d2850446174615265636f72645b5d293a206c697374206973206c6f6e676572207468616e207a65726f001615333573466e1d20020021324994ccd55cf8008a4c2a66ae712401317074727946726f6d2850446174615265636f72645b5d293a206c697374206973206c6f6e676572207468616e207a65726f00161533573892013f7265616368656420656e64206f662073756d207768696c65207374696c6c206e6f7420686176696e6720666f756e642074686520636f6e7374727563746f72001635573c0046aae74004dd5001991aba2357446ae88d5d11aba235744002646aae78dd50009aba13235573c6ea800400530126d8799fd87a9f581c95725140beeec2d8e33570197fd33df021c283b537ded58515cc5e02ffff0001
=====================================================

=================== LUCID EVOLUTION =================
5831590bc20130004c0126d8799fd87a9f581c95725140beeec2d8e33570197fd33df021c283b537ded58515cc5e02ffff0001
=====================================================
SynthLuvr commented 2 months ago

Is this not subjective? Because now the "fix" breaks other codebases. How do you deem the new solution correct and the old solution incorrect?

SynthLuvr commented 2 months ago

Commit 721d971b1ae5df49c0a38baadb57c58b27da3d95 broke our codebase with error: CBOR decode error: too many terminals, data makes no sense.

To workaround this, I had to use the previous version instead of importing it from this library:

import { Exact } from "@lucid-evolution/core-types";
import { Data } from "@lucid-evolution/plutus";
import * as UPLC from "@lucid-evolution/uplc";
import { fromHex, toHex } from "@lucid-evolution/core-utils";

const applyParamsToScript = <T extends unknown[] = Data[]>(
  plutusScript: string,
  params: Exact<[...T]>,
  type?: T,
): string => {
  const p = (type ? Data.castTo<T>(params, type) : params) as Data[];
  return toHex(
    UPLC.apply_params_to_script(fromHex(Data.to(p)), fromHex(plutusScript)),
  );
};

export { applyParamsToScript };