@oneaddress/partner-sdk 1.2.0 → 1.3.0

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/dist/index.d.ts CHANGED
@@ -1,5 +1,80 @@
1
- export { DEFAULT_REPLAY_WINDOW_SECS, isFreshTimestamp } from '@oneaddress/crypto';
2
- export { CURRENT_VERSION, SUPPORTED_VERSIONS } from '@oneaddress/protocol';
1
+ /**
2
+ * Webhook timestamp / replay-window helper.
3
+ *
4
+ * OneAddress webhook dispatches carry an `X-OneAddress-Timestamp` header
5
+ * (Unix seconds) which is included in the signed material so it cannot be
6
+ * tampered with after the fact. Receivers SHOULD also enforce a freshness
7
+ * window so that a captured payload (e.g. from a partner's logs that later
8
+ * leak) cannot be replayed indefinitely.
9
+ *
10
+ * The check is intentionally tiny and dependency-free so that:
11
+ * - The customer app's webhook receiver and the partner app's receiver
12
+ * can share the exact same logic.
13
+ * - The official @oneaddress/partner-sdk can re-export it for external
14
+ * partners with no additional dependencies.
15
+ *
16
+ * The default tolerance is 5 minutes. That is wide enough to absorb realistic
17
+ * clock drift between OneAddress dispatch and a partner endpoint, and narrow
18
+ * enough that a captured webhook is not useful after the fact. The choice
19
+ * matches industry norms for HMAC-signed request freshness — Stripe webhooks
20
+ * (docs.stripe.com/webhooks/signatures), AWS SigV4 (5-minute default), and
21
+ * GitHub App webhooks all use comparable windows.
22
+ */
23
+ /**
24
+ * The default ±tolerance window enforced when none is provided. 300 seconds
25
+ * (5 minutes) — same value publicly documented on the OneAddress security
26
+ * page and in the partner integration guide.
27
+ */
28
+ declare const DEFAULT_REPLAY_WINDOW_SECS = 300;
29
+ /**
30
+ * Returns true if `timestampHeader` represents a Unix-second value within
31
+ * `toleranceSecs` of the current time (i.e. abs(now - ts) <= toleranceSecs).
32
+ *
33
+ * - Returns `false` for non-numeric, empty, NaN, or non-finite inputs.
34
+ * - Returns `false` for negative values.
35
+ * - Compares against `nowSecs` if provided (used by tests for determinism);
36
+ * otherwise reads the current epoch.
37
+ */
38
+ declare function isFreshTimestamp(timestampHeader: string | null | undefined, toleranceSecs?: number, nowSecs?: number): boolean;
39
+
40
+ /** Fields that can be turned into a blind index. */
41
+ type BlindIndexField = 'email' | 'name' | 'account_number';
42
+ /**
43
+ * Canonicalise a value before hashing. BOTH sides MUST normalise identically or
44
+ * the indexes won't match, so these rules are part of the wire contract:
45
+ * - `email` → NFKC, trim, lowercase.
46
+ * - `name` → NFKC, trim, lowercase, internal whitespace collapsed to one space.
47
+ * - `account_number` → NFKC, trim, ALL whitespace removed, case PRESERVED
48
+ * (account numbers can be case-sensitive).
49
+ */
50
+ declare function normalizeForBlindIndex(field: BlindIndexField, value: string): string;
51
+ /**
52
+ * Compute the blind index for a single field:
53
+ * `base64url( HMAC-SHA256(secret, "oneaddress:bidx:<field>:<normalized>") )`.
54
+ *
55
+ * Returns the full 32-byte digest (43 base64url chars) — untruncated to keep
56
+ * collisions negligible.
57
+ *
58
+ * @param secret The shared per-partner blind-index secret.
59
+ * @param field Which field is being indexed (also domain-separates the hash).
60
+ * @param value The raw field value; normalised internally per {@link normalizeForBlindIndex}.
61
+ */
62
+ declare function computeBlindIndex(secret: string, field: BlindIndexField, value: string): Promise<string>;
63
+
64
+ /**
65
+ * @oneaddress/protocol
66
+ *
67
+ * Canonical wire-format types for the OneAddress webhook envelope.
68
+ * Version 2026.1 — frozen. Do not modify field names or remove fields.
69
+ *
70
+ * Versioning policy: versions are date-prefixed (YYYY.N).
71
+ * A new version is published only when a breaking change is required.
72
+ * Partners pin a version and trust it will not change under them.
73
+ */
74
+ /** The current protocol version emitted by the OneAddress platform. */
75
+ declare const CURRENT_VERSION: "2026.1";
76
+ /** All protocol versions this build of the SDK recognises. */
77
+ declare const SUPPORTED_VERSIONS: readonly ["2026.1"];
3
78
 
4
79
  /**
5
80
  * @oneaddress/partner-sdk
@@ -32,6 +107,36 @@ interface OneAddressAddress {
32
107
  interface OneAddressCustomer {
33
108
  email: string;
34
109
  name: string;
110
+ /** Account/policy/membership number the customer provided for this service.
111
+ * Null when the guest did not enter an account ref, or for account-flow
112
+ * dispatches that don't include this field. Use to match the customer in
113
+ * your records. */
114
+ account_number: string | null;
115
+ /** Additional names the customer is known by (maiden name, nickname, etc.).
116
+ * Always an array; empty when no aliases were provided. Partners should
117
+ * check `name` and each entry in `known_names` when matching their records. */
118
+ known_names: string[];
119
+ }
120
+ /**
121
+ * Blind-index match block — present INSTEAD of the cleartext `customer` block
122
+ * for partners who enabled blind-index matching in the portal. Each value is
123
+ * `base64url( HMAC-SHA256(yourBlindIndexSecret, normalize(field)) )`.
124
+ *
125
+ * Match a record by recomputing the same blind index over your own stored
126
+ * fields (with {@link computeBlindIndex} and your shared secret) and comparing
127
+ * for equality — no plaintext name/email/account crosses the wire and you do
128
+ * not need to decrypt the envelope to locate the record. The decrypted
129
+ * `SessionData` still carries the verified name + account number once you do
130
+ * decrypt.
131
+ */
132
+ interface OneAddressCustomerMatch {
133
+ /** Blind index of the consumer's email, when present. */
134
+ email?: string;
135
+ /** Blind index of this partner's account_number, when present. */
136
+ account_number?: string;
137
+ /** Blind indexes of the legal name plus every known-name alias. Match if ANY
138
+ * of your stored name indexes appears here. */
139
+ names: string[];
35
140
  }
36
141
  interface OneAddressLOA {
37
142
  reference: string;
@@ -48,23 +153,23 @@ interface OneAddressLOA {
48
153
  * live IDV credit (Global Data DVS + biometric, or the mock equivalent in
49
154
  * sandbox builds).
50
155
  *
51
- * Partners should persist `verification_ref` against their record of the
52
- * update — it is the immutable handle for the IDV that authorised this
53
- * dispatch. Quote it back to OneAddress support to trace an update back to
54
- * the underlying verification.
156
+ * This is a **cleartext** attestation — it carries no customer identity, only
157
+ * the fact and format of the verification. Under the zero-knowledge dispatch
158
+ * rules the immutable IDV handle (`verification_ref`) stays inside the
159
+ * encrypted `session_envelope`, exposed as {@link SessionData.id_verification_ref}
160
+ * after you decrypt it. Read the ref from the decrypted {@link SessionData}, not
161
+ * from this outer block — quote it back to OneAddress support to trace an
162
+ * update back to the underlying verification.
55
163
  *
56
164
  * Absent on test-mode dispatches that bypass the credit (BYPASS_STRIPE
57
165
  * dev flows). Production dispatches always include it.
58
166
  */
59
167
  interface OneAddressIdVerification {
60
- /** UUID — immutable handle for the IDV credit consumed by this dispatch */
61
- verification_ref: string;
62
- /** Verification method, e.g. `global_data_idpass` or `mock` (sandbox) */
168
+ /** Verification method — `identity_document` for a completed document +
169
+ * biometric check; `mock` in sandbox builds. Carries no provider identity. */
63
170
  method: string;
64
171
  /** ISO-8601 timestamp the verification was completed */
65
172
  verified_at: string;
66
- /** Provider-issued certificate id (Global Data api_reference); may be null */
67
- certificate_id: string | null;
68
173
  /** Always `true` on dispatches — partners can rely on the field's presence */
69
174
  verified: true;
70
175
  /** Format tag — currently `oneaddress/v1` */
@@ -95,9 +200,23 @@ interface OneAddressUpdateEvent {
95
200
  * Deduplicating on `(partner_id, dispatchId)` prevents double-processing.
96
201
  */
97
202
  dispatchId: string;
98
- customer: OneAddressCustomer;
203
+ /** Cleartext matching block. Present by default; ABSENT for partners who
204
+ * enabled blind-index matching (see `customer_match`). Exactly one of
205
+ * `customer` / `customer_match` is present on any given dispatch. */
206
+ customer?: OneAddressCustomer;
207
+ /** Blind-index matching block — present instead of `customer` for blind-index
208
+ * partners. See {@link OneAddressCustomerMatch}. */
209
+ customer_match?: OneAddressCustomerMatch;
99
210
  address: OneAddressAddress;
100
- loa?: OneAddressLOA;
211
+ /** The Letter of Authority. On D5 dispatches this is the structured
212
+ * {@link OneAddressD5LOA} recovered by decrypting `loa_encrypted`; on legacy
213
+ * dispatches it is the freeform {@link OneAddressLOA}. Verify with
214
+ * {@link verifyD5LOA} / {@link verifyLOA} respectively. */
215
+ loa?: OneAddressLOA | OneAddressD5LOA;
216
+ /** D5 only — `base64url(SHA-256(canonical LOA form))`, recomputed from the
217
+ * decrypted LOA. Echo this in your `/api/confirm` callback's `loa_ref` field
218
+ * so OneAddress can confirm you decrypted a valid signed consent. */
219
+ loa_ref?: string;
101
220
  /**
102
221
  * Identity-verification attestation for the customer behind this update.
103
222
  * Present on every production dispatch; absent on BYPASS_STRIPE test
@@ -117,9 +236,23 @@ interface OneAddressVerifyEvent {
117
236
  dispatchId: string;
118
237
  batch_id: number;
119
238
  user_id: string;
120
- customer: OneAddressCustomer;
239
+ /** Cleartext matching block. Present by default; ABSENT for partners who
240
+ * enabled blind-index matching (see `customer_match`). Exactly one of
241
+ * `customer` / `customer_match` is present on any given dispatch. */
242
+ customer?: OneAddressCustomer;
243
+ /** Blind-index matching block — present instead of `customer` for blind-index
244
+ * partners. See {@link OneAddressCustomerMatch}. */
245
+ customer_match?: OneAddressCustomerMatch;
121
246
  address: OneAddressAddress;
122
- loa?: OneAddressLOA;
247
+ /** The Letter of Authority. On D5 dispatches this is the structured
248
+ * {@link OneAddressD5LOA} recovered by decrypting `loa_encrypted`; on legacy
249
+ * dispatches it is the freeform {@link OneAddressLOA}. Verify with
250
+ * {@link verifyD5LOA} / {@link verifyLOA} respectively. */
251
+ loa?: OneAddressLOA | OneAddressD5LOA;
252
+ /** D5 only — `base64url(SHA-256(canonical LOA form))`, recomputed from the
253
+ * decrypted LOA. Echo this in your `/api/confirm` callback's `loa_ref` field
254
+ * so OneAddress can confirm you decrypted a valid signed consent. */
255
+ loa_ref?: string;
123
256
  /**
124
257
  * Identity-verification attestation for the customer behind this verify
125
258
  * request. Present on every production dispatch; absent on BYPASS_STRIPE
@@ -153,7 +286,13 @@ interface OneAddressAccountVerifyEvent {
153
286
  /** Unique dispatch ID from `X-OneAddress-Dispatch`. Falls back to `''` if header is absent — guard before using as idempotency key. */
154
287
  dispatchId: string;
155
288
  user_id: string;
156
- customer: OneAddressCustomer;
289
+ /** Cleartext matching block. Present by default; ABSENT for partners who
290
+ * enabled blind-index matching (see `customer_match`). Exactly one of
291
+ * `customer` / `customer_match` is present on any given dispatch. */
292
+ customer?: OneAddressCustomer;
293
+ /** Blind-index matching block — present instead of `customer` for blind-index
294
+ * partners. See {@link OneAddressCustomerMatch}. */
295
+ customer_match?: OneAddressCustomerMatch;
157
296
  raw: Record<string, unknown>;
158
297
  }
159
298
  interface EncryptedPayload {
@@ -188,6 +327,16 @@ interface OneAddressHandlerOptions {
188
327
  webhookSecretGraceUntil?: string | number;
189
328
  /** PKCS#8 PEM-encoded ECDH P-256 private key for address decryption */
190
329
  privateKeyPem: string;
330
+ /**
331
+ * OPTIONAL — OneAddress's ECDSA P-256 LOA signing public key (SPKI PEM), from
332
+ * {@link fetchLoaPublicKey}. When set, the handler decrypts `loa_encrypted`,
333
+ * verifies the signed D5 LOA, and REJECTS the dispatch (500) if the signature
334
+ * is invalid — you never process an update whose consent proof doesn't verify.
335
+ * When unset, the decrypted LOA is still passed to your handler as
336
+ * `event.loa` (and `event.loa_ref`) for you to verify yourself. Cache the key;
337
+ * it only changes on a planned rotation.
338
+ */
339
+ loaPublicKeyPem?: string;
191
340
  /**
192
341
  * Called when a consumer dispatches an address update to you.
193
342
  * Return an object with optional `status` and `reference` for the confirmation callback.
@@ -312,12 +461,23 @@ declare function decryptAddress(payload: EncryptedPayload, privateKeyPem: string
312
461
  interface SessionKeyShare {
313
462
  /** Stable identifier of the partner key OneAddress used to wrap SK_n. */
314
463
  key_id: string;
315
- /** Base64url ECDH-ES wrap of SK_n. */
464
+ /** Base64url wrap of SK_n. ECDH-ES: salt+iv+ct+tag. RSA-OAEP: the RSA ciphertext. MLKEM768X25519: salt+iv+ct+tag. */
316
465
  wrapped_key: string;
317
- /** Base64url SPKI of the ephemeral public key used in the wrap. */
318
- epk: string;
319
- /** Always `ECDH-ES+HKDF-SHA256+A256GCM`. */
320
- alg: 'ECDH-ES+HKDF-SHA256+A256GCM';
466
+ /**
467
+ * ECDH-ES: base64url SPKI of the ephemeral public key. MLKEM768X25519:
468
+ * base64url of the X-Wing KEM ciphertext (1120 B). Absent for
469
+ * `RSA-OAEP-256+A256GCM` (key transport has no ephemeral/KEM part).
470
+ */
471
+ epk?: string;
472
+ /**
473
+ * Wrap algorithm. `decryptSession` routes on this automatically — supply the
474
+ * private key matching `key_id`:
475
+ * - `ECDH-ES+HKDF-SHA256+A256GCM` (default) — ECDH P-256 private key.
476
+ * - `RSA-OAEP-256+A256GCM` — RSA private key (partners whose KMS/HSM is RSA).
477
+ * - `MLKEM768X25519+HKDF-SHA256+A256GCM` — post-quantum hybrid (X-Wing =
478
+ * ML-KEM-768 + X25519); supply your 32-byte X-Wing secret (base64).
479
+ */
480
+ alg: 'ECDH-ES+HKDF-SHA256+A256GCM' | 'RSA-OAEP-256+A256GCM' | 'MLKEM768X25519+HKDF-SHA256+A256GCM';
321
481
  /** Wrap-blob schema version. Always `2`. */
322
482
  v: 2;
323
483
  }
@@ -436,6 +596,53 @@ declare function decryptSession(share: SessionKeyShare, envelopeB64: string, pri
436
596
  * await db.storeEncryptedAddress(event.customer.email, kmsBlob);
437
597
  */
438
598
  declare function transformAddress(payload: EncryptedPayload, privateKeyPem: string, partnerId: string, recipientSpkiPem: string): Promise<EncryptedPayload>;
599
+ /**
600
+ * Decrypt a D5 `session_envelope` and immediately re-encrypt the recovered
601
+ * `SessionData` to a different ECDH P-256 public key — for example, your
602
+ * internal KMS or HSM public key.
603
+ *
604
+ * This is the D5 session-envelope counterpart to {@link transformAddress}
605
+ * (which handles the legacy single-blob `address_encrypted` path). The
606
+ * decrypted `SessionData` JSON is held in memory only for the duration of
607
+ * this call and is **never returned** to the caller — the function returns a
608
+ * fresh ciphertext only. Use it when you want your application tier to relay
609
+ * OneAddress dispatches into KMS-encrypted storage without ever handling the
610
+ * plaintext address.
611
+ *
612
+ * The re-encryption uses an ephemeral ECDH key pair (forward secrecy),
613
+ * HKDF-SHA256 with a fresh per-transform salt, and AES-256-GCM — identical to
614
+ * `transformAddress` but with a distinct HKDF `info` string
615
+ * (`oneaddress:transform-session:<partnerId>`) so the two transform paths
616
+ * never derive the same AES key.
617
+ *
618
+ * **Typical use case:** you receive the OneAddress webhook, call
619
+ * `transformSession` with `body.session_key_share`, `body.session_envelope`,
620
+ * your retained private key matching `share.key_id`, and your KMS public key,
621
+ * then store the resulting ciphertext. Decryption only happens inside your
622
+ * KMS/HSM, not in your application tier.
623
+ *
624
+ * @param share - The `session_key_share` block from the webhook body.
625
+ * @param envelopeB64 - The `session_envelope` base64url string from the webhook body.
626
+ * @param privateKeyPem - Your PKCS#8 PEM private key whose key_id matches `share.key_id`.
627
+ * @param partnerId - Your partner UUID (used as HKDF info during the session decrypt).
628
+ * @param recipientSpkiPem - SPKI PEM public key of the target recipient (e.g. KMS key).
629
+ * @returns A fresh `EncryptedPayload` encrypted to `recipientSpkiPem`.
630
+ *
631
+ * @example
632
+ * import { transformSession } from '@oneaddress/partner-sdk';
633
+ *
634
+ * const body = await req.json();
635
+ * const myPrivateKey = keyStore.get(body.session_key_share.key_id);
636
+ * const kmsBlob = await transformSession(
637
+ * body.session_key_share,
638
+ * body.session_envelope,
639
+ * myPrivateKey,
640
+ * body.partner_id,
641
+ * process.env.KMS_PUBLIC_KEY_SPKI_PEM!,
642
+ * );
643
+ * await db.storeEncryptedAddress(body.customer.email, kmsBlob);
644
+ */
645
+ declare function transformSession(share: SessionKeyShare, envelopeB64: string, privateKeyPem: string, partnerId: string, recipientSpkiPem: string): Promise<EncryptedPayload>;
439
646
  /**
440
647
  * Fetch OneAddress's ECDSA P-256 LOA signing public key from the well-known endpoint.
441
648
  *
@@ -482,6 +689,61 @@ declare function fetchLoaPublicKey(appBaseUrl?: string): Promise<string>;
482
689
  * @returns `true` if the signature is valid; `false` if invalid or unrecognised algorithm
483
690
  */
484
691
  declare function verifyLOA(loa: OneAddressLOA, publicKeyPem: string): Promise<boolean>;
692
+ /**
693
+ * The D5 Letter of Authority — the signed consent attestation recovered by
694
+ * decrypting `loa_encrypted`. Unlike the legacy {@link OneAddressLOA} (which
695
+ * carried a freeform `text` blob), the D5 LOA is structured: the signature
696
+ * covers the canonical pipe-joined form (see {@link canonicalD5LoaForm}).
697
+ *
698
+ * The consumer's legal name (`consumer_name`) is present here because this
699
+ * object only ever exists AFTER you decrypt `loa_encrypted` with your private
700
+ * key — it never travels in cleartext.
701
+ */
702
+ interface OneAddressD5LOA {
703
+ consumer_name: string;
704
+ authorisation_date: string;
705
+ scope: string;
706
+ update_uid: string;
707
+ oneaddress_acn: string;
708
+ oneaddress_signature: string;
709
+ signature_algorithm: 'ECDSA-P256' | 'HMAC-SHA256';
710
+ }
711
+ /**
712
+ * The exact canonical string OneAddress signs for a D5 LOA — must be
713
+ * reproduced byte-for-byte before verifying the signature. Pipe-delimited:
714
+ * `consumer_name|authorisation_date|scope|update_uid|oneaddress_acn`.
715
+ * Mirrors `apps/customer/src/lib/loa-d5.ts → canonicalLoaForm`.
716
+ */
717
+ declare function canonicalD5LoaForm(loa: OneAddressD5LOA): string;
718
+ /**
719
+ * Recompute the OneAddress LOA reference — `base64url(SHA-256(canonical form))`
720
+ * — from a decrypted D5 LOA. Echo the returned value in your `/api/confirm`
721
+ * callback (the `loa_ref` field) so OneAddress can verify, against the value it
722
+ * stored at dispatch time, that you decrypted a valid signed consent. The ref
723
+ * carries no name in the clear and is only computable after you decrypt the
724
+ * LOA. Mirrors `lib/loa-d5.ts → d5LoaRef`.
725
+ */
726
+ declare function d5LoaRef(loa: OneAddressD5LOA): string;
727
+ /**
728
+ * Verify the ECDSA P-256 signature on a decrypted D5 Letter of Authority.
729
+ * Reconstructs {@link canonicalD5LoaForm} and verifies `oneaddress_signature`
730
+ * against OneAddress's public key (from {@link fetchLoaPublicKey}).
731
+ *
732
+ * @returns `true` if valid; `false` for an invalid signature or the deprecated
733
+ * HMAC algorithm (which partners cannot verify).
734
+ */
735
+ declare function verifyD5LOA(loa: OneAddressD5LOA, publicKeyPem: string): Promise<boolean>;
736
+ /**
737
+ * Decrypt a per-partner `loa_encrypted` block (the `{ session_envelope,
738
+ * session_key_share }` shape emitted by OneAddress under the mandate) to
739
+ * recover the signed {@link OneAddressD5LOA}. Uses the same {@link decryptSession}
740
+ * machinery as the address envelope — the LOA is wrapped to the SAME key_id, so
741
+ * the private key matching `share.key_id` decrypts both.
742
+ */
743
+ declare function decryptLoaEncrypted(loaEncrypted: {
744
+ session_envelope: string;
745
+ session_key_share: SessionKeyShare;
746
+ }, privateKeyPem: string, partnerId: string): Promise<OneAddressD5LOA>;
485
747
  /**
486
748
  * Validate and narrow the decrypted address payload to `OneAddressAddress`.
487
749
  *
@@ -562,4 +824,4 @@ type HandlerResponse = Response;
562
824
  */
563
825
  declare function createOneAddressHandler(options: OneAddressHandlerOptions): (req: HandlerRequest) => Promise<HandlerResponse>;
564
826
 
565
- export { type ComparableAddress, type EncryptedPayload, type OneAddressAccountVerifyEvent, type OneAddressAddress, type OneAddressCustomer, type OneAddressHandlerOptions, type OneAddressIdVerification, type OneAddressLOA, type OneAddressUpdateEvent, type OneAddressVerifyEvent, type SessionData, type SessionKeyShare, compareAddresses, createOneAddressHandler, decryptAddress as decrypt, decryptAddress, decryptSession, fetchLoaPublicKey, transformAddress as transform, transformAddress, validateAddress, verifySignature as verify, verifyWithGrace as verifyGrace, verifyLOA, verifySignature, verifyWithGrace };
827
+ export { type BlindIndexField, CURRENT_VERSION, type ComparableAddress, DEFAULT_REPLAY_WINDOW_SECS, type EncryptedPayload, type OneAddressAccountVerifyEvent, type OneAddressAddress, type OneAddressCustomer, type OneAddressCustomerMatch, type OneAddressD5LOA, type OneAddressHandlerOptions, type OneAddressIdVerification, type OneAddressLOA, type OneAddressUpdateEvent, type OneAddressVerifyEvent, SUPPORTED_VERSIONS, type SessionData, type SessionKeyShare, canonicalD5LoaForm, compareAddresses, computeBlindIndex, createOneAddressHandler, d5LoaRef, decryptAddress as decrypt, decryptAddress, decryptLoaEncrypted, decryptSession, fetchLoaPublicKey, isFreshTimestamp, normalizeForBlindIndex, transformAddress as transform, transformAddress, transformSession, validateAddress, verifySignature as verify, verifyD5LOA, verifyWithGrace as verifyGrace, verifyLOA, verifySignature, verifyWithGrace };