blob: 27a74de662b77a88a10df7b6c5175484f572a081 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
var iconFactory
const isValidAddress = require('ethereumjs-util').isValidAddress
const toChecksumAddress = require('ethereumjs-util').toChecksumAddress
const contractMap = require('eth-contract-metadata')
module.exports = function (jazzicon) {
if (!iconFactory) {
iconFactory = new IconFactory(jazzicon)
}
return iconFactory
}
function IconFactory (jazzicon) {
this.jazzicon = jazzicon
this.cache = {}
}
IconFactory.prototype.iconForAddress = function (address, diameter) {
const addr = toChecksumAddress(address)
if (iconExistsFor(addr)) {
return imageElFor(addr)
}
return this.generateIdenticonSvg(address, diameter)
}
// returns svg dom element
IconFactory.prototype.generateIdenticonSvg = function (address, diameter) {
var cacheId = `${address}:${diameter}`
// check cache, lazily generate and populate cache
var identicon = this.cache[cacheId] || (this.cache[cacheId] = this.generateNewIdenticon(address, diameter))
// create a clean copy so you can modify it
var cleanCopy = identicon.cloneNode(true)
return cleanCopy
}
// creates a new identicon
IconFactory.prototype.generateNewIdenticon = function (address, diameter) {
var numericRepresentation = jsNumberForAddress(address)
var identicon = this.jazzicon(diameter, numericRepresentation)
return identicon
}
// util
function iconExistsFor (address) {
return contractMap[address] && isValidAddress(address) && contractMap[address].logo
}
function imageElFor (address) {
const contract = contractMap[address]
const fileName = contract.logo
const path = `images/contract/${fileName}`
const img = document.createElement('img')
img.src = path
img.style.width = '75%'
return img
}
function jsNumberForAddress (address) {
var addr = address.slice(2, 10)
var seed = parseInt(addr, 16)
return seed
}
|