feat: productionize paid modes and PvP capture

This commit is contained in:
jwinterm
2026-07-21 20:41:27 -04:00
parent 8fb284f8ed
commit 89398335c7
230 changed files with 31755 additions and 2777 deletions
+236 -16
View File
@@ -3,6 +3,34 @@ const WalletRPCService = require('../src/payments/walletRPCService');
// Minimal debugManager mock
const debugManagerMock = { CONSOLE_LOGGING: false };
function confirmedTransfer({ txid, amount, height, minor, major = 0, confirmations = 1,
doubleSpend = false, unlockTime = 0 }) {
return {
txid,
amount,
height,
confirmations,
double_spend_seen: doubleSpend,
unlock_time: unlockTime,
subaddr_index: { major, minor }
};
}
function incomingOutput({ txHash, amount, blockHeight, minor, major = 0, pubkey,
globalIndex, frozen = false, spent = false, unlocked = false }) {
return {
tx_hash: txHash,
amount,
block_height: blockHeight,
frozen,
global_index: globalIndex,
pubkey,
spent,
subaddr_index: { major, minor },
unlocked
};
}
describe('WalletRPCService interface', () => {
test('exposes expected methods', () => {
@@ -25,6 +53,17 @@ describe('WalletRPCService interface', () => {
await expect(svc.processBatchPayout([])).rejects.toThrow(/destinations/i);
});
test('the injected payout gate blocks every transfer entrypoint before wallet RPC', async () => {
const svc = new WalletRPCService(debugManagerMock, { transferAllowed: () => false });
svc.rpcCall = jest.fn();
await expect(svc.processPayout({ address: '5'.repeat(95), amount: 1 }))
.rejects.toMatchObject({ code: 'PAYOUT_DISPATCH_DISABLED', statusCode: 503 });
await expect(svc.processBatchPayout([{ address: '5'.repeat(95), amount: 1 }]))
.rejects.toMatchObject({ code: 'PAYOUT_DISPATCH_DISABLED', statusCode: 503 });
expect(svc.rpcCall).not.toHaveBeenCalled();
});
test('rpcCall wraps failures in ExternalServiceError', async () => {
const svc = new WalletRPCService(debugManagerMock);
svc.walletEndpoint = 'http://127.0.0.1:65535';
@@ -73,10 +112,10 @@ describe('WalletRPCService.normalizeAtomicAmount', () => {
expect(result).toBe(largeValue.toString());
});
test('truncates floating point numbers to integers', () => {
expect(svc.normalizeAtomicAmount(100.9)).toBe(100);
expect(svc.normalizeAtomicAmount(100.1)).toBe(100);
expect(svc.normalizeAtomicAmount(99.9999)).toBe(99);
test('rejects fractional atomic-unit numbers', () => {
expect(() => svc.normalizeAtomicAmount(100.9)).toThrow();
expect(() => svc.normalizeAtomicAmount(100.1)).toThrow();
expect(() => svc.normalizeAtomicAmount(99.9999)).toThrow();
});
test('throws on zero amount', () => {
@@ -154,7 +193,7 @@ describe('WalletRPCService.processPayout', () => {
svc.validateAddress = jest.fn().mockResolvedValue({ valid: true });
svc.rpcCall = jest.fn().mockResolvedValueOnce({
result: {
tx_hash_list: ['abc123def456'],
tx_hash_list: ['a'.repeat(64)],
tx_key_list: ['key789'],
fee_list: [1000000]
}
@@ -168,7 +207,7 @@ describe('WalletRPCService.processPayout', () => {
});
expect(result.success).toBe(true);
expect(result.txHash).toBe('abc123def456');
expect(result.txHash).toBe('a'.repeat(64));
expect(result.txKey).toBe('key789');
expect(result.fee).toBe(1000000);
expect(result.userId).toBe('user1');
@@ -189,7 +228,7 @@ describe('WalletRPCService.processPayout', () => {
test('includes subaddr_indices when subaddressIndex specified', async () => {
svc.validateAddress = jest.fn().mockResolvedValue({ valid: true });
svc.rpcCall = jest.fn().mockResolvedValueOnce({
result: { tx_hash_list: ['xyz'], tx_key_list: ['key'], fee_list: [100] }
result: { tx_hash_list: ['b'.repeat(64)], tx_key_list: ['key'], fee_list: [100] }
});
await svc.processPayout({
@@ -208,7 +247,7 @@ describe('WalletRPCService.processPayout', () => {
test('does not include subaddr_indices for invalid index values', async () => {
svc.validateAddress = jest.fn().mockResolvedValue({ valid: true });
svc.rpcCall = jest.fn().mockResolvedValueOnce({
result: { tx_hash_list: ['xyz'], tx_key_list: ['key'], fee_list: [100] }
result: { tx_hash_list: ['c'.repeat(64)], tx_key_list: ['key'], fee_list: [100] }
});
await svc.processPayout({
@@ -412,20 +451,23 @@ describe('WalletRPCService.checkPaymentStatus', () => {
confirmed: false
});
// Mock RPC calls
// Contract-shaped wallet RPC calls: get_transfers has transaction metadata but no output
// identity; incoming_transfers has pubkey/global_index and uses tx_hash/block_height names.
svc.rpcCall = jest.fn()
.mockResolvedValueOnce({
result: {
pool: [],
in: [{
txid: 'txhash123',
amount: 100000000000,
height: 1000
}]
in: [confirmedTransfer({
txid: 'd'.repeat(64), amount: 100000000000, height: 1000,
confirmations: 5, minor: 5
})]
}
})
.mockResolvedValueOnce({
result: { height: 1005 } // Current height for confirmations
result: { transfers: [incomingOutput({
txHash: 'd'.repeat(64), pubkey: '1'.repeat(64), globalIndex: 12345,
amount: 100000000000, blockHeight: 1000, minor: 5
})] }
});
const status = await svc.checkPaymentStatus('wow1testaddr');
@@ -434,7 +476,185 @@ describe('WalletRPCService.checkPaymentStatus', () => {
expect(status.confirmed).toBe(true);
expect(status.amount).toBe(100000000000);
expect(status.confirmations).toBe(5); // 1005 - 1000
expect(status.txHash).toBe('txhash123');
expect(status.txHash).toBe('d'.repeat(64));
expect(status.receipts).toEqual([expect.objectContaining({
txHash: 'd'.repeat(64),
outputId: '1'.repeat(64),
evidenceId: `${'d'.repeat(64)}:${'1'.repeat(64)}`,
addressIndex: 5,
amount: '100000000000',
confirmed: true
})]);
expect(svc.rpcCall).toHaveBeenNthCalledWith(2, 'incoming_transfers', {
transfer_type: 'all', account_index: 0, subaddr_indices: [5]
});
});
test('does not combine a confirmed dust transfer with an unconfirmed pool top-up', async () => {
svc.addressToUser.set('wow1mixed', {
userId: 'user1', socketId: 'sock1', amount: 1000,
addressIndex: 7, accountIndex: 0, detected: false, confirmed: false
});
svc.rpcCall = jest.fn()
.mockResolvedValueOnce({ result: {
pool: [{ txid: 'e'.repeat(64), amount: 999, subaddr_index: { major: 0, minor: 7 } }],
in: [confirmedTransfer({ txid: 'f'.repeat(64), amount: 1, height: 200,
confirmations: 1, minor: 7 })]
} })
.mockResolvedValueOnce({ result: { transfers: [incomingOutput({
txHash: 'f'.repeat(64), pubkey: '2'.repeat(64), globalIndex: 200,
amount: 1, blockHeight: 200, minor: 7
})] } });
const status = await svc.checkPaymentStatus('wow1mixed');
expect(status.confirmed).toBe(true);
expect(status.in_mempool).toBe(true);
expect(status.complete).toBe(false);
expect(status.amount).toBe(1);
expect(status.pendingAmount).toBe(999);
expect(status.observedAmount).toBe(1000);
expect(status.receipts).toHaveLength(1);
expect(status.receipts[0].txHash).toBe('f'.repeat(64));
});
test('returns every confirmed top-up receipt in deterministic order', async () => {
svc.addressToUser.set('wow1topups', {
userId: 'user1', socketId: 'sock1', amount: 1000,
addressIndex: 8, accountIndex: 0, detected: false, confirmed: false
});
svc.rpcCall = jest.fn()
.mockResolvedValueOnce({ result: { pool: [], in: [
confirmedTransfer({ txid: 'b'.repeat(64), amount: 400, height: 301,
confirmations: 3, minor: 8 }),
confirmedTransfer({ txid: 'a'.repeat(64), amount: 600, height: 302,
confirmations: 2, minor: 8 })
] } })
.mockResolvedValueOnce({ result: { transfers: [
incomingOutput({ txHash: 'b'.repeat(64), pubkey: '3'.repeat(64), globalIndex: 301,
amount: 400, blockHeight: 301, minor: 8 }),
incomingOutput({ txHash: 'a'.repeat(64), pubkey: '4'.repeat(64), globalIndex: 302,
amount: 600, blockHeight: 302, minor: 8 })
] } });
const status = await svc.checkPaymentStatus('wow1topups');
expect(status.complete).toBe(true);
expect(status.amount).toBe(1000);
expect(status.receipts.map(receipt => receipt.txHash)).toEqual([
'a'.repeat(64),
'b'.repeat(64)
]);
expect(status.confirmations).toBe(2);
});
test('counts distinct same-transaction outputs to one subaddress and ignores output replay', async () => {
const txid = '9'.repeat(64);
const firstPubkey = '5'.repeat(64);
const secondPubkey = '6'.repeat(64);
svc.addressToUser.set('wow1sameoutputscope', {
userId: 'user1', socketId: 'sock1', amount: 100,
addressIndex: 13, accountIndex: 0, detected: false, confirmed: false
});
svc.rpcCall = jest.fn()
.mockResolvedValueOnce({ result: { pool: [], in: [confirmedTransfer({
txid, amount: 100, height: 400, confirmations: 4, minor: 13
})] } })
.mockResolvedValueOnce({ result: { transfers: [
incomingOutput({ txHash: txid, pubkey: firstPubkey, globalIndex: 401,
amount: 40, blockHeight: 400, minor: 13 }),
incomingOutput({ txHash: txid, pubkey: secondPubkey, globalIndex: 402,
amount: 60, blockHeight: 400, minor: 13 }),
incomingOutput({ txHash: txid, pubkey: firstPubkey, globalIndex: 401,
amount: 40, blockHeight: 400, minor: 13 })
] } });
const status = await svc.checkPaymentStatus('wow1sameoutputscope');
expect(status.complete).toBe(true);
expect(status.amount).toBe(100);
expect(status.receipts).toHaveLength(2);
expect(status.receipts.map(receipt => receipt.outputId)).toEqual([firstPubkey, secondPubkey]);
});
test('fails closed when a confirmed transfer has no stable output identifier', async () => {
svc.addressToUser.set('wow1missingoutputid', {
userId: 'user1', socketId: 'sock1', amount: 100,
addressIndex: 14, accountIndex: 0, detected: false, confirmed: false
});
svc.rpcCall = jest.fn()
.mockResolvedValueOnce({ result: { pool: [], in: [confirmedTransfer({
txid: '8'.repeat(64), amount: 100, height: 500, confirmations: 1, minor: 14
})] } })
.mockResolvedValueOnce({ result: { transfers: [incomingOutput({
txHash: '8'.repeat(64), amount: 100, blockHeight: 500, minor: 14
})] } });
const status = await svc.checkPaymentStatus('wow1missingoutputid');
expect(status.complete).toBe(false);
expect(status.confirmed).toBe(false);
expect(status.amount).toBe(0);
expect(status.receipts).toEqual([]);
});
test('ignores double-spend-marked and explicitly timelocked incoming transfers', async () => {
svc.addressToUser.set('wow1unsafeoutputs', {
userId: 'user1', socketId: 'sock1', amount: 100,
addressIndex: 15, accountIndex: 0, detected: false, confirmed: false
});
svc.rpcCall = jest.fn()
.mockResolvedValueOnce({ result: { pool: [], in: [
confirmedTransfer({ txid: '7'.repeat(64), amount: 100, height: 600,
confirmations: 2, minor: 15, doubleSpend: true }),
confirmedTransfer({ txid: '6'.repeat(64), amount: 100, height: 601,
confirmations: 2, minor: 15, unlockTime: 42 }),
confirmedTransfer({ txid: '5'.repeat(64), amount: 100, height: 602,
confirmations: 2, minor: 15, unlockTime: Number.MAX_SAFE_INTEGER + 1 })
] } })
.mockResolvedValueOnce({ result: { transfers: [
incomingOutput({ txHash: '7'.repeat(64), pubkey: 'a'.repeat(64), globalIndex: 600,
amount: 100, blockHeight: 600, minor: 15 }),
incomingOutput({ txHash: '6'.repeat(64), pubkey: 'b'.repeat(64), globalIndex: 601,
amount: 100, blockHeight: 601, minor: 15 }),
incomingOutput({ txHash: '5'.repeat(64), pubkey: 'c'.repeat(64), globalIndex: 602,
amount: 100, blockHeight: 602, minor: 15 })
] } });
const status = await svc.checkPaymentStatus('wow1unsafeoutputs');
expect(status.complete).toBe(false);
expect(status.confirmed).toBe(false);
expect(status.amount).toBe(0);
expect(status.receipts).toEqual([]);
});
test('requires the configured minimum confirmations and rejects frozen output evidence', async () => {
svc = new WalletRPCService(debugManagerMock, { minConfirmations: 2 });
svc.addressToUser.set('wow1depth', {
userId: 'user1', socketId: 'sock1', amount: 100,
addressIndex: 16, accountIndex: 0, detected: false, confirmed: false
});
svc.rpcCall = jest.fn()
.mockResolvedValueOnce({ result: { pool: [], in: [
confirmedTransfer({ txid: '4'.repeat(64), amount: 50, height: 700,
confirmations: 1, minor: 16 }),
confirmedTransfer({ txid: '3'.repeat(64), amount: 50, height: 699,
confirmations: 2, minor: 16 })
] } })
.mockResolvedValueOnce({ result: { transfers: [
incomingOutput({ txHash: '4'.repeat(64), pubkey: 'd'.repeat(64), globalIndex: 700,
amount: 50, blockHeight: 700, minor: 16 }),
incomingOutput({ txHash: '3'.repeat(64), pubkey: 'e'.repeat(64), globalIndex: 699,
amount: 50, blockHeight: 699, minor: 16, frozen: true })
] } });
const status = await svc.checkPaymentStatus('wow1depth');
expect(status).toEqual(expect.objectContaining({
complete: false, confirmed: false, amount: 0, confirmations: 0
}));
expect(status.receipts).toEqual([]);
});
});