From e8efe84320ea791535b40e69a64525f7fdb3ea8a Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Thu, 7 Jul 2016 13:21:45 -0700 Subject: [PATCH] Add nickname rendering for recipient address --- test/unit/nameForAccount_test.js | 44 +++++++++++++++++++++++++ ui/app/components/pending-tx-details.js | 2 +- ui/lib/contract-namer.js | 24 +++++++++++--- 3 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 test/unit/nameForAccount_test.js diff --git a/test/unit/nameForAccount_test.js b/test/unit/nameForAccount_test.js new file mode 100644 index 000000000..6839d40f8 --- /dev/null +++ b/test/unit/nameForAccount_test.js @@ -0,0 +1,44 @@ +var assert = require('assert') +var sinon = require('sinon') + +var path = require('path') +var contractNamer = require(path.join(__dirname, '..', '..', 'ui', 'lib', 'contract-namer.js')) + +describe('contractNamer', function() { + + beforeEach(function() { + this.sinon = sinon.sandbox.create() + }) + + afterEach(function() { + this.sinon.restore() + }) + + describe('naming a contract', function() { + + it('should return nothing for an unknown random account', function() { + const input = '0x2386F26FC10000' + const output = contractNamer(input) + assert.deepEqual(output, null) + }) + + it('should accept identities as an optional second parameter', function() { + const input = '0x2386F26FC10000'.toLowerCase() + const expected = 'bar' + const identities = {} + identities[input] = { name: expected } + const output = contractNamer(input, identities) + assert.deepEqual(output, expected) + }) + + it('should check for identities case insensitively', function() { + const input = '0x2386F26FC10000'.toLowerCase() + const expected = 'bar' + const identities = {} + identities[input] = { name: expected } + const output = contractNamer(input.toUpperCase(), identities) + assert.deepEqual(output, expected) + }) + + }) +}) diff --git a/ui/app/components/pending-tx-details.js b/ui/app/components/pending-tx-details.js index e0b629e89..b2c16e772 100644 --- a/ui/app/components/pending-tx-details.js +++ b/ui/app/components/pending-tx-details.js @@ -177,7 +177,7 @@ PTXP.miniAccountPanelForRecipient = function () { style: { fontFamily: 'Montserrat Bold, Montserrat, sans-serif', }, - }, nameForAddress(txParams.to)), + }, nameForAddress(txParams.to, props.identities)), h('span.font-small', { style: { fontFamily: 'Montserrat Light, Montserrat, sans-serif', diff --git a/ui/lib/contract-namer.js b/ui/lib/contract-namer.js index eae066ad5..62c7933e8 100644 --- a/ui/lib/contract-namer.js +++ b/ui/lib/contract-namer.js @@ -5,13 +5,27 @@ * otherwise returns null. */ +// Nickname keys must be stored in lower case. const nicknames = {} -module.exports = function(address) { +module.exports = function(addr, identities = {}) { - if (address in nicknames) { - return nicknames[address] - } + const address = addr.toLowerCase() + const ids = hashFromIdentities(identities) - return null + console.dir({ addr, ids }) + return addrFromHash(address, ids) || addrFromHash(address, nicknames) +} + +function hashFromIdentities(identities) { + const result = {} + for (let key in identities) { + result[key] = identities[key].name + } + return result +} + +function addrFromHash(addr, hash) { + const address = addr.toLowerCase() + return hash[address] || null }