Skip to content

Commit

Permalink
Merge pull request #248 from thanhson1085/master
Browse files Browse the repository at this point in the history
update hdpath
  • Loading branch information
thanhson1085 authored Oct 19, 2018
2 parents 95e20cc + f6e2db4 commit ac05a82
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 116 deletions.
6 changes: 2 additions & 4 deletions apis/candidates.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const express = require('express')
const router = express.Router()
const db = require('../models/mongodb')
const { Validator } = require('../models/blockchain/validator')
const HDWalletProvider = require('truffle-hdwallet-provider')
const { HDWalletProvider } = require('../helpers')
const PrivateKeyProvider = require('truffle-privatekey-provider')
const config = require('config')

Expand Down Expand Up @@ -31,9 +31,7 @@ router.get('/', async function (req, res, next) {

let map = candidates.map(async c => {
let bs = await db.BlockSigner.findOne({
'signers.signer': {
$in: signers
}
'signers.signer': c.candidate
}).sort({ _id: 'desc' })
c.latestSignedBlock = (bs || {}).blockNumber || 0

Expand Down
3 changes: 2 additions & 1 deletion app/components/Setting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ import {
required
} from 'vuelidate/lib/validators'
import localhostUrl from '../../validators/localhostUrl.js'
const HDWalletProvider = require('truffle-hdwallet-provider')
const { HDWalletProvider } = require('../../helpers')
const PrivateKeyProvider = require('truffle-privatekey-provider')
export default {
name: 'App',
Expand Down Expand Up @@ -254,6 +254,7 @@ export default {
}
self.wh.push(it)
})
self.isReady = true
} catch (e) {
console.log(e)
}
Expand Down
13 changes: 13 additions & 0 deletions app/components/candidates/Apply.vue
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ export default {
let account = await self.getAccount()
self.account = account
} catch (e) {
self.$toasted.show(`You need login your account before voting`,
{
type : 'default',
duration: 5000,
action : [
{
text : 'Login',
onClick : (e, toastObject) => {
self.$router.push({ path: '/setting' })
}
}
]
})
console.log(e)
}
},
Expand Down
6 changes: 3 additions & 3 deletions app/components/candidates/List.vue
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ export default {
{
key: 'address',
label: 'Address',
sortable: true
sortable: false
},
{
key: 'name',
label: 'Name',
sortable: true
sortable: false
},
{
key: 'cap',
Expand All @@ -162,7 +162,7 @@ export default {
{
key: 'latestSignedBlock',
label: 'Latest Signed Block',
sortable: false
sortable: true
},
{
key: 'status',
Expand Down
15 changes: 14 additions & 1 deletion app/components/voters/Voting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<li class="tomo-list__item">
<i class="tm-tomo tomo-list__icon" />
<p class="tomo-list__text">
<span><router-link :to="`/voter/${voter}`">{{ voter }}</router-link></span>
<span><router-link :to="`/voter/${voter}`">{{ voter || 'Unknown' }}</router-link></span>
<span>Voter</span>
</p>
</li>
Expand Down Expand Up @@ -111,6 +111,19 @@ export default {
let account = await self.getAccount()
self.voter = account
} catch (e) {
self.$toasted.show(`You need login your account before voting`,
{
type : 'default',
duration: 5000,
action : [
{
text : 'Login',
onClick : (e, toastObject) => {
self.$router.push({ path: '/setting' })
}
}
]
})
console.log(e)
}
},
Expand Down
88 changes: 88 additions & 0 deletions helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
var bip39 = require('bip39')
var hdkey = require('ethereumjs-wallet/hdkey')
var ProviderEngine = require('web3-provider-engine')
var FiltersSubprovider = require('web3-provider-engine/subproviders/filters.js')
var NonceSubProvider = require('web3-provider-engine/subproviders/nonce-tracker.js')
var HookedSubprovider = require('web3-provider-engine/subproviders/hooked-wallet.js')
var ProviderSubprovider = require('web3-provider-engine/subproviders/provider.js')
var Web3 = require('web3')
var Transaction = require('ethereumjs-tx')

// This line shares nonce state across multiple provider instances. Necessary
// because within truffle the wallet is repeatedly newed if it's declared in the config within a
// function, resetting nonce from tx to tx. An instance can opt out
// of this behavior by passing `shareNonce=false` to the constructor.
// See issue #65 for more
var singletonNonceSubProvider = new NonceSubProvider()

/* eslint-disable */

function HDWalletProvider (
mnemonic,
provider_url,
address_index = 0,
num_addresses = 1,
shareNonce = true
) {
this.mnemonic = mnemonic
this.hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(mnemonic))
this.wallet_hdpath = "m/44'/889'/0'/0/"
this.wallets = {}
this.addresses = []

for (let i = address_index; i < address_index + num_addresses; i++) {
var wallet = this.hdwallet.derivePath(this.wallet_hdpath + i).getWallet()
var addr = '0x' + wallet.getAddress().toString('hex')
this.addresses.push(addr)
this.wallets[addr] = wallet
}

const tmp_accounts = this.addresses
const tmp_wallets = this.wallets

this.engine = new ProviderEngine()
this.engine.addProvider(new HookedSubprovider({
getAccounts: function (cb) { cb(null, tmp_accounts) },
getPrivateKey: function (address, cb) {
if (!tmp_wallets[address]) { return cb('Account not found') } else { cb(null, tmp_wallets[address].getPrivateKey().toString('hex')) }
},
signTransaction: function (txParams, cb) {
let pkey
if (tmp_wallets[txParams.from]) { pkey = tmp_wallets[txParams.from].getPrivateKey() } else { cb('Account not found') }
var tx = new Transaction(txParams)
tx.sign(pkey)
var rawTx = '0x' + tx.serialize().toString('hex')
cb(null, rawTx)
}
}));

(!shareNonce)
? this.engine.addProvider(new NonceSubProvider())
: this.engine.addProvider(singletonNonceSubProvider)

this.engine.addProvider(new FiltersSubprovider())
this.engine.addProvider(new ProviderSubprovider(new Web3.providers.HttpProvider(provider_url)))
this.engine.start() // Required by the provider engine.
};

HDWalletProvider.prototype.sendAsync = function () {
this.engine.sendAsync.apply(this.engine, arguments)
}

HDWalletProvider.prototype.send = function () {
return this.engine.send.apply(this.engine, arguments)
}

// returns the address of the given address_index, first checking the cache
HDWalletProvider.prototype.getAddress = function (idx) {
console.log('getting addresses', this.addresses[0], idx)
if (!idx) { return this.addresses[0] } else { return this.addresses[idx] }
}

// returns the addresses cache
HDWalletProvider.prototype.getAddresses = function () {
return this.addresses
}
/* eslint-enable */

module.exports = { HDWalletProvider }
Loading

0 comments on commit ac05a82

Please sign in to comment.