Skip to content

Commit

Permalink
Remove client block builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoralf-M committed Jun 28, 2023
1 parent 6917048 commit 7c89886
Show file tree
Hide file tree
Showing 72 changed files with 126 additions and 5,039 deletions.
49 changes: 1 addition & 48 deletions bindings/core/src/method/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@ use derivative::Derivative;
#[cfg(feature = "mqtt")]
use iota_sdk::client::mqtt::Topic;
use iota_sdk::{
client::{
api::{
ClientBlockBuilderOptions as BuildBlockOptions, GetAddressesOptions as GenerateAddressesOptions,
PreparedTransactionDataDto,
},
node_api::indexer::query_parameters::QueryParameter,
node_manager::node::NodeAuth,
secret::SecretManagerDto,
},
client::{node_api::indexer::query_parameters::QueryParameter, node_manager::node::NodeAuth},
types::block::{
address::{Bech32Address, Hrp},
output::{
Expand All @@ -26,8 +18,6 @@ use iota_sdk::{
};
use serde::{Deserialize, Serialize};

use crate::OmittedDebug;

/// Each public client method.
#[derive(Clone, Derivative, Serialize, Deserialize)]
#[derivative(Debug)]
Expand Down Expand Up @@ -95,15 +85,6 @@ pub enum ClientMethod {
/// Topics for which listeners should be removed.
topics: Vec<Topic>,
},
/// Build and post a block
#[serde(rename_all = "camelCase")]
BuildAndPostBlock {
/// Secret manager
#[derivative(Debug(format_with = "OmittedDebug::omitted_fmt"))]
secret_manager: Option<SecretManagerDto>,
/// Options
options: Option<BuildBlockOptions>,
},
/// Get a node candidate from the healthy node pool.
GetNode,
/// Gets the network related information such as network_id and min_pow_score
Expand All @@ -125,24 +106,6 @@ pub enum ClientMethod {
/// Returns the unhealthy nodes.
#[cfg(not(target_family = "wasm"))]
UnhealthyNodes,
/// Prepare a transaction for signing
#[serde(rename_all = "camelCase")]
PrepareTransaction {
/// Secret manager
#[derivative(Debug(format_with = "OmittedDebug::omitted_fmt"))]
secret_manager: Option<SecretManagerDto>,
/// Options
options: Option<BuildBlockOptions>,
},
/// Sign a transaction
#[serde(rename_all = "camelCase")]
SignTransaction {
/// Secret manager
#[derivative(Debug(format_with = "OmittedDebug::omitted_fmt"))]
secret_manager: SecretManagerDto,
/// Prepared transaction data
prepared_transaction_data: PreparedTransactionDataDto,
},
/// Build a block containing the specified payload and post it to the network.
PostBlockPayload {
/// The payload to send
Expand Down Expand Up @@ -310,16 +273,6 @@ pub enum ClientMethod {
/// Maximum attempts
max_attempts: Option<u64>,
},
/// Function to consolidate all funds from a range of addresses to the address with the lowest index in that range
/// Returns the address to which the funds got consolidated, if any were available
#[serde(rename_all = "camelCase")]
ConsolidateFunds {
/// Secret manager
#[derivative(Debug(format_with = "OmittedDebug::omitted_fmt"))]
secret_manager: SecretManagerDto,
/// Addresses generation options
generate_addresses_options: GenerateAddressesOptions,
},
/// Function to find inputs from addresses for a provided amount (useful for offline signing)
FindInputs {
/// Addresses
Expand Down
99 changes: 10 additions & 89 deletions bindings/core/src/method_handler/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
#[cfg(feature = "mqtt")]
use iota_sdk::client::mqtt::{MqttPayload, Topic};
use iota_sdk::{
client::{
api::{PreparedTransactionData, PreparedTransactionDataDto},
request_funds_from_faucet, Client,
},
client::{request_funds_from_faucet, Client},
types::{
api::core::response::OutputWithMetadataResponse,
block::{
Expand All @@ -16,7 +13,7 @@ use iota_sdk::{
dto::{OutputBuilderAmountDto, OutputDto, OutputMetadataDto},
AliasOutput, BasicOutput, FoundryOutput, NftOutput, Output, RentStructure,
},
payload::{dto::PayloadDto, Payload},
payload::Payload,
protocol::dto::ProtocolParametersDto,
Block, BlockDto,
},
Expand Down Expand Up @@ -160,31 +157,6 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM

Response::Output(OutputDto::from(&output))
}
ClientMethod::BuildAndPostBlock {
secret_manager,
options,
} => {
// Prepare transaction
let mut block_builder = client.block();

let secret_manager = match secret_manager {
Some(secret_manager) => Some(secret_manager.try_into()?),
None => None,
};

if let Some(secret_manager) = &secret_manager {
block_builder = block_builder.with_secret_manager(secret_manager);
}

if let Some(options) = options {
block_builder = block_builder.set_options(options).await?;
}

let block = block_builder.finish().await?;
let block_id = block.id();

Response::BlockIdWithBlock(block_id, BlockDto::from(&block))
}
#[cfg(feature = "mqtt")]
ClientMethod::ClearListeners { topics } => {
client.unsubscribe(topics).await?;
Expand Down Expand Up @@ -217,55 +189,15 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
}
ClientMethod::GetLocalPow => Response::Bool(client.get_local_pow().await),
ClientMethod::GetFallbackToLocalPow => Response::Bool(client.get_fallback_to_local_pow().await),
ClientMethod::PrepareTransaction {
secret_manager,
options,
} => {
let mut block_builder = client.block();

let secret_manager = match secret_manager {
Some(secret_manager) => Some(secret_manager.try_into()?),
None => None,
};

if let Some(secret_manager) = &secret_manager {
block_builder = block_builder.with_secret_manager(secret_manager);
}

if let Some(options) = options {
block_builder = block_builder.set_options(options).await?;
}

Response::PreparedTransactionData(PreparedTransactionDataDto::from(
&block_builder.prepare_transaction().await?,
))
}
ClientMethod::SignTransaction {
secret_manager,
prepared_transaction_data,
} => {
let mut block_builder = client.block();

let secret_manager = secret_manager.try_into()?;

block_builder = block_builder.with_secret_manager(&secret_manager);

Response::SignedTransaction(PayloadDto::from(
&block_builder
.sign_transaction(PreparedTransactionData::try_from_dto_unverified(
prepared_transaction_data,
)?)
.await?,
))
}
ClientMethod::PostBlockPayload { payload } => {
let block_builder = client.block();

let block = block_builder
.finish_block(Some(Payload::try_from_dto(
payload,
&client.get_protocol_parameters().await?,
)?))
let block = client
.finish_block_builder(
None,
Some(Payload::try_from_dto(
payload,
&client.get_protocol_parameters().await?,
)?),
)
.await?;

let block_id = block.id();
Expand Down Expand Up @@ -369,17 +301,6 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
.collect();
Response::RetryUntilIncludedSuccessful(res)
}
ClientMethod::ConsolidateFunds {
secret_manager,
generate_addresses_options,
} => {
let secret_manager = secret_manager.try_into()?;
Response::ConsolidatedFunds(
client
.consolidate_funds(&secret_manager, generate_addresses_options)
.await?,
)
}
ClientMethod::FindInputs { addresses, amount } => Response::Inputs(
client
.find_inputs(addresses, amount)
Expand Down
9 changes: 1 addition & 8 deletions bindings/core/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ pub enum Response {
/// - [`GetProtocolParameters`](crate::method::ClientMethod::GetProtocolParameters)
ProtocolParameters(ProtocolParametersDto),
/// Response for:
/// - [`PrepareTransaction`](crate::method::ClientMethod::PrepareTransaction)
PreparedTransactionData(PreparedTransactionDataDto),
/// Response for:
/// - [`SignTransaction`](crate::method::ClientMethod::SignTransaction)
/// - [`SignTransaction`](crate::method::SecretManagerMethod::SignTransaction)
SignedTransaction(PayloadDto),
/// Response for:
/// - [`SignatureUnlock`](crate::method::SecretManagerMethod::SignatureUnlock)
Expand Down Expand Up @@ -121,7 +118,6 @@ pub enum Response {
/// - [`GetIncludedBlock`](crate::method::ClientMethod::GetIncludedBlock)
Block(BlockDto),
/// Response for:
/// - [`BuildAndPostBlock`](crate::method::ClientMethod::BuildAndPostBlock)
/// - [`PostBlockPayload`](crate::method::ClientMethod::PostBlockPayload)
/// - [`Retry`](crate::method::ClientMethod::Retry)
BlockIdWithBlock(BlockId, BlockDto),
Expand Down Expand Up @@ -160,9 +156,6 @@ pub enum Response {
/// - [`RetryUntilIncluded`](crate::method::ClientMethod::RetryUntilIncluded)
RetryUntilIncludedSuccessful(Vec<(BlockId, BlockDto)>),
/// Response for:
/// - [`ConsolidateFunds`](crate::method::ClientMethod::ConsolidateFunds)
ConsolidatedFunds(Bech32Address),
/// Response for:
/// - [`FindInputs`](crate::method::ClientMethod::FindInputs)
Inputs(Vec<UtxoInputDto>),
/// Response for:
Expand Down
27 changes: 3 additions & 24 deletions bindings/core/tests/secrets_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,15 @@
// SPDX-License-Identifier: Apache-2.0

use iota_sdk::client::secret::SecretManagerDto;
use iota_sdk_bindings_core::{ClientMethod, Response, UtilsMethod, WalletOptions};
use iota_sdk_bindings_core::{Response, UtilsMethod, WalletOptions};

#[test]
fn method_interface_secrets_debug() {
let client_method = ClientMethod::BuildAndPostBlock {
secret_manager: None,
options: None,
};
assert_eq!(
format!("{:?}", client_method),
"BuildAndPostBlock { secret_manager: None, options: None }"
);

#[cfg(feature = "ledger_nano")]
{
let client_method = ClientMethod::BuildAndPostBlock {
secret_manager: Some(SecretManagerDto::LedgerNano(false)),
options: None,
};
assert_eq!(
format!("{:?}", client_method),
"BuildAndPostBlock { secret_manager: Some(<omitted>), options: None }"
);
}

let client_method = UtilsMethod::MnemonicToHexSeed {
let utils_method = UtilsMethod::MnemonicToHexSeed {
mnemonic: "mnemonic".to_string(),
};
assert_eq!(
format!("{:?}", client_method),
format!("{:?}", utils_method),
"MnemonicToHexSeed { mnemonic: <omitted> }"
);

Expand Down
5 changes: 4 additions & 1 deletion bindings/nodejs/examples/client/06-simple-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ async function run() {

try {
// Create block with no payload
const blockIdAndBlock = await client.buildAndPostBlock();
// TODO: have a way in the bindings to send an empty block
const blockIdAndBlock = await client.postBlockPayload(
new TaggedDataPayload(utf8ToHex('Hello'), utf8ToHex('Tangle')),
);
console.log('Block:', blockIdAndBlock, '\n');

console.log(
Expand Down
12 changes: 2 additions & 10 deletions bindings/nodejs/examples/client/08-data-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,10 @@ async function run() {
nodes: [process.env.NODE_URL],
});

const options = {
tag: utf8ToHex('Hello'),
data: utf8ToHex('Tangle'),
};
try {
const mnemonic = Utils.generateMnemonic();
const secretManager = { mnemonic: mnemonic };

// Create block with tagged payload
const blockIdAndBlock = await client.buildAndPostBlock(
secretManager,
options,
const blockIdAndBlock = await client.postBlockPayload(
new TaggedDataPayload(utf8ToHex('Hello'), utf8ToHex('Tangle')),
);

console.log(
Expand Down
61 changes: 0 additions & 61 deletions bindings/nodejs/examples/client/09-transaction.ts

This file was deleted.

Loading

0 comments on commit 7c89886

Please sign in to comment.