@invisi-labs/protocol 1.1.3 → 1.1.5
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 +37 -80
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +12 -20
- package/dist/index.d.ts +12 -20
- package/dist/index.js +25 -71
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -43,7 +43,6 @@ __export(src_exports, {
|
|
|
43
43
|
buildTopUpCreditCalldata: () => buildTopUpCreditCalldata,
|
|
44
44
|
buildTransactAggregateCalldata: () => buildTransactAggregateCalldata,
|
|
45
45
|
buildTransactCalldata: () => buildTransactCalldata,
|
|
46
|
-
buildWithdrawalCalldata: () => buildWithdrawalCalldata,
|
|
47
46
|
cancelCreditBaseInputs: () => cancelCreditBaseInputs,
|
|
48
47
|
cancelCreditPublicInputs: () => cancelCreditPublicInputs,
|
|
49
48
|
cancelCreditRequestHash: () => cancelCreditRequestHash,
|
|
@@ -73,8 +72,8 @@ __export(src_exports, {
|
|
|
73
72
|
getEIP712Domain: () => getEIP712Domain,
|
|
74
73
|
known: () => known,
|
|
75
74
|
normalizeContext: () => normalizeContext,
|
|
76
|
-
normalizeDelivery: () => normalizeDelivery,
|
|
77
75
|
normalizeFunding: () => normalizeFunding,
|
|
76
|
+
normalizeOpaqueDelivery: () => normalizeOpaqueDelivery,
|
|
78
77
|
normalizeSources: () => normalizeSources,
|
|
79
78
|
normalizeTransactRequest: () => normalizeTransactRequest,
|
|
80
79
|
notes: () => notes,
|
|
@@ -3943,27 +3942,20 @@ var RegistrationOperation = class _RegistrationOperation {
|
|
|
3943
3942
|
update: this.#plan.update
|
|
3944
3943
|
};
|
|
3945
3944
|
}
|
|
3946
|
-
/**
|
|
3947
|
-
async
|
|
3945
|
+
/** Export portable EIP-712 data for external signing. */
|
|
3946
|
+
async exportRegistrationAuthorization(options = {}) {
|
|
3947
|
+
check(this.status === "prepared", "registry operation is not awaiting authorization");
|
|
3948
|
+
options.signal?.throwIfAborted();
|
|
3949
|
+
await this.validateFreshness();
|
|
3950
|
+
options.signal?.throwIfAborted();
|
|
3951
|
+
return (0, import_core2.createSigningRequest)(this.typedData());
|
|
3952
|
+
}
|
|
3953
|
+
/** Validate and attach an independently obtained EOA or ERC-1271 signature. */
|
|
3954
|
+
async withSignature(signature, options = {}) {
|
|
3948
3955
|
check(this.status === "prepared", "registry operation is not awaiting authorization");
|
|
3949
3956
|
options.signal?.throwIfAborted();
|
|
3950
3957
|
await this.validateFreshness();
|
|
3951
3958
|
options.signal?.throwIfAborted();
|
|
3952
|
-
let signature = options.signature;
|
|
3953
|
-
if (signature === void 0) {
|
|
3954
|
-
const signer = this.#runtime.signer;
|
|
3955
|
-
check(signer, "registry signer capability is missing");
|
|
3956
|
-
check(
|
|
3957
|
-
BigInt(await signer.getAddress()) === BigInt(this.#plan.registration.walletIdentity),
|
|
3958
|
-
"registry signer does not match wallet owner"
|
|
3959
|
-
);
|
|
3960
|
-
const typed = this.typedData();
|
|
3961
|
-
signature = await signer.signTypedData(
|
|
3962
|
-
typed.domain,
|
|
3963
|
-
typed.types,
|
|
3964
|
-
typed.message
|
|
3965
|
-
);
|
|
3966
|
-
}
|
|
3967
3959
|
check(
|
|
3968
3960
|
/^0x(?:[a-fA-F0-9]{2})+$/.test(signature),
|
|
3969
3961
|
"registry signature must be nonempty hexadecimal bytes"
|
|
@@ -4220,9 +4212,10 @@ var AddressRegistry = class {
|
|
|
4220
4212
|
|
|
4221
4213
|
// src/credit-create.ts
|
|
4222
4214
|
var import_ethers5 = require("ethers");
|
|
4223
|
-
var
|
|
4215
|
+
var import_core5 = require("@invisi-labs/core");
|
|
4224
4216
|
|
|
4225
4217
|
// src/validation.ts
|
|
4218
|
+
var import_core4 = require("@invisi-labs/core");
|
|
4226
4219
|
var import_ethers4 = require("ethers");
|
|
4227
4220
|
var FIELD_MODULUS = 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
|
|
4228
4221
|
var ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
@@ -4309,19 +4302,14 @@ var pad = (v, n) => [
|
|
|
4309
4302
|
...v,
|
|
4310
4303
|
...Array(n - v.length).fill(0n)
|
|
4311
4304
|
];
|
|
4312
|
-
function
|
|
4305
|
+
function normalizeOpaqueDelivery(value, options = {}) {
|
|
4313
4306
|
known(value, ["ciphertext", "clue"], "delivery");
|
|
4314
4307
|
if (options.empty) {
|
|
4315
4308
|
if (value.ciphertext !== "0x" || value.clue !== "0x")
|
|
4316
4309
|
throw new Error("absent output requires an empty delivery");
|
|
4317
4310
|
return { ciphertext: "0x", clue: "0x" };
|
|
4318
4311
|
}
|
|
4319
|
-
const ciphertext = envelope(
|
|
4320
|
-
value.ciphertext,
|
|
4321
|
-
options.credit ? 498 : 160,
|
|
4322
|
-
options.credit ? 5 : 2,
|
|
4323
|
-
options.credit ? 754 : 416
|
|
4324
|
-
);
|
|
4312
|
+
const ciphertext = envelope(value.ciphertext, 1, void 0, import_core4.MAX_CIPHERTEXT_BYTES);
|
|
4325
4313
|
const clue = envelope(value.clue, 68);
|
|
4326
4314
|
if (clue.slice(130, 132).toLowerCase() !== "18") throw new Error("clue precision must be 24");
|
|
4327
4315
|
return { ciphertext, clue };
|
|
@@ -4404,7 +4392,7 @@ function validateCreateCreditRequest(c, p, cs, funding = {}) {
|
|
|
4404
4392
|
throw new Error("owner must be nonzero and expiry future");
|
|
4405
4393
|
if (dense(cs, "deliveries").length !== 3) throw new Error("deliveries count mismatch");
|
|
4406
4394
|
const deliveries = cs.map(
|
|
4407
|
-
(v, i) =>
|
|
4395
|
+
(v, i) => normalizeOpaqueDelivery(v, { empty: i === 2 && BigInt(changeCommitment) === 0n })
|
|
4408
4396
|
);
|
|
4409
4397
|
if (ns.length === 0 && f.amount === 0n)
|
|
4410
4398
|
throw new Error("empty inputs require positive funding");
|
|
@@ -4454,7 +4442,7 @@ function createCreditBaseInputs(c, p, cs, f = {}) {
|
|
|
4454
4442
|
function createCreditPublicInputs(c, p, deliveries, funding = {}) {
|
|
4455
4443
|
const base = createCreditBaseInputs(c, p, deliveries, funding);
|
|
4456
4444
|
const digest = BigInt(
|
|
4457
|
-
(0,
|
|
4445
|
+
(0, import_core5.computeWalletAuthorizationDigest)({
|
|
4458
4446
|
context: { chainId: c.chainId, protocolAddress: BigInt(c.protocolAddress) },
|
|
4459
4447
|
operation: "create-credit",
|
|
4460
4448
|
capacity: 10,
|
|
@@ -4490,7 +4478,7 @@ function createCreditRequestHash(p, cs, f = {}) {
|
|
|
4490
4478
|
);
|
|
4491
4479
|
}
|
|
4492
4480
|
function encodeCreateCreditTypedData(c, p, deliveries, funding = {}) {
|
|
4493
|
-
return (0,
|
|
4481
|
+
return (0, import_core5.walletAuthorizationTypedData)({
|
|
4494
4482
|
context: { chainId: c.chainId, protocolAddress: BigInt(c.protocolAddress) },
|
|
4495
4483
|
operation: "create-credit",
|
|
4496
4484
|
capacity: 10,
|
|
@@ -4537,7 +4525,7 @@ function encodeCreditProof(proof) {
|
|
|
4537
4525
|
|
|
4538
4526
|
// src/credit-lifecycle.ts
|
|
4539
4527
|
var import_ethers6 = require("ethers");
|
|
4540
|
-
var
|
|
4528
|
+
var import_core6 = require("@invisi-labs/core");
|
|
4541
4529
|
var CREDIT_DRAW_CAPACITIES = [1, 12, 50, 120];
|
|
4542
4530
|
function selectCreditDrawCircuit(count) {
|
|
4543
4531
|
if (!Number.isInteger(count) || count < 1 || count > 120)
|
|
@@ -4586,7 +4574,7 @@ function validateCreditDrawRequest(c, p, cs) {
|
|
|
4586
4574
|
if (dense(cs, "deliveries").length !== 2 * ns.length)
|
|
4587
4575
|
throw new Error("deliveries count mismatch");
|
|
4588
4576
|
const deliveries = cs.map(
|
|
4589
|
-
(v, i) => BigInt(remainderCommitments[Math.floor(i / 2)]) === 0n ?
|
|
4577
|
+
(v, i) => BigInt(remainderCommitments[Math.floor(i / 2)]) === 0n ? normalizeOpaqueDelivery(v, { empty: true }) : normalizeOpaqueDelivery(v, {})
|
|
4590
4578
|
);
|
|
4591
4579
|
return {
|
|
4592
4580
|
...ctx,
|
|
@@ -4600,7 +4588,7 @@ function validateCreditDrawRequest(c, p, cs) {
|
|
|
4600
4588
|
deadline: checkDeadline(c, p.deadline),
|
|
4601
4589
|
deliveries,
|
|
4602
4590
|
profile,
|
|
4603
|
-
periods: (0,
|
|
4591
|
+
periods: (0, import_core6.creditPeriods)(c.timestamp)
|
|
4604
4592
|
};
|
|
4605
4593
|
}
|
|
4606
4594
|
function creditDrawBaseInputs(c, p, cs) {
|
|
@@ -4728,7 +4716,7 @@ function topUpCreditRequestHash(p, cs, f = {}) {
|
|
|
4728
4716
|
);
|
|
4729
4717
|
}
|
|
4730
4718
|
function authorize(c, operation, capacity, words) {
|
|
4731
|
-
return (0,
|
|
4719
|
+
return (0, import_core6.walletAuthorizationTypedData)({
|
|
4732
4720
|
context: { chainId: c.chainId, protocolAddress: BigInt(c.protocolAddress) },
|
|
4733
4721
|
operation,
|
|
4734
4722
|
capacity,
|
|
@@ -4737,7 +4725,7 @@ function authorize(c, operation, capacity, words) {
|
|
|
4737
4725
|
}
|
|
4738
4726
|
function appendDigest(c, operation, capacity, words) {
|
|
4739
4727
|
const digest = BigInt(
|
|
4740
|
-
(0,
|
|
4728
|
+
(0, import_core6.computeWalletAuthorizationDigest)({
|
|
4741
4729
|
context: { chainId: c.chainId, protocolAddress: BigInt(c.protocolAddress) },
|
|
4742
4730
|
operation,
|
|
4743
4731
|
capacity,
|
|
@@ -4827,7 +4815,7 @@ function buildTopUpCreditCalldata(p) {
|
|
|
4827
4815
|
|
|
4828
4816
|
// src/funds.ts
|
|
4829
4817
|
var import_ethers7 = require("ethers");
|
|
4830
|
-
var
|
|
4818
|
+
var import_core7 = require("@invisi-labs/core");
|
|
4831
4819
|
var TRANSACT_CAPACITIES = [14, 56, 112, 168];
|
|
4832
4820
|
function selectTransactCapacity(inputs, outputs) {
|
|
4833
4821
|
if (!Number.isInteger(inputs) || !Number.isInteger(outputs) || inputs < 0 || outputs < 0 || inputs + outputs < 1 || inputs === 0 && outputs < 2 || inputs + outputs > 168)
|
|
@@ -4848,7 +4836,7 @@ function normalizeTransactRequest(c, p, cs, funding = {}) {
|
|
|
4848
4836
|
"deadline"
|
|
4849
4837
|
]);
|
|
4850
4838
|
const ns = notes(p.nullifiers, "nullifiers"), os = notes(p.outputCommitments, "outputCommitments"), capacity = selectTransactCapacity(ns.length, os.length);
|
|
4851
|
-
const sources = normalizeSources(p.sources, ns.length, capacity), deliveries = dense(cs, "deliveries").map((v) =>
|
|
4839
|
+
const sources = normalizeSources(p.sources, ns.length, capacity), deliveries = dense(cs, "deliveries").map((v) => normalizeOpaqueDelivery(v));
|
|
4852
4840
|
if (deliveries.length !== os.length) throw new Error("deliveries count mismatch");
|
|
4853
4841
|
const withdrawalAmount = uint(p.withdrawalAmount, 128, "withdrawalAmount"), withdrawalAddress = (0, import_ethers7.getAddress)(p.withdrawalAddress), f = normalizeFunding(funding);
|
|
4854
4842
|
if (withdrawalAmount === 0n !== (BigInt(withdrawalAddress) === 0n) || os.length === 0 && withdrawalAmount === 0n)
|
|
@@ -4906,7 +4894,7 @@ function transactPublicInputs(c, p, cs, f = {}) {
|
|
|
4906
4894
|
}
|
|
4907
4895
|
function encodeTransactTypedData(c, p, deliveries, f = {}) {
|
|
4908
4896
|
const r = normalizeTransactRequest(c, p, deliveries, f);
|
|
4909
|
-
return (0,
|
|
4897
|
+
return (0, import_core7.walletAuthorizationTypedData)({
|
|
4910
4898
|
context: { chainId: r.chainId, protocolAddress: BigInt(r.protocolAddress) },
|
|
4911
4899
|
operation: "transact",
|
|
4912
4900
|
capacity: r.capacity,
|
|
@@ -5027,8 +5015,8 @@ async function validateShieldRequest(p) {
|
|
|
5027
5015
|
uint(o.amount, 128, "amount");
|
|
5028
5016
|
if (o.amount === 0n) throw new Error("amount must be positive");
|
|
5029
5017
|
field(o.identity);
|
|
5030
|
-
|
|
5031
|
-
if (await (0,
|
|
5018
|
+
normalizeOpaqueDelivery(cs[i]);
|
|
5019
|
+
if (await (0, import_core7.computeNoteCommitment)(
|
|
5032
5020
|
p.context.chainId,
|
|
5033
5021
|
o.identity,
|
|
5034
5022
|
BigInt(p.context.tokenAddress),
|
|
@@ -5156,7 +5144,7 @@ var import_rpc = require("@invisi-labs/rpc");
|
|
|
5156
5144
|
|
|
5157
5145
|
// src/TokenProtocol.ts
|
|
5158
5146
|
var import_ethers8 = require("ethers");
|
|
5159
|
-
var
|
|
5147
|
+
var import_core8 = require("@invisi-labs/core");
|
|
5160
5148
|
var TokenProtocol = class {
|
|
5161
5149
|
context;
|
|
5162
5150
|
expectedRuntimeCodeHash;
|
|
@@ -5174,7 +5162,7 @@ var TokenProtocol = class {
|
|
|
5174
5162
|
}
|
|
5175
5163
|
async validateDeployment(reader, options = {}) {
|
|
5176
5164
|
if ((options.requireRuntimeProfile ?? true) && !this.expectedRuntimeCodeHash) {
|
|
5177
|
-
throw new
|
|
5165
|
+
throw new import_core8.MissingCapabilityError("trusted deployment profile");
|
|
5178
5166
|
}
|
|
5179
5167
|
if (await reader.getChainId() !== this.context.chainId) {
|
|
5180
5168
|
throw new Error("deployment chain mismatch");
|
|
@@ -5322,20 +5310,20 @@ var Protocol = class _Protocol {
|
|
|
5322
5310
|
};
|
|
5323
5311
|
|
|
5324
5312
|
// src/verify.ts
|
|
5325
|
-
var
|
|
5313
|
+
var import_core9 = require("@invisi-labs/core");
|
|
5326
5314
|
async function verifyNoteCommitment(params) {
|
|
5327
|
-
const expected = await (0,
|
|
5315
|
+
const expected = await (0, import_core9.computeNoteCommitment)(
|
|
5328
5316
|
params.chainId,
|
|
5329
|
-
await (0,
|
|
5317
|
+
await (0, import_core9.computeNoteIdentity)(BigInt(params.walletIdentity), BigInt(params.nonce)),
|
|
5330
5318
|
BigInt(params.tokenAddress),
|
|
5331
5319
|
params.amount
|
|
5332
5320
|
);
|
|
5333
5321
|
return BigInt(expected) === BigInt(params.noteCommitment);
|
|
5334
5322
|
}
|
|
5335
5323
|
async function verifyCreditCommitment(params) {
|
|
5336
|
-
if (params.creditSharedKey <= 0n || params.creditSharedKey >=
|
|
5324
|
+
if (params.creditSharedKey <= 0n || params.creditSharedKey >= import_core9.FIELD_MODULUS)
|
|
5337
5325
|
throw new RangeError("creditSharedKey must be a nonzero canonical Field");
|
|
5338
|
-
const expected = await (0,
|
|
5326
|
+
const expected = await (0, import_core9.computeCreditCommitment)(
|
|
5339
5327
|
params.chainId,
|
|
5340
5328
|
params.spenderWalletIdentity,
|
|
5341
5329
|
params.ownerWalletIdentity,
|
|
@@ -5345,42 +5333,12 @@ async function verifyCreditCommitment(params) {
|
|
|
5345
5333
|
params.amount,
|
|
5346
5334
|
params.nonce,
|
|
5347
5335
|
params.expiry,
|
|
5348
|
-
await (0,
|
|
5336
|
+
await (0, import_core9.computeCreditSharedKeyHash)(params.creditSharedKey),
|
|
5349
5337
|
params
|
|
5350
5338
|
);
|
|
5351
5339
|
return BigInt(expected) === BigInt(params.creditCommitment);
|
|
5352
5340
|
}
|
|
5353
5341
|
|
|
5354
|
-
// src/withdrawal.ts
|
|
5355
|
-
var import_core9 = require("@invisi-labs/core");
|
|
5356
|
-
async function buildWithdrawalCalldata(p) {
|
|
5357
|
-
const r = normalizeTransactRequest(p.context, p.params, [], p.funding);
|
|
5358
|
-
if (r.outputCount !== 0 || r.depositAmount !== 0n || p.sourceNotes.length === 0 || p.sourceNotes.length !== r.inputCount) {
|
|
5359
|
-
throw new Error("full withdrawal requires inputs, no private outputs and no funding");
|
|
5360
|
-
}
|
|
5361
|
-
const walletIdentity = BigInt(p.sourceNotes[0].walletIdentity);
|
|
5362
|
-
let total = 0n;
|
|
5363
|
-
for (const [index, note] of p.sourceNotes.entries()) {
|
|
5364
|
-
if (note.chainId !== p.context.chainId || note.protocolAddress !== BigInt(p.context.protocolAddress))
|
|
5365
|
-
throw new Error("source note context mismatch");
|
|
5366
|
-
if (BigInt(note.walletIdentity) !== walletIdentity || note.tokenAddress !== BigInt(p.context.tokenAddress) || note.amount <= 0n)
|
|
5367
|
-
throw new Error("source note wallet/token/amount mismatch");
|
|
5368
|
-
const commitment = await (0, import_core9.computeNoteCommitment)(
|
|
5369
|
-
note.chainId,
|
|
5370
|
-
await (0, import_core9.computeNoteIdentity)(walletIdentity, note.nonce),
|
|
5371
|
-
note.tokenAddress,
|
|
5372
|
-
note.amount
|
|
5373
|
-
);
|
|
5374
|
-
const nullifier = await (0, import_core9.computeNoteNullifier)(note.chainId, note.nonce, commitment);
|
|
5375
|
-
if (nullifier.toLowerCase() !== r.nullifiers[index].toLowerCase())
|
|
5376
|
-
throw new Error("source note nullifier mismatch");
|
|
5377
|
-
total += note.amount;
|
|
5378
|
-
}
|
|
5379
|
-
if (r.withdrawalAmount !== total)
|
|
5380
|
-
throw new Error("withdrawal must equal the full input balance");
|
|
5381
|
-
return buildTransactCalldata({ ...p, deliveries: [] });
|
|
5382
|
-
}
|
|
5383
|
-
|
|
5384
5342
|
// src/witness.ts
|
|
5385
5343
|
var import_core10 = require("@invisi-labs/core");
|
|
5386
5344
|
var import_prover = require("@invisi-labs/prover");
|
|
@@ -7298,7 +7256,6 @@ async function resolveInstanceAddress(client, factoryAddress, salt) {
|
|
|
7298
7256
|
buildTopUpCreditCalldata,
|
|
7299
7257
|
buildTransactAggregateCalldata,
|
|
7300
7258
|
buildTransactCalldata,
|
|
7301
|
-
buildWithdrawalCalldata,
|
|
7302
7259
|
cancelCreditBaseInputs,
|
|
7303
7260
|
cancelCreditPublicInputs,
|
|
7304
7261
|
cancelCreditRequestHash,
|
|
@@ -7328,8 +7285,8 @@ async function resolveInstanceAddress(client, factoryAddress, salt) {
|
|
|
7328
7285
|
getEIP712Domain,
|
|
7329
7286
|
known,
|
|
7330
7287
|
normalizeContext,
|
|
7331
|
-
normalizeDelivery,
|
|
7332
7288
|
normalizeFunding,
|
|
7289
|
+
normalizeOpaqueDelivery,
|
|
7333
7290
|
normalizeSources,
|
|
7334
7291
|
normalizeTransactRequest,
|
|
7335
7292
|
notes,
|