Skip to content

Add Witness builder #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 175 additions & 0 deletions rust/pkg/cardano_serialization_lib.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,13 @@ declare export var NativeScriptKind: {|
* This is because you could have a language where the same bytes have different semantics
* So this avoids scripts in different languages mapping to the same hash
* Note that the enum value here is different than the enum value for deciding the cost model of a script
* https://github.com/input-output-hk/cardano-ledger/blob/9c3b4737b13b30f71529e76c5330f403165e28a6/eras/alonzo/impl/src/Cardano/Ledger/Alonzo.hs#L127
*/

declare export var ScriptHashNamespace: {|
+NativeScript: 0, // 0
+PlutusV1: 1, // 1
+PlutusV2: 2, // 2
|};

/**
Expand All @@ -278,6 +281,7 @@ declare export var StakeCredKind: {|

declare export var LanguageKind: {|
+PlutusV1: 0, // 0
+PlutusV2: 1, // 1
|};

/**
Expand Down Expand Up @@ -2173,6 +2177,11 @@ declare export class Language {
*/
static new_plutus_v1(): Language;

/**
* @returns {Language}
*/
static new_plutus_v2(): Language;

/**
* @returns {number}
*/
Expand Down Expand Up @@ -3115,6 +3124,12 @@ declare export class PlutusScript {
*/
static from_bytes(bytes: Uint8Array): PlutusScript;

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

/**
* @param {Uint8Array} bytes
* @returns {PlutusScript}
Expand Down Expand Up @@ -4016,6 +4031,28 @@ declare export class RedeemerTag {
*/
kind(): number;
}
/**
*/
declare export class RedeemerWitnessKey {
free(): void;

/**
* @returns {RedeemerTag}
*/
tag(): RedeemerTag;

/**
* @returns {BigNum}
*/
index(): BigNum;

/**
* @param {RedeemerTag} tag
* @param {BigNum} index
* @returns {RedeemerWitnessKey}
*/
static new(tag: RedeemerTag, index: BigNum): RedeemerWitnessKey;
}
/**
*/
declare export class Redeemers {
Expand Down Expand Up @@ -4144,6 +4181,81 @@ declare export class Relays {
*/
add(elem: Relay): void;
}
/**
*/
declare export class RequiredSignatureSet {
free(): void;

/**
* @param {Vkeywitness} vkey
*/
add_vkey(vkey: Vkeywitness): void;

/**
* @param {Vkey} vkey
*/
add_vkey_key(vkey: Vkey): void;

/**
* @param {BootstrapWitness} bootstrap
*/
add_bootstrap(bootstrap: BootstrapWitness): void;

/**
* @param {Vkey} bootstrap
*/
add_bootstrap_key(bootstrap: Vkey): void;

/**
* @param {NativeScript} native_script
*/
add_native_script(native_script: NativeScript): void;

/**
* @param {ScriptHash} native_script
*/
add_native_script_hash(native_script: ScriptHash): void;

/**
* @param {PlutusScript} plutus_script
*/
add_plutus_script(plutus_script: PlutusScript): void;

/**
* @param {ScriptHash} plutus_script
*/
add_plutus_hash(plutus_script: ScriptHash): void;

/**
* @param {PlutusData} plutus_datum
*/
add_plutus_datum(plutus_datum: PlutusData): void;

/**
* @param {DataHash} plutus_datum
*/
add_plutus_datum_hash(plutus_datum: DataHash): void;

/**
* @param {Redeemer} redeemer
*/
add_redeemer(redeemer: Redeemer): void;

/**
* @param {RedeemerWitnessKey} redeemer
*/
add_redeemer_tag(redeemer: RedeemerWitnessKey): void;

/**
* @param {RequiredSignatureSet} requirements
*/
add_all(requirements: RequiredSignatureSet): void;

/**
* @returns {RequiredSignatureSet}
*/
static new(): RequiredSignatureSet;
}
/**
*/
declare export class RewardAddress {
Expand Down Expand Up @@ -5821,6 +5933,69 @@ declare export class TransactionWitnessSet {
*/
static new(): TransactionWitnessSet;
}
/**
* Builder de-duplicates witnesses as they are added
*/
declare export class TransactionWitnessSetBuilder {
free(): void;

/**
* @param {Vkeywitness} vkey
*/
add_vkey(vkey: Vkeywitness): void;

/**
* @param {BootstrapWitness} bootstrap
*/
add_bootstrap(bootstrap: BootstrapWitness): void;

/**
* @param {NativeScript} native_script
*/
add_native_script(native_script: NativeScript): void;

/**
* @param {PlutusScript} plutus_script
*/
add_plutus_script(plutus_script: PlutusScript): void;

/**
* @param {PlutusData} plutus_datum
*/
add_plutus_datum(plutus_datum: PlutusData): void;

/**
* @param {Redeemer} redeemer
*/
add_redeemer(redeemer: Redeemer): void;

/**
* @param {RequiredSignatureSet} required_sigs
*/
set_required_sigs(required_sigs: RequiredSignatureSet): void;

/**
* @returns {RequiredSignatureSet}
*/
get_required_sigs(): RequiredSignatureSet;

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

/**
* @param {TransactionWitnessSet} wit_set
*/
static add_existing(
wit_set: TransactionWitnessSet
): void;

/**
* @returns {TransactionWitnessSet}
*/
build(): TransactionWitnessSet;
}
/**
*/
declare export class TransactionWitnessSets {
Expand Down
8 changes: 4 additions & 4 deletions rust/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl PrivateKey {

/// ED25519 key used as public key
#[wasm_bindgen]
#[derive(Clone)]
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct PublicKey(crypto::PublicKey<crypto::Ed25519>);

impl From<crypto::PublicKey<crypto::Ed25519>> for PublicKey {
Expand Down Expand Up @@ -329,7 +329,7 @@ impl PublicKey {
}

#[wasm_bindgen]
#[derive(Clone)]
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Vkey(PublicKey);

to_from_bytes!(Vkey);
Expand Down Expand Up @@ -471,7 +471,7 @@ impl Deserialize for Vkeywitness {

#[wasm_bindgen]
#[derive(Clone)]
pub struct Vkeywitnesses(Vec<Vkeywitness>);
pub struct Vkeywitnesses(pub (crate) Vec<Vkeywitness>);

#[wasm_bindgen]
impl Vkeywitnesses {
Expand Down Expand Up @@ -613,7 +613,7 @@ impl DeserializeEmbeddedGroup for BootstrapWitness {

#[wasm_bindgen]
#[derive(Clone)]
pub struct BootstrapWitnesses(Vec<BootstrapWitness>);
pub struct BootstrapWitnesses(pub (crate) Vec<BootstrapWitness>);

#[wasm_bindgen]
impl BootstrapWitnesses {
Expand Down
17 changes: 2 additions & 15 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub mod typed_bytes;
pub mod emip3;
#[macro_use]
pub mod utils;
pub mod witness_builder;

use address::*;
use crypto::*;
Expand Down Expand Up @@ -1613,24 +1614,10 @@ pub struct NativeScript(NativeScriptEnum);

to_from_bytes!(NativeScript);

/// Each new language uses a different namespace for hashing its script
/// This is because you could have a language where the same bytes have different semantics
/// So this avoids scripts in different languages mapping to the same hash
/// Note that the enum value here is different than the enum value for deciding the cost model of a script
#[wasm_bindgen]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum ScriptHashNamespace {
NativeScript,
// TODO: do we need to update this for Plutus?
}

#[wasm_bindgen]
impl NativeScript {
pub fn hash(&self, namespace: ScriptHashNamespace) -> ScriptHash {
let mut bytes = Vec::with_capacity(self.to_bytes().len() + 1);
bytes.extend_from_slice(&vec![namespace as u8]);
bytes.extend_from_slice(&self.to_bytes());
ScriptHash::from(blake2b224(bytes.as_ref()))
hash_script(namespace, self.to_bytes())
}

pub fn new_script_pubkey(script_pubkey: &ScriptPubkey) -> Self {
Expand Down
22 changes: 17 additions & 5 deletions rust/src/plutus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ to_from_bytes!(PlutusScript);

#[wasm_bindgen]
impl PlutusScript {
pub fn hash(&self, namespace: ScriptHashNamespace) -> ScriptHash {
hash_script(namespace, self.to_bytes())
}

pub fn new(bytes: Vec<u8>) -> PlutusScript {
Self(bytes)
}
Expand All @@ -26,7 +30,7 @@ impl PlutusScript {

#[wasm_bindgen]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct PlutusScripts(Vec<PlutusScript>);
pub struct PlutusScripts(pub (crate) Vec<PlutusScript>);

to_from_bytes!(PlutusScripts);

Expand Down Expand Up @@ -258,6 +262,7 @@ impl ExUnits {
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum LanguageKind {
PlutusV1,
PlutusV2,
}

#[wasm_bindgen]
Expand All @@ -272,6 +277,10 @@ impl Language {
Self(LanguageKind::PlutusV1)
}

pub fn new_plutus_v2() -> Self {
Self(LanguageKind::PlutusV2)
}

pub fn kind(&self) -> LanguageKind {
self.0
}
Expand Down Expand Up @@ -424,7 +433,7 @@ impl PlutusData {

#[wasm_bindgen]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct PlutusList(Vec<PlutusData>);
pub struct PlutusList(pub (crate) Vec<PlutusData>);

to_from_bytes!(PlutusList);

Expand Down Expand Up @@ -487,7 +496,7 @@ impl Redeemer {
}

#[wasm_bindgen]
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub enum RedeemerTagKind {
Spend,
Mint,
Expand All @@ -496,7 +505,7 @@ pub enum RedeemerTagKind {
}

#[wasm_bindgen]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub struct RedeemerTag(RedeemerTagKind);

to_from_bytes!(RedeemerTag);
Expand Down Expand Up @@ -526,7 +535,7 @@ impl RedeemerTag {

#[wasm_bindgen]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Redeemers(Vec<Redeemer>);
pub struct Redeemers(pub (crate) Vec<Redeemer>);

to_from_bytes!(Redeemers);

Expand Down Expand Up @@ -832,6 +841,9 @@ impl cbor_event::se::Serialize for Language {
LanguageKind::PlutusV1 => {
serializer.write_unsigned_integer(0u64)
},
LanguageKind::PlutusV2 => {
serializer.write_unsigned_integer(1u64)
},
}
}
}
Expand Down
Loading