@invisi-labs/primitives 1.2.0 → 1.2.1

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.cts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { ReceivingAddress } from './address.cjs';
2
2
  export { DecodedAddress, ReceivingKeyLookup, decodeAddress } from './address.cjs';
3
- import { N as NoteBytes, a as NoteDecryptionContext, C as CreditStateDecryptionContext } from './encryption-0HQXc3tT.cjs';
4
- export { b as CreditStateEncryptionContext, c as NoteEncryptionContext, d as decryptCreditState, e as decryptPrivateTransferNote, f as encryptCreditState, g as encryptPrivateTransferNote } from './encryption-0HQXc3tT.cjs';
3
+ import { N as NoteBytes, a as NoteDecryptionContext, C as CreditStateDecryptionContext } from './encryption-P2HJ89FQ.cjs';
4
+ export { b as CreditStateEncryptionContext, c as NoteEncryptionContext, d as decryptCreditState, e as decryptPrivateTransferNote, f as encryptCreditState, g as encryptPrivateTransferNote } from './encryption-P2HJ89FQ.cjs';
5
5
  import { a as CreditState, b as CreditPolicy, c as CreditUsage, N as Note, P as ProtocolContext, C as CreditDeliveryState, d as CreditDataV1, F as FundsDataV1 } from './types-CEAuh35C.cjs';
6
6
  export { e as CreditStateRecovery, M as MAX_MEMO_BYTES, f as Memo, R as ReceivingKeys, g as decodeMemoText, h as encodeMemo } from './types-CEAuh35C.cjs';
7
7
  export { computeNoteCommitment, computeNoteIdentity, computeNoteNullifier } from './note.cjs';
@@ -465,10 +465,23 @@ declare function acceptWalletResponses(prepared: readonly PreparedWalletRequest[
465
465
 
466
466
  declare function provenanceIntentHash(originalRequestHash: string): string;
467
467
 
468
+ type DeliveryDecryptionResult = Uint8Array | null | {
469
+ status: "authentication-failed";
470
+ };
471
+ interface DeliveryDetectionCandidate extends NoteDecryptionContext {
472
+ role: "funds" | "owner" | "spender";
473
+ clue: `0x${string}`;
474
+ ciphertext: `0x${string}`;
475
+ }
476
+ interface DeliveryDetectionBatch {
477
+ ownerAddress: `0x${string}`;
478
+ results: readonly ("candidate" | "not-recipient" | "unresolved")[];
479
+ }
468
480
  /** App-owned decryption. Only null means nonrecipient; operational failures must reject. */
469
481
  interface DeliveryDecryptor {
470
- decryptNote(envelope: NoteBytes, context: NoteDecryptionContext): Promise<Uint8Array | null>;
471
- decryptCredit(envelope: NoteBytes, context: CreditStateDecryptionContext): Promise<Uint8Array | null>;
482
+ detect?(candidates: readonly DeliveryDetectionCandidate[]): Promise<DeliveryDetectionBatch>;
483
+ decryptNote(envelope: NoteBytes, context: NoteDecryptionContext): Promise<DeliveryDecryptionResult>;
484
+ decryptCredit(envelope: NoteBytes, context: CreditStateDecryptionContext): Promise<DeliveryDecryptionResult>;
472
485
  }
473
486
  /** Keeps App failures and invalid plaintext distinct from unrelated local scan candidates. */
474
487
  declare class DeliveryDecryptionError extends Error {
@@ -477,6 +490,7 @@ declare class DeliveryDecryptionError extends Error {
477
490
  /** Bind untrusted App plaintext to the requested wallet, deployment, token and commitment. */
478
491
  declare function authenticatedDecryptor(wallet: Wallet, decryptor: DeliveryDecryptor): {
479
492
  ownerAddress: `0x${string}`;
493
+ detectCandidates(candidates: readonly DeliveryDetectionCandidate[]): Promise<any[]>;
480
494
  decryptNote(envelope: NoteBytes, context: Omit<NoteDecryptionContext, "ownerAddress">): Promise<{
481
495
  chainId: bigint;
482
496
  protocolAddress: bigint;
@@ -489,4 +503,9 @@ declare function authenticatedDecryptor(wallet: Wallet, decryptor: DeliveryDecry
489
503
  decryptCredit(envelope: NoteBytes, context: Omit<CreditStateDecryptionContext, "ownerAddress">): Promise<CreditDeliveryState | null>;
490
504
  };
491
505
 
492
- export { type Address, AuthorizedStatement, CreditDataV1, CreditDeliveryState, type CreditOpening, CreditPolicy, CreditState, CreditStateDecryptionContext, CreditUsage, D_CNULL, D_CREDIT, D_CREDIT_KEY_HASH, D_CREDIT_RECOVERY, D_CREDIT_STATE_NONCE, D_IDENT, D_NOTE, D_NULL, DeliveryDecryptionError, type DeliveryDecryptor, type DeliveryKind, Detector, Disclosure, type DisclosureExport, type DisclosureOutput, type DisclosureResult, FIELD_MODULUS, FundsDataV1, type Hex, MAX_CIPHERTEXT_BYTES, MAX_WALLET_BATCH_BYTES, MissingCapabilityError, NativeWalletError, Note, NoteBytes, NoteDecryptionContext, NullifierCapability, type PreparedWalletRequest, ProtocolContext, ReceivingAddress, type SigningRequest, TREE_DEPTH, ViewingCapability, WALLET_AUTHORIZATION_TYPES, Wallet, type WalletAcceptedResults, type WalletAuthorizationRequest, type WalletAuthorizationWitness, type WalletBytes, type WalletDecryptContext, type WalletHex, type WalletMethod, type WalletOperation, type WalletParams, type WalletRequest, type WalletResponse, type WalletResults, type WalletWireContext, acceptWalletResponse, acceptWalletResponses, authenticatedDecryptor, computeOwnerAddress, computeWalletAuthorizationDigest, createClue, createClueFromSeed, createSigningRequest, creditV1, decodeDeliveryHeader, deliveryCodec, deliveryHeader, deriveClueKey, fundsV1, generateCreditSharedKey, generateNonce, poseidonHash, prepareWalletRequest, provenanceIntentHash, validateClueKey, validateDeliveryCiphertext, validateWalletBatch, validateWalletResponses, verifyWalletAuthorization, viewingPublicKey, walletAuthorizationTypedData };
506
+ /** AEAD failure is ambiguous: wrong recipient key, wrong context or damaged ciphertext. */
507
+ declare class DeliveryAuthenticationError extends Error {
508
+ constructor(cause?: unknown);
509
+ }
510
+
511
+ export { type Address, AuthorizedStatement, CreditDataV1, CreditDeliveryState, type CreditOpening, CreditPolicy, CreditState, CreditStateDecryptionContext, CreditUsage, D_CNULL, D_CREDIT, D_CREDIT_KEY_HASH, D_CREDIT_RECOVERY, D_CREDIT_STATE_NONCE, D_IDENT, D_NOTE, D_NULL, DeliveryAuthenticationError, DeliveryDecryptionError, type DeliveryDecryptionResult, type DeliveryDecryptor, type DeliveryDetectionBatch, type DeliveryDetectionCandidate, type DeliveryKind, Detector, Disclosure, type DisclosureExport, type DisclosureOutput, type DisclosureResult, FIELD_MODULUS, FundsDataV1, type Hex, MAX_CIPHERTEXT_BYTES, MAX_WALLET_BATCH_BYTES, MissingCapabilityError, NativeWalletError, Note, NoteBytes, NoteDecryptionContext, NullifierCapability, type PreparedWalletRequest, ProtocolContext, ReceivingAddress, type SigningRequest, TREE_DEPTH, ViewingCapability, WALLET_AUTHORIZATION_TYPES, Wallet, type WalletAcceptedResults, type WalletAuthorizationRequest, type WalletAuthorizationWitness, type WalletBytes, type WalletDecryptContext, type WalletHex, type WalletMethod, type WalletOperation, type WalletParams, type WalletRequest, type WalletResponse, type WalletResults, type WalletWireContext, acceptWalletResponse, acceptWalletResponses, authenticatedDecryptor, computeOwnerAddress, computeWalletAuthorizationDigest, createClue, createClueFromSeed, createSigningRequest, creditV1, decodeDeliveryHeader, deliveryCodec, deliveryHeader, deriveClueKey, fundsV1, generateCreditSharedKey, generateNonce, poseidonHash, prepareWalletRequest, provenanceIntentHash, validateClueKey, validateDeliveryCiphertext, validateWalletBatch, validateWalletResponses, verifyWalletAuthorization, viewingPublicKey, walletAuthorizationTypedData };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { ReceivingAddress } from './address.js';
2
2
  export { DecodedAddress, ReceivingKeyLookup, decodeAddress } from './address.js';
3
- import { N as NoteBytes, a as NoteDecryptionContext, C as CreditStateDecryptionContext } from './encryption-D06YBTW0.js';
4
- export { b as CreditStateEncryptionContext, c as NoteEncryptionContext, d as decryptCreditState, e as decryptPrivateTransferNote, f as encryptCreditState, g as encryptPrivateTransferNote } from './encryption-D06YBTW0.js';
3
+ import { N as NoteBytes, a as NoteDecryptionContext, C as CreditStateDecryptionContext } from './encryption-Bzsl9VQk.js';
4
+ export { b as CreditStateEncryptionContext, c as NoteEncryptionContext, d as decryptCreditState, e as decryptPrivateTransferNote, f as encryptCreditState, g as encryptPrivateTransferNote } from './encryption-Bzsl9VQk.js';
5
5
  import { a as CreditState, b as CreditPolicy, c as CreditUsage, N as Note, P as ProtocolContext, C as CreditDeliveryState, d as CreditDataV1, F as FundsDataV1 } from './types-CEAuh35C.js';
6
6
  export { e as CreditStateRecovery, M as MAX_MEMO_BYTES, f as Memo, R as ReceivingKeys, g as decodeMemoText, h as encodeMemo } from './types-CEAuh35C.js';
7
7
  export { computeNoteCommitment, computeNoteIdentity, computeNoteNullifier } from './note.js';
@@ -465,10 +465,23 @@ declare function acceptWalletResponses(prepared: readonly PreparedWalletRequest[
465
465
 
466
466
  declare function provenanceIntentHash(originalRequestHash: string): string;
467
467
 
468
+ type DeliveryDecryptionResult = Uint8Array | null | {
469
+ status: "authentication-failed";
470
+ };
471
+ interface DeliveryDetectionCandidate extends NoteDecryptionContext {
472
+ role: "funds" | "owner" | "spender";
473
+ clue: `0x${string}`;
474
+ ciphertext: `0x${string}`;
475
+ }
476
+ interface DeliveryDetectionBatch {
477
+ ownerAddress: `0x${string}`;
478
+ results: readonly ("candidate" | "not-recipient" | "unresolved")[];
479
+ }
468
480
  /** App-owned decryption. Only null means nonrecipient; operational failures must reject. */
469
481
  interface DeliveryDecryptor {
470
- decryptNote(envelope: NoteBytes, context: NoteDecryptionContext): Promise<Uint8Array | null>;
471
- decryptCredit(envelope: NoteBytes, context: CreditStateDecryptionContext): Promise<Uint8Array | null>;
482
+ detect?(candidates: readonly DeliveryDetectionCandidate[]): Promise<DeliveryDetectionBatch>;
483
+ decryptNote(envelope: NoteBytes, context: NoteDecryptionContext): Promise<DeliveryDecryptionResult>;
484
+ decryptCredit(envelope: NoteBytes, context: CreditStateDecryptionContext): Promise<DeliveryDecryptionResult>;
472
485
  }
473
486
  /** Keeps App failures and invalid plaintext distinct from unrelated local scan candidates. */
474
487
  declare class DeliveryDecryptionError extends Error {
@@ -477,6 +490,7 @@ declare class DeliveryDecryptionError extends Error {
477
490
  /** Bind untrusted App plaintext to the requested wallet, deployment, token and commitment. */
478
491
  declare function authenticatedDecryptor(wallet: Wallet, decryptor: DeliveryDecryptor): {
479
492
  ownerAddress: `0x${string}`;
493
+ detectCandidates(candidates: readonly DeliveryDetectionCandidate[]): Promise<any[]>;
480
494
  decryptNote(envelope: NoteBytes, context: Omit<NoteDecryptionContext, "ownerAddress">): Promise<{
481
495
  chainId: bigint;
482
496
  protocolAddress: bigint;
@@ -489,4 +503,9 @@ declare function authenticatedDecryptor(wallet: Wallet, decryptor: DeliveryDecry
489
503
  decryptCredit(envelope: NoteBytes, context: Omit<CreditStateDecryptionContext, "ownerAddress">): Promise<CreditDeliveryState | null>;
490
504
  };
491
505
 
492
- export { type Address, AuthorizedStatement, CreditDataV1, CreditDeliveryState, type CreditOpening, CreditPolicy, CreditState, CreditStateDecryptionContext, CreditUsage, D_CNULL, D_CREDIT, D_CREDIT_KEY_HASH, D_CREDIT_RECOVERY, D_CREDIT_STATE_NONCE, D_IDENT, D_NOTE, D_NULL, DeliveryDecryptionError, type DeliveryDecryptor, type DeliveryKind, Detector, Disclosure, type DisclosureExport, type DisclosureOutput, type DisclosureResult, FIELD_MODULUS, FundsDataV1, type Hex, MAX_CIPHERTEXT_BYTES, MAX_WALLET_BATCH_BYTES, MissingCapabilityError, NativeWalletError, Note, NoteBytes, NoteDecryptionContext, NullifierCapability, type PreparedWalletRequest, ProtocolContext, ReceivingAddress, type SigningRequest, TREE_DEPTH, ViewingCapability, WALLET_AUTHORIZATION_TYPES, Wallet, type WalletAcceptedResults, type WalletAuthorizationRequest, type WalletAuthorizationWitness, type WalletBytes, type WalletDecryptContext, type WalletHex, type WalletMethod, type WalletOperation, type WalletParams, type WalletRequest, type WalletResponse, type WalletResults, type WalletWireContext, acceptWalletResponse, acceptWalletResponses, authenticatedDecryptor, computeOwnerAddress, computeWalletAuthorizationDigest, createClue, createClueFromSeed, createSigningRequest, creditV1, decodeDeliveryHeader, deliveryCodec, deliveryHeader, deriveClueKey, fundsV1, generateCreditSharedKey, generateNonce, poseidonHash, prepareWalletRequest, provenanceIntentHash, validateClueKey, validateDeliveryCiphertext, validateWalletBatch, validateWalletResponses, verifyWalletAuthorization, viewingPublicKey, walletAuthorizationTypedData };
506
+ /** AEAD failure is ambiguous: wrong recipient key, wrong context or damaged ciphertext. */
507
+ declare class DeliveryAuthenticationError extends Error {
508
+ constructor(cause?: unknown);
509
+ }
510
+
511
+ export { type Address, AuthorizedStatement, CreditDataV1, CreditDeliveryState, type CreditOpening, CreditPolicy, CreditState, CreditStateDecryptionContext, CreditUsage, D_CNULL, D_CREDIT, D_CREDIT_KEY_HASH, D_CREDIT_RECOVERY, D_CREDIT_STATE_NONCE, D_IDENT, D_NOTE, D_NULL, DeliveryAuthenticationError, DeliveryDecryptionError, type DeliveryDecryptionResult, type DeliveryDecryptor, type DeliveryDetectionBatch, type DeliveryDetectionCandidate, type DeliveryKind, Detector, Disclosure, type DisclosureExport, type DisclosureOutput, type DisclosureResult, FIELD_MODULUS, FundsDataV1, type Hex, MAX_CIPHERTEXT_BYTES, MAX_WALLET_BATCH_BYTES, MissingCapabilityError, NativeWalletError, Note, NoteBytes, NoteDecryptionContext, NullifierCapability, type PreparedWalletRequest, ProtocolContext, ReceivingAddress, type SigningRequest, TREE_DEPTH, ViewingCapability, WALLET_AUTHORIZATION_TYPES, Wallet, type WalletAcceptedResults, type WalletAuthorizationRequest, type WalletAuthorizationWitness, type WalletBytes, type WalletDecryptContext, type WalletHex, type WalletMethod, type WalletOperation, type WalletParams, type WalletRequest, type WalletResponse, type WalletResults, type WalletWireContext, acceptWalletResponse, acceptWalletResponses, authenticatedDecryptor, computeOwnerAddress, computeWalletAuthorizationDigest, createClue, createClueFromSeed, createSigningRequest, creditV1, decodeDeliveryHeader, deliveryCodec, deliveryHeader, deriveClueKey, fundsV1, generateCreditSharedKey, generateNonce, poseidonHash, prepareWalletRequest, provenanceIntentHash, validateClueKey, validateDeliveryCiphertext, validateWalletBatch, validateWalletResponses, verifyWalletAuthorization, viewingPublicKey, walletAuthorizationTypedData };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  computeNoteNullifier
3
- } from "./chunk-TJYV6KTQ.js";
3
+ } from "./chunk-XM4B6K37.js";
4
4
  import {
5
5
  CREDIT_POLICY_FIELDS,
6
6
  CREDIT_USAGE_FIELDS,
@@ -12,6 +12,7 @@ import {
12
12
  D_IDENT,
13
13
  D_NOTE,
14
14
  D_NULL,
15
+ DeliveryAuthenticationError,
15
16
  FIELD_MODULUS,
16
17
  MAX_CIPHERTEXT_BYTES,
17
18
  TREE_DEPTH,
@@ -49,7 +50,7 @@ import {
49
50
  validateContext,
50
51
  validateDeliveryCiphertext,
51
52
  viewingPublicKey
52
- } from "./chunk-PRUUPDVD.js";
53
+ } from "./chunk-QAC5T4S5.js";
53
54
  import {
54
55
  MissingCapabilityError,
55
56
  ReceivingAddress,
@@ -915,6 +916,17 @@ function authenticatedDecryptor(wallet, decryptor) {
915
916
  const ownerAddress = wallet.ownerAddress;
916
917
  return {
917
918
  ownerAddress,
919
+ async detectCandidates(candidates) {
920
+ if (!decryptor.detect) return candidates.map(() => "candidate");
921
+ const result = await decryptor.detect(structuredClone(candidates));
922
+ if (!result || BigInt(result.ownerAddress) !== BigInt(ownerAddress) || !Array.isArray(result.results) || result.results.length !== candidates.length || result.results.some(
923
+ (value) => !["candidate", "not-recipient", "unresolved"].includes(value)
924
+ ))
925
+ throw new DeliveryDecryptionError(
926
+ new Error("invalid detection batch or wrong account")
927
+ );
928
+ return [...result.results];
929
+ },
918
930
  async decryptNote(envelope, context2) {
919
931
  context2 = { ...context2 };
920
932
  try {
@@ -922,6 +934,11 @@ function authenticatedDecryptor(wallet, decryptor) {
922
934
  const full = { ...context2, ownerAddress };
923
935
  const plaintext = await decryptor.decryptNote(bytes2.slice(), { ...full });
924
936
  if (plaintext === null) return null;
937
+ if (!(plaintext instanceof Uint8Array)) {
938
+ if (plaintext?.status === "authentication-failed")
939
+ throw new DeliveryAuthenticationError();
940
+ throw new Error("invalid decryption result");
941
+ }
925
942
  const codec = deliveryCodec(bytes2[0]);
926
943
  if (codec.kind !== "funds") throw new Error("unexpected envelope kind");
927
944
  const note = codec.decode(checkedPlaintext(plaintext, codec), full);
@@ -930,6 +947,7 @@ function authenticatedDecryptor(wallet, decryptor) {
930
947
  await noteCommitment(full, note);
931
948
  return note;
932
949
  } catch (cause) {
950
+ if (cause instanceof DeliveryAuthenticationError) throw cause;
933
951
  throw new DeliveryDecryptionError(cause);
934
952
  }
935
953
  },
@@ -940,6 +958,11 @@ function authenticatedDecryptor(wallet, decryptor) {
940
958
  const full = { ...context2, ownerAddress };
941
959
  const plaintext = await decryptor.decryptCredit(bytes2.slice(), { ...full });
942
960
  if (plaintext === null) return null;
961
+ if (!(plaintext instanceof Uint8Array)) {
962
+ if (plaintext?.status === "authentication-failed")
963
+ throw new DeliveryAuthenticationError();
964
+ throw new Error("invalid decryption result");
965
+ }
943
966
  const codec = deliveryCodec(bytes2[0]);
944
967
  if (codec.kind !== "credit") throw new Error("unexpected envelope kind");
945
968
  const state = codec.decode(checkedPlaintext(plaintext, codec), full);
@@ -954,6 +977,7 @@ function authenticatedDecryptor(wallet, decryptor) {
954
977
  await creditCommitment(full, state.note);
955
978
  return state;
956
979
  } catch (cause) {
980
+ if (cause instanceof DeliveryAuthenticationError) throw cause;
957
981
  throw new DeliveryDecryptionError(cause);
958
982
  }
959
983
  }
@@ -1242,6 +1266,7 @@ export {
1242
1266
  D_IDENT,
1243
1267
  D_NOTE,
1244
1268
  D_NULL,
1269
+ DeliveryAuthenticationError,
1245
1270
  DeliveryDecryptionError,
1246
1271
  Detector,
1247
1272
  Disclosure,