fuego-cli 0.1.0

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.
@@ -0,0 +1,189 @@
1
+ import {
2
+ Keypair,
3
+ Connection,
4
+ PublicKey,
5
+ Transaction,
6
+ SystemProgram,
7
+ LAMPORTS_PER_SOL
8
+ } from '@solana/web3.js';
9
+ import fs from 'fs-extra';
10
+ import path from 'path';
11
+ import { getWalletPath, getWalletConfigPath, saveWalletConfig } from './config.js';
12
+
13
+ export interface WalletBalance {
14
+ sol: number;
15
+ tokens: Array<{
16
+ mint: string;
17
+ symbol: string;
18
+ amount: string;
19
+ decimals: number;
20
+ }>;
21
+ }
22
+
23
+ export interface SendResult {
24
+ signature: string;
25
+ confirmation: string;
26
+ }
27
+
28
+ export interface TransactionRecord {
29
+ signature: string;
30
+ type: 'incoming' | 'outgoing';
31
+ amount: string;
32
+ token: string;
33
+ timestamp: number;
34
+ counterparty?: string;
35
+ }
36
+
37
+ export class FuegoWallet {
38
+ private walletPath: string;
39
+ private keypair?: Keypair;
40
+
41
+ constructor(walletPath?: string) {
42
+ this.walletPath = walletPath || getWalletPath();
43
+ }
44
+
45
+ exists(): boolean {
46
+ return fs.existsSync(this.walletPath);
47
+ }
48
+
49
+ async create(name?: string): Promise<{ publicKey: string; mnemonic?: string }> {
50
+ // Ensure directory exists
51
+ await fs.ensureDir(path.dirname(this.walletPath));
52
+
53
+ // Generate new keypair
54
+ this.keypair = Keypair.generate();
55
+ const publicKey = this.keypair.publicKey.toBase58();
56
+
57
+ // Save wallet.json with ONLY the keypair (minimal, secure)
58
+ const walletData = {
59
+ secretKey: Array.from(this.keypair.secretKey)
60
+ };
61
+
62
+ await fs.writeJson(this.walletPath, walletData);
63
+
64
+ // Set restrictive permissions (owner read/write only)
65
+ await fs.chmod(this.walletPath, 0o600);
66
+
67
+ // Save wallet-config.json with metadata (safe to modify)
68
+ saveWalletConfig({
69
+ publicKey,
70
+ name: name || 'default',
71
+ createdAt: new Date().toISOString(),
72
+ version: '0.1.0'
73
+ });
74
+
75
+ return {
76
+ publicKey,
77
+ mnemonic: undefined
78
+ };
79
+ }
80
+
81
+ load(): Keypair {
82
+ if (this.keypair) return this.keypair;
83
+
84
+ if (!this.exists()) {
85
+ throw new Error('Wallet not found. Run "fuego init" first.');
86
+ }
87
+
88
+ const walletData = fs.readJsonSync(this.walletPath);
89
+ this.keypair = Keypair.fromSecretKey(Uint8Array.from(walletData.secretKey));
90
+
91
+ return this.keypair;
92
+ }
93
+
94
+ getPublicKey(): string {
95
+ if (this.keypair) {
96
+ return this.keypair.publicKey.toBase58();
97
+ }
98
+
99
+ if (!this.exists()) {
100
+ throw new Error('Wallet not found. Run "fuego init" first.');
101
+ }
102
+
103
+ // Read from wallet-config.json if available, otherwise from wallet.json
104
+ const configPath = getWalletConfigPath();
105
+ if (fs.existsSync(configPath)) {
106
+ const config = fs.readJsonSync(configPath);
107
+ return config.publicKey;
108
+ }
109
+
110
+ // Fallback: derive from wallet.json
111
+ const walletData = fs.readJsonSync(this.walletPath);
112
+ const kp = Keypair.fromSecretKey(Uint8Array.from(walletData.secretKey));
113
+ return kp.publicKey.toBase58();
114
+ }
115
+
116
+ async getBalance(): Promise<WalletBalance> {
117
+ const keypair = this.load();
118
+ const connection = this.getConnection();
119
+
120
+ const lamports = await connection.getBalance(keypair.publicKey);
121
+
122
+ // TODO: Fetch SPL token balances
123
+
124
+ return {
125
+ sol: lamports / LAMPORTS_PER_SOL,
126
+ tokens: []
127
+ };
128
+ }
129
+
130
+ async send(params: {
131
+ to: string;
132
+ amount: number;
133
+ token: string;
134
+ network?: string;
135
+ }): Promise<SendResult> {
136
+ const keypair = this.load();
137
+ const connection = this.getConnection(params.network);
138
+
139
+ const recipient = new PublicKey(params.to);
140
+
141
+ if (params.token === 'SOL') {
142
+ const lamports = params.amount * LAMPORTS_PER_SOL;
143
+
144
+ const transaction = new Transaction().add(
145
+ SystemProgram.transfer({
146
+ fromPubkey: keypair.publicKey,
147
+ toPubkey: recipient,
148
+ lamports
149
+ })
150
+ );
151
+
152
+ const signature = await connection.sendTransaction(transaction, [keypair]);
153
+
154
+ // Wait for confirmation
155
+ await connection.confirmTransaction(signature, 'confirmed');
156
+
157
+ return {
158
+ signature,
159
+ confirmation: 'confirmed'
160
+ };
161
+ } else {
162
+ // TODO: SPL token transfers
163
+ throw new Error('SPL token transfers coming in v0.2');
164
+ }
165
+ }
166
+
167
+ async getHistory(limit: number): Promise<TransactionRecord[]> {
168
+ const keypair = this.load();
169
+ const connection = this.getConnection();
170
+
171
+ const signatures = await connection.getSignaturesForAddress(
172
+ keypair.publicKey,
173
+ { limit }
174
+ );
175
+
176
+ return signatures.map(sig => ({
177
+ signature: sig.signature,
178
+ type: sig.err ? 'outgoing' : 'incoming', // Simplified - actual logic needs transaction parsing
179
+ amount: '0', // TODO: Parse actual amount
180
+ token: 'SOL',
181
+ timestamp: sig.blockTime ? sig.blockTime * 1000 : Date.now(),
182
+ }));
183
+ }
184
+
185
+ private getConnection(rpcUrl?: string): Connection {
186
+ const endpoint = rpcUrl || process.env.FUEGO_RPC_URL || 'https://api.mainnet-beta.solana.com';
187
+ return new Connection(endpoint, 'confirmed');
188
+ }
189
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "NodeNext",
5
+ "moduleResolution": "NodeNext",
6
+ "lib": ["ES2022"],
7
+ "outDir": "./dist",
8
+ "rootDir": "./src",
9
+ "strict": true,
10
+ "esModuleInterop": true,
11
+ "skipLibCheck": true,
12
+ "forceConsistentCasingInFileNames": true,
13
+ "resolveJsonModule": true,
14
+ "declaration": true,
15
+ "declarationMap": true,
16
+ "sourceMap": true
17
+ },
18
+ "include": ["src/**/*"],
19
+ "exclude": ["node_modules", "dist"]
20
+ }