Install the library using npm:
npm install path/to/monero-rpc
First, import the library and create a new instance:
import MoneroWalletRPC from 'monero-rpc';
const wallet = new MoneroWalletRPC('http://127.0.0.1:18083/json_rpc');
Create an integrated address for receiving funds.
const result = await wallet.makeIntegratedAddress('payment_id_here');
Set the daemon to use for this wallet.
const result = await wallet.setDaemon({
address: '127.0.0.1:18081',
trusted: true
});
Retrieve the balance of the wallet or specific account.
const result = await wallet.getBalance({
account_index: 0,
all_accounts: true
});
Get the address for a specific account and subaddress indices.
const result = await wallet.getAddress(0, [0, 1]);
Create a new address for an account.
const result = await wallet.createAddress(0, 'New subaddress');
Check if an address is valid.
const result = await wallet.validateAddress('4...');
Send a transaction.
const result = await wallet.transfer({
destinations: [{ amount: 1000000000000, address: '4...' }],
account_index: 0,
subaddr_indices: [0],
priority: 2,
mixin: 7
});
Sweep unmixable dust outputs.
const result = await wallet.sweepDust();
Get all accounts of the wallet.
const result = await wallet.getAccounts();
Get the current block height.
const result = await wallet.getHeight();
Stop the wallet, saving its state.
const result = await wallet.stopWallet();
Rescan the blockchain from scratch.
const result = await wallet.rescanBlockchain();
Get the transaction key for a given transaction.
const result = await wallet.getTxKey('transaction_id_here');
Get a list of transfers.
const result = await wallet.getTransfers({
in: true,
out: true,
pending: true,
failed: true,
pool: true
});
Start mining in the Monero daemon.
const result = await wallet.startMining(2, false, false);
Stop mining in the Monero daemon.
const result = await wallet.stopMining();
All methods return a Promise. Use try/catch blocks or .catch() to handle errors:
try {
const result = await wallet.getBalance();
console.log(result);
} catch (error) {
console.error('An error occurred:', error.message);
}
- Ensure your Monero wallet RPC server is running and accessible at the URL you provide when creating the MoneroWalletRPC instance.
- Some methods may require the wallet to be unlocked or may have other prerequisites. Refer to the official Monero documentation for detailed information on each RPC method.
- Always handle sensitive information (like private keys and transaction details) securely.
- Remember to manage your connections properly, especially in long-running applications.
For more detailed information on Monero RPC calls, refer to the official Monero RPC documentation: Monero RPC Documentation