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
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)
```

View File

@ -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) {

View File

@ -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": {