Skip to content
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

feat: Add native scripts #241

Merged
merged 3 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
69 changes: 43 additions & 26 deletions src/mapper/map.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use pallas::codec::minicbor::bytes::ByteVec;
use pallas::crypto::hash::Hash;
use pallas::codec::minicbor::{self, bytes::ByteVec};
use pallas::crypto::hash::{Hash, Hasher};
use pallas::ledger::primitives::alonzo::{
self as alonzo, AuxiliaryData, Block, Certificate, InstantaneousRewardSource,
InstantaneousRewardTarget, Metadatum, Relay, TransactionInput, TransactionOutput, Value,
Expand All @@ -12,8 +12,8 @@ use pallas::network::miniprotocols::Point;
use serde_json::{json, Value as JsonValue};

use crate::model::{
BlockRecord, Era, EventData, MetadataRecord, MetadatumRendition, MintRecord, OutputAssetRecord,
StakeCredential, TransactionRecord, TxInputRecord, TxOutputRecord,
BlockRecord, Era, EventData, MetadataRecord, MetadatumRendition, MintRecord, NativeScript,
OutputAssetRecord, StakeCredential, TransactionRecord, TxInputRecord, TxOutputRecord,
};

use crate::utils::time::TimeProvider;
Expand Down Expand Up @@ -187,8 +187,45 @@ impl EventWriter {
}
}

pub fn to_native_script_event(&self) -> EventData {
EventData::NativeScript {}
pub fn to_native_script_event(&self, script: &alonzo::NativeScript) -> EventData {
let mut cbor = Vec::new();
minicbor::encode(script, &mut cbor).unwrap();
cbor.insert(0, 0);
let digest = Hasher::<224>::hash(&cbor);
SmaugPool marked this conversation as resolved.
Show resolved Hide resolved

EventData::NativeScript {
policy_id: hex::encode(digest),
script: self.to_native_script(script),
}
}

pub fn to_native_script(&self, script: &alonzo::NativeScript) -> NativeScript {
match script {
alonzo::NativeScript::ScriptPubkey(keyhash) => NativeScript::Sig {
key_hash: keyhash.to_string(),
},
alonzo::NativeScript::ScriptAll(scripts) => NativeScript::All {
scripts: (*scripts)
.iter()
.map(|script| self.to_native_script(script))
.collect(),
},
alonzo::NativeScript::ScriptAny(scripts) => NativeScript::Any {
scripts: (*scripts)
.iter()
.map(|script| self.to_native_script(script))
.collect(),
},
alonzo::NativeScript::ScriptNOfK(n, scripts) => NativeScript::AtLeast {
required: *n,
scripts: (*scripts)
.iter()
.map(|script| self.to_native_script(script))
.collect(),
},
alonzo::NativeScript::InvalidBefore(slot) => NativeScript::After { slot: *slot },
alonzo::NativeScript::InvalidHereafter(slot) => NativeScript::Before { slot: *slot },
}
}

pub fn to_plutus_script_event(&self, script: &ByteVec) -> EventData {
Expand Down Expand Up @@ -324,26 +361,6 @@ impl EventWriter {
};
}

// TODO: add witness set data to transaction
SmaugPool marked this conversation as resolved.
Show resolved Hide resolved
/*
if let Some(witness) = self.transaction_witness_sets.get(idx) {
let plutus_count = match &witness.plutus_script {
Some(scripts) => scripts.len(),
None => 0,
};

let native_count = match &witness.native_script {
Some(scripts) => scripts.len(),
None => 0,
};

let redeemer_count = match &witness.redeemer {
Some(redeemer) => redeemer.len(),
None => 0,
};
}
*/

if self.config.include_transaction_details {
record.metadata = match aux_data {
Some(aux_data) => self.collect_metadata_records(aux_data)?.into(),
Expand Down
25 changes: 21 additions & 4 deletions src/mapper/shelley.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ impl EventWriter {
self.crawl_metadata(metadata)?;
}

for _native in data.native_scripts.iter() {
self.append(self.to_native_script_event())?;
if let Some(native) = &data.native_scripts {
for script in native.iter() {
self.append(self.to_native_script_event(script))?;
}
}

if let Some(plutus) = &data.plutus_scripts {
Expand All @@ -58,8 +60,10 @@ impl EventWriter {
} => {
self.crawl_metadata(transaction_metadata)?;

for _native in auxiliary_scripts.iter() {
self.append(self.to_native_script_event())?;
if let Some(native) = &auxiliary_scripts {
for script in native.iter() {
self.append(self.to_native_script_event(script))?;
}
}
}
}
Expand Down Expand Up @@ -228,6 +232,19 @@ impl EventWriter {
self.append(EventData::BlockEnd(record))?;
}

for witness in block.transaction_witness_sets.iter() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we would like to allow to distinguish witnesses scripts from auxiliary data ones?

if let Some(native) = &witness.native_script {
for script in native.iter() {
self.append(self.to_native_script_event(script))?;
}
}
if let Some(plutus) = &witness.plutus_script {
for script in plutus.iter() {
self.append(self.to_plutus_script_event(script))?;
}
}
}

Ok(())
}

Expand Down
31 changes: 30 additions & 1 deletion src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,32 @@ pub enum StakeCredential {
Scripthash(String),
}

#[derive(Serialize, Deserialize, Debug, Clone, Display)]
SmaugPool marked this conversation as resolved.
Show resolved Hide resolved
#[serde(rename_all = "camelCase")]
#[serde(tag = "type")]
pub enum NativeScript {
#[serde(rename_all = "camelCase")]
Sig {
key_hash: String,
},
All {
scripts: Vec<NativeScript>,
},
Any {
scripts: Vec<NativeScript>,
},
AtLeast {
required: u32,
scripts: Vec<NativeScript>,
},
Before {
slot: u64,
},
After {
slot: u64,
},
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct BlockRecord {
pub era: Era,
Expand Down Expand Up @@ -215,7 +241,10 @@ pub enum EventData {
tx_id: String,
index: u64,
},
NativeScript {},
NativeScript {
policy_id: String,
script: NativeScript,
},
PlutusScript {
data: String,
},
Expand Down
4 changes: 2 additions & 2 deletions src/sinks/terminal/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ impl LogLine {
source,
max_width,
},
EventData::NativeScript {} => LogLine {
EventData::NativeScript { policy_id, script } => LogLine {
prefix: "NATIVE",
color: Color::White,
content: "{{ ... }}".to_string(),
content: format!("{{ policy_id: {}, script: {} }}", policy_id, script),
source,
max_width,
},
Expand Down