Skip to content

Commit

Permalink
Refactor block signature (#995)
Browse files Browse the repository at this point in the history
* Add parents to block

* Add burned mana to rust block (#926)

* merge imports

* Split block into basic and validation types

* udep

* Comment out more tests that will change

* oop

* no_std

* impl more of BlockWrapper

* fix client block builder

* Update sdk/src/types/block/protocol.rs

Co-authored-by: Thibault Martinez <[email protected]>

* PR suggestion

* no_std

* Update sdk/src/types/block/validation.rs

Co-authored-by: Thoralf-M <[email protected]>

* invalid block kind

* Refactor types so that `Block` is the highest level enum

* small improvements

* Pr suggestions

* Allow warnings and stub in missing values

* clippy

* properly pack kind and disable clippy CI for now

* Implement new block ID

* Rework BlockId into struct

* serde with string

* align dto to spec

* PR suggestions

* PR suggs

* remove pub(crate) protocol params fields

* cleanup

* comments

* fix tests

* Use Signature for serialized layouts

* more signature fixes

* Refactor block signature

* Fix block id computation, fix return type, reexport BlockHash, clippy (#998)

* no_std

* fix block ID slot index on big endian systems and clippy

* fix with_slot_index too

* PR suggestion

* oof

Co-authored-by: Abdulrahim Al Methiab <[email protected]>

* add memory layout test

* no_std

* Merge branch 2.0

* Add ext trait to help block builder signing

* Update sdk/src/types/block/core.rs

* Fix merge breakage and clippy

* fix nostd

* fix tests

* remove generics

* Nits

* simplify name

* rename trait

* use header more

* move it move it

* PR suggestions

* no_std

* Nit

---------

Co-authored-by: Thibault Martinez <[email protected]>
Co-authored-by: Thoralf-M <[email protected]>
Co-authored-by: Abdulrahim Al Methiab <[email protected]>
  • Loading branch information
4 people authored Oct 5, 2023
1 parent d480f57 commit 60efd78
Show file tree
Hide file tree
Showing 45 changed files with 359 additions and 121 deletions.
7 changes: 4 additions & 3 deletions bindings/core/src/method_handler/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#[cfg(feature = "mqtt")]
use iota_sdk::client::mqtt::{MqttPayload, Topic};
use iota_sdk::{
client::{request_funds_from_faucet, Client},
client::{request_funds_from_faucet, secret::SecretManager, Client},
types::{
api::core::response::OutputWithMetadataResponse,
block::{
Expand Down Expand Up @@ -173,15 +173,16 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
ClientMethod::GetProtocolParameters => Response::ProtocolParameters(client.get_protocol_parameters().await?),
ClientMethod::PostBlockPayload { payload } => {
let block = client
.finish_basic_block_builder(
.build_basic_block::<SecretManager>(
todo!("issuer id"),
todo!("block signature"),
todo!("issuing time"),
None,
Some(Payload::try_from_dto_with_params(
payload,
&client.get_protocol_parameters().await?,
)?),
todo!("secret manager"),
todo!("chain"),
)
.await?;

Expand Down
10 changes: 7 additions & 3 deletions sdk/examples/client/block/00_block_no_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
//! cargo run --release --example block_no_payload
//! ```
use iota_sdk::client::{Client, Result};
use crypto::keys::bip44::Bip44;
use iota_sdk::client::{constants::IOTA_COIN_TYPE, secret::SecretManager, Client, Result};

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -20,14 +21,17 @@ async fn main() -> Result<()> {
// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;

let secret_manager = SecretManager::try_from_mnemonic(std::env::var("MNEMONIC").unwrap())?;

// Create and send the block.
let block = client
.finish_basic_block_builder(
.build_basic_block(
todo!("issuer id"),
todo!("block signature"),
todo!("issuing time"),
None,
None,
&secret_manager,
Bip44::new(IOTA_COIN_TYPE),
)
.await?;

Expand Down
10 changes: 7 additions & 3 deletions sdk/examples/client/block/01_block_confirmation_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
//! cargo run --release --example block_confirmation_time
//! ```
use crypto::keys::bip44::Bip44;
use iota_sdk::{
client::{Client, Result},
client::{constants::IOTA_COIN_TYPE, secret::SecretManager, Client, Result},
types::api::core::response::BlockState,
};

Expand All @@ -23,14 +24,17 @@ async fn main() -> Result<()> {
// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;

let secret_manager = SecretManager::try_from_mnemonic(std::env::var("MNEMONIC").unwrap())?;

// Create and send a block.
let block = client
.finish_basic_block_builder(
.build_basic_block(
todo!("issuer id"),
todo!("block signature"),
todo!("issuing time"),
None,
None,
&secret_manager,
Bip44::new(IOTA_COIN_TYPE),
)
.await?;
let block_id = client.block_id(&block).await?;
Expand Down
10 changes: 7 additions & 3 deletions sdk/examples/client/block/02_block_custom_parents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
//! cargo run --release --example block_custom_parents
//! ```
use iota_sdk::client::{Client, Result};
use crypto::keys::bip44::Bip44;
use iota_sdk::client::{constants::IOTA_COIN_TYPE, secret::SecretManager, Client, Result};

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -20,18 +21,21 @@ async fn main() -> Result<()> {
// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;

let secret_manager = SecretManager::try_from_mnemonic(std::env::var("MNEMONIC").unwrap())?;

// Use issuance as custom parents.
let issuance = client.get_issuance().await?;
println!("Issuance:\n{issuance:#?}");

// Create and send the block with custom parents.
let block = client
.finish_basic_block_builder(
.build_basic_block(
todo!("issuer id"),
todo!("block signature"),
todo!("issuing time"),
Some(issuance.strong_parents()?),
None,
&secret_manager,
Bip44::new(IOTA_COIN_TYPE),
)
.await?;

Expand Down
10 changes: 7 additions & 3 deletions sdk/examples/client/block/03_block_custom_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
//! cargo run --release --example block_custom_payload
//! ```
use crypto::keys::bip44::Bip44;
use iota_sdk::{
client::{Client, Result},
client::{constants::IOTA_COIN_TYPE, secret::SecretManager, Client, Result},
types::block::payload::{Payload, TaggedDataPayload},
};

Expand All @@ -23,17 +24,20 @@ async fn main() -> Result<()> {
// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;

let secret_manager = SecretManager::try_from_mnemonic(std::env::var("MNEMONIC").unwrap())?;

// Create a custom payload.
let tagged_data_payload = TaggedDataPayload::new(*b"Your tag", *b"Your data")?;

// Create and send the block with the custom payload.
let block = client
.finish_basic_block_builder(
.build_basic_block(
todo!("issuer id"),
todo!("block signature"),
todo!("issuing time"),
None,
Some(Payload::from(tagged_data_payload)),
&secret_manager,
Bip44::new(IOTA_COIN_TYPE),
)
.await?;

Expand Down
10 changes: 7 additions & 3 deletions sdk/examples/client/block/04_block_tagged_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
//! cargo run --release --example block_tagged_data [TAG] [DATA]
//! ```
use crypto::keys::bip44::Bip44;
use iota_sdk::{
client::{Client, Result},
client::{constants::IOTA_COIN_TYPE, secret::SecretManager, Client, Result},
types::block::payload::{Payload, TaggedDataPayload},
};

Expand All @@ -23,11 +24,12 @@ async fn main() -> Result<()> {
// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;

let secret_manager = SecretManager::try_from_mnemonic(std::env::var("MNEMONIC").unwrap())?;

// Create and send the block with tag and data.
let block = client
.finish_basic_block_builder(
.build_basic_block(
todo!("issuer id"),
todo!("block signature"),
todo!("issuing time"),
None,
Some(Payload::TaggedData(Box::new(
Expand All @@ -43,6 +45,8 @@ async fn main() -> Result<()> {
)
.unwrap(),
))),
&secret_manager,
Bip44::new(IOTA_COIN_TYPE),
)
.await?;

Expand Down
10 changes: 7 additions & 3 deletions sdk/examples/client/node_api_core/04_post_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
//! cargo run --release --example node_api_core_post_block [NODE URL]
//! ```
use iota_sdk::client::{Client, Result};
use crypto::keys::bip44::Bip44;
use iota_sdk::client::{constants::IOTA_COIN_TYPE, secret::SecretManager, Client, Result};

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -23,14 +24,17 @@ async fn main() -> Result<()> {
// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;

let secret_manager = SecretManager::try_from_mnemonic(std::env::var("MNEMONIC").unwrap())?;

// Create the block.
let block = client
.finish_basic_block_builder(
.build_basic_block(
todo!("issuer id"),
todo!("block signature"),
todo!("issuing time"),
None,
None,
&secret_manager,
Bip44::new(IOTA_COIN_TYPE),
)
.await?;
// Post the block.
Expand Down
10 changes: 7 additions & 3 deletions sdk/examples/client/node_api_core/05_post_block_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
//! cargo run --release --example node_api_core_post_block_raw [NODE URL]
//! ```
use iota_sdk::client::{Client, Result};
use crypto::keys::bip44::Bip44;
use iota_sdk::client::{constants::IOTA_COIN_TYPE, secret::SecretManager, Client, Result};

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -23,14 +24,17 @@ async fn main() -> Result<()> {
// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;

let secret_manager = SecretManager::try_from_mnemonic(std::env::var("MNEMONIC").unwrap())?;

// Create the block.
let block = client
.finish_basic_block_builder(
.build_basic_block(
todo!("issuer id"),
todo!("block signature"),
todo!("issuing time"),
None,
None,
&secret_manager,
Bip44::new(IOTA_COIN_TYPE),
)
.await?;
// Post the block as raw bytes.
Expand Down
48 changes: 29 additions & 19 deletions sdk/src/client/api/block_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,34 @@
pub mod input_selection;
pub mod transaction;

use crypto::keys::bip44::Bip44;

pub use self::transaction::verify_semantic;
use crate::{
client::{ClientInner, Result},
client::{
secret::{SecretManage, SignBlock},
ClientInner, Result,
},
types::block::{
core::{basic, Block, BlockWrapper},
core::{basic, BlockHeader, BlockWrapper},
payload::Payload,
signature::Ed25519Signature,
IssuerId,
Block, IssuerId,
},
};

impl ClientInner {
pub async fn finish_basic_block_builder(
pub async fn build_basic_block<S: SecretManage>(
&self,
issuer_id: IssuerId,
signature: Ed25519Signature,
issuing_time: Option<u64>,
strong_parents: Option<basic::StrongParents>,
payload: Option<Payload>,
) -> Result<BlockWrapper> {
secret_manager: &S,
chain: Bip44,
) -> Result<BlockWrapper>
where
crate::client::Error: From<S::Error>,
{
let issuance = self.get_issuance().await?;
let strong_parents = strong_parents.unwrap_or(issuance.strong_parents()?);

Expand All @@ -40,22 +48,24 @@ impl ClientInner {
issuing_time
});

let protocol_parameters = self.get_protocol_parameters().await?;
let protocol_params = self.get_protocol_parameters().await?;

Ok(BlockWrapper::new(
protocol_parameters.version(),
protocol_parameters.network_id(),
issuing_time,
issuance.commitment.id(),
issuance.latest_finalized_slot,
issuer_id,
// TODO correct value for max_burned_mana
Block::build_basic(strong_parents, 0)
Ok(BlockWrapper::build(
BlockHeader::new(
protocol_params.version(),
protocol_params.network_id(),
issuing_time,
issuance.commitment.id(),
issuance.latest_finalized_slot,
issuer_id,
),
Block::build_basic(strong_parents, 0) // TODO: burned mana calculation
.with_weak_parents(issuance.weak_parents()?)
.with_shallow_like_parents(issuance.shallow_like_parents()?)
.with_payload(payload)
.finish_block()?,
signature,
))
)
.sign_ed25519(secret_manager, chain)
.await?)
}
}
28 changes: 28 additions & 0 deletions sdk/src/client/secret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ use crate::{
},
types::block::{
address::{Address, Ed25519Address},
core::BlockWrapperBuilder,
output::Output,
payload::{transaction::TransactionEssence, TransactionPayload},
signature::{Ed25519Signature, Signature},
unlock::{AccountUnlock, NftUnlock, ReferenceUnlock, SignatureUnlock, Unlock, Unlocks},
BlockWrapper,
},
};

Expand Down Expand Up @@ -602,3 +604,29 @@ where

Ok(tx_payload)
}

#[async_trait]
pub trait SignBlock {
async fn sign_ed25519<S: SecretManage>(
self,
secret_manager: &S,
chain: Bip44,
) -> crate::client::Result<BlockWrapper>
where
crate::client::Error: From<S::Error>;
}

#[async_trait]
impl SignBlock for BlockWrapperBuilder {
async fn sign_ed25519<S: SecretManage>(
self,
secret_manager: &S,
chain: Bip44,
) -> crate::client::Result<BlockWrapper>
where
crate::client::Error: From<S::Error>,
{
let msg = self.signing_input();
Ok(self.finish(secret_manager.sign_ed25519(&msg, chain).await?)?)
}
}
12 changes: 12 additions & 0 deletions sdk/src/types/block/core/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ impl BasicBlockBuilder {
}
}

impl From<BasicBlock> for BasicBlockBuilder {
fn from(value: BasicBlock) -> Self {
Self {
strong_parents: value.strong_parents,
weak_parents: value.weak_parents,
shallow_like_parents: value.shallow_like_parents,
payload: value.payload,
max_burned_mana: value.max_burned_mana,
}
}
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BasicBlock {
/// Blocks that are strongly directly approved.
Expand Down
Loading

0 comments on commit 60efd78

Please sign in to comment.