@oneaddress/partner-sdk 2.9.0 → 2.10.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.
@@ -5,7 +5,7 @@ import {
5
5
  isFreshTimestamp,
6
6
  isSafeWebhookUrl,
7
7
  verifyRegistryHead
8
- } from "./chunk-PP2TM7HN.mjs";
8
+ } from "./chunk-JJ2B5VFO.mjs";
9
9
 
10
10
  // src/index.ts
11
11
  import crypto from "crypto";
@@ -206,6 +206,33 @@ var LOA_V2_REFUSALS = [
206
206
  "loa_v2_name_mismatch"
207
207
  ];
208
208
 
209
+ // ../protocol/src/verification-commitment.ts
210
+ var VERIFICATION_COMMITMENT_VERSION = "oa-verify.v1";
211
+ var ASCII_WHITESPACE_RUN = /[\t\n\v\f\r ]+/g;
212
+ function normaliseCommitmentField(value) {
213
+ if (typeof value !== "string") return "";
214
+ const collapsed = value.replace(ASCII_WHITESPACE_RUN, " ");
215
+ const start = collapsed.startsWith(" ") ? 1 : 0;
216
+ const end = collapsed.length > start && collapsed.endsWith(" ") ? collapsed.length - 1 : collapsed.length;
217
+ return collapsed.slice(start, end);
218
+ }
219
+ function verificationCommitmentString(verificationId, index, record) {
220
+ if (!Number.isInteger(index) || index <= 0) {
221
+ throw new RangeError(`verification commitment index must be a positive integer, got ${String(index)}`);
222
+ }
223
+ return [
224
+ VERIFICATION_COMMITMENT_VERSION,
225
+ normaliseCommitmentField(verificationId),
226
+ String(index),
227
+ normaliseCommitmentField(record.name),
228
+ normaliseCommitmentField(record.street),
229
+ normaliseCommitmentField(record.suburb),
230
+ normaliseCommitmentField(record.state),
231
+ normaliseCommitmentField(record.postcode),
232
+ normaliseCommitmentField(record.country)
233
+ ].join("\n");
234
+ }
235
+
209
236
  // src/app-hosts.ts
210
237
  function allowedAppHosts() {
211
238
  const hosts = ["oneaddress.io", "app.oneaddress.io"];
@@ -239,6 +266,96 @@ function resolveAppOrigin(baseUrl, label, extraHosts) {
239
266
  return parsed.origin;
240
267
  }
241
268
 
269
+ // src/update-check.ts
270
+ var SDK_VERSION = "2.10.0";
271
+ var UPDATE_GUIDE_URL = "https://partners.oneaddress.io/#updating-safely";
272
+ var REGISTRY_URL = "https://registry.npmjs.org/@oneaddress/partner-sdk/latest";
273
+ var ONE_DAY_MS = 24 * 60 * 60 * 1e3;
274
+ var TIMEOUT_MS = 5e3;
275
+ function updateCheckEnabled(setting) {
276
+ const s = (setting ?? "").trim().toLowerCase();
277
+ return !(s === "off" || s === "never" || s === "0" || s === "false");
278
+ }
279
+ function parseVersion(v) {
280
+ if (typeof v !== "string") return null;
281
+ const m = /^(\d{1,6})\.(\d{1,6})\.(\d{1,6})$/.exec(v.trim());
282
+ return m ? [Number(m[1]), Number(m[2]), Number(m[3])] : null;
283
+ }
284
+ function isNewerVersion(latest, running) {
285
+ const a = parseVersion(latest);
286
+ const b = parseVersion(running);
287
+ if (!a || !b) return false;
288
+ for (let i = 0; i < 3; i++) {
289
+ if (a[i] !== b[i]) return a[i] > b[i];
290
+ }
291
+ return false;
292
+ }
293
+ function updateAvailableLine(running, latest) {
294
+ return `update available: ${running} -> ${latest}, see ${UPDATE_GUIDE_URL}`;
295
+ }
296
+ function updateCheckMetric(result, product = "partner-sdk-ts") {
297
+ const latest = result.latest ?? "unknown";
298
+ const value = result.state === "available" ? 1 : 0;
299
+ return `oneaddress_update_available{product="${product}",running="${result.running}",latest="${latest}"} ${value}`;
300
+ }
301
+ async function checkForUpdate(opts = {}) {
302
+ const running = opts.running ?? SDK_VERSION;
303
+ const env = opts.env ?? (typeof process !== "undefined" ? process.env : {});
304
+ if (!updateCheckEnabled(env.ONEADDRESS_UPDATE_CHECK)) return { state: "disabled", running };
305
+ const fetchImpl = opts.fetchImpl ?? globalThis.fetch;
306
+ const now = opts.now ?? Date.now;
307
+ if (typeof fetchImpl !== "function") return { state: "unknown", running };
308
+ const abort = new AbortController();
309
+ const timer = setTimeout(() => abort.abort(), opts.timeoutMs ?? TIMEOUT_MS);
310
+ try {
311
+ const res = await fetchImpl(opts.registryUrl ?? REGISTRY_URL, { signal: abort.signal });
312
+ const checkedAt = now();
313
+ if (!res.ok) return { state: "unknown", running, checkedAt };
314
+ const body = await res.json();
315
+ if (!parseVersion(body?.version)) return { state: "unknown", running, checkedAt };
316
+ const latest = body.version.trim();
317
+ return {
318
+ state: isNewerVersion(latest, running) ? "available" : "current",
319
+ running,
320
+ latest,
321
+ checkedAt
322
+ };
323
+ } catch {
324
+ return { state: "unknown", running };
325
+ } finally {
326
+ clearTimeout(timer);
327
+ }
328
+ }
329
+ function startUpdateCheck(opts = {}) {
330
+ const running = opts.running ?? SDK_VERSION;
331
+ let last = { state: "unknown", running };
332
+ let announced = null;
333
+ const log = opts.log ?? ((line) => console.warn(line));
334
+ const env = opts.env ?? (typeof process !== "undefined" ? process.env : {});
335
+ if (!updateCheckEnabled(env.ONEADDRESS_UPDATE_CHECK)) {
336
+ last = { state: "disabled", running };
337
+ opts.onResult?.(last);
338
+ return { current: () => last, stop: () => {
339
+ } };
340
+ }
341
+ const run = async () => {
342
+ const result = await checkForUpdate({ ...opts, running, env });
343
+ if (result.state !== "unknown" || last.state === "unknown") last = result;
344
+ if (last.state === "available" && last.latest && last.latest !== announced) {
345
+ announced = last.latest;
346
+ log(updateAvailableLine(running, last.latest));
347
+ }
348
+ opts.onResult?.(last);
349
+ };
350
+ void run();
351
+ const every = Math.max(opts.intervalMs ?? ONE_DAY_MS, 60 * 60 * 1e3);
352
+ const timer = setInterval(() => {
353
+ void run();
354
+ }, every);
355
+ timer.unref?.();
356
+ return { current: () => last, stop: () => clearInterval(timer) };
357
+ }
358
+
242
359
  // ../name-match/src/index.ts
243
360
  function isEmailLike(value) {
244
361
  return value.includes("@");
@@ -889,14 +1006,21 @@ function signPullRequest(opts) {
889
1006
  };
890
1007
  }
891
1008
  function verifyWithGrace(rawBody, timestamp, signature, currentSecret, previousSecret, graceUntil) {
892
- if (verifySignature(rawBody, signature, timestamp, currentSecret)) return true;
1009
+ return matchingWebhookSecret(rawBody, timestamp, signature, currentSecret, previousSecret, graceUntil) !== null;
1010
+ }
1011
+ function matchingWebhookSecret(rawBody, timestamp, signature, currentSecret, previousSecret, graceUntil) {
1012
+ if (verifySignature(rawBody, signature, timestamp, currentSecret)) return currentSecret;
893
1013
  if (previousSecret && previousSecret !== currentSecret && graceUntil) {
894
1014
  const until = typeof graceUntil === "number" ? graceUntil : Date.parse(graceUntil);
895
- if (!Number.isNaN(until) && Date.now() < until) {
896
- return verifySignature(rawBody, signature, timestamp, previousSecret);
1015
+ if (!Number.isNaN(until) && Date.now() < until && verifySignature(rawBody, signature, timestamp, previousSecret)) {
1016
+ return previousSecret;
897
1017
  }
898
1018
  }
899
- return false;
1019
+ return null;
1020
+ }
1021
+ function computeVerificationCommitment(secret, verificationId, index, record) {
1022
+ if (!secret) throw new Error("[OneAddress SDK] a verification commitment needs the webhook secret");
1023
+ return crypto.createHmac("sha256", secret).update(verificationCommitmentString(verificationId, index, record), "utf8").digest("hex");
900
1024
  }
901
1025
  function base64urlToBuffer(b64) {
902
1026
  return Buffer.from(b64.replace(/-/g, "+").replace(/_/g, "/"), "base64");
@@ -962,15 +1086,34 @@ async function decryptAddress(payload, privateKeyPem, partnerId) {
962
1086
  );
963
1087
  return JSON.parse(new TextDecoder().decode(plaintext));
964
1088
  }
1089
+ var PRODUCT_CODE_PATTERN = /^[a-z0-9][a-z0-9_-]{0,39}$/;
1090
+ function readSessionProduct(raw) {
1091
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null;
1092
+ const { id, code, name } = raw;
1093
+ if (typeof id !== "string" || typeof code !== "string" || !PRODUCT_CODE_PATTERN.test(code)) return null;
1094
+ return { id, code, name: typeof name === "string" && name !== "" ? name : code };
1095
+ }
1096
+ function normaliseSessionProduct(data) {
1097
+ if (!data || typeof data !== "object" || !("product" in data)) return data;
1098
+ const product = readSessionProduct(data.product);
1099
+ if (product === null && data.product != null) {
1100
+ console.warn(
1101
+ // NOSONAR - intentional SDK diagnostic, not user-controlled data
1102
+ "[OneAddress SDK] the session envelope carried a malformed product, so it is treated as absent and the update is handled as a plain account."
1103
+ );
1104
+ }
1105
+ return { ...data, product };
1106
+ }
965
1107
  async function decryptSession(share, envelopeB64, privateKeyPem, partnerId, expectedAlg) {
966
- const { decryptSession: decryptSessionCrypto } = await import("./src-P3LR7F4A.mjs");
967
- return decryptSessionCrypto(
1108
+ const { decryptSession: decryptSessionCrypto } = await import("./src-XBG2QHG4.mjs");
1109
+ const data = await decryptSessionCrypto(
968
1110
  share,
969
1111
  envelopeB64,
970
1112
  typeof privateKeyPem === "string" ? pemToBase64(privateKeyPem) : privateKeyPem,
971
1113
  partnerId,
972
1114
  expectedAlg
973
1115
  );
1116
+ return normaliseSessionProduct(data);
974
1117
  }
975
1118
  async function transformAddress(payload, privateKeyPem, partnerId, recipientSpkiPem) {
976
1119
  const plaintext = await decryptAddress(payload, privateKeyPem, partnerId);
@@ -1085,7 +1228,11 @@ async function confirmUpdate(opts) {
1085
1228
  "Content-Type": "application/json",
1086
1229
  "X-OneAddress-Timestamp": timestamp,
1087
1230
  "X-OneAddress-Signature": signature,
1088
- [PULL_SIGNATURE_VERSION_HEADER]: "2"
1231
+ [PULL_SIGNATURE_VERSION_HEADER]: "2",
1232
+ // Which SDK sent this, so the partner portal can say when an update
1233
+ // exists. Software name and version only; outside the signature, and
1234
+ // recorded by OneAddress only after the signature verifies.
1235
+ "X-OneAddress-Client": `partner-sdk-ts/${SDK_VERSION}`
1089
1236
  },
1090
1237
  // The SAME string that was signed. Not an object, and not re-stringified.
1091
1238
  body
@@ -1781,7 +1928,8 @@ function createOneAddressHandler(options) {
1781
1928
  }
1782
1929
  }
1783
1930
  assertEffectiveDateBound(data.effective_date, body.effective_date);
1784
- return { updateTypes, address, newPhone, newEmail, customer, loa, loa_ref, raw: data };
1931
+ const product = data.product ?? null;
1932
+ return { updateTypes, address, newPhone, newEmail, customer, loa, loa_ref, product, raw: data };
1785
1933
  }
1786
1934
  function isD5Body(body) {
1787
1935
  return typeof body.session_envelope === "string" && !!body.session_key_share && typeof body.session_key_share === "object";
@@ -1810,7 +1958,8 @@ function createOneAddressHandler(options) {
1810
1958
  headers: { "Content-Type": "application/json" }
1811
1959
  });
1812
1960
  }
1813
- if (!verifyWithGrace(rawBody, tsHeader, sig, webhookSecret, previousWebhookSecret, webhookSecretGraceUntil)) {
1961
+ const signingSecret = matchingWebhookSecret(rawBody, tsHeader, sig, webhookSecret, previousWebhookSecret, webhookSecretGraceUntil);
1962
+ if (signingSecret === null) {
1814
1963
  return new Response(JSON.stringify({ error: "Invalid signature" }), {
1815
1964
  status: 401,
1816
1965
  headers: { "Content-Type": "application/json" }
@@ -1954,6 +2103,7 @@ function createOneAddressHandler(options) {
1954
2103
  loa_ref: d5.loa_ref,
1955
2104
  id_verification: body.id_verification,
1956
2105
  ...body.effective_date !== void 0 ? { effective_date: body.effective_date } : {},
2106
+ product: d5.product,
1957
2107
  raw: d5.raw
1958
2108
  };
1959
2109
  } else {
@@ -2022,6 +2172,8 @@ function createOneAddressHandler(options) {
2022
2172
  loa,
2023
2173
  loa_ref,
2024
2174
  id_verification: body.id_verification,
2175
+ // The legacy blob predates product lines.
2176
+ product: null,
2025
2177
  raw: rawAddress
2026
2178
  };
2027
2179
  }
@@ -2163,11 +2315,26 @@ function createOneAddressHandler(options) {
2163
2315
  address: d5.address,
2164
2316
  raw: d5.raw
2165
2317
  });
2318
+ const index = typeof body.index === "number" ? body.index : void 0;
2319
+ const verificationId = typeof body.verification_id === "string" ? body.verification_id : "";
2320
+ let commitment;
2321
+ if (index !== void 0 && Number.isInteger(index) && index > 0 && verificationId) {
2322
+ const newAddress = d5.raw.new_address ?? {};
2323
+ commitment = computeVerificationCommitment(signingSecret, verificationId, index, {
2324
+ name: d5.raw.verified_name,
2325
+ street: newAddress.street,
2326
+ suburb: newAddress.suburb,
2327
+ state: newAddress.state,
2328
+ postcode: newAddress.postcode,
2329
+ country: newAddress.country
2330
+ });
2331
+ }
2166
2332
  return declared(200, {
2167
2333
  ok: true,
2168
2334
  test: true,
2169
2335
  verification: true,
2170
- index: typeof body.index === "number" ? body.index : void 0
2336
+ index,
2337
+ ...commitment ? { commitment } : {}
2171
2338
  });
2172
2339
  }
2173
2340
  const evt = typeof body.event === "string" ? body.event : "(none)";
@@ -2388,6 +2555,17 @@ export {
2388
2555
  isAcceptableAlias,
2389
2556
  acceptTypedAliases,
2390
2557
  LOA_V2_REFUSALS,
2558
+ VERIFICATION_COMMITMENT_VERSION,
2559
+ normaliseCommitmentField,
2560
+ verificationCommitmentString,
2561
+ SDK_VERSION,
2562
+ UPDATE_GUIDE_URL,
2563
+ updateCheckEnabled,
2564
+ isNewerVersion,
2565
+ updateAvailableLine,
2566
+ updateCheckMetric,
2567
+ checkForUpdate,
2568
+ startUpdateCheck,
2391
2569
  isEmailLike,
2392
2570
  namePoolMatches,
2393
2571
  findMatchingStoredName,
@@ -2402,7 +2580,11 @@ export {
2402
2580
  verifySignature,
2403
2581
  signPullRequest,
2404
2582
  verifyWithGrace,
2583
+ matchingWebhookSecret,
2584
+ computeVerificationCommitment,
2405
2585
  decryptAddress,
2586
+ PRODUCT_CODE_PATTERN,
2587
+ readSessionProduct,
2406
2588
  decryptSession,
2407
2589
  transformAddress,
2408
2590
  transformSession,
package/dist/edge.d.mts CHANGED
@@ -1 +1 @@
1
- export { BlindIndexField, CURRENT_VERSION, ComparableAddress, DEFAULT_REPLAY_WINDOW_SECS, EncryptedPayload, LoaV2Context, LoaV2Refusal, LoaV2RefusedError, OneAddressAccountVerifyEvent, OneAddressAddress, OneAddressCustomer, OneAddressCustomerMatch, OneAddressD5LOA, OneAddressHandlerOptions, OneAddressIdVerification, OneAddressLOA, OneAddressUpdateEvent, OneAddressVerifyEvent, SUPPORTED_VERSIONS, SessionData, SessionKeyShare, SessionUpdateType, VerifyResultCallbackParams, acceptTypedAliases, canonicalD5LoaForm, canonicalD5LoaFormV2, checkLoaV2, compareAddresses, computeBlindIndex, createOneAddressHandler, d5LoaRef, decrypt as decryptAddress, decryptLoaEncrypted, decryptSession, fetchLoaPublicKey, findMatchingStoredName, hasLoaV2, isAcceptableAlias, isEmailLike, isFreshTimestamp, loaEnvelopeSha256, loaFirstName, loaNamesMatch, loaSurname, namePoolMatches, normaliseLoaName, normalizeForBlindIndex, sendVerifyResult, transform as transformAddress, transformSession, typedAliasesOf, validateAddress, verifyD5LOA, verifyD5LOAv2, verifyLOA, verify as verifySignature, verifyGrace as verifyWithGrace } from './index.mjs';
1
+ export { BlindIndexField, CURRENT_VERSION, ComparableAddress, DEFAULT_REPLAY_WINDOW_SECS, EncryptedPayload, LoaV2Context, LoaV2Refusal, LoaV2RefusedError, OneAddressAccountVerifyEvent, OneAddressAddress, OneAddressCustomer, OneAddressCustomerMatch, OneAddressD5LOA, OneAddressHandlerOptions, OneAddressIdVerification, OneAddressLOA, OneAddressUpdateEvent, OneAddressVerifyEvent, SUPPORTED_VERSIONS, SessionData, SessionKeyShare, SessionUpdateType, VERIFICATION_COMMITMENT_VERSION, VerifyResultCallbackParams, acceptTypedAliases, canonicalD5LoaForm, canonicalD5LoaFormV2, checkLoaV2, compareAddresses, computeBlindIndex, computeVerificationCommitment, createOneAddressHandler, d5LoaRef, decrypt as decryptAddress, decryptLoaEncrypted, decryptSession, fetchLoaPublicKey, findMatchingStoredName, hasLoaV2, isAcceptableAlias, isEmailLike, isFreshTimestamp, loaEnvelopeSha256, loaFirstName, loaNamesMatch, loaSurname, matchingWebhookSecret, namePoolMatches, normaliseCommitmentField, normaliseLoaName, normalizeForBlindIndex, sendVerifyResult, transform as transformAddress, transformSession, typedAliasesOf, validateAddress, verificationCommitmentString, verifyD5LOA, verifyD5LOAv2, verifyLOA, verify as verifySignature, verifyGrace as verifyWithGrace } from './index.mjs';
package/dist/edge.d.ts CHANGED
@@ -1 +1 @@
1
- export { BlindIndexField, CURRENT_VERSION, ComparableAddress, DEFAULT_REPLAY_WINDOW_SECS, EncryptedPayload, LoaV2Context, LoaV2Refusal, LoaV2RefusedError, OneAddressAccountVerifyEvent, OneAddressAddress, OneAddressCustomer, OneAddressCustomerMatch, OneAddressD5LOA, OneAddressHandlerOptions, OneAddressIdVerification, OneAddressLOA, OneAddressUpdateEvent, OneAddressVerifyEvent, SUPPORTED_VERSIONS, SessionData, SessionKeyShare, SessionUpdateType, VerifyResultCallbackParams, acceptTypedAliases, canonicalD5LoaForm, canonicalD5LoaFormV2, checkLoaV2, compareAddresses, computeBlindIndex, createOneAddressHandler, d5LoaRef, decrypt as decryptAddress, decryptLoaEncrypted, decryptSession, fetchLoaPublicKey, findMatchingStoredName, hasLoaV2, isAcceptableAlias, isEmailLike, isFreshTimestamp, loaEnvelopeSha256, loaFirstName, loaNamesMatch, loaSurname, namePoolMatches, normaliseLoaName, normalizeForBlindIndex, sendVerifyResult, transform as transformAddress, transformSession, typedAliasesOf, validateAddress, verifyD5LOA, verifyD5LOAv2, verifyLOA, verify as verifySignature, verifyGrace as verifyWithGrace } from './index.js';
1
+ export { BlindIndexField, CURRENT_VERSION, ComparableAddress, DEFAULT_REPLAY_WINDOW_SECS, EncryptedPayload, LoaV2Context, LoaV2Refusal, LoaV2RefusedError, OneAddressAccountVerifyEvent, OneAddressAddress, OneAddressCustomer, OneAddressCustomerMatch, OneAddressD5LOA, OneAddressHandlerOptions, OneAddressIdVerification, OneAddressLOA, OneAddressUpdateEvent, OneAddressVerifyEvent, SUPPORTED_VERSIONS, SessionData, SessionKeyShare, SessionUpdateType, VERIFICATION_COMMITMENT_VERSION, VerifyResultCallbackParams, acceptTypedAliases, canonicalD5LoaForm, canonicalD5LoaFormV2, checkLoaV2, compareAddresses, computeBlindIndex, computeVerificationCommitment, createOneAddressHandler, d5LoaRef, decrypt as decryptAddress, decryptLoaEncrypted, decryptSession, fetchLoaPublicKey, findMatchingStoredName, hasLoaV2, isAcceptableAlias, isEmailLike, isFreshTimestamp, loaEnvelopeSha256, loaFirstName, loaNamesMatch, loaSurname, matchingWebhookSecret, namePoolMatches, normaliseCommitmentField, normaliseLoaName, normalizeForBlindIndex, sendVerifyResult, transform as transformAddress, transformSession, typedAliasesOf, validateAddress, verificationCommitmentString, verifyD5LOA, verifyD5LOAv2, verifyLOA, verify as verifySignature, verifyGrace as verifyWithGrace } from './index.js';