A Metamask fork with Infura removed and default networks editable
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ciphermask/app/scripts/lib/config-manager.js

387 lines
9.8 KiB

const Migrator = require('pojo-migrator')
const MetamaskConfig = require('../config.js')
9 years ago
const migrations = require('./migrations')
const rp = require('request-promise')
9 years ago
const TESTNET_RPC = MetamaskConfig.network.testnet
const MAINNET_RPC = MetamaskConfig.network.mainnet
const txLimit = 40
/* The config-manager is a convenience object
* wrapping a pojo-migrator.
*
* It exists mostly to allow the creation of
* convenience methods to access and persist
* particular portions of the state.
*/
module.exports = ConfigManager
function ConfigManager (opts) {
this.txLimit = txLimit
// ConfigManager is observable and will emit updates
this._subs = []
/* The migrator exported on the config-manager
* has two methods the user should be concerned with:
*
* getData(), which returns the app-consumable data object
* saveData(), which persists the app-consumable data object.
*/
8 years ago
this.migrator = new Migrator({
// Migrations must start at version 1 or later.
// They are objects with a `version` number
// and a `migrate` function.
//
// The `migrate` function receives the previous
// config data format, and returns the new one.
migrations: migrations,
// How to load initial config.
// Includes step on migrating pre-pojo-migrator data.
loadData: opts.loadData,
// How to persist migrated config.
setData: opts.setData,
})
}
8 years ago
ConfigManager.prototype.setConfig = function (config) {
var data = this.migrator.getData()
data.config = config
this.setData(data)
this._emitUpdates(config)
}
8 years ago
ConfigManager.prototype.getConfig = function () {
var data = this.migrator.getData()
if ('config' in data) {
return data.config
} else {
return {
provider: {
type: 'testnet',
8 years ago
},
}
}
}
8 years ago
ConfigManager.prototype.setRpcTarget = function (rpcUrl) {
var config = this.getConfig()
config.provider = {
type: 'rpc',
rpcTarget: rpcUrl,
}
this.setConfig(config)
}
8 years ago
ConfigManager.prototype.setProviderType = function (type) {
var config = this.getConfig()
config.provider = {
type: type,
}
this.setConfig(config)
}
8 years ago
ConfigManager.prototype.useEtherscanProvider = function () {
var config = this.getConfig()
config.provider = {
type: 'etherscan',
}
this.setConfig(config)
}
8 years ago
ConfigManager.prototype.getProvider = function () {
var config = this.getConfig()
return config.provider
}
8 years ago
ConfigManager.prototype.setData = function (data) {
this.migrator.saveData(data)
}
8 years ago
ConfigManager.prototype.getData = function () {
return this.migrator.getData()
}
8 years ago
ConfigManager.prototype.setWallet = function (wallet) {
var data = this.migrator.getData()
data.wallet = wallet
this.setData(data)
}
8 years ago
ConfigManager.prototype.getSelectedAccount = function () {
var config = this.getConfig()
return config.selectedAccount
}
8 years ago
ConfigManager.prototype.setSelectedAccount = function (address) {
var config = this.getConfig()
config.selectedAccount = address
this.setConfig(config)
}
8 years ago
ConfigManager.prototype.getWallet = function () {
return this.migrator.getData().wallet
}
// Takes a boolean
8 years ago
ConfigManager.prototype.setShowSeedWords = function (should) {
var data = this.migrator.getData()
data.showSeedWords = should
this.setData(data)
}
8 years ago
ConfigManager.prototype.getShouldShowSeedWords = function () {
var data = this.migrator.getData()
return data.showSeedWords
}
8 years ago
ConfigManager.prototype.getCurrentRpcAddress = function () {
var provider = this.getProvider()
if (!provider) return null
8 years ago
switch (provider.type) {
8 years ago
case 'mainnet':
return MAINNET_RPC
8 years ago
case 'testnet':
return TESTNET_RPC
8 years ago
default:
return provider && provider.rpcTarget ? provider.rpcTarget : TESTNET_RPC
}
}
8 years ago
ConfigManager.prototype.setData = function (data) {
this.migrator.saveData(data)
}
//
// Tx
//
8 years ago
ConfigManager.prototype.getTxList = function () {
var data = this.migrator.getData()
if (data.transactions !== undefined) {
return data.transactions
} else {
return []
}
}
8 years ago
ConfigManager.prototype.unconfirmedTxs = function () {
var transactions = this.getTxList()
return transactions.filter(tx => tx.status === 'unconfirmed')
.reduce((result, tx) => { result[tx.id] = tx; return result }, {})
}
8 years ago
ConfigManager.prototype._saveTxList = function (txList) {
var data = this.migrator.getData()
data.transactions = txList
this.setData(data)
}
8 years ago
ConfigManager.prototype.addTx = function (tx) {
var transactions = this.getTxList()
while (transactions.length > this.txLimit - 1) {
transactions.shift()
}
transactions.push(tx)
this._saveTxList(transactions)
}
8 years ago
ConfigManager.prototype.getTx = function (txId) {
var transactions = this.getTxList()
var matching = transactions.filter(tx => tx.id === txId)
return matching.length > 0 ? matching[0] : null
}
8 years ago
ConfigManager.prototype.confirmTx = function (txId) {
this._setTxStatus(txId, 'confirmed')
}
8 years ago
ConfigManager.prototype.rejectTx = function (txId) {
this._setTxStatus(txId, 'rejected')
}
8 years ago
ConfigManager.prototype._setTxStatus = function (txId, status) {
var tx = this.getTx(txId)
tx.status = status
this.updateTx(tx)
}
8 years ago
ConfigManager.prototype.updateTx = function (tx) {
var transactions = this.getTxList()
var found, index
transactions.forEach((otherTx, i) => {
if (otherTx.id === tx.id) {
found = true
index = i
}
})
if (found) {
transactions[index] = tx
}
this._saveTxList(transactions)
}
// wallet nickname methods
8 years ago
ConfigManager.prototype.getWalletNicknames = function () {
var data = this.getData()
8 years ago
const nicknames = ('walletNicknames' in data) ? data.walletNicknames : {}
return nicknames
}
8 years ago
ConfigManager.prototype.nicknameForWallet = function (account) {
const nicknames = this.getWalletNicknames()
return nicknames[account]
}
8 years ago
ConfigManager.prototype.setNicknameForWallet = function (account, nickname) {
const nicknames = this.getWalletNicknames()
nicknames[account] = nickname
var data = this.getData()
data.walletNicknames = nicknames
this.setData(data)
}
// observable
8 years ago
ConfigManager.prototype.subscribe = function (fn) {
this._subs.push(fn)
var unsubscribe = this.unsubscribe.bind(this, fn)
return unsubscribe
}
8 years ago
ConfigManager.prototype.unsubscribe = function (fn) {
var index = this._subs.indexOf(fn)
if (index !== -1) this._subs.splice(index, 1)
}
8 years ago
ConfigManager.prototype._emitUpdates = function (state) {
this._subs.forEach(function (handler) {
handler(state)
})
}
8 years ago
ConfigManager.prototype.setConfirmed = function (confirmed) {
var data = this.getData()
data.isConfirmed = confirmed
this.setData(data)
}
8 years ago
ConfigManager.prototype.getConfirmed = function () {
var data = this.getData()
return ('isConfirmed' in data) && data.isConfirmed
}
ConfigManager.prototype.setTOSHash = function (hash) {
var data = this.getData()
data.TOSHash = hash
this.setData(data)
}
ConfigManager.prototype.getTOSHash = function () {
var data = this.getData()
return ('TOSHash' in data) && data.TOSHash
}
ConfigManager.prototype.setCurrentFiat = function (currency) {
var data = this.getData()
data.fiatCurrency = currency
this.setData(data)
}
ConfigManager.prototype.getCurrentFiat = function () {
var data = this.getData()
return ('fiatCurrency' in data) && data.fiatCurrency
}
ConfigManager.prototype.updateConversionRate = function () {
var data = this.getData()
return rp(`https://www.cryptonator.com/api/ticker/eth-${data.fiatCurrency}`)
.then((response) => {
const parsedResponse = JSON.parse(response)
this.setConversionPrice(parsedResponse.ticker.price)
this.setConversionDate(parsedResponse.timestamp)
}).catch((err) => {
console.error('Error in conversion.', err)
this.setConversionPrice(0)
this.setConversionDate('N/A')
})
}
ConfigManager.prototype.setConversionPrice = function (price) {
var data = this.getData()
8 years ago
data.conversionRate = Number(price)
this.setData(data)
}
ConfigManager.prototype.setConversionDate = function (datestring) {
var data = this.getData()
data.conversionDate = datestring
this.setData(data)
}
ConfigManager.prototype.getConversionRate = function () {
var data = this.getData()
return (('conversionRate' in data) && data.conversionRate) || 0
}
ConfigManager.prototype.getConversionDate = function () {
var data = this.getData()
return (('conversionDate' in data) && data.conversionDate) || 'N/A'
}
ConfigManager.prototype.setShouldntShowWarning = function () {
var data = this.getData()
if (data.isEthConfirmed) {
data.isEthConfirmed = !data.isEthConfirmed
} else {
data.isEthConfirmed = true
}
this.setData(data)
}
ConfigManager.prototype.getShouldntShowWarning = function () {
var data = this.getData()
return ('isEthConfirmed' in data) && data.isEthConfirmed
}
ConfigManager.prototype.getShapeShiftTxList = function () {
var data = this.getData()
var shapeShiftTxList = data.shapeShiftTxList ? data.shapeShiftTxList : []
shapeShiftTxList.forEach((tx) => {
if (tx.response.status !== 'complete') {
var requestListner = function (request) {
tx.response = JSON.parse(this.responseText)
if (tx.response.status === 'complete') {
tx.time = new Date().getTime()
}
}
var shapShiftReq = new XMLHttpRequest()
shapShiftReq.addEventListener('load', requestListner)
shapShiftReq.open('GET', `https://shapeshift.io/txStat/${tx.depositAddress}`, true)
shapShiftReq.send()
}
})
this.setData(data)
return shapeShiftTxList
}
ConfigManager.prototype.createShapeShiftTx = function (depositAddress, depositType) {
var data = this.getData()
var shapeShiftTx = {depositAddress, depositType, key: 'shapeshift', time: new Date().getTime(), response: {}}
if (!data.shapeShiftTxList) {
data.shapeShiftTxList = [shapeShiftTx]
} else {
data.shapeShiftTxList.push(shapeShiftTx)
}
this.setData(data)
}