@calimero-network/mero-js 23.5.0 → 23.7.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.mjs CHANGED
@@ -3475,6 +3475,7 @@ async function resolveSigner(secret, signer, label2) {
3475
3475
  var DOMAIN = new TextEncoder().encode("calimero.mdma.account-ownership-claim.v1\0");
3476
3476
  var DEFAULT_TTL_MS = 6e4;
3477
3477
  var MAX_TTL_MS = 5 * 6e4;
3478
+ var MAX_RELAY_URL = 1024;
3478
3479
  var ACCOUNT_OWNERSHIP_AUDIENCE = "mdma:enable-ha-namespace";
3479
3480
  var ACCOUNT_HA_AUDIENCE = "mdma:enable-ha-namespace-as-account";
3480
3481
  function base64(bytes) {
@@ -3486,7 +3487,10 @@ var hex32 = (value, what) => {
3486
3487
  if (!/^[0-9a-fA-F]{64}$/.test(value)) throw new Error(`${what} must be 64 hex characters`);
3487
3488
  return value.toLowerCase();
3488
3489
  };
3489
- async function signClaim(input, audience, subject) {
3490
+ async function signClaim(input, audience, subject, relayUrl) {
3491
+ if (relayUrl !== void 0 && !(relayUrl.length > 0 && relayUrl.length <= MAX_RELAY_URL)) {
3492
+ throw new Error(`relayUrl must be a non-empty string of at most ${MAX_RELAY_URL} characters`);
3493
+ }
3490
3494
  const ttl = input.ttlMs ?? DEFAULT_TTL_MS;
3491
3495
  if (!(ttl > 0 && ttl <= MAX_TTL_MS)) throw new Error(`ttlMs must be in (0, ${MAX_TTL_MS}]`);
3492
3496
  const groupId = hex32(input.namespaceId, "namespaceId");
@@ -3505,7 +3509,8 @@ async function signClaim(input, audience, subject) {
3505
3509
  ...subject === void 0 ? {} : { subject },
3506
3510
  nonce: nonce2,
3507
3511
  issued_at_ms: issued,
3508
- expires_at_ms: issued + ttl
3512
+ expires_at_ms: issued + ttl,
3513
+ ...relayUrl === void 0 ? {} : { relay_url: relayUrl }
3509
3514
  })
3510
3515
  );
3511
3516
  const signature = await signer.sign(concat(DOMAIN, payload));
@@ -3516,7 +3521,7 @@ async function signAccountOwnershipClaim(input) {
3516
3521
  return signClaim(input, ACCOUNT_OWNERSHIP_AUDIENCE, input.subject);
3517
3522
  }
3518
3523
  async function signAccountHaClaim(input) {
3519
- return signClaim(input, ACCOUNT_HA_AUDIENCE, void 0);
3524
+ return signClaim(input, ACCOUNT_HA_AUDIENCE, void 0, input.relayUrl);
3520
3525
  }
3521
3526
 
3522
3527
  // src/device-cert/device-cert.ts
@@ -5941,6 +5946,31 @@ var AccountLinkedToSeveralUsersError = class extends AccountHaRefusedError {
5941
5946
  }
5942
5947
  };
5943
5948
  AccountLinkedToSeveralUsersError.CODE = "account_linked_to_several_users";
5949
+ var HaRequestPendingError = class extends AccountHaRefusedError {
5950
+ constructor() {
5951
+ super(...arguments);
5952
+ this.name = "HaRequestPendingError";
5953
+ }
5954
+ };
5955
+ HaRequestPendingError.CODE = "ha_request_pending";
5956
+ var UnknownRelayError = class extends AccountHaRefusedError {
5957
+ constructor() {
5958
+ super(...arguments);
5959
+ this.name = "UnknownRelayError";
5960
+ }
5961
+ };
5962
+ UnknownRelayError.CODE = "unknown_relay";
5963
+ var RelayNotDialableError = class extends AccountHaRefusedError {
5964
+ constructor() {
5965
+ super(...arguments);
5966
+ this.name = "RelayNotDialableError";
5967
+ }
5968
+ };
5969
+ RelayNotDialableError.CODE = "relay_not_dialable";
5970
+ var ACCOUNT_HA_REFUSALS = {
5971
+ 409: [AccountNotLinkedError, AccountLinkedToSeveralUsersError, HaRequestPendingError],
5972
+ 422: [UnknownRelayError, RelayNotDialableError]
5973
+ };
5944
5974
  function conflictCode(bodyText) {
5945
5975
  if (!bodyText) return void 0;
5946
5976
  try {
@@ -6363,7 +6393,11 @@ var CloudClient = class {
6363
6393
  * Pass merod's response object straight through. The cloud accepts core's
6364
6394
  * `signerPublicKey`/`signedPayload` casing as well as snake_case, so there is
6365
6395
  * no re-keying step — and getting that wrong used to surface as a `422`
6366
- * naming three missing fields.
6396
+ * naming three missing fields. Passing it whole also carries the `founding`
6397
+ * and `credential` merod attaches: the cloud accepts a namespace proof only
6398
+ * from a node of the account that founded the namespace, checked with those
6399
+ * two, and only when that account is linked to this login. A node that
6400
+ * predates them, or an admin that did not found the namespace, gets a `422`.
6367
6401
  *
6368
6402
  * Idempotent for the same account. A namespace already claimed by someone
6369
6403
  * else is a `409`; a replayed proof nonce is a `403`.
@@ -6438,7 +6472,8 @@ var CloudClient = class {
6438
6472
  credential: options.credential,
6439
6473
  deviceSecret: options.deviceSecret,
6440
6474
  signer: options.signer,
6441
- ttlMs: options.ttlMs
6475
+ ttlMs: options.ttlMs,
6476
+ relayUrl: options.relayUrl
6442
6477
  });
6443
6478
  const account = options.accountId.toLowerCase();
6444
6479
  const namespace = options.namespaceId.toLowerCase();
@@ -6449,9 +6484,9 @@ var CloudClient = class {
6449
6484
  { body: { ownership_proof: proof }, anonymous: true }
6450
6485
  );
6451
6486
  } catch (err) {
6452
- if (err instanceof HTTPError && err.status === 409) {
6487
+ if (err instanceof HTTPError && ACCOUNT_HA_REFUSALS[err.status]) {
6453
6488
  const code = conflictCode(err.bodyText);
6454
- const Typed = code === AccountNotLinkedError.CODE ? AccountNotLinkedError : code === AccountLinkedToSeveralUsersError.CODE ? AccountLinkedToSeveralUsersError : void 0;
6489
+ const Typed = ACCOUNT_HA_REFUSALS[err.status].find((t) => t.CODE === code);
6455
6490
  if (Typed && code) {
6456
6491
  throw new Typed(code, err.status, err.statusText, err.url, err.headers, err.bodyText);
6457
6492
  }
@@ -11180,6 +11215,7 @@ export {
11180
11215
  EphemeralClient,
11181
11216
  HANDSHAKE_PATH,
11182
11217
  HTTPError,
11218
+ HaRequestPendingError,
11183
11219
  INVITED_ROLE,
11184
11220
  IntentRefusedError,
11185
11221
  LocalStorageTokenStore,
@@ -11192,6 +11228,7 @@ export {
11192
11228
  PRESENCE_DOMAIN,
11193
11229
  REQUEST_PROOF_HEADER,
11194
11230
  RelayClient,
11231
+ RelayNotDialableError,
11195
11232
  RelayObserver,
11196
11233
  RelayPresenceClient,
11197
11234
  RelayTransport,
@@ -11203,6 +11240,7 @@ export {
11203
11240
  SealedTransportError,
11204
11241
  SseClient,
11205
11242
  StaleTransportKeyError,
11243
+ UnknownRelayError,
11206
11244
  WarrantNonceExhaustedError,
11207
11245
  WarrantNonceRouteUnavailableError,
11208
11246
  WebHttpClient,