@cheqd/sdk
Version:
A TypeScript SDK built with CosmJS to interact with the cheqd network ledger
223 lines • 10.9 kB
TypeScript
import { IKeyPair, IKeyValuePair, ISignInputs, VerificationMethods, TMethodSpecificId, MethodSpecificIdAlgo, TVerificationKey, TVerificationKeyPrefix, CheqdNetwork, IVerificationKeys, VerificationMethod, DIDDocument, SpecValidationResult, Service } from './types.js';
import { KeyPair } from '@stablelib/ed25519';
import { DirectSecp256k1HdWallet, DirectSecp256k1Wallet } from '@cosmjs/proto-signing';
import { EnglishMnemonic as _ } from '@cosmjs/crypto';
import { Service as ProtoService, DidDoc } from '@cheqd/ts-proto/cheqd/did/v2/index.js';
import { MsgCreateResourcePayload } from '@cheqd/ts-proto/cheqd/resource/v2/index.js';
import { Coin } from 'cosmjs-types/cosmos/base/v1beta1/coin';
import { BackoffOptions } from 'exponential-backoff';
/**
* Represents an importable Ed25519 key with hexadecimal encoding.
* This type ensures type safety for Ed25519 key operations.
*/
export type TImportableEd25519Key = {
/** Public key in hexadecimal format */
publicKeyHex: string;
/** Private key in hexadecimal format */
privateKeyHex: string;
/** Key identifier */
kid: string;
/** Key type, must be 'Ed25519' */
type: 'Ed25519';
};
/**
* Utility object for validating TImportableEd25519Key objects.
* Provides type guard functionality to ensure key structure integrity.
*/
export declare const TImportableEd25519Key: {
/**
* Type guard to validate if an object is a valid TImportableEd25519Key.
*
* @param key - Object to validate
* @returns True if the object is a valid TImportableEd25519Key
*/
isValid(key: any): key is TImportableEd25519Key;
};
/**
* Compares two arrays of key-value pairs for equality.
*
* @param kv1 - First array of key-value pairs
* @param kv2 - Second array of key-value pairs
* @returns True if both arrays contain identical key-value pairs in the same order
*/
export declare function isEqualKeyValuePair(kv1: IKeyValuePair[], kv2: IKeyValuePair[]): boolean;
/**
* Extended English mnemonic class with additional validation patterns.
* Provides regex pattern matching for mnemonic phrase validation.
*/
export declare class EnglishMnemonic extends _ {
/** Regular expression pattern for validating English mnemonic phrases */
static readonly _mnemonicMatcher: RegExp;
}
/**
* Creates signing inputs from an importable Ed25519 key by matching it with verification methods.
* Supports multiple verification method types and key formats.
*
* @param key - The Ed25519 key to create signing inputs from
* @param verificationMethod - Array of verification methods to match against
* @returns Signing inputs containing verification method ID and private key
* @throws Error if key validation fails or no matching verification method is found
*/
export declare function createSignInputsFromImportableEd25519Key(key: TImportableEd25519Key, verificationMethod: VerificationMethod[]): ISignInputs;
/**
* Creates a raw Ed25519 key pair using the StableLib library.
*
* @param seed - Optional seed string for deterministic key generation
* @returns Raw KeyPair object with publicKey and secretKey as Uint8Arrays
*/
export declare function createKeyPairRaw(seed?: string): KeyPair;
/**
* Creates an Ed25519 key pair with Base64-encoded keys.
*
* @param seed - Optional seed string for deterministic key generation
* @returns Key pair with Base64-encoded public and private keys
*/
export declare function createKeyPairBase64(seed?: string): IKeyPair;
/**
* Creates an Ed25519 key pair with hexadecimal-encoded keys.
*
* @param seed - Optional seed string for deterministic key generation
* @returns Key pair with hexadecimal-encoded public and private keys
*/
export declare function createKeyPairHex(seed?: string): IKeyPair;
/**
* Creates verification keys structure with DID URLs and key identifiers.
* Supports multiple algorithm types and network configurations.
*
* @param publicKey - Public key in base64 or hex format
* @param algo - Algorithm for method-specific ID generation
* @param keyFragment - Key fragment for the verification key identifier
* @param network - Cheqd network (defaults to Testnet)
* @param methodSpecificId - Optional pre-computed method-specific ID
* @param didUrl - Optional pre-computed DID URL
* @returns Verification keys structure with all identifiers
* @throws Error if public key format is invalid
*/
export declare function createVerificationKeys(publicKey: string, algo: MethodSpecificIdAlgo, keyFragment: TVerificationKey<TVerificationKeyPrefix, number>, network?: CheqdNetwork, methodSpecificId?: TMethodSpecificId, didUrl?: string): IVerificationKeys;
/**
* Creates DID verification methods from verification method types and keys.
* Supports Ed25519 keys in multiple formats (multibase, base58, JWK).
*
* @param verificationMethodTypes - Array of verification method types to create
* @param verificationKeys - Array of verification keys corresponding to each type
* @returns Array of formatted verification methods for DID documents
*/
export declare function createDidVerificationMethod(verificationMethodTypes: VerificationMethods[], verificationKeys: IVerificationKeys[]): VerificationMethod[];
/**
* Creates a complete DID document payload with verification methods and controllers.
*
* @param verificationMethods - Array of verification methods for the DID
* @param verificationKeys - Array of verification keys for authentication
* @param controller - Optional array of controller DIDs (defaults to self-controlled)
* @returns Complete DID document with all required fields
* @throws Error if verification methods or keys are missing
*/
export declare function createDidPayload(verificationMethods: VerificationMethod[], verificationKeys: IVerificationKeys[], controller?: string[]): DIDDocument;
/**
* Validates a DID document against the Cheqd specification and converts to protobuf format.
* Ensures all required fields are present and verification methods are supported.
*
* @param didDocument - DID document to validate
* @returns Validation result with protobuf conversion or error details
*/
export declare function validateSpecCompliantPayload(didDocument: DIDDocument): SpecValidationResult;
/**
* Creates a Cosmos wallet from a seed phrase or private key.
* Supports both HD wallets from mnemonics and direct key wallets.
*
* @param cosmosPayerSeed - Mnemonic phrase or private key hex string
* @returns Promise resolving to either HD wallet or direct key wallet
*/
export declare function createCosmosPayerWallet(cosmosPayerSeed: string): Promise<DirectSecp256k1HdWallet | DirectSecp256k1Wallet>;
/**
* Converts a raw Ed25519 public key to multibase format with proper multicodec header.
*
* @param key - Raw Ed25519 public key as Uint8Array
* @returns Multibase-encoded string with Ed25519 multicodec prefix
*/
export declare function toMultibaseRaw(key: Uint8Array): string;
/**
* Creates a MsgCreateDidDoc payload ready for signing.
* Validates the DID document and converts it to protobuf format.
*
* @param didPayload - DID document to create message payload from
* @param versionId - Version identifier for the DID document
* @returns Encoded message payload bytes ready for signing
*/
export declare function createMsgCreateDidDocPayloadToSign(didPayload: DIDDocument, versionId: string): Promise<Uint8Array<ArrayBuffer>>;
/** Alias for createMsgCreateDidDocPayloadToSign - used for DID document updates */
export declare const createMsgUpdateDidDocPayloadToSign: typeof createMsgCreateDidDocPayloadToSign;
/**
* Creates a MsgDeactivateDidDoc payload ready for signing.
*
* @param didPayload - DID document containing the ID to deactivate
* @param versionId - Optional version identifier for the DID document
* @returns Encoded message payload bytes ready for signing
*/
export declare function createMsgDeactivateDidDocPayloadToSign(didPayload: DIDDocument, versionId?: string): Uint8Array<ArrayBuffer>;
/**
* Creates a resource payload ready for signing.
*
* @param payload - Resource payload to encode
* @returns Encoded resource payload bytes ready for signing
*/
export declare function createMsgResourcePayloadToSign(payload: Partial<MsgCreateResourcePayload> | MsgCreateResourcePayload): Uint8Array<ArrayBuffer>;
/**
* Converts a public key to a Cosmos account address.
*
* @param publicKeyHex - Public key in hexadecimal format
* @returns Bech32-encoded Cosmos account address with 'cheqd' prefix
*/
export declare function getCosmosAccount(publicKeyHex: string): string;
/**
* Checks the balance of all coins for a given address on the blockchain.
*
* @param address - Bech32-encoded account address to check balance for
* @param rpcAddress - RPC endpoint URL of the blockchain node
* @returns Promise resolving to array of coin balances
*/
export declare function checkBalance(address: string, rpcAddress: string): Promise<readonly Coin[]>;
/**
* Checks if a given input is valid JSON.
*
* @param input - Input to validate as JSON
* @returns True if the input is a valid JSON string
*/
export declare function isJSON(input: any): boolean;
/** Default configuration options for exponential backoff retry logic */
export declare const DefaultBackoffOptions: BackoffOptions;
export declare function retry<T>(fn: () => Promise<T>, options?: BackoffOptions): Promise<T | undefined>;
/**
* Normalizes the authentication property of a DID document to an array of strings.
* Extracts authentication method identifiers from mixed string/object format.
*
* @param didDocument - DID document to normalize authentication for
* @returns Array of authentication method identifiers
* @throws Error if authentication section is missing
*/
export declare function normalizeAuthentication(didDocument: DIDDocument): string[];
/**
* Normalizes the controller property of a DID document to an array of strings.
* Defaults to self-controlled if no controller is specified.
*
* @param didDocument - DID document to normalize controller for
* @returns Array of controller DID identifiers
*/
export declare function normalizeController(didDocument: DIDDocument): string[];
/**
* Normalizes DID document services to protobuf format.
* Converts service endpoints to arrays and includes optional properties.
*
* @param didDocument - DID document containing services to normalize
* @returns Array of protobuf-formatted services or undefined if no services
*/
export declare function normalizeService(didDocument: DIDDocument): ProtoService[] | undefined;
/**
* Converts protobuf services back to standard DID document service format.
* Handles special context requirements for LinkedDomains services.
*
* @param didDocument - Protobuf DID document containing services to denormalize
* @returns Array of standard DID document services
*/
export declare function denormalizeService(didDocument: DidDoc): Service[];
//# sourceMappingURL=utils.d.ts.map