2022-07-18 16:43:30 +02:00
|
|
|
import { isValidHexAddress } from '../../../shared/modules/hexstring-utils';
|
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
|
|
|
}
|
|
|
|
|
2022-08-10 03:26:25 +02:00
|
|
|
IconFactory.prototype.iconForAddress = function (
|
|
|
|
address,
|
|
|
|
diameter,
|
|
|
|
tokenMetadata,
|
|
|
|
) {
|
|
|
|
if (iconExistsFor(address, tokenMetadata)) {
|
|
|
|
return imageElFor(tokenMetadata);
|
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
|
|
|
|
|
2022-08-10 03:26:25 +02:00
|
|
|
function iconExistsFor(address, tokenMetadata) {
|
2020-11-03 00:41:28 +01:00
|
|
|
return (
|
2021-05-17 21:00:59 +02:00
|
|
|
isValidHexAddress(address, { allowNonPrefixed: false }) &&
|
2022-08-10 03:26:25 +02:00
|
|
|
tokenMetadata &&
|
|
|
|
tokenMetadata.iconUrl
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2017-05-26 09:19:24 +02:00
|
|
|
}
|
|
|
|
|
2022-08-10 03:26:25 +02:00
|
|
|
function imageElFor(tokenMetadata = {}) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const img = document.createElement('img');
|
2022-07-18 16:43:30 +02:00
|
|
|
img.src = tokenMetadata?.iconUrl;
|
2021-02-04 19:15:23 +01:00
|
|
|
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
|
|
|
}
|