@invisi-labs/primitives 1.2.1 → 1.2.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/dist/address.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  ReceivingAddress,
3
3
  decodeAddress
4
- } from "./chunk-3SEC7POH.js";
4
+ } from "./chunk-C5QNE2WF.js";
5
5
  import "./chunk-Q3NNEKKR.js";
6
6
  export {
7
7
  ReceivingAddress,
@@ -161,8 +161,9 @@ var ReceivingAddress = class _ReceivingAddress {
161
161
  };
162
162
 
163
163
  export {
164
+ encodeLongAddress,
164
165
  decodeAddress,
165
166
  MissingCapabilityError,
166
167
  ReceivingAddress
167
168
  };
168
- //# sourceMappingURL=chunk-3SEC7POH.js.map
169
+ //# sourceMappingURL=chunk-C5QNE2WF.js.map
package/dist/index.cjs CHANGED
@@ -2233,35 +2233,34 @@ var WalletAuthorization = class _WalletAuthorization {
2233
2233
 
2234
2234
  // src/wallet/wallet-object.ts
2235
2235
  var WalletAddress = class {
2236
- #address;
2236
+ #ownerAddress;
2237
+ #receivingKeys;
2237
2238
  constructor(address) {
2238
- this.#address = address;
2239
+ this.#receivingKeys = address.getReceivingKeys();
2240
+ this.#ownerAddress = address.ownerAddress;
2239
2241
  }
2240
2242
  get ownerAddress() {
2241
- return this.#address.ownerAddress;
2243
+ return this.#ownerAddress;
2242
2244
  }
2243
2245
  get viewingPublicKey() {
2244
- return this.#address.viewingPublicKey;
2246
+ return this.#receivingKeys.viewingPublicKey.slice();
2245
2247
  }
2246
2248
  get clueKey() {
2247
- return this.#address.clueKey;
2249
+ return this.#receivingKeys.clueKey.slice();
2248
2250
  }
2249
- /** Complete 0i receiving address. Owner-only wallets must first resolve their receiving keys. */
2251
+ /** Encode the complete public receiving material on demand. */
2250
2252
  getAddress() {
2251
- if (this.#address.kind !== "long") {
2252
- throw new MissingCapabilityError("resolved receiving keys");
2253
- }
2254
- return this.#address.toString();
2253
+ return encodeLongAddress({ ownerAddress: this.#ownerAddress, ...this.#receivingKeys });
2255
2254
  }
2256
2255
  getReceivingAddress() {
2257
- return this.#address;
2256
+ return ReceivingAddress.from({ ownerAddress: this.#ownerAddress, ...this.#receivingKeys });
2258
2257
  }
2259
2258
  /** Nullifiers derive from public identity and do not grant spending authority. */
2260
2259
  exportNullifier() {
2261
2260
  return NullifierCapability.fromIdentity(this.ownerAddress);
2262
2261
  }
2263
2262
  toJSON() {
2264
- return { ownerAddress: this.ownerAddress, address: this.#address.toString() };
2263
+ return { ownerAddress: this.ownerAddress, address: this.getAddress() };
2265
2264
  }
2266
2265
  [/* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom")]() {
2267
2266
  return this.toJSON();
@@ -2272,7 +2271,7 @@ var Wallet = class _Wallet extends WalletAddress {
2272
2271
  super(address);
2273
2272
  Object.freeze(this);
2274
2273
  }
2275
- /** Parse a complete 0i address or an EVM owner address without network access. */
2274
+ /** Parse complete 0i receiving material without network access. */
2276
2275
  static fromAddress(address) {
2277
2276
  if (typeof address !== "string" && !(address instanceof ReceivingAddress)) {
2278
2277
  throw new TypeError("expected a validated receiving address");
@@ -2371,8 +2370,12 @@ function checkedPlaintext(plaintext, codec) {
2371
2370
  throw new Error("invalid plaintext length");
2372
2371
  return plaintext.slice();
2373
2372
  }
2374
- function authenticatedDecryptor(wallet, decryptor) {
2375
- const ownerAddress = wallet.ownerAddress;
2373
+ function authenticatedDecryptor(owner2, decryptor, expectedKeys) {
2374
+ const ownerAddress = ReceivingAddress.fromIdentity(owner2).ownerAddress;
2375
+ const receivingKeys3 = expectedKeys ? {
2376
+ viewingPublicKey: expectedKeys.viewingPublicKey.slice(),
2377
+ clueKey: expectedKeys.clueKey.slice()
2378
+ } : void 0;
2376
2379
  return {
2377
2380
  ownerAddress,
2378
2381
  async detectCandidates(candidates) {
@@ -2428,9 +2431,9 @@ function authenticatedDecryptor(wallet, decryptor) {
2428
2431
  const identity2 = context2.role === "owner" ? state.note.ownerWalletIdentity : state.note.spenderWalletIdentity;
2429
2432
  if (BigInt(identity2) !== BigInt(ownerAddress) || state.note.tokenAddress !== context2.tokenAddress)
2430
2433
  throw new Error("decrypted credit context mismatch");
2431
- if (wallet.getReceivingAddress().kind === "long") {
2434
+ if (receivingKeys3) {
2432
2435
  const keys = context2.role === "owner" ? state.ownerReceivingKeys : state.spenderReceivingKeys;
2433
- if ((0, import_utils6.bytesToHex)(keys.viewingPublicKey) !== (0, import_utils6.bytesToHex)(wallet.viewingPublicKey))
2436
+ if ((0, import_utils6.bytesToHex)(keys.viewingPublicKey) !== (0, import_utils6.bytesToHex)(receivingKeys3.viewingPublicKey))
2434
2437
  throw new Error("decrypted credit receiving key mismatch");
2435
2438
  }
2436
2439
  await creditCommitment(full, state.note);
@@ -2547,17 +2550,24 @@ function validateWalletRequest(request) {
2547
2550
  }
2548
2551
  var retainedWallets = /* @__PURE__ */ new WeakMap();
2549
2552
  function prepareWalletRequest(wallet, id, method, params) {
2550
- if (!(wallet instanceof Wallet)) throw new TypeError("public Wallet required");
2553
+ if (!(wallet instanceof Wallet) && typeof wallet !== "string")
2554
+ throw new TypeError("ownerAddress or public Wallet required");
2555
+ const ownerAddress = ReceivingAddress.fromIdentity(
2556
+ typeof wallet === "string" ? wallet : wallet.ownerAddress
2557
+ ).ownerAddress;
2551
2558
  const request = structuredClone({
2552
2559
  version: 1,
2553
2560
  id,
2554
- ownerAddress: wallet.ownerAddress.toLowerCase(),
2561
+ ownerAddress,
2555
2562
  method,
2556
2563
  params
2557
2564
  });
2558
2565
  validateWalletRequest(request);
2559
2566
  const prepared = freeze({ request });
2560
- retainedWallets.set(prepared, wallet);
2567
+ retainedWallets.set(
2568
+ prepared,
2569
+ wallet instanceof Wallet ? { ownerAddress, viewingPublicKey: wallet.viewingPublicKey, clueKey: wallet.clueKey } : { ownerAddress }
2570
+ );
2561
2571
  return prepared;
2562
2572
  }
2563
2573
  async function acceptWalletResponse(prepared, response) {
@@ -2584,7 +2594,7 @@ async function acceptResponse(prepared, response) {
2584
2594
  walletBytes(result.clueKey, 32);
2585
2595
  if (await computeOwnerAddress(result.spendingPublicKey) !== request.ownerAddress)
2586
2596
  throw new Error("wallet public identity mismatch");
2587
- if (wallet.getReceivingAddress().kind === "long" && (walletHex(wallet.viewingPublicKey) !== result.viewingPublicKey || walletHex(wallet.clueKey) !== result.clueKey))
2597
+ if (wallet.viewingPublicKey && wallet.clueKey && (walletHex(wallet.viewingPublicKey) !== result.viewingPublicKey || walletHex(wallet.clueKey) !== result.clueKey))
2588
2598
  throw new Error("wallet receiving key mismatch");
2589
2599
  ReceivingAddress.from({
2590
2600
  ownerAddress: request.ownerAddress,
@@ -2613,10 +2623,14 @@ async function acceptResponse(prepared, response) {
2613
2623
  const typed = request;
2614
2624
  const context2 = walletDecryptionContext(typed);
2615
2625
  const plaintext = walletBytes(result.plaintext);
2616
- const viewing = authenticatedDecryptor(wallet, {
2617
- decryptNote: async () => plaintext,
2618
- decryptCredit: async () => plaintext
2619
- });
2626
+ const viewing = authenticatedDecryptor(
2627
+ request.ownerAddress,
2628
+ {
2629
+ decryptNote: async () => plaintext,
2630
+ decryptCredit: async () => plaintext
2631
+ },
2632
+ wallet.viewingPublicKey && wallet.clueKey ? { viewingPublicKey: wallet.viewingPublicKey, clueKey: wallet.clueKey } : void 0
2633
+ );
2620
2634
  return typed.method === "wallet.decryptNote" ? viewing.decryptNote(typed.params.envelope, context2) : viewing.decryptCredit(typed.params.envelope, context2);
2621
2635
  }
2622
2636
  return result;