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 Jan 11, 2024
2 parents 15e073b + 5ac148f commit a5449dd
Show file tree
Hide file tree
Showing 49 changed files with 753 additions and 486 deletions.
21 changes: 21 additions & 0 deletions bindings/core/src/method/secret_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ pub enum SecretManagerMethod {
#[derivative(Debug(format_with = "OmittedDebug::omitted_fmt"))]
mnemonic: String,
},
/// Set the stronghold password.
/// Expected response: [`Ok`](crate::Response::Ok)
#[cfg(feature = "stronghold")]
#[cfg_attr(docsrs, doc(cfg(feature = "stronghold")))]
SetStrongholdPassword {
#[derivative(Debug(format_with = "OmittedDebug::omitted_fmt"))]
password: String,
},
/// Change the stronghold password.
/// Expected response: [`Ok`](crate::Response::Ok)
#[cfg(feature = "stronghold")]
#[cfg_attr(docsrs, doc(cfg(feature = "stronghold")))]
ChangeStrongholdPassword {
#[derivative(Debug(format_with = "OmittedDebug::omitted_fmt"))]
password: String,
},
/// Clear the stronghold password.
/// Expected response: [`Ok`](crate::Response::Ok)
#[cfg(feature = "stronghold")]
#[cfg_attr(docsrs, doc(cfg(feature = "stronghold")))]
ClearStrongholdPassword
}

#[cfg(test)]
Expand Down
36 changes: 36 additions & 0 deletions bindings/core/src/method_handler/secret_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,42 @@ where
return Err(iota_sdk::client::Error::SecretManagerMismatch.into());
}
}
#[cfg(feature = "stronghold")]
SecretManagerMethod::SetStrongholdPassword { password } => {
let stronghold = if let Some(secret_manager) = secret_manager.downcast::<StrongholdSecretManager>() {
secret_manager
} else if let Some(SecretManager::Stronghold(secret_manager)) = secret_manager.downcast::<SecretManager>() {
secret_manager
} else {
return Err(iota_sdk::client::Error::SecretManagerMismatch.into());
};
stronghold.set_password(password).await?;
Response::Ok
}
#[cfg(feature = "stronghold")]
SecretManagerMethod::ChangeStrongholdPassword { password } => {
let stronghold = if let Some(secret_manager) = secret_manager.downcast::<StrongholdSecretManager>() {
secret_manager
} else if let Some(SecretManager::Stronghold(secret_manager)) = secret_manager.downcast::<SecretManager>() {
secret_manager
} else {
return Err(iota_sdk::client::Error::SecretManagerMismatch.into());
};
stronghold.change_password(password).await?;
Response::Ok
}
#[cfg(feature = "stronghold")]
SecretManagerMethod::ClearStrongholdPassword => {
let stronghold = if let Some(secret_manager) = secret_manager.downcast::<StrongholdSecretManager>() {
secret_manager
} else if let Some(SecretManager::Stronghold(secret_manager)) = secret_manager.downcast::<SecretManager>() {
secret_manager
} else {
return Err(iota_sdk::client::Error::SecretManagerMismatch.into());
};
stronghold.clear_key().await;
Response::Ok
}
};
Ok(response)
}
11 changes: 5 additions & 6 deletions bindings/nodejs/examples/exchange/4-listen-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
// Run with command:
// yarn run-example ./exchange/4-listen-events.ts

import { Wallet, Event, WalletEventType } from '@iota/sdk';
import { Wallet, WalletEvent, WalletEventType } from '@iota/sdk';

// This example uses secrets in environment variables for simplicity which should not be done in production.
require('dotenv').config({ path: '.env' });

async function run() {
try {
for (const envVar of ['WALLET_DB_PATH']) {
for (const envVar of ['WALLET_DB_PATH', 'FAUCET_URL']) {
if (!(envVar in process.env)) {
throw new Error(
`.env ${envVar} is undefined, see .env.example`,
Expand All @@ -24,9 +24,8 @@ async function run() {
storagePath: process.env.WALLET_DB_PATH,
});

const callback = function (err: any, event: Event) {
console.log('AccountIndex:', event.accountIndex);
console.log('Event:', event.event);
const callback = function (err: any, event: WalletEvent) {
console.log('Event:', event);

// Exit after receiving an event.
process.exit(0);
Expand All @@ -37,7 +36,7 @@ async function run() {

// Use the faucet to send testnet tokens to your address.
console.log(
'Fill your address with the faucet: https://faucet.testnet.shimmer.network/',
`Fill your address with the faucet: ${process.env.FAUCET_URL}`,
);

const address = await wallet.address();
Expand Down
6 changes: 2 additions & 4 deletions bindings/nodejs/examples/wallet/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import {
Event,
ConsolidationRequiredWalletEvent,
WalletEvent,
TransactionProgressWalletEvent,
SelectingInputsProgress,
} from '@iota/sdk';
Expand All @@ -24,13 +23,12 @@ async function run() {
// Create the wallet
const wallet = await getUnlockedWallet();

const callback = function (err: any, event: Event) {
const callback = function (err: any, event: WalletEvent) {
console.log('Event:', event);
};

await wallet.listen([], callback);

await wallet.emitTestEvent(new ConsolidationRequiredWalletEvent());
await wallet.emitTestEvent(
new TransactionProgressWalletEvent(new SelectingInputsProgress()),
);
Expand Down
29 changes: 29 additions & 0 deletions bindings/nodejs/lib/secret_manager/secret-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,33 @@ export class SecretManager {

return JSON.parse(response).payload;
}

/**
* Set the Stronghold password.
*/
async setStrongholdPassword(password: string): Promise<void> {
await this.methodHandler.callMethod({
name: 'setStrongholdPassword',
data: { password },
});
}

/**
* Change the Stronghold password.
*/
async changeStrongholdPassword(password: string): Promise<void> {
await this.methodHandler.callMethod({
name: 'changeStrongholdPassword',
data: { password },
});
}

/**
* Clear the Stronghold password.
*/
async clearStrongholdPassword(): Promise<void> {
await this.methodHandler.callMethod({
name: 'clearStrongholdPassword',
});
}
}
4 changes: 4 additions & 0 deletions bindings/nodejs/lib/types/models/info/node-info-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ export interface RewardsParameters {
* in the pool rewards calculations.
*/
poolCoefficientExponent: number;
/**
* The number of epochs for which rewards are retained.
*/
retentionPeriod: number;
}

/**
Expand Down
8 changes: 7 additions & 1 deletion bindings/nodejs/lib/types/secret_manager/bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import type {
__SignatureUnlockMethod__,
__SignEd25519Method__,
__SignSecp256k1EcdsaMethod__,
__SetStrongholdPasswordMethod__,
__ChangeStrongholdPasswordMethod__,
__ClearStrongholdPasswordMethod__,
} from './secret-manager';

export type __SecretManagerMethods__ =
Expand All @@ -19,4 +22,7 @@ export type __SecretManagerMethods__ =
| __SignatureUnlockMethod__
| __StoreMnemonicMethod__
| __SignEd25519Method__
| __SignSecp256k1EcdsaMethod__;
| __SignSecp256k1EcdsaMethod__
| __SetStrongholdPasswordMethod__
| __ChangeStrongholdPasswordMethod__
| __ClearStrongholdPasswordMethod__;
14 changes: 14 additions & 0 deletions bindings/nodejs/lib/types/secret_manager/bridge/secret-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,17 @@ export interface __SignSecp256k1EcdsaMethod__ {
export interface __GetLedgerNanoStatusMethod__ {
name: 'getLedgerNanoStatus';
}

export interface __SetStrongholdPasswordMethod__ {
name: 'setStrongholdPassword';
data: { password: string };
}

export interface __ChangeStrongholdPasswordMethod__ {
name: 'changeStrongholdPassword';
data: { password: string };
}

export interface __ClearStrongholdPasswordMethod__ {
name: 'clearStrongholdPassword';
}
46 changes: 5 additions & 41 deletions bindings/nodejs/lib/types/wallet/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,20 @@ import { HexEncodedString } from '../utils';
*/
export type TransactionId = string;

/**
* An wallet account event.
*/
class Event {
/**
* The account index for which the event was emitted.
*/
accountIndex: number;
/**
* The wallet event.
*/
event: WalletEvent;

/**
* @param accountIndex The account index.
* @param event The wallet event.
*/
constructor(accountIndex: number, event: WalletEvent) {
this.accountIndex = accountIndex;
this.event = event;
}
}

/**
* All of the wallet event types.
*/
enum WalletEventType {
/** Consolidation is required. */
ConsolidationRequired = 0,
/** Nano Ledger has generated an address. */
LedgerAddressGeneration = 1,
LedgerAddressGeneration = 0,
/** A new output was created. */
NewOutput = 2,
NewOutput = 1,
/** An output was spent. */
SpentOutput = 3,
SpentOutput = 2,
/** A transaction was included into the ledger. */
TransactionInclusion = 4,
TransactionInclusion = 3,
/** A progress update while submitting a transaction. */
TransactionProgress = 5,
TransactionProgress = 4,
}

/**
Expand All @@ -68,15 +43,6 @@ abstract class WalletEvent {
}
}

/**
* A 'consolidation required' wallet event.
*/
class ConsolidationRequiredWalletEvent extends WalletEvent {
constructor() {
super(WalletEventType.ConsolidationRequired);
}
}

/**
* A 'ledger address generation' wallet event.
*/
Expand Down Expand Up @@ -280,10 +246,8 @@ class BroadcastingProgress extends TransactionProgress {
}

export {
Event,
WalletEventType,
WalletEvent,
ConsolidationRequiredWalletEvent,
LedgerAddressGenerationWalletEvent,
NewOutputWalletEvent,
SpentOutputWalletEvent,
Expand Down
8 changes: 4 additions & 4 deletions bindings/nodejs/lib/wallet/wallet-method-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
} from '../bindings';
import {
WalletEventType,
WalletEvent,
WalletOptions,
__WalletMethod__,
Event,
} from '../types/wallet';
import { Client, ClientMethodHandler } from '../client';
import { SecretManager, SecretManagerMethodHandler } from '../secret_manager';
Expand Down Expand Up @@ -74,17 +74,17 @@ export class WalletMethodHandler {
*/
async listen(
eventTypes: WalletEventType[],
callback: (error: Error, event: Event) => void,
callback: (error: Error, event: WalletEvent) => void,
): Promise<void> {
return listenWallet(
this.methodHandler,
eventTypes,
function (err: any, data: string) {
const parsed = JSON.parse(data);
const parsed: WalletEvent = JSON.parse(data);
callback(
// Send back raw error instead of parsing
err,
new Event(parsed.accountIndex, parsed.event),
parsed,
);
},
).catch((error: any) => {
Expand Down
3 changes: 1 addition & 2 deletions bindings/nodejs/lib/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import type {
WalletOptions,
WalletEventType,
WalletEvent,
Event,
} from '../types/wallet';
import { IAuth, IClientOptions, LedgerNanoStatus } from '../types/client';
import { SecretManager } from '../secret_manager';
Expand Down Expand Up @@ -169,7 +168,7 @@ export class Wallet {
*/
async listen(
eventTypes: WalletEventType[],
callback: (error: Error, event: Event) => void,
callback: (error: Error, event: WalletEvent) => void,
): Promise<void> {
return this.methodHandler.listen(eventTypes, callback);
}
Expand Down
5 changes: 2 additions & 3 deletions bindings/python/examples/exchange/4_listen_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@

def callback(event):
"""Callback function for the event listener"""
event_dict = json.loads(event)
print('AccountIndex:', event_dict["accountIndex"])
print('Event:', event_dict["event"])
event = json.loads(event)
print('Event:', event)

# Exit after receiving an event.
# pylint: disable=global-statement
Expand Down
22 changes: 10 additions & 12 deletions bindings/python/iota_sdk/types/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ class WalletEventType(IntEnum):
"""Types of wallet events.
Attributes:
ConsolidationRequired (0): Consolidation is required.
LedgerAddressGeneration (1): Nano Ledger has generated an address.
NewOutput (2): A new output was created.
SpentOutput (3): An output was spent.
TransactionInclusion (4): A transaction was included into the ledger.
TransactionProgress (5): A progress update while submitting a transaction.
LedgerAddressGeneration (0): Nano Ledger has generated an address.
NewOutput (1): A new output was created.
SpentOutput (2): An output was spent.
TransactionInclusion (3): A transaction was included into the ledger.
TransactionProgress (4): A progress update while submitting a transaction.
"""
ConsolidationRequired = 0
LedgerAddressGeneration = 1
NewOutput = 2
SpentOutput = 3
TransactionInclusion = 4
TransactionProgress = 5
LedgerAddressGeneration = 0
NewOutput = 1
SpentOutput = 2
TransactionInclusion = 3
TransactionProgress = 4
2 changes: 2 additions & 0 deletions bindings/python/iota_sdk/types/node_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ class RewardsParameters:
decay_balancing_constant_exponent: The exponent used for calculation of the initial reward.
decay_balancing_constant: An integer approximation which is calculated using the `decay_balancing_constant_exponent`.
pool_coefficient_exponent: The exponent used for shifting operation during the pool rewards calculations.
retention_period: The number of epochs for which rewards are retained.
"""
profit_margin_exponent: int
bootstrapping_duration: int
Expand All @@ -220,6 +221,7 @@ class RewardsParameters:
encoder=str
))
pool_coefficient_exponent: int
retention_period: int


@json
Expand Down
Loading

0 comments on commit a5449dd

Please sign in to comment.