From bca9dfd89e7f28b1d36704b26a58a3f62071dff6 Mon Sep 17 00:00:00 2001 From: lisicky Date: Sat, 24 Dec 2022 02:18:22 +0400 Subject: [PATCH 1/7] fix for plutus mints with ref input --- rust/pkg/cardano_serialization_lib.js.flow | 95 ++++++----- rust/src/tx_builder.rs | 180 +++++++++++++++++++++ rust/src/tx_builder/mint_builder.rs | 23 ++- rust/src/tx_builder/tx_inputs_builder.rs | 8 + 4 files changed, 263 insertions(+), 43 deletions(-) diff --git a/rust/pkg/cardano_serialization_lib.js.flow b/rust/pkg/cardano_serialization_lib.js.flow index 4ba6668d..0782567a 100644 --- a/rust/pkg/cardano_serialization_lib.js.flow +++ b/rust/pkg/cardano_serialization_lib.js.flow @@ -5,26 +5,6 @@ * @flow */ -/** - * @param {string} json - * @param {number} schema - * @returns {PlutusData} - */ -declare export function encode_json_str_to_plutus_datum( - json: string, - schema: number -): PlutusData; - -/** - * @param {PlutusData} datum - * @param {number} schema - * @returns {string} - */ -declare export function decode_plutus_datum_to_json_str( - datum: PlutusData, - schema: number -): string; - /** * @param {Uint8Array} bytes * @returns {TransactionMetadatum} @@ -61,6 +41,26 @@ declare export function decode_metadatum_to_json_str( schema: number ): string; +/** + * @param {string} json + * @param {number} schema + * @returns {PlutusData} + */ +declare export function encode_json_str_to_plutus_datum( + json: string, + schema: number +): PlutusData; + +/** + * @param {PlutusData} datum + * @param {number} schema + * @returns {string} + */ +declare export function decode_plutus_datum_to_json_str( + datum: PlutusData, + schema: number +): string; + /** * @param {Transaction} tx * @param {LinearFee} linear_fee @@ -333,6 +333,26 @@ declare export var NetworkIdKind: {| +Mainnet: 1, // 1 |}; +/** + */ + +declare export var TransactionMetadatumKind: {| + +MetadataMap: 0, // 0 + +MetadataList: 1, // 1 + +Int: 2, // 2 + +Bytes: 3, // 3 + +Text: 4, // 4 +|}; + +/** + */ + +declare export var MetadataJsonSchema: {| + +NoConversions: 0, // 0 + +BasicConversions: 1, // 1 + +DetailedSchema: 2, // 2 +|}; + /** */ @@ -379,26 +399,6 @@ declare export var PlutusDatumSchema: {| +DetailedSchema: 1, // 1 |}; -/** - */ - -declare export var TransactionMetadatumKind: {| - +MetadataMap: 0, // 0 - +MetadataList: 1, // 1 - +Int: 2, // 2 - +Bytes: 3, // 3 - +Text: 4, // 4 -|}; - -/** - */ - -declare export var MetadataJsonSchema: {| - +NoConversions: 0, // 0 - +BasicConversions: 1, // 1 - +DetailedSchema: 2, // 2 -|}; - /** */ @@ -3846,6 +3846,11 @@ declare export class MintBuilder { */ get_plutus_witnesses(): PlutusWitnesses; + /** + * @returns {TransactionInputs} + */ + get_ref_inputs(): TransactionInputs; + /** * @returns {Redeemers} */ @@ -5023,6 +5028,16 @@ declare export class PlutusWitness { redeemer: Redeemer ): PlutusWitness; + /** + * @param {PlutusScriptSource} script + * @param {Redeemer} redeemer + * @returns {PlutusWitness} + */ + static new_with_ref_without_datum( + script: PlutusScriptSource, + redeemer: Redeemer + ): PlutusWitness; + /** * @returns {PlutusScript | void} */ diff --git a/rust/src/tx_builder.rs b/rust/src/tx_builder.rs index ed30067f..8eea2af8 100644 --- a/rust/src/tx_builder.rs +++ b/rust/src/tx_builder.rs @@ -1253,6 +1253,12 @@ impl TransactionBuilder { inputs.insert(input); } + if let Some(mint) = &self.mint { + for input in mint.get_ref_inputs().0 { + inputs.insert(input); + } + } + let vec_inputs = inputs.into_iter().collect(); TransactionInputs(vec_inputs) } @@ -7644,11 +7650,185 @@ mod tests { let build_res = tx_builder.build_tx(); assert!(build_res.is_ok()); + assert!(mint_builder.get_plutus_witnesses().len() == 1); + let tx = build_res.unwrap(); assert!(tx.body.mint.is_some()); assert_eq!(tx.body.mint.unwrap().0.iter().next().unwrap().0, plutus_script.hash()); } + #[test] + fn plutus_mint_with_script_ref_test() { + let mut tx_builder = create_reallistic_tx_builder(); + let colateral_adress = Address::from_bech32("addr_test1qpu5vlrf4xkxv2qpwngf6cjhtw542ayty80v8dyr49rf5ewvxwdrt70qlcpeeagscasafhffqsxy36t90ldv06wqrk2qum8x5w").unwrap(); + let colateral_input = TransactionInput::from_json("\ + { + \"transaction_id\": \"69b0b867056a2d4fdc3827e23aa7069b125935e2def774941ca8cc7f9e0de774\", + \"index\": 1 + }").unwrap(); + + let tx_input = TransactionInput::from_json("\ + { + \"transaction_id\": \"f58a5bc761b1efdcf4b5684f6ad5495854a0d64b866e2f0f525d134750d3511b\", + \"index\": 1 + }").unwrap(); + let tx_input_ref = TransactionInput::from_json("\ + { + \"transaction_id\": \"f58a5bc7adaadadcf4b5684f6ad5495854a0d64b866e2f0f525d134750d3511b\", + \"index\": 2 + }").unwrap(); + let plutus_script = plutus::PlutusScript::from_hex("5907d2010000332323232323232323232323232323322323232323222232325335332201b3333573466e1cd55ce9baa0044800080608c98c8060cd5ce00c80c00b1999ab9a3370e6aae7540092000233221233001003002323232323232323232323232323333573466e1cd55cea8062400046666666666664444444444442466666666666600201a01801601401201000e00c00a00800600466a02a02c6ae854030cd4054058d5d0a80599a80a80b9aba1500a3335501975ca0306ae854024ccd54065d7280c1aba1500833501502035742a00e666aa032042eb4d5d0a8031919191999ab9a3370e6aae75400920002332212330010030023232323333573466e1cd55cea8012400046644246600200600466a056eb4d5d0a80118161aba135744a004464c6405c66ae700bc0b80b04d55cf280089baa00135742a0046464646666ae68cdc39aab9d5002480008cc8848cc00400c008cd40add69aba15002302c357426ae8940088c98c80b8cd5ce01781701609aab9e5001137540026ae84d5d1280111931901519ab9c02b02a028135573ca00226ea8004d5d0a80299a80abae35742a008666aa03203a40026ae85400cccd54065d710009aba15002301f357426ae8940088c98c8098cd5ce01381301209aba25001135744a00226ae8940044d5d1280089aba25001135744a00226ae8940044d5d1280089aba25001135744a00226aae7940044dd50009aba15002300f357426ae8940088c98c8060cd5ce00c80c00b080b89931900b99ab9c4910350543500017135573ca00226ea800448c88c008dd6000990009aa80a911999aab9f0012500a233500930043574200460066ae880080508c8c8cccd5cd19b8735573aa004900011991091980080180118061aba150023005357426ae8940088c98c8050cd5ce00a80a00909aab9e5001137540024646464646666ae68cdc39aab9d5004480008cccc888848cccc00401401000c008c8c8c8cccd5cd19b8735573aa0049000119910919800801801180a9aba1500233500f014357426ae8940088c98c8064cd5ce00d00c80b89aab9e5001137540026ae854010ccd54021d728039aba150033232323333573466e1d4005200423212223002004357426aae79400c8cccd5cd19b875002480088c84888c004010dd71aba135573ca00846666ae68cdc3a801a400042444006464c6403666ae7007006c06406005c4d55cea80089baa00135742a00466a016eb8d5d09aba2500223263201533573802c02a02626ae8940044d5d1280089aab9e500113754002266aa002eb9d6889119118011bab00132001355012223233335573e0044a010466a00e66442466002006004600c6aae754008c014d55cf280118021aba200301213574200222440042442446600200800624464646666ae68cdc3a800a40004642446004006600a6ae84d55cf280191999ab9a3370ea0049001109100091931900819ab9c01101000e00d135573aa00226ea80048c8c8cccd5cd19b875001480188c848888c010014c01cd5d09aab9e500323333573466e1d400920042321222230020053009357426aae7940108cccd5cd19b875003480088c848888c004014c01cd5d09aab9e500523333573466e1d40112000232122223003005375c6ae84d55cf280311931900819ab9c01101000e00d00c00b135573aa00226ea80048c8c8cccd5cd19b8735573aa004900011991091980080180118029aba15002375a6ae84d5d1280111931900619ab9c00d00c00a135573ca00226ea80048c8cccd5cd19b8735573aa002900011bae357426aae7940088c98c8028cd5ce00580500409baa001232323232323333573466e1d4005200c21222222200323333573466e1d4009200a21222222200423333573466e1d400d2008233221222222233001009008375c6ae854014dd69aba135744a00a46666ae68cdc3a8022400c4664424444444660040120106eb8d5d0a8039bae357426ae89401c8cccd5cd19b875005480108cc8848888888cc018024020c030d5d0a8049bae357426ae8940248cccd5cd19b875006480088c848888888c01c020c034d5d09aab9e500b23333573466e1d401d2000232122222223005008300e357426aae7940308c98c804ccd5ce00a00980880800780700680600589aab9d5004135573ca00626aae7940084d55cf280089baa0012323232323333573466e1d400520022333222122333001005004003375a6ae854010dd69aba15003375a6ae84d5d1280191999ab9a3370ea0049000119091180100198041aba135573ca00c464c6401866ae700340300280244d55cea80189aba25001135573ca00226ea80048c8c8cccd5cd19b875001480088c8488c00400cdd71aba135573ca00646666ae68cdc3a8012400046424460040066eb8d5d09aab9e500423263200933573801401200e00c26aae7540044dd500089119191999ab9a3370ea00290021091100091999ab9a3370ea00490011190911180180218031aba135573ca00846666ae68cdc3a801a400042444004464c6401466ae7002c02802001c0184d55cea80089baa0012323333573466e1d40052002200723333573466e1d40092000212200123263200633573800e00c00800626aae74dd5000a4c24002920103505431001220021123230010012233003300200200133351222335122335004335500248811c2b194b7d10a3d2d3152c5f3a628ff50cb9fc11e59453e8ac7a1aea4500488104544e4654005005112212330010030021120011122002122122330010040031200101").unwrap(); + let plutus_script2 = plutus::PlutusScript::from_hex("5907adaada00332323232323232323232323232323322323232323222232325335332201b3333573466e1cd55ce9baa0044800080608c98c8060cd5ce00c80c00b1999ab9a3370e6aae7540092000233221233001003002323232323232323232323232323333573466e1cd55cea8062400046666666666664444444444442466666666666600201a01801601401201000e00c00a00800600466a02a02c6ae854030cd4054058d5d0a80599a80a80b9aba1500a3335501975ca0306ae854024ccd54065d7280c1aba1500833501502035742a00e666aa032042eb4d5d0a8031919191999ab9a3370e6aae75400920002332212330010030023232323333573466e1cd55cea8012400046644246600200600466a056eb4d5d0a80118161aba135744a004464c6405c66ae700bc0b80b04d55cf280089baa00135742a0046464646666ae68cdc39aab9d5002480008cc8848cc00400c008cd40add69aba15002302c357426ae8940088c98c80b8cd5ce01781701609aab9e5001137540026ae84d5d1280111931901519ab9c02b02a028135573ca00226ea8004d5d0a80299a80abae35742a008666aa03203a40026ae85400cccd54065d710009aba15002301f357426ae8940088c98c8098cd5ce01381301209aba25001135744a00226ae8940044d5d1280089aba25001135744a00226ae8940044d5d1280089aba25001135744a00226aae7940044dd50009aba15002300f357426ae8940088c98c8060cd5ce00c80c00b080b89931900b99ab9c4910350543500017135573ca00226ea800448c88c008dd6000990009aa80a911999aab9f0012500a233500930043574200460066ae880080508c8c8cccd5cd19b8735573aa004900011991091980080180118061aba150023005357426ae8940088c98c8050cd5ce00a80a00909aab9e5001137540024646464646666ae68cdc39aab9d5004480008cccc888848cccc00401401000c008c8c8c8cccd5cd19b8735573aa0049000119910919800801801180a9aba1500233500f014357426ae8940088c98c8064cd5ce00d00c80b89aab9e5001137540026ae854010ccd54021d728039aba150033232323333573466e1d4005200423212223002004357426aae79400c8cccd5cd19b875002480088c84888c004010dd71aba135573ca00846666ae68cdc3a801a400042444006464c6403666ae7007006c06406005c4d55cea80089baa00135742a00466a016eb8d5d09aba2500223263201533573802c02a02626ae8940044d5d1280089aab9e500113754002266aa002eb9d6889119118011bab00132001355012223233335573e0044a010466a00e66442466002006004600c6aae754008c014d55cf280118021aba200301213574200222440042442446600200800624464646666ae68cdc3a800a40004642446004006600a6ae84d55cf280191999ab9a3370ea0049001109100091931900819ab9c01101000e00d135573aa00226ea80048c8c8cccd5cd19b875001480188c848888c010014c01cd5d09aab9e500323333573466e1d400920042321222230020053009357426aae7940108cccd5cd19b875003480088c848888c004014c01cd5d09aab9e500523333573466e1d40112000232122223003005375c6ae84d55cf280311931900819ab9c01101000e00d00c00b135573aa00226ea80048c8c8cccd5cd19b8735573aa004900011991091980080180118029aba15002375a6ae84d5d1280111931900619ab9c00d00c00a135573ca00226ea80048c8cccd5cd19b8735573aa002900011bae357426aae7940088c98c8028cd5ce00580500409baa001232323232323333573466e1d4005200c21222222200323333573466e1d4009200a21222222200423333573466e1d400d2008233221222222233001009008375c6ae854014dd69aba135744a00a46666ae68cdc3a8022400c4664424444444660040120106eb8d5d0a8039bae357426ae89401c8cccd5cd19b875005480108cc8848888888cc018024020c030d5d0a8049bae357426ae8940248cccd5cd19b875006480088c848888888c01c020c034d5d09aab9e500b23333573466e1d401d2000232122222223005008300e357426aae7940308c98c804ccd5ce00a00980880800780700680600589aab9d5004135573ca00626aae7940084d55cf280089baa0012323232323333573466e1d400520022333222122333001005004003375a6ae854010dd69aba15003375a6ae84d5d1280191999ab9a3370ea0049000119091180100198041aba135573ca00c464c6401866ae700340300280244d55cea80189aba25001135573ca00226ea80048c8c8cccd5cd19b875001480088c8488c00400cdd71aba135573ca00646666ae68cdc3a8012400046424460040066eb8d5d09aab9e500423263200933573801401200e00c26aae7540044dd500089119191999ab9a3370ea00290021091100091999ab9a3370ea00490011190911180180218031aba135573ca00846666ae68cdc3a801a400042444004464c6401466ae7002c02802001c0184d55cea80089baa0012323333573466e1d40052002200723333573466e1d40092000212200123263200633573800e00c00800626aae74dd5000a4c24002920103505431001220021123230010012233003300200200133351222335122335004335500248811c2b194b7d10a3d2d3152c5f3a628ff50cb9fc11e59453e8ac7a1aea4500488104544e4654005005112212330010030021120011122002122122330010040031200101").unwrap(); + + let redeemer = Redeemer::from_json("\ + { + \"tag\": \"Mint\", + \"index\": \"0\", + \"data\": \"{\\\"constructor\\\":0,\\\"fields\\\":[]}\", + \"ex_units\": { + \"mem\": \"1042996\", + \"steps\": \"446100241\" + } + }").unwrap(); + + let redeemer2 = Redeemer::from_json("\ + { + \"tag\": \"Mint\", + \"index\": \"0\", + \"data\": \"{\\\"constructor\\\":0,\\\"fields\\\":[]}\", + \"ex_units\": { + \"mem\": \"2929292\", + \"steps\": \"446188888\" + } + }").unwrap(); + + let asset_name = AssetName::from_hex("44544e4654").unwrap(); + let asset_name2 = AssetName::from_hex("44544e4ada").unwrap(); + let mut mint_builder = MintBuilder::new(); + let plutus_script_source = PlutusScriptSource::new(&plutus_script); + let plutus_script_source_ref = PlutusScriptSource::new_ref_input_with_lang_ver(&plutus_script2.hash(), &tx_input_ref, &Language::new_plutus_v2()); + let mint_witnes = MintWitness::new_plutus_script(&plutus_script_source, &redeemer); + let mint_witnes_ref = MintWitness::new_plutus_script(&plutus_script_source_ref, &redeemer2); + mint_builder.add_asset(&mint_witnes, &asset_name, &Int::new(&BigNum::from(100u64))); + mint_builder.add_asset(&mint_witnes_ref, &asset_name, &Int::new(&BigNum::from(100u64))); + + let output_adress = Address::from_bech32("addr_test1qpm5njmgzf4t7225v6j34wl30xfrufzt3jtqtdzf3en9ahpmnhtmynpasyc8fq75zv0uaj86vzsr7g3g8q5ypgu5fwtqr9zsgj").unwrap(); + let mut output_assets = MultiAsset::new(); + let mut asset = Assets::new(); + asset.insert(&asset_name, &BigNum::from(100u64)); + output_assets.insert(&plutus_script.hash(), &asset); + let output_value = Value::new_with_assets(&Coin::from(50000u64), &output_assets); + let output = TransactionOutput::new(&output_adress, &output_value); + + let mut col_builder = TxInputsBuilder::new(); + col_builder.add_input(&colateral_adress, &colateral_input, &Value::new(&Coin::from(1000000000u64))); + tx_builder.set_collateral(&col_builder); + tx_builder.add_output(&output); + tx_builder.add_input(&output_adress, &tx_input, &Value::new(&BigNum::from(100000000000u64))); + tx_builder.set_mint_builder(&mint_builder); + + tx_builder.calc_script_data_hash(&TxBuilderConstants::plutus_vasil_cost_models()).unwrap(); + + let change_res = tx_builder.add_change_if_needed(&output_adress); + assert!(change_res.is_ok()); + + let build_res = tx_builder.build_tx(); + assert!(build_res.is_ok()); + + let tx = build_res.unwrap(); + assert_eq!(tx.witness_set.plutus_scripts.unwrap().len(), 1usize); + assert_eq!(tx.witness_set.redeemers.unwrap().len(), 2usize); + assert!(tx.witness_set.plutus_data.is_none()); + assert_eq!(tx.body.reference_inputs.unwrap().len(), 1usize); + assert!(tx.body.mint.is_some()); + assert_eq!(tx.body.mint.unwrap().len(), 2usize); + } + + #[test] + fn plutus_mint_defferent_redeemers_test() { + let mut tx_builder = create_reallistic_tx_builder(); + let colateral_adress = Address::from_bech32("addr_test1qpu5vlrf4xkxv2qpwngf6cjhtw542ayty80v8dyr49rf5ewvxwdrt70qlcpeeagscasafhffqsxy36t90ldv06wqrk2qum8x5w").unwrap(); + let colateral_input = TransactionInput::from_json("\ + { + \"transaction_id\": \"69b0b867056a2d4fdc3827e23aa7069b125935e2def774941ca8cc7f9e0de774\", + \"index\": 1 + }").unwrap(); + + let tx_input = TransactionInput::from_json("\ + { + \"transaction_id\": \"f58a5bc761b1efdcf4b5684f6ad5495854a0d64b866e2f0f525d134750d3511b\", + \"index\": 1 + }").unwrap(); + let tx_input_ref = TransactionInput::from_json("\ + { + \"transaction_id\": \"f58a5bc7adaadadcf4b5684f6ad5495854a0d64b866e2f0f525d134750d3511b\", + \"index\": 2 + }").unwrap(); + let plutus_script = plutus::PlutusScript::from_hex("5907d2010000332323232323232323232323232323322323232323222232325335332201b3333573466e1cd55ce9baa0044800080608c98c8060cd5ce00c80c00b1999ab9a3370e6aae7540092000233221233001003002323232323232323232323232323333573466e1cd55cea8062400046666666666664444444444442466666666666600201a01801601401201000e00c00a00800600466a02a02c6ae854030cd4054058d5d0a80599a80a80b9aba1500a3335501975ca0306ae854024ccd54065d7280c1aba1500833501502035742a00e666aa032042eb4d5d0a8031919191999ab9a3370e6aae75400920002332212330010030023232323333573466e1cd55cea8012400046644246600200600466a056eb4d5d0a80118161aba135744a004464c6405c66ae700bc0b80b04d55cf280089baa00135742a0046464646666ae68cdc39aab9d5002480008cc8848cc00400c008cd40add69aba15002302c357426ae8940088c98c80b8cd5ce01781701609aab9e5001137540026ae84d5d1280111931901519ab9c02b02a028135573ca00226ea8004d5d0a80299a80abae35742a008666aa03203a40026ae85400cccd54065d710009aba15002301f357426ae8940088c98c8098cd5ce01381301209aba25001135744a00226ae8940044d5d1280089aba25001135744a00226ae8940044d5d1280089aba25001135744a00226aae7940044dd50009aba15002300f357426ae8940088c98c8060cd5ce00c80c00b080b89931900b99ab9c4910350543500017135573ca00226ea800448c88c008dd6000990009aa80a911999aab9f0012500a233500930043574200460066ae880080508c8c8cccd5cd19b8735573aa004900011991091980080180118061aba150023005357426ae8940088c98c8050cd5ce00a80a00909aab9e5001137540024646464646666ae68cdc39aab9d5004480008cccc888848cccc00401401000c008c8c8c8cccd5cd19b8735573aa0049000119910919800801801180a9aba1500233500f014357426ae8940088c98c8064cd5ce00d00c80b89aab9e5001137540026ae854010ccd54021d728039aba150033232323333573466e1d4005200423212223002004357426aae79400c8cccd5cd19b875002480088c84888c004010dd71aba135573ca00846666ae68cdc3a801a400042444006464c6403666ae7007006c06406005c4d55cea80089baa00135742a00466a016eb8d5d09aba2500223263201533573802c02a02626ae8940044d5d1280089aab9e500113754002266aa002eb9d6889119118011bab00132001355012223233335573e0044a010466a00e66442466002006004600c6aae754008c014d55cf280118021aba200301213574200222440042442446600200800624464646666ae68cdc3a800a40004642446004006600a6ae84d55cf280191999ab9a3370ea0049001109100091931900819ab9c01101000e00d135573aa00226ea80048c8c8cccd5cd19b875001480188c848888c010014c01cd5d09aab9e500323333573466e1d400920042321222230020053009357426aae7940108cccd5cd19b875003480088c848888c004014c01cd5d09aab9e500523333573466e1d40112000232122223003005375c6ae84d55cf280311931900819ab9c01101000e00d00c00b135573aa00226ea80048c8c8cccd5cd19b8735573aa004900011991091980080180118029aba15002375a6ae84d5d1280111931900619ab9c00d00c00a135573ca00226ea80048c8cccd5cd19b8735573aa002900011bae357426aae7940088c98c8028cd5ce00580500409baa001232323232323333573466e1d4005200c21222222200323333573466e1d4009200a21222222200423333573466e1d400d2008233221222222233001009008375c6ae854014dd69aba135744a00a46666ae68cdc3a8022400c4664424444444660040120106eb8d5d0a8039bae357426ae89401c8cccd5cd19b875005480108cc8848888888cc018024020c030d5d0a8049bae357426ae8940248cccd5cd19b875006480088c848888888c01c020c034d5d09aab9e500b23333573466e1d401d2000232122222223005008300e357426aae7940308c98c804ccd5ce00a00980880800780700680600589aab9d5004135573ca00626aae7940084d55cf280089baa0012323232323333573466e1d400520022333222122333001005004003375a6ae854010dd69aba15003375a6ae84d5d1280191999ab9a3370ea0049000119091180100198041aba135573ca00c464c6401866ae700340300280244d55cea80189aba25001135573ca00226ea80048c8c8cccd5cd19b875001480088c8488c00400cdd71aba135573ca00646666ae68cdc3a8012400046424460040066eb8d5d09aab9e500423263200933573801401200e00c26aae7540044dd500089119191999ab9a3370ea00290021091100091999ab9a3370ea00490011190911180180218031aba135573ca00846666ae68cdc3a801a400042444004464c6401466ae7002c02802001c0184d55cea80089baa0012323333573466e1d40052002200723333573466e1d40092000212200123263200633573800e00c00800626aae74dd5000a4c24002920103505431001220021123230010012233003300200200133351222335122335004335500248811c2b194b7d10a3d2d3152c5f3a628ff50cb9fc11e59453e8ac7a1aea4500488104544e4654005005112212330010030021120011122002122122330010040031200101").unwrap(); + + let redeemer = Redeemer::from_json("\ + { + \"tag\": \"Mint\", + \"index\": \"0\", + \"data\": \"{\\\"constructor\\\":0,\\\"fields\\\":[]}\", + \"ex_units\": { + \"mem\": \"1042996\", + \"steps\": \"446100241\" + } + }").unwrap(); + + let redeemer2 = Redeemer::from_json("\ + { + \"tag\": \"Mint\", + \"index\": \"0\", + \"data\": \"{\\\"constructor\\\":0,\\\"fields\\\":[]}\", + \"ex_units\": { + \"mem\": \"2929292\", + \"steps\": \"446188888\" + } + }").unwrap(); + + let asset_name = AssetName::from_hex("44544e4654").unwrap(); + let asset_name2 = AssetName::from_hex("44544e4ada").unwrap(); + let mut mint_builder = MintBuilder::new(); + let plutus_script_source = PlutusScriptSource::new(&plutus_script); + let mint_witnes = MintWitness::new_plutus_script(&plutus_script_source, &redeemer); + let mint_witnes2 = MintWitness::new_plutus_script(&plutus_script_source, &redeemer2); + mint_builder.add_asset(&mint_witnes, &asset_name, &Int::new(&BigNum::from(100u64))); + mint_builder.add_asset(&mint_witnes2, &asset_name, &Int::new(&BigNum::from(100u64))); + + let output_adress = Address::from_bech32("addr_test1qpm5njmgzf4t7225v6j34wl30xfrufzt3jtqtdzf3en9ahpmnhtmynpasyc8fq75zv0uaj86vzsr7g3g8q5ypgu5fwtqr9zsgj").unwrap(); + let mut output_assets = MultiAsset::new(); + let mut asset = Assets::new(); + asset.insert(&asset_name, &BigNum::from(100u64)); + output_assets.insert(&plutus_script.hash(), &asset); + let output_value = Value::new_with_assets(&Coin::from(50000u64), &output_assets); + let output = TransactionOutput::new(&output_adress, &output_value); + + let mut col_builder = TxInputsBuilder::new(); + col_builder.add_input(&colateral_adress, &colateral_input, &Value::new(&Coin::from(1000000000u64))); + tx_builder.set_collateral(&col_builder); + tx_builder.add_output(&output); + tx_builder.add_input(&output_adress, &tx_input, &Value::new(&BigNum::from(100000000000u64))); + tx_builder.set_mint_builder(&mint_builder); + + tx_builder.calc_script_data_hash(&TxBuilderConstants::plutus_vasil_cost_models()).unwrap(); + + let change_res = tx_builder.add_change_if_needed(&output_adress); + assert!(change_res.is_ok()); + + let build_res = tx_builder.build_tx(); + assert!(build_res.is_ok()); + + let tx = build_res.unwrap(); + assert_eq!(tx.witness_set.plutus_scripts.unwrap().len(), 1usize); + assert_eq!(tx.witness_set.redeemers.unwrap().len(), 2usize); + assert!(tx.witness_set.plutus_data.is_none()); + assert!(tx.body.reference_inputs.is_none()); + assert!(tx.body.mint.is_some()); + assert_eq!(tx.body.mint.unwrap().len(), 2usize); + } + #[test] fn multiple_plutus_inputs_test() { let mut tx_builder = create_reallistic_tx_builder(); diff --git a/rust/src/tx_builder/mint_builder.rs b/rust/src/tx_builder/mint_builder.rs index 1f3cb9f9..e91c9b3a 100644 --- a/rust/src/tx_builder/mint_builder.rs +++ b/rust/src/tx_builder/mint_builder.rs @@ -148,9 +148,11 @@ impl MintBuilder { match script_mint { ScriptMint::Plutus(plutus_mints) => { for (redeemer, _) in &plutus_mints.redeemer_mints { - if let PlutusScriptSourceEnum::Script(script) = &plutus_mints.script { - plutus_witnesses.push(PlutusWitness::new_without_datum(script, redeemer)); - } + plutus_witnesses.push( + PlutusWitness::new_with_ref_without_datum( + &PlutusScriptSource(plutus_mints.script.clone()), + redeemer) + ); } }, _ => {}, @@ -159,6 +161,21 @@ impl MintBuilder { PlutusWitnesses(plutus_witnesses) } + pub fn get_ref_inputs(&self) -> TransactionInputs { + let mut reference_inputs = Vec::new(); + for script_mint in self.mints.values() { + match script_mint { + ScriptMint::Plutus(plutus_mints) => { + if let PlutusScriptSourceEnum::RefInput(ref_input, _, _) = &plutus_mints.script { + reference_inputs.push(ref_input.clone()); + } + }, + _ => {}, + } + } + TransactionInputs(reference_inputs) + } + pub fn get_redeeemers(&self) -> Result { let mut redeeemers = Vec::new(); let mut index = BigNum::zero(); diff --git a/rust/src/tx_builder/tx_inputs_builder.rs b/rust/src/tx_builder/tx_inputs_builder.rs index 895e8581..53ad619f 100644 --- a/rust/src/tx_builder/tx_inputs_builder.rs +++ b/rust/src/tx_builder/tx_inputs_builder.rs @@ -157,6 +157,14 @@ impl PlutusWitness { } } + pub fn new_with_ref_without_datum(script: &PlutusScriptSource, redeemer: &Redeemer) -> Self { + Self { + script: script.0.clone(), + datum: None, + redeemer: redeemer.clone(), + } + } + pub fn script(&self) -> Option { match &self.script { PlutusScriptSourceEnum::Script(script) => Some(script.clone()), From 7a5da592b0c871fd86e01aba133ad7728a5dac42 Mon Sep 17 00:00:00 2001 From: lisicky Date: Mon, 26 Dec 2022 08:19:26 +0400 Subject: [PATCH 2/7] version bump --- package-lock.json | 2 +- package.json | 2 +- rust/Cargo.toml | 2 +- rust/json-gen/Cargo.lock | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 34026325..47d06642 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "cardano-serialization-lib", - "version": "11.2.1", + "version": "11.2.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 85b0d90d..68dcf7ec 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cardano-serialization-lib", - "version": "11.2.1", + "version": "11.2.2", "description": "(De)serialization functions for the Cardano blockchain along with related utility functions", "scripts": { "rust:build-nodejs": "(rimraf ./rust/pkg && cd rust; wasm-pack build --target=nodejs; cd ..; npm run js:ts-json-gen; cd rust; wasm-pack pack) && npm run js:flowgen", diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 72f8118a..dae2fe5e 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cardano-serialization-lib" -version = "11.2.1" +version = "11.2.2" edition = "2018" authors = ["EMURGO"] license = "MIT" diff --git a/rust/json-gen/Cargo.lock b/rust/json-gen/Cargo.lock index b238d9c9..3cf9c338 100644 --- a/rust/json-gen/Cargo.lock +++ b/rust/json-gen/Cargo.lock @@ -37,7 +37,7 @@ checksum = "a4a45a46ab1f2412e53d3a0ade76ffad2025804294569aae387231a0cd6e0899" [[package]] name = "cardano-serialization-lib" -version = "11.2.1" +version = "11.2.2" dependencies = [ "bech32", "cbor_event", From 6d912ce56c4f871b061851b2de0b46242cba0fb7 Mon Sep 17 00:00:00 2001 From: lisicky Date: Tue, 17 Jan 2023 14:04:25 +0400 Subject: [PATCH 3/7] switch plutus_default_cost_models --- rust/src/tx_builder_constants.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/src/tx_builder_constants.rs b/rust/src/tx_builder_constants.rs index 1f7c62a0..18f939f5 100644 --- a/rust/src/tx_builder_constants.rs +++ b/rust/src/tx_builder_constants.rs @@ -11,7 +11,7 @@ pub struct TxBuilderConstants(); #[wasm_bindgen] impl TxBuilderConstants { pub fn plutus_default_cost_models() -> Costmdls { - TxBuilderConstants::plutus_alonzo_cost_models() + TxBuilderConstants::plutus_vasil_cost_models() } pub fn plutus_alonzo_cost_models() -> Costmdls { From 2f97cad7e169d6435ec02e6b98004bb95f927002 Mon Sep 17 00:00:00 2001 From: lisicky Date: Tue, 17 Jan 2023 14:04:46 +0400 Subject: [PATCH 4/7] mark new_ref_input as deprecated --- rust/src/tx_builder/tx_inputs_builder.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rust/src/tx_builder/tx_inputs_builder.rs b/rust/src/tx_builder/tx_inputs_builder.rs index 53ad619f..ffdb490d 100644 --- a/rust/src/tx_builder/tx_inputs_builder.rs +++ b/rust/src/tx_builder/tx_inputs_builder.rs @@ -89,6 +89,14 @@ impl PlutusScriptSource { Self(PlutusScriptSourceEnum::Script(script.clone())) } + /// !!! DEPRECATED !!! + /// This constructor has missed information about plutus script language vesrion. That can affect + /// the script data hash calculation. + /// Use `.new_ref_input_with_lang_ver` instead + #[deprecated( + since = "11.2.2", + note = "This constructor has missed information about plutus script language vesrion. That can affect the script data hash calculation. Use `.new_ref_input_with_lang_ver` instead." + )] pub fn new_ref_input(script_hash: &ScriptHash, input: &TransactionInput) -> Self { Self(PlutusScriptSourceEnum::RefInput(input.clone(), script_hash.clone(), From 03b91e686b00ef42c8bf7e9d2092d3d9c7ac471b Mon Sep 17 00:00:00 2001 From: lisicky Date: Tue, 31 Jan 2023 19:35:35 +0400 Subject: [PATCH 5/7] bump csl version --- package-lock.json | 2 +- package.json | 2 +- rust/Cargo.toml | 2 +- rust/json-gen/Cargo.lock | 2 +- rust/src/tx_builder/tx_inputs_builder.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 47d06642..1c2a6fb3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "cardano-serialization-lib", - "version": "11.2.2", + "version": "11.3.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 68dcf7ec..07d7c79d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cardano-serialization-lib", - "version": "11.2.2", + "version": "11.3.0", "description": "(De)serialization functions for the Cardano blockchain along with related utility functions", "scripts": { "rust:build-nodejs": "(rimraf ./rust/pkg && cd rust; wasm-pack build --target=nodejs; cd ..; npm run js:ts-json-gen; cd rust; wasm-pack pack) && npm run js:flowgen", diff --git a/rust/Cargo.toml b/rust/Cargo.toml index dae2fe5e..ffa7f78a 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cardano-serialization-lib" -version = "11.2.2" +version = "11.3.0" edition = "2018" authors = ["EMURGO"] license = "MIT" diff --git a/rust/json-gen/Cargo.lock b/rust/json-gen/Cargo.lock index 3cf9c338..e3f6e40c 100644 --- a/rust/json-gen/Cargo.lock +++ b/rust/json-gen/Cargo.lock @@ -37,7 +37,7 @@ checksum = "a4a45a46ab1f2412e53d3a0ade76ffad2025804294569aae387231a0cd6e0899" [[package]] name = "cardano-serialization-lib" -version = "11.2.2" +version = "11.3.0" dependencies = [ "bech32", "cbor_event", diff --git a/rust/src/tx_builder/tx_inputs_builder.rs b/rust/src/tx_builder/tx_inputs_builder.rs index ffdb490d..c3cebbbd 100644 --- a/rust/src/tx_builder/tx_inputs_builder.rs +++ b/rust/src/tx_builder/tx_inputs_builder.rs @@ -94,7 +94,7 @@ impl PlutusScriptSource { /// the script data hash calculation. /// Use `.new_ref_input_with_lang_ver` instead #[deprecated( - since = "11.2.2", + since = "11.3.0", note = "This constructor has missed information about plutus script language vesrion. That can affect the script data hash calculation. Use `.new_ref_input_with_lang_ver` instead." )] pub fn new_ref_input(script_hash: &ScriptHash, input: &TransactionInput) -> Self { From 4840584682e2e6aaa5ec3008104a681486af0180 Mon Sep 17 00:00:00 2001 From: lisicky Date: Tue, 31 Jan 2023 19:43:41 +0400 Subject: [PATCH 6/7] fix test --- rust/src/tx_builder.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rust/src/tx_builder.rs b/rust/src/tx_builder.rs index 8eea2af8..ab881793 100644 --- a/rust/src/tx_builder.rs +++ b/rust/src/tx_builder.rs @@ -6079,9 +6079,12 @@ mod tests { let res2 = tx_builder.build_tx(); assert!(res2.is_ok()); + let mut used_langs = Languages::new(); + used_langs.add(Language::new_plutus_v1()); + let data_hash = hash_script_data( &Redeemers::from(vec![redeemer.clone()]), - &TxBuilderConstants::plutus_default_cost_models(), + &TxBuilderConstants::plutus_default_cost_models().retain_language_versions(&used_langs), Some(PlutusList::from(vec![datum])), ); assert_eq!(tx_builder.script_data_hash.unwrap(), data_hash); From 591211129525918000ee56a54c920b5507f5d873 Mon Sep 17 00:00:00 2001 From: lisicky Date: Tue, 31 Jan 2023 19:43:58 +0400 Subject: [PATCH 7/7] update js.flow --- rust/pkg/cardano_serialization_lib.js.flow | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rust/pkg/cardano_serialization_lib.js.flow b/rust/pkg/cardano_serialization_lib.js.flow index dedf3edb..9a03536e 100644 --- a/rust/pkg/cardano_serialization_lib.js.flow +++ b/rust/pkg/cardano_serialization_lib.js.flow @@ -5012,6 +5012,10 @@ declare export class PlutusScriptSource { static new(script: PlutusScript): PlutusScriptSource; /** + * !!! DEPRECATED !!! + * This constructor has missed information about plutus script language vesrion. That can affect + * the script data hash calculation. + * Use `.new_ref_input_with_lang_ver` instead * @param {ScriptHash} script_hash * @param {TransactionInput} input * @returns {PlutusScriptSource}