2021-05-17 23:19:39 +02:00
|
|
|
import {
|
|
|
|
isValidHexAddress,
|
|
|
|
toChecksumHexAddress,
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2021-09-09 22:56:27 +02:00
|
|
|
IconFactory.prototype.iconForAddress = function (
|
|
|
|
address,
|
|
|
|
diameter,
|
|
|
|
useTokenDetection,
|
|
|
|
tokenList,
|
|
|
|
) {
|
|
|
|
// When useTokenDetection flag is true the tokenList contains tokens with non-checksum address from the dynamic token service api,
|
|
|
|
// When useTokenDetection flag is false the tokenList contains tokens with checksum addresses from contract-metadata.
|
|
|
|
// So the flag indicates whether the address of tokens currently on the tokenList is checksum or not.
|
|
|
|
const addr = useTokenDetection ? address : toChecksumHexAddress(address);
|
|
|
|
if (iconExistsFor(addr, tokenList)) {
|
|
|
|
return imageElFor(addr, useTokenDetection, tokenList);
|
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
|
|
|
|
|
2021-09-09 22:56:27 +02:00
|
|
|
function iconExistsFor(address, tokenList) {
|
2020-11-03 00:41:28 +01:00
|
|
|
return (
|
2021-09-09 22:56:27 +02:00
|
|
|
tokenList[address] &&
|
2021-05-17 21:00:59 +02:00
|
|
|
isValidHexAddress(address, { allowNonPrefixed: false }) &&
|
2021-09-09 22:56:27 +02:00
|
|
|
tokenList[address].iconUrl
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2017-05-26 09:19:24 +02:00
|
|
|
}
|
|
|
|
|
2021-09-09 22:56:27 +02:00
|
|
|
function imageElFor(address, useTokenDetection, tokenList) {
|
|
|
|
const tokenMetadata = tokenList[address];
|
|
|
|
const fileName = tokenMetadata?.iconUrl;
|
|
|
|
// token from dynamic api list is fetched when useTokenDetection is true
|
|
|
|
// In the static list, the iconUrl will be holding only a filename for the image,
|
|
|
|
// the corresponding images will be available in the `images/contract/` location when the contract-metadata package was added to the extension
|
|
|
|
// so that it can be accessed using the filename in iconUrl.
|
|
|
|
const path = useTokenDetection ? fileName : `images/contract/${fileName}`;
|
2021-02-04 19:15:23 +01:00
|
|
|
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
|
|
|
}
|