Skip to content

Commit 3c85d18

Browse files
authored
Merge pull request #436 from Emurgo/ruslan/script-inputs
Script inputs support
2 parents 0fc2eb3 + 29d31e1 commit 3c85d18

10 files changed

+1139
-64
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cardano-serialization-lib",
3-
"version": "10.0.5-beta.1",
3+
"version": "10.1.0-beta.1",
44
"description": "(De)serialization functions for the Cardano blockchain along with related utility functions",
55
"scripts": {
66
"rust:build-nodejs": "(rimraf ./rust/pkg && cd rust; wasm-pack build --target=nodejs; wasm-pack pack) && npm run js:flowgen",

rust/Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cardano-serialization-lib"
3-
version = "10.0.5-beta.1"
3+
version = "10.1.0-beta.1"
44
edition = "2018"
55
authors = ["EMURGO"]
66
license = "MIT"

rust/pkg/cardano_serialization_lib.js.flow

+181-2
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ declare export var NativeScriptKind: {|
255255

256256
declare export var ScriptHashNamespace: {|
257257
+NativeScript: 0, // 0
258+
+PlutusScript: 1, // 1
258259
|};
259260

260261
/**
@@ -2773,10 +2774,9 @@ declare export class NativeScript {
27732774
static from_bytes(bytes: Uint8Array): NativeScript;
27742775

27752776
/**
2776-
* @param {number} namespace
27772777
* @returns {ScriptHash}
27782778
*/
2779-
hash(namespace: number): ScriptHash;
2779+
hash(): ScriptHash;
27802780

27812781
/**
27822782
* @param {ScriptPubkey} script_pubkey
@@ -3218,6 +3218,11 @@ declare export class PlutusScript {
32183218
* @returns {Uint8Array}
32193219
*/
32203220
bytes(): Uint8Array;
3221+
3222+
/**
3223+
* @returns {ScriptHash}
3224+
*/
3225+
hash(): ScriptHash;
32213226
}
32223227
/**
32233228
*/
@@ -3256,6 +3261,64 @@ declare export class PlutusScripts {
32563261
*/
32573262
add(elem: PlutusScript): void;
32583263
}
3264+
/**
3265+
*/
3266+
declare export class PlutusWitness {
3267+
free(): void;
3268+
3269+
/**
3270+
* @param {PlutusScript} script
3271+
* @param {PlutusData} datum
3272+
* @param {Redeemer} redeemer
3273+
* @returns {PlutusWitness}
3274+
*/
3275+
static new(
3276+
script: PlutusScript,
3277+
datum: PlutusData,
3278+
redeemer: Redeemer
3279+
): PlutusWitness;
3280+
3281+
/**
3282+
* @returns {PlutusScript}
3283+
*/
3284+
script(): PlutusScript;
3285+
3286+
/**
3287+
* @returns {PlutusData}
3288+
*/
3289+
datum(): PlutusData;
3290+
3291+
/**
3292+
* @returns {Redeemer}
3293+
*/
3294+
redeemer(): Redeemer;
3295+
}
3296+
/**
3297+
*/
3298+
declare export class PlutusWitnesses {
3299+
free(): void;
3300+
3301+
/**
3302+
* @returns {PlutusWitnesses}
3303+
*/
3304+
static new(): PlutusWitnesses;
3305+
3306+
/**
3307+
* @returns {number}
3308+
*/
3309+
len(): number;
3310+
3311+
/**
3312+
* @param {number} index
3313+
* @returns {PlutusWitness}
3314+
*/
3315+
get(index: number): PlutusWitness;
3316+
3317+
/**
3318+
* @param {PlutusWitness} elem
3319+
*/
3320+
add(elem: PlutusWitness): void;
3321+
}
32593322
/**
32603323
*/
32613324
declare export class Pointer {
@@ -5244,6 +5307,13 @@ declare export class TransactionBuilder {
52445307
): void;
52455308

52465309
/**
5310+
* This method adds the input to the builder BUT leaves a missing spot for the witness native script
5311+
*
5312+
* After adding the input with this method, use `.add_required_native_input_scripts`
5313+
* and `.add_required_plutus_input_scripts` to add the witness scripts
5314+
*
5315+
* Or instead use `.add_native_script_input` and `.add_plutus_script_input`
5316+
* to add inputs right along with the script, instead of the script hash
52475317
* @param {ScriptHash} hash
52485318
* @param {TransactionInput} input
52495319
* @param {Value} amount
@@ -5254,6 +5324,30 @@ declare export class TransactionBuilder {
52545324
amount: Value
52555325
): void;
52565326

5327+
/**
5328+
* This method will add the input to the builder and also register the required native script witness
5329+
* @param {NativeScript} script
5330+
* @param {TransactionInput} input
5331+
* @param {Value} amount
5332+
*/
5333+
add_native_script_input(
5334+
script: NativeScript,
5335+
input: TransactionInput,
5336+
amount: Value
5337+
): void;
5338+
5339+
/**
5340+
* This method will add the input to the builder and also register the required plutus witness
5341+
* @param {PlutusWitness} witness
5342+
* @param {TransactionInput} input
5343+
* @param {Value} amount
5344+
*/
5345+
add_plutus_script_input(
5346+
witness: PlutusWitness,
5347+
input: TransactionInput,
5348+
amount: Value
5349+
): void;
5350+
52575351
/**
52585352
* @param {ByronAddress} hash
52595353
* @param {TransactionInput} input
@@ -5266,12 +5360,55 @@ declare export class TransactionBuilder {
52665360
): void;
52675361

52685362
/**
5363+
* Note that for script inputs this method will use underlying generic `.add_script_input`
5364+
* which leaves a required empty spot for the script witness (or witnesses in case of Plutus).
5365+
* You can use `.add_native_script_input` or `.add_plutus_script_input` directly to register the input along with the witness.
52695366
* @param {Address} address
52705367
* @param {TransactionInput} input
52715368
* @param {Value} amount
52725369
*/
52735370
add_input(address: Address, input: TransactionInput, amount: Value): void;
52745371

5372+
/**
5373+
* Returns the number of still missing input scripts (either native or plutus)
5374+
* Use `.add_required_native_input_scripts` or `.add_required_plutus_input_scripts` to add the missing scripts
5375+
* @returns {number}
5376+
*/
5377+
count_missing_input_scripts(): number;
5378+
5379+
/**
5380+
* Try adding the specified scripts as witnesses for ALREADY ADDED script inputs
5381+
* Any scripts that don't match any of the previously added inputs will be ignored
5382+
* Returns the number of remaining required missing witness scripts
5383+
* Use `.count_missing_input_scripts` to find the number of still missing scripts
5384+
* @param {NativeScripts} scripts
5385+
* @returns {number}
5386+
*/
5387+
add_required_native_input_scripts(scripts: NativeScripts): number;
5388+
5389+
/**
5390+
* Try adding the specified scripts as witnesses for ALREADY ADDED script inputs
5391+
* Any scripts that don't match any of the previously added inputs will be ignored
5392+
* Returns the number of remaining required missing witness scripts
5393+
* Use `.count_missing_input_scripts` to find the number of still missing scripts
5394+
* @param {PlutusWitnesses} scripts
5395+
* @returns {number}
5396+
*/
5397+
add_required_plutus_input_scripts(scripts: PlutusWitnesses): number;
5398+
5399+
/**
5400+
* Returns a copy of the current script input witness scripts in the builder
5401+
* @returns {NativeScripts | void}
5402+
*/
5403+
get_native_input_scripts(): NativeScripts | void;
5404+
5405+
/**
5406+
* Returns a copy of the current plutus input witness scripts in the builder.
5407+
* NOTE: each plutus witness will be cloned with a specific corresponding input index
5408+
* @returns {PlutusWitnesses | void}
5409+
*/
5410+
get_plutus_input_scripts(): PlutusWitnesses | void;
5411+
52755412
/**
52765413
* calculates how much the fee would increase if you added a given output
52775414
* @param {Address} address
@@ -5522,6 +5659,31 @@ declare export class TransactionBuilder {
55225659
*/
55235660
add_change_if_needed(address: Address): boolean;
55245661

5662+
/**
5663+
* This method will calculate the script hash data
5664+
* using the plutus datums and redeemers already present in the builder
5665+
* along with the provided cost model, and will register the calculated value
5666+
* in the builder to be used when building the tx body.
5667+
* In case there are no plutus input witnesses present - nothing will change
5668+
* You can set specific hash value using `.set_script_data_hash`
5669+
* @param {Costmdls} cost_models
5670+
*/
5671+
calc_script_data_hash(cost_models: Costmdls): void;
5672+
5673+
/**
5674+
* Sets the specified hash value.
5675+
* Alternatively you can use `.calc_script_data_hash` to calculate the hash automatically.
5676+
* Or use `.remove_script_data_hash` to delete the previously set value
5677+
* @param {ScriptDataHash} hash
5678+
*/
5679+
set_script_data_hash(hash: ScriptDataHash): void;
5680+
5681+
/**
5682+
* Deletes any previously set plutus data hash value.
5683+
* Use `.set_script_data_hash` or `.calc_script_data_hash` to set it.
5684+
*/
5685+
remove_script_data_hash(): void;
5686+
55255687
/**
55265688
* @returns {number}
55275689
*/
@@ -5544,10 +5706,17 @@ declare export class TransactionBuilder {
55445706
* Returns full Transaction object with the body and the auxiliary data
55455707
* NOTE: witness_set will contain all mint_scripts if any been added or set
55465708
* NOTE: is_valid set to true
5709+
* NOTE: Will fail in case there are any script inputs added with no corresponding witness
55475710
* @returns {Transaction}
55485711
*/
55495712
build_tx(): Transaction;
55505713

5714+
/**
5715+
* Similar to `.build_tx()` but will NOT fail in case there are missing script witnesses
5716+
* @returns {Transaction}
5717+
*/
5718+
build_tx_unsafe(): Transaction;
5719+
55515720
/**
55525721
* warning: sum of all parts of a transaction must equal 0. You cannot just set the fee to the min value and forget about it
55535722
* warning: min_fee may be slightly larger than the actual minimum fee (ex: a few lovelaces)
@@ -6167,6 +6336,16 @@ declare export class TransactionWitnessSets {
61676336
*/
61686337
add(elem: TransactionWitnessSet): void;
61696338
}
6339+
/**
6340+
*/
6341+
declare export class TxBuilderConstants {
6342+
free(): void;
6343+
6344+
/**
6345+
* @returns {Costmdls}
6346+
*/
6347+
static plutus_default_cost_models(): Costmdls;
6348+
}
61706349
/**
61716350
*/
61726351
declare export class URL {

rust/src/address.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ mod tests {
10681068
let oneof_native_script = NativeScript::new_script_n_of_k(&ScriptNOfK::new(1, &pubkey_native_scripts));
10691069

10701070
let script_hash = ScriptHash::from_bytes(
1071-
oneof_native_script.hash(ScriptHashNamespace::NativeScript).to_bytes()
1071+
oneof_native_script.hash().to_bytes()
10721072
).unwrap();
10731073

10741074
let spend_cred = StakeCredential::from_scripthash(&script_hash);

rust/src/lib.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub mod output_builder;
4848
pub mod plutus;
4949
pub mod serialization;
5050
pub mod tx_builder;
51+
pub mod tx_builder_constants;
5152
pub mod typed_bytes;
5253
pub mod emip3;
5354
#[macro_use]
@@ -1770,15 +1771,18 @@ to_from_bytes!(NativeScript);
17701771
#[wasm_bindgen]
17711772
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
17721773
pub enum ScriptHashNamespace {
1773-
NativeScript,
1774-
// TODO: do we need to update this for Plutus?
1774+
NativeScript = 0,
1775+
PlutusScript = 1,
17751776
}
17761777

17771778
#[wasm_bindgen]
17781779
impl NativeScript {
1779-
pub fn hash(&self, namespace: ScriptHashNamespace) -> ScriptHash {
1780+
1781+
pub fn hash(&self) -> ScriptHash {
17801782
let mut bytes = Vec::with_capacity(self.to_bytes().len() + 1);
1781-
bytes.extend_from_slice(&vec![namespace as u8]);
1783+
bytes.extend_from_slice(&vec![
1784+
ScriptHashNamespace::NativeScript as u8,
1785+
]);
17821786
bytes.extend_from_slice(&self.to_bytes());
17831787
ScriptHash::from(blake2b224(bytes.as_ref()))
17841788
}
@@ -3043,7 +3047,7 @@ mod tests {
30433047

30443048
let script = NativeScript::new_script_pubkey(&ScriptPubkey::new(&keyhash));
30453049

3046-
let script_hash = script.hash(ScriptHashNamespace::NativeScript);
3050+
let script_hash = script.hash();
30473051

30483052
assert_eq!(hex::encode(&script_hash.to_bytes()), "187b8d3ddcb24013097c003da0b8d8f7ddcf937119d8f59dccd05a0f");
30493053
}

0 commit comments

Comments
 (0)