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.
import asyncio
from secretarium_connector import SCP, Key
async def main():
#We create the new connector context
context = SCP()
#We create a new connection key or provide an existing one
myKey = await Key.createKey()
#We start the connection to Klave
connection = await context.connect('wss://on.klave.network', myKey)
asyncio.run(main())
The default createKey
method uses the python library cryptography.hazmat
particularly the elliptic curve module
to generate a private key with ECDSA algorithm and "secp256r1" curve.
use klave_connector::{SCP};
#[tokio::main]
async fn main() {
let mut client = SCP::new("wss://on.klave.network", None, None);
match client.connect().await {
Ok(_) => println!("Connected to Klave successfully."),
Err(e) => {
eprintln!("Failed to connect to Klave: {}", e);
return;
}
};
match client.close().await {
Ok(_) => println!("Connection closed successfully."),
Err(e) => eprintln!("Failed to close connection: {}", e),
};
}
The creation of a key is available via the Key object and leverages the p256 crate to create an ECDSA keypair on the "secp256r1" curve.
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.
use klave_connector::{SCP, Key};
#[tokio::main]
async fn main() {
let new_key = Key::new(None);
let exported_key = new_key.clone().export_jwk("<your password>").expect("Failed to export key");
println!("Exported key: {}", exported_key);
let imported_key = match Key::import_jwk(&exported_key, "<your password>") {
Ok(key) => {
println!("Key imported successfully.");
key
}
Err(e) => {
eprintln!("Failed to import key: {}", e);
return;
}
};
let mut client = SCP::new("wss://on.klave.network", Some(&imported_key), None);
let key_from_file = match Key::import_jwk_from_file("<Use your own key>", "<your password>") {
Ok(key) => {
println!("Key imported successfully.");
key
}
Err(e) => {
eprintln!("Failed to import key: {}", e);
return;
}
};
let mut other_client = SCP::new("wss://on.klave.network", Some(&key_from_file), None);
}
Both import_key
and import_key_from_file
methods assumes that the private key is in JWK format with a data field encrypted using a password.
{
"version": 2,
"name": "<your-key-name>",
"iv": "koQ4bHwMJ3oMlh",
"salt": "2z2-4i0NmPk5K3CX3Z8iekoQ4bHwMJ3oMlh3Q2mXHhW4=",
"data": "Xw-yBxGHSr70KRF6ukJPQaDQ4Zfw2CX3Z8iekoQ4bHwM06-DVnvEXtYCM67NA7T2w8fHpSQmHEeA5FmwpqeQJPkp3FAmQ4l-IqrqhYBspBqDiJwZm0nnSkatINmky1jXuEQ4TbL3wXwmBDdjL7isztp2bI2hQvBY6xeMMji82mEzrWE7OX-vzQT99s0dm44ub4x42OtI06_OZkDnQGyNYjx4MBxyXLzsfbVYCnkOwmON8wkW_VhA-In_7kfxWgbzROUN6AvfkPXzkvU71NPOwtYTGdQW6e_yIOWv96tF8VzIkVEf3t39XuFw1fsSel8RbokT-avTuDlcRgTrHD9iLhPCCredNBnzJtWfvkSfVRIblg4f8Iytey30FcisymkwkVaNDjIG39R5gow-kqvClB9ifXvdr-PtYS_25cBctYnu46Yl027YFezwRtVGd5YLdRB-h5oiGFPgJ98rBI3acycjUtiTqpR7m92sNzUVQPyroPQnBDsVnI2B6dXB7jxvPu8aa059kJBgCCL9lnOMEl5yQv8SEwDZzKyxiI"
}
Last updated on