@invisi-labs/primitives 1.3.2 → 1.3.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
@@ -80,9 +80,11 @@ __export(src_exports, {
80
80
  encryptCreditState: () => encryptCreditState,
81
81
  encryptPrivateTransferNote: () => encryptPrivateTransferNote,
82
82
  fundsV1: () => fundsV1,
83
+ generateCreditGrantId: () => generateCreditGrantId,
83
84
  generateCreditSharedKey: () => generateCreditSharedKey,
84
85
  generateNonce: () => generateNonce,
85
86
  normalizeCreditDeliveryState: () => normalizeCreditDeliveryState,
87
+ normalizeCreditGrantId: () => normalizeCreditGrantId,
86
88
  normalizeCreditPolicy: () => normalizeCreditPolicy,
87
89
  normalizeCreditState: () => normalizeCreditState,
88
90
  normalizeCreditUsage: () => normalizeCreditUsage,
@@ -651,6 +653,21 @@ function assertNoteContext(note, context2) {
651
653
  }
652
654
 
653
655
  // src/credit/state.ts
656
+ function normalizeCreditGrantId(value) {
657
+ if (typeof value === "string" && /^[1-9][0-9]*$/.test(value)) value = BigInt(value);
658
+ if (typeof value !== "bigint" || value <= 0n || value >= 1n << 128n)
659
+ throw new RangeError("grantId must be a canonical positive uint128");
660
+ return value;
661
+ }
662
+ function generateCreditGrantId(value) {
663
+ if (value !== void 0) return normalizeCreditGrantId(value);
664
+ const bytes3 = new Uint8Array(16);
665
+ for (; ; ) {
666
+ crypto.getRandomValues(bytes3);
667
+ const candidate = bytes3.reduce((result, byte) => result << 8n | BigInt(byte), 0n);
668
+ if (candidate !== 0n) return candidate;
669
+ }
670
+ }
654
671
  var CREDIT_POLICY_FIELDS = ["singleLimit", "dailyLimit", "monthlyLimit"];
655
672
  var CREDIT_USAGE_FIELDS = [
656
673
  "lastDay",
@@ -743,8 +760,7 @@ function normalizeCreditState(state) {
743
760
  "tokenAddress",
744
761
  "spenderWalletIdentity",
745
762
  "ownerWalletIdentity",
746
- "revocationShard",
747
- "revocationSlot",
763
+ "grantId",
748
764
  "nonce",
749
765
  "expiry",
750
766
  "creditSharedKey",
@@ -760,16 +776,16 @@ function normalizeCreditState(state) {
760
776
  ["chainId", 64],
761
777
  ["protocolAddress", 160],
762
778
  ["tokenAddress", 160],
763
- ["revocationShard", 64],
764
- ["revocationSlot", 16],
779
+ ["grantId", 128],
765
780
  ["expiry", 64]
766
781
  ])
767
782
  uint(state[name], bits, name);
783
+ normalizeCreditGrantId(state.grantId);
768
784
  for (const id of [state.spenderWalletIdentity, state.ownerWalletIdentity])
769
785
  if (!/^0x[0-9a-fA-F]{40}$/.test(id) || BigInt(id) === 0n)
770
786
  throw Error("invalid Credit identity");
771
- if (!state.protocolAddress || !state.tokenAddress || !state.revocationShard || !state.expiry)
772
- throw Error("Credit deployment, token, shard and expiry must be positive");
787
+ if (!state.protocolAddress || !state.tokenAddress || !state.grantId || !state.expiry)
788
+ throw Error("Credit deployment, token, grantId and expiry must be positive");
773
789
  field2(state.nonce, "nonce");
774
790
  field2(state.creditSharedKey, "creditSharedKey");
775
791
  if (!state.creditSharedKey) throw Error("Credit shared key must be positive");
@@ -789,16 +805,16 @@ async function computeCreditCommitment(state, keyHash) {
789
805
  ["chainId", 64],
790
806
  ["protocolAddress", 160],
791
807
  ["tokenAddress", 160],
792
- ["revocationShard", 64],
793
- ["revocationSlot", 16],
808
+ ["grantId", 128],
794
809
  ["expiry", 64]
795
810
  ])
796
811
  uint(state[name], bits, name);
812
+ normalizeCreditGrantId(state.grantId);
797
813
  for (const identity2 of [state.spenderWalletIdentity, state.ownerWalletIdentity])
798
814
  if (!/^0x[0-9a-fA-F]{40}$/.test(identity2) || BigInt(identity2) === 0n)
799
815
  throw new Error("invalid Credit identity");
800
- if (state.revocationShard === 0n || state.expiry === 0n)
801
- throw new Error("Credit shard and expiry must be positive");
816
+ if (state.grantId === 0n || state.expiry === 0n)
817
+ throw new Error("Credit grantId and expiry must be positive");
802
818
  field2(state.nonce, "nonce");
803
819
  const hash3 = keyHash ?? await computeCreditSharedKeyHash(state.creditSharedKey);
804
820
  field2(BigInt(hash3), "Credit shared key hash");
@@ -809,8 +825,7 @@ async function computeCreditCommitment(state, keyHash) {
809
825
  BigInt(state.spenderWalletIdentity),
810
826
  BigInt(state.ownerWalletIdentity),
811
827
  state.tokenAddress,
812
- state.revocationShard,
813
- state.revocationSlot,
828
+ state.grantId,
814
829
  state.nonce,
815
830
  state.expiry,
816
831
  BigInt(hash3),
@@ -1019,8 +1034,7 @@ function normalizeCreditDeliveryState(state) {
1019
1034
  "tokenAddress",
1020
1035
  "spenderWalletIdentity",
1021
1036
  "ownerWalletIdentity",
1022
- "revocationShard",
1023
- "revocationSlot",
1037
+ "grantId",
1024
1038
  "nonce",
1025
1039
  "expiry",
1026
1040
  "creditSharedKey",
@@ -1089,8 +1103,7 @@ var fundsEnvelopeV1 = {
1089
1103
  var creditWireFields = [
1090
1104
  ["spenderWalletIdentity", 32],
1091
1105
  ["ownerWalletIdentity", 32],
1092
- ["revocationShard", 8],
1093
- ["revocationSlot", 2],
1106
+ ["grantId", 16],
1094
1107
  ["nonce", 32],
1095
1108
  ["expiry", 8],
1096
1109
  ["creditSharedKey", 32],
@@ -1105,10 +1118,10 @@ var creditWireFields = [
1105
1118
  ["monthlySpent", 16],
1106
1119
  ["totalSpent", 16]
1107
1120
  ];
1108
- var creditEnvelopeV2 = {
1121
+ var creditEnvelopeV4 = {
1109
1122
  kind: "credit",
1110
- version: 2,
1111
- minimum: 485,
1123
+ version: 4,
1124
+ minimum: 491,
1112
1125
  maximum: 4096,
1113
1126
  encode(input) {
1114
1127
  const state = normalizeCreditDeliveryState(input);
@@ -1157,7 +1170,7 @@ var creditEnvelopeV2 = {
1157
1170
  var MAX_CIPHERTEXT_BYTES = 4096;
1158
1171
  function deliveryHeader(kind) {
1159
1172
  if (kind !== "funds" && kind !== "credit") throw new Error("unsupported delivery kind");
1160
- return kind === "credit" ? 130 : 1;
1173
+ return kind === "credit" ? 132 : 1;
1161
1174
  }
1162
1175
  function decodeDeliveryHeader(header) {
1163
1176
  if (!Number.isInteger(header) || header < 0 || header > 255)
@@ -1169,12 +1182,12 @@ function decodeDeliveryHeader(header) {
1169
1182
  }
1170
1183
  function deliveryCodec(header) {
1171
1184
  const { kind, version } = decodeDeliveryHeader(header);
1172
- if (version !== (kind === "credit" ? 2 : 1))
1185
+ if (version !== (kind === "credit" ? 4 : 1))
1173
1186
  throw new Error(`unsupported envelope version ${version}`);
1174
- const codec = kind === "funds" ? fundsEnvelopeV1 : creditEnvelopeV2;
1187
+ const codec = kind === "funds" ? fundsEnvelopeV1 : creditEnvelopeV4;
1175
1188
  return {
1176
1189
  ...codec,
1177
- domain: `Invisi.${kind === "funds" ? "Note" : "Credit"}Delivery.v${kind === "credit" ? 2 : 1}`
1190
+ domain: kind === "credit" ? "Invisi.CreditDelivery.v4.GrantId.Uint128" : "Invisi.NoteDelivery.v1"
1178
1191
  };
1179
1192
  }
1180
1193
  function validateDeliveryCiphertext(value, kind) {
@@ -1562,8 +1575,7 @@ var NOTE_FIELDS = ["ownerAddress", "amount", "nonce"];
1562
1575
  var CREDIT_FIELDS = [
1563
1576
  "spenderWalletIdentity",
1564
1577
  "ownerWalletIdentity",
1565
- "revocationShard",
1566
- "revocationSlot",
1578
+ "grantId",
1567
1579
  "nonce",
1568
1580
  "expiry",
1569
1581
  "creditSharedKeyHash",
@@ -1636,8 +1648,7 @@ async function creditOpening(note) {
1636
1648
  unlimited: copied.unlimited,
1637
1649
  spenderWalletIdentity: owner(copied.spenderWalletIdentity, "spender identity"),
1638
1650
  ownerWalletIdentity: owner(copied.ownerWalletIdentity, "owner identity"),
1639
- revocationShard: number(copied.revocationShard, 64, "revocation shard"),
1640
- revocationSlot: number(copied.revocationSlot, 16, "revocation slot"),
1651
+ grantId: normalizeCreditGrantId(copied.grantId),
1641
1652
  nonce: nonce(copied.nonce),
1642
1653
  expiry: number(copied.expiry, 64, "expiry"),
1643
1654
  creditSharedKeyHash: await computeCreditSharedKeyHash(copied.creditSharedKey)
@@ -1785,7 +1796,7 @@ var Disclosure = class _Disclosure {
1785
1796
  throw Error("invalid disclosure unlimited");
1786
1797
  data[key] = raw[key];
1787
1798
  } else {
1788
- const bits = key === "availableCredit" ? 128 : key.endsWith("Payout") ? 160 : key === "expiry" ? 64 : key === "amount" ? root.kind === "note" ? 128 : 126 : 254;
1799
+ const bits = key === "availableCredit" || key === "grantId" ? 128 : key.endsWith("Payout") ? 160 : key === "expiry" ? 64 : key === "amount" ? root.kind === "note" ? 128 : 126 : 254;
1789
1800
  const parsed = decimal(raw[key], bits, key);
1790
1801
  if (key === "nonce") nonce(parsed);
1791
1802
  data[key] = parsed.toString();
@@ -1892,8 +1903,7 @@ var Disclosure = class _Disclosure {
1892
1903
  ...scoped,
1893
1904
  spenderWalletIdentity: data.spenderWalletIdentity,
1894
1905
  ownerWalletIdentity: data.ownerWalletIdentity,
1895
- revocationShard: BigInt(data.revocationShard),
1896
- revocationSlot: BigInt(data.revocationSlot),
1906
+ grantId: normalizeCreditGrantId(data.grantId),
1897
1907
  nonce: BigInt(data.nonce),
1898
1908
  expiry: BigInt(data.expiry),
1899
1909
  ...normalizeCreditPolicy(
@@ -2016,6 +2026,19 @@ function walletAuthorizationTypedData(request) {
2016
2026
  for (const value of words)
2017
2027
  if (typeof value !== "bigint" || value < 0n || value >= FIELD_MODULUS)
2018
2028
  throw new RangeError("noncanonical public field");
2029
+ if (operation !== "transact") {
2030
+ const expected = operation === "create-credit" ? 11 : operation === "update-credit" ? 14 : operation === "credit-draw" ? 15 + 9 * capacity : 15;
2031
+ if (words.length !== expected) throw new Error("invalid Credit statement width");
2032
+ const start = operation === "update-credit" ? 8 : operation === "credit-draw" ? 6 + 5 * capacity : 4;
2033
+ const count = operation === "credit-draw" ? capacity : 1;
2034
+ const actual = operation === "credit-draw" ? Number(words[3]) : 1;
2035
+ if (!Number.isSafeInteger(actual) || actual <= 0 || actual > count)
2036
+ throw new Error("invalid grant count");
2037
+ for (let i = 0; i < count; i++) {
2038
+ if (i < actual) normalizeCreditGrantId(words[start + i]);
2039
+ else if (words[start + i] !== 0n) throw new Error("grantId padding must be zero");
2040
+ }
2041
+ }
2019
2042
  const { chainId, protocolAddress } = context2;
2020
2043
  if (typeof chainId !== "bigint" || chainId < 0n || chainId >= 1n << 64n)
2021
2044
  throw new RangeError("invalid chain id");
@@ -2042,7 +2065,7 @@ function walletAuthorizationTypedData(request) {
2042
2065
  operation: hex(
2043
2066
  (0, import_sha32.keccak_256)(
2044
2067
  (0, import_utils4.utf8ToBytes)(
2045
- `Invisi.${OPERATIONS[operation]}.${operation === "transact" ? "v1" : "v4"}`
2068
+ `Invisi.${OPERATIONS[operation]}.${operation === "transact" ? "v1" : "GrantId.Uint128.v1"}`
2046
2069
  )
2047
2070
  )
2048
2071
  ),
@@ -2835,9 +2858,11 @@ function provenanceIntentHash(originalRequestHash) {
2835
2858
  encryptCreditState,
2836
2859
  encryptPrivateTransferNote,
2837
2860
  fundsV1,
2861
+ generateCreditGrantId,
2838
2862
  generateCreditSharedKey,
2839
2863
  generateNonce,
2840
2864
  normalizeCreditDeliveryState,
2865
+ normalizeCreditGrantId,
2841
2866
  normalizeCreditPolicy,
2842
2867
  normalizeCreditState,
2843
2868
  normalizeCreditUsage,