Skip to content

Commit 3bf4166

Browse files
authored
Merge pull request #986 from LIT-Protocol/fix/auth-service-docker-build-issue
fix(contracts): guard module path resolution without import.meta.url
2 parents 032ac24 + 4984b44 commit 3bf4166

File tree

11 files changed

+11069
-15
lines changed

11 files changed

+11069
-15
lines changed

.changeset/mighty-dryers-prove.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@lit-protocol/contracts': patch
3+
---
4+
5+
Provide a browser-only stub for `custom-network-signatures` so web builds skip the Node-specific implementation
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
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.";
3+
function throwBrowserUnsupported() {
4+
throw new Error(UNSUPPORTED_MESSAGE);
5+
}
6+
function buildSignaturesFromContext(_options) {
7+
return throwBrowserUnsupported();
8+
}
9+
async function generateSignaturesFromContext(_options) {
10+
throwBrowserUnsupported();
11+
}
12+
module.exports = {
13+
buildSignaturesFromContext,
14+
generateSignaturesFromContext
15+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Browser stub for @lit-protocol/contracts/custom-network-signatures.
3+
* These utilities require Node.js filesystem access and cannot run in the browser.
4+
*/
5+
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.";
6+
function throwBrowserUnsupported() {
7+
throw new Error(UNSUPPORTED_MESSAGE);
8+
}
9+
function buildSignaturesFromContext(_options) {
10+
return throwBrowserUnsupported();
11+
}
12+
async function generateSignaturesFromContext(_options) {
13+
throwBrowserUnsupported();
14+
}
15+
export {
16+
buildSignaturesFromContext,
17+
generateSignaturesFromContext
18+
};

packages/contracts/dist/custom-network-signatures.cjs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ var METHODS_TO_EXTRACT = [
7575
"PubkeyRouter.ethAddressToPkpId",
7676
"PubkeyRouter.getPubkey",
7777
"PubkeyRouter.getEthAddress",
78+
"PubkeyRouter.getDerivedPubkey",
7879
// Ledger:
7980
"Ledger.deposit",
8081
"Ledger.depositForUser",
@@ -5064,6 +5065,27 @@ function extractAbiMethods(networkCache, methodNames) {
50645065
}
50655066

50665067
// packages/contracts/src/custom-network-signatures.ts
5068+
function getModulePathFromImportMeta() {
5069+
const moduleUrl = __import_meta__?.url;
5070+
if (typeof moduleUrl === "string") {
5071+
try {
5072+
return (0, import_url.fileURLToPath)(moduleUrl);
5073+
} catch (error) {
5074+
console.warn("Failed to resolve fileURLToPath from import.meta.url:", error);
5075+
}
5076+
}
5077+
return void 0;
5078+
}
5079+
function getCurrentModulePath() {
5080+
const modulePath = getModulePathFromImportMeta();
5081+
if (modulePath) {
5082+
return modulePath;
5083+
}
5084+
if (typeof __filename !== "undefined") {
5085+
return __filename;
5086+
}
5087+
return void 0;
5088+
}
50675089
function getBaseDirectory(useScriptDirectory = false, callerPath) {
50685090
if (useScriptDirectory) {
50695091
if (callerPath) {
@@ -5075,9 +5097,14 @@ function getBaseDirectory(useScriptDirectory = false, callerPath) {
50755097
console.log("Using __dirname:", __dirname);
50765098
return __dirname;
50775099
}
5078-
const moduleDir = (0, import_path.dirname)((0, import_url.fileURLToPath)(__import_meta__.url));
5079-
console.log("Using module directory:", moduleDir);
5080-
return moduleDir;
5100+
const modulePath = getCurrentModulePath();
5101+
if (modulePath) {
5102+
const moduleDir = (0, import_path.dirname)(modulePath);
5103+
console.log("Using module directory:", moduleDir);
5104+
return moduleDir;
5105+
}
5106+
console.log("Using current working directory:", process.cwd());
5107+
return process.cwd();
50815108
}
50825109
const cwd = process.cwd();
50835110
console.log("Using current working directory:", cwd);
@@ -5122,6 +5149,14 @@ function generateAbiSignatures(networkData) {
51225149
if (methodsByContract.has(contractName)) {
51235150
const methods = methodsByContract.get(contractName);
51245151
const contractMethods = extractAbiMethods(networkData, methods);
5152+
const missingMethods = methods.filter(
5153+
(methodName) => !contractMethods[methodName]
5154+
);
5155+
if (missingMethods.length > 0) {
5156+
throw new Error(
5157+
`Missing ABI definitions for ${contractName}: ${missingMethods.join(", ")}. Ensure your networkContext.json includes these functions.`
5158+
);
5159+
}
51255160
if (Object.keys(contractMethods).length > 0) {
51265161
const address = contractGroup.contracts[0].address_hash;
51275162
const events = contractGroup.contracts[0].ABI.filter(
@@ -5230,8 +5265,9 @@ module.exports = {
52305265
}
52315266
}
52325267
var mainScriptPath = import_path.default.resolve(process.argv[1] || "");
5233-
var currentScriptPath = (0, import_url.fileURLToPath)(__import_meta__.url);
5234-
if (mainScriptPath === currentScriptPath) {
5268+
var modulePathFromMeta = getModulePathFromImportMeta();
5269+
var resolvedModulePath = modulePathFromMeta ? import_path.default.resolve(modulePathFromMeta) : void 0;
5270+
if (resolvedModulePath && mainScriptPath === resolvedModulePath) {
52355271
const jsonFilePath = process.argv[2];
52365272
const networkName = process.argv[3];
52375273
if (!jsonFilePath) {

packages/contracts/dist/custom-network-signatures.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var METHODS_TO_EXTRACT = [
3939
"PubkeyRouter.ethAddressToPkpId",
4040
"PubkeyRouter.getPubkey",
4141
"PubkeyRouter.getEthAddress",
42+
"PubkeyRouter.getDerivedPubkey",
4243
// Ledger:
4344
"Ledger.deposit",
4445
"Ledger.depositForUser",
@@ -5028,6 +5029,27 @@ function extractAbiMethods(networkCache, methodNames) {
50285029
}
50295030

50305031
// packages/contracts/src/custom-network-signatures.ts
5032+
function getModulePathFromImportMeta() {
5033+
const moduleUrl = import.meta?.url;
5034+
if (typeof moduleUrl === "string") {
5035+
try {
5036+
return fileURLToPath(moduleUrl);
5037+
} catch (error) {
5038+
console.warn("Failed to resolve fileURLToPath from import.meta.url:", error);
5039+
}
5040+
}
5041+
return void 0;
5042+
}
5043+
function getCurrentModulePath() {
5044+
const modulePath = getModulePathFromImportMeta();
5045+
if (modulePath) {
5046+
return modulePath;
5047+
}
5048+
if (typeof __filename !== "undefined") {
5049+
return __filename;
5050+
}
5051+
return void 0;
5052+
}
50315053
function getBaseDirectory(useScriptDirectory = false, callerPath) {
50325054
if (useScriptDirectory) {
50335055
if (callerPath) {
@@ -5039,9 +5061,14 @@ function getBaseDirectory(useScriptDirectory = false, callerPath) {
50395061
console.log("Using __dirname:", __dirname);
50405062
return __dirname;
50415063
}
5042-
const moduleDir = dirname(fileURLToPath(import.meta.url));
5043-
console.log("Using module directory:", moduleDir);
5044-
return moduleDir;
5064+
const modulePath = getCurrentModulePath();
5065+
if (modulePath) {
5066+
const moduleDir = dirname(modulePath);
5067+
console.log("Using module directory:", moduleDir);
5068+
return moduleDir;
5069+
}
5070+
console.log("Using current working directory:", process.cwd());
5071+
return process.cwd();
50455072
}
50465073
const cwd = process.cwd();
50475074
console.log("Using current working directory:", cwd);
@@ -5086,6 +5113,14 @@ function generateAbiSignatures(networkData) {
50865113
if (methodsByContract.has(contractName)) {
50875114
const methods = methodsByContract.get(contractName);
50885115
const contractMethods = extractAbiMethods(networkData, methods);
5116+
const missingMethods = methods.filter(
5117+
(methodName) => !contractMethods[methodName]
5118+
);
5119+
if (missingMethods.length > 0) {
5120+
throw new Error(
5121+
`Missing ABI definitions for ${contractName}: ${missingMethods.join(", ")}. Ensure your networkContext.json includes these functions.`
5122+
);
5123+
}
50895124
if (Object.keys(contractMethods).length > 0) {
50905125
const address = contractGroup.contracts[0].address_hash;
50915126
const events = contractGroup.contracts[0].ABI.filter(
@@ -5194,8 +5229,9 @@ module.exports = {
51945229
}
51955230
}
51965231
var mainScriptPath = path.resolve(process.argv[1] || "");
5197-
var currentScriptPath = fileURLToPath(import.meta.url);
5198-
if (mainScriptPath === currentScriptPath) {
5232+
var modulePathFromMeta = getModulePathFromImportMeta();
5233+
var resolvedModulePath = modulePathFromMeta ? path.resolve(modulePathFromMeta) : void 0;
5234+
if (resolvedModulePath && mainScriptPath === resolvedModulePath) {
51995235
const jsonFilePath = process.argv[2];
52005236
const networkName = process.argv[3];
52015237
if (!jsonFilePath) {

packages/contracts/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@
9494
"types": "./dist/signatures/develop.d.ts"
9595
},
9696
"./custom-network-signatures": {
97+
"browser": {
98+
"import": "./dist/custom-network-signatures.browser.js",
99+
"require": "./dist/custom-network-signatures.browser.cjs"
100+
},
97101
"import": "./dist/custom-network-signatures.js",
98102
"require": "./dist/custom-network-signatures.cjs",
99103
"types": "./dist/custom-network-signatures.d.ts"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type {
2+
BuildSignaturesFromContextOptions,
3+
BuildSignaturesFromContextResult,
4+
GenerateSignaturesOptions,
5+
} from './custom-network-signatures';
6+
7+
const UNSUPPORTED_MESSAGE =
8+
'@lit-protocol/contracts/custom-network-signatures is not supported in browser environments. ' +
9+
'Please generate contract signatures ahead of time in a Node.js process and ship the artifacts instead.';
10+
11+
function throwBrowserUnsupported(): never {
12+
throw new Error(UNSUPPORTED_MESSAGE);
13+
}
14+
15+
export function buildSignaturesFromContext(
16+
_options: BuildSignaturesFromContextOptions
17+
): BuildSignaturesFromContextResult {
18+
return throwBrowserUnsupported();
19+
}
20+
21+
export async function generateSignaturesFromContext(
22+
_options: GenerateSignaturesOptions
23+
): Promise<never> {
24+
throwBrowserUnsupported();
25+
}
26+
27+
export type {
28+
BuildSignaturesFromContextOptions,
29+
BuildSignaturesFromContextResult,
30+
GenerateSignaturesOptions,
31+
} from './custom-network-signatures';

packages/contracts/src/custom-network-signatures.ts

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,44 @@ export interface BuildSignaturesFromContextResult {
5252
baseDirectory: string;
5353
}
5454

55+
/**
56+
* Resolves the on-disk path of this module in both ESM and CJS bundles.
57+
* Falls back to __filename when bundlers strip import.meta.url.
58+
*/
59+
function getModulePathFromImportMeta(): string | undefined {
60+
const moduleUrl = (import.meta as unknown as { url?: string } | undefined)
61+
?.url;
62+
if (typeof moduleUrl === 'string') {
63+
try {
64+
return fileURLToPath(moduleUrl);
65+
} catch (error) {
66+
console.warn(
67+
'Failed to resolve fileURLToPath from import.meta.url:',
68+
error
69+
);
70+
}
71+
}
72+
73+
return undefined;
74+
}
75+
76+
/**
77+
* Resolves the on-disk path of this module in both ESM and CJS bundles.
78+
* Falls back to __filename when bundlers strip import.meta.url.
79+
*/
80+
function getCurrentModulePath(): string | undefined {
81+
const modulePath = getModulePathFromImportMeta();
82+
if (modulePath) {
83+
return modulePath;
84+
}
85+
86+
if (typeof __filename !== 'undefined') {
87+
return __filename;
88+
}
89+
90+
return undefined;
91+
}
92+
5593
/**
5694
* Gets the base directory for resolving paths
5795
* @param useScriptDirectory - Whether to use script's directory or current working directory
@@ -75,9 +113,14 @@ function getBaseDirectory(
75113
return __dirname;
76114
}
77115
// When running as module without callerPath
78-
const moduleDir = dirname(fileURLToPath(import.meta.url));
79-
console.log('Using module directory:', moduleDir);
80-
return moduleDir;
116+
const modulePath = getCurrentModulePath();
117+
if (modulePath) {
118+
const moduleDir = dirname(modulePath);
119+
console.log('Using module directory:', moduleDir);
120+
return moduleDir;
121+
}
122+
console.log('Using current working directory:', process.cwd());
123+
return process.cwd();
81124
}
82125
// Use current working directory
83126
const cwd = process.cwd();
@@ -165,6 +208,17 @@ function generateAbiSignatures(networkData: NetworkCache) {
165208
if (methodsByContract.has(contractName)) {
166209
const methods = methodsByContract.get(contractName)!;
167210
const contractMethods = extractAbiMethods(networkData, methods);
211+
const missingMethods = methods.filter(
212+
(methodName) => !contractMethods[methodName]
213+
);
214+
215+
if (missingMethods.length > 0) {
216+
throw new Error(
217+
`Missing ABI definitions for ${contractName}: ${missingMethods.join(
218+
', '
219+
)}. ` + 'Ensure your networkContext.json includes these functions.'
220+
);
221+
}
168222

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

275+
console.log('✅ Signatures generated successfully for network:', networkName);
276+
221277
return {
222278
signatures,
223279
networkName,
@@ -308,9 +364,12 @@ module.exports = {
308364
// process.argv[0] is the bun executable
309365
// process.argv[1] is the script being run
310366
const mainScriptPath = path.resolve(process.argv[1] || '');
311-
const currentScriptPath = fileURLToPath(import.meta.url);
367+
const modulePathFromMeta = getModulePathFromImportMeta();
368+
const resolvedModulePath = modulePathFromMeta
369+
? path.resolve(modulePathFromMeta)
370+
: undefined;
312371

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

0 commit comments

Comments
 (0)