Skip to content

Commit

Permalink
encrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
lovelycs committed Sep 20, 2018
1 parent 81e0f53 commit e427922
Show file tree
Hide file tree
Showing 19 changed files with 180 additions and 134 deletions.
7 changes: 7 additions & 0 deletions libs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,12 @@ export default {
hex += ''+str.charCodeAt(i).toString(16);
}
return hex;
},
hexToStr (hex) {
var string = '';
for (var i = 0; i < hex.length; i += 2) {
string += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
}
return string;
}
};
18 changes: 8 additions & 10 deletions src/Vite/account/index.js → src/Vite/account.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
let nacl = require('../../../libs/nacl_blake2b');
let nacl = require('../../libs/nacl_blake2b');
let blake = require('blakejs/blake2b');

import basicStruct from '../basicStruct';
import libUtils from '../../../libs/utils';
import utils from '../../utils/index';
import basicStruct from './basicStruct';
import libUtils from '../../libs/utils';
import address from '../address';

class Account extends basicStruct {
constructor(provider) {
super(provider);
}

newHexAddr(privKey) {
return utils.newHexAddr(privKey);
return address.newHexAddr(privKey);
}

signTX(accountBlock, privKey) {
let source = getSource(accountBlock);
source = libUtils.hexToBytes(source);

let addr = utils.newHexAddr(privKey);
let addr = address.newHexAddr(privKey);
let pubKey = addr.pubKey; // Hex string

let hash = blake.blake2b(source, null, 32);
Expand All @@ -41,9 +41,9 @@ function getSource(accountBlock) {

source += accountBlock.prevHash || '';
source += (accountBlock.meta && accountBlock.meta.height) ? libUtils.strToHex(accountBlock.meta.height) : '';
source += accountBlock.accountAddress ? utils.getAddrFromHexAddr(accountBlock.accountAddress) : '';
source += accountBlock.accountAddress ? address.getAddrFromHexAddr(accountBlock.accountAddress) : '';
if (accountBlock.to) {
source += utils.getAddrFromHexAddr(accountBlock.to);
source += address.getAddrFromHexAddr(accountBlock.to);
source += getRawTokenid(accountBlock.tokenId) || '';
source += libUtils.strToHex(accountBlock.amount) || '';
} else {
Expand All @@ -64,7 +64,5 @@ function getRawTokenid(tokenId) {
if (tokenId.indexOf('tti_') !== 0) {
return null;
}
console.log(tokenId);
console.log(tokenId.slice(4, tokenId.length - 2));
return tokenId.slice(4, tokenId.length - 2);
}
10 changes: 5 additions & 5 deletions src/Vite/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Version from './version.js';
import Account from './account/index.js';
import Ledger from './ledger/index.js';
import P2P from './p2p/index.js';
import Types from './types/index.js';
import Account from './account.js';
import Ledger from './ledger.js';
import P2P from './p2p.js';
import Types from './types.js';

class Vite {
constructor(provider) {
Expand All @@ -21,4 +21,4 @@ class Vite {
}
}

export default Vite;
export default Vite;
10 changes: 1 addition & 9 deletions src/Vite/ledger/index.js → src/Vite/ledger.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import basicStruct from '../basicStruct.js';
import basicStruct from './basicStruct.js';
import BigNumber from 'bignumber.js';

BigNumber.config({
Expand All @@ -21,14 +21,6 @@ class Ledger extends basicStruct {
return this.provider.request('ledger_sendTx', [ accountBlock ]);
}

createTxWithPassphrase({
selfAddr, toAddr, passphrase, tokenTypeId, amount
}) {
return this.provider.request('ledger_createTxWithPassphrase', {
selfAddr, toAddr, passphrase, tokenTypeId, amount
});
}

getBlocksByAccAddr ({
accAddr, index, count = 20, needTokenInfo = false
}) {
Expand Down
2 changes: 1 addition & 1 deletion src/Vite/p2p/index.js → src/Vite/p2p.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import basicStruct from '../basicStruct.js';
import basicStruct from './basicStruct.js';

class P2P extends basicStruct {
constructor(provider) {
Expand Down
6 changes: 3 additions & 3 deletions src/Vite/types/index.js → src/Vite/types.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import basicStruct from '../basicStruct.js';
import utils from '../../utils/index';
import basicStruct from './basicStruct.js';
import address from '../address';

class Types extends basicStruct {
constructor(provider) {
super(provider);
}

isValidHexAddr(hexAddr) {
return utils.isValidHexAddr(hexAddr);
return address.isValidHexAddr(hexAddr);
}

isValidTokenId(tokenId) {
Expand Down
1 change: 0 additions & 1 deletion src/Vite/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class Version extends basicStruct {

}

// ???
getP2PVersion() {

}
Expand Down
96 changes: 83 additions & 13 deletions src/Wallet/account.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
// const loopTime = 2000;
const nacl = require('../../libs/nacl_blake2b');
const scryptsy = require('scryptsy');

import libUtils from '../../libs/utils';

const loopTime = 2000;

class Account {
constructor(Vite) {
this.Vite = Vite;
this.addrList = [];

this.version = 1;
// LightScryptN is the N parameter of Scrypt encryption algorithm, using 4MB
// memory and taking approximately 100ms CPU time on a modern processor.
this.n = 4096;
// LightScryptP is the P parameter of Scrypt encryption algorithm, using 4MB
// memory and taking approximately 100ms CPU time on a modern processor.
this.p = 6;
this.scryptR = 8;
this.scryptKeyLen = 32;
}

getunLockAddrList () {
Expand All @@ -28,13 +43,13 @@ class Account {
return;
}

// let loop = ()=>{
// let loopTimeout = setTimeout(()=>{
// clearTimeout(loopTimeout);
// loopTimeout = null;
// this._loopAddr(address, privKey);
// }, loopTime);
// };
let loop = ()=>{
let loopTimeout = setTimeout(()=>{
clearTimeout(loopTimeout);
loopTimeout = null;
this._loopAddr(address, privKey);
}, loopTime);
};

this.Vite.Ledger.getReceiveBlock(address).then((accountBlock)=>{
if (!accountBlock) {
Expand All @@ -52,16 +67,71 @@ class Account {
}).catch((err)=>{
console.log(err);
});
// loop();
loop();
}).catch((err)=>{
console.error(err);
// loop();
loop();
});
}

// signTX() {
encrypt(str) {
let scryptParams = {
n: this.n,
r: this.scryptR,
p: this.p,
keylen: this.scryptKeyLen,
salt: libUtils.bytesToHex(nacl.randomBytes(32)),
};
let encryptP = encryptKey(str, scryptParams);

return {
encryptP: encryptP.toString('hex'),
scryptParams,
version: this.version
};
}

verify(scryptP, str) {
if ( !isValid.call(this, scryptP) ) {
return false;
}

let encryptP = encryptKey(str, scryptP.scryptParams);
return encryptP.toString('hex') === scryptP.encryptP;
}
}

export default Account;

// }
function encryptKey(pwd, scryptParams) {
let pwdBuff = Buffer.from(pwd);
let salt = libUtils.hexToBytes(scryptParams.salt);
salt = Buffer.from(salt);
return scryptsy(pwdBuff, salt, +scryptParams.n, +scryptParams.r, +scryptParams.p, +scryptParams.keylen);
}

export default Account;
function isValid(scryptP) {
if (!scryptP.scryptParams ||
!scryptP.encryptP ||
!scryptP.version ||
scryptP.version !== 1) {
return false;
}

let scryptParams = scryptP.scryptParams;
if (!scryptParams.n ||
!scryptParams.r ||
!scryptParams.p ||
!scryptParams.keylen ||
!scryptParams.salt) {
return false;
}

try {
libUtils.hexToBytes(scryptParams.salt);
} catch(err) {
return false;
}

return true;
}
20 changes: 12 additions & 8 deletions src/Wallet/address.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import bip39 from 'bip39';
import hd from '../../libs/hd';
import libUtils from '../../libs/utils';
import utils from '../utils/index';
import address from '../address';

const rootPath = 'm/44\'/666666\'';

class Address {
getEntropyFromMnemonic(mnemonic) {
Expand All @@ -16,16 +18,16 @@ class Address {
return bip39.entropyToMnemonic(entropy);
}

newAddr(rootPath) {
newAddr() {
let mnemonic = bip39.generateMnemonic(256);
let entropy = bip39.mnemonicToEntropy(mnemonic);
let seed = bip39.mnemonicToSeedHex(mnemonic);
let addr = getAddrFromPath.call(this, rootPath, seed);
return { addr, entropy };
}

newAddrFromMnemonic(mnemonic, path) {
if (!mnemonic || !path) {
newAddrFromMnemonic(mnemonic, index) {
if (!mnemonic) {
return false;
}

Expand All @@ -34,12 +36,14 @@ class Address {
return false;
}

let path = `${rootPath}/${index}\'`;
let seed = bip39.mnemonicToSeedHex(mnemonic);
return getAddrFromPath.call(this, path, seed);
}

getAddrsFromMnemonic(mnemonic, rootPath, num = 10) {
if (!mnemonic || !rootPath) {
getAddrsFromMnemonic(mnemonic, num = 10, path) {
path = path || rootPath;
if (!mnemonic || num > 10 || num < 0) {
return false;
}

Expand All @@ -52,7 +56,7 @@ class Address {
let seed = bip39.mnemonicToSeedHex(mnemonic);

for (let i=0; i<num; i++) {
let currentPath = `${rootPath}/${i}\'`;
let currentPath = `${path}/${i}\'`;
let addr = getAddrFromPath.call(this, currentPath, seed);
addrs.push(addr);
}
Expand All @@ -67,5 +71,5 @@ function getAddrFromPath(path, seed) {
let { key } = hd.derivePath(path, seed);
let { privateKey } = hd.getPublicKey(key);
let priv = libUtils.bytesToHex(privateKey);
return utils.newHexAddr(priv);
return address.newHexAddr(priv);
}
5 changes: 3 additions & 2 deletions src/Wallet/keystore.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import libUtils from '../../libs/utils';
import utils from '../utils/index';
import address from '../address.js';

const uuid = require('pure-uuid');
const scryptsy = require('scryptsy');
Expand Down Expand Up @@ -95,7 +95,7 @@ class keystore {
!keyJson.crypto ||
!keyJson.hexaddress ||
!keyJson.keystoreversion ||
!utils.isValidHexAddr(keyJson.hexaddress)) {
!address.isValidHexAddr(keyJson.hexaddress)) {
return false;
}

Expand Down Expand Up @@ -150,6 +150,7 @@ class keystore {
privKey = decipher.update(libUtils.hexToBytes(ciphertext), 'utf8', 'hex');
privKey += decipher.final('hex');
} catch(err) {
console.warn(err);
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/utils/address.js → src/address.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
let blake = require('blakejs/blake2b');
let nacl = require('../../libs/nacl_blake2b');
let nacl = require('../libs/nacl_blake2b');

import utils from '../../libs/utils';
import utils from '../libs/utils';

const ADDR_PRE = 'vite_';
const ADDR_SIZE = 20;
Expand Down
10 changes: 0 additions & 10 deletions src/utils/index.js

This file was deleted.

Loading

0 comments on commit e427922

Please sign in to comment.