Skip to content

Commit

Permalink
Add TryFromDto trait to unify functionality and reduce repeated code (
Browse files Browse the repository at this point in the history
#801)

* TryFromDto

* boop

* okay i missed a lot

* kind --> type

* fix changelog

* Update bindings/python/CHANGELOG.md

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

* Revert some renames

* Fix changelog

* Fix nodejs Changelog

* Nit

---------

Co-authored-by: Thoralf-M <[email protected]>
Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
3 people authored Jul 24, 2023
1 parent 851eca4 commit e78ba0d
Show file tree
Hide file tree
Showing 84 changed files with 1,095 additions and 1,279 deletions.
31 changes: 17 additions & 14 deletions bindings/core/src/message_interface_old/message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ use iota_sdk::{
secret::SecretManage,
utils, Client, NodeInfoWrapper,
},
types::block::{
address::{Hrp, ToBech32Ext},
output::{
dto::{OutputBuilderAmountDto, OutputDto},
AliasOutput, BasicOutput, FoundryOutput, NativeToken, NftOutput, Output, Rent,
types::{
block::{
address::{Hrp, ToBech32Ext},
output::{
dto::{OutputBuilderAmountDto, OutputDto},
AliasOutput, BasicOutput, FoundryOutput, NativeToken, NftOutput, Output, Rent,
},
signature::Ed25519Signature,
ConvertTo, Error,
},
signature::Ed25519Signature,
ConvertTo, Error,
TryFromDto,
},
wallet::{
account::{
Expand Down Expand Up @@ -708,7 +711,7 @@ impl WalletMessageHandler {
}
AccountMethod::MinimumRequiredStorageDeposit { output } => {
convert_async_panics(|| async {
let output = Output::try_from_dto(output, account.client().get_token_supply().await?)?;
let output = Output::try_from_dto_with_params(output, account.client().get_token_supply().await?)?;
let rent_structure = account.client().get_rent_structure().await?;

let minimum_storage_deposit = output.rent_cost(&rent_structure);
Expand Down Expand Up @@ -760,7 +763,7 @@ impl WalletMessageHandler {
.prepare_transaction(
outputs
.into_iter()
.map(|o| Ok(Output::try_from_dto(o, token_supply)?))
.map(|o| Ok(Output::try_from_dto_with_params(o, token_supply)?))
.collect::<Result<Vec<Output>>>()?,
options.map(TransactionOptions::try_from_dto).transpose()?,
)
Expand Down Expand Up @@ -837,7 +840,7 @@ impl WalletMessageHandler {
.send_outputs(
outputs
.into_iter()
.map(|o| Ok(Output::try_from_dto(o, token_supply)?))
.map(|o| Ok(Output::try_from_dto_with_params(o, token_supply)?))
.collect::<iota_sdk::wallet::Result<Vec<_>>>()?,
options.map(TransactionOptions::try_from_dto).transpose()?,
)
Expand All @@ -851,9 +854,9 @@ impl WalletMessageHandler {
} => {
convert_async_panics(|| async {
let signed_transaction_data = account
.sign_transaction_essence(&PreparedTransactionData::try_from_dto(
.sign_transaction_essence(&PreparedTransactionData::try_from_dto_with_params(
prepared_transaction_data,
&account.client().get_protocol_parameters().await?,
account.client().get_protocol_parameters().await?,
)?)
.await?;
Ok(Response::SignedTransactionData(SignedTransactionDataDto::from(
Expand All @@ -866,9 +869,9 @@ impl WalletMessageHandler {
signed_transaction_data,
} => {
convert_async_panics(|| async {
let signed_transaction_data = SignedTransactionData::try_from_dto(
let signed_transaction_data = SignedTransactionData::try_from_dto_with_params(
signed_transaction_data,
&account.client().get_protocol_parameters().await?,
account.client().get_protocol_parameters().await?,
)?;
let transaction = account
.submit_and_store_transaction(signed_transaction_data, None)
Expand Down
2 changes: 1 addition & 1 deletion bindings/core/src/method/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub enum UtilsMethod {
/// Serial number
serial_number: u32,
/// Token scheme kind
token_scheme_kind: u8,
token_scheme_type: u8,
},
/// Computes the NFT ID
#[serde(rename_all = "camelCase")]
Expand Down
29 changes: 13 additions & 16 deletions bindings/core/src/method_handler/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ use std::str::FromStr;

use iota_sdk::{
client::api::{
input_selection::Burn, PreparedTransactionData, PreparedTransactionDataDto, SignedTransactionData,
SignedTransactionDataDto,
PreparedTransactionData, PreparedTransactionDataDto, SignedTransactionData, SignedTransactionDataDto,
},
types::{
block::output::{dto::OutputDto, Output},
TryFromDto,
},
types::block::output::{dto::OutputDto, Output},
wallet::account::{
types::TransactionDto, Account, OutputDataDto, PreparedCreateNativeTokenTransactionDto, TransactionOptions,
},
Expand Down Expand Up @@ -109,10 +111,7 @@ pub(crate) async fn call_account_method_internal(account: &Account, method: Acco
}
AccountMethod::PrepareBurn { burn, options } => {
let data = account
.prepare_burn(
Burn::try_from(burn)?,
options.map(TransactionOptions::try_from_dto).transpose()?,
)
.prepare_burn(burn, options.map(TransactionOptions::try_from_dto).transpose()?)
.await?;
Response::PreparedTransaction(PreparedTransactionDataDto::from(&data))
}
Expand Down Expand Up @@ -231,7 +230,7 @@ pub(crate) async fn call_account_method_internal(account: &Account, method: Acco
.prepare_transaction(
outputs
.into_iter()
.map(|o| Ok(Output::try_from_dto(o, token_supply)?))
.map(|o| Ok(Output::try_from_dto_with_params(o, token_supply)?))
.collect::<Result<Vec<Output>>>()?,
options.map(TransactionOptions::try_from_dto).transpose()?,
)
Expand Down Expand Up @@ -284,7 +283,7 @@ pub(crate) async fn call_account_method_internal(account: &Account, method: Acco
.send_outputs(
outputs
.into_iter()
.map(|o| Ok(Output::try_from_dto(o, token_supply)?))
.map(|o| Ok(Output::try_from_dto_with_params(o, token_supply)?))
.collect::<iota_sdk::wallet::Result<Vec<Output>>>()?,
options.map(TransactionOptions::try_from_dto).transpose()?,
)
Expand All @@ -304,9 +303,9 @@ pub(crate) async fn call_account_method_internal(account: &Account, method: Acco
} => {
let transaction = account
.sign_and_submit_transaction(
PreparedTransactionData::try_from_dto(
PreparedTransactionData::try_from_dto_with_params(
prepared_transaction_data,
&account.client().get_protocol_parameters().await?,
account.client().get_protocol_parameters().await?,
)?,
None,
)
Expand All @@ -317,18 +316,16 @@ pub(crate) async fn call_account_method_internal(account: &Account, method: Acco
prepared_transaction_data,
} => {
let signed_transaction_data = account
.sign_transaction_essence(&PreparedTransactionData::try_from_dto_unverified(
prepared_transaction_data,
)?)
.sign_transaction_essence(&PreparedTransactionData::try_from_dto(prepared_transaction_data)?)
.await?;
Response::SignedTransactionData(SignedTransactionDataDto::from(&signed_transaction_data))
}
AccountMethod::SubmitAndStoreTransaction {
signed_transaction_data,
} => {
let signed_transaction_data = SignedTransactionData::try_from_dto(
let signed_transaction_data = SignedTransactionData::try_from_dto_with_params(
signed_transaction_data,
&account.client().get_protocol_parameters().await?,
account.client().get_protocol_parameters().await?,
)?;
let transaction = account
.submit_and_store_transaction(signed_transaction_data, None)
Expand Down
16 changes: 9 additions & 7 deletions bindings/core/src/method_handler/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use iota_sdk::{
payload::{dto::MilestonePayloadDto, Payload},
Block, BlockDto,
},
TryFromDto,
},
};

Expand Down Expand Up @@ -239,9 +240,7 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM

Response::SignedTransaction(
(&block_builder
.sign_transaction(PreparedTransactionData::try_from_dto_unverified(
prepared_transaction_data,
)?)
.sign_transaction(PreparedTransactionData::try_from_dto(prepared_transaction_data)?)
.await?)
.into(),
)
Expand All @@ -250,9 +249,9 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
let block_builder = client.build_block();

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

Expand All @@ -277,7 +276,10 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
),
ClientMethod::PostBlock { block } => Response::BlockId(
client
.post_block(&Block::try_from_dto(block, &client.get_protocol_parameters().await?)?)
.post_block(&Block::try_from_dto_with_params(
block,
client.get_protocol_parameters().await?,
)?)
.await?,
),
ClientMethod::GetBlock { block_id } => Response::Block(BlockDto::from(&client.get_block(&block_id).await?)),
Expand Down Expand Up @@ -428,7 +430,7 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
Response::Bech32Address(client.hex_public_key_to_bech32_address(&hex, bech32_hrp).await?)
}
ClientMethod::MinimumRequiredStorageDeposit { output } => {
let output = Output::try_from_dto(output, client.get_token_supply().await?)?;
let output = Output::try_from_dto_with_params(output, client.get_token_supply().await?)?;
let rent_structure = client.get_rent_structure().await?;

let minimum_storage_deposit = output.rent_cost(&rent_structure);
Expand Down
9 changes: 5 additions & 4 deletions bindings/core/src/method_handler/secret_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use iota_sdk::{
api::PreparedTransactionData,
secret::{SecretManage, SecretManager},
},
types::block::{signature::dto::Ed25519SignatureDto, unlock::Unlock},
types::{
block::{signature::dto::Ed25519SignatureDto, unlock::Unlock},
TryFromDto,
},
};
use tokio::sync::RwLock;

Expand Down Expand Up @@ -39,9 +42,7 @@ pub(crate) async fn call_secret_manager_method_internal(
prepared_transaction_data,
} => {
let transaction = &secret_manager
.sign_transaction(PreparedTransactionData::try_from_dto_unverified(
prepared_transaction_data,
)?)
.sign_transaction(PreparedTransactionData::try_from_dto(prepared_transaction_data)?)
.await?;
Response::SignedTransaction(transaction.into())
}
Expand Down
42 changes: 21 additions & 21 deletions bindings/core/src/method_handler/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
use crypto::keys::bip39::Mnemonic;
use iota_sdk::{
client::{hex_public_key_to_bech32_address, hex_to_bech32, verify_mnemonic, Client},
types::block::{
address::{dto::AddressDto, Address, AliasAddress, ToBech32Ext},
input::{dto::UtxoInputDto, UtxoInput},
output::{AliasId, FoundryId, InputsCommitment, NftId, Output, OutputId, Rent, TokenId},
payload::{transaction::TransactionEssence, MilestonePayload, TransactionPayload},
signature::Ed25519Signature,
Block,
types::{
block::{
address::{dto::AddressDto, Address, AliasAddress, ToBech32Ext},
input::UtxoInput,
output::{AliasId, FoundryId, InputsCommitment, NftId, Output, OutputId, Rent, TokenId},
payload::{transaction::TransactionEssence, MilestonePayload, TransactionPayload},
signature::Ed25519Signature,
Block,
},
TryFromDto,
},
};

Expand All @@ -36,26 +39,26 @@ pub(crate) fn call_utils_method_internal(method: UtilsMethod) -> Result<Response
Response::MnemonicHexSeed(Client::mnemonic_to_hex_seed(mnemonic)?)
}
UtilsMethod::BlockId { block } => {
let block = Block::try_from_dto_unverified(block)?;
let block = Block::try_from_dto(block)?;
Response::BlockId(block.id())
}
UtilsMethod::MilestoneId { payload } => {
let payload = MilestonePayload::try_from_dto_unverified(payload)?;
let payload = MilestonePayload::try_from_dto(payload)?;
Response::MilestoneId(payload.id())
}
UtilsMethod::TransactionId { payload } => {
let payload = TransactionPayload::try_from_dto_unverified(payload)?;
let payload = TransactionPayload::try_from_dto(payload)?;
Response::TransactionId(payload.id())
}
UtilsMethod::ComputeAliasId { output_id } => Response::AliasId(AliasId::from(&output_id)),
UtilsMethod::ComputeFoundryId {
alias_id,
serial_number,
token_scheme_kind,
token_scheme_type,
} => Response::FoundryId(FoundryId::build(
&AliasAddress::new(alias_id),
serial_number,
token_scheme_kind,
token_scheme_type,
)),
UtilsMethod::ComputeNftId { output_id } => Response::NftId(NftId::from(&output_id)),
UtilsMethod::ComputeOutputId { id, index } => Response::OutputId(OutputId::new(id, index)?),
Expand All @@ -67,18 +70,18 @@ pub(crate) fn call_utils_method_internal(method: UtilsMethod) -> Result<Response
let foundry_id = FoundryId::build(&AliasAddress::new(alias_id), serial_number, token_scheme_type);
Response::TokenId(TokenId::from(foundry_id))
}
UtilsMethod::HashTransactionEssence { essence } => Response::Hash(prefix_hex::encode(
TransactionEssence::try_from_dto_unverified(essence)?.hash(),
)),
UtilsMethod::HashTransactionEssence { essence } => {
Response::Hash(prefix_hex::encode(TransactionEssence::try_from_dto(essence)?.hash()))
}
UtilsMethod::ComputeInputsCommitment { inputs } => {
let inputs = inputs
.into_iter()
.map(|o| Ok(Output::try_from_dto_unverified(o)?))
.map(|o| Ok(Output::try_from_dto(o)?))
.collect::<Result<Vec<Output>>>()?;
Response::Hash(InputsCommitment::new(inputs.iter()).to_string())
}
UtilsMethod::ComputeStorageDeposit { output, rent } => {
let out = Output::try_from_dto_unverified(output)?;
let out = Output::try_from_dto(output)?;
Response::MinimumRequiredStorageDeposit(out.rent_cost(&rent).to_string())
}
UtilsMethod::VerifyMnemonic { mnemonic } => {
Expand All @@ -105,10 +108,7 @@ pub(crate) fn call_utils_method_internal(method: UtilsMethod) -> Result<Response
let message: Vec<u8> = prefix_hex::decode(message)?;
Response::Bool(public_key.verify_keccak256(&signature, &message))
}
UtilsMethod::OutputIdToUtxoInput { output_id } => {
let input: UtxoInputDto = UtxoInputDto::from(&UtxoInput::from(output_id));
Response::Input(input)
}
UtilsMethod::OutputIdToUtxoInput { output_id } => Response::Input((&UtxoInput::from(output_id)).into()),
};
Ok(response)
}
3 changes: 2 additions & 1 deletion bindings/nodejs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- `Utils.computeStorageDeposit()`;
- `Utils.computeTokenId()`
- `Utils.computeTokenId()`;
- `Utils.computeFoundryId` 's parameter `tokenSchemeKind` renamed to `tokenSchemeType`;

## 1.0.0-rc.3 - 2023-07-21

Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/lib/types/utils/bridge/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface __ComputeFoundryIdMethod__ {
data: {
aliasId: AliasId;
serialNumber: number;
tokenSchemeKind: number;
tokenSchemeType: number;
};
}

Expand Down
4 changes: 2 additions & 2 deletions bindings/nodejs/lib/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ export class Utils {
static computeFoundryId(
aliasId: AliasId,
serialNumber: number,
tokenSchemeKind: number,
tokenSchemeType: number,
): FoundryId {
return callUtilsMethod({
name: 'computeFoundryId',
data: {
aliasId,
serialNumber,
tokenSchemeKind,
tokenSchemeType,
},
});
}
Expand Down
4 changes: 2 additions & 2 deletions bindings/nodejs/tests/client/utilityMethods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ describe('Client utility methods', () => {
const aliasId =
'0xcf077d276686ba64c0404b9eb2d15556782113c5a1985f262b70f9964d3bbd7f';
const serialNumber = 0;
const tokenSchemeKind = 0;
const tokenSchemeType = 0;

const foundryId = Utils.computeFoundryId(
aliasId,
serialNumber,
tokenSchemeKind,
tokenSchemeType,
);

expect(foundryId).toBe(
Expand Down
1 change: 1 addition & 0 deletions bindings/python/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- `Account::prepare_consolidate_outputs` takes a `ConsolidationParams`;
- Utils `compute_token_id` and `compute_foundry_id` param `token_scheme_kind` renamed to `token_scheme_type`;

### Fixed

Expand Down
Loading

0 comments on commit e78ba0d

Please sign in to comment.