javascript-solid-server 0.0.130 → 0.0.132
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/handlers/pay.js +8 -4
- package/src/token.js +21 -20
package/package.json
CHANGED
package/src/handlers/pay.js
CHANGED
|
@@ -507,7 +507,11 @@ export function createPayHandler(options = {}) {
|
|
|
507
507
|
|
|
508
508
|
const didUri = pubkeyToDidNostr(pubkey);
|
|
509
509
|
const ledger = await readLedger();
|
|
510
|
-
|
|
510
|
+
if (body?.currency && (!payChains || !payChains.includes(body.currency))) {
|
|
511
|
+
return reply.code(400).send({ error: `Unsupported currency: ${body.currency}`, enabledChains: payChains || [] });
|
|
512
|
+
}
|
|
513
|
+
const currency = body?.currency || null;
|
|
514
|
+
const balance = getBalance(ledger, didUri, currency);
|
|
511
515
|
|
|
512
516
|
// Calculate withdrawal amount
|
|
513
517
|
let satCost, tokenAmount;
|
|
@@ -562,7 +566,7 @@ export function createPayHandler(options = {}) {
|
|
|
562
566
|
}
|
|
563
567
|
|
|
564
568
|
// Debit balance
|
|
565
|
-
debit(ledger, didUri, satCost);
|
|
569
|
+
debit(ledger, didUri, satCost, currency);
|
|
566
570
|
await writeLedger(ledger);
|
|
567
571
|
|
|
568
572
|
return reply.send({
|
|
@@ -570,8 +574,8 @@ export function createPayHandler(options = {}) {
|
|
|
570
574
|
ticker: payToken,
|
|
571
575
|
cost: satCost,
|
|
572
576
|
rate: payRate,
|
|
573
|
-
balance: getBalance(ledger, didUri),
|
|
574
|
-
unit: 'sat',
|
|
577
|
+
balance: getBalance(ledger, didUri, currency),
|
|
578
|
+
unit: currency || 'sat',
|
|
575
579
|
txid: result.txid,
|
|
576
580
|
proof: {
|
|
577
581
|
state: result.state,
|
package/src/token.js
CHANGED
|
@@ -187,33 +187,34 @@ async function broadcastTx(rawTxHex, mempoolUrl) {
|
|
|
187
187
|
}
|
|
188
188
|
|
|
189
189
|
// --- Trail persistence ---
|
|
190
|
-
function trailDir() {
|
|
191
|
-
return path.join(process.env.DATA_ROOT || './data', '.well-known', 'token');
|
|
190
|
+
function trailDir(root) {
|
|
191
|
+
return path.join(root || process.env.DATA_ROOT || './data', '.well-known', 'token');
|
|
192
192
|
}
|
|
193
193
|
|
|
194
|
-
function trailPath(ticker) {
|
|
195
|
-
return path.join(trailDir(), `${ticker.toLowerCase()}.json`);
|
|
194
|
+
function trailPath(ticker, root) {
|
|
195
|
+
return path.join(trailDir(root), `${ticker.toLowerCase()}.json`);
|
|
196
196
|
}
|
|
197
197
|
|
|
198
|
-
export async function loadTrail(ticker) {
|
|
198
|
+
export async function loadTrail(ticker, root) {
|
|
199
199
|
try {
|
|
200
|
-
const data = await fs.readFile(trailPath(ticker), 'utf8');
|
|
200
|
+
const data = await fs.readFile(trailPath(ticker, root), 'utf8');
|
|
201
201
|
return JSON.parse(data);
|
|
202
202
|
} catch { return null; }
|
|
203
203
|
}
|
|
204
204
|
|
|
205
|
-
async function saveTrail(trail) {
|
|
206
|
-
await fs.ensureDir(trailDir());
|
|
207
|
-
await fs.writeFile(trailPath(trail.ticker), JSON.stringify(trail, null, 2));
|
|
205
|
+
async function saveTrail(trail, root) {
|
|
206
|
+
await fs.ensureDir(trailDir(root));
|
|
207
|
+
await fs.writeFile(trailPath(trail.ticker, root), JSON.stringify(trail, null, 2));
|
|
208
208
|
}
|
|
209
209
|
|
|
210
|
-
export async function listTrails() {
|
|
210
|
+
export async function listTrails(root) {
|
|
211
211
|
try {
|
|
212
|
-
const
|
|
212
|
+
const dir = trailDir(root);
|
|
213
|
+
const files = await fs.readdir(dir);
|
|
213
214
|
const trails = [];
|
|
214
215
|
for (const f of files) {
|
|
215
216
|
if (f.endsWith('.json')) {
|
|
216
|
-
const data = await fs.readFile(path.join(
|
|
217
|
+
const data = await fs.readFile(path.join(dir, f), 'utf8');
|
|
217
218
|
trails.push(JSON.parse(data));
|
|
218
219
|
}
|
|
219
220
|
}
|
|
@@ -235,14 +236,14 @@ export function parseTxoUri(uri) {
|
|
|
235
236
|
}
|
|
236
237
|
|
|
237
238
|
// --- Mint: create genesis MRC20 token ---
|
|
238
|
-
export async function mintToken({ ticker, name, supply, voucher, mempoolUrl = 'https://mempool.space/testnet4', network = 'testnet4' }) {
|
|
239
|
+
export async function mintToken({ ticker, name, supply, voucher, mempoolUrl = 'https://mempool.space/testnet4', network = 'testnet4', root }) {
|
|
239
240
|
const txo = parseTxoUri(voucher);
|
|
240
241
|
const privkeyBytes = hexToU8(txo.privkey);
|
|
241
242
|
const pubkeyBase = new Uint8Array(secp256k1.getPublicKey(privkeyBytes, true));
|
|
242
243
|
const pubkeyBaseHex = bytesToHex(pubkeyBase);
|
|
243
244
|
|
|
244
245
|
// Check if token already exists
|
|
245
|
-
const existing = await loadTrail(ticker);
|
|
246
|
+
const existing = await loadTrail(ticker, root);
|
|
246
247
|
if (existing) throw new Error(`Token ${ticker} already exists`);
|
|
247
248
|
|
|
248
249
|
// Create genesis MRC20 state
|
|
@@ -300,14 +301,14 @@ export async function mintToken({ ticker, name, supply, voucher, mempoolUrl = 'h
|
|
|
300
301
|
network,
|
|
301
302
|
dateCreated: new Date().toISOString()
|
|
302
303
|
};
|
|
303
|
-
await saveTrail(trail);
|
|
304
|
+
await saveTrail(trail, root);
|
|
304
305
|
|
|
305
306
|
return { trail, txid: newTxid, address: genesisAddr };
|
|
306
307
|
}
|
|
307
308
|
|
|
308
309
|
// --- Transfer: send tokens to an address ---
|
|
309
|
-
export async function transferToken({ ticker, from, to, amount, mempoolUrl = 'https://mempool.space/testnet4' }) {
|
|
310
|
-
const trail = await loadTrail(ticker);
|
|
310
|
+
export async function transferToken({ ticker, from, to, amount, mempoolUrl = 'https://mempool.space/testnet4', root }) {
|
|
311
|
+
const trail = await loadTrail(ticker, root);
|
|
311
312
|
if (!trail) throw new Error(`Token ${ticker} not found`);
|
|
312
313
|
|
|
313
314
|
const privkeyBytes = hexToU8(trail.privkey);
|
|
@@ -382,14 +383,14 @@ export async function transferToken({ ticker, from, to, amount, mempoolUrl = 'ht
|
|
|
382
383
|
trail.currentTxid = newTxid;
|
|
383
384
|
trail.currentVout = 0;
|
|
384
385
|
trail.currentAmount = outputAmount;
|
|
385
|
-
await saveTrail(trail);
|
|
386
|
+
await saveTrail(trail, root);
|
|
386
387
|
|
|
387
388
|
return { trail, txid: newTxid, address: newAddr, state: newState, prevState: currentState };
|
|
388
389
|
}
|
|
389
390
|
|
|
390
391
|
// --- Info: show token state ---
|
|
391
|
-
export async function tokenInfo(ticker) {
|
|
392
|
-
const trail = await loadTrail(ticker);
|
|
392
|
+
export async function tokenInfo(ticker, { root } = {}) {
|
|
393
|
+
const trail = await loadTrail(ticker, root);
|
|
393
394
|
if (!trail) throw new Error(`Token ${ticker} not found`);
|
|
394
395
|
|
|
395
396
|
const currentState = trail.states[trail.states.length - 1];
|