Skip to content

Commit

Permalink
static async constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoralf-M committed Nov 17, 2023
1 parent d8bc188 commit cf8b374
Show file tree
Hide file tree
Showing 36 changed files with 58 additions and 50 deletions.
2 changes: 1 addition & 1 deletion bindings/nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const walletOptions: WalletOptions = {
},
},
};
const wallet = new Wallet(walletOptions);
const wallet = await Wallet.create(walletOptions);
```

## Examples
Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/examples/exchange/1-create-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async function run() {
secretManager: strongholdSecretManager,
};

const wallet = new Wallet(walletOptions);
const wallet = await Wallet.create(walletOptions);

// Set syncOnlyMostBasicOutputs to true if not interested in outputs that are timelocked,
// have a storage deposit return, expiration or are nft/account/foundry outputs.
Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/examples/exchange/3-check-balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function run() {
);
}

const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});
const address = await wallet.address();
Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/examples/exchange/4-listen-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function run() {
);
}

const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});

Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/examples/exchange/5-send-amount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function run() {
);
}

const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});

Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/examples/how_tos/account_output/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function run() {

try {
// Create the wallet
const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});

Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/examples/how_tos/account_output/destroy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function run() {

try {
// Create the wallet
const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function run() {
const faucetUrl = process.env.FAUCET_URL;

// Create the wallet
const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function run() {
},
};

const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function run() {
throw new Error('.env WALLET_DB_PATH is undefined, see .env.example');
}
try {
const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async function run() {
);
}

const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async function run() {
secretManager: strongholdSecretManager,
};

const wallet = new Wallet(walletOptions);
const wallet = await Wallet.create(walletOptions);

console.log(
'Generated wallet with address: ' + (await wallet.address()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function run() {
throw new Error('.env WALLET_DB_PATH is undefined, see .env.example');
}
try {
const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function run() {
throw new Error('.env WALLET_DB_PATH is undefined, see .env.example');
}
try {
const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function run() {
throw new Error('.env WALLET_DB_PATH is undefined, see .env.example');
}
try {
const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});
await wallet.sync({ syncIncomingTransactions: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function run() {
);
}

const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function run() {
);
}

const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function run() {
);
}

const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function run() {
}

// Create the wallet
const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async function run() {
}

// Create the wallet
const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});

Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/examples/how_tos/nfts/burn_nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async function run() {
}

// Create the wallet
const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});

Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/examples/how_tos/nfts/mint_nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function run() {
);
}

const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});

Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/examples/how_tos/nfts/send_nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function run() {
);
}

const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async function run() {
const faucetUrl = process.env.FAUCET_URL;

// Create the wallet
const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async function run() {
);
}

const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});

Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/examples/wallet/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async function getUnlockedWallet() {
const walletOptions: WalletOptions = {
storagePath: process.env.WALLET_DB_PATH,
};
const wallet = new Wallet(walletOptions);
const wallet = await Wallet.create(walletOptions);

return wallet;
}
Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/examples/wallet/getting-started.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async function main() {
secretManager: strongholdSecretManager,
};

const wallet = new Wallet(walletOptions);
const wallet = await Wallet.create(walletOptions);

console.log('Generated wallet with address: ' + (await wallet.address()));

Expand Down
12 changes: 10 additions & 2 deletions bindings/nodejs/lib/wallet/wallet-method-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ import { SecretManager } from '../secret_manager';
export class WalletMethodHandler {
methodHandler: any;

/**
* @param methodHandler The Rust method handler created in `WalletMethodHandler.create()`.
*/
constructor(methodHandler: any) {
this.methodHandler = methodHandler;
}

/**
* @param options The wallet options.
*/
constructor(options?: WalletOptions) {
this.methodHandler = createWallet(JSON.stringify(options));
static async create(options: WalletOptions): Promise<WalletMethodHandler> {
const methodHandler = await createWallet(JSON.stringify(options));
return new WalletMethodHandler(methodHandler);
}

/**
Expand Down
13 changes: 10 additions & 3 deletions bindings/nodejs/lib/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,17 @@ export class Wallet {
private methodHandler: WalletMethodHandler;

/**
* @param options Wallet options.
* @param methodHandler The Rust method handler created in `WalletMethodHandler.create()`.
*/
constructor(options: WalletOptions) {
this.methodHandler = new WalletMethodHandler(options);
constructor(methodHandler: WalletMethodHandler) {
this.methodHandler = methodHandler;
}

/**
* @param options The wallet options.
*/
static async create(options: WalletOptions): Promise<Wallet> {
return new Wallet(await WalletMethodHandler.create(options));
}

/**
Expand Down
5 changes: 2 additions & 3 deletions bindings/nodejs/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ use crate::{client::ClientMethodHandler, secret_manager::SecretManagerMethodHand
pub type WalletMethodHandler = Arc<RwLock<Option<Wallet>>>;

#[napi(js_name = "createWallet")]
pub fn create_wallet(options: String) -> Result<External<WalletMethodHandler>> {
pub async fn create_wallet(options: String) -> Result<External<WalletMethodHandler>> {
let wallet_options = serde_json::from_str::<WalletOptions>(&options).map_err(NodejsError::from)?;
let runtime = tokio::runtime::Runtime::new().map_err(NodejsError::from)?;
let wallet = runtime.block_on(wallet_options.build()).map_err(NodejsError::from)?;
let wallet = wallet_options.build().await.map_err(NodejsError::from)?;

Ok(External::new(Arc::new(RwLock::new(Some(wallet)))))
}
Expand Down
6 changes: 3 additions & 3 deletions bindings/nodejs/tests/wallet/wallet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('Wallet', () => {
};


const wallet = new Wallet(walletOptions);
const wallet = await Wallet.create(walletOptions);

await wallet.destroy()
removeDir(storagePath)
Expand Down Expand Up @@ -90,15 +90,15 @@ describe('Wallet', () => {
};


const wallet = new Wallet(walletOptions);
const wallet = await Wallet.create(walletOptions);

const client = await wallet.getClient();
const hrp = await client.getBech32Hrp();
expect(hrp).toEqual("smr");

await wallet.destroy();

const recreatedWallet = new Wallet({ storagePath: './test-recreate-wallet' });
const recreatedWallet = await Wallet.create({ storagePath: './test-recreate-wallet' });

await recreatedWallet.destroy()
removeDir(storagePath)
Expand Down
4 changes: 2 additions & 2 deletions bindings/wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ const { Wallet, CoinType } = require('@iota/sdk-wasm/node');
async function run() {
try {
const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: './my-database',
coinType: CoinType.Shimmer,
clientOptions: {
Expand Down Expand Up @@ -214,7 +214,7 @@ run().then(() => process.exit());
import init, {Wallet, CoinType} from "@iota/sdk-wasm/web";
init().then(async () => {
const wallet = new Wallet({
const wallet = await Wallet.create({
storagePath: './my-database',
coinType: CoinType.Shimmer,
clientOptions: {
Expand Down
2 changes: 1 addition & 1 deletion bindings/wasm/examples/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function run() {
bech32Hrp: 'tst',
});

const wallet = new Wallet({
const wallet = await Wallet.create({
address: walletAddress[0],
storagePath: './alice-database',
bipPath: {
Expand Down
7 changes: 2 additions & 5 deletions bindings/wasm/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@ pub struct WalletMethodHandler {
/// Creates a method handler with the given options.
#[wasm_bindgen(js_name = createWallet)]
#[allow(non_snake_case)]
pub fn create_wallet(options: String) -> Result<WalletMethodHandler, JsValue> {
pub async fn create_wallet(options: String) -> Result<WalletMethodHandler, JsValue> {
let wallet_options = serde_json::from_str::<WalletOptions>(&options).map_err(|e| e.to_string())?;

let wallet_method_handler = tokio::runtime::Builder::new_current_thread()
.build()
.map_err(|err| err.to_string())?
.block_on(async move { wallet_options.build().await.map_err(|e| e.to_string()) })?;
let wallet_method_handler = wallet_options.build().await.map_err(|e| e.to_string())?;

Ok(WalletMethodHandler {
wallet: Arc::new(Mutex::new(Some(wallet_method_handler))),
Expand Down
2 changes: 1 addition & 1 deletion bindings/wasm/test/wallet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function run() {
bech32Hrp: 'tst',
});

const wallet = new Wallet({
const wallet = await Wallet.create({
address: walletAddress[0],
bipPath: {
coinType: CoinType.IOTA,
Expand Down
3 changes: 0 additions & 3 deletions sdk/src/wallet/core/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,6 @@ where
// If the wallet builder is not set, it means the user provided it and we need to update the addresses.
// In the other case it was loaded from the database and addresses are up to date.
if provided_client_options {
// Can't call it in wasm, because it will panic with `condvar wait not supported`. Updating the bech32 hrp
// is still possible in wasm when setting the client options on the wallet.
#[cfg(not(target_family = "wasm"))]
wallet.update_bech32_hrp().await?;
}

Expand Down

0 comments on commit cf8b374

Please sign in to comment.