forked from LIT-Protocol/agent-wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaveInfoUtils.ts
186 lines (172 loc) · 4.8 KB
/
saveInfoUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { readFileSync, writeFileSync, existsSync } from 'fs';
/**
* Safe PKP Configuration Interface
* ------------------------------
* Defines the structure for Safe PKP system configuration.
*
* Components:
* 1. PKP Information
* 2. Contract Addresses
* 3. Network Configuration
* 4. Owner Settings
*
* @property pkp - PKP token information and credentials
* @property safeAddress - Deployed Safe contract address
* @property pkpToolsAddress - Tools contract address
* @property chainId - Network chain identifier
* @property owners - Array of owner addresses
* @property threshold - Required signature threshold
* @property network - Network configuration details
* @property deploymentTimestamp - Initial deployment time
* @property capacityTokenId - Capacity token ID
*/
interface Config {
pkp: any;
safeAddress: string;
pkpToolsAddress: string;
chainId: number;
owners: string[];
threshold: number;
network: {
name: string;
rpcUrl: string;
chainId: number;
};
deploymentTimestamp: string;
capacityTokenId?: string;
}
/**
* Configuration Management System
* -----------------------------
* Handles Safe PKP configuration persistence and validation.
*
* Core Features:
* 1. Configuration file management
* 2. Validation checks
* 3. Deployment verification
*
* File Structure:
* - Main config: safe-pkp-config.json
*/
export class ConfigManager {
private readonly configPath: string;
private config: Config | null = null;
/**
* Configuration Manager Constructor
* ------------------------------
* Initializes configuration path.
*
* @param configPath - Path to configuration file
*/
constructor(configPath: string = 'safe-pkp-config.json') {
this.configPath = configPath;
}
/**
* Configuration Loader
* ------------------
* Loads configuration from filesystem.
*
* Process:
* 1. Attempts to load configuration
* 2. Returns null if not found
*
* @returns Loaded configuration or null
*/
public loadConfig(): Config | null {
try {
if (existsSync(this.configPath)) {
const configData = readFileSync(this.configPath, 'utf8');
this.config = JSON.parse(configData);
return this.config;
}
return null;
} catch (error) {
console.error('Error loading config:', error);
return null;
}
}
/**
* Configuration Persistence Handler
* ------------------------------
* Saves configuration to filesystem.
*
* Process:
* 1. Merges new config with existing
* 2. Updates timestamps
* 3. Writes to filesystem
*
* @param configData - New configuration to save
* @throws Error if save operation fails
*/
public saveConfig(configData: Partial<Config>): void {
try {
const existingConfig = this.loadConfig();
const newConfig: Config = {
...existingConfig,
...configData,
lastUpdated: new Date().toISOString(),
deploymentTimestamp: existingConfig?.deploymentTimestamp || new Date().toISOString(),
} as Config;
writeFileSync(this.configPath, JSON.stringify(newConfig, null, 2));
this.config = newConfig;
console.log('Configuration saved successfully');
} catch (error) {
console.error('Error saving config:', error);
throw error;
}
}
/**
* Configuration Validator
* ---------------------
* Verifies completeness and validity of configuration.
*
* Checks:
* 1. PKP information
* 2. Contract addresses
* 3. Network settings
* 4. Owner configuration
*
* @returns True if configuration is valid
*/
public validateConfig(): boolean {
if (!this.config) return false;
const requiredFields = [
'pkp',
'safeAddress',
'pkpToolsAddress',
'chainId',
'owners',
'threshold',
'network'
];
return requiredFields.every(field => {
const value = this.config![field as keyof Config];
return value !== undefined && value !== null;
});
}
/**
* Deployment Verification System
* ---------------------------
* Verifies contract deployments on the blockchain.
*
* Verifications:
* 1. Safe contract deployment
* 2. Tools contract deployment
*
* @param provider - Ethereum provider instance
* @returns True if all deployments are verified
*/
public async verifyDeployments(provider: any): Promise<boolean> {
if (!this.config) return false;
try {
const safeCode = await provider.getCode(this.config.safeAddress);
if (safeCode === '0x') return false;
const toolsCode = await provider.getCode(this.config.pkpToolsAddress);
if (toolsCode === '0x') return false;
return true;
} catch (error) {
console.error('Error verifying deployments:', error);
return false;
}
}
}