2017-03-31 03:33:19 +02:00
|
|
|
const extension = require('extensionizer')
|
2019-02-12 15:04:48 +01:00
|
|
|
const {createExplorerLink: explorerLink} = require('etherscan-link')
|
2017-03-31 03:33:19 +02:00
|
|
|
|
|
|
|
class ExtensionPlatform {
|
|
|
|
|
|
|
|
//
|
|
|
|
// Public
|
|
|
|
//
|
|
|
|
reload () {
|
|
|
|
extension.runtime.reload()
|
|
|
|
}
|
|
|
|
|
|
|
|
openWindow ({ url }) {
|
|
|
|
extension.tabs.create({ url })
|
|
|
|
}
|
|
|
|
|
2018-07-24 19:31:03 +02:00
|
|
|
closeCurrentWindow () {
|
2018-07-24 02:50:06 +02:00
|
|
|
return extension.windows.getCurrent((windowDetails) => {
|
|
|
|
return extension.windows.remove(windowDetails.id)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-04-01 03:04:13 +02:00
|
|
|
getVersion () {
|
|
|
|
return extension.runtime.getManifest().version
|
|
|
|
}
|
|
|
|
|
2018-07-31 04:57:05 +02:00
|
|
|
openExtensionInBrowser (route = null, queryString = null) {
|
2018-07-27 03:11:58 +02:00
|
|
|
let extensionURL = extension.runtime.getURL('home.html')
|
2018-07-31 04:57:05 +02:00
|
|
|
|
|
|
|
if (queryString) {
|
|
|
|
extensionURL += `?${queryString}`
|
|
|
|
}
|
|
|
|
|
2018-07-27 03:11:58 +02:00
|
|
|
if (route) {
|
|
|
|
extensionURL += `#${route}`
|
|
|
|
}
|
2017-12-09 01:48:08 +01:00
|
|
|
this.openWindow({ url: extensionURL })
|
|
|
|
}
|
|
|
|
|
2017-10-04 19:55:10 +02:00
|
|
|
getPlatformInfo (cb) {
|
2017-10-06 02:13:58 +02:00
|
|
|
try {
|
2017-10-10 17:36:15 +02:00
|
|
|
extension.runtime.getPlatformInfo((platform) => {
|
|
|
|
cb(null, platform)
|
|
|
|
})
|
2017-10-06 02:13:58 +02:00
|
|
|
} catch (e) {
|
2017-10-10 17:36:15 +02:00
|
|
|
cb(e)
|
2017-10-06 02:13:58 +02:00
|
|
|
}
|
2017-10-04 18:56:18 +02:00
|
|
|
}
|
2018-07-20 13:20:40 +02:00
|
|
|
|
2018-07-27 03:11:58 +02:00
|
|
|
showTransactionNotification (txMeta) {
|
2018-12-11 03:02:26 +01:00
|
|
|
const { status, txReceipt: { status: receiptStatus } = {} } = txMeta
|
2018-07-27 03:11:58 +02:00
|
|
|
|
|
|
|
if (status === 'confirmed') {
|
2018-12-11 03:02:26 +01:00
|
|
|
// There was an on-chain failure
|
|
|
|
receiptStatus === '0x0'
|
|
|
|
? this._showFailedTransaction(txMeta, 'Transaction encountered an error.')
|
|
|
|
: this._showConfirmedTransaction(txMeta)
|
2018-07-27 03:11:58 +02:00
|
|
|
} else if (status === 'failed') {
|
|
|
|
this._showFailedTransaction(txMeta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_showConfirmedTransaction (txMeta) {
|
|
|
|
|
|
|
|
this._subscribeToNotificationClicked()
|
|
|
|
|
|
|
|
const url = explorerLink(txMeta.hash, parseInt(txMeta.metamaskNetworkId))
|
|
|
|
const nonce = parseInt(txMeta.txParams.nonce, 16)
|
|
|
|
|
|
|
|
const title = 'Confirmed transaction'
|
|
|
|
const message = `Transaction ${nonce} confirmed! View on EtherScan`
|
|
|
|
this._showNotification(title, message, url)
|
2018-07-20 13:20:40 +02:00
|
|
|
}
|
|
|
|
|
2018-12-11 03:02:26 +01:00
|
|
|
_showFailedTransaction (txMeta, errorMessage) {
|
2018-07-27 03:11:58 +02:00
|
|
|
|
|
|
|
const nonce = parseInt(txMeta.txParams.nonce, 16)
|
|
|
|
const title = 'Failed transaction'
|
2018-12-11 03:02:26 +01:00
|
|
|
const message = `Transaction ${nonce} failed! ${errorMessage || txMeta.err.message}`
|
2018-07-27 03:11:58 +02:00
|
|
|
this._showNotification(title, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
_showNotification (title, message, url) {
|
|
|
|
extension.notifications.create(
|
|
|
|
url,
|
|
|
|
{
|
|
|
|
'type': 'basic',
|
|
|
|
'title': title,
|
|
|
|
'iconUrl': extension.extension.getURL('../../images/icon-64.png'),
|
|
|
|
'message': message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
_subscribeToNotificationClicked () {
|
|
|
|
if (!extension.notifications.onClicked.hasListener(this._viewOnEtherScan)) {
|
|
|
|
extension.notifications.onClicked.addListener(this._viewOnEtherScan)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_viewOnEtherScan (txId) {
|
|
|
|
if (txId.startsWith('http://')) {
|
2019-01-03 01:03:35 +01:00
|
|
|
extension.tabs.create({ url: txId })
|
2018-07-27 03:11:58 +02:00
|
|
|
}
|
|
|
|
}
|
2017-03-31 03:33:19 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 03:15:16 +02:00
|
|
|
module.exports = ExtensionPlatform
|