From afc930e3a4decea659396cadfdf49be8cb6fa681 Mon Sep 17 00:00:00 2001 From: Keith Irwin Date: Mon, 22 Apr 2024 11:39:23 -0600 Subject: [PATCH] fix: :bug: IIAFE for async constructor --- README.md | 36 +++++++++++++++++++++--------------- main.js | 8 +++++--- package.json | 2 +- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 3d409c3..109f718 100644 --- a/README.md +++ b/README.md @@ -4,22 +4,23 @@ Simple wrapper for monero-wallet-rpc calls. ```javascript import Wallet from 'monero-wallet-rpc-js' -(async () => { - // Load wallet - let wallet; try { - wallet = new Wallet({ - url: 'http://localhost:18082', - filename: 'mywallet', - password: 'secretpassword', - address: '4....', - viewKey: 'XXXXXXX...', - spendKey: 'XXXXXXX...', - restoreHeight: '573936', - }) - } catch (err) { - console.error(`Failed to open wallet: ${err}`) - } +// Load wallet +let loadedWallet; try { + loadedWallet = new Wallet({ + url: 'http://localhost:18082', + filename: 'mywallet', + password: 'secretpassword', + address: '4....', + viewKey: 'XXXXXXX...', + spendKey: 'XXXXXXX...', + restoreHeight: '573936', + }) +} catch (err) { + console.error(`Failed to open wallet: ${err}`) +} + +(async (wallet) => { // Save wallet await wallet.store() @@ -40,4 +41,9 @@ import Wallet from 'monero-wallet-rpc-js' console.log((await wallet.rpc('get_address', { account_index: 2 })).address) + + // Save and close wallet + await wallet.close() + +})(loadedWallet) ``` diff --git a/main.js b/main.js index 4112fa7..a4ee2fe 100644 --- a/main.js +++ b/main.js @@ -1,7 +1,8 @@ const axios = require('axios') module.exports = class Wallet { - constructor (settings) { + // IIAFE https://stackoverflow.com/a/50885340 + constructor (settings) { return (async () => { this.url = settings.url // Open wallet @@ -37,8 +38,9 @@ module.exports = class Wallet { } catch (err) { console.error(`Failed to refresh wallet: ${err}`) } - - } + + return this; + })() } // Arbitrary RPC call async rpc(method, params=undefined) { diff --git a/package.json b/package.json index 876bf87..3fdefe9 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "monero-wallet-rpc-js", "type": "commonjs", - "version": "1.2.9", + "version": "1.2.10", "description": "Javascript wrapper for monero-wallet-rpc calls", "main": "main.js", "scripts": {