mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
53 lines
1.3 KiB
JavaScript
53 lines
1.3 KiB
JavaScript
|
var iconFactory
|
||
|
|
||
|
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) {
|
||
|
if (this.isCached(address, diameter)) {
|
||
|
return this.cache[address][diameter]
|
||
|
}
|
||
|
|
||
|
const dataUri = this.generateNewUri(address, diameter)
|
||
|
this.cacheIcon(address, diameter, dataUri)
|
||
|
return dataUri
|
||
|
}
|
||
|
|
||
|
IconFactory.prototype.generateNewUri = function(address, diameter) {
|
||
|
var numericRepresentation = jsNumberForAddress(address)
|
||
|
var identicon = this.jazzicon(diameter, numericRepresentation)
|
||
|
var identiconSrc = identicon.innerHTML
|
||
|
var dataUri = 'data:image/svg+xml;charset=utf-8,'+encodeURIComponent(identiconSrc)
|
||
|
return dataUri
|
||
|
}
|
||
|
|
||
|
IconFactory.prototype.cacheIcon = function(address, diameter, icon) {
|
||
|
if (!(address in this.cache)) {
|
||
|
var sizeCache = {}
|
||
|
sizeCache[diameter] = icon
|
||
|
return this.cache[address] = sizeCache
|
||
|
|
||
|
} else {
|
||
|
return this.cache[address][diameter] = icon
|
||
|
}
|
||
|
}
|
||
|
|
||
|
IconFactory.prototype.isCached = function(address, diameter) {
|
||
|
return address in this.cache && diameter in this.cache[address]
|
||
|
}
|
||
|
|
||
|
function jsNumberForAddress(address) {
|
||
|
var addr = address.slice(2, 10)
|
||
|
var seed = parseInt(addr, 16)
|
||
|
return seed
|
||
|
}
|