kalp-pkg
Version:
KALP blockchain SDK for cryptographic operations and blockchain interactions
222 lines (167 loc) • 5.19 kB
Markdown
A comprehensive TypeScript SDK for interacting with the KALP blockchain network. This package provides cryptographic operations, key management, user registration, and transaction capabilities.
- 🔐 **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
```bash
npm install kalp-sdk
```
```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
);
```
Generates a random mnemonic seed phrase.
```typescript
const seedPhrase = await getSeedPhrase();
```
Generates a new ECDSA key pair.
```typescript
const { pemPrivateKey, pemPublicKey } = await getKeyPair();
```
Derives a key pair from a seed phrase.
```typescript
const keyPair = await getKeyPairFromSeedPhrase(seedPhrase);
```
Generates an enrollment ID from a public key.
```typescript
const enrollmentId = await getEnrollmentId(publicKey);
```
Creates a Certificate Signing Request (CSR).
```typescript
const csr = createCsr(enrollmentId, privateKey, publicKey);
```
Registers and enrolls a user in one step.
```typescript
const certificate = await registerAndEnrollUser(
network,
nglURL,
channelName,
enrollmentID,
csr
);
```
Submits a transaction to the blockchain.
```typescript
const transactionId = await submitTransaction(
network,
gatewayURL,
enrollmentID,
privateKey,
certificate,
channelName,
chaincodeName,
transactionName,
transactionParams
);
```
Evaluates a transaction without committing to the blockchain.
```typescript
const result = await evaluateTransaction(
network,
gatewayURL,
enrollmentID,
privateKey,
certificate,
channelName,
chaincodeName,
transactionName,
transactionParams
);
```
Encrypts text using AES encryption.
```typescript
const encrypted = encryptText('Hello World');
```
Decrypts AES-encrypted text.
```typescript
const decrypted = decryptText(encrypted);
```
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'
```
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);
}
```
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
- `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
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.