Skip to content

Commit

Permalink
feat: add a wallet prop to the eth connection allowing for wallet conf (
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosantangelo authored and abarmat committed Mar 26, 2018
1 parent ef38658 commit 0c98378
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"./dist/index.js": "./dist/browser.js"
},
"scripts": {
"docs": "npx jsdoc -c conf.json -r src/**/*.js",
"docs": "npx jsdoc -c conf.json -r src/**/*",
"lint": "npx eslint .",
"lint:fix": "npx eslint --fix .",
"init": "mkdir -p dist",
Expand Down
1 change: 1 addition & 0 deletions src/ethereum/abi/Abi.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const abi = {
}
}

/** Abi decorator, extends abi objects with useful methods */
export const Abi = {
new(abiObject) {
const newAbi = Object.create(abiObject)
Expand Down
40 changes: 23 additions & 17 deletions src/ethereum/eth.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const eth = {
* Connect to web3
* @param {object} [options] - Options for the ETH connection
* @param {array<Contract>} [options.contracts=[]] - An array of objects defining contracts or Contract subclasses to use. Check {@link eth#setContracts}
* @param {array<Wallet>} [options.wallets=[Ledger, Node]] - An array of Wallet classes. It'll use the first successful connection. Check {@link wallets}
* @param {string} [options.defaultAccount=web3.eth.accounts[0]] - Override the default account address
* @param {string} [options.providerUrl] - URL for a provider forwarded to {@link Wallet#getWeb3Provider}
* @param {string} [options.derivationPath] - Path to derive the hardware wallet in. Defaults to each wallets most common value
Expand All @@ -38,45 +39,50 @@ export const eth = {

const {
contracts = [],
wallets = [LedgerWallet, NodeWallet],
defaultAccount,
providerUrl,
derivationPath
} = options

try {
this.wallet = await this.connectWallet(
this.wallet = await this.connectWallet(wallets, {
defaultAccount,
providerUrl,
derivationPath
)
})
web3 = this.wallet.getWeb3()

this.setContracts(contracts)

return true
} catch (error) {
log.info(`Error trying to connect Ethereum wallet: ${error.message}`)
log.info(`Error trying to connect Ethereum wallet:\n${error.message}`)
return false
}
},

async connectWallet(defaultAccount, providerUrl = '', derivationPath = null) {
let wallet
const networks = this.getNetworks()

try {
const network =
networks.find(network => providerUrl.includes(network.name)) ||
networks[0]
async connectWallet(wallets, options = {}) {
const { defaultAccount, providerUrl = '', derivationPath = null } = options

wallet = new LedgerWallet(defaultAccount, derivationPath)
await wallet.connect(providerUrl, network.id)
} catch (error) {
wallet = new NodeWallet(defaultAccount)
await wallet.connect(providerUrl)
const networks = this.getNetworks()
const network =
networks.find(network => providerUrl.includes(network.name)) ||
networks[0]

const errors = []

for (const Wallet of wallets) {
try {
const wallet = new Wallet(defaultAccount, derivationPath)
await wallet.connect(providerUrl, network.id)
return wallet
} catch (error) {
errors.push(error.message)
}
}

return wallet
throw new Error(errors.join('\n'))
},

isConnected() {
Expand Down
4 changes: 3 additions & 1 deletion src/ethereum/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as wallets from './wallets'

export { eth } from './eth'
export { Contract } from './Contract'
export { Abi } from './abi'
export { Event } from './Event'
export { SignedMessage } from './SignedMessage'
export { txUtils } from './txUtils'
export { WALLET_TYPES } from './wallets'
export { wallets }
4 changes: 4 additions & 0 deletions src/ethereum/wallets/LedgerWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import LedgerWalletSubprovider from 'ledger-wallet-provider'
import { Wallet } from './Wallet'
import { sleep } from '../../utils'

/**
* Ledger Wallet class
* @extends Wallet
*/
export class LedgerWallet extends Wallet {
// eslint-disable-next-line
static derivationPath = "44'/60'/0'/0"
Expand Down
4 changes: 4 additions & 0 deletions src/ethereum/wallets/NodeWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import Web3 from 'web3'
import { Wallet } from './Wallet'
import { Contract } from '../Contract'

/**
* Node Wallet class
* @extends Wallet
*/
export class NodeWallet extends Wallet {
static type = 'node'

Expand Down
14 changes: 12 additions & 2 deletions src/ethereum/wallets/Wallet.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Interface
/**
* Wallet class
*/
export class Wallet {
static type = ''

Expand Down Expand Up @@ -27,6 +29,10 @@ export class Wallet {
this.account = account
}

/**
* Connect to the Ethereum Wallet
* @abstract
*/
async connect() {
throw new Error('Not implemented. Check wallet support')
}
Expand All @@ -39,6 +45,7 @@ export class Wallet {
/**
* Gets the appropiate Web3 provider for the given environment.
* Check each implementation for in detail information
* @abstract
* @param {string} [providerURL] - URL for a provider.
* @return {object} The web3 provider
*/
Expand All @@ -48,6 +55,7 @@ export class Wallet {

/**
* Return available accounts for the current wallet
* @abstract
* @return {Promise<array<string>>} accounts
*/
async getAccounts() {
Expand All @@ -58,7 +66,7 @@ export class Wallet {
* Creates a new contract instance with all its methods and events defined in its json interface object (abi).
* @param {object} abi - Application Binary Interface.
* @param {string} address - Contract address
* @return {object} instance
* @return {object} contract instance
*/
createContractInstance(abi, address) {
return this.web3.eth.contract(abi).at(address)
Expand All @@ -75,6 +83,7 @@ export class Wallet {

/**
* Signs data from the default account
* @abstract
* @param {string} message - Message to sign, ussually in Hex
* @return {Promise<string>} signature
*/
Expand All @@ -84,6 +93,7 @@ export class Wallet {

/**
* Recovers the account that signed the data
* @abstract
* @param {string} message - Data that was signed
* @param {string} signature
* @return {Promise<string>} account
Expand Down

0 comments on commit 0c98378

Please sign in to comment.