import config from "$config"; import { authenticator } from "otplib"; import db from "$db"; import axios from "axios"; import bcrypt from "bcrypt"; import { l } from "$lib/logging"; export default async (user, ip, requireChallenge) => { if (!(user && user.username)) throw new Error("Username required"); user.username = user.username.replace(/ /g, ""); let exists = await db.User.count({ where: { username: user.username } }); if (exists) throw new Error(`Username ${user.username} taken`); if (user.password) { user.password = await bcrypt.hash(user.password, 1); } let countries = { CA: "CAD", US: "USD", JP: "JPY", CN: "CNY", AU: "AUD", GB: "GBP" }; if (!config.ipstack || ip.startsWith("127") || ip.startsWith("192")) user.currency = "CAD"; else { let info = await axios.get( `http://api.ipstack.com/${ip}?access_key=${config.ipstack}` ); user.currency = countries[info.data.country_code] || "USD"; } user.currencies = [...new Set([user.currency, "CAD", "USD"])]; user.fiat = false; user.otpsecret = authenticator.generateSecret(); user = await db.User.create(user); let account = await db.Account.create({ user_id: user.id, asset: config.liquid.btcasset, balance: 0, pending: 0, name: "Bitcoin", ticker: "BTC", precision: 8 }); user.accounts = [account]; const d = ip.split("."); const numericIp = ((+d[0] * 256 + +d[1]) * 256 + +d[2]) * 256 + +d[3]; if (Number.isInteger(numericIp)) { user.ip = numericIp; const ipExists = await db.User.findOne({ where: { ip: numericIp } }); } user.account_id = account.id; await user.save(); l("new user", user.username, ip); return user; }; a4d7a37945e0325'>diffstats
path: root/tools/ark/renew-txid-test.js
blob: ea0be35fa788b9ee424914150848b830c6538916 (plain) (tree)












































                                                                                                                                                                     
// A renewal remembers its round's transaction as soon as the round has run —
// before that transaction confirms, which is exactly when someone wants to
// look it up — so the coins page (and later the history row) can link to it.
// Run: bun tools/ark/renew-txid-test.js
import assert from 'node:assert/strict';
import { mock } from 'bun:test';
import { hex } from '@scure/base';

const refresh = await import('../../src/ark/refresh.js');
// version 2, one input, one 1000-sat OP_TRUE output
const rawTx = hex.decode('02000000' + '01' + '11'.repeat(32) + '00000000' + '00' + 'ffffffff' + '01' + 'e803000000000000' + '0151' + '00000000');
const wantTxid = refresh.parseTx(rawTx).txid;
let roundRan = false;
mock.module('../../src/ark/refresh.js', () => ({
  ...refresh,
  roundParticipationStatus: async () => (roundRan ? { status: 1, fundingTx: rawTx, outputVtxos: [] } : { status: 0 }),
}));
const { ArkManager } = await import('../../src/ark/manager.js');
let saves = 0;
const mgr = new ArkManager({ storage: { save() { saves++; } }, arkUrl: 'http://test.invalid' });
const asked = [];
Object.defineProperty(mgr, 'chain', { value: { getTxStatus: async (txid) => { asked.push(txid); return { confirmed: false }; } } });
const action = { id: 'refresh-1', type: 'refresh', step: 'submitted', inputIds: ['a:0'], outKeyIndex: 1, outAmountSat: 900, feeSat: 0, unlockHash: '22'.repeat(32) };
mgr.state = { nextKeyIndex: 2, movements: [], actions: [action], vtxos: [{ id: 'a:0', amountSat: 900, expiryHeight: 500, state: 'pending' }] };
mgr._refreshOutputs = () => [];

await mgr._driveRefresh(action);
assert.equal(action.fundingTxid, undefined, 'no transaction before the round has run');
assert.equal(action.step, 'submitted');
console.log('✓ Before the round runs there is no transaction to point at');

roundRan = true;
const before = saves;
await mgr._driveRefresh(action);
assert.equal(action.fundingTxid, wantTxid, 'the round transaction is kept');
assert.equal(action.step, 'submitted', 'still waiting for the confirmation');
assert.deepEqual(asked, [wantTxid]);
assert(saves > before, 'and saved, so a reload still has it');
console.log('✓ Once the round has run its transaction id is kept, though unconfirmed: ' + wantTxid.slice(0, 16) + '…');

const again = saves;
await mgr._driveRefresh(action);
assert.equal(saves, again, 'not saved again on every sync while it waits');
console.log('✓ ...and waiting for the confirmation writes nothing more');
console.log('\n✅ a renewal knows its round transaction');