CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-polkadot--util-crypto

A collection of useful crypto utilities for Polkadot ecosystem projects

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

json-encryption.mddocs/

JSON Encryption

Secure JSON encryption and decryption with password protection, supporting multiple encoding formats and versions for secure data storage and transmission.

Capabilities

JSON Encryption

Encrypt data to password-protected JSON format.

/**
 * Encrypt data to JSON format with password protection
 * @param data - Data bytes to encrypt
 * @param contentType - Array describing content type
 * @param passphrase - Password for encryption
 * @returns Encrypted JSON object
 */
function jsonEncrypt(data: Uint8Array, contentType: string[], passphrase: string): EncryptedJson;

/**
 * Format encrypted data as JSON
 * @param encoded - Encoded encrypted data
 * @param contentType - Optional content type array
 * @returns Formatted encrypted JSON
 */
function jsonEncryptFormat(encoded: string, contentType?: string[]): EncryptedJson;

JSON Decryption

Decrypt password-protected JSON data.

/**
 * Decrypt encrypted JSON with passphrase
 * @param json - Encrypted JSON object
 * @param passphrase - Password for decryption
 * @returns Decrypted data bytes
 */
function jsonDecrypt(json: EncryptedJson, passphrase: string): Uint8Array;

/**
 * Decrypt encrypted data directly
 * @param encrypted - Encrypted data object
 * @param passphrase - Password for decryption
 * @returns Decrypted data bytes
 */
function jsonDecryptData(encrypted: EncryptedJson, passphrase: string): Uint8Array;

Types

type EncryptedJsonVersion = '0' | '1' | '2' | '3';
type EncryptedJsonEncoding = 'none' | 'scrypt' | 'xsalsa20-poly1305';

interface EncryptedJsonDescriptor {
  /** Descriptor for the content */
  content: string[];
  /** The encoding (in current/latest versions this is always an array) */
  type: EncryptedJsonEncoding | EncryptedJsonEncoding[];
  /** The version of encoding applied */
  version: EncryptedJsonVersion;
}

interface EncryptedJson {
  /** The encoded string */
  encoded: string;
  /** The encoding used */
  encoding: EncryptedJsonDescriptor;
}

Usage Example

import { jsonEncrypt, jsonDecrypt } from "@polkadot/util-crypto";

// Data to encrypt
const secretData = new TextEncoder().encode("sensitive information");
const contentType = ["pkcs8", "sr25519"];
const passphrase = "strong-password";

// Encrypt data
const encrypted = jsonEncrypt(secretData, contentType, passphrase);
console.log(encrypted.encoding.version); // Current version
console.log(encrypted.encoding.type); // Encoding methods used

// Decrypt data
const decrypted = jsonDecrypt(encrypted, passphrase);
const decryptedText = new TextDecoder().decode(decrypted);
console.log(decryptedText); // "sensitive information"

Security Features

  • Password-based encryption using Scrypt key derivation
  • XSalsa20-Poly1305 authenticated encryption
  • Version support for format evolution
  • Content type metadata for proper handling
  • Salt and nonce generation for unique encryptions

Install with Tessl CLI

npx tessl i tessl/npm-polkadot--util-crypto

docs

address.md

base-encoding.md

crypto-init.md

ethereum.md

hashing.md

index.md

json-encryption.md

key-derivation-pbkdf.md

key-derivation.md

keypairs.md

mnemonic.md

random.md

signatures.md

tile.json