Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mighty-dryers-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lit-protocol/contracts': patch
---

Provide a browser-only stub for `custom-network-signatures` so web builds skip the Node-specific implementation
15 changes: 15 additions & 0 deletions packages/contracts/dist/custom-network-signatures.browser.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";
const UNSUPPORTED_MESSAGE = "@lit-protocol/contracts/custom-network-signatures is not supported in browser environments. Please pre-generate contract signatures in a Node.js process and ship the artifacts instead.";
function throwBrowserUnsupported() {
throw new Error(UNSUPPORTED_MESSAGE);
}
function buildSignaturesFromContext(_options) {
return throwBrowserUnsupported();
}
async function generateSignaturesFromContext(_options) {
throwBrowserUnsupported();
}
module.exports = {
buildSignaturesFromContext,
generateSignaturesFromContext
};
18 changes: 18 additions & 0 deletions packages/contracts/dist/custom-network-signatures.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Browser stub for @lit-protocol/contracts/custom-network-signatures.
* These utilities require Node.js filesystem access and cannot run in the browser.
*/
const UNSUPPORTED_MESSAGE = "@lit-protocol/contracts/custom-network-signatures is not supported in browser environments. Please pre-generate contract signatures in a Node.js process and ship the artifacts instead.";
function throwBrowserUnsupported() {
throw new Error(UNSUPPORTED_MESSAGE);
}
function buildSignaturesFromContext(_options) {
return throwBrowserUnsupported();
}
async function generateSignaturesFromContext(_options) {
throwBrowserUnsupported();
}
export {
buildSignaturesFromContext,
generateSignaturesFromContext
};
46 changes: 41 additions & 5 deletions packages/contracts/dist/custom-network-signatures.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ var METHODS_TO_EXTRACT = [
"PubkeyRouter.ethAddressToPkpId",
"PubkeyRouter.getPubkey",
"PubkeyRouter.getEthAddress",
"PubkeyRouter.getDerivedPubkey",
// Ledger:
"Ledger.deposit",
"Ledger.depositForUser",
Expand Down Expand Up @@ -5064,6 +5065,27 @@ function extractAbiMethods(networkCache, methodNames) {
}

// packages/contracts/src/custom-network-signatures.ts
function getModulePathFromImportMeta() {
const moduleUrl = __import_meta__?.url;
if (typeof moduleUrl === "string") {
try {
return (0, import_url.fileURLToPath)(moduleUrl);
} catch (error) {
console.warn("Failed to resolve fileURLToPath from import.meta.url:", error);
}
}
return void 0;
}
function getCurrentModulePath() {
const modulePath = getModulePathFromImportMeta();
if (modulePath) {
return modulePath;
}
if (typeof __filename !== "undefined") {
return __filename;
}
return void 0;
}
function getBaseDirectory(useScriptDirectory = false, callerPath) {
if (useScriptDirectory) {
if (callerPath) {
Expand All @@ -5075,9 +5097,14 @@ function getBaseDirectory(useScriptDirectory = false, callerPath) {
console.log("Using __dirname:", __dirname);
return __dirname;
}
const moduleDir = (0, import_path.dirname)((0, import_url.fileURLToPath)(__import_meta__.url));
console.log("Using module directory:", moduleDir);
return moduleDir;
const modulePath = getCurrentModulePath();
if (modulePath) {
const moduleDir = (0, import_path.dirname)(modulePath);
console.log("Using module directory:", moduleDir);
return moduleDir;
}
console.log("Using current working directory:", process.cwd());
return process.cwd();
}
const cwd = process.cwd();
console.log("Using current working directory:", cwd);
Expand Down Expand Up @@ -5122,6 +5149,14 @@ function generateAbiSignatures(networkData) {
if (methodsByContract.has(contractName)) {
const methods = methodsByContract.get(contractName);
const contractMethods = extractAbiMethods(networkData, methods);
const missingMethods = methods.filter(
(methodName) => !contractMethods[methodName]
);
if (missingMethods.length > 0) {
throw new Error(
`Missing ABI definitions for ${contractName}: ${missingMethods.join(", ")}. Ensure your networkContext.json includes these functions.`
);
}
if (Object.keys(contractMethods).length > 0) {
const address = contractGroup.contracts[0].address_hash;
const events = contractGroup.contracts[0].ABI.filter(
Expand Down Expand Up @@ -5230,8 +5265,9 @@ module.exports = {
}
}
var mainScriptPath = import_path.default.resolve(process.argv[1] || "");
var currentScriptPath = (0, import_url.fileURLToPath)(__import_meta__.url);
if (mainScriptPath === currentScriptPath) {
var modulePathFromMeta = getModulePathFromImportMeta();
var resolvedModulePath = modulePathFromMeta ? import_path.default.resolve(modulePathFromMeta) : void 0;
if (resolvedModulePath && mainScriptPath === resolvedModulePath) {
const jsonFilePath = process.argv[2];
const networkName = process.argv[3];
if (!jsonFilePath) {
Expand Down
46 changes: 41 additions & 5 deletions packages/contracts/dist/custom-network-signatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var METHODS_TO_EXTRACT = [
"PubkeyRouter.ethAddressToPkpId",
"PubkeyRouter.getPubkey",
"PubkeyRouter.getEthAddress",
"PubkeyRouter.getDerivedPubkey",
// Ledger:
"Ledger.deposit",
"Ledger.depositForUser",
Expand Down Expand Up @@ -5028,6 +5029,27 @@ function extractAbiMethods(networkCache, methodNames) {
}

// packages/contracts/src/custom-network-signatures.ts
function getModulePathFromImportMeta() {
const moduleUrl = import.meta?.url;
if (typeof moduleUrl === "string") {
try {
return fileURLToPath(moduleUrl);
} catch (error) {
console.warn("Failed to resolve fileURLToPath from import.meta.url:", error);
}
}
return void 0;
}
function getCurrentModulePath() {
const modulePath = getModulePathFromImportMeta();
if (modulePath) {
return modulePath;
}
if (typeof __filename !== "undefined") {
return __filename;
}
return void 0;
}
function getBaseDirectory(useScriptDirectory = false, callerPath) {
if (useScriptDirectory) {
if (callerPath) {
Expand All @@ -5039,9 +5061,14 @@ function getBaseDirectory(useScriptDirectory = false, callerPath) {
console.log("Using __dirname:", __dirname);
return __dirname;
}
const moduleDir = dirname(fileURLToPath(import.meta.url));
console.log("Using module directory:", moduleDir);
return moduleDir;
const modulePath = getCurrentModulePath();
if (modulePath) {
const moduleDir = dirname(modulePath);
console.log("Using module directory:", moduleDir);
return moduleDir;
}
console.log("Using current working directory:", process.cwd());
return process.cwd();
}
const cwd = process.cwd();
console.log("Using current working directory:", cwd);
Expand Down Expand Up @@ -5086,6 +5113,14 @@ function generateAbiSignatures(networkData) {
if (methodsByContract.has(contractName)) {
const methods = methodsByContract.get(contractName);
const contractMethods = extractAbiMethods(networkData, methods);
const missingMethods = methods.filter(
(methodName) => !contractMethods[methodName]
);
if (missingMethods.length > 0) {
throw new Error(
`Missing ABI definitions for ${contractName}: ${missingMethods.join(", ")}. Ensure your networkContext.json includes these functions.`
);
}
if (Object.keys(contractMethods).length > 0) {
const address = contractGroup.contracts[0].address_hash;
const events = contractGroup.contracts[0].ABI.filter(
Expand Down Expand Up @@ -5194,8 +5229,9 @@ module.exports = {
}
}
var mainScriptPath = path.resolve(process.argv[1] || "");
var currentScriptPath = fileURLToPath(import.meta.url);
if (mainScriptPath === currentScriptPath) {
var modulePathFromMeta = getModulePathFromImportMeta();
var resolvedModulePath = modulePathFromMeta ? path.resolve(modulePathFromMeta) : void 0;
if (resolvedModulePath && mainScriptPath === resolvedModulePath) {
const jsonFilePath = process.argv[2];
const networkName = process.argv[3];
if (!jsonFilePath) {
Expand Down
4 changes: 4 additions & 0 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@
"types": "./dist/signatures/develop.d.ts"
},
"./custom-network-signatures": {
"browser": {
"import": "./dist/custom-network-signatures.browser.js",
"require": "./dist/custom-network-signatures.browser.cjs"
},
"import": "./dist/custom-network-signatures.js",
"require": "./dist/custom-network-signatures.cjs",
"types": "./dist/custom-network-signatures.d.ts"
Expand Down
31 changes: 31 additions & 0 deletions packages/contracts/src/custom-network-signatures.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type {
BuildSignaturesFromContextOptions,
BuildSignaturesFromContextResult,
GenerateSignaturesOptions,
} from './custom-network-signatures';

const UNSUPPORTED_MESSAGE =
'@lit-protocol/contracts/custom-network-signatures is not supported in browser environments. ' +
'Please generate contract signatures ahead of time in a Node.js process and ship the artifacts instead.';

function throwBrowserUnsupported(): never {
throw new Error(UNSUPPORTED_MESSAGE);
}

export function buildSignaturesFromContext(
_options: BuildSignaturesFromContextOptions
): BuildSignaturesFromContextResult {
return throwBrowserUnsupported();
}

export async function generateSignaturesFromContext(
_options: GenerateSignaturesOptions
): Promise<never> {
throwBrowserUnsupported();
}

export type {
BuildSignaturesFromContextOptions,
BuildSignaturesFromContextResult,
GenerateSignaturesOptions,
} from './custom-network-signatures';
69 changes: 64 additions & 5 deletions packages/contracts/src/custom-network-signatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,44 @@ export interface BuildSignaturesFromContextResult {
baseDirectory: string;
}

/**
* Resolves the on-disk path of this module in both ESM and CJS bundles.
* Falls back to __filename when bundlers strip import.meta.url.
*/
function getModulePathFromImportMeta(): string | undefined {
const moduleUrl = (import.meta as unknown as { url?: string } | undefined)
?.url;
if (typeof moduleUrl === 'string') {
try {
return fileURLToPath(moduleUrl);
} catch (error) {
console.warn(
'Failed to resolve fileURLToPath from import.meta.url:',
error
);
}
}

return undefined;
}

/**
* Resolves the on-disk path of this module in both ESM and CJS bundles.
* Falls back to __filename when bundlers strip import.meta.url.
*/
function getCurrentModulePath(): string | undefined {
const modulePath = getModulePathFromImportMeta();
if (modulePath) {
return modulePath;
}

if (typeof __filename !== 'undefined') {
return __filename;
}

return undefined;
}

/**
* Gets the base directory for resolving paths
* @param useScriptDirectory - Whether to use script's directory or current working directory
Expand All @@ -75,9 +113,14 @@ function getBaseDirectory(
return __dirname;
}
// When running as module without callerPath
const moduleDir = dirname(fileURLToPath(import.meta.url));
console.log('Using module directory:', moduleDir);
return moduleDir;
const modulePath = getCurrentModulePath();
if (modulePath) {
const moduleDir = dirname(modulePath);
console.log('Using module directory:', moduleDir);
return moduleDir;
}
console.log('Using current working directory:', process.cwd());
return process.cwd();
}
// Use current working directory
const cwd = process.cwd();
Expand Down Expand Up @@ -165,6 +208,17 @@ function generateAbiSignatures(networkData: NetworkCache) {
if (methodsByContract.has(contractName)) {
const methods = methodsByContract.get(contractName)!;
const contractMethods = extractAbiMethods(networkData, methods);
const missingMethods = methods.filter(
(methodName) => !contractMethods[methodName]
);

if (missingMethods.length > 0) {
throw new Error(
`Missing ABI definitions for ${contractName}: ${missingMethods.join(
', '
)}. ` + 'Ensure your networkContext.json includes these functions.'
);
}

if (Object.keys(contractMethods).length > 0) {
const address = contractGroup.contracts[0].address_hash;
Expand Down Expand Up @@ -218,6 +272,8 @@ export function buildSignaturesFromContext(
console.log('📊 Generating signatures...');
const signatures = generateAbiSignatures(jsonData);

console.log('✅ Signatures generated successfully for network:', networkName);

return {
signatures,
networkName,
Expand Down Expand Up @@ -308,9 +364,12 @@ module.exports = {
// process.argv[0] is the bun executable
// process.argv[1] is the script being run
const mainScriptPath = path.resolve(process.argv[1] || '');
const currentScriptPath = fileURLToPath(import.meta.url);
const modulePathFromMeta = getModulePathFromImportMeta();
const resolvedModulePath = modulePathFromMeta
? path.resolve(modulePathFromMeta)
: undefined;

if (mainScriptPath === currentScriptPath) {
if (resolvedModulePath && mainScriptPath === resolvedModulePath) {
// This means custom-network-signatures.ts was the script passed to `bun run`
const jsonFilePath = process.argv[2];
const networkName = process.argv[3];
Expand Down
Loading
Loading