selfguard-node v1.1.54
Installation
npm install selfguard-client
Usage
Import SelfGuard-Client
import SelfGuard from 'selfguard-client';
// or
let SelfGuard = require('selfguard-client');
Instantiate
let sg = new SelfGuard(API_KEY);
// or
let sg = new SelfGuard(API_KEY, PUBLIC_KEY, PRIVATE_KEY);
//This will leverage asymmetric encryption for all stored encryption keys.
//When encryption keys are sent to SelfGuard they are encrypted using the PUBLIC_KEY.
//When encryption keys are recieved from SelfGuard they are decrypted using the PRIVATE_KEY.
// or
let sg = new SelfGuard(API_KEY, 'metamask');
//This will leverage the user's metamask wallet to asymmetrically encrypt data.
Key Pair
Generate Public Private Key Pair
Allows you to create an RSA or ECSDA key pair.
let key_pair = sg.createKeyPair('rsa' || 'ecdsa');
console.log(key_pair);
/*
{
public_key: '-----BEGIN RSA PUBLIC KEY-----\n....',
private_key: '-----BEGIN RSA PRIVATE KEY-----\n...'
}
*/
Upload Key Pair
Allows you to save this key pair with SelfGuard, encrypted with a password
await sg.uploadKeyPair(key_pair,'password');
Get Key Pairs
Allows you to retrieve all encrypted key pairs stored with this account
await sg.getKeyPairs();
Encryption
Encrypt:
Allows you to encrypt any piece of data and receive the respective encryption key id (the encryption key is stored with SelfGuard) and the encrypted text.
let {encrypted_text, encryption_key_id} = await sg.encrypt( 'This is some super top secret text!')
console.log({encrypted_text,encryption_key_id})
// {encrypted_text: '5ac4asffda...... ', encryption_key_id:'e791a8a...'}
Decrypt:
Allows you to decrypt previously encrypted data by providing the encrypted text and the encryption key id respective to the encrypted data.
let decryptedText = await sg.decrypt(encrypted_text, encryption_key_id)
console.log(decryptedText)
// 'This is some super top secret text!'
Encrypt With Password
Allows you to encrypt any piece of data with a password.
let ciphertext = sg.encryptWithPassword( 'This is some super top secret text!','password')
Decrypt With Password:
Allows you to decrypt encrypted data with the respective password.
let decryptedText = sg.decryptWithPassword(ciphertext, 'password')
console.log(decryptedText)
// 'This is some super top secret text!'
File Storage
Used for storing encrypted files onto decentralized storage protocols like IPFS.
Upload/Encrypt File
await sg.encryptFile(file,(err,progress) => {
console.log(progress);
});
Decrypt File
await sg.decryptFile(id,(err,progress) => {
console.log(progress);
});
Get List of Files
await sg.getFiles();
Data Tokenization
Tokenize:
Allows you to encrypt data and store the encrypted data with SelfGuard itself.
let token_id = await sg.tokenize( 'This is some super top secret text!')
console.log(token_id)
// tok_14A...
Detokenize:
Allows you to retrieve the previously tokenized data by providing the respective token id.
let data = await sg.detokenize(token_id)
console.log(data)
// 'This is some super top secret text!'
Encrypted Key/Value Storage
Put:
Allows you to store any key value data where the value is encrypted.
let success = await sg.put('key','value');
Get:
Allows you to retrieve key value data where the value is decrypted upon retrieval
let value = await sg.get('key');
console.log(value)
// 'value'
Get Keys
Allows you to get all the keys (amongst all the key-value objects) stored with this account
await sg.getKeys();
Encrypted Array Storage
Used as an encrypted database to store key -> multiple values. Value data is fully encrypted by an encryption key set up at the initiation of the array. Value data can only be decrypted by users who have been assigned access to the encryption key via asymmetric encryption.
Create Array
await sg.initArray('key');
Add To Array
await sg.addToArray('key','value');
await sg.addToArray('key','value2');
Add User To Array
await sg.addUserToArray('key','0xabc...');
Get Array
let data = await sg.getArray('key');
console.log(data)
// ['value','value2']
Get Array Names
let keys = await sg.getArrayNames();
console.log(keys);
/*
[{
name: 'key',
length: 2,
created_at: '2022-09-07T19:58:35.616997+00:00'
}]
*/
Notifications
Used to send texts or emails to addresses who's email and phone number are stored using the encrypted key/value storage.
React Component For Notifications
Add Package
npm install selfguard-react-components
Implement Component
import { Notifications } from 'selfguard-react-components';
return (
<Notifications userAddress={account} api_key={process.env.REACT_APP_SELFGUARD_API_KEY} />
)
Update Profile
await sg.updateProfile('0xadfb..',{email:'test@gmail.com',phone:'+12121112345'});
Send SMS
await sg.sendSMS({address:'0xadfb..',text:'Example Text'});
Send Email
await sg.sendEmail({address:'0xadfb..',from:'example@test.com',fromName:'test',replyTo:'reply@test.com', reployToName:'test',subject:'Test Subject', html:'This is the content of the email'});