fix: 🐛 Fixed account missing

main
Keith Irwin 2024-04-22 18:02:46 -06:00
parent f4b0c75f24
commit 742aae921d
Signed by: ki9
GPG Key ID: DF773B3F4A88DA86
3 changed files with 55 additions and 17 deletions

56
main.js
View File

@ -4,6 +4,8 @@ const Wallet = require('monero-wallet-rpc-js')
;(async () => {
const deAtomize = (atomic) => Number(atomic) * 0.000000000001
dotenv.config()
const WALLET_ACCOUNT_INDEX = Number(process.env.WALLET_ACCOUNT_INDEX)
const WALLET_AUTOSAVE_SEC = Number(process.env.WALLET_AUTOSAVE_SEC)
@ -33,12 +35,38 @@ const Wallet = require('monero-wallet-rpc-js')
console.error(`Failed to set auto_refresh to ${process.env.WALLET_REFRESH_SEC}: ${err}`)
}
// Confirm sync
try {
console.log(`Wallet synced to block ${await wallet.getHeight()}.`)
} catch (err) {
console.error(`Failed to get blockchain height: ${err}`)
}
// Check accounts, create account if needed
// TODO: Validate that the admin didn't set WALLET_ACCOUNT_INDEX too high
const getAccount = async (i) => {
try {
accounts = (await wallet.getAccounts()).subaddress_accounts
} catch (err) {
console.error(`Failed to get account listing from rpc: ${err}`)
}
account = accounts.find(a => a.account_index == i)
if (account===undefined) {
console.log(`There is no account #${i}. Creating account...`)
try {
const newAccount = await wallet.createAccount()
console.log(`Created account #${newAccount.account_index}.`)
} catch (err) {
console.error(`Failed to create new account: ${err}`)
}
return getAccount(i)
} else {
console.log(`Account #${i} exists with a balance of ${deAtomize(account.balance)} XMR.`)
return account
}
}; getAccount(WALLET_ACCOUNT_INDEX)
// Server
const app = express()
app.use(express.json())
@ -49,20 +77,30 @@ const Wallet = require('monero-wallet-rpc-js')
app.get('/wallet/height', async (req, res) =>
res.send((await wallet.getHeight()).toString())
)
app.get('/wallet/balance', async (req, res) =>
res.send(
String((await wallet.getBalance({
app.get('/wallet/balance', async (req, res) => {
let result; try {
result = await wallet.getBalance({
account_index: WALLET_ACCOUNT_INDEX,
})).balance * 0.000000000001)
)
)
})
} catch (err) {
console.error(`Failed to get balance: ${err}`)
return res.sendStatus(500)
}
return res.send(`${deAtomize(result.balance)} XMR`)
})
// New payment
app.post('/payment', async (req, res) => {
res.redirect(`/payment/${(await wallet.createAddress({
account_index: WALLET_ACCOUNT_INDEX,
let result; try {
result = await wallet.createAddress({
account_index: 1,
label: req.body.label,
})).address}`)
})
} catch (err) {
console.error(`Failed to create address "${req.body.label}": ${err}`)
return res.sendStatus(500)
}
return res.redirect(`/payment/${result.address}`)
})
// Check payment

12
package-lock.json generated
View File

@ -1,17 +1,17 @@
{
"name": "pago",
"version": "1.1.3",
"version": "1.1.5",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "pago",
"version": "1.1.3",
"version": "1.1.5",
"license": "MIT",
"dependencies": {
"dotenv": "^16.4.5",
"express": "^4.19.2",
"monero-wallet-rpc-js": "^1.2.18",
"monero-wallet-rpc-js": "^1.2.20",
"socket.io": "^4.7.5"
}
},
@ -625,9 +625,9 @@
}
},
"node_modules/monero-wallet-rpc-js": {
"version": "1.2.18",
"resolved": "https://registry.npmjs.org/monero-wallet-rpc-js/-/monero-wallet-rpc-js-1.2.18.tgz",
"integrity": "sha512-Fl8/sg0Gkgpj8nw4lLLrVSheaW983bnqa4iwPLpJ4xuuduXjOqjVk1iHJE5hZwH/EfIiI8j5iVwkCxd7Wpp/IA==",
"version": "1.2.20",
"resolved": "https://registry.npmjs.org/monero-wallet-rpc-js/-/monero-wallet-rpc-js-1.2.20.tgz",
"integrity": "sha512-Hvdxvugs911QL6lte9SaauQLUfTT+ZXrAJ1W9DX5acczEqhSncuneN9bSFQnXgGKeIg37FU9dkVfFK3G1Rkl6g==",
"dependencies": {
"axios": "^1.6.8"
}

View File

@ -1,6 +1,6 @@
{
"name": "pago",
"version": "1.1.4",
"version": "1.1.5",
"description": "Lightweight monero payment gateway",
"main": "main.js",
"scripts": {
@ -18,7 +18,7 @@
"dependencies": {
"dotenv": "^16.4.5",
"express": "^4.19.2",
"monero-wallet-rpc-js": "^1.2.18",
"monero-wallet-rpc-js": "^1.2.20",
"socket.io": "^4.7.5"
}
}