# KALP SDK

A comprehensive TypeScript SDK for interacting with the KALP blockchain network. This package provides cryptographic operations, key management, user registration, and transaction capabilities.

## Features

- 🔐 **Cryptographic Operations**: Generate key pairs, create CSRs, and handle digital signatures
- 🌱 **Seed Phrase Support**: Generate and derive keys from mnemonic phrases
- 🔗 **Multi-Network Support**: Support for Stagenet, Testnet, and Mainnet environments
- 📝 **Transaction Handling**: Submit and evaluate blockchain transactions
- 👤 **User Management**: Register and enroll users on the KALP network
- 🔒 **Encryption Utilities**: Built-in text encryption and decryption
- 🌐 **Cross-Platform**: Works in both browser and Node.js environments

## Installation

```bash
npm install kalp-sdk
```

## Quick Start

```typescript
import { 
  getSeedPhrase, 
  getKeyPair, 
  getEnrollmentId, 
  createCsr,
  registerAndEnrollUser,
  Network 
} from 'kalp-sdk';

// Generate a new seed phrase
const seedPhrase = await getSeedPhrase();
console.log('Seed Phrase:', seedPhrase);

// Generate key pair
const keyPair = await getKeyPair();
console.log('Public Key:', keyPair.pemPublicKey);
console.log('Private Key:', keyPair.pemPrivateKey);

// Get enrollment ID
const enrollmentId = await getEnrollmentId(keyPair.pemPublicKey);
console.log('Enrollment ID:', enrollmentId);

// Create CSR
const csr = createCsr(enrollmentId, keyPair.pemPrivateKey, keyPair.pemPublicKey);

// Register and enroll user
const certificate = await registerAndEnrollUser(
  Network.Testnet,
  null, // Use default network governance URL
  null, // Use default channel name
  enrollmentId,
  csr
);
```

## API Reference

### Key Generation

#### `getSeedPhrase()`
Generates a random mnemonic seed phrase.

```typescript
const seedPhrase = await getSeedPhrase();
```

#### `getKeyPair()`
Generates a new ECDSA key pair.

```typescript
const { pemPrivateKey, pemPublicKey } = await getKeyPair();
```

#### `getKeyPairFromSeedPhrase(seedPhrase: string)`
Derives a key pair from a seed phrase.

```typescript
const keyPair = await getKeyPairFromSeedPhrase(seedPhrase);
```

### User Management

#### `getEnrollmentId(publicKey: string)`
Generates an enrollment ID from a public key.

```typescript
const enrollmentId = await getEnrollmentId(publicKey);
```

#### `createCsr(enrollmentID: string, privateKeyPem: string, publicKeyPem: string)`
Creates a Certificate Signing Request (CSR).

```typescript
const csr = createCsr(enrollmentId, privateKey, publicKey);
```

#### `registerAndEnrollUser(...)`
Registers and enrolls a user in one step.

```typescript
const certificate = await registerAndEnrollUser(
  network,
  nglURL,
  channelName,
  enrollmentID,
  csr
);
```

### Transactions

#### `submitTransaction(...)`
Submits a transaction to the blockchain.

```typescript
const transactionId = await submitTransaction(
  network,
  gatewayURL,
  enrollmentID,
  privateKey,
  certificate,
  channelName,
  chaincodeName,
  transactionName,
  transactionParams
);
```

#### `evaluateTransaction(...)`
Evaluates a transaction without committing to the blockchain.

```typescript
const result = await evaluateTransaction(
  network,
  gatewayURL,
  enrollmentID,
  privateKey,
  certificate,
  channelName,
  chaincodeName,
  transactionName,
  transactionParams
);
```

### Encryption

#### `encryptText(text: string)`
Encrypts text using AES encryption.

```typescript
const encrypted = encryptText('Hello World');
```

#### `decryptText(ciphertext: string)`
Decrypts AES-encrypted text.

```typescript
const decrypted = decryptText(encrypted);
```

## Networks

The SDK supports multiple network environments:

```typescript
import { Network } from 'kalp-sdk';

// Available networks
Network.Stagenet          // 'STAGENET'
Network.Testnet           // 'TESTNET' 
Network.Mainnet           // 'MAINNET'
Network.IntegrationMainnet // 'INTEGRATIONMAINNET'
```

## Error Handling

All functions throw descriptive errors. Wrap calls in try-catch blocks:

```typescript
try {
  const keyPair = await getKeyPair();
  // Use keyPair...
} catch (error) {
  console.error('Key generation failed:', error.message);
}
```

## Environment Compatibility

This SDK works in both browser and Node.js environments:

- **Browser**: Uses native Web Crypto API
- **Node.js**: Uses Node.js crypto module with webcrypto polyfill
- **TypeScript**: Full type definitions included

## Dependencies

- `ethers`: Ethereum library for blockchain interactions
- `elliptic`: Elliptic curve cryptography
- `jsrsasign`: JavaScript RSA signing and encryption
- `crypto-js`: Cryptographic functions
- `cross-fetch`: Universal fetch for HTTP requests

## Contributing

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Support

For support and questions, please open an issue on the GitHub repository.