smartledger-sdk
Version:
A comprehensive blockchain and cryptographic operations SDK for JavaScript
132 lines (94 loc) • 3.52 kB
Markdown
# SmartLedger
A comprehensive blockchain and cryptographic operations SDK for JavaScript, providing a unified interface for key management, digital signatures, encryption, secret sharing, and hashing operations.
## Features
- **Key Management**
- Generate and manage BSV keys
- Multi-purpose key derivation (identity, financial, contractual, property, document, privacy)
- WIF import/export
- HD wallet support
- **Digital Signatures**
- Sign with WIF or mnemonic
- Signature verification
- Purpose-specific signing
- **Encryption**
- AES encryption/decryption
- JSON data support
- **Secret Sharing**
- Shamir's Secret Sharing implementation
- Configurable shares and threshold
- Hex encoding for storage
- **Hashing**
- SHA256, SHA512
- Double SHA256 (Bitcoin standard)
- HASH160 (RIPEMD160(SHA256))
- Hash verification
- **UUID Generation**
- UUIDv4 (random)
- UUIDv5 (namespace-based)
## Installation
```bash
npm install smartledger
```
## Usage
```javascript
import SmartLedger from 'smartledger';
// Create a new instance
const ledger = new SmartLedger();
// Generate a mnemonic
const mnemonic = SmartLedger.generateMnemonic();
// Generate keys for different purposes
const keys = ledger.generateAllKeys(mnemonic);
// Encryption
const encrypted = SmartLedger.encrypt({ message: "Hello, World!" }, "my-secret-key");
const decrypted = SmartLedger.decrypt(encrypted, "my-secret-key");
// Hashing
const message = "Hello, SmartLedger!";
const hash = SmartLedger.hash256(message);
const doubleHash = SmartLedger.doubleHash256(message);
const hash160 = SmartLedger.hash160(message);
// UUID Generation
const uuid = ledger.getUUID(); // Random UUIDv4
const uuidv5 = SmartLedger.generateUUIDv5("transaction/123"); // Namespace-based UUIDv5
```
## Secret Sharing Example
```javascript
// Split a secret into shares
const shares = ledger.splitSecret("my secret", 3, 2);
// Convert shares to hex for storage
const hexShares = shares.map(share => ledger.shareToHex(share));
// Reconstruct the secret using any 2 shares
const reconstructedShares = hexShares
.slice(0, 2)
.map(hexShare => ledger.hexToShare(hexShare));
const secret = ledger.combineShares(reconstructedShares);
```
## API Documentation
### Key Management
- `generateMnemonic()`: Generate a new mnemonic phrase
- `generateKeyPair()`: Generate a new key pair
- `fromWIF(wif)`: Create keys from WIF
- `fromMnemonic(mnemonic, purpose)`: Create keys from mnemonic for specific purpose
- `generateAllKeys(mnemonic)`: Generate all purpose-specific keys
### Digital Signatures
- `signWithWIF(data, wif)`: Sign data using WIF
- `signWithMnemonic(data, mnemonic, purpose)`: Sign data using mnemonic
- `verifySignature(data, signature, publicKey)`: Verify a signature
### Encryption
- `encrypt(data, key)`: Encrypt data using AES
- `decrypt(ciphertext, key)`: Decrypt AES encrypted data
### Secret Sharing
- `splitSecret(secret, shares, threshold)`: Split a secret into shares
- `combineShares(shares)`: Combine shares to reconstruct secret
- `shareToHex(share)`: Convert share to hex format
- `hexToShare(hexShare)`: Convert hex back to share
### Hashing
- `hash256(data)`: SHA256 hash
- `hash512(data)`: SHA512 hash
- `doubleHash256(data)`: Double SHA256 hash
- `hash160(data)`: RIPEMD160(SHA256) hash
- `verifyHash(data, hash, algorithm)`: Verify a hash
### UUID Generation
- `getUUID()`: Get a random UUIDv4
- `generateUUIDv5(name, namespace?)`: Generate a namespace-based UUIDv5
## License
MIT