@invariance/sdk 0.1.0 → 0.1.1
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 +281 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +116 -3
- package/dist/index.d.ts +116 -3
- package/dist/index.js +272 -1
- package/dist/index.js.map +1 -1
- package/package.json +7 -3
package/dist/index.cjs
CHANGED
|
@@ -7804,7 +7804,7 @@ var require_main = __commonJS({
|
|
|
7804
7804
|
var fs = require("fs");
|
|
7805
7805
|
var path = require("path");
|
|
7806
7806
|
var os = require("os");
|
|
7807
|
-
var
|
|
7807
|
+
var crypto2 = require("crypto");
|
|
7808
7808
|
var packageJson = require_package();
|
|
7809
7809
|
var version = packageJson.version;
|
|
7810
7810
|
var LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg;
|
|
@@ -8023,7 +8023,7 @@ var require_main = __commonJS({
|
|
|
8023
8023
|
const authTag = ciphertext.subarray(-16);
|
|
8024
8024
|
ciphertext = ciphertext.subarray(12, -16);
|
|
8025
8025
|
try {
|
|
8026
|
-
const aesgcm =
|
|
8026
|
+
const aesgcm = crypto2.createDecipheriv("aes-256-gcm", key, nonce);
|
|
8027
8027
|
aesgcm.setAuthTag(authTag);
|
|
8028
8028
|
return `${aesgcm.update(ciphertext)}${aesgcm.final()}`;
|
|
8029
8029
|
} catch (error) {
|
|
@@ -8849,6 +8849,7 @@ __export(index_exports, {
|
|
|
8849
8849
|
GovernmentComplianceKit: () => GovernmentComplianceKit,
|
|
8850
8850
|
IdentityGatekeeper: () => IdentityGatekeeper,
|
|
8851
8851
|
IdentityManager: () => IdentityManager,
|
|
8852
|
+
InMemoryLedgerAdapter: () => InMemoryLedgerAdapter,
|
|
8852
8853
|
IntentProtocol: () => IntentProtocol,
|
|
8853
8854
|
Invariance: () => Invariance,
|
|
8854
8855
|
InvarianceError: () => InvarianceError,
|
|
@@ -8858,6 +8859,7 @@ __export(index_exports, {
|
|
|
8858
8859
|
MarketplaceKit: () => MarketplaceKit,
|
|
8859
8860
|
MarketplacePlugin: () => MarketplacePlugin,
|
|
8860
8861
|
MultiAgentComposer: () => MultiAgentComposer,
|
|
8862
|
+
OffchainLedger: () => OffchainLedger,
|
|
8861
8863
|
PipelineBuilder: () => PipelineBuilder,
|
|
8862
8864
|
PolicyEngine: () => PolicyEngine,
|
|
8863
8865
|
ReputationBridge: () => ReputationBridge,
|
|
@@ -8866,6 +8868,7 @@ __export(index_exports, {
|
|
|
8866
8868
|
SDK_VERSION: () => SDK_VERSION,
|
|
8867
8869
|
SessionContext: () => SessionContext,
|
|
8868
8870
|
SocialGraphAdapter: () => SocialGraphAdapter,
|
|
8871
|
+
SupabaseLedgerAdapter: () => SupabaseLedgerAdapter,
|
|
8869
8872
|
Telemetry: () => Telemetry,
|
|
8870
8873
|
Verifier: () => Verifier,
|
|
8871
8874
|
VotingManager: () => VotingManager,
|
|
@@ -8881,6 +8884,8 @@ __export(index_exports, {
|
|
|
8881
8884
|
module.exports = __toCommonJS(index_exports);
|
|
8882
8885
|
|
|
8883
8886
|
// src/core/InvarianceClient.ts
|
|
8887
|
+
init_dist();
|
|
8888
|
+
init_InvarianceError();
|
|
8884
8889
|
var import_chains2 = require("viem/chains");
|
|
8885
8890
|
var import_accounts2 = require("viem/accounts");
|
|
8886
8891
|
|
|
@@ -17071,8 +17076,191 @@ var VotingManager = class {
|
|
|
17071
17076
|
}
|
|
17072
17077
|
};
|
|
17073
17078
|
|
|
17079
|
+
// src/modules/ledger/OffchainLedger.ts
|
|
17080
|
+
var OffchainLedger = class {
|
|
17081
|
+
events;
|
|
17082
|
+
telemetry;
|
|
17083
|
+
failOpen;
|
|
17084
|
+
adapter;
|
|
17085
|
+
constructor(events, telemetry, options) {
|
|
17086
|
+
this.events = events;
|
|
17087
|
+
this.telemetry = telemetry;
|
|
17088
|
+
this.adapter = options.adapter;
|
|
17089
|
+
this.failOpen = options.failOpen ?? true;
|
|
17090
|
+
}
|
|
17091
|
+
/**
|
|
17092
|
+
* Log a ledger event.
|
|
17093
|
+
*
|
|
17094
|
+
* @param event - Same `LedgerEventInput` used by on-chain ledger
|
|
17095
|
+
* @returns The created off-chain ledger entry
|
|
17096
|
+
*/
|
|
17097
|
+
async log(event) {
|
|
17098
|
+
this.telemetry.track("offchain_ledger.log", { action: event.action });
|
|
17099
|
+
const entryId = crypto.randomUUID();
|
|
17100
|
+
const timestamp = Date.now();
|
|
17101
|
+
const category = event.category ?? "custom";
|
|
17102
|
+
const severity = event.severity ?? "info";
|
|
17103
|
+
const entry = {
|
|
17104
|
+
entryId,
|
|
17105
|
+
action: event.action,
|
|
17106
|
+
actor: event.actor,
|
|
17107
|
+
category,
|
|
17108
|
+
severity,
|
|
17109
|
+
timestamp,
|
|
17110
|
+
createdAt: new Date(timestamp).toISOString()
|
|
17111
|
+
};
|
|
17112
|
+
if (event.metadata) {
|
|
17113
|
+
entry.metadata = event.metadata;
|
|
17114
|
+
}
|
|
17115
|
+
try {
|
|
17116
|
+
const persisted = await this.adapter.insert(entry);
|
|
17117
|
+
this.events.emit("ledger.logged", { entryId: persisted.entryId, action: persisted.action });
|
|
17118
|
+
return persisted;
|
|
17119
|
+
} catch (err) {
|
|
17120
|
+
if (this.failOpen) {
|
|
17121
|
+
this.events.emit("ledger.logged", { entryId: entry.entryId, action: entry.action });
|
|
17122
|
+
return entry;
|
|
17123
|
+
}
|
|
17124
|
+
throw err;
|
|
17125
|
+
}
|
|
17126
|
+
}
|
|
17127
|
+
/**
|
|
17128
|
+
* Query off-chain ledger entries.
|
|
17129
|
+
*
|
|
17130
|
+
* @param filters - Same `LedgerQueryFilters` used by on-chain ledger
|
|
17131
|
+
* @returns Matching off-chain ledger entries
|
|
17132
|
+
*/
|
|
17133
|
+
async query(filters) {
|
|
17134
|
+
this.telemetry.track("offchain_ledger.query", { action: filters.action });
|
|
17135
|
+
try {
|
|
17136
|
+
return await this.adapter.query(filters);
|
|
17137
|
+
} catch (err) {
|
|
17138
|
+
if (this.failOpen) {
|
|
17139
|
+
return [];
|
|
17140
|
+
}
|
|
17141
|
+
throw err;
|
|
17142
|
+
}
|
|
17143
|
+
}
|
|
17144
|
+
};
|
|
17145
|
+
|
|
17146
|
+
// src/modules/ledger/adapters/SupabaseLedgerAdapter.ts
|
|
17147
|
+
init_dist();
|
|
17148
|
+
init_InvarianceError();
|
|
17149
|
+
var TABLE_NAME = "offchain_ledger_entries";
|
|
17150
|
+
var SupabaseLedgerAdapter = class _SupabaseLedgerAdapter {
|
|
17151
|
+
url;
|
|
17152
|
+
key;
|
|
17153
|
+
client = null;
|
|
17154
|
+
clientPromise = null;
|
|
17155
|
+
constructor(options) {
|
|
17156
|
+
this.url = options.url;
|
|
17157
|
+
this.key = options.key;
|
|
17158
|
+
}
|
|
17159
|
+
/** @internal */
|
|
17160
|
+
async getClient() {
|
|
17161
|
+
if (this.client) return this.client;
|
|
17162
|
+
if (this.clientPromise) return this.clientPromise;
|
|
17163
|
+
this.clientPromise = (async () => {
|
|
17164
|
+
try {
|
|
17165
|
+
const { createClient } = await import("@supabase/supabase-js");
|
|
17166
|
+
this.client = createClient(this.url, this.key);
|
|
17167
|
+
return this.client;
|
|
17168
|
+
} catch {
|
|
17169
|
+
throw new InvarianceError(
|
|
17170
|
+
ErrorCode.NETWORK_ERROR,
|
|
17171
|
+
"Failed to import @supabase/supabase-js. Install it with: npm install @supabase/supabase-js"
|
|
17172
|
+
);
|
|
17173
|
+
}
|
|
17174
|
+
})();
|
|
17175
|
+
return this.clientPromise;
|
|
17176
|
+
}
|
|
17177
|
+
/** Persist a ledger entry to Supabase. */
|
|
17178
|
+
async insert(entry) {
|
|
17179
|
+
const row = {
|
|
17180
|
+
entry_id: entry.entryId,
|
|
17181
|
+
action: entry.action,
|
|
17182
|
+
actor_type: entry.actor.type,
|
|
17183
|
+
actor_address: entry.actor.address,
|
|
17184
|
+
category: entry.category,
|
|
17185
|
+
severity: entry.severity,
|
|
17186
|
+
metadata: entry.metadata ?? null,
|
|
17187
|
+
timestamp: entry.timestamp
|
|
17188
|
+
};
|
|
17189
|
+
const client = await this.getClient();
|
|
17190
|
+
const { data, error } = await client.from(TABLE_NAME).insert(row).select().single();
|
|
17191
|
+
if (error) {
|
|
17192
|
+
throw new InvarianceError(ErrorCode.NETWORK_ERROR, `Supabase insert failed: ${error.message}`);
|
|
17193
|
+
}
|
|
17194
|
+
return _SupabaseLedgerAdapter.toEntry(data);
|
|
17195
|
+
}
|
|
17196
|
+
/** Query ledger entries from Supabase. */
|
|
17197
|
+
async query(filters) {
|
|
17198
|
+
const client = await this.getClient();
|
|
17199
|
+
let q = client.from(TABLE_NAME).select("*");
|
|
17200
|
+
if (filters.actor) {
|
|
17201
|
+
q = q.eq("actor_address", filters.actor);
|
|
17202
|
+
}
|
|
17203
|
+
if (filters.actorType) {
|
|
17204
|
+
q = q.eq("actor_type", filters.actorType);
|
|
17205
|
+
}
|
|
17206
|
+
if (filters.action) {
|
|
17207
|
+
if (Array.isArray(filters.action)) {
|
|
17208
|
+
q = q.in("action", filters.action);
|
|
17209
|
+
} else {
|
|
17210
|
+
q = q.eq("action", filters.action);
|
|
17211
|
+
}
|
|
17212
|
+
}
|
|
17213
|
+
if (filters.category) {
|
|
17214
|
+
q = q.eq("category", filters.category);
|
|
17215
|
+
}
|
|
17216
|
+
if (filters.from !== void 0) {
|
|
17217
|
+
const fromTs = typeof filters.from === "string" ? new Date(filters.from).getTime() : filters.from;
|
|
17218
|
+
q = q.gte("timestamp", fromTs);
|
|
17219
|
+
}
|
|
17220
|
+
if (filters.to !== void 0) {
|
|
17221
|
+
const toTs = typeof filters.to === "string" ? new Date(filters.to).getTime() : filters.to;
|
|
17222
|
+
q = q.lte("timestamp", toTs);
|
|
17223
|
+
}
|
|
17224
|
+
const orderCol = filters.orderBy ?? "timestamp";
|
|
17225
|
+
const ascending = (filters.order ?? "desc") === "asc";
|
|
17226
|
+
q = q.order(orderCol, { ascending });
|
|
17227
|
+
if (filters.limit) {
|
|
17228
|
+
q = q.limit(filters.limit);
|
|
17229
|
+
}
|
|
17230
|
+
if (filters.offset) {
|
|
17231
|
+
q = q.range(filters.offset, filters.offset + (filters.limit ?? 100) - 1);
|
|
17232
|
+
}
|
|
17233
|
+
const { data, error } = await q;
|
|
17234
|
+
if (error) {
|
|
17235
|
+
throw new InvarianceError(ErrorCode.NETWORK_ERROR, `Supabase query failed: ${error.message}`);
|
|
17236
|
+
}
|
|
17237
|
+
return data.map(
|
|
17238
|
+
(row) => _SupabaseLedgerAdapter.toEntry(row)
|
|
17239
|
+
);
|
|
17240
|
+
}
|
|
17241
|
+
/** Map a Supabase row to an OffchainLedgerEntry. */
|
|
17242
|
+
static toEntry(row) {
|
|
17243
|
+
const entry = {
|
|
17244
|
+
entryId: row["entry_id"],
|
|
17245
|
+
action: row["action"],
|
|
17246
|
+
actor: {
|
|
17247
|
+
type: row["actor_type"],
|
|
17248
|
+
address: row["actor_address"]
|
|
17249
|
+
},
|
|
17250
|
+
category: row["category"],
|
|
17251
|
+
severity: row["severity"],
|
|
17252
|
+
timestamp: row["timestamp"],
|
|
17253
|
+
createdAt: row["created_at"]
|
|
17254
|
+
};
|
|
17255
|
+
if (row["metadata"] != null) {
|
|
17256
|
+
entry.metadata = row["metadata"];
|
|
17257
|
+
}
|
|
17258
|
+
return entry;
|
|
17259
|
+
}
|
|
17260
|
+
};
|
|
17261
|
+
|
|
17074
17262
|
// src/core/InvarianceClient.ts
|
|
17075
|
-
var SDK_VERSION = "0.1.
|
|
17263
|
+
var SDK_VERSION = "0.1.1";
|
|
17076
17264
|
var Invariance = class _Invariance {
|
|
17077
17265
|
config;
|
|
17078
17266
|
contracts;
|
|
@@ -17097,6 +17285,7 @@ var Invariance = class _Invariance {
|
|
|
17097
17285
|
_marketplace;
|
|
17098
17286
|
_auditTrail;
|
|
17099
17287
|
_voting;
|
|
17288
|
+
_ledgerOffchain;
|
|
17100
17289
|
// ===========================================================================
|
|
17101
17290
|
// Static Factory Methods
|
|
17102
17291
|
// ===========================================================================
|
|
@@ -17475,6 +17664,39 @@ var Invariance = class _Invariance {
|
|
|
17475
17664
|
}
|
|
17476
17665
|
return this._auditTrail;
|
|
17477
17666
|
}
|
|
17667
|
+
/**
|
|
17668
|
+
* Off-chain ledger module backed by a pluggable {@link LedgerAdapter}.
|
|
17669
|
+
*
|
|
17670
|
+
* Same `LedgerEventInput` interface as on-chain, zero gas.
|
|
17671
|
+
* Resolution order: `config.ledgerAdapter` > `config.supabase` > env vars.
|
|
17672
|
+
*
|
|
17673
|
+
* @example
|
|
17674
|
+
* ```typescript
|
|
17675
|
+
* const entry = await inv.ledgerOffchain.log({
|
|
17676
|
+
* action: 'model-inference',
|
|
17677
|
+
* actor: { type: 'agent', address: '0xBot' },
|
|
17678
|
+
* });
|
|
17679
|
+
* ```
|
|
17680
|
+
*/
|
|
17681
|
+
get ledgerOffchain() {
|
|
17682
|
+
if (!this._ledgerOffchain) {
|
|
17683
|
+
let adapter = this.config.ledgerAdapter;
|
|
17684
|
+
if (!adapter) {
|
|
17685
|
+
const url = this.config.supabase?.url ?? process.env["INVARIANCE_SUPABASE_URL"];
|
|
17686
|
+
const key = this.config.supabase?.key ?? process.env["INVARIANCE_SUPABASE_KEY"];
|
|
17687
|
+
if (!url || !key) {
|
|
17688
|
+
throw new InvarianceError(
|
|
17689
|
+
ErrorCode.INVALID_INPUT,
|
|
17690
|
+
"Off-chain ledger requires either config.ledgerAdapter, config.supabase (url + key), or INVARIANCE_SUPABASE_URL / INVARIANCE_SUPABASE_KEY env vars."
|
|
17691
|
+
);
|
|
17692
|
+
}
|
|
17693
|
+
adapter = new SupabaseLedgerAdapter({ url, key });
|
|
17694
|
+
}
|
|
17695
|
+
const failOpen = this.config.supabase?.failOpen ?? true;
|
|
17696
|
+
this._ledgerOffchain = new OffchainLedger(this.events, this.telemetry, { adapter, failOpen });
|
|
17697
|
+
}
|
|
17698
|
+
return this._ledgerOffchain;
|
|
17699
|
+
}
|
|
17478
17700
|
/**
|
|
17479
17701
|
* Off-chain batch voting with merkle root settlement.
|
|
17480
17702
|
*
|
|
@@ -18043,6 +18265,59 @@ var Invariance = class _Invariance {
|
|
|
18043
18265
|
init_InvarianceError();
|
|
18044
18266
|
init_dist();
|
|
18045
18267
|
|
|
18268
|
+
// src/modules/ledger/adapters/InMemoryLedgerAdapter.ts
|
|
18269
|
+
var InMemoryLedgerAdapter = class {
|
|
18270
|
+
/** @internal */
|
|
18271
|
+
entries = [];
|
|
18272
|
+
async insert(entry) {
|
|
18273
|
+
this.entries.push(entry);
|
|
18274
|
+
return entry;
|
|
18275
|
+
}
|
|
18276
|
+
async query(filters) {
|
|
18277
|
+
let results = [...this.entries];
|
|
18278
|
+
if (filters.actor) {
|
|
18279
|
+
results = results.filter((e) => e.actor.address === filters.actor);
|
|
18280
|
+
}
|
|
18281
|
+
if (filters.actorType) {
|
|
18282
|
+
results = results.filter((e) => e.actor.type === filters.actorType);
|
|
18283
|
+
}
|
|
18284
|
+
if (filters.action) {
|
|
18285
|
+
if (Array.isArray(filters.action)) {
|
|
18286
|
+
results = results.filter((e) => filters.action.includes(e.action));
|
|
18287
|
+
} else {
|
|
18288
|
+
results = results.filter((e) => e.action === filters.action);
|
|
18289
|
+
}
|
|
18290
|
+
}
|
|
18291
|
+
if (filters.category) {
|
|
18292
|
+
results = results.filter((e) => e.category === filters.category);
|
|
18293
|
+
}
|
|
18294
|
+
if (filters.from !== void 0) {
|
|
18295
|
+
const fromTs = typeof filters.from === "string" ? new Date(filters.from).getTime() : filters.from;
|
|
18296
|
+
results = results.filter((e) => e.timestamp >= fromTs);
|
|
18297
|
+
}
|
|
18298
|
+
if (filters.to !== void 0) {
|
|
18299
|
+
const toTs = typeof filters.to === "string" ? new Date(filters.to).getTime() : filters.to;
|
|
18300
|
+
results = results.filter((e) => e.timestamp <= toTs);
|
|
18301
|
+
}
|
|
18302
|
+
const ascending = (filters.order ?? "desc") === "asc";
|
|
18303
|
+
const orderBy = filters.orderBy ?? "timestamp";
|
|
18304
|
+
results.sort((a, b) => {
|
|
18305
|
+
const aVal = a[orderBy];
|
|
18306
|
+
const bVal = b[orderBy];
|
|
18307
|
+
if (aVal < bVal) return ascending ? -1 : 1;
|
|
18308
|
+
if (aVal > bVal) return ascending ? 1 : -1;
|
|
18309
|
+
return 0;
|
|
18310
|
+
});
|
|
18311
|
+
if (filters.offset) {
|
|
18312
|
+
results = results.slice(filters.offset);
|
|
18313
|
+
}
|
|
18314
|
+
if (filters.limit) {
|
|
18315
|
+
results = results.slice(0, filters.limit);
|
|
18316
|
+
}
|
|
18317
|
+
return results;
|
|
18318
|
+
}
|
|
18319
|
+
};
|
|
18320
|
+
|
|
18046
18321
|
// src/utils/webhook.ts
|
|
18047
18322
|
var import_crypto = require("crypto");
|
|
18048
18323
|
function verifyWebhookSignature(body, signature, secret) {
|
|
@@ -19005,6 +19280,7 @@ var import_accounts3 = require("viem/accounts");
|
|
|
19005
19280
|
GovernmentComplianceKit,
|
|
19006
19281
|
IdentityGatekeeper,
|
|
19007
19282
|
IdentityManager,
|
|
19283
|
+
InMemoryLedgerAdapter,
|
|
19008
19284
|
IntentProtocol,
|
|
19009
19285
|
Invariance,
|
|
19010
19286
|
InvarianceError,
|
|
@@ -19014,6 +19290,7 @@ var import_accounts3 = require("viem/accounts");
|
|
|
19014
19290
|
MarketplaceKit,
|
|
19015
19291
|
MarketplacePlugin,
|
|
19016
19292
|
MultiAgentComposer,
|
|
19293
|
+
OffchainLedger,
|
|
19017
19294
|
PipelineBuilder,
|
|
19018
19295
|
PolicyEngine,
|
|
19019
19296
|
ReputationBridge,
|
|
@@ -19022,6 +19299,7 @@ var import_accounts3 = require("viem/accounts");
|
|
|
19022
19299
|
SDK_VERSION,
|
|
19023
19300
|
SessionContext,
|
|
19024
19301
|
SocialGraphAdapter,
|
|
19302
|
+
SupabaseLedgerAdapter,
|
|
19025
19303
|
Telemetry,
|
|
19026
19304
|
Verifier,
|
|
19027
19305
|
VotingManager,
|