Skip to content

Commit

Permalink
Merge branch '2.0' into api-tips-example-test
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez authored Dec 6, 2023
2 parents aab9618 + 6d3e4c9 commit 4bb3472
Show file tree
Hide file tree
Showing 54 changed files with 7,994 additions and 5,724 deletions.
72 changes: 14 additions & 58 deletions bindings/core/src/method/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,95 +29,51 @@ use crate::OmittedDebug;
#[non_exhaustive]
pub enum UtilsMethod {
/// Transforms bech32 to hex
Bech32ToHex {
/// Bech32 encoded address
bech32: Bech32Address,
},
Bech32ToHex { bech32: Bech32Address },
/// Transforms a hex encoded address to a bech32 encoded address
#[serde(rename_all = "camelCase")]
HexToBech32 {
/// Hex encoded bech32 address
hex: String,
/// Human readable part
bech32_hrp: Hrp,
},
HexToBech32 { hex: String, bech32_hrp: Hrp },
/// Transforms an account id to a bech32 encoded address
#[serde(rename_all = "camelCase")]
AccountIdToBech32 {
/// Account ID
account_id: AccountId,
/// Human readable part
bech32_hrp: Hrp,
},
AccountIdToBech32 { account_id: AccountId, bech32_hrp: Hrp },
/// Transforms an nft id to a bech32 encoded address
#[serde(rename_all = "camelCase")]
NftIdToBech32 {
/// Nft ID
nft_id: NftId,
/// Human readable part
bech32_hrp: Hrp,
},
NftIdToBech32 { nft_id: NftId, bech32_hrp: Hrp },
/// Transforms a hex encoded public key to a bech32 encoded address
#[serde(rename_all = "camelCase")]
HexPublicKeyToBech32Address {
/// Hex encoded public key
hex: String,
/// Human readable part
bech32_hrp: Hrp,
},
HexPublicKeyToBech32Address { hex: String, bech32_hrp: Hrp },
/// Returns a valid Address parsed from a String.
ParseBech32Address {
/// Address
address: Bech32Address,
},
ParseBech32Address { address: Bech32Address },
/// Checks if a String is a valid bech32 encoded address.
IsAddressValid {
/// Address
address: String,
},
IsAddressValid { address: String },
/// Generates a new mnemonic.
GenerateMnemonic,
/// Returns a hex encoded seed for a mnemonic.
MnemonicToHexSeed {
/// Mnemonic
#[derivative(Debug(format_with = "OmittedDebug::omitted_fmt"))]
mnemonic: String,
},
/// Returns a block ID from a block and slot protocol parameters
#[serde(rename_all = "camelCase")]
BlockId {
/// Block
block: BlockDto,
/// Network Protocol Parameters
protocol_parameters: ProtocolParameters,
},
/// Returns the transaction ID (Blake2b256 hash of the provided transaction payload)
TransactionId {
/// Transaction Payload
payload: SignedTransactionPayloadDto,
},
TransactionId { payload: SignedTransactionPayloadDto },
/// Computes the account ID
#[serde(rename_all = "camelCase")]
ComputeAccountId {
/// Output ID
output_id: OutputId,
},
ComputeAccountId { output_id: OutputId },
/// Computes the Foundry ID
#[serde(rename_all = "camelCase")]
ComputeFoundryId {
/// Alias id
account_id: AccountId,
/// Serial number
serial_number: u32,
/// Token scheme kind
token_scheme_type: u8,
},
/// Computes the NFT ID
#[serde(rename_all = "camelCase")]
ComputeNftId {
/// Output ID
output_id: OutputId,
},
ComputeNftId { output_id: OutputId },
/// Computes the output ID from transaction id and output index
ComputeOutputId { id: TransactionId, index: u16 },
/// Computes a tokenId from the aliasId, serial number and token scheme type.
Expand All @@ -127,11 +83,11 @@ pub enum UtilsMethod {
serial_number: u32,
token_scheme_type: u8,
},
/// Computes the hash of the given protocol parameters.
#[serde(rename_all = "camelCase")]
ProtocolParametersHash { protocol_parameters: ProtocolParameters },
/// Computes the signing hash of a transaction.
TransactionSigningHash {
/// The transaction.
transaction: TransactionDto,
},
TransactionSigningHash { transaction: TransactionDto },
/// Computes the minimum required amount of an output.
#[serde(rename_all = "camelCase")]
ComputeMinimumOutputAmount {
Expand Down
9 changes: 8 additions & 1 deletion bindings/core/src/method/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#[cfg(feature = "stronghold")]
use std::path::PathBuf;

use crypto::{keys::bip44::Bip44, signatures::ed25519::PublicKey};
use derivative::Derivative;
#[cfg(feature = "events")]
use iota_sdk::wallet::events::types::{WalletEvent, WalletEventType};
Expand Down Expand Up @@ -194,7 +195,13 @@ pub enum WalletMethod {
/// Prepares to transition an implicit account to an account.
/// Expected response: [`PreparedTransaction`](crate::Response::PreparedTransaction)
#[serde(rename_all = "camelCase")]
PrepareImplicitAccountTransition { output_id: OutputId },
PrepareImplicitAccountTransition {
output_id: OutputId,
#[serde(default)]
public_key: Option<String>,
#[serde(default)]
bip_path: Option<Bip44>,
},
/// Returns the implicit accounts of the wallet.
/// Expected response: [`OutputsData`](crate::Response::OutputsData)
ImplicitAccounts,
Expand Down
3 changes: 3 additions & 0 deletions bindings/core/src/method_handler/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ pub(crate) fn call_utils_method_internal(method: UtilsMethod) -> Result<Response
let foundry_id = FoundryId::build(&AccountAddress::new(account_id), serial_number, token_scheme_type);
Response::TokenId(TokenId::from(foundry_id))
}
UtilsMethod::ProtocolParametersHash { protocol_parameters } => {
Response::Hash(protocol_parameters.hash().to_string())
}
UtilsMethod::TransactionSigningHash { transaction } => {
Response::Hash(Transaction::try_from_dto(transaction)?.signing_hash().to_string())
}
Expand Down
24 changes: 19 additions & 5 deletions bindings/core/src/method_handler/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use std::time::Duration;

use crypto::signatures::ed25519::PublicKey;
use iota_sdk::{
client::api::{
PreparedTransactionData, PreparedTransactionDataDto, SignedTransactionData, SignedTransactionDataDto,
Expand Down Expand Up @@ -204,13 +205,25 @@ pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletM
let implicit_account_creation_address = wallet.implicit_account_creation_address().await?;
Response::Bech32Address(implicit_account_creation_address)
}
WalletMethod::PrepareImplicitAccountTransition {
output_id,
public_key,
bip_path,
} => {
let data = if let Some(public_key_str) = public_key {
let public_key = PublicKey::try_from_bytes(prefix_hex::decode(public_key_str)?)
.map_err(iota_sdk::wallet::Error::from)?;
wallet
.prepare_implicit_account_transition(&output_id, Some(public_key))
.await?
} else {
wallet.prepare_implicit_account_transition(&output_id, bip_path).await?
};
Response::PreparedTransaction(PreparedTransactionDataDto::from(&data))
}
WalletMethod::ImplicitAccounts => {
Response::OutputsData(wallet.data().await.implicit_accounts().cloned().collect())
}
WalletMethod::PrepareImplicitAccountTransition { output_id } => {
let data = wallet.prepare_implicit_account_transition(&output_id).await?;
Response::PreparedTransaction(PreparedTransactionDataDto::from(&data))
}
WalletMethod::IncomingTransactions => Response::Transactions(
wallet
.data()
Expand Down Expand Up @@ -368,6 +381,7 @@ pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletM
&wallet.client().get_protocol_parameters().await?,
)?,
None,
None,
)
.await?;
Response::SentTransaction(TransactionWithMetadataDto::from(&transaction))
Expand All @@ -388,7 +402,7 @@ pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletM
&wallet.client().get_protocol_parameters().await?,
)?;
let transaction = wallet
.submit_and_store_transaction(signed_transaction_data, None)
.submit_and_store_transaction(signed_transaction_data, None, None)
.await?;
Response::SentTransaction(TransactionWithMetadataDto::from(&transaction))
}
Expand Down
24 changes: 18 additions & 6 deletions bindings/nodejs/lib/types/models/info/node-info-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,18 @@ export interface ProtocolParameters {
* Work Score Parameters lists the work score of each type, it is used to denote the computation costs of processing an object.
*/
workScoreParameters: WorkScoreParameters;
/**
* The parameters used by mana calculation.
*/
manaParameters: ManaParameters;
/**
* Current supply of base token.
*/
tokenSupply: u64;
/**
* Genesis Slot defines the slot of the genesis.
*/
genesisSlot: number;
/**
* The genesis timestamp at which the slots start to count.
*/
Expand All @@ -63,10 +71,6 @@ export interface ProtocolParameters {
* The number of slots in an epoch expressed as an exponent of 2.
*/
slotsPerEpochExponent: number;
/**
* The parameters used by mana calculation.
*/
manaParameters: ManaParameters;
/**
* The unbonding period in epochs before an account can stop staking.
*/
Expand All @@ -80,9 +84,13 @@ export interface ProtocolParameters {
*/
punishmentEpochs: number;
/**
* Determine if a block is eligible by evaluating issuingTime and commitments in its pastcone to ATT and lastCommittedSlot respectively.
* Liveness Threshold Lower Bound is used by tip-selection to determine if a block is eligible by evaluating issuingTimes.
*/
livenessThreshold: number;
livenessThresholdLowerBound: number;
/**
* Liveness Threshold Upper Bound is used by tip-selection to determine if a block is eligible by evaluating issuingTimes.
*/
livenessThresholdUpperBound: number;
/**
* MinCommittableAge is the minimum age relative to the accepted tangle time slot index that a slot can be committed.
*/
Expand All @@ -107,6 +115,10 @@ export interface ProtocolParameters {
* Rewards Parameters defines the parameters that are used to calculate Mana rewards.
*/
rewardsParameters: RewardsParameters;
/**
* Target Committee Size defines the target size of the committee. If there's fewer candidates the actual committee size could be smaller in a given epoch.
*/
targetCommitteeSize: number;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions bindings/nodejs/lib/types/utils/bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
__NftIdToBech32Method__,
__HexPublicKeyToBech32AddressMethod__,
__IsAddressValidMethod__,
__ProtocolParametersHashMethod__,
__TransactionSigningHashMethod__,
__VerifyEd25519SignatureMethod__,
__VerifySecp256k1EcdsaSignatureMethod__,
Expand Down Expand Up @@ -45,6 +46,7 @@ export type __UtilsMethods__ =
| __NftIdToBech32Method__
| __HexPublicKeyToBech32AddressMethod__
| __IsAddressValidMethod__
| __ProtocolParametersHashMethod__
| __TransactionSigningHashMethod__
| __VerifyEd25519SignatureMethod__
| __VerifySecp256k1EcdsaSignatureMethod__
Expand Down
7 changes: 7 additions & 0 deletions bindings/nodejs/lib/types/utils/bridge/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ export interface __IsAddressValidMethod__ {
};
}

export interface __ProtocolParametersHashMethod__ {
name: 'protocolParametersHash';
data: {
protocolParameters: ProtocolParameters;
};
}

export interface __TransactionSigningHashMethod__ {
name: 'transactionSigningHash';
data: {
Expand Down
17 changes: 17 additions & 0 deletions bindings/nodejs/lib/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,23 @@ export class Utils {
});
}

/**
* Compute the hash of an instance of ProtocolParameters.
*
* @param protocolParameters A ProtocolParameters instance.
* @returns The hash of the protocol parameters as a hex-encoded string.
*/
static protocolParametersHash(
protocolParameters: ProtocolParameters,
): HexEncodedString {
return callUtilsMethod({
name: 'protocolParametersHash',
data: {
protocolParameters,
},
});
}

/**
* Compute the signing hash of a transaction.
*
Expand Down
4 changes: 2 additions & 2 deletions bindings/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"typescript": "^4.9.4"
},
"devDependencies": {
"@napi-rs/cli": "^1.0.0",
"@types/jest": "^29.4.0",
"@typescript-eslint/eslint-plugin": "^5.30.7",
"@typescript-eslint/parser": "^5.30.7",
Expand All @@ -48,8 +49,7 @@
"typedoc": "^0.24.6",
"typedoc-plugin-markdown": "^3.14.0",
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4",
"@napi-rs/cli": "^1.0.0"
"webpack-cli": "^5.1.4"
},
"overrides": {
"tar@<=4.4.17": "^4.4.19",
Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/tests/client/examples.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { describe, it } from '@jest/globals';
import { expect, describe, it } from '@jest/globals';
import {
Client,
utf8ToHex,
Expand Down
21 changes: 21 additions & 0 deletions bindings/nodejs/tests/types/protocol_parameters.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import 'reflect-metadata';

import { expect, describe, it } from '@jest/globals';
import * as protocol_parameters from '../../../../sdk/tests/types/fixtures/protocol_parameters.json';
import { ProtocolParameters } from '../../lib/types/models/info/node-info-protocol';
import { Utils } from '../../';

describe('ProtocolParameters tests', () => {

it('compares ProtocolParameters hash from a fixture', async () => {

const params = protocol_parameters.params as unknown as ProtocolParameters;
const hash = Utils.protocolParametersHash(params);
const expected_hash = protocol_parameters.hash;

expect(hash).toEqual(expected_hash);
});
});
Loading

0 comments on commit 4bb3472

Please sign in to comment.