A powerful TypeScript SDK for building privacy-preserving applications on the COTI blockchain. Encrypt sensitive transaction inputs before submitting them to smart contracts, and decrypt encrypted data retrieved from the blockchain.
The COTI TypeScript SDK enables privacy-preserving transactions on the COTI blockchain by providing cryptographic utilities to:
- Encrypt transaction inputs before sending them to smart contracts
- Decrypt encrypted data retrieved from the blockchain
- Generate cryptographic signatures for contract verification
- Support multiple data types: unsigned integers (128-bit and 256-bit) and strings
This SDK is essential for applications that need to protect sensitive data while leveraging COTI's privacy features, including garbled circuits and on-chain compute capabilities.
Protect sensitive transaction data (amounts, user IDs, personal information) from being publicly visible on the blockchain while still maintaining verifiability through cryptographic signatures.
Seamlessly prepare encrypted input text (itUint, itUint256, itString) that matches the expected format of COTI smart contracts, making integration straightforward.
Full TypeScript support with comprehensive type definitions for all encrypted data structures, ensuring compile-time safety and better developer experience.
npm install @coti-io/coti-sdk-typescript- Node.js >= 18.20.5
- TypeScript (for TypeScript projects)
import { Wallet } from 'ethers'
import { prepareIT, decryptUint } from '@coti-io/coti-sdk-typescript'
// Create a wallet and user key
const wallet = new Wallet('0x...') // Your private key
const userKey = '12345678901234567890123456789012' // 32-character hex string (AES key)
// Prepare encrypted input text for a 128-bit unsigned integer
const plaintext = 12345n
const contractAddress = '0x...' // Your smart contract address
const functionSelector = '0xa9059cbb' // Function selector (first 4 bytes of function signature hash)
const { ciphertext, signature } = prepareIT(
plaintext,
{ wallet, userKey },
contractAddress,
functionSelector
)
// Submit ciphertext and signature to your smart contract
// ...
// Later, decrypt the data
const decrypted = decryptUint(ciphertext, userKey)
console.log(decrypted) // 12345nimport { prepareIT256, decryptUint256 } from '@coti-io/coti-sdk-typescript'
// For values larger than 128 bits, use prepareIT256
const largeValue = 2n ** 200n
const { ciphertext, signature } = prepareIT256(
largeValue,
{ wallet, userKey },
contractAddress,
functionSelector
)
// Decrypt
const decrypted = decryptUint256(ciphertext, userKey)import { buildStringInputText, decryptString } from '@coti-io/coti-sdk-typescript'
const message = 'Hello, COTI!'
const { ciphertext, signature } = buildStringInputText(
message,
{ wallet, userKey },
contractAddress,
functionSelector
)
// Decrypt
const decrypted = decryptString(ciphertext, userKey)
console.log(decrypted) // 'Hello, COTI!'| Type | Function | Max Size | Use Case |
|---|---|---|---|
uint128 |
prepareIT / decryptUint |
128 bits | Standard integers, amounts, IDs |
uint256 |
prepareIT256 / decryptUint256 |
256 bits | Large numbers, hashes, timestamps |
string |
buildStringInputText / decryptString |
Unlimited | Messages, metadata, JSON data |
prepareIT(plaintext, sender, contractAddress, functionSelector)- Encrypts a 128-bit unsigned integerprepareIT256(plaintext, sender, contractAddress, functionSelector)- Encrypts a 256-bit unsigned integerbuildStringInputText(plaintext, sender, contractAddress, functionSelector)- Encrypts a string
decryptUint(ciphertext, userKey)- Decrypts a 128-bit unsigned integerdecryptUint256(ciphertext, userKey)- Decrypts a 256-bit unsigned integerdecryptString(ciphertext, userKey)- Decrypts a string
encrypt(key, plaintext)- Low-level AES encryptiondecrypt(key, r, ciphertext, r2?, ciphertext2?)- Low-level AES decryptionsign(message, privateKey)- Sign arbitrary messagesgenerateRSAKeyPair()- Generate RSA key pairsrecoverUserKey(privateKey, encryptedKeyShare0, encryptedKeyShare1)- Recover AES key from encrypted shares
- User Key Management: The
userKey(AES key) must be kept secure. It's used for both encryption and decryption. - Private Key Security: Never expose your wallet's private key. Use environment variables or secure key management systems.
- Function Selectors: Use the correct function selector for your smart contract function to ensure signature verification works correctly.
- Key Generation: Generate strong, random keys for production use. Never use hardcoded keys in production.
Full API documentation with detailed examples is available in the COTI Documentation.
npm install
npm run buildThis compiles TypeScript to JavaScript in the dist/ directory.
The SDK includes comprehensive test coverage with 212+ tests:
# Run all tests
npm test
# Run specific test suites
npm test -- tests/unit
npm test -- tests/integrationTests require a .env file with the following variables:
TEST_PRIVATE_KEY=0x... # Private key for wallet operations
TEST_USER_KEY=... # 32-character hex string (AES key)See example.env for reference.
coti-sdk-typescript/
βββ src/ # Source TypeScript files
β βββ crypto_utils.ts # Core cryptographic functions
β βββ types.ts # TypeScript type definitions
β βββ index.ts # Main entry point
βββ dist/ # Compiled JavaScript (generated)
βββ tests/ # Test files
β βββ unit/ # Unit tests
β βββ integration/ # Integration tests
βββ package.json
We welcome contributions! Please see our Contributing Guide for details on:
- Code of Conduct
- Development workflow
- Coding standards
- How to submit pull requests
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.
- Documentation: COTI Docs
- Website: COTI.io
- Telegram: @coti_io
- Discord: COTI Community
- Twitter: @COTInetwork
Found a bug or have a feature request? Please open an issue on GitHub.