Create xrp package json #1
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
// index.js — XRPL Dev Boilerplate (Express API)
// Usage: configure .env, then
npm startrequire('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const xrpl = require('xrpl');
const app = express();
app.use(bodyParser.json());
const PORT = process.env.PORT || 3000;
const NETWORK = process.env.XRPL_NETWORK === 'mainnet'
? 'wss://s1.ripple.com' // Mainnet public node (be careful with real XRP)
: 'wss://s.altnet.rippletest.net:51233'; // Testnet
// Shared client instance (connect once)
const client = new xrpl.Client(NETWORK);
// connect at startup
(async () => {
try {
await client.connect();
console.log(
Connected to XRPL network: ${NETWORK});} catch (err) {
console.error('Failed to connect to XRPL:', err);
process.exit(1);
}
})();
/**
*/
function xrpToDrops(xrpAmount) {
// xrpl.js provides this util, but keep a small helper for safety
return xrpl.xrpToDrops(String(xrpAmount));
}
/**
*/
// Generate a fresh wallet (Testnet/mainnet compatible - funding required on testnet)
app.get('/wallet/new', async (req, res) => {
try {
const wallet = xrpl.Wallet.generate();
// wallet: { seed, classicAddress, publicKey, privateKey }
res.json({
ok: true,
wallet: {
seed: wallet.seed,
address: wallet.classicAddress,
publicKey: wallet.publicKey
},
warning: 'Keep your seed private. This endpoint returns the seed for dev only.'
});
} catch (err) {
res.status(500).json({ ok: false, error: err.toString() });
}
});
// Import from seed
app.post('/wallet/import', async (req, res) => {
try {
const { seed } = req.body;
if (!seed) return res.status(400).json({ ok: false, error: 'seed required' });
const wallet = xrpl.Wallet.fromSeed(seed);
res.json({
ok: true,
wallet: {
address: wallet.classicAddress,
publicKey: wallet.publicKey
}
});
} catch (err) {
res.status(500).json({ ok: false, error: err.toString() });
}
});
// Get account balance
app.get('/balance/:address', async (req, res) => {
const address = req.params.address;
try {
const response = await client.request({
command: 'account_info',
account: address,
ledger_index: 'validated'
});
const balanceDrops = response.result.account_data.Balance;
const balanceXrp = xrpl.dropsToXrp(balanceDrops);
res.json({ ok: true, address, balance: balanceXrp, raw: response.result.account_data });
} catch (err) {
// If account doesn't exist yet on ledger (unfunded) you'll get an error
res.status(400).json({ ok: false, error: err.message || err.toString() });
}
});
// Send XRP (dangerous on mainnet — dev use only)
// POST body: { seed: "<s...>", destination: "r...", amount: "1.5" }
app.post('/send', async (req, res) => {
try {
const { seed, destination, amount } = req.body;
if (!seed || !destination || !amount) {
return res.status(400).json({ ok: false, error: 'seed, destination, and amount are required' });
}
} catch (err) {
res.status(500).json({ ok: false, error: err.message || err.toString() });
}
});
// Get transaction details by hash
app.get('/tx/:hash', async (req, res) => {
const hash = req.params.hash;
try {
const response = await client.request({
command: 'tx',
transaction: hash
});
res.json({ ok: true, tx: response.result });
} catch (err) {
res.status(400).json({ ok: false, error: err.message || err.toString() });
}
});
// Simple health check
app.get('/health', (req, res) => res.json({ ok: true, network: NETWORK }));
// Shutdown handler to close client
process.on('SIGINT', async () => {
console.log('Shutting down — disconnecting XRPL client');
try { await client.disconnect(); } catch (e) {}
process.exit(0);
});
app.listen(PORT, () => {
console.log(
XRPL dev API listening on http://localhost:${PORT});});