Skip to content

Commit 6a8c67a

Browse files
authored
Merge pull request #273 from Emurgo/ruslan/mint-fix
Fixing minting API
2 parents ffe663e + 3021134 commit 6a8c67a

8 files changed

+509
-122
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cardano-serialization-lib",
3-
"version": "9.1.4",
3+
"version": "10.0.0-beta.8",
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

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cardano-serialization-lib"
3-
version = "9.1.4"
3+
version = "10.0.0-beta.8"
44
edition = "2018"
55
authors = ["EMURGO"]
66
license = "MIT"

rust/pkg/cardano_serialization_lib.js.flow

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5197,31 +5197,45 @@ declare export class TransactionBuilder {
51975197
): void;
51985198

51995199
/**
5200-
* Set explicit Mint object to this builder
5201-
* it will replace any previously existing mint
5200+
* Set explicit Mint object and the required witnesses to this builder
5201+
* it will replace any previously existing mint and mint scripts
5202+
* NOTE! Error will be returned in case a mint policy does not have a matching script
52025203
* @param {Mint} mint
5204+
* @param {NativeScripts} mint_scripts
52035205
*/
5204-
set_mint(mint: Mint): void;
5206+
set_mint(mint: Mint, mint_scripts: NativeScripts): void;
5207+
5208+
/**
5209+
* Returns a copy of the current mint state in the builder
5210+
* @returns {Mint | void}
5211+
*/
5212+
get_mint(): Mint | void;
5213+
5214+
/**
5215+
* Returns a copy of the current mint witness scripts in the builder
5216+
* @returns {NativeScripts | void}
5217+
*/
5218+
get_mint_scripts(): NativeScripts | void;
52055219

52065220
/**
52075221
* Add a mint entry to this builder using a PolicyID and MintAssets object
52085222
* It will be securely added to existing or new Mint in this builder
52095223
* It will replace any existing mint assets with the same PolicyID
5210-
* @param {ScriptHash} policy_id
5224+
* @param {NativeScript} policy_script
52115225
* @param {MintAssets} mint_assets
52125226
*/
5213-
set_mint_asset(policy_id: ScriptHash, mint_assets: MintAssets): void;
5227+
set_mint_asset(policy_script: NativeScript, mint_assets: MintAssets): void;
52145228

52155229
/**
52165230
* Add a mint entry to this builder using a PolicyID, AssetName, and Int object for amount
52175231
* It will be securely added to existing or new Mint in this builder
52185232
* It will replace any previous existing amount same PolicyID and AssetName
5219-
* @param {ScriptHash} policy_id
5233+
* @param {NativeScript} policy_script
52205234
* @param {AssetName} asset_name
52215235
* @param {Int} amount
52225236
*/
52235237
add_mint_asset(
5224-
policy_id: ScriptHash,
5238+
policy_script: NativeScript,
52255239
asset_name: AssetName,
52265240
amount: Int
52275241
): void;
@@ -5231,14 +5245,14 @@ declare export class TransactionBuilder {
52315245
* Using a PolicyID, AssetName, Int for amount, Address, and Coin (BigNum) objects
52325246
* The asset will be securely added to existing or new Mint in this builder
52335247
* A new output will be added with the specified Address, the Coin value, and the minted asset
5234-
* @param {ScriptHash} policy_id
5248+
* @param {NativeScript} policy_script
52355249
* @param {AssetName} asset_name
52365250
* @param {Int} amount
52375251
* @param {Address} address
52385252
* @param {BigNum} output_coin
52395253
*/
52405254
add_mint_asset_and_output(
5241-
policy_id: ScriptHash,
5255+
policy_script: NativeScript,
52425256
asset_name: AssetName,
52435257
amount: Int,
52445258
address: Address,
@@ -5251,13 +5265,13 @@ declare export class TransactionBuilder {
52515265
* The asset will be securely added to existing or new Mint in this builder
52525266
* A new output will be added with the specified Address and the minted asset
52535267
* The output will be set to contain the minimum required amount of Coin
5254-
* @param {ScriptHash} policy_id
5268+
* @param {NativeScript} policy_script
52555269
* @param {AssetName} asset_name
52565270
* @param {Int} amount
52575271
* @param {Address} address
52585272
*/
52595273
add_mint_asset_and_output_min_required_coin(
5260-
policy_id: ScriptHash,
5274+
policy_script: NativeScript,
52615275
asset_name: AssetName,
52625276
amount: Int,
52635277
address: Address
@@ -5320,14 +5334,14 @@ declare export class TransactionBuilder {
53205334
/**
53215335
* Returns object the body of the new transaction
53225336
* Auxiliary data itself is not included
5323-
* You can use `get_auxiliary_date` or `build_tx`
5337+
* You can use `get_auxiliary_data` or `build_tx`
53245338
* @returns {TransactionBody}
53255339
*/
53265340
build(): TransactionBody;
53275341

53285342
/**
53295343
* Returns full Transaction object with the body and the auxiliary data
5330-
* NOTE: witness_set is set to just empty set
5344+
* NOTE: witness_set will contain all mint_scripts if any been added or set
53315345
* NOTE: is_valid set to true
53325346
* @returns {Transaction}
53335347
*/

rust/src/lib.rs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use cbor_event::{
3434
se::{Serialize, Serializer},
3535
};
3636

37+
pub mod traits;
3738
pub mod address;
3839
pub mod chain_core;
3940
pub mod chain_crypto;
@@ -58,6 +59,8 @@ use plutus::*;
5859
use metadata::*;
5960
use utils::*;
6061
use std::cmp::Ordering;
62+
use std::collections::BTreeSet;
63+
use crate::traits::NoneOrEmpty;
6164

6265
type DeltaCoin = Int;
6366

@@ -221,6 +224,7 @@ impl Certificates {
221224
}
222225

223226
pub type RequiredSigners = Ed25519KeyHashes;
227+
pub type RequiredSignersSet = BTreeSet<Ed25519KeyHash>;
224228

225229
#[wasm_bindgen]
226230
#[derive(Clone)]
@@ -1744,6 +1748,21 @@ impl NativeScripts {
17441748
}
17451749
}
17461750

1751+
impl From<Vec<NativeScript>> for NativeScripts {
1752+
fn from(scripts: Vec<NativeScript>) -> Self {
1753+
scripts.iter().fold(NativeScripts::new(), |mut scripts, s| {
1754+
scripts.add(s);
1755+
scripts
1756+
})
1757+
}
1758+
}
1759+
1760+
impl NoneOrEmpty for NativeScripts {
1761+
fn is_none_or_empty(&self) -> bool {
1762+
self.0.is_empty()
1763+
}
1764+
}
1765+
17471766
#[wasm_bindgen]
17481767
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
17491768
pub struct Update {
@@ -2782,6 +2801,39 @@ impl NetworkId {
27822801
}
27832802
}
27842803

2804+
impl From<&NativeScript> for RequiredSignersSet {
2805+
fn from(script: &NativeScript) -> Self {
2806+
match &script.0 {
2807+
NativeScriptEnum::ScriptPubkey(spk) => {
2808+
let mut set = BTreeSet::new();
2809+
set.insert(spk.addr_keyhash());
2810+
set
2811+
},
2812+
NativeScriptEnum::ScriptAll(all) => {
2813+
RequiredSignersSet::from(&all.native_scripts)
2814+
},
2815+
NativeScriptEnum::ScriptAny(any) => {
2816+
RequiredSignersSet::from(&any.native_scripts)
2817+
},
2818+
NativeScriptEnum::ScriptNOfK(ofk) => {
2819+
RequiredSignersSet::from(&ofk.native_scripts)
2820+
},
2821+
_ => BTreeSet::new(),
2822+
}
2823+
}
2824+
}
2825+
2826+
impl From<&NativeScripts> for RequiredSignersSet {
2827+
fn from(scripts: &NativeScripts) -> Self {
2828+
scripts.0.iter().fold(BTreeSet::new(), |mut set, s| {
2829+
RequiredSignersSet::from(s).iter().for_each(|pk| {
2830+
set.insert(pk.clone());
2831+
});
2832+
set
2833+
})
2834+
}
2835+
}
2836+
27852837
#[cfg(test)]
27862838
mod tests {
27872839
use super::*;
@@ -2963,4 +3015,77 @@ mod tests {
29633015
assert_eq!(p_ass.get(&name1).unwrap(), amount1);
29643016
assert_eq!(n_ass.get(&name1).unwrap(), amount1);
29653017
}
3018+
3019+
fn keyhash(x: u8) -> Ed25519KeyHash {
3020+
Ed25519KeyHash::from_bytes(vec![x, 180, 186, 93, 223, 42, 243, 7, 81, 98, 86, 125, 97, 69, 110, 52, 130, 243, 244, 98, 246, 13, 33, 212, 128, 168, 136, 40]).unwrap()
3021+
}
3022+
3023+
fn pkscript(pk: &Ed25519KeyHash) -> NativeScript {
3024+
NativeScript::new_script_pubkey(&ScriptPubkey::new(pk))
3025+
}
3026+
3027+
fn scripts_vec(scripts: Vec<&NativeScript>) -> NativeScripts {
3028+
NativeScripts(scripts.iter().map(|s| { (*s).clone() }).collect())
3029+
}
3030+
3031+
#[test]
3032+
fn native_scripts_get_pubkeys() {
3033+
let keyhash1 = keyhash(1);
3034+
let keyhash2 = keyhash(2);
3035+
let keyhash3 = keyhash(3);
3036+
3037+
let pks1 = RequiredSignersSet::from(&pkscript(&keyhash1));
3038+
assert_eq!(pks1.len(), 1);
3039+
assert!(pks1.contains(&keyhash1));
3040+
3041+
let pks2 = RequiredSignersSet::from(
3042+
&NativeScript::new_timelock_start(
3043+
&TimelockStart::new(123),
3044+
),
3045+
);
3046+
assert_eq!(pks2.len(), 0);
3047+
3048+
let pks3 = RequiredSignersSet::from(
3049+
&NativeScript::new_script_all(
3050+
&ScriptAll::new(&scripts_vec(vec![
3051+
&pkscript(&keyhash1),
3052+
&pkscript(&keyhash2),
3053+
]))
3054+
),
3055+
);
3056+
assert_eq!(pks3.len(), 2);
3057+
assert!(pks3.contains(&keyhash1));
3058+
assert!(pks3.contains(&keyhash2));
3059+
3060+
let pks4 = RequiredSignersSet::from(
3061+
&NativeScript::new_script_any(
3062+
&ScriptAny::new(&scripts_vec(vec![
3063+
&NativeScript::new_script_n_of_k(&ScriptNOfK::new(
3064+
1,
3065+
&scripts_vec(vec![
3066+
&NativeScript::new_timelock_start(&TimelockStart::new(132)),
3067+
&pkscript(&keyhash3),
3068+
]),
3069+
)),
3070+
&NativeScript::new_script_all(&ScriptAll::new(
3071+
&scripts_vec(vec![
3072+
&NativeScript::new_timelock_expiry(&TimelockExpiry::new(132)),
3073+
&pkscript(&keyhash1),
3074+
]),
3075+
)),
3076+
&NativeScript::new_script_any(&ScriptAny::new(
3077+
&scripts_vec(vec![
3078+
&pkscript(&keyhash1),
3079+
&pkscript(&keyhash2),
3080+
&pkscript(&keyhash3),
3081+
]),
3082+
)),
3083+
]))
3084+
),
3085+
);
3086+
assert_eq!(pks4.len(), 3);
3087+
assert!(pks4.contains(&keyhash1));
3088+
assert!(pks4.contains(&keyhash2));
3089+
assert!(pks4.contains(&keyhash3));
3090+
}
29663091
}

rust/src/traits.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
pub trait NoneOrEmpty {
2+
fn is_none_or_empty(&self) -> bool;
3+
}
4+
5+
impl<T: NoneOrEmpty> NoneOrEmpty for &T {
6+
fn is_none_or_empty(&self) -> bool {
7+
(*self).is_none_or_empty()
8+
}
9+
}
10+
11+
impl<T: NoneOrEmpty> NoneOrEmpty for Option<T> {
12+
fn is_none_or_empty(&self) -> bool {
13+
self.is_none() || self.as_ref().unwrap().is_none_or_empty()
14+
}
15+
}

0 commit comments

Comments
 (0)