Klave logo

Connecting to Klave

The SCP class is the main entry point to Klave and provides a set of methods to connect to Klave, manage the connection and interact with your Klave applications.

The connection api of the SCP is the connect method, which needs a url and a cryptographic key. You can either import an existing key or create a new one through the Key class.

Connecting to Klave

import { Key, SCP } from '@secretarium/connector';
 
async function main() {
 
    // We create the new connector context
    const context = new SCP();
 
    // We create a new connection key or provide an existing one
    const myKey = await Key.createKey()
 
    // We start the connection to Klave
    const connection = await context.connect('wss://on.klave.network', myKey)
}
 
main()

The default createKey method uses the SubtleCrypto library to generate a cryptographic key with ECDSA algorithm and P-256 curve, extractable and whose usages allow to sign and verify.

Connecting to Klave importing an existing key

Klave provides the method Context.get('sender') to identify the key used for connection purposes. This allows you to whitelist a specific key in your Klave application. To this end, we provide the exportKey and importKey methods for exporting and importing keys in clear text, and the seal and exportEncryptedKey methods for exporting and importing keys in encrypted format in the TypeScript connector.

import { SCP } from '@secretarium/connector';
 
async function main() {
    
    // We create the new connector context
    const context = new SCP();
    
    // We create a key and export/import it to/from clear text
    const myKey1 = await Key.createKey();
    const myExportedKey = await myKey1.exportKey();
    const myImportedKey1 = await Key.importKey(myExportedKey);
 
    // We create a key and export/import it to/from encrypted text
    const myKey2 = await Key.createKey();
    const myEncryptedKey = myKey2.seal('password');
    const myExportedEncryptedKey = await myEncryptedKey.exportEncryptedKey();
    const myImportedKey2 = await Key.importKey(myExportedEncryptedKey);
 
    // We start the connection to Klave
    const connection = await context.connect('wss://on.klave.network', myImportedKey2)
}
 
main()

The importKey method assumes that the public and private key are in JWK format.

On this page