anya-core 1.2.0

Enterprise-grade Bitcoin Infrastructure Platform
Documentation
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import React, { useState, useEffect, useCallback } from 'react';
import { Web5 } from '@web5/api';
import { LightningService, useLightningService, LightningConfig } from './lightning-service';
import { RGBService, useRGBService, RGBConfig } from './rgb-service';

// Comprehensive Layer 2 management for React
export interface Layer2Config {
  lightning: LightningConfig;
  rgb: RGBConfig;
  bob: {
    rpcUrl: string;
    chainId: number;
  };
  liquid: {
    nodeEndpoint: string;
    network: string;
  };
  rsk: {
    rpcUrl: string;
    network: string;
  };
  stacks: {
    rpcUrl: string;
    network: string;
  };
  taprootAssets: {
    bitcoinRpcUrl: string;
    tapdUrl: string;
    network: string;
  };
}

export type Layer2Protocol = 
  | 'lightning' 
  | 'rgb' 
  | 'bob' 
  | 'liquid' 
  | 'rsk' 
  | 'stacks' 
  | 'taproot-assets';

export interface ProtocolStatus {
  protocol: Layer2Protocol;
  connected: boolean;
  initialized: boolean;
  blockHeight?: number;
  peers?: number;
  error?: string;
}

export interface CrossLayerTransfer {
  fromProtocol: Layer2Protocol;
  toProtocol: Layer2Protocol;
  assetId: string;
  amount: number;
  fromAddress: string;
  toAddress: string;
}

export function useLayer2Manager(config: Layer2Config) {
  const [protocolStatuses, setProtocolStatuses] = useState<Record<Layer2Protocol, ProtocolStatus>>({
    lightning: { protocol: 'lightning', connected: false, initialized: false },
    rgb: { protocol: 'rgb', connected: false, initialized: false },
    bob: { protocol: 'bob', connected: false, initialized: false },
    liquid: { protocol: 'liquid', connected: false, initialized: false },
    rsk: { protocol: 'rsk', connected: false, initialized: false },
    stacks: { protocol: 'stacks', connected: false, initialized: false },
    'taproot-assets': { protocol: 'taproot-assets', connected: false, initialized: false },
  });

  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);

  // Initialize individual protocol hooks
  const lightning = useLightningService(config.lightning);
  const rgb = useRGBService(config.rgb);

  const initializeAllProtocols = useCallback(async () => {
    setLoading(true);
    setError(null);

    try {
      // Initialize Lightning Network
      await lightning.fetchNodeInfo();
      setProtocolStatuses(prev => ({
        ...prev,
        lightning: {
          ...prev.lightning,
          connected: lightning.nodeInfo !== null,
          initialized: true,
          blockHeight: lightning.nodeInfo?.blockHeight,
          peers: lightning.nodeInfo?.numPeers,
        }
      }));

      // Initialize RGB
      await rgb.fetchAssets();
      setProtocolStatuses(prev => ({
        ...prev,
        rgb: {
          ...prev.rgb,
          connected: rgb.assets.length >= 0, // Connection successful if no error
          initialized: true,
        }
      }));

      // Initialize other protocols (mock implementations for now)
      await initializeBOB();
      await initializeLiquid();
      await initializeRSK();
      await initializeStacks();
      await initializeTaprootAssets();

    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to initialize protocols');
    } finally {
      setLoading(false);
    }
  }, [lightning, rgb]);

  const initializeBOB = async () => {
    try {
      // Mock BOB initialization
      const response = await fetch(`${config.bob.rpcUrl}/health`);
      setProtocolStatuses(prev => ({
        ...prev,
        bob: {
          ...prev.bob,
          connected: response.ok,
          initialized: true,
        }
      }));
    } catch (err) {
      setProtocolStatuses(prev => ({
        ...prev,
        bob: {
          ...prev.bob,
          connected: false,
          initialized: true,
          error: 'Failed to connect to BOB',
        }
      }));
    }
  };

  const initializeLiquid = async () => {
    try {
      // Mock Liquid initialization
      setProtocolStatuses(prev => ({
        ...prev,
        liquid: {
          ...prev.liquid,
          connected: true,
          initialized: true,
        }
      }));
    } catch (err) {
      setProtocolStatuses(prev => ({
        ...prev,
        liquid: {
          ...prev.liquid,
          connected: false,
          initialized: true,
          error: 'Failed to connect to Liquid',
        }
      }));
    }
  };

  const initializeRSK = async () => {
    try {
      // Mock RSK initialization
      setProtocolStatuses(prev => ({
        ...prev,
        rsk: {
          ...prev.rsk,
          connected: true,
          initialized: true,
        }
      }));
    } catch (err) {
      setProtocolStatuses(prev => ({
        ...prev,
        rsk: {
          ...prev.rsk,
          connected: false,
          initialized: true,
          error: 'Failed to connect to RSK',
        }
      }));
    }
  };

  const initializeStacks = async () => {
    try {
      // Mock Stacks initialization
      setProtocolStatuses(prev => ({
        ...prev,
        stacks: {
          ...prev.stacks,
          connected: true,
          initialized: true,
        }
      }));
    } catch (err) {
      setProtocolStatuses(prev => ({
        ...prev,
        stacks: {
          ...prev.stacks,
          connected: false,
          initialized: true,
          error: 'Failed to connect to Stacks',
        }
      }));
    }
  };

  const initializeTaprootAssets = async () => {
    try {
      // Mock Taproot Assets initialization
      setProtocolStatuses(prev => ({
        ...prev,
        'taproot-assets': {
          ...prev['taproot-assets'],
          connected: true,
          initialized: true,
        }
      }));
    } catch (err) {
      setProtocolStatuses(prev => ({
        ...prev,
        'taproot-assets': {
          ...prev['taproot-assets'],
          connected: false,
          initialized: true,
          error: 'Failed to connect to Taproot Assets',
        }
      }));
    }
  };

  const executeCrossLayerTransfer = useCallback(async (transfer: CrossLayerTransfer) => {
    setLoading(true);
    setError(null);

    try {
      // Implementation would handle actual cross-layer bridging
      console.log('Executing cross-layer transfer:', transfer);
      
      // Mock implementation
      const transferId = `${transfer.fromProtocol}-${transfer.toProtocol}-${Date.now()}`;
      
      // This would typically involve:
      // 1. Lock assets on source protocol
      // 2. Verify lock transaction
      // 3. Mint/unlock assets on destination protocol
      // 4. Provide proof of completion
      
      return {
        transferId,
        status: 'completed',
        timestamp: Date.now(),
      };
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Cross-layer transfer failed');
      throw err;
    } finally {
      setLoading(false);
    }
  }, []);

  const getProtocolHealth = useCallback((protocol: Layer2Protocol): 'healthy' | 'degraded' | 'down' => {
    const status = protocolStatuses[protocol];
    if (!status.initialized) return 'down';
    if (!status.connected) return 'down';
    if (status.error) return 'degraded';
    return 'healthy';
  }, [protocolStatuses]);

  const getAllConnectedProtocols = useCallback((): Layer2Protocol[] => {
    return Object.entries(protocolStatuses)
      .filter(([_, status]) => status.connected && status.initialized)
      .map(([protocol, _]) => protocol as Layer2Protocol);
  }, [protocolStatuses]);

  useEffect(() => {
    initializeAllProtocols();
  }, [initializeAllProtocols]);

  return {
    protocolStatuses,
    loading,
    error,
    lightning,
    rgb,
    initializeAllProtocols,
    executeCrossLayerTransfer,
    getProtocolHealth,
    getAllConnectedProtocols,
  };
}

// React component for Layer 2 dashboard
export interface Layer2DashboardProps {
  config: Layer2Config;
}

export const Layer2Dashboard: React.FC<Layer2DashboardProps> = ({ config }) => {
  const {
    protocolStatuses,
    loading,
    error,
    lightning,
    rgb,
    initializeAllProtocols,
    executeCrossLayerTransfer,
    getProtocolHealth,
    getAllConnectedProtocols,
  } = useLayer2Manager(config);

  const getHealthColor = (health: string) => {
    switch (health) {
      case 'healthy': return 'text-green-600';
      case 'degraded': return 'text-yellow-600';
      case 'down': return 'text-red-600';
      default: return 'text-gray-600';
    }
  };

  return (
    <div className="p-6 max-w-6xl mx-auto">
      <div className="mb-6">
        <h1 className="text-3xl font-bold text-gray-900 mb-2">Layer 2 Dashboard</h1>
        <p className="text-gray-600">Monitor and manage all Bitcoin Layer 2 protocols</p>
      </div>

      {error && (
        <div className="mb-4 p-4 bg-red-100 border border-red-400 text-red-700 rounded">
          {error}
        </div>
      )}

      <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 mb-6">
        {Object.entries(protocolStatuses).map(([protocol, status]) => {
          const health = getProtocolHealth(protocol as Layer2Protocol);
          return (
            <div key={protocol} className="bg-white rounded-lg shadow p-4 border">
              <div className="flex items-center justify-between mb-2">
                <h3 className="text-lg font-semibold capitalize">{protocol.replace('-', ' ')}</h3>
                <span className={`text-sm font-medium ${getHealthColor(health)}`}>
                  {health.toUpperCase()}
                </span>
              </div>
              <div className="text-sm text-gray-600">
                <p>Connected: {status.connected ? 'Yes' : 'No'}</p>
                <p>Initialized: {status.initialized ? 'Yes' : 'No'}</p>
                {status.blockHeight && <p>Block Height: {status.blockHeight}</p>}
                {status.peers && <p>Peers: {status.peers}</p>}
                {status.error && <p className="text-red-600">Error: {status.error}</p>}
              </div>
            </div>
          );
        })}
      </div>

      <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
        {/* Lightning Network Section */}
        <div className="bg-white rounded-lg shadow p-6">
          <h2 className="text-xl font-semibold mb-4">Lightning Network</h2>
          {lightning.nodeInfo && (
            <div className="space-y-2 text-sm">
              <p><span className="font-medium">Alias:</span> {lightning.nodeInfo.alias}</p>
              <p><span className="font-medium">Channels:</span> {lightning.nodeInfo.numActiveChannels}</p>
              <p><span className="font-medium">Peers:</span> {lightning.nodeInfo.numPeers}</p>
              <p><span className="font-medium">Synced:</span> {lightning.nodeInfo.synced ? 'Yes' : 'No'}</p>
            </div>
          )}
        </div>

        {/* RGB Protocol Section */}
        <div className="bg-white rounded-lg shadow p-6">
          <h2 className="text-xl font-semibold mb-4">RGB Protocol</h2>
          <div className="space-y-2 text-sm">
            <p><span className="font-medium">Assets:</span> {rgb.assets.length}</p>
            <p><span className="font-medium">Transactions:</span> {rgb.transactions.length}</p>
          </div>
        </div>
      </div>

      <div className="mt-6 flex gap-4">
        <button
          onClick={initializeAllProtocols}
          disabled={loading}
          className="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 disabled:opacity-50"
        >
          {loading ? 'Initializing...' : 'Reinitialize All Protocols'}
        </button>
        
        <div className="text-sm text-gray-600 flex items-center">
          Connected Protocols: {getAllConnectedProtocols().length}/7
        </div>
      </div>
    </div>
  );
};