1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/app/scripts/lib/notifications.js

163 lines
4.5 KiB
JavaScript
Raw Normal View History

const createId = require('hat')
const unmountComponentAtNode = require('react-dom').unmountComponentAtNode
const findDOMNode = require('react-dom').findDOMNode
const render = require('react-dom').render
const h = require('react-hyperscript')
const uiUtils = require('../../../ui/app/util')
2016-06-23 04:28:11 +02:00
const renderPendingTx = require('../../../ui/app/components/pending-tx').prototype.renderGeneric
const MetaMaskUiCss = require('../../../ui/css')
var notificationHandlers = {}
module.exports = {
createUnlockRequestNotification: createUnlockRequestNotification,
createTxNotification: createTxNotification,
createMsgNotification: createMsgNotification,
}
2016-06-03 02:29:49 +02:00
setupListeners()
2016-06-21 22:18:32 +02:00
function setupListeners () {
2016-06-03 02:29:49 +02:00
// guard for chrome bug https://github.com/MetaMask/metamask-plugin/issues/236
if (!chrome.notifications) return console.error('Chrome notifications API missing...')
2016-06-03 02:29:49 +02:00
// notification button press
2016-06-21 22:18:32 +02:00
chrome.notifications.onButtonClicked.addListener(function (notificationId, buttonIndex) {
2016-06-03 02:29:49 +02:00
var handlers = notificationHandlers[notificationId]
if (buttonIndex === 0) {
handlers.confirm()
} else {
handlers.cancel()
}
chrome.notifications.clear(notificationId)
})
// notification teardown
2016-06-21 22:18:32 +02:00
chrome.notifications.onClosed.addListener(function (notificationId) {
2016-06-03 02:29:49 +02:00
delete notificationHandlers[notificationId]
})
}
// creation helper
2016-06-21 22:18:32 +02:00
function createUnlockRequestNotification (opts) {
// guard for chrome bug https://github.com/MetaMask/metamask-plugin/issues/236
if (!chrome.notifications) return console.error('Chrome notifications API missing...')
var message = 'An Ethereum app has requested a signature. Please unlock your account.'
var id = createId()
chrome.notifications.create(id, {
type: 'basic',
iconUrl: '/images/icon-128.png',
title: opts.title,
message: message,
})
}
2016-06-21 22:18:32 +02:00
function createTxNotification (opts) {
// guard for chrome bug https://github.com/MetaMask/metamask-plugin/issues/236
if (!chrome.notifications) return console.error('Chrome notifications API missing...')
2016-06-24 01:42:40 +02:00
renderTransactionNotificationSVG(opts, function(err, source){
2016-06-24 01:53:45 +02:00
if (err) throw err
2016-06-23 04:28:11 +02:00
var imageUrl = 'data:image/svg+xml;utf8,' + encodeURIComponent(source)
var id = createId()
chrome.notifications.create(id, {
type: 'image',
// requireInteraction: true,
iconUrl: '/images/icon-128.png',
imageUrl: imageUrl,
title: opts.title,
2016-06-24 01:42:40 +02:00
message: '',
2016-06-23 04:28:11 +02:00
buttons: [{
title: 'confirm',
}, {
title: 'cancel',
}],
})
notificationHandlers[id] = {
confirm: opts.confirm,
cancel: opts.cancel,
}
})
}
2016-06-21 22:18:32 +02:00
function createMsgNotification (opts) {
// guard for chrome bug https://github.com/MetaMask/metamask-plugin/issues/236
if (!chrome.notifications) return console.error('Chrome notifications API missing...')
var message = [
2016-06-21 22:18:32 +02:00
'Submitted by ' + opts.msgParams.origin,
'to be signed by: ' + uiUtils.addressSummary(opts.msgParams.from),
'message:\n' + opts.msgParams.data,
].join('\n')
var id = createId()
chrome.notifications.create(id, {
type: 'basic',
requireInteraction: true,
iconUrl: '/images/icon-128.png',
title: opts.title,
message: message,
buttons: [{
title: 'confirm',
2016-06-21 22:18:32 +02:00
}, {
title: 'cancel',
2016-06-21 22:18:32 +02:00
}],
})
notificationHandlers[id] = {
confirm: opts.confirm,
cancel: opts.cancel,
}
2016-06-21 22:18:32 +02:00
}
2016-06-23 04:28:11 +02:00
2016-06-24 01:42:40 +02:00
function renderTransactionNotificationSVG(opts, cb){
2016-06-23 04:28:11 +02:00
var state = {
nonInteractive: true,
2016-06-24 01:42:40 +02:00
inlineIdenticons: true,
2016-06-23 04:28:11 +02:00
txData: {
2016-06-24 01:42:40 +02:00
txParams: opts.txParams,
time: (new Date()).getTime(),
2016-06-23 04:28:11 +02:00
},
identities: {
},
accounts: {
},
}
var container = document.createElement('div')
var confirmView = h('div.app-primary', {
style: {
width: '450px',
height: '300px',
padding: '16px',
// background: '#F7F7F7',
background: 'white',
},
}, [
h('style', MetaMaskUiCss()),
2016-06-23 04:28:11 +02:00
renderPendingTx(h, state),
])
render(confirmView, container, function ready(){
var rootElement = findDOMNode(this)
var viewSource = rootElement.outerHTML
2016-06-23 04:28:11 +02:00
unmountComponentAtNode(container)
var svgSource = svgWrapper(viewSource)
2016-06-23 04:28:11 +02:00
// insert content into svg wrapper
cb(null, svgSource)
2016-06-23 04:28:11 +02:00
})
}
function svgWrapper(content){
var wrapperSource = `
<svg xmlns="http://www.w3.org/2000/svg" width="450" height="300">
<foreignObject x="0" y="0" width="100%" height="100%">
<body xmlns="http://www.w3.org/1999/xhtml" height="100%">{{content}}</body>
</foreignObject>
</svg>
`
return wrapperSource.split('{{content}}').join(content)
2016-06-23 04:28:11 +02:00
}