1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-18 15:33:21 +01:00
metamask-extension/ui/example.js

124 lines
2.8 KiB
JavaScript
Raw Normal View History

const injectCss = require('inject-css')
const MetaMaskUi = require('./index.js')
const MetaMaskUiCss = require('./css.js')
const EventEmitter = require('events').EventEmitter
// account management
var identities = {
'0x1113462427bcc9133bb46e88bcbe39cd7ef0e111': {
name: 'Walrus',
img: 'QmW6hcwYzXrNkuHrpvo58YeZvbZxUddv69ATSHY3BHpPdd',
address: '0x1113462427bcc9133bb46e88bcbe39cd7ef0e111',
balance: 220,
txCount: 4,
},
'0x222462427bcc9133bb46e88bcbe39cd7ef0e7222': {
name: 'Tardus',
img: 'QmQYaRdrf2EhRhJWaHnts8Meu1mZiXrNib5W1P6cYmXWRL',
address: '0x222462427bcc9133bb46e88bcbe39cd7ef0e7222',
balance: 10.005,
txCount: 16,
},
'0x333462427bcc9133bb46e88bcbe39cd7ef0e7333': {
name: 'Gambler',
img: 'QmW6hcwYzXrNkuHrpvo58YeZvbZxUddv69ATSHY3BHpPdd',
address: '0x333462427bcc9133bb46e88bcbe39cd7ef0e7333',
balance: 0.000001,
txCount: 1,
2016-06-21 22:18:32 +02:00
},
}
var unapprovedTxs = {}
addUnconfTx({
from: '0x222462427bcc9133bb46e88bcbe39cd7ef0e7222',
to: '0x1113462427bcc9133bb46e88bcbe39cd7ef0e111',
value: '0x123',
})
addUnconfTx({
from: '0x1113462427bcc9133bb46e88bcbe39cd7ef0e111',
to: '0x333462427bcc9133bb46e88bcbe39cd7ef0e7333',
value: '0x0000',
data: '0x000462427bcc9133bb46e88bcbe39cd7ef0e7000',
})
2016-06-21 22:18:32 +02:00
function addUnconfTx (txParams) {
var time = (new Date()).getTime()
var id = createRandomId()
unapprovedTxs[id] = {
id: id,
txParams: txParams,
time: time,
}
}
var isUnlocked = false
var selectedAccount = null
2016-06-21 22:18:32 +02:00
function getState () {
return {
isUnlocked: isUnlocked,
identities: isUnlocked ? identities : {},
unapprovedTxs: isUnlocked ? unapprovedTxs : {},
selectedAccount: selectedAccount,
}
}
var accountManager = new EventEmitter()
2016-06-21 22:18:32 +02:00
accountManager.getState = function (cb) {
cb(null, getState())
}
2016-06-21 22:18:32 +02:00
accountManager.setLocked = function () {
isUnlocked = false
this._didUpdate()
}
2016-06-21 22:18:32 +02:00
accountManager.submitPassword = function (password, cb) {
if (password === 'test') {
isUnlocked = true
cb(null, getState())
this._didUpdate()
} else {
cb(new Error('Bad password -- try "test"'))
}
}
accountManager.setSelectedAccount = function (address, cb) {
selectedAccount = address
cb(null, getState())
this._didUpdate()
}
2016-06-21 22:18:32 +02:00
accountManager.signTransaction = function (txParams, cb) {
alert('signing tx....')
}
2016-06-21 22:18:32 +02:00
accountManager._didUpdate = function () {
this.emit('update', getState())
}
// start app
var container = document.getElementById('app-content')
var css = MetaMaskUiCss()
injectCss(css)
2016-06-21 22:56:04 +02:00
MetaMaskUi({
container: container,
2016-06-21 22:18:32 +02:00
accountManager: accountManager,
})
// util
2016-06-21 22:18:32 +02:00
function createRandomId () {
// 13 time digits
2016-06-21 22:18:32 +02:00
var datePart = new Date().getTime() * Math.pow(10, 3)
// 3 random digits
2016-06-21 22:18:32 +02:00
var extraPart = Math.floor(Math.random() * Math.pow(10, 3))
// 16 digits
2016-06-21 22:18:32 +02:00
return datePart + extraPart
}