@facto-to/card 0.0.0-stage → 0.0.1-beta.2
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/CHANGELOG.md +20 -0
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +63 -2
- package/dist/card-inbox-DzsHFRPT.d.cts +312 -0
- package/dist/card-inbox-DzsHFRPT.d.ts +312 -0
- package/dist/chunk-KBS2PWM4.js +2 -0
- package/dist/cli/index.js +6 -0
- package/dist/docs/README.md +9 -0
- package/dist/docs/authentication.md +96 -0
- package/dist/docs/authorization-binding.md +182 -0
- package/dist/docs/authorizations.md +182 -0
- package/dist/docs/concepts.md +36 -0
- package/dist/docs/deliveries.md +170 -0
- package/dist/docs/errors.md +100 -0
- package/dist/docs/getting-access.md +56 -0
- package/dist/docs/idempotency.md +78 -0
- package/dist/docs/integration-checklist.md +15 -0
- package/dist/docs/integration.zh.md +59 -0
- package/dist/docs/interlace-adapter-v1.md +87 -0
- package/dist/docs/interlace-adapter-v1.zh.md +87 -0
- package/dist/docs/not-yet-available.md +13 -0
- package/dist/docs/quickstart.md +126 -0
- package/dist/docs/reconciliation.md +86 -0
- package/dist/docs/reference/api.md +2541 -0
- package/dist/docs/reference/events.md +639 -0
- package/dist/docs/reference/schemas.json +6603 -0
- package/dist/docs/reference/schemas.md +1001 -0
- package/dist/docs/simulator.md +235 -0
- package/dist/docs/versioning.md +9 -0
- package/dist/index.cjs +3 -0
- package/dist/index.d.cts +987 -0
- package/dist/index.d.ts +987 -0
- package/dist/index.js +2 -0
- package/dist/source/build.mjs +5 -0
- package/dist/source/package.json +16 -0
- package/dist/source/src/connect/cli/deliveries.ts +48 -0
- package/dist/source/src/connect/cli/index.ts +57 -0
- package/dist/source/src/connect/server/card-binding-assertion.ts +312 -0
- package/dist/source/src/connect/server/card-client.ts +1018 -0
- package/dist/source/src/connect/server/card-delivery.ts +301 -0
- package/dist/source/src/connect/server/card-events.ts +473 -0
- package/dist/source/src/connect/server/card-idempotency.ts +27 -0
- package/dist/source/src/connect/server/card-inbox.ts +82 -0
- package/dist/source/src/connect/server/card-intent-token.ts +588 -0
- package/dist/source/src/connect/server/card-polling.ts +69 -0
- package/dist/source/src/connect/server/card-signing.ts +115 -0
- package/dist/source/src/connect/server/card-transport.ts +282 -0
- package/dist/source/src/connect/server/errors.ts +280 -0
- package/dist/source/src/connect/server/fixtures/engine/card-events.synthetic.ts +84 -0
- package/dist/source/src/connect/server/retry-after.ts +12 -0
- package/dist/source/src/connect/testing/card-inbox.ts +124 -0
- package/dist/source/src/connect/testing/card-scenarios.ts +243 -0
- package/dist/source/src/connect/testing/card-simulator-vectors.ts +262 -0
- package/dist/source/src/connect/testing/card-simulator.ts +531 -0
- package/dist/source/src/connect/testing/interlace-card-transactions.ts +704 -0
- package/dist/source/src/connect/testing/pure-sha256.ts +123 -0
- package/dist/source/src/connect/wire.d.ts +544 -0
- package/dist/source/src/index.ts +163 -0
- package/dist/source/src/testing-browser.ts +24 -0
- package/dist/source/src/testing.ts +26 -0
- package/dist/source/tsconfig.json +20 -0
- package/dist/testing-browser.d.ts +220 -0
- package/dist/testing-browser.js +5 -0
- package/dist/testing.cjs +6 -0
- package/dist/testing.d.cts +199 -0
- package/dist/testing.d.ts +199 -0
- package/dist/testing.js +5 -0
- package/package.json +60 -4
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Synthetic Envelope v2 payloads for local SDK and partner testing.
|
|
2
|
+
const base = {
|
|
3
|
+
envelope_version: 2 as const,
|
|
4
|
+
event_id: '00000000-0000-4000-8000-000000000001',
|
|
5
|
+
environment: 'dev' as const,
|
|
6
|
+
program_id: 'cprog_SYNTHETIC00000001',
|
|
7
|
+
origin_connection_id: 'pconn_SYNTHETIC00000001',
|
|
8
|
+
connection_sequence: 1,
|
|
9
|
+
occurred_at: '2026-11-18T00:00:00Z',
|
|
10
|
+
}
|
|
11
|
+
const bindingBase = {
|
|
12
|
+
...base,
|
|
13
|
+
binding_id: 'cb_01JZ9X8K4M2V7R5T3N6P8Q0W2Z',
|
|
14
|
+
binding_revision: 1,
|
|
15
|
+
}
|
|
16
|
+
const cardDisplay = { network: 'Visa', type: 'virtual' as const, last4: '4242' }
|
|
17
|
+
const nullCardDisplay = { network: null, type: null, last4: null }
|
|
18
|
+
const linkedData = {
|
|
19
|
+
binding_id: bindingBase.binding_id,
|
|
20
|
+
intent_id: null,
|
|
21
|
+
external_card_id: 'scp_card_0001',
|
|
22
|
+
subject_link_id: 'csl_synthetic',
|
|
23
|
+
card: cardDisplay,
|
|
24
|
+
approved_scopes: ['card.metadata.view'],
|
|
25
|
+
consent_revision: 'synthetic',
|
|
26
|
+
}
|
|
27
|
+
const authorizationCorrelation = {
|
|
28
|
+
operation_type: 'authorization_requested' as const,
|
|
29
|
+
resource_version: 'v1',
|
|
30
|
+
external_authorization_id: 'synthetic-auth',
|
|
31
|
+
mapping_revision: 'synthetic-mapping',
|
|
32
|
+
}
|
|
33
|
+
const facts = [
|
|
34
|
+
{ kind: 'facto_admission', state: 'confirmed', recorded_at: base.occurred_at, source_system: 'facto', observed_at: base.occurred_at },
|
|
35
|
+
{ kind: 'durable_commit', state: 'confirmed', recorded_at: base.occurred_at, source_system: 'facto', observed_at: base.occurred_at },
|
|
36
|
+
{ kind: 'provider_authorization', state: 'unobserved', recorded_at: base.occurred_at, source_system: 'provider' },
|
|
37
|
+
{ kind: 'chain_finality', state: 'confirmed', recorded_at: base.occurred_at, source_system: 'chain', observed_at: base.occurred_at },
|
|
38
|
+
{ kind: 'provider_settlement', state: 'unobserved', recorded_at: base.occurred_at, source_system: 'provider' },
|
|
39
|
+
]
|
|
40
|
+
const updatedFacts = facts.map((fact) =>
|
|
41
|
+
fact.kind === 'provider_authorization' ? { ...fact, state: 'confirmed', observed_at: base.occurred_at } : fact,
|
|
42
|
+
)
|
|
43
|
+
const finalReceipt = {
|
|
44
|
+
receipt_event_id: base.event_id,
|
|
45
|
+
charge_id: '0x' + '0'.repeat(62) + '01',
|
|
46
|
+
external_authorization_id: authorizationCorrelation.external_authorization_id,
|
|
47
|
+
mapping_revision: authorizationCorrelation.mapping_revision,
|
|
48
|
+
operation_type: authorizationCorrelation.operation_type,
|
|
49
|
+
resource_version: authorizationCorrelation.resource_version,
|
|
50
|
+
execution_outcome: 'succeeded',
|
|
51
|
+
terminal_action: 'releaseFor',
|
|
52
|
+
finalized_at: base.occurred_at,
|
|
53
|
+
facts_at_finality: facts,
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export const cardEventFixtures = [
|
|
57
|
+
{ ...bindingBase, event_kind: 'card.binding.linked', data: linkedData },
|
|
58
|
+
{ ...bindingBase, event_kind: 'card.binding.linked', connection_sequence: null, data: { ...linkedData, intent_id: 'cint_01JZ9X8K4M2V7R5T3N6P8Q0W2Y', card: nullCardDisplay } },
|
|
59
|
+
{ ...bindingBase, event_kind: 'card.binding.relinked', data: linkedData },
|
|
60
|
+
{ ...bindingBase, event_kind: 'card.binding.relinked', connection_sequence: null, data: { ...linkedData, intent_id: 'cint_01JZ9X8K4M2V7R5T3N6P8Q0W2Y', card: nullCardDisplay } },
|
|
61
|
+
{ ...bindingBase, event_kind: 'card.binding.revoked', data: { binding_id: bindingBase.binding_id, binding_revision: 1, external_card_id: 'scp_card_0001', card: cardDisplay, revocation_source: 'user_portfolio', reason_code: 'user_requested', payable_projections_closed: 0, routes_detached: 0 } },
|
|
62
|
+
{ ...bindingBase, event_kind: 'card.binding.revoked', connection_sequence: null, data: { binding_id: bindingBase.binding_id, binding_revision: 1, external_card_id: 'scp_card_0001', card: nullCardDisplay, revocation_source: 'provider_lifecycle_event', payable_projections_closed: 0, routes_detached: 0 } },
|
|
63
|
+
{ ...bindingBase, event_kind: 'card.binding.route_changed', data: { binding_id: bindingBase.binding_id, binding_revision: 1, association_status: 'associated' } },
|
|
64
|
+
{ ...bindingBase, event_kind: 'card.binding.route_changed', connection_sequence: null, data: { binding_id: bindingBase.binding_id, binding_revision: 1, association_status: 'unassociated' } },
|
|
65
|
+
{ ...bindingBase, event_kind: 'card.binding.limits_changed', data: { binding_id: bindingBase.binding_id, binding_revision: 1, per_tx_limit: '1.000000', period_limit: '2.000000', period: 'monthly', expires_at: '2027-01-01T00:00:00Z' } },
|
|
66
|
+
{ ...bindingBase, event_kind: 'card.binding.limits_changed', connection_sequence: null, data: { binding_id: bindingBase.binding_id, binding_revision: 1, per_tx_limit: '1.000000', period_limit: '2.000000', period: 'monthly', expires_at: null } },
|
|
67
|
+
{ ...bindingBase, event_kind: 'card.binding.ownership_transferred', data: { previous_binding_id: bindingBase.binding_id, new_binding_id: 'cb_new0000000000001', external_card_id: 'scp_card_0001', card: cardDisplay, transferred_at: base.occurred_at } },
|
|
68
|
+
{ ...bindingBase, event_kind: 'card.binding.ownership_transferred', connection_sequence: null, data: { previous_binding_id: bindingBase.binding_id, new_binding_id: 'cb_new0000000000001', external_card_id: 'scp_card_0001', card: nullCardDisplay, transferred_at: base.occurred_at } },
|
|
69
|
+
{ ...base, event_kind: 'card.charge.finalized', data: { authorization_revision: 1, ...authorizationCorrelation, final_receipt: finalReceipt } },
|
|
70
|
+
{ ...base, event_id: '00000000-0000-4000-8000-000000000002', event_kind: 'card.charge.facts_updated', connection_sequence: 2, data: { authorization_revision: 2, ...authorizationCorrelation, charge_id: finalReceipt.charge_id, final_receipt_event_id: finalReceipt.receipt_event_id, previous_authorization_revision: 1, facts: updatedFacts } },
|
|
71
|
+
{
|
|
72
|
+
...base,
|
|
73
|
+
event_kind: 'card.authorization.updated',
|
|
74
|
+
data: {
|
|
75
|
+
lifecycle_revision: 1,
|
|
76
|
+
...authorizationCorrelation,
|
|
77
|
+
authorization: { amount: '1.000000', currency: 'USDC' },
|
|
78
|
+
charge: null,
|
|
79
|
+
issuer: { state: 'authorized', cleared_amount: '0.000000', reversed_amount: '0.000000', refunded_amount: '0.000000', entry_count: 0, unlinked_count: 0 },
|
|
80
|
+
refunds: { case_count: 0, status_counts: { awaiting_charge_final: 0, awaiting_funds: 0, payable: 0, paid: 0, failed: 0, not_payable: 0 }, total_amount: '0.000000', total_paid_amount: '0.000000', total_excess_amount: '0.000000' },
|
|
81
|
+
change: { type: 'authorization.recorded', at: base.occurred_at },
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
] as const
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse a `Retry-After` header in its delta-seconds form: decimal digits only.
|
|
3
|
+
* Anything else (signs, decimals, exponents, hexadecimal, inner or surrounding
|
|
4
|
+
* whitespace, empty values, words, and the HTTP-date form, which this SDK has
|
|
5
|
+
* never honored) is malformed and yields `undefined`, so callers fall back to
|
|
6
|
+
* their bounded backoff instead of retrying immediately.
|
|
7
|
+
*/
|
|
8
|
+
export function parseRetryAfterSeconds(response: Response): number | undefined {
|
|
9
|
+
const raw = response.headers.get('retry-after')
|
|
10
|
+
if (raw === null || !/^[0-9]+$/.test(raw)) return undefined
|
|
11
|
+
return Number(raw)
|
|
12
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { DeliveryInboxStore, DeliveryInboxTransaction } from '../server/card-inbox'
|
|
2
|
+
import type { DeliveryScope, VerifyEventDeliveryInput } from '../server/card-delivery'
|
|
3
|
+
import { cardEventFixtures } from '../server/fixtures/engine/card-events.synthetic'
|
|
4
|
+
import { hmacSha256Hex, sha256Hex } from './pure-sha256'
|
|
5
|
+
|
|
6
|
+
/** Single-process reference only; never use as durable or cross-process storage. */
|
|
7
|
+
export class MemoryDeliveryInboxStore implements DeliveryInboxStore {
|
|
8
|
+
private readonly states = new Map<string, { observedMax: number | null; digests: Map<string, string>; fence: number; lease?: { fence: number; expiresAt: number } }>()
|
|
9
|
+
private readonly capacity: number
|
|
10
|
+
private readonly leaseMs: number
|
|
11
|
+
private readonly now: () => number
|
|
12
|
+
constructor(options: { capacity?: number; leaseMs?: number; nowMs?: () => number } = {}) {
|
|
13
|
+
this.capacity = options.capacity ?? 1000
|
|
14
|
+
this.leaseMs = options.leaseMs ?? 30_000
|
|
15
|
+
this.now = options.nowMs ?? (() => performance.now())
|
|
16
|
+
if (!Number.isSafeInteger(this.capacity) || this.capacity < 1) throw new RangeError('invalid_capacity')
|
|
17
|
+
if (!Number.isSafeInteger(this.leaseMs) || this.leaseMs < 1) throw new RangeError('invalid_lease')
|
|
18
|
+
}
|
|
19
|
+
async withConnection<T>(scope: Readonly<DeliveryScope>, operation: (transaction: DeliveryInboxTransaction) => Promise<T>): Promise<T> {
|
|
20
|
+
const key = JSON.stringify([scope.connectionId, scope.environment, scope.programId])
|
|
21
|
+
let state = this.states.get(key)
|
|
22
|
+
if (!state) {
|
|
23
|
+
if (this.states.size >= this.capacity) throw new Error('inbox_capacity')
|
|
24
|
+
state = { observedMax: null, digests: new Map(), fence: 0 }
|
|
25
|
+
this.states.set(key, state)
|
|
26
|
+
}
|
|
27
|
+
const now = this.now()
|
|
28
|
+
if (!Number.isFinite(now)) throw new Error('invalid_clock')
|
|
29
|
+
if (state.lease && state.lease.expiresAt > now) throw new Error('inbox_busy')
|
|
30
|
+
if (!Number.isSafeInteger(state.fence + 1) || !Number.isFinite(now + this.leaseMs)) throw new Error('inbox_fence_exhausted')
|
|
31
|
+
const fence = ++state.fence
|
|
32
|
+
state.lease = { fence, expiresAt: now + this.leaseMs }
|
|
33
|
+
const current = state
|
|
34
|
+
const assertLease = () => {
|
|
35
|
+
const time = this.now()
|
|
36
|
+
if (!Number.isFinite(time) || current.lease?.fence !== fence || time >= current.lease.expiresAt) throw new Error('inbox_lease_expired')
|
|
37
|
+
}
|
|
38
|
+
let observedMax = state.observedMax
|
|
39
|
+
const digests = new Map(state.digests)
|
|
40
|
+
try {
|
|
41
|
+
const tx: DeliveryInboxTransaction = {
|
|
42
|
+
context: undefined, fence,
|
|
43
|
+
async getDigest(id) { assertLease(); return digests.get(id) },
|
|
44
|
+
async getObservedMaxSequence() { assertLease(); return observedMax },
|
|
45
|
+
markProcessed: async (id, digest, sequence) => {
|
|
46
|
+
assertLease()
|
|
47
|
+
if (!digests.has(id) && digests.size >= this.capacity) throw new Error('inbox_capacity')
|
|
48
|
+
digests.set(id, digest)
|
|
49
|
+
if (sequence !== null) observedMax = Math.max(observedMax ?? 0, sequence)
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
const result = await operation(tx)
|
|
53
|
+
assertLease()
|
|
54
|
+
current.observedMax = observedMax
|
|
55
|
+
current.digests = digests
|
|
56
|
+
return result
|
|
57
|
+
} finally {
|
|
58
|
+
if (current.lease?.fence === fence) delete current.lease
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const scope = {
|
|
64
|
+
connectionId: 'pconn_SYNTHETIC00000001',
|
|
65
|
+
environment: 'dev',
|
|
66
|
+
programId: 'cprog_SYNTHETIC00000001',
|
|
67
|
+
targetRevision: '00000000-0000-4000-8000-000000000001',
|
|
68
|
+
} as const
|
|
69
|
+
const kind = 'card.binding.linked'
|
|
70
|
+
const secret = new TextEncoder().encode('synthetic-delivery-test-key')
|
|
71
|
+
const key = {
|
|
72
|
+
...scope,
|
|
73
|
+
keyId: 'synthetic-key',
|
|
74
|
+
signerKeyVersion: 1,
|
|
75
|
+
notBefore: '2026-01-01T00:00:00Z',
|
|
76
|
+
expiresAt: '2027-01-01T00:00:00Z',
|
|
77
|
+
status: 'active' as const,
|
|
78
|
+
eventKinds: [kind],
|
|
79
|
+
secret,
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function vector(sequence: number, unsequenced = false, nullCard = false): VerifyEventDeliveryInput {
|
|
83
|
+
const eventId = `00000000-0000-4000-8000-${String(sequence).padStart(12, '0')}`
|
|
84
|
+
const body = JSON.stringify({
|
|
85
|
+
...cardEventFixtures[unsequenced || nullCard ? 1 : 0],
|
|
86
|
+
event_id: eventId,
|
|
87
|
+
binding_revision: sequence,
|
|
88
|
+
connection_sequence: unsequenced ? null : sequence,
|
|
89
|
+
})
|
|
90
|
+
const rawBody = new TextEncoder().encode(body)
|
|
91
|
+
const timestamp = '1792713600'
|
|
92
|
+
const digest = sha256Hex(rawBody)
|
|
93
|
+
const signature = hmacSha256Hex(
|
|
94
|
+
secret,
|
|
95
|
+
['facto:card:outbound-delivery-signature', 'v1', eventId, kind, timestamp, digest].join('\n'),
|
|
96
|
+
)
|
|
97
|
+
return {
|
|
98
|
+
rawBody,
|
|
99
|
+
headers: [
|
|
100
|
+
['facto-signature', `v1=${signature}`],
|
|
101
|
+
['facto-signature-key-id', key.keyId],
|
|
102
|
+
['facto-event-id', eventId],
|
|
103
|
+
['facto-event-kind', kind],
|
|
104
|
+
['facto-event-timestamp-seconds', timestamp],
|
|
105
|
+
['facto-delivery-attempt', '1'],
|
|
106
|
+
],
|
|
107
|
+
registry: [key],
|
|
108
|
+
expectedScope: scope,
|
|
109
|
+
nowSeconds: Number(timestamp),
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const one = vector(1)
|
|
114
|
+
const two = vector(2)
|
|
115
|
+
const three = vector(3)
|
|
116
|
+
|
|
117
|
+
/** Signed synthetic Envelope v2 arrivals. */
|
|
118
|
+
export const deliveryInboxVectors = {
|
|
119
|
+
duplicate: { deliveries: [one, one] },
|
|
120
|
+
outOfOrder: { deliveries: [one, three, two] },
|
|
121
|
+
gap: { deliveries: [one, three] },
|
|
122
|
+
nullableCard: { deliveries: [vector(1, false, true)] },
|
|
123
|
+
unsequenced: { deliveries: [vector(4, true), one] },
|
|
124
|
+
} as const
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import type { DeliverySigningKey, VerifyEventDeliveryInput } from '../server/card-delivery'
|
|
2
|
+
import { deliveryInboxVectors } from './card-inbox'
|
|
3
|
+
import { cardEventFixtures } from '../server/fixtures/engine/card-events.synthetic'
|
|
4
|
+
import { hmacSha256Hex, sha256Hex } from './pure-sha256'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Synthetic activation-approval snapshot for Envelope v2 binding and charge events.
|
|
8
|
+
* Test data only: never a runtime default, activation decision, or permission grant.
|
|
9
|
+
*/
|
|
10
|
+
export const requiredEventCatalogFixture = Object.freeze({
|
|
11
|
+
revision: '2026-11-18-card-events.1',
|
|
12
|
+
digest: 'sha256:9fcad68499ef3f770533ad3f3a4c4950d16c0385c56b2b0af6bba5375038aa98',
|
|
13
|
+
approvedFeatureSnapshot: Object.freeze(['binding', 'new_consumption', 'supplier_fact_updates'] as const),
|
|
14
|
+
featureClosures: Object.freeze({
|
|
15
|
+
binding: Object.freeze([
|
|
16
|
+
'card.binding.linked',
|
|
17
|
+
'card.binding.relinked',
|
|
18
|
+
'card.binding.revoked',
|
|
19
|
+
'card.binding.route_changed',
|
|
20
|
+
'card.binding.limits_changed',
|
|
21
|
+
'card.binding.ownership_transferred',
|
|
22
|
+
] as const),
|
|
23
|
+
new_consumption: Object.freeze(['card.charge.finalized'] as const),
|
|
24
|
+
supplier_fact_updates: Object.freeze(['card.charge.facts_updated'] as const),
|
|
25
|
+
}),
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
/** Synthetic declaration of the full Envelope v2 catalog including authorization updates. */
|
|
29
|
+
export const authorizationEventCatalogFixture = Object.freeze({
|
|
30
|
+
revision: '2026-11-18-card-events.1',
|
|
31
|
+
digest: 'sha256:9fcad68499ef3f770533ad3f3a4c4950d16c0385c56b2b0af6bba5375038aa98',
|
|
32
|
+
approvedFeatureSnapshot: Object.freeze([...requiredEventCatalogFixture.approvedFeatureSnapshot, 'authorization_updates'] as const),
|
|
33
|
+
featureClosures: Object.freeze({
|
|
34
|
+
...requiredEventCatalogFixture.featureClosures,
|
|
35
|
+
authorization_updates: Object.freeze(['card.authorization.updated'] as const),
|
|
36
|
+
}),
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
/** Public synthetic values for local tests only; never use as production credentials. */
|
|
40
|
+
export const simulatorDeliverySecret = 'synthetic-delivery-test-key'
|
|
41
|
+
export const simulatorDeliveryKinds = [
|
|
42
|
+
'duplicate',
|
|
43
|
+
'outOfOrder',
|
|
44
|
+
'gap',
|
|
45
|
+
'nullableCard',
|
|
46
|
+
'unsequenced',
|
|
47
|
+
'authorizationOutOfOrder',
|
|
48
|
+
'authorizationRedelivery',
|
|
49
|
+
'refundAwaitingChargeFinal',
|
|
50
|
+
'refundAwaitingFunds',
|
|
51
|
+
'refundPayable',
|
|
52
|
+
'refundPaid',
|
|
53
|
+
'refundFailed',
|
|
54
|
+
'refundNotPayable',
|
|
55
|
+
'chargeFailed',
|
|
56
|
+
] as const
|
|
57
|
+
export type SimulatorDeliveryKind = (typeof simulatorDeliveryKinds)[number]
|
|
58
|
+
const eventKind = 'card.binding.linked'
|
|
59
|
+
const authorizationEventKind = 'card.authorization.updated'
|
|
60
|
+
|
|
61
|
+
export function simulatorDeliveryConfig(name: SimulatorDeliveryKind) {
|
|
62
|
+
const index = simulatorDeliveryKinds.indexOf(name) + 1
|
|
63
|
+
return {
|
|
64
|
+
name,
|
|
65
|
+
selectionId: `00000000-0000-4000-8000-${String(100 + index).padStart(12, '0')}`,
|
|
66
|
+
connectionId: `pconn_SYNTHETIC0000000${index}`,
|
|
67
|
+
programId: 'cprog_SYNTHETIC00000001',
|
|
68
|
+
environment: 'dev' as const,
|
|
69
|
+
targetRevision: '00000000-0000-4000-8000-000000000001',
|
|
70
|
+
keyId: `synthetic-delivery-${name}`,
|
|
71
|
+
secretUtf8: simulatorDeliverySecret,
|
|
72
|
+
signerKeyVersion: 1,
|
|
73
|
+
notBefore: '2020-01-01T00:00:00Z',
|
|
74
|
+
expiresAt: '2099-01-01T00:00:00Z',
|
|
75
|
+
status: 'active' as const,
|
|
76
|
+
eventKinds: [index > 5 ? authorizationEventKind : eventKind],
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Build independently scoped deliveries from the SDK's synthetic fixtures. */
|
|
81
|
+
export function simulatorDeliveryScenario(name: SimulatorDeliveryKind, nowSeconds = Math.floor(Date.now() / 1000), attempt = 1) {
|
|
82
|
+
const config = simulatorDeliveryConfig(name)
|
|
83
|
+
const scope = {
|
|
84
|
+
connectionId: config.connectionId,
|
|
85
|
+
programId: config.programId,
|
|
86
|
+
environment: config.environment,
|
|
87
|
+
targetRevision: config.targetRevision,
|
|
88
|
+
}
|
|
89
|
+
const key: DeliverySigningKey = {
|
|
90
|
+
...scope,
|
|
91
|
+
keyId: config.keyId,
|
|
92
|
+
secret: new TextEncoder().encode(config.secretUtf8),
|
|
93
|
+
signerKeyVersion: config.signerKeyVersion,
|
|
94
|
+
notBefore: config.notBefore,
|
|
95
|
+
expiresAt: config.expiresAt,
|
|
96
|
+
status: config.status,
|
|
97
|
+
eventKinds: config.eventKinds,
|
|
98
|
+
}
|
|
99
|
+
if (name.startsWith('authorization') || name.startsWith('refund') || name === 'chargeFailed') {
|
|
100
|
+
const template = cardEventFixtures.find((event) => event.event_kind === 'card.authorization.updated')!
|
|
101
|
+
const statuses: Partial<Record<SimulatorDeliveryKind, string>> = {
|
|
102
|
+
refundAwaitingChargeFinal: 'awaiting_charge_final',
|
|
103
|
+
refundAwaitingFunds: 'awaiting_funds',
|
|
104
|
+
refundPayable: 'payable',
|
|
105
|
+
refundPaid: 'paid',
|
|
106
|
+
refundFailed: 'failed',
|
|
107
|
+
refundNotPayable: 'not_payable',
|
|
108
|
+
}
|
|
109
|
+
const status = statuses[name]
|
|
110
|
+
const create = (revision: number, redelivery: boolean): VerifyEventDeliveryInput => {
|
|
111
|
+
const id = `00000000-0000-4000-8002-${String(simulatorDeliveryKinds.indexOf(name) * 10 + revision + (redelivery ? 1 : 0)).padStart(12, '0')}`
|
|
112
|
+
const refundCase = status ? {
|
|
113
|
+
case_id: 'rfc_00000000000000000000000000000001',
|
|
114
|
+
source_operation_type: 'refund',
|
|
115
|
+
status,
|
|
116
|
+
amount: '1.000000',
|
|
117
|
+
excess_amount: '0.000000',
|
|
118
|
+
updated_at: template.occurred_at,
|
|
119
|
+
...(status === 'paid'
|
|
120
|
+
? {
|
|
121
|
+
paid_amount: '1.000000',
|
|
122
|
+
refund_payment: {
|
|
123
|
+
chain_id: 8453,
|
|
124
|
+
tx_hash: `0x${'1'.repeat(64)}`,
|
|
125
|
+
paid_amount: '1.000000',
|
|
126
|
+
recorded_by: 'operator',
|
|
127
|
+
},
|
|
128
|
+
}
|
|
129
|
+
: {}),
|
|
130
|
+
} : undefined
|
|
131
|
+
const event = {
|
|
132
|
+
...template,
|
|
133
|
+
event_id: id,
|
|
134
|
+
program_id: config.programId,
|
|
135
|
+
origin_connection_id: config.connectionId,
|
|
136
|
+
connection_sequence: revision + (redelivery ? 1 : 0),
|
|
137
|
+
data: {
|
|
138
|
+
...template.data,
|
|
139
|
+
lifecycle_revision: revision,
|
|
140
|
+
...(status
|
|
141
|
+
? {
|
|
142
|
+
refunds: {
|
|
143
|
+
...template.data.refunds,
|
|
144
|
+
case_count: 1,
|
|
145
|
+
status_counts: { ...template.data.refunds.status_counts, [status]: 1 },
|
|
146
|
+
total_amount: '1.000000',
|
|
147
|
+
total_paid_amount: status === 'paid' ? '1.000000' : '0.000000',
|
|
148
|
+
},
|
|
149
|
+
charge: {
|
|
150
|
+
charge_id: `0x${'2'.repeat(64)}`,
|
|
151
|
+
status: status === 'awaiting_charge_final' ? 'processing' : 'succeeded',
|
|
152
|
+
amount: '1.000000',
|
|
153
|
+
fee_total: '0.000000',
|
|
154
|
+
total_debited: '1.000000',
|
|
155
|
+
currency: 'USDC',
|
|
156
|
+
updated_at: template.occurred_at,
|
|
157
|
+
},
|
|
158
|
+
}
|
|
159
|
+
: {}),
|
|
160
|
+
...(name === 'chargeFailed'
|
|
161
|
+
? {
|
|
162
|
+
charge: {
|
|
163
|
+
charge_id: `0x${'2'.repeat(64)}`,
|
|
164
|
+
status: 'failed',
|
|
165
|
+
amount: '1.000000',
|
|
166
|
+
fee_total: '0.000000',
|
|
167
|
+
total_debited: '0.000000',
|
|
168
|
+
currency: 'USDC',
|
|
169
|
+
failure_code: 'synthetic_failure',
|
|
170
|
+
updated_at: template.occurred_at,
|
|
171
|
+
},
|
|
172
|
+
}
|
|
173
|
+
: {}),
|
|
174
|
+
change: status ? { type: `refund_case.${status}`, at: template.occurred_at, refund_case: refundCase } : template.data.change,
|
|
175
|
+
},
|
|
176
|
+
}
|
|
177
|
+
const rawBody = new TextEncoder().encode(JSON.stringify(event))
|
|
178
|
+
const timestamp = String(nowSeconds)
|
|
179
|
+
const digest = sha256Hex(rawBody)
|
|
180
|
+
const signature = hmacSha256Hex(
|
|
181
|
+
key.secret,
|
|
182
|
+
['facto:card:outbound-delivery-signature', 'v1', id, authorizationEventKind, timestamp, digest].join('\n'),
|
|
183
|
+
)
|
|
184
|
+
const headers: (readonly [string, string])[] = [
|
|
185
|
+
['facto-signature', `v1=${signature}`],
|
|
186
|
+
['facto-signature-key-id', key.keyId],
|
|
187
|
+
['facto-event-id', id],
|
|
188
|
+
['facto-event-kind', authorizationEventKind],
|
|
189
|
+
['facto-event-timestamp-seconds', timestamp],
|
|
190
|
+
['facto-delivery-attempt', String(redelivery ? Math.max(attempt, 2) : attempt)],
|
|
191
|
+
]
|
|
192
|
+
if (redelivery) headers.push(['facto-redelivery-reason', 'retry'])
|
|
193
|
+
return {
|
|
194
|
+
rawBody,
|
|
195
|
+
headers,
|
|
196
|
+
registry: [key],
|
|
197
|
+
expectedScope: scope,
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return {
|
|
201
|
+
config,
|
|
202
|
+
deliveries: name === 'authorizationOutOfOrder'
|
|
203
|
+
? [create(2, false), create(1, false)]
|
|
204
|
+
: name === 'authorizationRedelivery'
|
|
205
|
+
? [create(1, false), create(1, true)]
|
|
206
|
+
: [create(1, false)],
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
const source = deliveryInboxVectors[name as keyof typeof deliveryInboxVectors]
|
|
210
|
+
const convert = (input: VerifyEventDeliveryInput, deliveryAttempt: number): VerifyEventDeliveryInput => {
|
|
211
|
+
const event = JSON.parse(new TextDecoder().decode(input.rawBody)) as {
|
|
212
|
+
event_id: string
|
|
213
|
+
origin_connection_id?: string
|
|
214
|
+
}
|
|
215
|
+
const original = Number(event.event_id.slice(-12))
|
|
216
|
+
event.event_id = `00000000-0000-4000-8001-${simulatorDeliveryKinds.indexOf(name) + 1}${String(original).padStart(11, '0')}`
|
|
217
|
+
event.origin_connection_id = config.connectionId
|
|
218
|
+
const rawBody = new TextEncoder().encode(JSON.stringify(event))
|
|
219
|
+
const timestamp = String(nowSeconds)
|
|
220
|
+
const digest = sha256Hex(rawBody)
|
|
221
|
+
const signature = hmacSha256Hex(
|
|
222
|
+
key.secret,
|
|
223
|
+
['facto:card:outbound-delivery-signature', 'v1', event.event_id, eventKind, timestamp, digest].join('\n'),
|
|
224
|
+
)
|
|
225
|
+
return {
|
|
226
|
+
rawBody,
|
|
227
|
+
headers: [
|
|
228
|
+
['facto-signature', `v1=${signature}`],
|
|
229
|
+
['facto-signature-key-id', key.keyId],
|
|
230
|
+
['facto-event-id', event.event_id],
|
|
231
|
+
['facto-event-kind', eventKind],
|
|
232
|
+
['facto-event-timestamp-seconds', timestamp],
|
|
233
|
+
['facto-delivery-attempt', String(deliveryAttempt)],
|
|
234
|
+
],
|
|
235
|
+
registry: [key],
|
|
236
|
+
expectedScope: scope,
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return {
|
|
240
|
+
config,
|
|
241
|
+
deliveries: source.deliveries.map((input, index) => convert(input, attempt + (name === 'duplicate' && index === 1 ? 1 : 0))),
|
|
242
|
+
}
|
|
243
|
+
}
|