Skip to content
This repository has been archived by the owner on Apr 18, 2021. It is now read-only.

Commit

Permalink
feat: #61 Allow to set private key
Browse files Browse the repository at this point in the history
  • Loading branch information
JhChoy authored and cloudinertia committed Jan 23, 2019
1 parent eec7b80 commit b9c3774
Show file tree
Hide file tree
Showing 18 changed files with 116 additions and 99 deletions.
2 changes: 2 additions & 0 deletions CONFIGURATION-ko.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ vvisp는 `.env` file에 작성된 환경 변수들을 [`process.env`](https://no
- `GAS_LIMIT`: 트랙잭션 발생시 설정하고자 하는 gas limit입니다. 기본값은 4600000입니다.
- `SOLC_VERSION`: 사용하고자 하는 컴파일러 버전을 입력합니다. 특정 버전 입력시 네트워크와의 통신이 필요하며, 입력 값이 없을 경우 vvisp에 내장된 로컬 solc를 사용합니다.
- `SOLC_OPTIMIZATION`: compile 최적화를 원하지 않는다면 false를 입력하십시오. 기본 값은 true입니다.
- `PRIVATE_KEY`: private key *주의: 이 옵션은 MNEMONIC 옵션을 덮어씁니다*

### Examples

Expand All @@ -32,6 +33,7 @@ GAS_PRICE= 20000000000 // 20Gwei
GAS_LIMIT= 5000000 // 500만
SOLC_VERSION= // 내장된 컴파일러를 사용합니다.
SOLC_OPTIMIZATION= // 최적화를 사용합니다.
PRIVATE_KEY= // private key // private key // private key // private key
```

## <a name="service"></a>service.vvisp.json
Expand Down
2 changes: 2 additions & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ We use [dotenv](/~https://github.com/motdotla/dotenv) that loads environment varia
- `GAS_LIMIT`: The gas limit to pay for transactions. Default is 4600000.
- `SOLC_VERSION`: The version of solc compiler version you want to use and it needs network communication. You can keep it empty to use local compiler in vvisp.
- `SOLC_OPTIMIZATION`: If you don't want to optimize compile, set this false. Default is true.
- PRIVATE_KEY: private key for specific account. *IT OVERRIDES MNEMONIC OPTIONS!!!*

### Example

Expand All @@ -32,6 +33,7 @@ GAS_PRICE= 20000000000 // 20Gwei
GAS_LIMIT= 5000000 // 5 million
SOLC_VERSION= // Use local compiler
SOLC_OPTIMIZATION= // Use optimization
PRIVATE_KEY= // private key
```


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
module.exports = function(mnemonic, index) {
if (process.env.PRIVATE_KEY) {
if (process.env.PRIVATE_KEY.slice(0, 2) === '0x')
return process.env.PRIVATE_KEY.slice(2);
else return process.env.PRIVATE_KEY;
}
if (!index) {
index = 0;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/vvisp-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const getCompiledContracts = require('./getCompiledContracts');
const getCycle = require('./getCycle');
const getTxCount = require('./getTxCount');
const getWeb3 = require('./getWeb3');
const mnemonicToPrivateKey = require('./mnemonicToPrivateKey');
const getPrivateKey = require('./getPrivateKey');
const printOrSilent = require('./printOrSilent');
const privateKeyToAddress = require('./privateKeyToAddress');
const sendTx = require('./sendTx');
Expand All @@ -29,7 +29,7 @@ module.exports = {
getCycle,
getTxCount,
getWeb3,
mnemonicToPrivateKey,
getPrivateKey,
printOrSilent,
privateKeyToAddress,
sendTx,
Expand Down
4 changes: 2 additions & 2 deletions packages/vvisp-utils/test/deploy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {
deploy,
getCompiledContracts,
getWeb3,
mnemonicToPrivateKey,
getPrivateKey,
privateKeyToAddress
} = require('../src');
const web3 = getWeb3();
Expand All @@ -19,7 +19,7 @@ const ARRAY_INPUT_CONTRACT = path.join(
'../contracts/DependencyD.sol'
);
const NO_INPUT_CONTRACT = path.join(__dirname, '../contracts/SecondB.sol');
const PRIV_KEY = mnemonicToPrivateKey(process.env.MNEMONIC);
const PRIV_KEY = getPrivateKey(process.env.MNEMONIC);
const SENDER = privateKeyToAddress(PRIV_KEY);

describe('# deploy test', function() {
Expand Down
71 changes: 71 additions & 0 deletions packages/vvisp-utils/test/getPrivateKey.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const chai = require('chai');
const expect = chai.expect;
chai.should();

const { getPrivateKey } = require('../src');

const MNEMONIC =
'away clutch still element short tooth spy hood army split stomach sail';

describe('# getPrivateKey test', function() {
describe('# input arguments', function() {
it('should reject when mnemonic is not string', function() {
expect(() => getPrivateKey(undefined)).to.throw(TypeError);
expect(() => getPrivateKey(null)).to.throw(TypeError);
expect(() => getPrivateKey(123)).to.throw(TypeError);
expect(() => getPrivateKey({ a: 1 })).to.throw(TypeError);
expect(() => getPrivateKey(['hello', 'there'])).to.throw(TypeError);
});
it('should reject when length of mnemonic is not 12', function() {
const mnemonic11 =
'away clutch still element short tooth spy hood army split stomach';
const mnemonic13 =
'away clutch still element short tooth spy hood army split stomach sail still';
expect(() => getPrivateKey('')).to.throw(TypeError);
expect(() => getPrivateKey(mnemonic11)).to.throw(TypeError);
expect(() => getPrivateKey(mnemonic13)).to.throw(TypeError);
});
it('should reject when index is not number except undefined and null', function() {
expect(() => getPrivateKey(MNEMONIC, '123')).to.throw(TypeError);
expect(() => getPrivateKey(MNEMONIC, { a: 1 })).to.throw(TypeError);
expect(() => getPrivateKey(MNEMONIC, ['hello', 'there'])).to.throw(
TypeError
);
});
it('should set 0 when index is undefined or null', function() {
const nullIndex = getPrivateKey(MNEMONIC, null);
const undefinedIndex = getPrivateKey(MNEMONIC, undefined);
const noIndex = getPrivateKey(MNEMONIC);
const normalIndex = getPrivateKey(MNEMONIC, 0);
nullIndex.should.equal(normalIndex);
undefinedIndex.should.equal(normalIndex);
noIndex.should.equal(normalIndex);
});
});
describe('# return value', function() {
it('should be a string', function() {
const result = getPrivateKey(MNEMONIC);
result.should.a('string');
});
it('should have 64 length', function() {
const result = getPrivateKey(MNEMONIC);
result.should.have.lengthOf(64);
});
it('should be different when indexes are different', function() {
const result0 = getPrivateKey(MNEMONIC, 0);
const result1 = getPrivateKey(MNEMONIC, 1);
result0.should.not.equal(result1);
});
});
describe('private key enable', function() {
before(function() {
process.env.MNEMONIC = '';
process.env.PRIVATE_KEY =
'9741fa712a6912b862c9043f8752ffae513cb01895985998c61620da5aaf2d2d';
});
it('should pass when private key given', function() {
const result = getPrivateKey();
result.should.equal(process.env.PRIVATE_KEY);
});
});
});
64 changes: 0 additions & 64 deletions packages/vvisp-utils/test/mnemonicToPrivateKey.test.js

This file was deleted.

6 changes: 3 additions & 3 deletions packages/vvisp-utils/test/privateKeyToAddress.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const chai = require('chai');
chai.should();

const { privateKeyToAddress, mnemonicToPrivateKey } = require('../src');
const { privateKeyToAddress, getPrivateKey } = require('../src');

const MNEMONIC =
'away clutch still element short tooth spy hood army split stomach sail';

describe('# privateKeyToAddress test', function() {
before(function() {
this.privateKey = mnemonicToPrivateKey(MNEMONIC);
this.privateKey = getPrivateKey(MNEMONIC);
});
describe('# return value', function() {
it('should be a string type', function() {
Expand All @@ -34,7 +34,7 @@ describe('# privateKeyToAddress test', function() {
const MNEMONIC2 =
'away clutch still element short sail spy hood army split stomach sail';
const return1 = privateKeyToAddress(this.privateKey);
const return2 = privateKeyToAddress(mnemonicToPrivateKey(MNEMONIC2));
const return2 = privateKeyToAddress(getPrivateKey(MNEMONIC2));
return1.should.not.equal(return2);
});
});
Expand Down
4 changes: 2 additions & 2 deletions packages/vvisp-utils/test/utils/filterPrivateKey.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ const expect = chai.expect;
chai.should();
require('dotenv').config();

const { mnemonicToPrivateKey } = require('../../src');
const { getPrivateKey } = require('../../src');
const filterPrivateKey = require('../../src/utils/filterPrivateKey');

describe('# privateKeyToAddress test', function() {
before(function() {
this.privateKey = mnemonicToPrivateKey(process.env.MNEMONIC);
this.privateKey = getPrivateKey(process.env.MNEMONIC);
});
describe('# input arguments', function() {
it('should reject when private is wrong type', function() {
Expand Down
4 changes: 2 additions & 2 deletions packages/vvisp/bin/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ function checkEnv() {
}
}

if (!process.env.MNEMONIC) {
throw new Error('You should set MNEMONIC in .env file');
if (!process.env.MNEMONIC && !process.env.PRIVATE_KEY) {
throw new Error('You should set MNEMONIC or PRIVATE_KEY in .env file');
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/vvisp/scripts/deploy-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ module.exports = async function(file, arguments, options) {

const {
compileAndDeploy,
mnemonicToPrivateKey,
getPrivateKey,
getWeb3,
printOrSilent
} = require('@haechi-labs/vvisp-utils');
const web3 = getWeb3();

const privateKey = mnemonicToPrivateKey(
const privateKey = getPrivateKey(
process.env.MNEMONIC,
process.env.PRIV_INDEX
);
Expand Down
7 changes: 2 additions & 5 deletions packages/vvisp/scripts/deploy-service/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = (function() {
const path = require('path');
const { getWeb3, mnemonicToPrivateKey } = require('@haechi-labs/vvisp-utils');
const { getWeb3, getPrivateKey } = require('@haechi-labs/vvisp-utils');
const web3 = getWeb3();

return {
Expand All @@ -19,10 +19,7 @@ module.exports = (function() {
? web3.utils.toHex(process.env.GAS_LIMIT)
: undefined
},
PRIVATE_KEY: mnemonicToPrivateKey(
process.env.MNEMONIC,
process.env.PRIV_INDEX
),
PRIVATE_KEY: getPrivateKey(process.env.MNEMONIC, process.env.PRIV_INDEX),

// service contract property name
VARIABLES: 'variables',
Expand Down
4 changes: 2 additions & 2 deletions packages/vvisp/template/script.mustache
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const path = require('path');
const { getWeb3, mnemonicToPrivateKey, sendTx } = require('@haechi-labs/vvisp-utils');
const { getWeb3, getPrivateKey, sendTx } = require('@haechi-labs/vvisp-utils');
const web3 = getWeb3();
const fs = require('fs');

const privateKey = mnemonicToPrivateKey(process.env.MNEMONIC, process.env.PRIV_INDEX);
const privateKey = getPrivateKey(process.env.MNEMONIC, process.env.PRIV_INDEX);

const abi = fs.readFileSync(path.join(__dirname, '../abi/', '{{name}}.json'), {'encoding': 'utf8'});

Expand Down
2 changes: 1 addition & 1 deletion packages/vvisp/test/bin/error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ describe('# error test', function() {
it('should throw error when MNEMONIC is not set', function() {
delete process.env.MNEMONIC;
expect(() => checkEnv()).to.throw(
'You should set MNEMONIC in .env file'
'You should set MNEMONIC or PRIVATE_KEY in .env file'
);
});
});
Expand Down
11 changes: 2 additions & 9 deletions packages/vvisp/test/dummy/testContractApis/back/js/HaechiV1.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
const path = require('path');
const {
getWeb3,
mnemonicToPrivateKey,
sendTx
} = require('@haechi-labs/vvisp-utils');
const { getWeb3, getPrivateKey, sendTx } = require('@haechi-labs/vvisp-utils');
const web3 = getWeb3();
const fs = require('fs');

const privateKey = mnemonicToPrivateKey(
process.env.MNEMONIC,
process.env.PRIV_INDEX
);
const privateKey = getPrivateKey(process.env.MNEMONIC, process.env.PRIV_INDEX);

const abi = fs.readFileSync(path.join(__dirname, '../abi/', 'HaechiV1.json'), {
encoding: 'utf8'
Expand Down
4 changes: 2 additions & 2 deletions packages/vvisp/test/scripts/abi-to-script/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const {
compileAndDeploy,
forIn,
getWeb3,
mnemonicToPrivateKey,
getPrivateKey,
privateKeyToAddress
} = require('@haechi-labs/vvisp-utils');
const web3 = getWeb3();
Expand All @@ -23,7 +23,7 @@ const CONTRACT_PATH2 = path.join(`./contracts/test/${CONTRACT2}.sol`);
const FILES = [CONTRACT_PATH1, CONTRACT_PATH2];
const ROOT = path.join('./contractApis');

const PRIV_KEY = mnemonicToPrivateKey(process.env.MNEMONIC);
const PRIV_KEY = getPrivateKey(process.env.MNEMONIC);
const SENDER = privateKeyToAddress(PRIV_KEY);

describe('# abi-to-script process test', function() {
Expand Down
13 changes: 12 additions & 1 deletion packages/vvisp/test/scripts/deploy-contract.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const deployContract = require('../../scripts/deploy-contract');
const path = require('path');

require('dotenv').config();
describe('# deploy contract process test', function() {
this.timeout(50000);
const CONTRACT_PATH = path.join('./contracts', 'libs', 'Ownable.sol');
Expand All @@ -21,6 +21,17 @@ describe('# deploy contract process test', function() {
.fulfilled;
});

it('should deploy one contract through private key', async () => {
process.env.PRIVATE_KEY =
'9741fa712a6912b862c9043f8752ffae513cb01895985998c61620da5aaf2d2d';
process.env.MNEMONIC = '';
await deployContract(CONTRACT_PATH, [], { silent: true }).should.be
.fulfilled;
process.env.PRIVATE_KEY = '';
process.env.MNEMONIC =
'piano garage flag neglect spare title drill basic strong aware enforce fury';
});

it('should deploy two contract', async () => {
await deployContract(CONTRACT_PATH, [], { silent: true }).should.be
.fulfilled;
Expand Down
Loading

0 comments on commit b9c3774

Please sign in to comment.