Skip to content

Commit abd2cde

Browse files
committed
fix(naga-local): add assertion and TS Typeguard
1 parent 761174a commit abd2cde

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

packages/e2e/src/init.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ViemAccountAuthenticator,
55
} from '@lit-protocol/auth';
66
import { createLitClient, utils as litUtils } from '@lit-protocol/lit-client';
7+
import type { NagaLocalModule } from '@lit-protocol/networks';
78
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
89
import { z } from 'zod';
910
import { fundAccount } from './helper/fundAccount';
@@ -130,17 +131,24 @@ export const init = async (
130131
if (_network === 'naga-local') {
131132
const localContextPath = process.env['NAGA_LOCAL_CONTEXT_PATH'];
132133
if (localContextPath) {
133-
const withLocalContext = (_baseNetworkModule as any)?.withLocalContext;
134-
135-
if (typeof withLocalContext === 'function') {
134+
const isNagaLocalModule = (
135+
module: unknown
136+
): module is NagaLocalModule =>
137+
!!module &&
138+
typeof (module as { withLocalContext?: unknown }).withLocalContext ===
139+
'function';
140+
141+
if (isNagaLocalModule(_baseNetworkModule)) {
136142
const localContextName = process.env['NAGA_LOCAL_CONTEXT_NAME'];
137143

138144
console.log(
139145
'✅ Loading naga-local signatures from NAGA_LOCAL_CONTEXT_PATH:',
140146
localContextPath
141147
);
142148

143-
_baseNetworkModule = await withLocalContext({
149+
const nagaLocalModule: NagaLocalModule = _baseNetworkModule;
150+
151+
_baseNetworkModule = await nagaLocalModule.withLocalContext({
144152
networkContextPath: localContextPath,
145153
networkName: localContextName,
146154
});

packages/networks/src/networks/vNaga/envs/naga-local/naga-local.module.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
nagaLocalEnvironment,
77
type NagaLocalSignatures,
88
} from './naga-local.env';
9+
import { signatures as defaultSignatures } from './generated/naga-develop';
910
import type { ExpectedAccountOrWalletClient } from '../../shared/managers/contract-manager/createContractsManager';
1011

1112
type NagaLocalContextOptions = {
@@ -14,6 +15,32 @@ type NagaLocalContextOptions = {
1415
rpcUrlOverride?: string;
1516
};
1617

18+
const REQUIRED_SIGNATURE_KEYS = Object.keys(
19+
defaultSignatures
20+
) as (keyof NagaLocalSignatures)[];
21+
22+
function assertIsNagaLocalSignatures(
23+
value: unknown
24+
): asserts value is NagaLocalSignatures {
25+
if (typeof value !== 'object' || value === null) {
26+
throw new Error('Generated signatures is not an object');
27+
}
28+
29+
for (const key of REQUIRED_SIGNATURE_KEYS) {
30+
const contract = (value as Record<string, unknown>)[key];
31+
if (
32+
typeof contract !== 'object' ||
33+
contract === null ||
34+
typeof (contract as { address?: unknown }).address !== 'string' ||
35+
typeof (contract as { methods?: unknown }).methods !== 'object'
36+
) {
37+
throw new Error(
38+
`Generated signatures missing required contract metadata for ${key as string}`
39+
);
40+
}
41+
}
42+
}
43+
1744
const createChainManager = (
1845
env: NagaLocalEnvironment,
1946
account: ExpectedAccountOrWalletClient
@@ -48,12 +75,12 @@ const buildModule = (env: NagaLocalEnvironment) => {
4875
networkName: networkName ?? 'naga-develop',
4976
});
5077

51-
const resolvedSignatures = signatures as unknown as NagaLocalSignatures;
78+
assertIsNagaLocalSignatures(signatures);
5279

5380
return createWithEnv(
5481
new NagaLocalEnvironment({
5582
rpcUrlOverride: rpcUrlOverride ?? env.getConfig().rpcUrl,
56-
signatures: resolvedSignatures,
83+
signatures,
5784
})
5885
);
5986
};

0 commit comments

Comments
 (0)