@invisi-labs/primitives 1.3.2 → 1.3.4

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");
@@ -777,6 +793,9 @@ function normalizeCreditState(state) {
777
793
  throw TypeError("payload must be Uint8Array");
778
794
  return {
779
795
  ...state,
796
+ // Compare and group EVM identities by their address value, independent of hex casing.
797
+ ownerWalletIdentity: state.ownerWalletIdentity.toLowerCase(),
798
+ spenderWalletIdentity: state.spenderWalletIdentity.toLowerCase(),
780
799
  ...normalizeCreditPolicy(state),
781
800
  ...normalizeCreditUsage(state),
782
801
  ...balance(state),
@@ -789,16 +808,16 @@ async function computeCreditCommitment(state, keyHash) {
789
808
  ["chainId", 64],
790
809
  ["protocolAddress", 160],
791
810
  ["tokenAddress", 160],
792
- ["revocationShard", 64],
793
- ["revocationSlot", 16],
811
+ ["grantId", 128],
794
812
  ["expiry", 64]
795
813
  ])
796
814
  uint(state[name], bits, name);
815
+ normalizeCreditGrantId(state.grantId);
797
816
  for (const identity2 of [state.spenderWalletIdentity, state.ownerWalletIdentity])
798
817
  if (!/^0x[0-9a-fA-F]{40}$/.test(identity2) || BigInt(identity2) === 0n)
799
818
  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");
819
+ if (state.grantId === 0n || state.expiry === 0n)
820
+ throw new Error("Credit grantId and expiry must be positive");
802
821
  field2(state.nonce, "nonce");
803
822
  const hash3 = keyHash ?? await computeCreditSharedKeyHash(state.creditSharedKey);
804
823
  field2(BigInt(hash3), "Credit shared key hash");
@@ -809,8 +828,7 @@ async function computeCreditCommitment(state, keyHash) {
809
828
  BigInt(state.spenderWalletIdentity),
810
829
  BigInt(state.ownerWalletIdentity),
811
830
  state.tokenAddress,
812
- state.revocationShard,
813
- state.revocationSlot,
831
+ state.grantId,
814
832
  state.nonce,
815
833
  state.expiry,
816
834
  BigInt(hash3),
@@ -1019,8 +1037,7 @@ function normalizeCreditDeliveryState(state) {
1019
1037
  "tokenAddress",
1020
1038
  "spenderWalletIdentity",
1021
1039
  "ownerWalletIdentity",
1022
- "revocationShard",
1023
- "revocationSlot",
1040
+ "grantId",
1024
1041
  "nonce",
1025
1042
  "expiry",
1026
1043
  "creditSharedKey",
@@ -1089,8 +1106,7 @@ var fundsEnvelopeV1 = {
1089
1106
  var creditWireFields = [
1090
1107
  ["spenderWalletIdentity", 32],
1091
1108
  ["ownerWalletIdentity", 32],
1092
- ["revocationShard", 8],
1093
- ["revocationSlot", 2],
1109
+ ["grantId", 16],
1094
1110
  ["nonce", 32],
1095
1111
  ["expiry", 8],
1096
1112
  ["creditSharedKey", 32],
@@ -1105,10 +1121,10 @@ var creditWireFields = [
1105
1121
  ["monthlySpent", 16],
1106
1122
  ["totalSpent", 16]
1107
1123
  ];
1108
- var creditEnvelopeV2 = {
1124
+ var creditEnvelopeV4 = {
1109
1125
  kind: "credit",
1110
- version: 2,
1111
- minimum: 485,
1126
+ version: 4,
1127
+ minimum: 491,
1112
1128
  maximum: 4096,
1113
1129
  encode(input) {
1114
1130
  const state = normalizeCreditDeliveryState(input);
@@ -1157,7 +1173,7 @@ var creditEnvelopeV2 = {
1157
1173
  var MAX_CIPHERTEXT_BYTES = 4096;
1158
1174
  function deliveryHeader(kind) {
1159
1175
  if (kind !== "funds" && kind !== "credit") throw new Error("unsupported delivery kind");
1160
- return kind === "credit" ? 130 : 1;
1176
+ return kind === "credit" ? 132 : 1;
1161
1177
  }
1162
1178
  function decodeDeliveryHeader(header) {
1163
1179
  if (!Number.isInteger(header) || header < 0 || header > 255)
@@ -1169,12 +1185,12 @@ function decodeDeliveryHeader(header) {
1169
1185
  }
1170
1186
  function deliveryCodec(header) {
1171
1187
  const { kind, version } = decodeDeliveryHeader(header);
1172
- if (version !== (kind === "credit" ? 2 : 1))
1188
+ if (version !== (kind === "credit" ? 4 : 1))
1173
1189
  throw new Error(`unsupported envelope version ${version}`);
1174
- const codec = kind === "funds" ? fundsEnvelopeV1 : creditEnvelopeV2;
1190
+ const codec = kind === "funds" ? fundsEnvelopeV1 : creditEnvelopeV4;
1175
1191
  return {
1176
1192
  ...codec,
1177
- domain: `Invisi.${kind === "funds" ? "Note" : "Credit"}Delivery.v${kind === "credit" ? 2 : 1}`
1193
+ domain: kind === "credit" ? "Invisi.CreditDelivery.v4.GrantId.Uint128" : "Invisi.NoteDelivery.v1"
1178
1194
  };
1179
1195
  }
1180
1196
  function validateDeliveryCiphertext(value, kind) {
@@ -1562,8 +1578,7 @@ var NOTE_FIELDS = ["ownerAddress", "amount", "nonce"];
1562
1578
  var CREDIT_FIELDS = [
1563
1579
  "spenderWalletIdentity",
1564
1580
  "ownerWalletIdentity",
1565
- "revocationShard",
1566
- "revocationSlot",
1581
+ "grantId",
1567
1582
  "nonce",
1568
1583
  "expiry",
1569
1584
  "creditSharedKeyHash",
@@ -1636,8 +1651,7 @@ async function creditOpening(note) {
1636
1651
  unlimited: copied.unlimited,
1637
1652
  spenderWalletIdentity: owner(copied.spenderWalletIdentity, "spender identity"),
1638
1653
  ownerWalletIdentity: owner(copied.ownerWalletIdentity, "owner identity"),
1639
- revocationShard: number(copied.revocationShard, 64, "revocation shard"),
1640
- revocationSlot: number(copied.revocationSlot, 16, "revocation slot"),
1654
+ grantId: normalizeCreditGrantId(copied.grantId),
1641
1655
  nonce: nonce(copied.nonce),
1642
1656
  expiry: number(copied.expiry, 64, "expiry"),
1643
1657
  creditSharedKeyHash: await computeCreditSharedKeyHash(copied.creditSharedKey)
@@ -1785,7 +1799,7 @@ var Disclosure = class _Disclosure {
1785
1799
  throw Error("invalid disclosure unlimited");
1786
1800
  data[key] = raw[key];
1787
1801
  } else {
1788
- const bits = key === "availableCredit" ? 128 : key.endsWith("Payout") ? 160 : key === "expiry" ? 64 : key === "amount" ? root.kind === "note" ? 128 : 126 : 254;
1802
+ const bits = key === "availableCredit" || key === "grantId" ? 128 : key.endsWith("Payout") ? 160 : key === "expiry" ? 64 : key === "amount" ? root.kind === "note" ? 128 : 126 : 254;
1789
1803
  const parsed = decimal(raw[key], bits, key);
1790
1804
  if (key === "nonce") nonce(parsed);
1791
1805
  data[key] = parsed.toString();
@@ -1892,8 +1906,7 @@ var Disclosure = class _Disclosure {
1892
1906
  ...scoped,
1893
1907
  spenderWalletIdentity: data.spenderWalletIdentity,
1894
1908
  ownerWalletIdentity: data.ownerWalletIdentity,
1895
- revocationShard: BigInt(data.revocationShard),
1896
- revocationSlot: BigInt(data.revocationSlot),
1909
+ grantId: normalizeCreditGrantId(data.grantId),
1897
1910
  nonce: BigInt(data.nonce),
1898
1911
  expiry: BigInt(data.expiry),
1899
1912
  ...normalizeCreditPolicy(
@@ -2016,6 +2029,19 @@ function walletAuthorizationTypedData(request) {
2016
2029
  for (const value of words)
2017
2030
  if (typeof value !== "bigint" || value < 0n || value >= FIELD_MODULUS)
2018
2031
  throw new RangeError("noncanonical public field");
2032
+ if (operation !== "transact") {
2033
+ const expected = operation === "create-credit" ? 11 : operation === "update-credit" ? 14 : operation === "credit-draw" ? 15 + 9 * capacity : 15;
2034
+ if (words.length !== expected) throw new Error("invalid Credit statement width");
2035
+ const start = operation === "update-credit" ? 8 : operation === "credit-draw" ? 6 + 5 * capacity : 4;
2036
+ const count = operation === "credit-draw" ? capacity : 1;
2037
+ const actual = operation === "credit-draw" ? Number(words[3]) : 1;
2038
+ if (!Number.isSafeInteger(actual) || actual <= 0 || actual > count)
2039
+ throw new Error("invalid grant count");
2040
+ for (let i = 0; i < count; i++) {
2041
+ if (i < actual) normalizeCreditGrantId(words[start + i]);
2042
+ else if (words[start + i] !== 0n) throw new Error("grantId padding must be zero");
2043
+ }
2044
+ }
2019
2045
  const { chainId, protocolAddress } = context2;
2020
2046
  if (typeof chainId !== "bigint" || chainId < 0n || chainId >= 1n << 64n)
2021
2047
  throw new RangeError("invalid chain id");
@@ -2042,7 +2068,7 @@ function walletAuthorizationTypedData(request) {
2042
2068
  operation: hex(
2043
2069
  (0, import_sha32.keccak_256)(
2044
2070
  (0, import_utils4.utf8ToBytes)(
2045
- `Invisi.${OPERATIONS[operation]}.${operation === "transact" ? "v1" : "v4"}`
2071
+ `Invisi.${OPERATIONS[operation]}.${operation === "transact" ? "v1" : "GrantId.Uint128.v1"}`
2046
2072
  )
2047
2073
  )
2048
2074
  ),
@@ -2835,9 +2861,11 @@ function provenanceIntentHash(originalRequestHash) {
2835
2861
  encryptCreditState,
2836
2862
  encryptPrivateTransferNote,
2837
2863
  fundsV1,
2864
+ generateCreditGrantId,
2838
2865
  generateCreditSharedKey,
2839
2866
  generateNonce,
2840
2867
  normalizeCreditDeliveryState,
2868
+ normalizeCreditGrantId,
2841
2869
  normalizeCreditPolicy,
2842
2870
  normalizeCreditState,
2843
2871
  normalizeCreditUsage,