diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 8783c1e..957d04b 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -31,7 +31,22 @@ jobs: cache: npm cache-dependency-path: frontend/package-lock.json - run: npm ci - - run: npm test + - run: npm test -- --reporter=json --outputFile=payment-test-results.json + - name: Require complete passing payment source coverage + run: | + node --input-type=module - <<'JS' + import assert from 'node:assert/strict' + import fs from 'node:fs' + const report = JSON.parse(fs.readFileSync('payment-test-results.json', 'utf8')) + assert.equal(report.success, true) + assert.equal(report.numFailedTests, 0) + assert.equal(report.numPendingTests, 0) + assert.equal(report.numTodoTests, 0) + assert(report.numPassedTests >= 28) + const safety = report.testResults.find(result => result.name.endsWith('/paymentCompatibilitySafety.test.ts')) + assert(safety && safety.assertionResults.length === 9) + assert(safety.assertionResults.every(result => result.status === 'passed')) + JS - run: npx tsc --noEmit - run: npm run build - name: Bound startup JavaScript @@ -44,6 +59,13 @@ jobs: } JS + - uses: actions/upload-artifact@v6 + if: always() + with: + name: payment-source-results + path: frontend/payment-test-results.json + if-no-files-found: warn + deploy: name: Verify and deploy if: github.event_name != 'pull_request' diff --git a/frontend/src/components/PaymentList.tsx b/frontend/src/components/PaymentList.tsx index 2d827c5..51ed09f 100644 --- a/frontend/src/components/PaymentList.tsx +++ b/frontend/src/components/PaymentList.tsx @@ -162,6 +162,7 @@ const PaymentList: React.FC = ({ payments = [], onUpdatePaymen + {payment.token.amount < 2000 && Too small to return after the fee. Accept it to keep the funds.} = ({ payments = [], onUpdatePaymen color='secondary' variant='outlined' size='small' - disabled={isBulkAccepting || !!processingMap[payment.messageId]} + disabled={isBulkAccepting || !!processingMap[payment.messageId] || !Number.isSafeInteger(payment.token.amount) || payment.token.amount < 2000} > {processingMap[payment.messageId] === 'reject' ? ( <> diff --git a/frontend/src/utils/paymentCompatibility.ts b/frontend/src/utils/paymentCompatibility.ts index c2a7daf..11f5ebe 100644 --- a/frontend/src/utils/paymentCompatibility.ts +++ b/frontend/src/utils/paymentCompatibility.ts @@ -23,7 +23,8 @@ export function prepareIncomingPayment(payment: IncomingPayment): { } function assertPaymentAccepted(result: unknown): void { - if (typeof result === 'string' || result == null || typeof result !== 'object') { + if (typeof result === 'string' || result == null || typeof result !== 'object' || + (result as { paymentResult?: { accepted?: unknown } }).paymentResult?.accepted !== true) { throw new Error(typeof result === 'string' ? result : 'The wallet did not confirm the payment.') } } @@ -40,12 +41,13 @@ export async function rejectIncomingPayment(payment: IncomingPayment): Promise

({ acceptPayment: vi.fn(), rejectPayment: vi.fn(), sendPayment: vi.fn() })) +vi.mock('./peerPayClient', () => ({ peerPayClient: client })) +import { acceptIncomingPayment, rejectIncomingPayment } from './paymentCompatibility' + +// Unfunded bytes are used only by the format parser. No wallet or service runs. +const transaction = new Transaction() +transaction.addOutput({ satoshis: 1000, lockingScript: LockingScript.fromHex('51') }) +const incoming = (amount = 1000) => ({ + messageId: 'lab-fixture', sender: '02' + '11'.repeat(32), + token: { amount, transaction: transaction.toAtomicBEEF(), customInstructions: { derivationPrefix: 'AQ==', derivationSuffix: 'Ag==' } } +}) +beforeEach(() => vi.resetAllMocks()) +describe('retaining funds when accepting or returning a payment is unsafe', () => { + it.each([1000, 1999])('keeps a %i-satoshi payment queued instead of acknowledging without a credit or refund', async amount => { + await expect(rejectIncomingPayment(incoming(amount))).rejects.toThrow('too small to return') + expect(client.rejectPayment).not.toHaveBeenCalled() + expect(client.acceptPayment).not.toHaveBeenCalled() + expect(client.sendPayment).not.toHaveBeenCalled() + }) + it.each([undefined, 'failed', {}, { paymentResult: {} }, { paymentResult: { accepted: false } }])('requires an affirmative wallet receipt before removing an incoming row: %j', async result => { + client.acceptPayment.mockResolvedValue(result) + await expect(acceptIncomingPayment(incoming())).rejects.toThrow() + }) + it('retains legacy BEEF compatibility when the wallet confirms acceptance', async () => { + client.acceptPayment.mockResolvedValue({ paymentResult: { accepted: true } }) + const payment = incoming() + payment.token.transaction = transaction.toBEEF() + await expect(acceptIncomingPayment(payment)).resolves.toBe('legacy-converted') + expect(Transaction.fromAtomicBEEF(client.acceptPayment.mock.calls[0][0].token.transaction).id('hex')).toBe(transaction.id('hex')) + }) + it('does not fund a return from unrelated balance after a negative acceptance receipt', async () => { + client.acceptPayment.mockResolvedValue({ paymentResult: { accepted: false } }) + await expect(rejectIncomingPayment(incoming(5000))).rejects.toThrow() + expect(client.sendPayment).not.toHaveBeenCalled() + expect(client.rejectPayment).not.toHaveBeenCalled() + }) +})