Skip to content

Commit

Permalink
feat: use new drive response format (dashevo#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Shuplenkov committed Nov 18, 2020
1 parent b834768 commit 69f01be
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
16 changes: 6 additions & 10 deletions lib/externalApis/drive/DriveStateRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class DriveStateRepository {

if (response.code === undefined || response.code === 0) {
// no errors found return the serialized response value
return Buffer.from(response.value, 'base64');
const value = cbor.decode(Buffer.from(response.value, 'base64'));

return value.data;
}

const { error: abciError } = JSON.parse(response.log);
Expand Down Expand Up @@ -85,16 +87,14 @@ class DriveStateRepository {
* @return {Promise<Buffer[]>}
*/
async fetchDocuments(contractId, type, options) {
const serializedDocumentsArray = await this.request(
return this.request(
'/dataContracts/documents',
{
...options,
contractId,
type,
},
);

return cbor.decode(serializedDocumentsArray);
}

/**
Expand All @@ -121,14 +121,12 @@ class DriveStateRepository {
* @return {Promise<Buffer[]>}
*/
async fetchIdentitiesByPublicKeyHashes(publicKeyHashes) {
const serializedIdentitiesArray = await this.request(
return this.request(
'/identities/by-public-key-hash',
{
publicKeyHashes,
},
);

return cbor.decode(serializedIdentitiesArray);
}

/**
Expand All @@ -139,14 +137,12 @@ class DriveStateRepository {
* @return {Promise<Buffer[]>}
*/
async fetchIdentityIdsByPublicKeyHashes(publicKeyHashes) {
const serializedIdentityIdsArray = await this.request(
return this.request(
'/identities/by-public-key-hash/id',
{
publicKeyHashes,
},
);

return cbor.decode(serializedIdentityIdsArray);
}
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ describe('DriveStateRepository', () => {
const drive = new DriveStateRepository({ host: '127.0.0.1', port: 3000 });

const contractId = 'someId';
const buffer = Buffer.from('someData');
const data = Buffer.from('someData');

const buffer = cbor.encode({ data });

sinon.stub(drive.client, 'request')
.resolves({
Expand All @@ -99,7 +101,7 @@ describe('DriveStateRepository', () => {
path: '/dataContracts',
data: cbor.encode({ id: contractId }).toString('hex'), // cbor encoded empty object
});
expect(result).to.be.deep.equal(buffer);
expect(result).to.be.deep.equal(data);
});
});

Expand All @@ -112,7 +114,9 @@ describe('DriveStateRepository', () => {
const options = {
where: 'id === someId',
};
const buffer = cbor.encode([]);

const data = [];
const buffer = cbor.encode({ data });

sinon.stub(drive.client, 'request')
.resolves({
Expand All @@ -127,7 +131,7 @@ describe('DriveStateRepository', () => {
path: '/dataContracts/documents',
data: cbor.encode({ ...options, contractId, type }).toString('hex'), // cbor encoded empty object
});
expect(result).to.be.deep.equal([]);
expect(result).to.be.deep.equal(data);
});
});

Expand All @@ -136,7 +140,8 @@ describe('DriveStateRepository', () => {
const drive = new DriveStateRepository({ host: '127.0.0.1', port: 3000 });

const identityId = 'someId';
const buffer = Buffer.from('someData');
const data = Buffer.from('someData');
const buffer = cbor.encode({ data });

sinon.stub(drive.client, 'request')
.resolves({
Expand All @@ -151,7 +156,7 @@ describe('DriveStateRepository', () => {
path: '/identities',
data: cbor.encode({ id: identityId }).toString('hex'), // cbor encoded empty object
});
expect(result).to.be.deep.equal(buffer);
expect(result).to.be.deep.equal(data);
});
});

Expand All @@ -160,12 +165,13 @@ describe('DriveStateRepository', () => {
const drive = new DriveStateRepository({ host: '127.0.0.1', port: 3000 });

const identity = getIdentityFixture();
const buffer = cbor.encode({ data: [identity] });
const publicKeyHashes = [Buffer.alloc(1)];

sinon.stub(drive.client, 'request')
.resolves({
result: {
response: { code: 0, value: cbor.encode([identity]) },
response: { code: 0, value: buffer },
},
});

Expand All @@ -185,11 +191,12 @@ describe('DriveStateRepository', () => {

const identityId = generateRandomIdentifier();
const publicKeyHashes = [Buffer.alloc(1)];
const buffer = cbor.encode({ data: [identityId] });

sinon.stub(drive.client, 'request')
.resolves({
result: {
response: { code: 0, value: cbor.encode([identityId]) },
response: { code: 0, value: buffer },
},
});

Expand Down
File renamed without changes.

0 comments on commit 69f01be

Please sign in to comment.