cardano-serialization-lib
cardano-serialization-lib copied to clipboard
How to create plutusData for datum json
How to create plutusData for below json
{
"constructor":0,
"fields":
[
{
"bytes":"3f7826896a48c593598465a096d63606ceb8206"
},
{
"int":1888
},
{
"int":1
}
]
}
const datums = Load.cardano.PlutusList.new();
const datum = Load.cardano.PlutusData.new_constr_plutus_data(
Load.cardano.ConstrPlutusData.new(
Load.cardano.Int.new_i32(0),
Load.cardano.PlutusList.new()
)
);
try
const fields = Load.cardano.PlutusList.new();
fields.add(Load.cardano.PlutusData.new_bytes(Buffer.from('3f7826896a48c593598465a096d63606ceb8206', 'hex')));
fields.add(Load.cardano.PlutusData.new_integer(Load.cardano.Int.new_i32(1888)));
fields.add(Load.cardano.PlutusData.new_integer(Load.cardano.Int.new_i32(1)));
const constrDatum = Load.cardano.ConstrPlutusData.new(
Load.cardano.Int.new_i32(0),
fields
);
const datum = Load.cardano.new_constr_plutus_data(constrDatum);
Be careful if you're integrating this with some other tool as while two datums can be semantically equivalent with the same JSON representation, they might be byte-encoded differently and will generate different hashes.
See #317 for a change that will make this library respect the serialization format used for PlutusData::from_bytes()
.
See input-output-hk/cardano-node#3472 for a new flag for cardano-cli to use raw bytes for datums
Although I think this datum might be encoded the same across both tools but I am not certain. There are cases when serializing CBOR to bytes where you can end up with multiple byte representations e.g. definite vs indefinite lengths.
Datum hash on CLI is different " hash: 6bdee9b9710edcbb942f3ff4046f71ce384722efdb67ea727191f7130d8f9e98" When I try to unlock funds locked by transaction I get error that my datum is different from yours you did locking transaction with
@mailtodanish see my comment in #333 as well as issues #313 and PR #317. You shouldn't be relying on the hash being the same if two datums are created by 2 different tools, and should instead just be using the bytes directly that were used to generate the datum hash in the locked UTXO. Personally I think we should have had something like CIP-21 or some kind of standard enforcing canonical CBOR for generating datum hashes but as it stands there is no such standard so you must use whatever CBOR encoding the datum hash generator used. They are user-supplied data so the idea is that you have access to that data when you go to spend the locked utxo that has the corresponding datum hash.
We have a pending PR that implements the cardano-cli JSON format here #357 but as stated this doesn't mean that the hash will necessarily line up as you should be using the raw data you need to post on-chain and not rely on some specific CBOR encoding (or JSON representation of said CBOR, which then leaves twice as many ways for the raw bytes to change).
PR https://github.com/Emurgo/cardano-serialization-lib/pull/357 was merged. Thanks @rooooooooob !