2021-02-04 19:15:23 +01:00
|
|
|
import contractMap from '@metamask/contract-metadata';
|
2021-05-17 21:00:59 +02:00
|
|
|
import { isHexString, addHexPrefix } from 'ethereumjs-util';
|
|
|
|
import { isValidHexAddress } from '../../shared/modules/hexstring-utils';
|
|
|
|
import { checksumAddress } from '../app/helpers/utils/util';
|
2016-06-06 23:05:13 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let iconFactory;
|
2020-08-19 18:27:05 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function iconFactoryGenerator(jazzicon) {
|
2016-06-06 23:05:13 +02:00
|
|
|
if (!iconFactory) {
|
2021-02-04 19:15:23 +01:00
|
|
|
iconFactory = new IconFactory(jazzicon);
|
2016-06-06 23:05:13 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return iconFactory;
|
2016-06-06 23:05:13 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function IconFactory(jazzicon) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.jazzicon = jazzicon;
|
|
|
|
this.cache = {};
|
2016-06-06 23:05:13 +02:00
|
|
|
}
|
|
|
|
|
2017-05-26 09:19:24 +02:00
|
|
|
IconFactory.prototype.iconForAddress = function (address, diameter) {
|
2021-05-13 00:25:37 +02:00
|
|
|
let addr = address;
|
|
|
|
|
2021-05-17 21:00:59 +02:00
|
|
|
if (isHexString(addHexPrefix(address))) {
|
|
|
|
addr = checksumAddress(addHexPrefix(address));
|
2021-05-13 00:25:37 +02:00
|
|
|
}
|
|
|
|
|
2017-05-26 09:19:24 +02:00
|
|
|
if (iconExistsFor(addr)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return imageElFor(addr);
|
2016-06-06 23:05:13 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01: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) {
|
2021-02-04 19:15:23 +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] ||
|
2021-02-04 19:15:23 +01:00
|
|
|
(this.cache[cacheId] = this.generateNewIdenticon(address, diameter));
|
2016-06-24 01:09:25 +02:00
|
|
|
// create a clean copy so you can modify it
|
2021-02-04 19:15:23 +01:00
|
|
|
const cleanCopy = identicon.cloneNode(true);
|
|
|
|
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) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const numericRepresentation = jsNumberForAddress(address);
|
|
|
|
const identicon = this.jazzicon(diameter, numericRepresentation);
|
|
|
|
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 (
|
2021-05-17 21:00:59 +02:00
|
|
|
contractMap[address] &&
|
|
|
|
isValidHexAddress(address, { allowNonPrefixed: false }) &&
|
|
|
|
contractMap[address].logo
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2017-05-26 09:19:24 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function imageElFor(address) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const contract = contractMap[address];
|
|
|
|
const fileName = contract.logo;
|
|
|
|
const path = `images/contract/${fileName}`;
|
|
|
|
const img = document.createElement('img');
|
|
|
|
img.src = path;
|
|
|
|
img.style.width = '100%';
|
|
|
|
return img;
|
2017-05-26 09:19:24 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function jsNumberForAddress(address) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const addr = address.slice(2, 10);
|
|
|
|
const seed = parseInt(addr, 16);
|
|
|
|
return seed;
|
2016-06-06 23:05:13 +02:00
|
|
|
}
|