2020-01-09 04:34:58 +01:00
|
|
|
import { isValidAddress } from 'ethereumjs-util'
|
2020-12-01 23:55:01 +01:00
|
|
|
import contractMap from '@metamask/contract-metadata'
|
2020-08-18 21:18:25 +02:00
|
|
|
import { checksumAddress } from '../app/helpers/utils/util'
|
2016-06-06 23:05:13 +02:00
|
|
|
|
2020-08-19 18:27:05 +02:00
|
|
|
let iconFactory
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function iconFactoryGenerator(jazzicon) {
|
2016-06-06 23:05:13 +02:00
|
|
|
if (!iconFactory) {
|
|
|
|
iconFactory = new IconFactory(jazzicon)
|
|
|
|
}
|
|
|
|
return iconFactory
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function IconFactory(jazzicon) {
|
2016-06-06 23:05:13 +02:00
|
|
|
this.jazzicon = jazzicon
|
|
|
|
this.cache = {}
|
|
|
|
}
|
|
|
|
|
2017-05-26 09:19:24 +02:00
|
|
|
IconFactory.prototype.iconForAddress = function (address, diameter) {
|
2018-04-17 21:57:55 +02:00
|
|
|
const addr = checksumAddress(address)
|
2017-05-26 09:19:24 +02:00
|
|
|
if (iconExistsFor(addr)) {
|
|
|
|
return imageElFor(addr)
|
2016-06-06 23:05:13 +02:00
|
|
|
}
|
|
|
|
|
2017-05-26 09:19:24 +02:00
|
|
|
return this.generateIdenticonSvg(address, diameter)
|
2016-06-06 23:05:13 +02:00
|
|
|
}
|
|
|
|
|
2016-06-24 01:09:25 +02:00
|
|
|
// returns svg dom element
|
|
|
|
IconFactory.prototype.generateIdenticonSvg = function (address, diameter) {
|
2019-12-03 15:52:01 +01:00
|
|
|
const cacheId = `${address}:${diameter}`
|
2016-06-24 01:09:25 +02:00
|
|
|
// check cache, lazily generate and populate cache
|
2020-11-03 00:41:28 +01:00
|
|
|
const identicon =
|
|
|
|
this.cache[cacheId] ||
|
|
|
|
(this.cache[cacheId] = this.generateNewIdenticon(address, diameter))
|
2016-06-24 01:09:25 +02:00
|
|
|
// create a clean copy so you can modify it
|
2019-12-03 15:52:01 +01:00
|
|
|
const cleanCopy = identicon.cloneNode(true)
|
2016-06-24 01:09:25 +02:00
|
|
|
return cleanCopy
|
2016-06-06 23:05:13 +02:00
|
|
|
}
|
|
|
|
|
2016-06-24 01:09:25 +02:00
|
|
|
// creates a new identicon
|
|
|
|
IconFactory.prototype.generateNewIdenticon = function (address, diameter) {
|
2019-12-03 15:52:01 +01:00
|
|
|
const numericRepresentation = jsNumberForAddress(address)
|
|
|
|
const identicon = this.jazzicon(diameter, numericRepresentation)
|
2016-06-24 01:09:25 +02:00
|
|
|
return identicon
|
2016-06-06 23:05:13 +02:00
|
|
|
}
|
|
|
|
|
2016-06-24 01:09:25 +02:00
|
|
|
// util
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function iconExistsFor(address) {
|
|
|
|
return (
|
|
|
|
contractMap[address] && isValidAddress(address) && contractMap[address].logo
|
|
|
|
)
|
2017-05-26 09:19:24 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function imageElFor(address) {
|
2017-05-26 18:58:33 +02:00
|
|
|
const contract = contractMap[address]
|
|
|
|
const fileName = contract.logo
|
2017-05-26 09:19:24 +02:00
|
|
|
const path = `images/contract/${fileName}`
|
|
|
|
const img = document.createElement('img')
|
|
|
|
img.src = path
|
2017-09-05 10:48:52 +02:00
|
|
|
img.style.width = '100%'
|
2017-05-26 09:19:24 +02:00
|
|
|
return img
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function jsNumberForAddress(address) {
|
2019-12-03 15:52:01 +01:00
|
|
|
const addr = address.slice(2, 10)
|
|
|
|
const seed = parseInt(addr, 16)
|
2016-06-06 23:05:13 +02:00
|
|
|
return seed
|
|
|
|
}
|