Skip to content

Commit

Permalink
Merge 97dbeb9 into 33e8283
Browse files Browse the repository at this point in the history
  • Loading branch information
thanhnguyennguyen committed Nov 29, 2018
2 parents 33e8283 + 97dbeb9 commit cf8ef36
Show file tree
Hide file tree
Showing 17 changed files with 663 additions and 83 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@ This is Governance Dapp for TomoChain. Full-Node can apply to become a candidate
```
cp config/default.json config/local.json
```
Update `local.json` file to support your environment
- Update mnemonic
- Update mongodb configuration:
- For docker:
` "db": {
"uri": "mongodb://mongodb:27017/governance"
- Update `local.json` file to support your environment
- Update mnemonic
- Update mongodb configuration:
- For docker:
` "db": {
"uri": "mongodb://mongodb:27017/governance"
},
`
- For localhost:
`
"db": {
"uri": "mongodb://localhost:27017/governance"
},
`
- For localhost:
`
"db": {
"uri": "mongodb://localhost:27017/governance"
},
`


## Install
```
Expand All @@ -40,6 +39,8 @@ cp abis/*json build/contracts/
```
Note: before deploying to tomochain testnet, make sure you have TOMO in the wallet. If not, get free at [https://faucet.tomochain.com](https://faucet.testnet.tomochain.com)

## Enable https
``` npm run dev-https```
## Run
- Start mongodb
- Start TomoMaster
Expand Down
5 changes: 3 additions & 2 deletions app/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,19 @@ export default {
this.$router.push({ path: `/candidate/${s}` })
},
async checkNetworkAndLogin () {
let self = this
setTimeout(async () => {
let account
try {
const contract = await this.TomoValidator.deployed()
const contract = await self.getTomoValidatorInstance()
if (store.get('address')) {
account = store.get('address').toLowerCase()
} else {
account = this.$store.state.walletLoggedIn
? this.$store.state.walletLoggedIn : await self.getAccount()
}
if (account && contract) {
this.isTomonet = true
self.isTomonet = true
}
} catch (error) {}
}, 0)
Expand Down
104 changes: 96 additions & 8 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ import VueClipboards from 'vue-clipboards'
import Vuex from 'vuex'
import HDWalletProvider from 'truffle-hdwallet-provider'
import localStorage from 'store'
// Libusb is included as a submodule.
// On Linux, you'll need libudev to build libusb.
// On Ubuntu/Debian: sudo apt-get install build-essential libudev-dev
// import Transport from '@ledgerhq/hw-transport-node-hid'

import Transport from '@ledgerhq/hw-transport-u2f' // for browser
import Eth from '@ledgerhq/hw-app-eth'
import Transaction from 'ethereumjs-tx'

Vue.use(BootstrapVue)
Vue.use(VueClipboards)
Expand All @@ -50,6 +58,7 @@ Vue.use(HighchartsVue)

Vue.prototype.TomoValidator = contract(TomoValidatorArtifacts)
Vue.prototype.isElectron = !!(window && window.process && window.process.type)
Vue.prototype.hdDerivationPath = "44'/889'/0'/0/0"

Vue.prototype.setupProvider = function (provider, wjs) {
const self = this
Expand All @@ -59,16 +68,15 @@ Vue.prototype.setupProvider = function (provider, wjs) {
Vue.prototype.TomoValidator.setProvider(wjs.currentProvider)
Vue.prototype.getAccount = function () {
var p = new Promise(function (resolve, reject) {
wjs.eth.getAccounts(function (err, accs) {
wjs.eth.getAccounts(async function (err, accs) {
if (err != null) {
console.log('There was an error fetching your accounts.')
return reject(err)
}
if (provider === 'tomowallet') {
switch (provider) {
case 'tomowallet':
return resolve(self.$store.state.walletLoggedIn)
}

if (provider === 'rpc') {
case 'custom':
if (wjs.currentProvider.address) {
return resolve(wjs.currentProvider.address)
}
Expand All @@ -77,11 +85,27 @@ Vue.prototype.setupProvider = function (provider, wjs) {
return resolve(wjs.currentProvider.addresses[0])
}
return resolve('')
}
if (provider === 'wallet') {
case 'ledger':
let u2fSupported = await Transport.isSupported()
if (!u2fSupported) {
return reject(new Error(`U2F not supported in this browser.
Please try using Google Chrome with a secure (SSL / HTTPS) connection!`))
}
try {
let transport = await new Transport()
Vue.prototype.appEth = await new Eth(transport)
let result = await Vue.prototype.appEth.getAddress(
Vue.prototype.hdDerivationPath
)
return resolve(result.address)
} catch (error) {
return reject(error.message)
}
case 'wallet':
return resolve(this.$store.state.walletLoggedIn)
default:
break
}

if (accs.length === 0) {
console.log(`Couldn't get any accounts! Make sure
your Ethereum client is configured correctly.`)
Expand Down Expand Up @@ -248,6 +272,10 @@ Vue.prototype.detectNetwork = async function (provider) {
'',
chainConfig.rpc, 0, 1, true, "m/44'/889'/0'/0/"))
break
case 'ledger':
// wjs = new Web3(new Web3.providers.WebsocketProvider(chainConfig.ws))
wjs = new Web3(new Web3.providers.HttpProvider(chainConfig.rpc))
break
default:
break
}
Expand All @@ -257,6 +285,66 @@ Vue.prototype.detectNetwork = async function (provider) {
}
}

/**
* @return TomoValidator contract instance
*/
Vue.prototype.getTomoValidatorInstance = async function () {
// workaround for web3 version 1.0.0
// @link https://github.com/trufflesuite/truffle-contract/issues/57#issuecomment-331300494
if (typeof Vue.prototype.TomoValidator.currentProvider.sendAsync !== 'function') {
Vue.prototype.TomoValidator.currentProvider.sendAsync = function () {
return Vue.prototype.TomoValidator.currentProvider.send.apply(
Vue.prototype.TomoValidator.currentProvider,
arguments
)
}
}
let instance = await Vue.prototype.TomoValidator.deployed()
return instance
}

/**
* @param object txParams
* @return object signature {r, s, v}
*/
Vue.prototype.signTransaction = async function (txParams) {
let config = await getConfig()
let chainConfig = config.blockchain
let rawTx = new Transaction(txParams)
// for custom network (not ethereum mainnet, we need chainId)
rawTx.v = Buffer.from([chainConfig.networkId])
let serializedRawTx = rawTx.serialize().toString('hex')
let signature = await Vue.prototype.appEth.signTransaction(
Vue.prototype.hdDerivationPath,
serializedRawTx
)
return signature
}

/**
* @param object txParams
* @param object signature {r,s,v}
* @return transactionReceipt
*/
Vue.prototype.sendSignedTransaction = async function (txParams, signature) {
// "hexify" the keys
Object.keys(signature).map((key, _) => {
signature[key] = '0x' + signature[key]
})
let txObj = Object.assign({}, txParams, signature)
let tx = new Transaction(txObj)
let serializedTx = '0x' + tx.serialize().toString('hex')
// web3 v0.2, method name is sendRawTransaction
// You are using web3 v1.0. The method was renamed to sendSignedTransaction.
let rs = await Vue.prototype.web3.eth.sendSignedTransaction(
serializedTx
)
if (!rs.tx && rs.transactionHash) {
rs.tx = rs.transactionHash
}
return rs
}

new Vue({ // eslint-disable-line no-new
el: '#app',
store,
Expand Down
29 changes: 21 additions & 8 deletions app/components/Setting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
value="tomowallet"
selected>TomoWallet</option>
<option value="custom">PrivateKey/MNEMONIC</option>
<option value="ledger">Ledger Wallet</option>
<option
v-if="!isElectron"
value="metamask">Metamask</option>
Expand Down Expand Up @@ -99,7 +100,6 @@
target="_blank">Metamask Extension</a>
then connect it to Tomochain Mainnet or Testnet.</p>
</div>

<div class="buttons text-right">
<b-button
v-if="provider !== 'tomowallet'"
Expand Down Expand Up @@ -273,7 +273,7 @@ export default {
}
if (self.web3) {
try {
contract = await self.TomoValidator.deployed()
contract = await self.getTomoValidatorInstance()
} catch (error) {
throw Error('Make sure you choose correct tomochain network.')
}
Expand Down Expand Up @@ -364,7 +364,7 @@ export default {
}
},
validate: function () {
if (this.provider === 'metamask') {
if (['metamask', 'ledger'].includes(this.provider)) {
this.save()
}
Expand All @@ -384,12 +384,26 @@ export default {
var wjs = false
self.loading = true
try {
if (self.provider === 'metamask') {
switch (self.provider) {
case 'metamask':
if (window.web3) {
var p = window.web3.currentProvider
wjs = new Web3(p)
}
} else {
break
case 'ledger':
// Object - HttpProvider
wjs = new Web3(new Web3.providers.HttpProvider(self.networks.rpc))
// Object - IpcProvider: The IPC provider is used node.js dapps when running a local node
// import net from 'net'
// wjs = new Web3(new Web3.providers.IpcProvider('~/.ethereum/geth.ipc', net))
// Object - WebsocketProvider: The Websocket provider is the standard for usage in legacy browsers.
// wjs = await ws.connect(self.networks.wss)
// wjs = new Web3(new Web3.providers.WebsocketProvider(self.chainConfig.ws))
// web3 version 0.2 haven't supported WebsocketProvider yet. (for web@1.0 only)
break
default:
const walletProvider =
(self.mnemonic.indexOf(' ') >= 0)
? new HDWalletProvider(
Expand All @@ -398,10 +412,9 @@ export default {
: new PrivateKeyProvider(self.mnemonic, self.chainConfig.rpc)
wjs = new Web3(walletProvider)
break
}
await self.setupProvider(this.provider, wjs)
await self.setupAccount()
self.loading = false
self.$store.state.walletLoggedIn = null
Expand Down Expand Up @@ -487,7 +500,7 @@ export default {
await self.setupProvider(this.provider, web3)
try {
contract = await self.TomoValidator.deployed()
contract = await self.getTomoValidatorInstance()
} catch (error) {
if (self.interval) {
clearInterval(self.interval)
Expand Down
23 changes: 20 additions & 3 deletions app/components/candidates/Apply.vue
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,30 @@ export default {
self.coinbaseError = true
return false
}
let contract = await self.TomoValidator.deployed()
let rs = await contract.propose(coinbase, {
let contract = await self.getTomoValidatorInstance()
let txParams = {
from : self.account,
value: parseFloat(value) * 10 ** 18,
gasPrice: 2500,
gas: 2000000
})
}
let rs
if (self.NetworkProvider === 'ledger') {
let nonce = await self.web3.eth.getTransactionCount(self.account)
let dataTx = contract.propose.request(coinbase).params[0]
Object.assign(
dataTx,
dataTx,
txParams,
{
nonce: self.web3.utils.toHex(nonce)
}
)
let signature = await self.signTransaction(dataTx)
rs = await self.sendSignedTransaction(dataTx, signature)
} else {
rs = await contract.propose(coinbase, txParams)
}
let toastMessage = rs.tx ? 'You have successfully applied!'
: 'An error occurred while applying, please try again'
self.$toasted.show(toastMessage)
Expand Down
4 changes: 2 additions & 2 deletions app/components/candidates/List.vue
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export default {
try {
if (self.isReady) {
let contract = await self.TomoValidator.deployed()
let contract = await self.getTomoValidatorInstance()
if (store.get('address')) {
account = store.get('address').toLowerCase()
} else {
Expand Down Expand Up @@ -244,7 +244,7 @@ export default {
mounted () { },
methods: {
watch: async function () {
let contract = await self.TomoValidator.deployed()
let contract = await self.getTomoValidatorInstance()
const allEvents = contract.allEvents({
fromBlock: self.blockNumber,
toBlock: 'latest'
Expand Down
24 changes: 20 additions & 4 deletions app/components/candidates/Resign.vue
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,30 @@ export default {
self.loading = true
let account = (await self.getAccount() || '').toLowerCase()
let contract = await self.TomoValidator.deployed()
let contract = await self.getTomoValidatorInstance()
let coinbase = self.coinbase
let rs = await contract.resign(coinbase, {
let txParams = {
from: account,
gasPrice: 2500,
gas: 2000000
})
}
let rs
if (self.NetworkProvider === 'ledger') {
let nonce = await self.web3.eth.getTransactionCount(account)
let dataTx = contract.resign.request(coinbase).params[0]
Object.assign(
dataTx,
dataTx,
txParams,
{
nonce: self.web3.utils.toHex(nonce)
}
)
let signature = await self.signTransaction(dataTx)
rs = await self.sendSignedTransaction(dataTx, signature)
} else {
rs = await contract.resign(coinbase, txParams)
}
let toastMessage = rs.tx ? 'You have successfully resigned!'
: 'An error occurred while retiring, please try again'
self.$toasted.show(toastMessage)
Expand Down
Loading

0 comments on commit cf8ef36

Please sign in to comment.