@invisi-labs/protocol 1.1.2 → 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
@@ -4318,9 +4318,9 @@ function normalizeDelivery(value, options = {}) {
4318
4318
  }
4319
4319
  const ciphertext = envelope(
4320
4320
  value.ciphertext,
4321
- options.credit ? 402 : 160,
4322
- options.credit ? 4 : 2,
4323
- options.credit ? 658 : 416
4321
+ options.credit ? 498 : 160,
4322
+ options.credit ? 5 : 2,
4323
+ options.credit ? 754 : 416
4324
4324
  );
4325
4325
  const clue = envelope(value.clue, 68);
4326
4326
  if (clue.slice(130, 132).toLowerCase() !== "18") throw new Error("clue precision must be 24");
@@ -4546,7 +4546,7 @@ function selectCreditDrawCircuit(count) {
4546
4546
  return {
4547
4547
  name: `credit_draw_slots${maxSize}`,
4548
4548
  maxSize,
4549
- publicInputCount: 6 * maxSize + 12,
4549
+ publicInputCount: 6 * maxSize + 14,
4550
4550
  totalBits: 126 + Math.ceil(Math.log2(maxSize))
4551
4551
  };
4552
4552
  }
@@ -4599,7 +4599,8 @@ function validateCreditDrawRequest(c, p, cs) {
4599
4599
  totalAmount,
4600
4600
  deadline: checkDeadline(c, p.deadline),
4601
4601
  deliveries,
4602
- profile
4602
+ profile,
4603
+ periods: (0, import_core5.creditPeriods)(c.timestamp)
4603
4604
  };
4604
4605
  }
4605
4606
  function creditDrawBaseInputs(c, p, cs) {
@@ -4626,7 +4627,9 @@ function creditDrawBaseInputs(c, p, cs) {
4626
4627
  r.deadline,
4627
4628
  h >> 128n,
4628
4629
  h & (1n << 128n) - 1n,
4629
- ...pad(r.recoveryAmounts.map(BigInt), n)
4630
+ ...pad(r.recoveryAmounts.map(BigInt), n),
4631
+ r.periods.currentDay,
4632
+ r.periods.currentMonth
4630
4633
  ].map(word);
4631
4634
  }
4632
4635
  function validateCancelCreditRequest(c, p) {
@@ -5342,7 +5345,8 @@ async function verifyCreditCommitment(params) {
5342
5345
  params.amount,
5343
5346
  params.nonce,
5344
5347
  params.expiry,
5345
- await (0, import_core8.computeCreditSharedKeyHash)(params.creditSharedKey)
5348
+ await (0, import_core8.computeCreditSharedKeyHash)(params.creditSharedKey),
5349
+ params
5346
5350
  );
5347
5351
  return BigInt(expected) === BigInt(params.creditCommitment);
5348
5352
  }
@@ -5901,9 +5905,9 @@ var witness_layouts_default = {
5901
5905
  ]
5902
5906
  },
5903
5907
  create_credit: {
5904
- artifactSha256: "078c6d2da1ed2d6bd27fab28af9970261fe626a0b22ae56ad170e5d15b1b05ba",
5908
+ artifactSha256: "3ed6cfdee4f5fd840f023580f76c208768777fea79737f2511377e5c55be0230",
5905
5909
  keys: {
5906
- evm: "8285456447e92833a4fa9c330b9c667a3a1b9e48649578082041b9738f037c8f"
5910
+ evm: "56f663c6053be0d158a271022d200c732597efd3a2e0be48fe94549d0b1795e0"
5907
5911
  },
5908
5912
  publicInputCount: 46,
5909
5913
  public: [
@@ -6018,16 +6022,19 @@ var witness_layouts_default = {
6018
6022
  "credit_amount",
6019
6023
  "credit_nonce",
6020
6024
  "credit_shared_key",
6025
+ "single_limit",
6026
+ "daily_limit",
6027
+ "monthly_limit",
6021
6028
  "change_amount",
6022
6029
  "change_nonce"
6023
6030
  ]
6024
6031
  },
6025
6032
  credit_draw_slots1: {
6026
- artifactSha256: "2b9da9d3d1dbd411d9be1bbe2b34305ac5dce7c7d18e3857d8e2be7dd9bf7c21",
6033
+ artifactSha256: "4542b227e0552b45c302f1dace21cadeba4962c96244e021472f1f7531cff8d1",
6027
6034
  keys: {
6028
- evm: "613699a440f50a0937e4c57e5152b5fdea42c31b10157d6b93226fef313df553"
6035
+ evm: "43c051a2bedf9454c12e70b10156361e414944823ba23e80662c16c7ced0a2ce"
6029
6036
  },
6030
- publicInputCount: 18,
6037
+ publicInputCount: 20,
6031
6038
  public: [
6032
6039
  {
6033
6040
  name: "chain_id",
@@ -6109,6 +6116,16 @@ var witness_layouts_default = {
6109
6116
  count: 1,
6110
6117
  array: true
6111
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
+ },
6112
6129
  {
6113
6130
  name: "auth_digest_high",
6114
6131
  count: 1,
@@ -6133,15 +6150,22 @@ var witness_layouts_default = {
6133
6150
  "leaf_indices",
6134
6151
  "merkle_paths",
6135
6152
  "input_source_indices",
6136
- "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"
6137
6161
  ]
6138
6162
  },
6139
6163
  credit_draw_slots12: {
6140
- artifactSha256: "92b741e161a99c05b8810bc24cee6bafc1cebc85347ea47fe511ee6c8f7065be",
6164
+ artifactSha256: "dd72b0867fc3be7cd90de263bd2d0892075cb18eb4f6dd671b13111710f10369",
6141
6165
  keys: {
6142
- evm: "a5ae214784fe6e523977ef4e283a6d217fc3f4ec0c5a40effb275693a74ee5ce"
6166
+ evm: "5e31d771e6294993736078596d9c69f21fe7595eda50e3cce6d7406b65cf1443"
6143
6167
  },
6144
- publicInputCount: 84,
6168
+ publicInputCount: 86,
6145
6169
  public: [
6146
6170
  {
6147
6171
  name: "chain_id",
@@ -6223,6 +6247,16 @@ var witness_layouts_default = {
6223
6247
  count: 12,
6224
6248
  array: true
6225
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
+ },
6226
6260
  {
6227
6261
  name: "auth_digest_high",
6228
6262
  count: 1,
@@ -6247,15 +6281,22 @@ var witness_layouts_default = {
6247
6281
  "leaf_indices",
6248
6282
  "merkle_paths",
6249
6283
  "input_source_indices",
6250
- "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"
6251
6292
  ]
6252
6293
  },
6253
6294
  credit_draw_slots50: {
6254
- artifactSha256: "9c6a23d5dbca3a1aeed461c908f228e89642550775cbc519d633bb1c984a3d04",
6295
+ artifactSha256: "09c7469866748b4c0c4bbf2b040844771948a170999f6dc7a38fe5c0ecda4054",
6255
6296
  keys: {
6256
- evm: "f0b192e2cb312148c7a16fae3acaed9efb01b0c6a1b381ce03468f00b4ef0f2a"
6297
+ evm: "7b613225b6f32a077011d4196c356e836d22dcd116efe687fde53144fbd8db9f"
6257
6298
  },
6258
- publicInputCount: 312,
6299
+ publicInputCount: 314,
6259
6300
  public: [
6260
6301
  {
6261
6302
  name: "chain_id",
@@ -6337,6 +6378,16 @@ var witness_layouts_default = {
6337
6378
  count: 50,
6338
6379
  array: true
6339
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
+ },
6340
6391
  {
6341
6392
  name: "auth_digest_high",
6342
6393
  count: 1,
@@ -6361,15 +6412,22 @@ var witness_layouts_default = {
6361
6412
  "leaf_indices",
6362
6413
  "merkle_paths",
6363
6414
  "input_source_indices",
6364
- "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"
6365
6423
  ]
6366
6424
  },
6367
6425
  credit_draw_slots120: {
6368
- artifactSha256: "69686a4f0a8a98d8cef06485b0de1804ca2e8f47ce0244287a03ceef4d67625a",
6426
+ artifactSha256: "6ccd0366a669c50fd5da875c3eae194fe8aa4c36d35a5f9ebfd64636ea599b48",
6369
6427
  keys: {
6370
- evm: "6f32ca89775f332a9193af9b1e25641ef14fda9fbdd2685c33ee775d3c170b92"
6428
+ evm: "d715848afc5c1ecdfcef5ed5026865abef5040a3bc232fd65330a5ffee8475fb"
6371
6429
  },
6372
- publicInputCount: 732,
6430
+ publicInputCount: 734,
6373
6431
  public: [
6374
6432
  {
6375
6433
  name: "chain_id",
@@ -6451,6 +6509,16 @@ var witness_layouts_default = {
6451
6509
  count: 120,
6452
6510
  array: true
6453
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
+ },
6454
6522
  {
6455
6523
  name: "auth_digest_high",
6456
6524
  count: 1,
@@ -6475,13 +6543,20 @@ var witness_layouts_default = {
6475
6543
  "leaf_indices",
6476
6544
  "merkle_paths",
6477
6545
  "input_source_indices",
6478
- "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"
6479
6554
  ]
6480
6555
  },
6481
6556
  credit_cancel: {
6482
- artifactSha256: "ff278a04f22d2886c9f27f7785a0b8fc317dd242fd4aa9d0d9398d18b7513daf",
6557
+ artifactSha256: "b4459c2cf756d096e37291cf4d7cd129866b6d420b6d03c928f383b057112ccb",
6483
6558
  keys: {
6484
- evm: "1f8a3c20cb081d6a90962556da3124919cc4e5675e59ad31562af39299da02fb"
6559
+ evm: "fc8ad196ef15f3328afcc8f01d867a3ebefd15451c2813948442fab1923b73b2"
6485
6560
  },
6486
6561
  publicInputCount: 11,
6487
6562
  public: [
@@ -6551,14 +6626,21 @@ var witness_layouts_default = {
6551
6626
  "input_nonce",
6552
6627
  "expiry",
6553
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",
6554
6636
  "leaf_index",
6555
6637
  "merkle_path"
6556
6638
  ]
6557
6639
  },
6558
6640
  credit_top_up: {
6559
- artifactSha256: "fbe662363d9814983f7940f74369117cd6c84783945fbf527b0ed48d011b58a4",
6641
+ artifactSha256: "2ffebd5753ec1b0c050204d4f97498765fe61ea01a793a9b2466c2f97536345c",
6560
6642
  keys: {
6561
- evm: "44436f31414497a708355426da051ee48ba63fdebcb6cfd72347c37c34c98c50"
6643
+ evm: "40b02a9d2e7eb7b49daaac4b7760328537b7982fd5a225cc065bb74cd5ef44b3"
6562
6644
  },
6563
6645
  publicInputCount: 50,
6564
6646
  public: [
@@ -6693,6 +6775,16 @@ var witness_layouts_default = {
6693
6775
  "credit_amount",
6694
6776
  "credit_nonce",
6695
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",
6696
6788
  "change_amount",
6697
6789
  "change_nonce",
6698
6790
  "previous_credit_amount",
@@ -6708,6 +6800,20 @@ var pad2 = (values, count, zero) => {
6708
6800
  if (values.length > count) throw new Error("witness capacity exceeded");
6709
6801
  return [...values, ...Array.from({ length: count - values.length }, zero)];
6710
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
+ };
6711
6817
  var zeros = () => Array(16).fill(0n);
6712
6818
  var zeroAuthority = () => ({
6713
6819
  public_key_x: Array(32).fill(0),
@@ -6836,7 +6942,8 @@ var WitnessBuilder = class {
6836
6942
  note.amount,
6837
6943
  note.nonce,
6838
6944
  note.expiry,
6839
- await (0, import_core10.computeCreditSharedKeyHash)(note.creditSharedKey)
6945
+ await (0, import_core10.computeCreditSharedKeyHash)(note.creditSharedKey),
6946
+ note
6840
6947
  );
6841
6948
  }
6842
6949
  async noteInputs(inputs, sources, nullifiers, identity, authority) {
@@ -6920,6 +7027,8 @@ var WitnessBuilder = class {
6920
7027
  await this.creditLeaf(note) === p.params.creditCommitment && note.ownerPayout === BigInt(p.params.owner) && note.expiry === p.params.expiry,
6921
7028
  "Credit creation mismatch"
6922
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");
6923
7032
  const authority = await this.authority(
6924
7033
  operation,
6925
7034
  10,
@@ -6966,6 +7075,7 @@ var WitnessBuilder = class {
6966
7075
  spender_payout: note.spenderPayout,
6967
7076
  spender_wallet_identity: BigInt(note.spenderWalletIdentity),
6968
7077
  owner_wallet_identity: BigInt(note.ownerWalletIdentity),
7078
+ ...policyWitness(note, operation === "top-up-credit"),
6969
7079
  credit_amount: note.amount,
6970
7080
  credit_nonce: note.nonce,
6971
7081
  credit_shared_key: note.creditSharedKey,
@@ -7007,6 +7117,7 @@ var WitnessBuilder = class {
7007
7117
  spender_payout: note.spenderPayout,
7008
7118
  spender_wallet_identity: BigInt(note.spenderWalletIdentity),
7009
7119
  owner_wallet_identity: BigInt(note.ownerWalletIdentity),
7120
+ ...policyWitness(note),
7010
7121
  input_nonce: note.nonce,
7011
7122
  expiry: note.expiry,
7012
7123
  credit_shared_key: note.creditSharedKey,
@@ -7017,7 +7128,7 @@ var WitnessBuilder = class {
7017
7128
  );
7018
7129
  }
7019
7130
  async drawCredit(p) {
7020
- 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;
7021
7132
  check2(
7022
7133
  p.inputs.length === p.amounts.length && p.inputs.length === p.params.nullifiers.length,
7023
7134
  "draw input count mismatch"
@@ -7032,6 +7143,7 @@ var WitnessBuilder = class {
7032
7143
  );
7033
7144
  const sourceIndices = await Promise.all(
7034
7145
  notes2.map(async (note, i) => {
7146
+ (0, import_core10.applyCreditDrawUsage)(note, p.amounts[i], p.timestamp);
7035
7147
  const leaf = await this.creditLeaf(note);
7036
7148
  check2(
7037
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],
@@ -7044,6 +7156,26 @@ var WitnessBuilder = class {
7044
7156
  profile.name,
7045
7157
  {
7046
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
+ ),
7047
7179
  spender_wallet_identity: BigInt(identity),
7048
7180
  owner_payouts: pad2(
7049
7181
  notes2.map((note) => note.ownerPayout),
@@ -7093,7 +7225,11 @@ var WitnessBuilder = class {
7093
7225
  "ownerWalletIdentity",
7094
7226
  "spenderPayout",
7095
7227
  "ownerPayout",
7096
- "creditSharedKey"
7228
+ "creditSharedKey",
7229
+ "lastDay",
7230
+ "dailySpent",
7231
+ "lastMonth",
7232
+ "monthlySpent"
7097
7233
  ])
7098
7234
  check2(old[field2] === note[field2], "Credit role or payout changed");
7099
7235
  check2(
@@ -7114,6 +7250,9 @@ var WitnessBuilder = class {
7114
7250
  "credit_top_up",
7115
7251
  {
7116
7252
  ...base,
7253
+ previous_single_limit: old.singleLimit,
7254
+ previous_daily_limit: old.dailyLimit,
7255
+ previous_monthly_limit: old.monthlyLimit,
7117
7256
  previous_credit_amount: old.amount,
7118
7257
  previous_credit_nonce: old.nonce,
7119
7258
  previous_credit_leaf_index: BigInt(p.previous.leafIndex),