# Hyperledger Firefly Integration

## Overview

Hyperledger Firefly is integrated into the Besu network for tokenization and asset management. Firefly provides a high-level API for managing tokens, NFTs, and data on the blockchain.

## Architecture

### Components

1. **Firefly Core**: Main Firefly service
2. **PostgreSQL**: Database for Firefly
3. **IPFS**: Distributed storage for Firefly data
4. **Besu Connector**: Connects Firefly to Besu network

### Network Integration

- **Chain ID**: 138 (DeFi Oracle Meta Mainnet)
- **RPC Endpoint**: Besu RPC nodes
- **WebSocket Endpoint**: Besu WebSocket nodes

## Deployment

### Prerequisites

- Kubernetes cluster (AKS)
- Besu network deployed
- RPC endpoints accessible

### Deploy Firefly

```bash
# Deploy Firefly
./scripts/deployment/deploy-firefly.sh

# Or manually
kubectl apply -f k8s/firefly/
```

### Verify Deployment

```bash
# Check Firefly status
kubectl get pods -n firefly

# Check Firefly API
curl http://firefly-api.firefly.svc.cluster.local:5000/api/v1/status
```

## Configuration

### Firefly Configuration

Firefly is configured via ConfigMap (`k8s/firefly/configmap.yaml`):

- **Database**: PostgreSQL
- **Blockchain**: Ethereum (Besu)
- **RPC**: Besu RPC endpoints
- **Chain ID**: 138
- **IPFS**: IPFS service for data storage

### Environment Variables

- `FF_DATABASE_TYPE`: postgres
- `FF_DATABASE_URL`: PostgreSQL connection string
- `FF_BLOCKCHAIN_TYPE`: ethereum
- `FF_BLOCKCHAIN_RPC`: Besu RPC URL
- `FF_BLOCKCHAIN_WS`: Besu WebSocket URL
- `FF_CHAIN_ID`: 138

## Usage

### Create Token Pool

```bash
curl -X POST http://firefly-api.firefly.svc.cluster.local:5000/api/v1/tokens/pools \
  -H "Content-Type: application/json" \
  -d '{
    "name": "MyToken",
    "symbol": "MTK",
    "type": "fungible"
  }'
```

### Mint Tokens

```bash
curl -X POST http://firefly-api.firefly.svc.cluster.local:5000/api/v1/tokens/mint \
  -H "Content-Type: application/json" \
  -d '{
    "pool": "pool-id",
    "amount": "1000",
    "to": "0x..."
  }'
```

### Create NFT

```bash
curl -X POST http://firefly-api.firefly.svc.cluster.local:5000/api/v1/tokens/nfts \
  -H "Content-Type: application/json" \
  -d '{
    "pool": "pool-id",
    "tokenId": "1",
    "uri": "ipfs://...",
    "to": "0x..."
  }'
```

## Integration with Besu

Firefly connects to Besu via:

1. **RPC Endpoint**: HTTP JSON-RPC
2. **WebSocket Endpoint**: WebSocket JSON-RPC
3. **Chain ID**: 138

### Besu-Firefly Connector

The Besu-Firefly connector (`connectors/besu-firefly/connector.py`) provides:

- Network registration
- Contract deployment
- Status monitoring

## Tokenization

Firefly enables tokenization of:

- **Financial Files**: ISO-20022, SWIFT FIN
- **Assets**: Real-world assets
- **Data**: Any digital data

See [Financial Tokenization Service](../../../services/financial-tokenization/) for details.

## References

- [Firefly Documentation](https://hyperledger.github.io/firefly/)
- [Firefly API](https://hyperledger.github.io/firefly/api/)
- [Firefly Tokens](https://hyperledger.github.io/firefly/tokens/)
