Skip to content

Commit

Permalink
Merge pull request #436 from Emurgo/ruslan/script-inputs
Browse files Browse the repository at this point in the history
Script inputs support
  • Loading branch information
vsubhuman authored May 19, 2022
2 parents 0fc2eb3 + 29d31e1 commit 3c85d18
Show file tree
Hide file tree
Showing 10 changed files with 1,139 additions and 64 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cardano-serialization-lib",
"version": "10.0.5-beta.1",
"version": "10.1.0-beta.1",
"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; wasm-pack pack) && npm run js:flowgen",
Expand Down
2 changes: 1 addition & 1 deletion rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cardano-serialization-lib"
version = "10.0.5-beta.1"
version = "10.1.0-beta.1"
edition = "2018"
authors = ["EMURGO"]
license = "MIT"
Expand Down
183 changes: 181 additions & 2 deletions rust/pkg/cardano_serialization_lib.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ declare export var NativeScriptKind: {|

declare export var ScriptHashNamespace: {|
+NativeScript: 0, // 0
+PlutusScript: 1, // 1
|};

/**
Expand Down Expand Up @@ -2773,10 +2774,9 @@ declare export class NativeScript {
static from_bytes(bytes: Uint8Array): NativeScript;

/**
* @param {number} namespace
* @returns {ScriptHash}
*/
hash(namespace: number): ScriptHash;
hash(): ScriptHash;

/**
* @param {ScriptPubkey} script_pubkey
Expand Down Expand Up @@ -3218,6 +3218,11 @@ declare export class PlutusScript {
* @returns {Uint8Array}
*/
bytes(): Uint8Array;

/**
* @returns {ScriptHash}
*/
hash(): ScriptHash;
}
/**
*/
Expand Down Expand Up @@ -3256,6 +3261,64 @@ declare export class PlutusScripts {
*/
add(elem: PlutusScript): void;
}
/**
*/
declare export class PlutusWitness {
free(): void;

/**
* @param {PlutusScript} script
* @param {PlutusData} datum
* @param {Redeemer} redeemer
* @returns {PlutusWitness}
*/
static new(
script: PlutusScript,
datum: PlutusData,
redeemer: Redeemer
): PlutusWitness;

/**
* @returns {PlutusScript}
*/
script(): PlutusScript;

/**
* @returns {PlutusData}
*/
datum(): PlutusData;

/**
* @returns {Redeemer}
*/
redeemer(): Redeemer;
}
/**
*/
declare export class PlutusWitnesses {
free(): void;

/**
* @returns {PlutusWitnesses}
*/
static new(): PlutusWitnesses;

/**
* @returns {number}
*/
len(): number;

/**
* @param {number} index
* @returns {PlutusWitness}
*/
get(index: number): PlutusWitness;

/**
* @param {PlutusWitness} elem
*/
add(elem: PlutusWitness): void;
}
/**
*/
declare export class Pointer {
Expand Down Expand Up @@ -5244,6 +5307,13 @@ declare export class TransactionBuilder {
): void;

/**
* This method adds the input to the builder BUT leaves a missing spot for the witness native script
*
* After adding the input with this method, use `.add_required_native_input_scripts`
* and `.add_required_plutus_input_scripts` to add the witness scripts
*
* Or instead use `.add_native_script_input` and `.add_plutus_script_input`
* to add inputs right along with the script, instead of the script hash
* @param {ScriptHash} hash
* @param {TransactionInput} input
* @param {Value} amount
Expand All @@ -5254,6 +5324,30 @@ declare export class TransactionBuilder {
amount: Value
): void;

/**
* This method will add the input to the builder and also register the required native script witness
* @param {NativeScript} script
* @param {TransactionInput} input
* @param {Value} amount
*/
add_native_script_input(
script: NativeScript,
input: TransactionInput,
amount: Value
): void;

/**
* This method will add the input to the builder and also register the required plutus witness
* @param {PlutusWitness} witness
* @param {TransactionInput} input
* @param {Value} amount
*/
add_plutus_script_input(
witness: PlutusWitness,
input: TransactionInput,
amount: Value
): void;

/**
* @param {ByronAddress} hash
* @param {TransactionInput} input
Expand All @@ -5266,12 +5360,55 @@ declare export class TransactionBuilder {
): void;

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

/**
* Returns the number of still missing input scripts (either native or plutus)
* Use `.add_required_native_input_scripts` or `.add_required_plutus_input_scripts` to add the missing scripts
* @returns {number}
*/
count_missing_input_scripts(): number;

/**
* Try adding the specified scripts as witnesses for ALREADY ADDED script inputs
* Any scripts that don't match any of the previously added inputs will be ignored
* Returns the number of remaining required missing witness scripts
* Use `.count_missing_input_scripts` to find the number of still missing scripts
* @param {NativeScripts} scripts
* @returns {number}
*/
add_required_native_input_scripts(scripts: NativeScripts): number;

/**
* Try adding the specified scripts as witnesses for ALREADY ADDED script inputs
* Any scripts that don't match any of the previously added inputs will be ignored
* Returns the number of remaining required missing witness scripts
* Use `.count_missing_input_scripts` to find the number of still missing scripts
* @param {PlutusWitnesses} scripts
* @returns {number}
*/
add_required_plutus_input_scripts(scripts: PlutusWitnesses): number;

/**
* Returns a copy of the current script input witness scripts in the builder
* @returns {NativeScripts | void}
*/
get_native_input_scripts(): NativeScripts | void;

/**
* Returns a copy of the current plutus input witness scripts in the builder.
* NOTE: each plutus witness will be cloned with a specific corresponding input index
* @returns {PlutusWitnesses | void}
*/
get_plutus_input_scripts(): PlutusWitnesses | void;

/**
* calculates how much the fee would increase if you added a given output
* @param {Address} address
Expand Down Expand Up @@ -5522,6 +5659,31 @@ declare export class TransactionBuilder {
*/
add_change_if_needed(address: Address): boolean;

/**
* This method will calculate the script hash data
* using the plutus datums and redeemers already present in the builder
* along with the provided cost model, and will register the calculated value
* in the builder to be used when building the tx body.
* In case there are no plutus input witnesses present - nothing will change
* You can set specific hash value using `.set_script_data_hash`
* @param {Costmdls} cost_models
*/
calc_script_data_hash(cost_models: Costmdls): void;

/**
* Sets the specified hash value.
* Alternatively you can use `.calc_script_data_hash` to calculate the hash automatically.
* Or use `.remove_script_data_hash` to delete the previously set value
* @param {ScriptDataHash} hash
*/
set_script_data_hash(hash: ScriptDataHash): void;

/**
* Deletes any previously set plutus data hash value.
* Use `.set_script_data_hash` or `.calc_script_data_hash` to set it.
*/
remove_script_data_hash(): void;

/**
* @returns {number}
*/
Expand All @@ -5544,10 +5706,17 @@ declare export class TransactionBuilder {
* Returns full Transaction object with the body and the auxiliary data
* NOTE: witness_set will contain all mint_scripts if any been added or set
* NOTE: is_valid set to true
* NOTE: Will fail in case there are any script inputs added with no corresponding witness
* @returns {Transaction}
*/
build_tx(): Transaction;

/**
* Similar to `.build_tx()` but will NOT fail in case there are missing script witnesses
* @returns {Transaction}
*/
build_tx_unsafe(): Transaction;

/**
* 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
* warning: min_fee may be slightly larger than the actual minimum fee (ex: a few lovelaces)
Expand Down Expand Up @@ -6167,6 +6336,16 @@ declare export class TransactionWitnessSets {
*/
add(elem: TransactionWitnessSet): void;
}
/**
*/
declare export class TxBuilderConstants {
free(): void;

/**
* @returns {Costmdls}
*/
static plutus_default_cost_models(): Costmdls;
}
/**
*/
declare export class URL {
Expand Down
2 changes: 1 addition & 1 deletion rust/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ mod tests {
let oneof_native_script = NativeScript::new_script_n_of_k(&ScriptNOfK::new(1, &pubkey_native_scripts));

let script_hash = ScriptHash::from_bytes(
oneof_native_script.hash(ScriptHashNamespace::NativeScript).to_bytes()
oneof_native_script.hash().to_bytes()
).unwrap();

let spend_cred = StakeCredential::from_scripthash(&script_hash);
Expand Down
14 changes: 9 additions & 5 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub mod output_builder;
pub mod plutus;
pub mod serialization;
pub mod tx_builder;
pub mod tx_builder_constants;
pub mod typed_bytes;
pub mod emip3;
#[macro_use]
Expand Down Expand Up @@ -1770,15 +1771,18 @@ to_from_bytes!(NativeScript);
#[wasm_bindgen]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum ScriptHashNamespace {
NativeScript,
// TODO: do we need to update this for Plutus?
NativeScript = 0,
PlutusScript = 1,
}

#[wasm_bindgen]
impl NativeScript {
pub fn hash(&self, namespace: ScriptHashNamespace) -> ScriptHash {

pub fn hash(&self) -> ScriptHash {
let mut bytes = Vec::with_capacity(self.to_bytes().len() + 1);
bytes.extend_from_slice(&vec![namespace as u8]);
bytes.extend_from_slice(&vec![
ScriptHashNamespace::NativeScript as u8,
]);
bytes.extend_from_slice(&self.to_bytes());
ScriptHash::from(blake2b224(bytes.as_ref()))
}
Expand Down Expand Up @@ -3043,7 +3047,7 @@ mod tests {

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

let script_hash = script.hash(ScriptHashNamespace::NativeScript);
let script_hash = script.hash();

assert_eq!(hex::encode(&script_hash.to_bytes()), "187b8d3ddcb24013097c003da0b8d8f7ddcf937119d8f59dccd05a0f");
}
Expand Down
Loading

0 comments on commit 3c85d18

Please sign in to comment.