Skip to content

Commit

Permalink
Merge branch '2.0' into nodejs-unlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez authored Nov 17, 2023
2 parents 6fe6387 + 066cbd6 commit 40bebb1
Show file tree
Hide file tree
Showing 32 changed files with 448 additions and 431 deletions.
7 changes: 3 additions & 4 deletions bindings/core/src/method/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use iota_sdk::{
DelegationId, FoundryId, NativeToken, NftId, OutputId, TokenScheme,
},
payload::{dto::PayloadDto, signed_transaction::TransactionId},
BlockId, IssuerId, SignedBlockDto,
BlockDto, BlockId, IssuerId,
},
utils::serde::{option_string, string},
};
Expand Down Expand Up @@ -159,7 +159,7 @@ pub enum ClientMethod {
/// Post block (JSON)
PostBlock {
/// Block
block: SignedBlockDto,
block: BlockDto,
},
/// Post block (raw)
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -363,9 +363,8 @@ pub enum ClientMethod {
address: Bech32Address,
},
/// Returns a block ID from a block
#[serde(rename_all = "camelCase")]
BlockId {
/// Block
signed_block: SignedBlockDto,
block: BlockDto,
},
}
4 changes: 2 additions & 2 deletions bindings/core/src/method/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use iota_sdk::types::block::{
protocol::ProtocolParameters,
signature::Ed25519Signature,
slot::SlotCommitment,
SignedBlockDto,
BlockDto,
};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -83,7 +83,7 @@ pub enum UtilsMethod {
#[serde(rename_all = "camelCase")]
BlockId {
/// Block
block: SignedBlockDto,
block: BlockDto,
/// Network Protocol Parameters
protocol_parameters: ProtocolParameters,
},
Expand Down
18 changes: 8 additions & 10 deletions bindings/core/src/method_handler/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use iota_sdk::{
OutputBuilderAmount,
},
payload::Payload,
SignedBlock, SignedBlockDto, UnsignedBlockDto,
Block, BlockDto, UnsignedBlockDto,
},
TryFromDto,
},
Expand Down Expand Up @@ -186,23 +186,21 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
ClientMethod::GetIssuance => Response::Issuance(client.get_issuance().await?),
ClientMethod::PostBlockRaw { block_bytes } => Response::BlockId(
client
.post_block_raw(&SignedBlock::unpack_strict(
.post_block_raw(&Block::unpack_strict(
&block_bytes[..],
&client.get_protocol_parameters().await?,
)?)
.await?,
),
ClientMethod::PostBlock { block } => Response::BlockId(
client
.post_block(&SignedBlock::try_from_dto_with_params(
.post_block(&Block::try_from_dto_with_params(
block,
&client.get_protocol_parameters().await?,
)?)
.await?,
),
ClientMethod::GetBlock { block_id } => {
Response::SignedBlock(SignedBlockDto::from(&client.get_block(&block_id).await?))
}
ClientMethod::GetBlock { block_id } => Response::Block(BlockDto::from(&client.get_block(&block_id).await?)),
ClientMethod::GetBlockMetadata { block_id } => {
Response::BlockMetadata(client.get_block_metadata(&block_id).await?)
}
Expand All @@ -217,7 +215,7 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
Response::OutputMetadata(client.get_output_metadata(&output_id).await?)
}
ClientMethod::GetIncludedBlock { transaction_id } => {
Response::SignedBlock(SignedBlockDto::from(&client.get_included_block(&transaction_id).await?))
Response::Block(BlockDto::from(&client.get_included_block(&transaction_id).await?))
}
ClientMethod::GetIncludedBlockMetadata { transaction_id } => {
Response::BlockMetadata(client.get_included_block_metadata(&transaction_id).await?)
Expand Down Expand Up @@ -273,7 +271,7 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
.find_blocks(&block_ids)
.await?
.iter()
.map(SignedBlockDto::from)
.map(BlockDto::from)
.collect(),
),
ClientMethod::FindInputs { addresses, amount } => {
Expand Down Expand Up @@ -312,9 +310,9 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
.await?;
Response::CustomJson(data)
}
ClientMethod::BlockId { signed_block: block } => {
ClientMethod::BlockId { block } => {
let protocol_parameters = client.get_protocol_parameters().await?;
let block = SignedBlock::try_from_dto_with_params(block, &protocol_parameters)?;
let block = Block::try_from_dto_with_params(block, &protocol_parameters)?;
Response::BlockId(block.id(&protocol_parameters))
}
};
Expand Down
4 changes: 2 additions & 2 deletions bindings/core/src/method_handler/secret_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use iota_sdk::{
secret::{DowncastSecretManager, SecretManage, SecretManager, SignBlock},
},
types::{
block::{address::ToBech32Ext, core::UnsignedBlock, unlock::Unlock, SignedBlockDto},
block::{address::ToBech32Ext, core::UnsignedBlock, unlock::Unlock, BlockDto},
TryFromDto,
},
};
Expand Down Expand Up @@ -82,7 +82,7 @@ where
.map_err(iota_sdk::client::Error::from)?;
Response::SignedTransaction(transaction.into())
}
SecretManagerMethod::SignBlock { unsigned_block, chain } => Response::SignedBlock(SignedBlockDto::from(
SecretManagerMethod::SignBlock { unsigned_block, chain } => Response::Block(BlockDto::from(
&UnsignedBlock::try_from_dto(unsigned_block)?
.sign_ed25519(secret_manager, chain)
.await?,
Expand Down
4 changes: 2 additions & 2 deletions bindings/core/src/method_handler/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use iota_sdk::{
input::UtxoInput,
output::{AccountId, FoundryId, MinimumOutputAmount, NftId, Output, OutputId, TokenId},
payload::{signed_transaction::Transaction, SignedTransactionPayload},
SignedBlock,
Block,
},
TryFromDto,
},
Expand Down Expand Up @@ -42,7 +42,7 @@ pub(crate) fn call_utils_method_internal(method: UtilsMethod) -> Result<Response
block,
protocol_parameters,
} => {
let block = SignedBlock::try_from_dto_with_params(block, &protocol_parameters)?;
let block = Block::try_from_dto_with_params(block, &protocol_parameters)?;
Response::BlockId(block.id(&protocol_parameters))
}
UtilsMethod::TransactionId { payload } => {
Expand Down
6 changes: 3 additions & 3 deletions bindings/core/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use iota_sdk::{
signature::Ed25519Signature,
slot::SlotCommitmentId,
unlock::Unlock,
BlockId, SignedBlockDto, UnsignedBlockDto,
BlockDto, BlockId, UnsignedBlockDto,
},
},
utils::serde::string,
Expand Down Expand Up @@ -112,7 +112,7 @@ pub enum Response {
/// - [`GetBlock`](crate::method::ClientMethod::GetBlock)
/// - [`GetIncludedBlock`](crate::method::ClientMethod::GetIncludedBlock)
/// - [`SignBlock`](crate::method::SecretManagerMethod::SignBlock)
SignedBlock(SignedBlockDto),
Block(BlockDto),
/// Response for:
/// - [`GetBlockMetadata`](crate::method::ClientMethod::GetBlockMetadata)
BlockMetadata(BlockMetadataResponse),
Expand Down Expand Up @@ -147,7 +147,7 @@ pub enum Response {
OutputIdsResponse(OutputIdsResponse),
/// Response for:
/// - [`FindBlocks`](crate::method::ClientMethod::FindBlocks)
Blocks(Vec<SignedBlockDto>),
Blocks(Vec<BlockDto>),
/// Response for:
/// - [`FindInputs`](crate::method::ClientMethod::FindInputs)
Inputs(Vec<UtxoInput>),
Expand Down
24 changes: 11 additions & 13 deletions bindings/core/tests/combined.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use std::collections::BTreeMap;

use crypto::keys::bip44::Bip44;
use iota_sdk::{
client::{
Expand All @@ -13,7 +11,7 @@ use iota_sdk::{
types::{
block::{
payload::{dto::PayloadDto, Payload, TaggedDataPayload},
BlockDto, IssuerId, SignedBlock,
Block, BlockBodyDto, IssuerId,
},
TryFromDto,
},
Expand Down Expand Up @@ -134,8 +132,8 @@ async fn client_from_wallet() -> Result<()> {
// let unsigned_block = match response {
// Response::UnsignedBlock(unsigned_block) => {
// match &unsigned_block.block {
// BlockDto::Basic(b) => assert_eq!(b.payload.as_ref(), Some(&payload)),
// BlockDto::Validation(v) => panic!("unexpected block {v:?}"),
// BlockBodyDto::Basic(b) => assert_eq!(b.payload.as_ref(), Some(&payload)),
// BlockBodyDto::Validation(v) => panic!("unexpected block body {v:?}"),
// }
// unsigned_block
// }
Expand All @@ -152,13 +150,13 @@ async fn client_from_wallet() -> Result<()> {
// )
// .await;

// let signed_block = match response {
// Response::SignedBlock(block) => {
// match &block.block {
// BlockDto::Basic(b) => assert_eq!(b.payload.as_ref(), Some(&payload)),
// BlockDto::Validation(v) => panic!("unexpected block {v:?}"),
// let block = match response {
// Response::Block(block) => {
// match &block.body {
// BlockBodyDto::Basic(b) => assert_eq!(b.payload.as_ref(), Some(&payload)),
// BlockBodyDto::Validation(v) => panic!("unexpected block {v:?}"),
// }
// block
// block_body
// }
// _ => panic!("unexpected response {response:?}"),
// };
Expand All @@ -167,7 +165,7 @@ async fn client_from_wallet() -> Result<()> {
// let response = call_client_method(
// &client,
// ClientMethod::BlockId {
// signed_block: signed_block.clone(),
// block: block.clone(),
// },
// )
// .await;
Expand All @@ -176,7 +174,7 @@ async fn client_from_wallet() -> Result<()> {
// Response::BlockId(block_id) => {
// assert_eq!(
// block_id,
// SignedBlock::try_from_dto(signed_block)
// Block::try_from_dto(block)
// .unwrap()
// .id(&client.get_protocol_parameters().await.unwrap())
// );
Expand Down
6 changes: 3 additions & 3 deletions sdk/src/client/api/block_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
types::block::{
core::{BlockHeader, UnsignedBlock},
payload::Payload,
Block, IssuerId,
BlockBody, IssuerId,
},
};

Expand Down Expand Up @@ -43,11 +43,11 @@ impl ClientInner {
issuer_id,
),
// TODO: burned mana calculation
Block::build_basic(issuance.strong_parents()?, 0)
BlockBody::build_basic(issuance.strong_parents()?, 0)
.with_weak_parents(issuance.weak_parents()?)
.with_shallow_like_parents(issuance.shallow_like_parents()?)
.with_payload(payload)
.finish_block()?,
.finish_block_body()?,
))
}
}
5 changes: 2 additions & 3 deletions sdk/src/client/api/block_builder/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ use crate::{
payload::signed_transaction::{SignedTransactionPayload, Transaction},
semantic::{SemanticValidationContext, TransactionFailureReason},
signature::Ed25519Signature,
BlockId, SignedBlock,
Block, BlockId,
},
};

// TODO this is wrong because of https://github.com/iotaledger/iota-sdk/issues/1208
const MAX_TX_LENGTH_FOR_BLOCK_WITH_8_PARENTS: usize =
SignedBlock::LENGTH_MAX - SignedBlock::LENGTH_MIN - (7 * BlockId::LENGTH);
const MAX_TX_LENGTH_FOR_BLOCK_WITH_8_PARENTS: usize = Block::LENGTH_MAX - Block::LENGTH_MIN - (7 * BlockId::LENGTH);
// Length for unlocks with a single signature unlock (unlocks length + unlock type + signature type + public key +
// signature)
const SINGLE_UNLOCK_LENGTH: usize = 1 + 1 + Ed25519Signature::PUBLIC_KEY_LENGTH + Ed25519Signature::SIGNATURE_LENGTH;
Expand Down
16 changes: 8 additions & 8 deletions sdk/src/client/api/high_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
},
types::block::{
address::Bech32Address,
core::{BasicBlock, Block, SignedBlock},
core::{BasicBlockBody, Block, BlockBody},
input::{Input, UtxoInput, INPUT_COUNT_MAX},
output::OutputWithMetadata,
payload::{signed_transaction::TransactionId, Payload},
Expand All @@ -27,10 +27,10 @@ use crate::{
impl Client {
/// Get the inputs of a transaction for the given transaction id.
pub async fn inputs_from_transaction_id(&self, transaction_id: &TransactionId) -> Result<Vec<OutputWithMetadata>> {
let signed_block = self.get_included_block(transaction_id).await?;
let block = self.get_included_block(transaction_id).await?;

if let Block::Basic(block) = signed_block.block() {
let inputs = if let Some(Payload::SignedTransaction(t)) = block.payload() {
if let BlockBody::Basic(basic_block_body) = block.body() {
let inputs = if let Some(Payload::SignedTransaction(t)) = basic_block_body.payload() {
t.transaction().inputs()
} else {
return Err(Error::MissingTransactionPayload);
Expand All @@ -45,15 +45,15 @@ impl Client {

self.get_outputs_with_metadata(&input_ids).await
} else {
Err(Error::UnexpectedBlockKind {
expected: BasicBlock::KIND,
actual: signed_block.block().kind(),
Err(Error::UnexpectedBlockBodyKind {
expected: BasicBlockBody::KIND,
actual: block.body().kind(),
})
}
}

/// Find all blocks by provided block IDs.
pub async fn find_blocks(&self, block_ids: &[BlockId]) -> Result<Vec<SignedBlock>> {
pub async fn find_blocks(&self, block_ids: &[BlockId]) -> Result<Vec<Block>> {
// Use a `HashSet` to prevent duplicate block_ids.
let block_ids = block_ids.iter().copied().collect::<HashSet<_>>();
futures::future::try_join_all(block_ids.iter().map(|block_id| self.get_block(block_id))).await
Expand Down
6 changes: 3 additions & 3 deletions sdk/src/client/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ pub enum Error {
/// Missing BIP32 chain to sign with.
#[error("missing BIP32 chain to sign with")]
MissingBip32Chain,
/// Unexpected block kind.
#[error("unexpected block kind: expected {expected}, found {actual}")]
UnexpectedBlockKind { expected: u8, actual: u8 },
/// Unexpected block body kind.
#[error("unexpected block body kind: expected {expected}, found {actual}")]
UnexpectedBlockBodyKind { expected: u8, actual: u8 },
/// Missing transaction payload.
#[error("missing transaction payload")]
MissingTransactionPayload,
Expand Down
Loading

0 comments on commit 40bebb1

Please sign in to comment.