Skip to content

Commit

Permalink
#55 encrypt user data
Browse files Browse the repository at this point in the history
  • Loading branch information
fsw committed Jun 5, 2021
1 parent 8e12acd commit bede9c1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Engine.ts
Expand Up @@ -20,6 +20,7 @@ import {UniswapProdProvider, UniswapTestProvider} from "./ExchangeProviders/Unis
import {ChangellyProvider} from "./ExchangeProviders/ChangellyProvider";
import {ChangeNowProvider} from "./ExchangeProviders/ChangeNowProvider";
import {Version} from "./Tools/Changelog";
import { encrypt, decrypt } from 'eciesjs'

export {Keychain}
export {Strings}
Expand Down Expand Up @@ -241,7 +242,22 @@ export class Engine {
}
}
await this.storageSet('wallets', wallets);
console.log(this.encryptData(wallets));
//TODO save online

}

encryptData(data: any): string {
let encryptionKey = this.keychain.derivePath("m/10'/0'/0'/0/0");
let string = JSON.stringify(data);
let buf = Buffer.from(string)
return encrypt(encryptionKey.publicKey.toString('hex'), buf).toString('hex');
}

decryptData(encrypted: string): any {
let encryptionKey = this.keychain.derivePath("m/10'/0'/0'/0/0");
let decrypted = decrypt(encryptionKey.privateKey.toString('hex'), Buffer.from(encrypted, 'hex')).toString();
return JSON.parse(decrypted);
}

loadPortfolio(handler: BaseCoinHandler, data: PortfolioLegacyItemData[]|PortfolioItemData[]): PortfolioItem[] {
Expand Down
26 changes: 26 additions & 0 deletions test/dataencryption.ts
@@ -0,0 +1,26 @@
import {strictEqual, notStrictEqual, deepStrictEqual} from "assert";
import {Engine, Keychain} from "../src/Engine";
import {CacheMock, LogMock, StorageMock} from "./_mocks";
import {mnemonic1} from "./_data";

describe('Engine Data Encryption Test', function() {
describe('Data Encryption', function () {
it('core', function () {
let engine = new Engine(
new StorageMock(),
new LogMock(),
new CacheMock()
);
engine.keychain = new Keychain(mnemonic1);
let data = {
'test' : 'test',
'test2': 12345
};

let encrypted = engine.encryptData(data);
strictEqual(typeof encrypted, 'string');
let decrypted = engine.decryptData(encrypted);
deepStrictEqual(data, decrypted);
});
});
});

0 comments on commit bede9c1

Please sign in to comment.