Home
Helper Functions

Encryption

These functions allow you to encrypt/decrypt data.

Encrypt Bytes

bytesrequired
Uint8Array

The byte array that you want to encrypt

import {encryptBytes} from "selfguard-client/helpers/encryption.js";

encryptBytes(bytes);

Decrypt Bytes

encrypted_bytesrequired
Uint8Array

The byte array returned from encryptBytes

encryption_keyrequired
bytes

The encryption key returned from encryptBytes

import {decryptBytes} from "selfguard-client/helpers/encryption.js";

decryptBytes(encrypted_bytes, encryption_key)

Encrypt Value

valuerequired
string

The value that you want to encrypt

import {encryptValue} from "selfguard-client/helpers/encryption.js";

encryptValue(value);

Decrypt Value

encrypted_valuerequired
string

The value that you want to encrypt

encryption_keyrequired
bytes

The encryption key returned from encryptValue

import {decryptValue} from "selfguard-client/helpers/encryption.js";

decryptValue(encrypt_value, encryption_key)

Files

These functions allow you to encrypt/decrypt files, where the encryption keys are stored with Lit Protocol if metamask is enabled in the use of these functions.

Stream Encrypt Web

filerequired
File

The File you want to encrypt.

public_keyrequired
string

If this is set to metamask, it will save the encryption key with Lit Protocol.

chunk_functionrequired
function

This function is called after each file shard is encrypted with three parameters encrypted_bytes, encryption_key, and shard_size.

import {streamEncryptWeb} from "selfguard-client/helpers/encryption.js";

await streamEncryptWeb(file, public_key, chunk_function);

Decrypt Shard

filerequired
File

The File you want to decrypt.

encryption_keyrequired
string

The encryption key for this file shard returned from the chunk_function within streamEncryptWeb

public_keyrequired
string

If this is set to metamask, it will retrieve the encryption key with Lit Protocol. This meant that the encryption_key provided in this function was the encrypted encryption key returned from Lit during streamEncryptWeb.

import {decryptShard} from "selfguard-client/helpers/encryption.js";

await decryptShard(file, encryption_key, public_key)

Metamask

These functions allow you to perform encryption events with Metamask

Get Public Key

import {getPublicKey} from "selfguard-client/helpers/metamask.js";

await getPublicKey();

Encrypt Data

accountrequired
string

The address of the metamask user.

publicKeyrequired
string

The public key of the metamask user retrieved with getPublicKey

datarequired
object | string

The data you want to decrypt.

import {encryptData} from "selfguard-client/helpers/metamask.js";

await encryptData(data,{publicKey,account})

Decrypt Data

accountrequired
string

The address of the metamask user.

datarequired
object | string

The data you want to decrypt.

import {decryptData} from "selfguard-client/helpers/metamask.js";

await decryptData(account,data);

Lit Protocol

These functions allow you to save and retrieve encryption keys stored with lit protocol. These will automatically trigger Metamask to open and sign a condition ensuring the wallet owner is able to retrieve the encryption key.

symmetricKeyrequired
string

The key which you want to save with an access control restriction. Currenty this is based upon the user’s wallet ownership.

chainrequired
string

The chain upon which you want to have the access condition be based on. This wil be leveraged for more complex access controls.

Save Lit Encryption Key

import {saveLitEncryptionKey} from 'selfguard-client/helpers/lit.js';

await saveLitEncryptionKey(symmetricKey, chain)

Get Lit Encryption Key

encryptedSymmetricKeyrequired
string

The encrypted key retrieved from saveLitEncryptionKey

chainrequired
string

The chain upon which you want to have the access condition be based on. This wil be leveraged for more complex access controls.

import {getLitEncryptionKey} from 'selfguard-client/helpers/lit.js';

await getLitEncryptionKey(encryptedSymmetricKey, chain)