Skip to content
This repository has been archived by the owner on Jun 10, 2022. It is now read-only.

Commit

Permalink
feat: restore decoded address format (#668)
Browse files Browse the repository at this point in the history
Co-authored-by: fernando <[email protected]>
  • Loading branch information
rg911 and fboucquez authored Oct 30, 2021
1 parent 1b68e67 commit a9e9c82
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
26 changes: 16 additions & 10 deletions rest/src/db/dbUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
},

/**
Expand Down
6 changes: 3 additions & 3 deletions rest/test/db/dbFormattingRules_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
12 changes: 6 additions & 6 deletions rest/test/db/dbUtils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -169,26 +169,26 @@ 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');

// Act:
const result = dbUtils.bufferToUnresolvedAddress(object);

// Assert:
expect(result).to.equal('TDQNCOHK6KWDILABL7YLMMPMGYRORL72AS74YVQ');
expect(result).to.equal('98E0D138EAF2AC342C015FF0B631EC3622E8AFFA04BFCC56');
});

it('can convert from undefined to undefined address', () => {
Expand Down
4 changes: 2 additions & 2 deletions rest/test/server/messageFormattingRules_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ 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'));

// Act:
const result = formattingRules[ModelType.encodedAddress](object);

// Assert:
expect(result).to.deep.equal('TDQNCOHK6KWDILABL7YLMMPMGYRORL72AS74YVQ');
expect(result).to.deep.equal('98E0D138EAF2AC342C015FF0B631EC3622E8AFFA04BFCC56');
});
});

0 comments on commit a9e9c82

Please sign in to comment.