Plutonomicon / cardano-transaction-lib

A Purescript library for building smart contract transactions on Cardano
https://plutonomicon.github.io/cardano-transaction-lib/
MIT License
93 stars 50 forks source link

Fix 0.to_bytes in CSL #488

Closed klntsky closed 2 years ago

klntsky commented 2 years ago

Quite a while ago I discovered an inconsistency between plutus and cardano-serialization-lib:

const lib = require('@ngua/cardano-serialization-lib-nodejs');
const zero = lib.BigInt.from_str("0");
const zeroData = lib.PlutusData.new_integer(zero);
const one = lib.BigInt.from_str("1");
const oneData = lib.PlutusData.new_integer(one);
console.log('0:', zeroData.to_bytes(), '1:', oneData.to_bytes());

Output:

0: Uint8Array [ 194, 65, 0 ] 1: Uint8Array [ 1 ]

And in Plutus:

import PlutusTx
import PlutusTx.IsData
import Codec.Serialise
import Data.ByteString.Lazy as BL
import Data.ByteString as BS
import qualified Data.Text.Encoding as Text
import qualified Data.ByteString.Base16 as Base16
import Data.Text (Text)

toHexDigest :: BS.ByteString -> Text
toHexDigest = Text.decodeUtf8 . Base16.encode

toHex = toHexDigest . BL.toStrict . serialise . toData

test = [ toHex 0, toHex 1 ]
> test
["00","01"]

So serialization-lib treats zero very specifically for some reason.

This is the source to the problem that hashing any datum that contains Integer 0 gives a completely wrong result.

We really can't monkey-patch this in CTL, because to_bytes call recurses over WASM data on CSL side. So we really need to update our CSL fork and ask @ngua to upload a new version to NPM.

https://github.com/ngua/cardano-serialization-lib

The ask:

Actually we just need to update CSL: this has already been fixed: https://github.com/Emurgo/cardano-serialization-lib/pull/332

ngua commented 2 years ago

Closed by #493