Struct cardano_serialization_lib::NativeScripts
source · pub struct NativeScripts(_);
Implementations§
source§impl NativeScripts
impl NativeScripts
sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
More examples
src/tx_builder/tx_inputs_builder.rs (line 537)
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
pub fn get_native_input_scripts(&self) -> Option<NativeScripts> {
let mut scripts = NativeScripts::new();
self.required_witnesses.scripts
.values()
.flat_map(|v| v)
.for_each(|tx_in_with_wit| {
if let Some(ScriptWitnessType::NativeScriptWitness(s)) = tx_in_with_wit.1 {
scripts.add(&s);
}
});
if scripts.len() > 0 {
Some(scripts)
} else {
None
}
}
src/tx_builder.rs (line 1809)
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
fn get_combined_native_scripts(&self) -> Option<NativeScripts> {
let mut ns = NativeScripts::new();
if let Some(input_scripts) = self.inputs.get_native_input_scripts() {
input_scripts.0.iter().for_each(|s| {
ns.add(s);
});
}
if let Some(input_scripts) = self.collateral.get_native_input_scripts() {
input_scripts.0.iter().for_each(|s| {
ns.add(s);
});
}
if let Some(mint_builder) = &self.mint {
mint_builder.get_native_scripts().0.iter().for_each(|s| {
ns.add(s);
});
}
if ns.len() > 0 {
Some(ns)
} else {
None
}
}
src/utils.rs (line 1542)
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
fn encode_template_to_native_script(
template: &serde_json::Value,
cosigners: &HashMap<String, String>,
) -> Result<NativeScript, JsError> {
match template {
serde_json::Value::String(cosigner) => {
if let Some(xpub) = cosigners.get(cosigner) {
let bytes = Vec::from_hex(xpub).map_err(|e| JsError::from_str(&e.to_string()))?;
let public_key = Bip32PublicKey::from_bytes(&bytes)?;
Ok(NativeScript::new_script_pubkey(&ScriptPubkey::new(
&public_key.to_raw_key().hash(),
)))
} else {
Err(JsError::from_str(&format!(
"cosigner {} not found",
cosigner
)))
}
}
serde_json::Value::Object(map) if map.contains_key("all") => {
let mut all = NativeScripts::new();
if let serde_json::Value::Array(array) = map.get("all").unwrap() {
for val in array {
all.add(&encode_template_to_native_script(val, cosigners)?);
}
} else {
return Err(JsError::from_str("all must be an array"));
}
Ok(NativeScript::new_script_all(&ScriptAll::new(&all)))
}
serde_json::Value::Object(map) if map.contains_key("any") => {
let mut any = NativeScripts::new();
if let serde_json::Value::Array(array) = map.get("any").unwrap() {
for val in array {
any.add(&encode_template_to_native_script(val, cosigners)?);
}
} else {
return Err(JsError::from_str("any must be an array"));
}
Ok(NativeScript::new_script_any(&ScriptAny::new(&any)))
}
serde_json::Value::Object(map) if map.contains_key("some") => {
if let serde_json::Value::Object(some) = map.get("some").unwrap() {
if some.contains_key("at_least") && some.contains_key("from") {
let n = if let serde_json::Value::Number(at_least) =
some.get("at_least").unwrap()
{
if let Some(n) = at_least.as_u64() {
n as u32
} else {
return Err(JsError::from_str("at_least must be an integer"));
}
} else {
return Err(JsError::from_str("at_least must be an integer"));
};
let mut from_scripts = NativeScripts::new();
if let serde_json::Value::Array(array) = some.get("from").unwrap() {
for val in array {
from_scripts.add(&encode_template_to_native_script(val, cosigners)?);
}
} else {
return Err(JsError::from_str("from must be an array"));
}
Ok(NativeScript::new_script_n_of_k(&ScriptNOfK::new(
n,
&from_scripts,
)))
} else {
Err(JsError::from_str("some must contain at_least and from"))
}
} else {
Err(JsError::from_str("some must be an object"))
}
}
serde_json::Value::Object(map) if map.contains_key("active_from") => {
if let serde_json::Value::Number(active_from) = map.get("active_from").unwrap() {
if let Some(n) = active_from.as_u64() {
let slot: SlotBigNum = n.into();
let time_lock_start = TimelockStart::new_timelockstart(&slot);
Ok(NativeScript::new_timelock_start(&time_lock_start))
} else {
Err(JsError::from_str(
"active_from slot must be an integer greater than or equal to 0",
))
}
} else {
Err(JsError::from_str("active_from slot must be a number"))
}
}
serde_json::Value::Object(map) if map.contains_key("active_until") => {
if let serde_json::Value::Number(active_until) = map.get("active_until").unwrap() {
if let Some(n) = active_until.as_u64() {
let slot: SlotBigNum = n.into();
let time_lock_expiry = TimelockExpiry::new_timelockexpiry(&slot);
Ok(NativeScript::new_timelock_expiry(&time_lock_expiry))
} else {
Err(JsError::from_str(
"active_until slot must be an integer greater than or equal to 0",
))
}
} else {
Err(JsError::from_str("active_until slot must be a number"))
}
}
_ => Err(JsError::from_str("invalid template format")),
}
}
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Examples found in repository?
src/tx_builder/tx_inputs_builder.rs (line 546)
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
pub fn get_native_input_scripts(&self) -> Option<NativeScripts> {
let mut scripts = NativeScripts::new();
self.required_witnesses.scripts
.values()
.flat_map(|v| v)
.for_each(|tx_in_with_wit| {
if let Some(ScriptWitnessType::NativeScriptWitness(s)) = tx_in_with_wit.1 {
scripts.add(&s);
}
});
if scripts.len() > 0 {
Some(scripts)
} else {
None
}
}
More examples
src/tx_builder.rs (line 1825)
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
fn get_combined_native_scripts(&self) -> Option<NativeScripts> {
let mut ns = NativeScripts::new();
if let Some(input_scripts) = self.inputs.get_native_input_scripts() {
input_scripts.0.iter().for_each(|s| {
ns.add(s);
});
}
if let Some(input_scripts) = self.collateral.get_native_input_scripts() {
input_scripts.0.iter().for_each(|s| {
ns.add(s);
});
}
if let Some(mint_builder) = &self.mint {
mint_builder.get_native_scripts().0.iter().for_each(|s| {
ns.add(s);
});
}
if ns.len() > 0 {
Some(ns)
} else {
None
}
}
pub fn get(&self, index: usize) -> NativeScript
sourcepub fn add(&mut self, elem: &NativeScript)
pub fn add(&mut self, elem: &NativeScript)
Examples found in repository?
More examples
src/tx_builder/tx_inputs_builder.rs (line 543)
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
pub fn get_native_input_scripts(&self) -> Option<NativeScripts> {
let mut scripts = NativeScripts::new();
self.required_witnesses.scripts
.values()
.flat_map(|v| v)
.for_each(|tx_in_with_wit| {
if let Some(ScriptWitnessType::NativeScriptWitness(s)) = tx_in_with_wit.1 {
scripts.add(&s);
}
});
if scripts.len() > 0 {
Some(scripts)
} else {
None
}
}
src/tx_builder.rs (line 1812)
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
fn get_combined_native_scripts(&self) -> Option<NativeScripts> {
let mut ns = NativeScripts::new();
if let Some(input_scripts) = self.inputs.get_native_input_scripts() {
input_scripts.0.iter().for_each(|s| {
ns.add(s);
});
}
if let Some(input_scripts) = self.collateral.get_native_input_scripts() {
input_scripts.0.iter().for_each(|s| {
ns.add(s);
});
}
if let Some(mint_builder) = &self.mint {
mint_builder.get_native_scripts().0.iter().for_each(|s| {
ns.add(s);
});
}
if ns.len() > 0 {
Some(ns)
} else {
None
}
}
src/utils.rs (line 1546)
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
fn encode_template_to_native_script(
template: &serde_json::Value,
cosigners: &HashMap<String, String>,
) -> Result<NativeScript, JsError> {
match template {
serde_json::Value::String(cosigner) => {
if let Some(xpub) = cosigners.get(cosigner) {
let bytes = Vec::from_hex(xpub).map_err(|e| JsError::from_str(&e.to_string()))?;
let public_key = Bip32PublicKey::from_bytes(&bytes)?;
Ok(NativeScript::new_script_pubkey(&ScriptPubkey::new(
&public_key.to_raw_key().hash(),
)))
} else {
Err(JsError::from_str(&format!(
"cosigner {} not found",
cosigner
)))
}
}
serde_json::Value::Object(map) if map.contains_key("all") => {
let mut all = NativeScripts::new();
if let serde_json::Value::Array(array) = map.get("all").unwrap() {
for val in array {
all.add(&encode_template_to_native_script(val, cosigners)?);
}
} else {
return Err(JsError::from_str("all must be an array"));
}
Ok(NativeScript::new_script_all(&ScriptAll::new(&all)))
}
serde_json::Value::Object(map) if map.contains_key("any") => {
let mut any = NativeScripts::new();
if let serde_json::Value::Array(array) = map.get("any").unwrap() {
for val in array {
any.add(&encode_template_to_native_script(val, cosigners)?);
}
} else {
return Err(JsError::from_str("any must be an array"));
}
Ok(NativeScript::new_script_any(&ScriptAny::new(&any)))
}
serde_json::Value::Object(map) if map.contains_key("some") => {
if let serde_json::Value::Object(some) = map.get("some").unwrap() {
if some.contains_key("at_least") && some.contains_key("from") {
let n = if let serde_json::Value::Number(at_least) =
some.get("at_least").unwrap()
{
if let Some(n) = at_least.as_u64() {
n as u32
} else {
return Err(JsError::from_str("at_least must be an integer"));
}
} else {
return Err(JsError::from_str("at_least must be an integer"));
};
let mut from_scripts = NativeScripts::new();
if let serde_json::Value::Array(array) = some.get("from").unwrap() {
for val in array {
from_scripts.add(&encode_template_to_native_script(val, cosigners)?);
}
} else {
return Err(JsError::from_str("from must be an array"));
}
Ok(NativeScript::new_script_n_of_k(&ScriptNOfK::new(
n,
&from_scripts,
)))
} else {
Err(JsError::from_str("some must contain at_least and from"))
}
} else {
Err(JsError::from_str("some must be an object"))
}
}
serde_json::Value::Object(map) if map.contains_key("active_from") => {
if let serde_json::Value::Number(active_from) = map.get("active_from").unwrap() {
if let Some(n) = active_from.as_u64() {
let slot: SlotBigNum = n.into();
let time_lock_start = TimelockStart::new_timelockstart(&slot);
Ok(NativeScript::new_timelock_start(&time_lock_start))
} else {
Err(JsError::from_str(
"active_from slot must be an integer greater than or equal to 0",
))
}
} else {
Err(JsError::from_str("active_from slot must be a number"))
}
}
serde_json::Value::Object(map) if map.contains_key("active_until") => {
if let serde_json::Value::Number(active_until) = map.get("active_until").unwrap() {
if let Some(n) = active_until.as_u64() {
let slot: SlotBigNum = n.into();
let time_lock_expiry = TimelockExpiry::new_timelockexpiry(&slot);
Ok(NativeScript::new_timelock_expiry(&time_lock_expiry))
} else {
Err(JsError::from_str(
"active_until slot must be an integer greater than or equal to 0",
))
}
} else {
Err(JsError::from_str("active_until slot must be a number"))
}
}
_ => Err(JsError::from_str("invalid template format")),
}
}
Trait Implementations§
source§impl Clone for NativeScripts
impl Clone for NativeScripts
source§fn clone(&self) -> NativeScripts
fn clone(&self) -> NativeScripts
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Debug for NativeScripts
impl Debug for NativeScripts
source§impl<'de> Deserialize<'de> for NativeScripts
impl<'de> Deserialize<'de> for NativeScripts
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl Deserialize for NativeScripts
impl Deserialize for NativeScripts
fn deserialize<R: BufRead + Seek>(
raw: &mut Deserializer<R>
) -> Result<Self, DeserializeError>
source§impl From<&NativeScripts> for RequiredSignersSet
impl From<&NativeScripts> for RequiredSignersSet
source§fn from(scripts: &NativeScripts) -> Self
fn from(scripts: &NativeScripts) -> Self
Converts to this type from the input type.
source§impl From<Vec<NativeScript, Global>> for NativeScripts
impl From<Vec<NativeScript, Global>> for NativeScripts
source§fn from(scripts: Vec<NativeScript>) -> Self
fn from(scripts: Vec<NativeScript>) -> Self
Converts to this type from the input type.
source§impl JsonSchema for NativeScripts
impl JsonSchema for NativeScripts
source§fn schema_name() -> String
fn schema_name() -> String
The name of the generated JSON Schema. Read more
source§fn json_schema(gen: &mut SchemaGenerator) -> Schema
fn json_schema(gen: &mut SchemaGenerator) -> Schema
Generates a JSON Schema for this type. Read more
source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
Whether JSON Schemas generated for this type should be re-used where possible using the
$ref
keyword. Read moresource§impl NoneOrEmpty for NativeScripts
impl NoneOrEmpty for NativeScripts
fn is_none_or_empty(&self) -> bool
source§impl Ord for NativeScripts
impl Ord for NativeScripts
source§fn cmp(&self, other: &NativeScripts) -> Ordering
fn cmp(&self, other: &NativeScripts) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
source§impl PartialEq<NativeScripts> for NativeScripts
impl PartialEq<NativeScripts> for NativeScripts
source§fn eq(&self, other: &NativeScripts) -> bool
fn eq(&self, other: &NativeScripts) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd<NativeScripts> for NativeScripts
impl PartialOrd<NativeScripts> for NativeScripts
source§fn partial_cmp(&self, other: &NativeScripts) -> Option<Ordering>
fn partial_cmp(&self, other: &NativeScripts) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read more