Mind Lake SDK
  • Overview
  • Get started
    • Tutorial step-by-step
    • TypeScript Quick-Start
    • Python Quick-Start
  • Use Cases
    • 1-Single User with Structured Data
    • 2-Single User with UnStructured Data
    • 3-Multi Users with Permission Sharing
  • Glossary
  • TYPESCRIPT API REFERENCE
    • MindLake
    • MindLake.DataLake
    • MindLake.Cryptor
    • MindLake.Permission
    • Return Code
  • PYTHON API REFERENCE
    • MindLake
    • MindLake.DataLake
    • MindLake.Cryptor
    • MindLake.Permission
    • Return Code
Powered by GitBook
On this page
  • 2.1. Task
  • 2.2. Example code to Insert cipher
  • 2.3. Result Table in MindLake
  • 2.4. Example code to query on cipher
  • 2.5. Output
  1. Use Cases

2-Single User with UnStructured Data

In this scenario, users need to store pictures in an encrypted format to protect their privacy and ensure the security of their data. The system uses the Mind Lake library to achieve this by encrypting the pictures before saving them and decrypting them upon retrieval.

2.1. Task

  1. Encrypting the picture data and uploading to MindLake.

  2. Querying and decrypting the encrypted picture data from MindLake.

2.2. Example code to Insert cipher

import {MindLake} from 'mind-lake-sdk';
import md5 from 'js-md5';
const DataType = MindLake.DataType;
  
const response = await fetch("https://avatars.githubusercontent.com/u/97393721");
if(!response || response.status !== 200) {
  return console.error("Failed to get picture from github")
}
const image = await response.arrayBuffer();
console.log("MD5 of the original picture pic_origin.png: ", md5(image));
const base64 = Buffer.from(image).toString('base64');
const mindLake = await MindLake.getInstance("YOU OWN APP KEY");

// connect to MindLake."5" is example of Goerli Testnet chainId
const chainId = "5"
const res1 = await mindLake.connect(chainId);
if(res1.code !== 0) {
  console.error(res1.message);
  return
}

// create a table
const dataLake = mindLake.dataLake;
await dataLake.dropTable("album");
const res2 = await dataLake.createTable("album", [{columnName: 'name', type: DataType.text, encrypt: false}, {columnName: 'picture', type: DataType.text, encrypt: true}]);
if(res2.code !==0) {
  console.error(res2.message);
  return
}
const cryptor = mindLake.cryptor;
const res3 = await cryptor.encrypt(base64, "album.picture");
if(res3.code !== 0) {
  console.error(res3.message);
  return
}
const sql = `insert into album (name, picture) values ("mind.png", '${res3.result}')`;
const res4 = await dataLake.query(sql);
if(res4.code !== 0) {
  console.error(res4.message);
  return
}
import env
import mindlakesdk
import requests
from base64 import a85decode, a85encode
from hashlib import md5

# 1. connect to mindlake, '5' is example of Goerli Testnet chainID
chainID = '5'
mindlake = mindlakesdk.connect(env.walletPrivateKeyAlice, env.appKey, chainID)
assert mindlake, mindlake.message

# 2. create a table
result = mindlake.datalake.createTable('album',
        [
            mindlake.datalake.Column('name', mindlake.DataType.text, False),
            mindlake.datalake.Column('picture', mindlake.DataType.text, True),
        ])
assert result, result.message

# 3. get a picture from github
response = requests.get('https://avatars.githubusercontent.com/u/97393721')
if response and response.status_code == 200:
    pic = response.content
else:
    raise Exception('Failed to get picture from github')

file = open('pic_origin.png', 'wb')
file.write(pic)
file.close()
print('MD5 of the original picture pic_origin.png: ' + md5(pic).hexdigest())

# 4. encrypt and insert the data
result = mindlake.cryptor.encrypt(a85encode(pic).decode(),'album.picture')
assert result, result.message
encryptedPic = result.data

result = mindlake.datalake.query(f"""
INSERT INTO "album" 
       ("name", "picture") 
VALUES ('{'mind.png'}',
        '{encryptedPic}') returning *""")
assert result, result.message

2.3. Result Table in MindLake

name
picture

(text, encrypt=False)

(text, encrypt=True)

mind.png

0x11223344

2.4. Example code to query on cipher

const selectSql = `select * from album`;
const res9 = await dataLake.query(selectSql);
if(res9.code !== 0) {
  console.error(res9.message);
  return
}
for (const row of res9.result.data) {
  const decrypt = await cryptor.decrypt(row[1]);
  if(decrypt.code !== 0) {
    return console.error(decrypt.message)
  }
  console.log(`MD5 of the decrypted picture ${row[0]}: `, md5(Buffer.from(decrypt.result, 'hex')))
}
# 5. query encrypted data
result = mindlake.datalake.query('SELECT * FROM "album"')
assert result, result.message

# 6. decrypt the data
for row in result.data['data']:
    name = row[0]
    resultPic = mindlake.cryptor.decrypt(row[1])
    assert resultPic, resultPic.message
    decryptedPic = a85decode(resultPic.data)
    file = open(f'{name}', 'wb')
    file.write(decryptedPic)
    file.close()
    print(f'MD5 of the decrypted picture {name}: ' + md5(decryptedPic).hexdigest())

2.5. Output

MD5 of the original picture pic_origin.png: 7b9b1b4591f6af62bda68dd97a2728ca
MD5 of the decrypted picture mind.png: 7b9b1b4591f6af62bda68dd97a2728ca
Previous1-Single User with Structured DataNext3-Multi Users with Permission Sharing

Last updated 1 year ago