Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/PaymentList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ const PaymentList: React.FC<PaymentListProps> = ({ payments = [], onUpdatePaymen
</Typography>
<SatoshiAmount amount={payment.token.amount} />
</Box>
{payment.token.amount < 2000 && <Typography variant='caption' color='text.secondary'>Too small to return after the fee. Accept it to keep the funds.</Typography>}
</Box>

<Box
Expand Down Expand Up @@ -190,7 +191,7 @@ const PaymentList: React.FC<PaymentListProps> = ({ 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' ? (
<>
Expand Down
10 changes: 6 additions & 4 deletions frontend/src/utils/paymentCompatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
}
}
Expand All @@ -40,12 +41,13 @@ export async function rejectIncomingPayment(payment: IncomingPayment): Promise<P
const amount = prepared.payment.token.amount
if (!Number.isSafeInteger(amount) || amount <= 0) throw new Error('The payment amount is invalid.')

// Tiny payments cannot cover the library's refund fee, so rejecting only
// acknowledges them. Larger payments must be accepted successfully before
// The SDK's tiny-payment rejection acknowledges without crediting or returning
// funds. Keep the message queued so the recipient can explicitly accept it.
// Larger payments must be accepted successfully before
// refunding; the upstream rejectPayment method does not currently enforce
// that result and could otherwise refund from unrelated wallet funds.
if (amount - 1_000 < 1_000) {
await peerPayClient.rejectPayment(prepared.payment)
throw new Error('This payment is too small to return after the fee. It has been kept; accept it to add it to your balance.')
} else {
const result = await peerPayClient.acceptPayment(prepared.payment)
assertPaymentAccepted(result)
Expand Down
39 changes: 39 additions & 0 deletions frontend/src/utils/paymentCompatibilitySafety.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { LockingScript, Transaction } from '@bsv/sdk'
const client = vi.hoisted(() => ({ 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()
})
})
Loading