fix: 🐛 IIAFE for async constructor

main
Keith Irwin 2024-04-22 11:39:23 -06:00
parent 69c9b19c10
commit afc930e3a4
Signed by: ki9
GPG Key ID: DF773B3F4A88DA86
3 changed files with 27 additions and 19 deletions

View File

@ -4,22 +4,23 @@ Simple wrapper for monero-wallet-rpc calls.
```javascript ```javascript
import Wallet from 'monero-wallet-rpc-js' import Wallet from 'monero-wallet-rpc-js'
(async () => {
// Load wallet // Load wallet
let wallet; try { let loadedWallet; try {
wallet = new Wallet({ loadedWallet = new Wallet({
url: 'http://localhost:18082', url: 'http://localhost:18082',
filename: 'mywallet', filename: 'mywallet',
password: 'secretpassword', password: 'secretpassword',
address: '4....', address: '4....',
viewKey: 'XXXXXXX...', viewKey: 'XXXXXXX...',
spendKey: 'XXXXXXX...', spendKey: 'XXXXXXX...',
restoreHeight: '573936', restoreHeight: '573936',
}) })
} catch (err) { } catch (err) {
console.error(`Failed to open wallet: ${err}`) console.error(`Failed to open wallet: ${err}`)
} }
(async (wallet) => {
// Save wallet // Save wallet
await wallet.store() await wallet.store()
@ -40,4 +41,9 @@ import Wallet from 'monero-wallet-rpc-js'
console.log((await wallet.rpc('get_address', { console.log((await wallet.rpc('get_address', {
account_index: 2 account_index: 2
})).address) })).address)
// Save and close wallet
await wallet.close()
})(loadedWallet)
``` ```

View File

@ -1,7 +1,8 @@
const axios = require('axios') const axios = require('axios')
module.exports = class Wallet { module.exports = class Wallet {
constructor (settings) { // IIAFE https://stackoverflow.com/a/50885340
constructor (settings) { return (async () => {
this.url = settings.url this.url = settings.url
// Open wallet // Open wallet
@ -37,8 +38,9 @@ module.exports = class Wallet {
} catch (err) { } catch (err) {
console.error(`Failed to refresh wallet: ${err}`) console.error(`Failed to refresh wallet: ${err}`)
} }
} return this;
})() }
// Arbitrary RPC call // Arbitrary RPC call
async rpc(method, params=undefined) { async rpc(method, params=undefined) {

View File

@ -1,7 +1,7 @@
{ {
"name": "monero-wallet-rpc-js", "name": "monero-wallet-rpc-js",
"type": "commonjs", "type": "commonjs",
"version": "1.2.9", "version": "1.2.10",
"description": "Javascript wrapper for monero-wallet-rpc calls", "description": "Javascript wrapper for monero-wallet-rpc calls",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {