diff --git a/rest/src/db/dbUtils.js b/rest/src/db/dbUtils.js index f3bb4664..b1e1b87c 100644 --- a/rest/src/db/dbUtils.js +++ b/rest/src/db/dbUtils.js @@ -82,21 +82,27 @@ const dbUtils = { }, /** - * Convert binary to Unresolved address - * @param {MongoDb.Binary} binary Address|NamespaceId from MongoDb. - * @returns {string} AddressBase32|NamespaceId - */ - bufferToUnresolvedAddress: binary => { + * Formats binary to a base32 address or hex address + * @param {MongoDb.Binary} binary Address|NamespaceId from MongoDb. + * @param {boolean} formatAddressUsingBase32 if base32 format should be used when formatting an address. Hex otherwise. + * @returns {string} the address in base32 format or hex format depending on formatAddressUsingBase32 + */ + bufferToUnresolvedAddress: (binary, formatAddressUsingBase32) => { if (!binary) return undefined; - if ((binary instanceof MongoDb.Binary)) - return address.addressToString(binary.buffer); + const getBuffer = () => { + if ((binary instanceof MongoDb.Binary)) + return binary.buffer; - if ((binary instanceof Uint8Array)) - return address.addressToString(binary); + if ((binary instanceof Uint8Array)) + return binary; - throw new Error(`Cannot convert binary address, unknown ${binary.constructor.name} type`); + throw new Error( + `Cannot convert binary address, unknown ${binary.constructor.name} type` + ); + }; + return formatAddressUsingBase32 ? address.addressToString(getBuffer()) : catapult.utils.convert.uint8ToHex(getBuffer()); }, /** diff --git a/rest/test/db/dbFormattingRules_spec.js b/rest/test/db/dbFormattingRules_spec.js index 4f2ba76e..9cd4830e 100644 --- a/rest/test/db/dbFormattingRules_spec.js +++ b/rest/test/db/dbFormattingRules_spec.js @@ -240,14 +240,14 @@ describe('db formatting rules', () => { }); }); - it('can format encodedAddress type', () => { + it('can format encodedAddress type using hex', () => { // Arrange const object = test.factory.createBinary(Buffer.from('98E0D138EAF2AC342C015FF0B631EC3622E8AFFA04BFCC56', 'hex')); // Act: - const result = formattingRules[ModelType.encodedAddress](object); + const result = formattingRules[ModelType.encodedAddress](object, false); // Assert: - expect(result).to.equal('TDQNCOHK6KWDILABL7YLMMPMGYRORL72AS74YVQ'); + expect(result).to.equal('98E0D138EAF2AC342C015FF0B631EC3622E8AFFA04BFCC56'); }); }); diff --git a/rest/test/db/dbUtils_spec.js b/rest/test/db/dbUtils_spec.js index a2361c3d..7bbfebdd 100644 --- a/rest/test/db/dbUtils_spec.js +++ b/rest/test/db/dbUtils_spec.js @@ -24,7 +24,7 @@ const { convertToLong } = require('../../src/db/dbUtils'); const { expect } = require('chai'); const MongoDb = require('mongodb'); -const { ObjectId, Binary } = MongoDb; +const { ObjectId } = MongoDb; describe('db utils', () => { describe('convertToLong', () => { @@ -169,18 +169,18 @@ describe('db utils', () => { }); describe('bufferToUnresolvedAddress', () => { - it('can convert from Binary to Address', () => { + it('can convert from Buffer to encoded address', () => { // Arrange - const object = new Binary(Buffer.from('98E0D138EAF2AC342C015FF0B631EC3622E8AFFA04BFCC56', 'hex')); + const object = Buffer.from('98E0D138EAF2AC342C015FF0B631EC3622E8AFFA04BFCC56', 'hex'); // Act: - const result = dbUtils.bufferToUnresolvedAddress(object); + const result = dbUtils.bufferToUnresolvedAddress(object, true); // Assert: expect(result).to.equal('TDQNCOHK6KWDILABL7YLMMPMGYRORL72AS74YVQ'); }); - it('can convert from Buffer to Address', () => { + it('can convert from Buffer to decoded address', () => { // Arrange const object = Buffer.from('98E0D138EAF2AC342C015FF0B631EC3622E8AFFA04BFCC56', 'hex'); @@ -188,7 +188,7 @@ describe('db utils', () => { const result = dbUtils.bufferToUnresolvedAddress(object); // Assert: - expect(result).to.equal('TDQNCOHK6KWDILABL7YLMMPMGYRORL72AS74YVQ'); + expect(result).to.equal('98E0D138EAF2AC342C015FF0B631EC3622E8AFFA04BFCC56'); }); it('can convert from undefined to undefined address', () => { diff --git a/rest/test/server/messageFormattingRules_spec.js b/rest/test/server/messageFormattingRules_spec.js index 919c5c14..cba09307 100644 --- a/rest/test/server/messageFormattingRules_spec.js +++ b/rest/test/server/messageFormattingRules_spec.js @@ -153,7 +153,7 @@ describe('message formatting rules', () => { expect(result).to.deep.equal(true); }); - it('can format encodedAddress type', () => { + it('can format decodedAddress type', () => { // Arrange: const object = test.factory.createBinary(Buffer.from('98E0D138EAF2AC342C015FF0B631EC3622E8AFFA04BFCC56', 'hex')); @@ -161,6 +161,6 @@ describe('message formatting rules', () => { const result = formattingRules[ModelType.encodedAddress](object); // Assert: - expect(result).to.deep.equal('TDQNCOHK6KWDILABL7YLMMPMGYRORL72AS74YVQ'); + expect(result).to.deep.equal('98E0D138EAF2AC342C015FF0B631EC3622E8AFFA04BFCC56'); }); });