Skip to content

Commit

Permalink
shared ecnryption keys
Browse files Browse the repository at this point in the history
  • Loading branch information
nsjames committed Oct 13, 2018
1 parent 2f689aa commit c8b8252
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 4 deletions.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@
"@ledgerhq/hw-transport-node-hid": "^4.22.0",
"aes-oop": "^1.0.4",
"asn1-ber": "^1.0.9",
"bigi": "^1.4.2",
"bip32-path": "^0.4.2",
"bip39": "^2.5.0",
"bytebuffer": "^5.0.1",
"create-hash": "^1.2.0",
"ecurve": "^1.0.6",
"electron-store": "^2.0.0",
"eos-rc-parser": "^1.0.4",
"eosjs": "^16.0.9",
Expand Down Expand Up @@ -162,7 +166,9 @@
"asar": true,
"protocols": {
"name": "electron-deep-linking",
"schemes": ["scatter"]
"schemes": [
"scatter"
]
},
"files": [
"dist/**/*",
Expand Down
42 changes: 42 additions & 0 deletions src/components/sidebars/Auth.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
const { remote } = window.require('electron');
const fs = window.require('fs');
import {Blockchains} from '../../models/Blockchains'
import Crypto from "../../util/Crypto";
export default {
name: 'Auth',
Expand All @@ -76,6 +78,46 @@
this.confirmPassword = '';
document.addEventListener('keydown', this.modifyDPresses, true);
const test = {
[Blockchains.EOSIO]:{
privateKeyBuffer:Crypto.privateKeyToBuffer('5K55tgv4RZ1zKgsBrTsVhVDmHBUr4akojWjTgaaxuEAYfduRjGs', Blockchains.EOSIO),
publicKey:'EOS4vFYEWz7SXrrXmjgSDjop91B6ct83GrXie66uDoQo7FuGpYRqB',
privateKeyBuffer2:Crypto.privateKeyToBuffer('5HxhvMnjVrGaCh8KWQpva151gnNScj9jieinTdp3nFfJP9QFL4m', Blockchains.EOSIO),
publicKey2:'EOS8eG7dZaknL7z4PZ8P9Luv6KovuQyRpN82Xv1D1vkMT7k8hM3GU'
},
[Blockchains.ETH]:{
privateKeyBuffer:Crypto.privateKeyToBuffer('a54c3cb607311b0e825b9fd3021f71c7a5d1b09d06e5fd56ba59dba8eb465b0f', Blockchains.ETH),
publicKey:'0x867da690465f264a6fd9efc1674d6a64e3d9f0d6',
privateKeyBuffer2:Crypto.privateKeyToBuffer('131e2a08ca84921b3ff5e6f785a0843805d9e2d338d261fc491069da39fc4520', Blockchains.ETH),
publicKey2:'0xf748926d9426f949b3c5453186b0e7cc56325612'
},
[Blockchains.TRX]:{
privateKeyBuffer:Crypto.privateKeyToBuffer('a54c3cb607311b0e825b9fd3021f71c7a5d1b09d06e5fd56ba59dba8eb465b0f', Blockchains.TRX),
publicKey:'TNEL588Ti6maTWBMwv4akEawdGFVgVCoRH',
privateKeyBuffer2:Crypto.privateKeyToBuffer('131e2a08ca84921b3ff5e6f785a0843805d9e2d338d261fc491069da39fc4520', Blockchains.TRX),
publicKey2:'TYWiqq9jJnh1LMVRPMwSVue2fmzxbFBfyg'
},
}
Object.keys(test).map(blockchain => {
const {privateKeyBuffer, publicKey, privateKeyBuffer2, publicKey2} = test[blockchain];
const encryptionKey = Crypto.getEncryptionKey(privateKeyBuffer, publicKey, 1);
const encryptionKey2 = Crypto.getEncryptionKey(privateKeyBuffer2, publicKey2, 1);
console.log(`${blockchain.toUpperCase()} Encryption Key: ${encryptionKey}`);
const encrypted = Crypto.encryptMessage('helloworld', encryptionKey);
console.log('encrypted: ', encrypted);
const decrypted = Crypto.decryptMessage(encrypted, encryptionKey);
console.log('decrypted:', decrypted);
})
},
destroyed(){
document.removeEventListener('keydown', this.modifyDPresses, true);
Expand Down
1 change: 1 addition & 0 deletions src/models/api/ApiActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const LINK_ACCOUNT = 'linkAccount';
export const HAS_ACCOUNT_FOR = 'hasAccountFor';
export const GET_OR_REQUEST_IDENTITY = 'getOrRequestIdentity';
export const IDENTITY_FROM_PERMISSIONS = 'identityFromPermissions';
export const GET_ENCRYPTION_KEY = 'getEncryptionKey';
export const FORGET_IDENTITY = 'forgetIdentity';
export const REQUEST_TRANSFER = 'requestTransfer';
export const REQUEST_SIGNATURE = 'requestSignature';
Expand Down
18 changes: 18 additions & 0 deletions src/services/ApiService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as StoreActions from '../store/constants'
import ObjectHelpers from '../util/ObjectHelpers'
import Hasher from '../util/Hasher'
import IdGenerator from '../util/IdGenerator'
import Crypto from '../util/Crypto'

import {Popup} from '../models/popups/Popup';
import PopupService from '../services/PopupService';
Expand Down Expand Up @@ -455,6 +456,23 @@ export default class ApiService {
});
}

static async [Actions.GET_ENCRYPTION_KEY](request){
return new Promise(async resolve => {

const {payload} = request;
const {fromPublicKey, toPublicKey, nonce} = request.payload;

let keypair = KeyPairService.getKeyPairFromPublicKey(fromPublicKey);
if(!keypair) return resolve({id:request.id, result:Error.signatureError("no_from_key", "This user does not have the FROM key")});

// ... popup prompt for authorization

keypair = KeyPairService.getKeyPairFromPublicKey(fromPublicKey, true);
const encryptionKey = Crypto.getEncryptionKey(keypair.privateKey, toPublicKey, nonce);
return resolve({id:request.id, result:encryptionKey});
});
}

/***
* Gets the Scatter version
* @param request
Expand Down
34 changes: 33 additions & 1 deletion src/util/Crypto.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import ecc from 'eosjs-ecc';
const {PrivateKey} = ecc;
import bip39 from 'bip39'
const ecurve = require('ecurve');
const Point = ecurve.Point;
const secp256k1 = ecurve.getCurveByName('secp256k1');
const BigInteger = require('bigi');
const ByteBuffer = require('bytebuffer')
const createHash = require('create-hash');
import AES from 'aes-oop';
const crypto = require('crypto');

import PluginRepository from '../plugins/PluginRepository';

const sha512 = s => createHash('sha512').update(s).digest('hex');
const toBinaryBuffer = o => (o ? Buffer.isBuffer(o) ? o : new Buffer(o, 'binary') : o)


export default class Crypto {

static async generatePrivateKey(){
Expand All @@ -22,4 +33,25 @@ export default class Crypto {
return ecc.sha256(buffer);
}

static getEncryptionKey(privateKeyBuffer, publicKey, nonce){
const sharedKey = Crypto.sharedSecret(privateKeyBuffer, publicKey);
let ebuf = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN)
ebuf.writeUint64(nonce)
ebuf.append(sharedKey.toString('binary'), 'binary')
ebuf = new Buffer(ebuf.copy(0, ebuf.offset).toBinary(), 'binary')
return sha512(ebuf)
}

static sharedSecret(privateKeyBuffer, publicKey){
let publicKeyBuffer = Buffer.from(publicKey);
let keyBufferPoint = Point.fromAffine(
secp256k1,
BigInteger.fromBuffer( publicKeyBuffer.slice( 1,33 )), // x
BigInteger.fromBuffer( publicKeyBuffer.slice( 33,65 )) // y
)
let P = keyBufferPoint.multiply(BigInteger.fromBuffer(privateKeyBuffer));
let S = P.affineX.toBuffer({size: 32});
return sha512(S)
}

}
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2165,7 +2165,7 @@ create-error-class@^3.0.0:
dependencies:
capture-stack-trace "^1.0.0"

create-hash@^1.1.0, create-hash@^1.1.2, create-hash@^1.1.3:
create-hash@^1.1.0, create-hash@^1.1.2, create-hash@^1.1.3, create-hash@^1.2.0:
version "1.2.0"
resolved "http://registry.npmjs.org/create-hash/-/create-hash-1.2.0.tgz#889078af11a63756bcfb59bd221996be3a9ef196"
dependencies:
Expand Down Expand Up @@ -2777,7 +2777,7 @@ ecc-jsbn@~0.1.1:
jsbn "~0.1.0"
safer-buffer "^2.1.0"

ecurve@^1.0.5:
ecurve@^1.0.5, ecurve@^1.0.6:
version "1.0.6"
resolved "https://registry.yarnpkg.com/ecurve/-/ecurve-1.0.6.tgz#dfdabbb7149f8d8b78816be5a7d5b83fcf6de797"
dependencies:
Expand Down

0 comments on commit c8b8252

Please sign in to comment.