agentvault 1.0.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.
- package/.dfx/local/network-id +4 -0
- package/.next/trace +2 -0
- package/.vercel/README.txt +11 -0
- package/.vercel/project.json +1 -0
- package/AGENTS.md +43 -0
- package/CHANGELOG.md +196 -0
- package/LICENSE +21 -0
- package/PLAN_VAULT_INTEGRATION.md +318 -0
- package/README.md +253 -0
- package/backups/agentvault-backup-test-agent-2026-02-12T17-54-28-967Z.json +28 -0
- package/backups/agentvault-backup-test-agent-2026-02-12T17-54-29-032Z.backup +1 -0
- package/backups/agentvault-backup-test-agent-2026-02-12T17-57-42-373Z.json +28 -0
- package/backups/agentvault-backup-test-agent-2026-02-12T17-57-42-428Z.backup +1 -0
- package/backups/agentvault-backup-test-agent-2026-02-12T18-52-25-132Z.json +28 -0
- package/backups/agentvault-backup-test-agent-2026-02-12T18-52-25-247Z.backup +1 -0
- package/backups/agentvault-backup-test-agent-2026-02-12T18-54-09-216Z.json +28 -0
- package/backups/agentvault-backup-test-agent-2026-02-12T18-54-09-283Z.backup +1 -0
- package/backups/agentvault-backup-test-agent-2026-02-12T22-18-22-772Z.backup +1 -0
- package/backups/agentvault-backup-test-agent-2026-02-12T22-18-22-793Z.json +28 -0
- package/backups/test-backup.json +28 -0
- package/dist/cli/commands/approve.d.ts +4 -0
- package/dist/cli/commands/approve.js +232 -0
- package/dist/cli/commands/archive.d.ts +4 -0
- package/dist/cli/commands/archive.js +192 -0
- package/dist/cli/commands/backup.d.ts +4 -0
- package/dist/cli/commands/backup.js +164 -0
- package/dist/cli/commands/cloud-backup.d.ts +4 -0
- package/dist/cli/commands/cloud-backup.js +221 -0
- package/dist/cli/commands/cycles.d.ts +8 -0
- package/dist/cli/commands/cycles.js +83 -0
- package/dist/cli/commands/decrypt.d.ts +16 -0
- package/dist/cli/commands/decrypt.js +101 -0
- package/dist/cli/commands/deploy.d.ts +32 -0
- package/dist/cli/commands/deploy.js +208 -0
- package/dist/cli/commands/exec.d.ts +26 -0
- package/dist/cli/commands/exec.js +109 -0
- package/dist/cli/commands/fetch.d.ts +23 -0
- package/dist/cli/commands/fetch.js +164 -0
- package/dist/cli/commands/health.d.ts +8 -0
- package/dist/cli/commands/health.js +72 -0
- package/dist/cli/commands/identity.d.ts +8 -0
- package/dist/cli/commands/identity.js +140 -0
- package/dist/cli/commands/inference.d.ts +4 -0
- package/dist/cli/commands/inference.js +225 -0
- package/dist/cli/commands/info.d.ts +8 -0
- package/dist/cli/commands/info.js +59 -0
- package/dist/cli/commands/init.d.ts +19 -0
- package/dist/cli/commands/init.js +135 -0
- package/dist/cli/commands/instrument.d.ts +8 -0
- package/dist/cli/commands/instrument.js +35 -0
- package/dist/cli/commands/list.d.ts +36 -0
- package/dist/cli/commands/list.js +173 -0
- package/dist/cli/commands/logs.d.ts +8 -0
- package/dist/cli/commands/logs.js +96 -0
- package/dist/cli/commands/monitor.d.ts +8 -0
- package/dist/cli/commands/monitor.js +84 -0
- package/dist/cli/commands/network.d.ts +14 -0
- package/dist/cli/commands/network.js +258 -0
- package/dist/cli/commands/package.d.ts +36 -0
- package/dist/cli/commands/package.js +188 -0
- package/dist/cli/commands/profile.d.ts +8 -0
- package/dist/cli/commands/profile.js +76 -0
- package/dist/cli/commands/promote.d.ts +8 -0
- package/dist/cli/commands/promote.js +89 -0
- package/dist/cli/commands/rebuild.d.ts +21 -0
- package/dist/cli/commands/rebuild.js +140 -0
- package/dist/cli/commands/rollback.d.ts +8 -0
- package/dist/cli/commands/rollback.js +120 -0
- package/dist/cli/commands/show.d.ts +36 -0
- package/dist/cli/commands/show.js +200 -0
- package/dist/cli/commands/stats.d.ts +8 -0
- package/dist/cli/commands/stats.js +34 -0
- package/dist/cli/commands/status.d.ts +14 -0
- package/dist/cli/commands/status.js +83 -0
- package/dist/cli/commands/test.d.ts +8 -0
- package/dist/cli/commands/test.js +109 -0
- package/dist/cli/commands/tokens.d.ts +8 -0
- package/dist/cli/commands/tokens.js +62 -0
- package/dist/cli/commands/trace.d.ts +8 -0
- package/dist/cli/commands/trace.js +68 -0
- package/dist/cli/commands/wallet-export.d.ts +13 -0
- package/dist/cli/commands/wallet-export.js +140 -0
- package/dist/cli/commands/wallet-history.d.ts +10 -0
- package/dist/cli/commands/wallet-history.js +127 -0
- package/dist/cli/commands/wallet-import.d.ts +10 -0
- package/dist/cli/commands/wallet-import.js +209 -0
- package/dist/cli/commands/wallet-multi-send.d.ts +17 -0
- package/dist/cli/commands/wallet-multi-send.js +195 -0
- package/dist/cli/commands/wallet-process-queue.d.ts +19 -0
- package/dist/cli/commands/wallet-process-queue.js +209 -0
- package/dist/cli/commands/wallet-sign.d.ts +13 -0
- package/dist/cli/commands/wallet-sign.js +207 -0
- package/dist/cli/commands/wallet.d.ts +12 -0
- package/dist/cli/commands/wallet.js +794 -0
- package/dist/cli/index.d.ts +10 -0
- package/dist/cli/index.js +96 -0
- package/dist/vitest.config.d.ts +3 -0
- package/dist/vitest.config.js +14 -0
- package/fixup_1_0_OSS_release.md +136 -0
- package/fixup_REALEASE_PRD.md +136 -0
- package/package.json +79 -0
- package/pnpm-workspace.yaml +5 -0
- package/scripts/dev-dashboard.mjs +84 -0
- package/site/README.md +63 -0
- package/site/docusaurus.config.ts +148 -0
- package/site/package-lock.json +18383 -0
- package/site/package.json +47 -0
- package/site/sidebars.ts +86 -0
- package/site/static/.gitkeep +0 -0
- package/site/static/img/logo.svg +28 -0
- package/site/static/img/og-image.svg +35 -0
- package/src/archival/archive-manager.ts +372 -0
- package/src/archival/arweave-client.ts +289 -0
- package/src/archival/index.ts +8 -0
- package/src/backup/backup.ts +315 -0
- package/src/backup/index.ts +7 -0
- package/src/cloud-storage/cloud-sync.ts +461 -0
- package/src/cloud-storage/index.ts +11 -0
- package/src/cloud-storage/provider-detector.ts +198 -0
- package/src/cloud-storage/types.ts +104 -0
- package/src/debugging/index.ts +6 -0
- package/src/debugging/logs.ts +193 -0
- package/src/debugging/types.ts +100 -0
- package/src/deployment/deployer.ts +274 -0
- package/src/deployment/icpClient.ts +620 -0
- package/src/deployment/index.ts +46 -0
- package/src/deployment/promotion.ts +161 -0
- package/src/deployment/types.ts +111 -0
- package/src/icp/batch.ts +374 -0
- package/src/icp/cycles.ts +50 -0
- package/src/icp/environment.ts +215 -0
- package/src/icp/icpcli.ts +438 -0
- package/src/icp/icwasm.ts +222 -0
- package/src/icp/identity.ts +77 -0
- package/src/icp/index.ts +94 -0
- package/src/icp/optimization.ts +242 -0
- package/src/icp/tokens.ts +36 -0
- package/src/icp/tool-detector.ts +110 -0
- package/src/icp/types.ts +574 -0
- package/src/index.ts +25 -0
- package/src/inference/bittensor-client.ts +304 -0
- package/src/inference/index.ts +8 -0
- package/src/inference/inference-manager.ts +327 -0
- package/src/metrics/index.ts +7 -0
- package/src/metrics/metrics.ts +186 -0
- package/src/monitoring/alerting.ts +190 -0
- package/src/monitoring/health.ts +197 -0
- package/src/monitoring/index.ts +38 -0
- package/src/monitoring/info.ts +114 -0
- package/src/monitoring/types.ts +99 -0
- package/src/network/index.ts +5 -0
- package/src/network/network-config.ts +129 -0
- package/src/packaging/compiler.ts +647 -0
- package/src/packaging/config-persistence.ts +135 -0
- package/src/packaging/config-schemas.ts +156 -0
- package/src/packaging/detector.ts +220 -0
- package/src/packaging/index.ts +90 -0
- package/src/packaging/packager.ts +118 -0
- package/src/packaging/parsers/clawdbot.ts +278 -0
- package/src/packaging/parsers/cline.ts +223 -0
- package/src/packaging/parsers/generic.ts +266 -0
- package/src/packaging/parsers/goose.ts +214 -0
- package/src/packaging/parsers/index.ts +11 -0
- package/src/packaging/serializer.ts +260 -0
- package/src/packaging/types.ts +144 -0
- package/src/packaging/wasmedge-compiler.ts +406 -0
- package/src/security/index.ts +17 -0
- package/src/security/multisig.ts +415 -0
- package/src/security/types.ts +416 -0
- package/src/security/vetkeys.ts +655 -0
- package/src/testing/index.ts +6 -0
- package/src/testing/local-runner.ts +264 -0
- package/src/testing/types.ts +104 -0
- package/src/wallet/cbor-serializer.ts +323 -0
- package/src/wallet/chain-dispatcher.ts +313 -0
- package/src/wallet/cross-chain-aggregator.ts +346 -0
- package/src/wallet/index.ts +76 -0
- package/src/wallet/key-derivation.ts +425 -0
- package/src/wallet/providers/base-provider.ts +154 -0
- package/src/wallet/providers/cketh-provider.ts +434 -0
- package/src/wallet/providers/polkadot-provider.ts +503 -0
- package/src/wallet/providers/solana-provider.ts +490 -0
- package/src/wallet/transaction-queue.ts +284 -0
- package/src/wallet/types.ts +178 -0
- package/src/wallet/vetkeys-adapter.ts +431 -0
- package/src/wallet/wallet-manager.ts +597 -0
- package/src/wallet/wallet-storage.ts +380 -0
- package/vercel.json +8 -0
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Solana Wallet Provider (Full Implementation)
|
|
3
|
+
*
|
|
4
|
+
* Complete provider for Solana wallet operations.
|
|
5
|
+
* Integrates with @solana/web3.js for key derivation.
|
|
6
|
+
* Uses Connection for real RPC network interactions.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
Balance,
|
|
11
|
+
Transaction,
|
|
12
|
+
TransactionRequest,
|
|
13
|
+
SignedTransaction,
|
|
14
|
+
ProviderConfig,
|
|
15
|
+
} from '../types.js';
|
|
16
|
+
import { BaseWalletProvider } from './base-provider.js';
|
|
17
|
+
import {
|
|
18
|
+
Connection,
|
|
19
|
+
Keypair,
|
|
20
|
+
PublicKey,
|
|
21
|
+
SystemProgram,
|
|
22
|
+
Transaction as SolanaTransaction,
|
|
23
|
+
LAMPORTS_PER_SOL,
|
|
24
|
+
type Commitment,
|
|
25
|
+
} from '@solana/web3.js';
|
|
26
|
+
import { Buffer } from 'buffer';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Solana provider configuration
|
|
30
|
+
*/
|
|
31
|
+
interface SolanaConfig extends ProviderConfig {
|
|
32
|
+
/** Solana JSON-RPC endpoint (HTTP or WebSocket) */
|
|
33
|
+
rpcUrl: string;
|
|
34
|
+
/** Network type (mainnet, devnet) */
|
|
35
|
+
network?: 'mainnet' | 'devnet';
|
|
36
|
+
/** Commitment level */
|
|
37
|
+
commitment?: Commitment;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Solana wallet provider
|
|
42
|
+
*/
|
|
43
|
+
export class SolanaProvider extends BaseWalletProvider {
|
|
44
|
+
private connection: Connection | null = null;
|
|
45
|
+
private keypair: Keypair | null = null;
|
|
46
|
+
private network: string;
|
|
47
|
+
private commitment: Commitment;
|
|
48
|
+
|
|
49
|
+
constructor(config: SolanaConfig) {
|
|
50
|
+
super(config);
|
|
51
|
+
this.network = config.network || 'mainnet';
|
|
52
|
+
this.commitment = config.commitment || 'confirmed';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Connect to Solana network via RPC
|
|
57
|
+
*/
|
|
58
|
+
async connect(): Promise<void> {
|
|
59
|
+
try {
|
|
60
|
+
const rpcUrl = this.config.rpcUrl || this.getDefaultRpcUrl();
|
|
61
|
+
this.connection = new Connection(rpcUrl, this.commitment);
|
|
62
|
+
|
|
63
|
+
// Verify connection by getting latest slot
|
|
64
|
+
const slot = await this.connection.getSlot();
|
|
65
|
+
|
|
66
|
+
this.connected = true;
|
|
67
|
+
console.log(`Connected to Solana ${this.network} at slot ${slot}`);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
70
|
+
throw new Error(`Failed to connect to Solana network: ${message}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Disconnect from Solana network
|
|
76
|
+
*/
|
|
77
|
+
async disconnect(): Promise<void> {
|
|
78
|
+
this.connection = null;
|
|
79
|
+
this.keypair = null;
|
|
80
|
+
this.connected = false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Get wallet balance
|
|
85
|
+
*/
|
|
86
|
+
async getBalance(address: string): Promise<Balance> {
|
|
87
|
+
if (!this.connected || !this.connection) {
|
|
88
|
+
throw new Error('Provider not connected');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
const publicKey = new PublicKey(address);
|
|
93
|
+
const balanceInLamports = await this.connection.getBalance(publicKey);
|
|
94
|
+
const balanceInSol = balanceInLamports / LAMPORTS_PER_SOL;
|
|
95
|
+
|
|
96
|
+
const slot = await this.connection.getSlot();
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
amount: balanceInSol.toString(),
|
|
100
|
+
denomination: 'SOL',
|
|
101
|
+
chain: 'solana',
|
|
102
|
+
address,
|
|
103
|
+
blockNumber: slot,
|
|
104
|
+
};
|
|
105
|
+
} catch (error) {
|
|
106
|
+
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
107
|
+
throw new Error(`Failed to get balance: ${message}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Send transaction
|
|
113
|
+
*/
|
|
114
|
+
async sendTransaction(
|
|
115
|
+
from: string,
|
|
116
|
+
request: TransactionRequest
|
|
117
|
+
): Promise<Transaction> {
|
|
118
|
+
if (!this.connected || !this.connection || !this.keypair) {
|
|
119
|
+
throw new Error('Provider not connected or no wallet loaded');
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
try {
|
|
123
|
+
const toPublicKey = new PublicKey(request.to);
|
|
124
|
+
const fromPublicKey = new PublicKey(from);
|
|
125
|
+
const amountInLamports = this.parseSolAmount(request.amount);
|
|
126
|
+
|
|
127
|
+
// Get latest blockhash
|
|
128
|
+
const { blockhash } = await this.connection.getLatestBlockhash();
|
|
129
|
+
|
|
130
|
+
// Create SystemProgram transfer instruction
|
|
131
|
+
const instruction = SystemProgram.transfer({
|
|
132
|
+
fromPubkey: fromPublicKey,
|
|
133
|
+
toPubkey: toPublicKey,
|
|
134
|
+
lamports: amountInLamports,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// Create transaction
|
|
138
|
+
const transaction = new SolanaTransaction().add(instruction);
|
|
139
|
+
transaction.recentBlockhash = blockhash;
|
|
140
|
+
transaction.feePayer = fromPublicKey;
|
|
141
|
+
|
|
142
|
+
// Sign and send transaction
|
|
143
|
+
const signature = await this.connection.sendTransaction(
|
|
144
|
+
transaction,
|
|
145
|
+
[this.keypair]
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
const fee = await this.estimateFee(request);
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
hash: signature,
|
|
152
|
+
from,
|
|
153
|
+
to: request.to,
|
|
154
|
+
amount: request.amount,
|
|
155
|
+
chain: 'solana',
|
|
156
|
+
timestamp: Date.now(),
|
|
157
|
+
status: 'pending',
|
|
158
|
+
fee,
|
|
159
|
+
data: { memo: request.memo },
|
|
160
|
+
};
|
|
161
|
+
} catch (error) {
|
|
162
|
+
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
163
|
+
throw new Error(`Failed to send transaction: ${message}`);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Sign transaction
|
|
169
|
+
*/
|
|
170
|
+
async signTransaction(
|
|
171
|
+
tx: any,
|
|
172
|
+
privateKey: string
|
|
173
|
+
): Promise<SignedTransaction> {
|
|
174
|
+
if (!this.connection) {
|
|
175
|
+
throw new Error('Provider not connected');
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
try {
|
|
179
|
+
const keypair = this.createKeypairFromPrivateKey(privateKey);
|
|
180
|
+
const toPublicKey = new PublicKey(tx.to);
|
|
181
|
+
const fromPublicKey = keypair.publicKey;
|
|
182
|
+
const amountInLamports = this.parseSolAmount(tx.amount);
|
|
183
|
+
|
|
184
|
+
// Get latest blockhash
|
|
185
|
+
const { blockhash } = await this.connection.getLatestBlockhash();
|
|
186
|
+
|
|
187
|
+
// Create transaction
|
|
188
|
+
const instruction = SystemProgram.transfer({
|
|
189
|
+
fromPubkey: fromPublicKey,
|
|
190
|
+
toPubkey: toPublicKey,
|
|
191
|
+
lamports: amountInLamports,
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
const transaction = new SolanaTransaction().add(instruction);
|
|
195
|
+
transaction.recentBlockhash = blockhash;
|
|
196
|
+
transaction.feePayer = fromPublicKey;
|
|
197
|
+
|
|
198
|
+
// Sign transaction
|
|
199
|
+
transaction.sign(keypair);
|
|
200
|
+
|
|
201
|
+
return {
|
|
202
|
+
txHash: transaction.signature?.toString() || '',
|
|
203
|
+
signedTx: transaction.serialize().toString('base64'),
|
|
204
|
+
signature: Buffer.from(transaction.signature!).toString('hex'),
|
|
205
|
+
request: tx,
|
|
206
|
+
};
|
|
207
|
+
} catch (error) {
|
|
208
|
+
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
209
|
+
throw new Error(`Failed to sign transaction: ${message}`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Get transaction history
|
|
215
|
+
*/
|
|
216
|
+
async getTransactionHistory(address: string): Promise<Transaction[]> {
|
|
217
|
+
if (!this.connected || !this.connection) {
|
|
218
|
+
throw new Error('Provider not connected');
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
try {
|
|
222
|
+
const publicKey = new PublicKey(address);
|
|
223
|
+
const transactions: Transaction[] = [];
|
|
224
|
+
|
|
225
|
+
// Get recent signatures for this address (limit to 20)
|
|
226
|
+
const signatures = await this.connection.getSignaturesForAddress(
|
|
227
|
+
publicKey,
|
|
228
|
+
{ limit: 20 }
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
// Fetch detailed transaction info for each signature
|
|
232
|
+
for (const sig of signatures) {
|
|
233
|
+
try {
|
|
234
|
+
const tx = await this.connection.getParsedTransaction(
|
|
235
|
+
sig.signature,
|
|
236
|
+
{ maxSupportedTransactionVersion: 0 }
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
if (!tx || !tx.meta) continue;
|
|
240
|
+
|
|
241
|
+
// Find transfer instruction
|
|
242
|
+
const transfer = tx.transaction.message.instructions.find(
|
|
243
|
+
(ix: any) => ix.programId.toBase58() === SystemProgram.programId.toBase58()
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
if (!transfer) continue;
|
|
247
|
+
|
|
248
|
+
const parsedIx = (transfer as any).parsed;
|
|
249
|
+
if (parsedIx?.type !== 'transfer') continue;
|
|
250
|
+
|
|
251
|
+
const { from, to, lamports } = parsedIx.info;
|
|
252
|
+
|
|
253
|
+
// Only include transactions involving this address
|
|
254
|
+
if (from !== address && to !== address) continue;
|
|
255
|
+
|
|
256
|
+
transactions.push({
|
|
257
|
+
hash: sig.signature,
|
|
258
|
+
from,
|
|
259
|
+
to,
|
|
260
|
+
amount: (lamports / LAMPORTS_PER_SOL).toFixed(9),
|
|
261
|
+
chain: 'solana',
|
|
262
|
+
timestamp: (sig.blockTime || Date.now() / 1000) * 1000,
|
|
263
|
+
status: 'confirmed',
|
|
264
|
+
fee: tx.meta.fee
|
|
265
|
+
? (tx.meta.fee / LAMPORTS_PER_SOL).toFixed(9)
|
|
266
|
+
: '0',
|
|
267
|
+
});
|
|
268
|
+
} catch (error) {
|
|
269
|
+
console.warn(`Failed to fetch transaction ${sig.signature}:`, error);
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return transactions;
|
|
275
|
+
} catch (error) {
|
|
276
|
+
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
277
|
+
console.warn(`Failed to get transaction history: ${message}`);
|
|
278
|
+
return [];
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Validate Solana address (Base58 format)
|
|
284
|
+
*/
|
|
285
|
+
validateAddress(address: string): boolean {
|
|
286
|
+
try {
|
|
287
|
+
new PublicKey(address);
|
|
288
|
+
return true;
|
|
289
|
+
} catch {
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Estimate transaction fee
|
|
296
|
+
*/
|
|
297
|
+
async estimateFee(request: TransactionRequest): Promise<string> {
|
|
298
|
+
if (!this.connected || !this.connection || !this.keypair) {
|
|
299
|
+
return '0.000005'; // Default minimum
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
try {
|
|
303
|
+
const toPublicKey = new PublicKey(request.to);
|
|
304
|
+
const fromPublicKey = this.keypair.publicKey;
|
|
305
|
+
const amountInLamports = this.parseSolAmount(request.amount);
|
|
306
|
+
|
|
307
|
+
// Get latest blockhash
|
|
308
|
+
const { blockhash } = await this.connection.getLatestBlockhash();
|
|
309
|
+
|
|
310
|
+
// Create transaction
|
|
311
|
+
const instruction = SystemProgram.transfer({
|
|
312
|
+
fromPubkey: fromPublicKey,
|
|
313
|
+
toPubkey: toPublicKey,
|
|
314
|
+
lamports: amountInLamports,
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
const transaction = new SolanaTransaction().add(instruction);
|
|
318
|
+
transaction.recentBlockhash = blockhash;
|
|
319
|
+
transaction.feePayer = fromPublicKey;
|
|
320
|
+
|
|
321
|
+
// Estimate fee
|
|
322
|
+
const fee = await transaction.getEstimatedFee(this.connection);
|
|
323
|
+
if (!fee) return '0.000005';
|
|
324
|
+
const feeInSol = fee / LAMPORTS_PER_SOL;
|
|
325
|
+
|
|
326
|
+
return feeInSol.toFixed(9);
|
|
327
|
+
} catch (_error) {
|
|
328
|
+
return '0.000005';
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Get current block number (slot)
|
|
334
|
+
*/
|
|
335
|
+
async getBlockNumber(): Promise<number> {
|
|
336
|
+
if (!this.connected || !this.connection) {
|
|
337
|
+
throw new Error('Provider not connected');
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
try {
|
|
341
|
+
return await this.connection.getSlot();
|
|
342
|
+
} catch (error) {
|
|
343
|
+
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
344
|
+
throw new Error(`Failed to get block number: ${message}`);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Get transaction by hash
|
|
350
|
+
*/
|
|
351
|
+
async getTransaction(txHash: string): Promise<Transaction | null> {
|
|
352
|
+
if (!this.connected || !this.connection) {
|
|
353
|
+
throw new Error('Provider not connected');
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
try {
|
|
357
|
+
const tx = await this.connection.getParsedTransaction(
|
|
358
|
+
txHash,
|
|
359
|
+
{ maxSupportedTransactionVersion: 0 }
|
|
360
|
+
);
|
|
361
|
+
|
|
362
|
+
if (!tx || !tx.meta) return null;
|
|
363
|
+
|
|
364
|
+
// Find transfer instruction
|
|
365
|
+
const transfer = tx.transaction.message.instructions.find(
|
|
366
|
+
(ix: any) => ix.programId.toBase58() === SystemProgram.programId.toBase58()
|
|
367
|
+
);
|
|
368
|
+
|
|
369
|
+
if (!transfer) return null;
|
|
370
|
+
|
|
371
|
+
const parsedIx = (transfer as any).parsed;
|
|
372
|
+
if (parsedIx?.type !== 'transfer') return null;
|
|
373
|
+
|
|
374
|
+
const { from, to, lamports } = parsedIx.info;
|
|
375
|
+
|
|
376
|
+
return {
|
|
377
|
+
hash: txHash,
|
|
378
|
+
from,
|
|
379
|
+
to,
|
|
380
|
+
amount: (lamports / LAMPORTS_PER_SOL).toFixed(9),
|
|
381
|
+
chain: 'solana',
|
|
382
|
+
timestamp: (tx.blockTime || Date.now() / 1000) * 1000,
|
|
383
|
+
status: 'confirmed',
|
|
384
|
+
fee: tx.meta.fee
|
|
385
|
+
? (tx.meta.fee / LAMPORTS_PER_SOL).toFixed(9)
|
|
386
|
+
: '0',
|
|
387
|
+
};
|
|
388
|
+
} catch (error) {
|
|
389
|
+
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
390
|
+
throw new Error(`Failed to get transaction: ${message}`);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Initialize keypair from wallet data
|
|
396
|
+
*/
|
|
397
|
+
async initKeypair(mnemonic: string, derivationPath?: string): Promise<void> {
|
|
398
|
+
try {
|
|
399
|
+
const { generateSeedFromMnemonic, deriveSolanaKey } = await import('../key-derivation.js');
|
|
400
|
+
const seed = generateSeedFromMnemonic(mnemonic);
|
|
401
|
+
const path = derivationPath || "m/44'/501'/0'/0'/0'";
|
|
402
|
+
const derived = deriveSolanaKey(seed, path);
|
|
403
|
+
|
|
404
|
+
const privateKeyBytes = Uint8Array.from(
|
|
405
|
+
Buffer.from(derived.privateKey, 'hex')
|
|
406
|
+
);
|
|
407
|
+
this.keypair = Keypair.fromSecretKey(privateKeyBytes);
|
|
408
|
+
|
|
409
|
+
console.log('Solana keypair initialized for derivation:', path);
|
|
410
|
+
console.log('Address:', this.getAddress());
|
|
411
|
+
} catch (error) {
|
|
412
|
+
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
413
|
+
throw new Error(`Failed to initialize Solana keypair: ${message}`);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Initialize from private key
|
|
419
|
+
*/
|
|
420
|
+
async initFromPrivateKey(privateKey: string): Promise<void> {
|
|
421
|
+
try {
|
|
422
|
+
this.keypair = this.createKeypairFromPrivateKey(privateKey);
|
|
423
|
+
|
|
424
|
+
console.log('Solana keypair initialized from private key');
|
|
425
|
+
console.log('Address:', this.getAddress());
|
|
426
|
+
} catch (error) {
|
|
427
|
+
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
428
|
+
throw new Error(`Failed to initialize Solana keypair from private key: ${message}`);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Create Keypair from private key
|
|
434
|
+
*/
|
|
435
|
+
private createKeypairFromPrivateKey(privateKeyHex: string): Keypair {
|
|
436
|
+
const privateKeyBytes = Buffer.from(privateKeyHex, 'hex');
|
|
437
|
+
return Keypair.fromSecretKey(privateKeyBytes.subarray(0, 32));
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Get address from keypair
|
|
442
|
+
*/
|
|
443
|
+
getAddress(): string | null {
|
|
444
|
+
if (!this.keypair) {
|
|
445
|
+
return null;
|
|
446
|
+
}
|
|
447
|
+
return this.keypair.publicKey.toBase58();
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* Get public key
|
|
452
|
+
*/
|
|
453
|
+
getPublicKey(): string | null {
|
|
454
|
+
if (!this.keypair) {
|
|
455
|
+
return null;
|
|
456
|
+
}
|
|
457
|
+
return Buffer.from(this.keypair.publicKey.toBytes()).toString('hex');
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Parse SOL amount to lamports
|
|
462
|
+
*/
|
|
463
|
+
private parseSolAmount(amountStr: string): number {
|
|
464
|
+
try {
|
|
465
|
+
const cleanAmount = amountStr.replace(/,/g, '').trim();
|
|
466
|
+
const amount = parseFloat(cleanAmount);
|
|
467
|
+
return Math.floor(amount * LAMPORTS_PER_SOL);
|
|
468
|
+
} catch {
|
|
469
|
+
return 0;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Get default RPC URL for network type
|
|
475
|
+
* Checks environment variables first, then falls back to public RPC endpoints
|
|
476
|
+
*/
|
|
477
|
+
private getDefaultRpcUrl(): string {
|
|
478
|
+
const envUrls: Record<string, string | undefined> = {
|
|
479
|
+
mainnet: process.env.SOLANA_MAINNET_RPC_URL || process.env.SOLANA_RPC_URL,
|
|
480
|
+
devnet: process.env.SOLANA_DEVNET_RPC_URL || process.env.SOLANA_RPC_URL,
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
const publicUrls: Record<string, string> = {
|
|
484
|
+
mainnet: 'https://api.mainnet-beta.solana.com',
|
|
485
|
+
devnet: 'https://api.devnet.solana.com',
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
return envUrls[this.network] || publicUrls[this.network] || publicUrls.mainnet!;
|
|
489
|
+
}
|
|
490
|
+
}
|