@invisi-labs/protocol 1.1.1 → 1.1.3

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.cjs CHANGED
@@ -4271,8 +4271,8 @@ function notes(v, name) {
4271
4271
  throw new Error(`duplicate or zero ${name}`);
4272
4272
  return r;
4273
4273
  }
4274
- function envelope(v, length, version) {
4275
- if (!(0, import_ethers4.isHexString)(v) || !/^0x(?:[a-fA-F0-9]{2})*$/.test(v) || (0, import_ethers4.dataLength)(v) !== length)
4274
+ function envelope(v, length, version, maximum = length) {
4275
+ if (!(0, import_ethers4.isHexString)(v) || !/^0x(?:[a-fA-F0-9]{2})*$/.test(v) || (0, import_ethers4.dataLength)(v) < length || (0, import_ethers4.dataLength)(v) > maximum)
4276
4276
  throw new Error(`ciphertext must be ${length} bytes`);
4277
4277
  if (version !== void 0 && Number.parseInt(v.slice(2, 4), 16) !== version)
4278
4278
  throw new Error("unsupported ciphertext envelope version");
@@ -4318,8 +4318,9 @@ function normalizeDelivery(value, options = {}) {
4318
4318
  }
4319
4319
  const ciphertext = envelope(
4320
4320
  value.ciphertext,
4321
- options.credit ? 400 : 158,
4322
- options.credit ? 3 : 1
4321
+ options.credit ? 498 : 160,
4322
+ options.credit ? 5 : 2,
4323
+ options.credit ? 754 : 416
4323
4324
  );
4324
4325
  const clue = envelope(value.clue, 68);
4325
4326
  if (clue.slice(130, 132).toLowerCase() !== "18") throw new Error("clue precision must be 24");
@@ -4545,7 +4546,7 @@ function selectCreditDrawCircuit(count) {
4545
4546
  return {
4546
4547
  name: `credit_draw_slots${maxSize}`,
4547
4548
  maxSize,
4548
- publicInputCount: 6 * maxSize + 12,
4549
+ publicInputCount: 6 * maxSize + 14,
4549
4550
  totalBits: 126 + Math.ceil(Math.log2(maxSize))
4550
4551
  };
4551
4552
  }
@@ -4598,7 +4599,8 @@ function validateCreditDrawRequest(c, p, cs) {
4598
4599
  totalAmount,
4599
4600
  deadline: checkDeadline(c, p.deadline),
4600
4601
  deliveries,
4601
- profile
4602
+ profile,
4603
+ periods: (0, import_core5.creditPeriods)(c.timestamp)
4602
4604
  };
4603
4605
  }
4604
4606
  function creditDrawBaseInputs(c, p, cs) {
@@ -4625,7 +4627,9 @@ function creditDrawBaseInputs(c, p, cs) {
4625
4627
  r.deadline,
4626
4628
  h >> 128n,
4627
4629
  h & (1n << 128n) - 1n,
4628
- ...pad(r.recoveryAmounts.map(BigInt), n)
4630
+ ...pad(r.recoveryAmounts.map(BigInt), n),
4631
+ r.periods.currentDay,
4632
+ r.periods.currentMonth
4629
4633
  ].map(word);
4630
4634
  }
4631
4635
  function validateCancelCreditRequest(c, p) {
@@ -5341,7 +5345,8 @@ async function verifyCreditCommitment(params) {
5341
5345
  params.amount,
5342
5346
  params.nonce,
5343
5347
  params.expiry,
5344
- await (0, import_core8.computeCreditSharedKeyHash)(params.creditSharedKey)
5348
+ await (0, import_core8.computeCreditSharedKeyHash)(params.creditSharedKey),
5349
+ params
5345
5350
  );
5346
5351
  return BigInt(expected) === BigInt(params.creditCommitment);
5347
5352
  }
@@ -5900,9 +5905,9 @@ var witness_layouts_default = {
5900
5905
  ]
5901
5906
  },
5902
5907
  create_credit: {
5903
- artifactSha256: "078c6d2da1ed2d6bd27fab28af9970261fe626a0b22ae56ad170e5d15b1b05ba",
5908
+ artifactSha256: "3ed6cfdee4f5fd840f023580f76c208768777fea79737f2511377e5c55be0230",
5904
5909
  keys: {
5905
- evm: "8285456447e92833a4fa9c330b9c667a3a1b9e48649578082041b9738f037c8f"
5910
+ evm: "56f663c6053be0d158a271022d200c732597efd3a2e0be48fe94549d0b1795e0"
5906
5911
  },
5907
5912
  publicInputCount: 46,
5908
5913
  public: [
@@ -6017,16 +6022,19 @@ var witness_layouts_default = {
6017
6022
  "credit_amount",
6018
6023
  "credit_nonce",
6019
6024
  "credit_shared_key",
6025
+ "single_limit",
6026
+ "daily_limit",
6027
+ "monthly_limit",
6020
6028
  "change_amount",
6021
6029
  "change_nonce"
6022
6030
  ]
6023
6031
  },
6024
6032
  credit_draw_slots1: {
6025
- artifactSha256: "2b9da9d3d1dbd411d9be1bbe2b34305ac5dce7c7d18e3857d8e2be7dd9bf7c21",
6033
+ artifactSha256: "4542b227e0552b45c302f1dace21cadeba4962c96244e021472f1f7531cff8d1",
6026
6034
  keys: {
6027
- evm: "613699a440f50a0937e4c57e5152b5fdea42c31b10157d6b93226fef313df553"
6035
+ evm: "43c051a2bedf9454c12e70b10156361e414944823ba23e80662c16c7ced0a2ce"
6028
6036
  },
6029
- publicInputCount: 18,
6037
+ publicInputCount: 20,
6030
6038
  public: [
6031
6039
  {
6032
6040
  name: "chain_id",
@@ -6108,6 +6116,16 @@ var witness_layouts_default = {
6108
6116
  count: 1,
6109
6117
  array: true
6110
6118
  },
6119
+ {
6120
+ name: "current_day",
6121
+ count: 1,
6122
+ array: false
6123
+ },
6124
+ {
6125
+ name: "current_month",
6126
+ count: 1,
6127
+ array: false
6128
+ },
6111
6129
  {
6112
6130
  name: "auth_digest_high",
6113
6131
  count: 1,
@@ -6132,15 +6150,22 @@ var witness_layouts_default = {
6132
6150
  "leaf_indices",
6133
6151
  "merkle_paths",
6134
6152
  "input_source_indices",
6135
- "draw_amounts"
6153
+ "draw_amounts",
6154
+ "single_limits",
6155
+ "daily_limits",
6156
+ "monthly_limits",
6157
+ "last_days",
6158
+ "daily_spents",
6159
+ "last_months",
6160
+ "monthly_spents"
6136
6161
  ]
6137
6162
  },
6138
6163
  credit_draw_slots12: {
6139
- artifactSha256: "92b741e161a99c05b8810bc24cee6bafc1cebc85347ea47fe511ee6c8f7065be",
6164
+ artifactSha256: "dd72b0867fc3be7cd90de263bd2d0892075cb18eb4f6dd671b13111710f10369",
6140
6165
  keys: {
6141
- evm: "a5ae214784fe6e523977ef4e283a6d217fc3f4ec0c5a40effb275693a74ee5ce"
6166
+ evm: "5e31d771e6294993736078596d9c69f21fe7595eda50e3cce6d7406b65cf1443"
6142
6167
  },
6143
- publicInputCount: 84,
6168
+ publicInputCount: 86,
6144
6169
  public: [
6145
6170
  {
6146
6171
  name: "chain_id",
@@ -6222,6 +6247,16 @@ var witness_layouts_default = {
6222
6247
  count: 12,
6223
6248
  array: true
6224
6249
  },
6250
+ {
6251
+ name: "current_day",
6252
+ count: 1,
6253
+ array: false
6254
+ },
6255
+ {
6256
+ name: "current_month",
6257
+ count: 1,
6258
+ array: false
6259
+ },
6225
6260
  {
6226
6261
  name: "auth_digest_high",
6227
6262
  count: 1,
@@ -6246,15 +6281,22 @@ var witness_layouts_default = {
6246
6281
  "leaf_indices",
6247
6282
  "merkle_paths",
6248
6283
  "input_source_indices",
6249
- "draw_amounts"
6284
+ "draw_amounts",
6285
+ "single_limits",
6286
+ "daily_limits",
6287
+ "monthly_limits",
6288
+ "last_days",
6289
+ "daily_spents",
6290
+ "last_months",
6291
+ "monthly_spents"
6250
6292
  ]
6251
6293
  },
6252
6294
  credit_draw_slots50: {
6253
- artifactSha256: "9c6a23d5dbca3a1aeed461c908f228e89642550775cbc519d633bb1c984a3d04",
6295
+ artifactSha256: "09c7469866748b4c0c4bbf2b040844771948a170999f6dc7a38fe5c0ecda4054",
6254
6296
  keys: {
6255
- evm: "f0b192e2cb312148c7a16fae3acaed9efb01b0c6a1b381ce03468f00b4ef0f2a"
6297
+ evm: "7b613225b6f32a077011d4196c356e836d22dcd116efe687fde53144fbd8db9f"
6256
6298
  },
6257
- publicInputCount: 312,
6299
+ publicInputCount: 314,
6258
6300
  public: [
6259
6301
  {
6260
6302
  name: "chain_id",
@@ -6336,6 +6378,16 @@ var witness_layouts_default = {
6336
6378
  count: 50,
6337
6379
  array: true
6338
6380
  },
6381
+ {
6382
+ name: "current_day",
6383
+ count: 1,
6384
+ array: false
6385
+ },
6386
+ {
6387
+ name: "current_month",
6388
+ count: 1,
6389
+ array: false
6390
+ },
6339
6391
  {
6340
6392
  name: "auth_digest_high",
6341
6393
  count: 1,
@@ -6360,15 +6412,22 @@ var witness_layouts_default = {
6360
6412
  "leaf_indices",
6361
6413
  "merkle_paths",
6362
6414
  "input_source_indices",
6363
- "draw_amounts"
6415
+ "draw_amounts",
6416
+ "single_limits",
6417
+ "daily_limits",
6418
+ "monthly_limits",
6419
+ "last_days",
6420
+ "daily_spents",
6421
+ "last_months",
6422
+ "monthly_spents"
6364
6423
  ]
6365
6424
  },
6366
6425
  credit_draw_slots120: {
6367
- artifactSha256: "69686a4f0a8a98d8cef06485b0de1804ca2e8f47ce0244287a03ceef4d67625a",
6426
+ artifactSha256: "6ccd0366a669c50fd5da875c3eae194fe8aa4c36d35a5f9ebfd64636ea599b48",
6368
6427
  keys: {
6369
- evm: "6f32ca89775f332a9193af9b1e25641ef14fda9fbdd2685c33ee775d3c170b92"
6428
+ evm: "d715848afc5c1ecdfcef5ed5026865abef5040a3bc232fd65330a5ffee8475fb"
6370
6429
  },
6371
- publicInputCount: 732,
6430
+ publicInputCount: 734,
6372
6431
  public: [
6373
6432
  {
6374
6433
  name: "chain_id",
@@ -6450,6 +6509,16 @@ var witness_layouts_default = {
6450
6509
  count: 120,
6451
6510
  array: true
6452
6511
  },
6512
+ {
6513
+ name: "current_day",
6514
+ count: 1,
6515
+ array: false
6516
+ },
6517
+ {
6518
+ name: "current_month",
6519
+ count: 1,
6520
+ array: false
6521
+ },
6453
6522
  {
6454
6523
  name: "auth_digest_high",
6455
6524
  count: 1,
@@ -6474,13 +6543,20 @@ var witness_layouts_default = {
6474
6543
  "leaf_indices",
6475
6544
  "merkle_paths",
6476
6545
  "input_source_indices",
6477
- "draw_amounts"
6546
+ "draw_amounts",
6547
+ "single_limits",
6548
+ "daily_limits",
6549
+ "monthly_limits",
6550
+ "last_days",
6551
+ "daily_spents",
6552
+ "last_months",
6553
+ "monthly_spents"
6478
6554
  ]
6479
6555
  },
6480
6556
  credit_cancel: {
6481
- artifactSha256: "ff278a04f22d2886c9f27f7785a0b8fc317dd242fd4aa9d0d9398d18b7513daf",
6557
+ artifactSha256: "b4459c2cf756d096e37291cf4d7cd129866b6d420b6d03c928f383b057112ccb",
6482
6558
  keys: {
6483
- evm: "1f8a3c20cb081d6a90962556da3124919cc4e5675e59ad31562af39299da02fb"
6559
+ evm: "fc8ad196ef15f3328afcc8f01d867a3ebefd15451c2813948442fab1923b73b2"
6484
6560
  },
6485
6561
  publicInputCount: 11,
6486
6562
  public: [
@@ -6550,14 +6626,21 @@ var witness_layouts_default = {
6550
6626
  "input_nonce",
6551
6627
  "expiry",
6552
6628
  "credit_shared_key",
6629
+ "single_limit",
6630
+ "daily_limit",
6631
+ "monthly_limit",
6632
+ "last_day",
6633
+ "daily_spent",
6634
+ "last_month",
6635
+ "monthly_spent",
6553
6636
  "leaf_index",
6554
6637
  "merkle_path"
6555
6638
  ]
6556
6639
  },
6557
6640
  credit_top_up: {
6558
- artifactSha256: "fbe662363d9814983f7940f74369117cd6c84783945fbf527b0ed48d011b58a4",
6641
+ artifactSha256: "2ffebd5753ec1b0c050204d4f97498765fe61ea01a793a9b2466c2f97536345c",
6559
6642
  keys: {
6560
- evm: "44436f31414497a708355426da051ee48ba63fdebcb6cfd72347c37c34c98c50"
6643
+ evm: "40b02a9d2e7eb7b49daaac4b7760328537b7982fd5a225cc065bb74cd5ef44b3"
6561
6644
  },
6562
6645
  publicInputCount: 50,
6563
6646
  public: [
@@ -6692,6 +6775,16 @@ var witness_layouts_default = {
6692
6775
  "credit_amount",
6693
6776
  "credit_nonce",
6694
6777
  "credit_shared_key",
6778
+ "single_limit",
6779
+ "daily_limit",
6780
+ "monthly_limit",
6781
+ "previous_single_limit",
6782
+ "previous_daily_limit",
6783
+ "previous_monthly_limit",
6784
+ "last_day",
6785
+ "daily_spent",
6786
+ "last_month",
6787
+ "monthly_spent",
6695
6788
  "change_amount",
6696
6789
  "change_nonce",
6697
6790
  "previous_credit_amount",
@@ -6707,6 +6800,20 @@ var pad2 = (values, count, zero) => {
6707
6800
  if (values.length > count) throw new Error("witness capacity exceeded");
6708
6801
  return [...values, ...Array.from({ length: count - values.length }, zero)];
6709
6802
  };
6803
+ var policyWitness = (note, usage = true) => {
6804
+ const p = (0, import_core10.normalizeCreditPolicy)(note);
6805
+ return {
6806
+ single_limit: p.singleLimit,
6807
+ daily_limit: p.dailyLimit,
6808
+ monthly_limit: p.monthlyLimit,
6809
+ ...usage ? {
6810
+ last_day: p.lastDay,
6811
+ daily_spent: p.dailySpent,
6812
+ last_month: p.lastMonth,
6813
+ monthly_spent: p.monthlySpent
6814
+ } : {}
6815
+ };
6816
+ };
6710
6817
  var zeros = () => Array(16).fill(0n);
6711
6818
  var zeroAuthority = () => ({
6712
6819
  public_key_x: Array(32).fill(0),
@@ -6835,7 +6942,8 @@ var WitnessBuilder = class {
6835
6942
  note.amount,
6836
6943
  note.nonce,
6837
6944
  note.expiry,
6838
- await (0, import_core10.computeCreditSharedKeyHash)(note.creditSharedKey)
6945
+ await (0, import_core10.computeCreditSharedKeyHash)(note.creditSharedKey),
6946
+ note
6839
6947
  );
6840
6948
  }
6841
6949
  async noteInputs(inputs, sources, nullifiers, identity, authority) {
@@ -6919,6 +7027,8 @@ var WitnessBuilder = class {
6919
7027
  await this.creditLeaf(note) === p.params.creditCommitment && note.ownerPayout === BigInt(p.params.owner) && note.expiry === p.params.expiry,
6920
7028
  "Credit creation mismatch"
6921
7029
  );
7030
+ if (operation === "create-credit" && [note.lastDay, note.dailySpent, note.lastMonth, note.monthlySpent].some((v) => v !== 0n))
7031
+ throw new Error("new Credit usage must be zero");
6922
7032
  const authority = await this.authority(
6923
7033
  operation,
6924
7034
  10,
@@ -6965,6 +7075,7 @@ var WitnessBuilder = class {
6965
7075
  spender_payout: note.spenderPayout,
6966
7076
  spender_wallet_identity: BigInt(note.spenderWalletIdentity),
6967
7077
  owner_wallet_identity: BigInt(note.ownerWalletIdentity),
7078
+ ...policyWitness(note, operation === "top-up-credit"),
6968
7079
  credit_amount: note.amount,
6969
7080
  credit_nonce: note.nonce,
6970
7081
  credit_shared_key: note.creditSharedKey,
@@ -7006,6 +7117,7 @@ var WitnessBuilder = class {
7006
7117
  spender_payout: note.spenderPayout,
7007
7118
  spender_wallet_identity: BigInt(note.spenderWalletIdentity),
7008
7119
  owner_wallet_identity: BigInt(note.ownerWalletIdentity),
7120
+ ...policyWitness(note),
7009
7121
  input_nonce: note.nonce,
7010
7122
  expiry: note.expiry,
7011
7123
  credit_shared_key: note.creditSharedKey,
@@ -7016,7 +7128,7 @@ var WitnessBuilder = class {
7016
7128
  );
7017
7129
  }
7018
7130
  async drawCredit(p) {
7019
- const c = this.#protocol.context, profile = selectCreditDrawCircuit(p.inputs.length), capacity = profile.maxSize;
7131
+ const c = { ...this.#protocol.context, timestamp: p.timestamp }, profile = selectCreditDrawCircuit(p.inputs.length), capacity = profile.maxSize;
7020
7132
  check2(
7021
7133
  p.inputs.length === p.amounts.length && p.inputs.length === p.params.nullifiers.length,
7022
7134
  "draw input count mismatch"
@@ -7031,6 +7143,7 @@ var WitnessBuilder = class {
7031
7143
  );
7032
7144
  const sourceIndices = await Promise.all(
7033
7145
  notes2.map(async (note, i) => {
7146
+ (0, import_core10.applyCreditDrawUsage)(note, p.amounts[i], p.timestamp);
7034
7147
  const leaf = await this.creditLeaf(note);
7035
7148
  check2(
7036
7149
  note.spenderWalletIdentity === identity && note.spenderPayout === BigInt(p.params.spender) && note.expiry === p.params.expiries[i] && p.amounts[i] > 0n && p.amounts[i] <= note.amount && await (0, import_core10.computeCreditNullifier)(c.chainId, note.creditSharedKey, leaf) === p.params.nullifiers[i],
@@ -7043,6 +7156,26 @@ var WitnessBuilder = class {
7043
7156
  profile.name,
7044
7157
  {
7045
7158
  ...authority,
7159
+ ...Object.fromEntries(
7160
+ Object.entries({
7161
+ singleLimit: "single_limits",
7162
+ dailyLimit: "daily_limits",
7163
+ monthlyLimit: "monthly_limits",
7164
+ lastDay: "last_days",
7165
+ dailySpent: "daily_spents",
7166
+ lastMonth: "last_months",
7167
+ monthlySpent: "monthly_spents"
7168
+ }).map(([key, name]) => [
7169
+ name,
7170
+ pad2(
7171
+ notes2.map(
7172
+ (note) => (0, import_core10.normalizeCreditPolicy)(note)[key]
7173
+ ),
7174
+ capacity,
7175
+ () => 0n
7176
+ )
7177
+ ])
7178
+ ),
7046
7179
  spender_wallet_identity: BigInt(identity),
7047
7180
  owner_payouts: pad2(
7048
7181
  notes2.map((note) => note.ownerPayout),
@@ -7092,7 +7225,11 @@ var WitnessBuilder = class {
7092
7225
  "ownerWalletIdentity",
7093
7226
  "spenderPayout",
7094
7227
  "ownerPayout",
7095
- "creditSharedKey"
7228
+ "creditSharedKey",
7229
+ "lastDay",
7230
+ "dailySpent",
7231
+ "lastMonth",
7232
+ "monthlySpent"
7096
7233
  ])
7097
7234
  check2(old[field2] === note[field2], "Credit role or payout changed");
7098
7235
  check2(
@@ -7113,6 +7250,9 @@ var WitnessBuilder = class {
7113
7250
  "credit_top_up",
7114
7251
  {
7115
7252
  ...base,
7253
+ previous_single_limit: old.singleLimit,
7254
+ previous_daily_limit: old.dailyLimit,
7255
+ previous_monthly_limit: old.monthlyLimit,
7116
7256
  previous_credit_amount: old.amount,
7117
7257
  previous_credit_nonce: old.nonce,
7118
7258
  previous_credit_leaf_index: BigInt(p.previous.leafIndex),