Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk): add option to request all keys #2445

Merged
merged 5 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ const {
GetIdentityKeysRequest,
KeyRequestType,
SpecificKeys,
AllKeys,
},
} = require('@dashevo/dapi-grpc');

const { UInt32Value } = require('google-protobuf/google/protobuf/wrappers_pb');

const { GetIdentityKeysRequestV0 } = GetIdentityKeysRequest;

const GetIdentityKeysResponse = require('./GetIdentityKeysResponse');
const InvalidResponseError = require('../response/errors/InvalidResponseError');

Expand All @@ -20,25 +24,30 @@ function getIdentityKeysFactory(grpcTransport) {
* Fetch the version upgrade votes status
* @typedef {getIdentityKeys}
* @param {Buffer} identityId
* @param {number[]} keyIds
* @param {number[]=} keyIds
* @param {number} limit
* @param {DAPIClientOptions & {prove: boolean}} [options]
* @returns {Promise<GetIdentityKeysResponse>}
*/
async function getIdentityKeys(identityId, keyIds, limit = 100, options = {}) {
const { GetIdentityKeysRequestV0 } = GetIdentityKeysRequest;
const getIdentityKeysRequest = new GetIdentityKeysRequest();

if (Buffer.isBuffer(identityId)) {
// eslint-disable-next-line no-param-reassign
identityId = Buffer.from(identityId);
}

const getIdentityKeysRequest = new GetIdentityKeysRequest();
const requestType = new KeyRequestType();

if (keyIds) {
requestType.setSpecificKeys(new SpecificKeys().setKeyIdsList(keyIds));
} else {
requestType.setAllKeys(new AllKeys());
}

getIdentityKeysRequest.setV0(
new GetIdentityKeysRequestV0()
.setIdentityId(identityId)
.setRequestType(new KeyRequestType()
.setSpecificKeys(new SpecificKeys().setKeyIdsList(keyIds)))
.setRequestType(requestType)
.setLimit(new UInt32Value([limit]))
.setProve(!!options.prove),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
Proof: ProofResponse,
KeyRequestType,
SpecificKeys,
AllKeys,
},
} = require('@dashevo/dapi-grpc');
const { UInt32Value } = require('google-protobuf/google/protobuf/wrappers_pb');
Expand All @@ -24,6 +25,7 @@ describe('getIdentityKeysFactory', () => {
let getIdentityKeys;
let options;
let response;
let metadata;
let keys;
let identityId;
let keyIds;
Expand All @@ -42,7 +44,7 @@ describe('getIdentityKeysFactory', () => {
metadataFixture = getMetadataFixture();
proofFixture = getProofFixture();

const metadata = new ResponseMetadata();
metadata = new ResponseMetadata();
metadata.setHeight(metadataFixture.height);
metadata.setCoreChainLockedHeight(metadataFixture.coreChainLockedHeight);
metadata.setTimeMs(metadataFixture.timeMs);
Expand Down Expand Up @@ -74,16 +76,52 @@ describe('getIdentityKeysFactory', () => {
};
});

it('should return identity keys', async () => {
const result = await getIdentityKeys(identityId, keyIds, limit, options);
it('should return specific identity keys', async () => {
response.setV0(
new GetIdentityKeysResponseV0()
.setKeys(new Keys().setKeysBytesList([keys[0]]))
.setMetadata(metadata),
);

const result = await getIdentityKeys(identityId, [keyIds[0]], limit, options);

const { GetIdentityKeysRequestV0 } = GetIdentityKeysRequest;
const request = new GetIdentityKeysRequest();
request.setV0(
new GetIdentityKeysRequestV0()
.setIdentityId(identityId)
.setRequestType(new KeyRequestType().setSpecificKeys(new SpecificKeys()
.setKeyIdsList(keyIds)))
.setKeyIdsList([keyIds[0]])))
.setLimit(new UInt32Value([limit]))
.setProve(false),
);

expect(grpcTransportMock.request).to.be.calledOnceWithExactly(
PlatformPromiseClient,
'getIdentityKeys',
request,
options,
);
expect(result.getIdentityKeys()).to.deep.equal([keys[0]]);
expect(result.getMetadata()).to.deep.equal(metadataFixture);
expect(result.getProof()).to.equal(undefined);
});

it('should return all identity keys', async () => {
response.setV0(
new GetIdentityKeysResponseV0()
.setKeys(new Keys().setKeysBytesList(keys))
.setMetadata(metadata),
);

const result = await getIdentityKeys(identityId, null, limit, options);

const { GetIdentityKeysRequestV0 } = GetIdentityKeysRequest;
const request = new GetIdentityKeysRequest();
request.setV0(
new GetIdentityKeysRequestV0()
.setIdentityId(identityId)
.setRequestType(new KeyRequestType().setAllKeys(new AllKeys()))
.setLimit(new UInt32Value([limit]))
.setProve(false),
);
Expand Down
Loading