2016-07-07 02:58:46 +02:00
|
|
|
/* CONTRACT NAMER
|
|
|
|
*
|
|
|
|
* Takes an address,
|
|
|
|
* Returns a nicname if we have one stored,
|
|
|
|
* otherwise returns null.
|
|
|
|
*/
|
|
|
|
|
2017-05-26 19:25:00 +02:00
|
|
|
const contractMap = require('eth-contract-metadata')
|
|
|
|
const ethUtil = require('ethereumjs-util')
|
2016-07-07 02:58:46 +02:00
|
|
|
|
2016-11-11 19:26:12 +01:00
|
|
|
module.exports = function (addr, identities = {}) {
|
2017-05-26 19:25:00 +02:00
|
|
|
const checksummed = ethUtil.toChecksumAddress(addr)
|
2017-05-26 20:05:51 +02:00
|
|
|
if (contractMap[checksummed] && contractMap[checksummed].name) {
|
2017-05-26 19:25:00 +02:00
|
|
|
return contractMap[checksummed].name
|
|
|
|
}
|
|
|
|
|
2016-07-07 22:21:45 +02:00
|
|
|
const address = addr.toLowerCase()
|
|
|
|
const ids = hashFromIdentities(identities)
|
2017-05-26 19:25:00 +02:00
|
|
|
return addrFromHash(address, ids)
|
2016-07-07 22:21:45 +02:00
|
|
|
}
|
|
|
|
|
2016-11-11 19:26:12 +01:00
|
|
|
function hashFromIdentities (identities) {
|
2016-07-07 22:21:45 +02:00
|
|
|
const result = {}
|
2016-11-11 19:26:12 +01:00
|
|
|
for (const key in identities) {
|
2016-07-07 22:21:45 +02:00
|
|
|
result[key] = identities[key].name
|
2016-07-07 02:58:46 +02:00
|
|
|
}
|
2016-07-07 22:21:45 +02:00
|
|
|
return result
|
|
|
|
}
|
2016-07-07 02:58:46 +02:00
|
|
|
|
2016-11-11 19:26:12 +01:00
|
|
|
function addrFromHash (addr, hash) {
|
2016-07-07 22:21:45 +02:00
|
|
|
const address = addr.toLowerCase()
|
|
|
|
return hash[address] || null
|
2016-07-07 02:58:46 +02:00
|
|
|
}
|