@dashevo/dapi-client 0.25.21 → 1.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/docs/_sidebar.md +3 -1
  2. package/docs/usage/application/core/getBestBlockHeight.md +10 -0
  3. package/docs/usage/application/core/subscribeToBlockHeadersWithChainLocks.md +39 -0
  4. package/docs/usage/application/core/subscribeToMasternodeList.md +24 -0
  5. package/docs/usage/{utils → application/core}/subscribeToTransactionsWithProofs.md +13 -10
  6. package/lib/BlockHeadersProvider/createBlockHeadersProviderFromOptions.js +4 -2
  7. package/lib/DAPIClient.js +25 -2
  8. package/lib/SimplifiedMasternodeListProvider/SimplifiedMasternodeListProvider.js +174 -65
  9. package/lib/SimplifiedMasternodeListProvider/createMasternodeListStreamFactory.js +49 -0
  10. package/lib/dapiAddressProvider/createDAPIAddressProviderFromOptions.js +4 -9
  11. package/lib/logger/index.js +5 -1
  12. package/lib/methods/core/CoreMethodsFacade.js +10 -6
  13. package/lib/methods/core/getBestBlockHeightFactory.js +34 -0
  14. package/lib/methods/core/getBlockchainStatusFactory.js +59 -0
  15. package/lib/methods/core/getMasternodeStatusFactory.js +45 -0
  16. package/lib/methods/core/subscribeToMasternodeListFactory.js +46 -0
  17. package/lib/methods/core/subscribeToTransactionsWithProofsFactory.js +1 -1
  18. package/lib/methods/platform/PlatformMethodsFacade.js +10 -2
  19. package/lib/methods/platform/getIdentitiesContractKeys/GetIdentitiesContractKeysResponse.js +65 -0
  20. package/lib/methods/platform/getIdentitiesContractKeys/getIdentitiesContractKeysFactory.js +93 -0
  21. package/lib/methods/platform/getIdentityByPublicKeyHash/GetIdentityByPublicKeyHashResponse.js +40 -0
  22. package/lib/methods/platform/getIdentityByPublicKeyHash/getIdentityByPublicKeyHashFactory.js +64 -0
  23. package/lib/methods/platform/getIdentityContractNonce/GetIdentityContractNonceResponse.js +49 -0
  24. package/lib/methods/platform/getIdentityContractNonce/getIdentityContractNonceFactory.js +81 -0
  25. package/lib/methods/platform/getIdentityKeys/GetIdentityKeysResponse.js +52 -0
  26. package/lib/methods/platform/getIdentityKeys/getIdentityKeysFactory.js +78 -0
  27. package/lib/methods/platform/getIdentityNonce/GetIdentityNonceResponse.js +49 -0
  28. package/lib/methods/platform/getIdentityNonce/getIdentityNonceFactory.js +74 -0
  29. package/lib/methods/platform/getProtocolVersionUpgradeVoteStatus/GetProtocolVersionUpgradeVoteStatusResponse.js +1 -1
  30. package/lib/methods/platform/getProtocolVersionUpgradeVoteStatus/getProtocolVersionUpgradeVoteStatusFactory.js +1 -1
  31. package/lib/transport/GrpcTransport/GrpcTransport.js +9 -1
  32. package/lib/transport/GrpcTransport/createGrpcTransportError.js +1 -1
  33. package/lib/transport/ReconnectableStream.js +48 -6
  34. package/package.json +8 -8
  35. package/docs/usage/application/core/generateToAddress.md +0 -13
  36. package/docs/usage/application/core/getMnListDiff.md +0 -12
  37. package/lib/methods/core/generateToAddressFactory.js +0 -25
  38. package/lib/methods/core/getMnListDiffFactory.js +0 -21
  39. package/lib/methods/core/getStatusFactory.js +0 -68
  40. package/lib/methods/platform/getIdentitiesByPublicKeyHashes/GetIdentitiesByPublicKeyHashesResponse.js +0 -46
  41. package/lib/methods/platform/getIdentitiesByPublicKeyHashes/getIdentitiesByPublicKeyHashesFactory.js +0 -65
@@ -0,0 +1,34 @@
1
+ const {
2
+ v0: {
3
+ GetBestBlockHeightRequest,
4
+ CorePromiseClient,
5
+ },
6
+ } = require('@dashevo/dapi-grpc');
7
+
8
+ /**
9
+ *
10
+ * @param {GrpcTransport} grpcTransport
11
+ * @returns {getBestBlockHeight}
12
+ */
13
+ function getBestBlockHeightFactory(grpcTransport) {
14
+ /**
15
+ * Returns block height of chain tip
16
+ * @typedef {getBestBlockHeight}
17
+ * @param {DAPIClientOptions} [options]
18
+ * @returns {Promise<string>}
19
+ */
20
+ async function getBestBlockHeight(options = {}) {
21
+ const response = await grpcTransport.request(
22
+ CorePromiseClient,
23
+ 'getBestBlockHeight',
24
+ new GetBestBlockHeightRequest(),
25
+ options,
26
+ );
27
+
28
+ return response.getHeight();
29
+ }
30
+
31
+ return getBestBlockHeight;
32
+ }
33
+
34
+ module.exports = getBestBlockHeightFactory;
@@ -0,0 +1,59 @@
1
+ const {
2
+ v0: {
3
+ GetBlockchainStatusRequest,
4
+ GetBlockchainStatusResponse,
5
+ CorePromiseClient,
6
+ },
7
+ } = require('@dashevo/dapi-grpc');
8
+
9
+ /**
10
+ * @param {GrpcTransport} grpcTransport
11
+ * @returns {getBlockchainStatus}
12
+ */
13
+ function getBlockchainStatusFactory(grpcTransport) {
14
+ /**
15
+ * Get Core chain status
16
+ * @typedef {getBlockchainStatus}
17
+ * @param {DAPIClientOptions} [options]
18
+ * @returns {Promise<object>}
19
+ */
20
+ async function getBlockchainStatus(options = {}) {
21
+ const getBlockchainStatusRequest = new GetBlockchainStatusRequest();
22
+
23
+ const response = await grpcTransport.request(
24
+ CorePromiseClient,
25
+ 'getBlockchainStatus',
26
+ getBlockchainStatusRequest,
27
+ options,
28
+ );
29
+
30
+ const responseObject = response.toObject();
31
+
32
+ // Respond with Buffers instead of base64 for binary fields
33
+
34
+ if (response.getChain()) {
35
+ if (response.getChain()
36
+ .getBestBlockHash()) {
37
+ responseObject.chain.bestBlockHash = Buffer.from(response.getChain()
38
+ .getBestBlockHash());
39
+ }
40
+
41
+ if (response.getChain()
42
+ .getChainWork()) {
43
+ responseObject.chain.chainWork = Buffer.from(response.getChain()
44
+ .getChainWork());
45
+ }
46
+ }
47
+
48
+ // Respond with constant names instead of constant values
49
+
50
+ responseObject.status = Object.keys(GetBlockchainStatusResponse.Status)
51
+ .find((key) => GetBlockchainStatusResponse.Status[key] === responseObject.status);
52
+
53
+ return responseObject;
54
+ }
55
+
56
+ return getBlockchainStatus;
57
+ }
58
+
59
+ module.exports = getBlockchainStatusFactory;
@@ -0,0 +1,45 @@
1
+ const {
2
+ v0: {
3
+ GetMasternodeStatusRequest,
4
+ GetMasternodeStatusResponse,
5
+ CorePromiseClient,
6
+ },
7
+ } = require('@dashevo/dapi-grpc');
8
+
9
+ /**
10
+ * @param {GrpcTransport} grpcTransport
11
+ * @returns {getMasternodeStatus}
12
+ */
13
+ function getMasternodeStatusFactory(grpcTransport) {
14
+ /**
15
+ * Get Core chain status
16
+ * @typedef {getMasternodeStatus}
17
+ * @param {DAPIClientOptions} [options]
18
+ * @returns {Promise<object>}
19
+ */
20
+ async function getMasternodeStatus(options = {}) {
21
+ const getMasternodeStatusRequest = new GetMasternodeStatusRequest();
22
+
23
+ const response = await grpcTransport.request(
24
+ CorePromiseClient,
25
+ 'getMasternodeStatus',
26
+ getMasternodeStatusRequest,
27
+ options,
28
+ );
29
+
30
+ const responseObject = response.toObject();
31
+
32
+ // Respond with constant names instead of constant values
33
+
34
+ responseObject.status = Object.keys(GetMasternodeStatusResponse.Status)
35
+ .find((key) => GetMasternodeStatusResponse.Status[key] === responseObject.status);
36
+
37
+ responseObject.proTxHash = Buffer.from(responseObject.proTxHash, 'base64');
38
+
39
+ return responseObject;
40
+ }
41
+
42
+ return getMasternodeStatus;
43
+ }
44
+
45
+ module.exports = getMasternodeStatusFactory;
@@ -0,0 +1,46 @@
1
+ const {
2
+ v0: {
3
+ MasternodeListRequest,
4
+ CorePromiseClient,
5
+ },
6
+ } = require('@dashevo/dapi-grpc');
7
+
8
+ /**
9
+ * @param {GrpcTransport} grpcTransport
10
+ * @returns {subscribeToMasternodeList}
11
+ */
12
+ function subscribeToMasternodeListFactory(grpcTransport) {
13
+ /**
14
+ * @typedef {subscribeToMasternodeList}
15
+ * @param {DAPIClientOptions & subscribeToMasternodeListOptions} [options]
16
+ * @returns {
17
+ * EventEmitter|!grpc.web.ClientReadableStream<!MasternodeListResponse>
18
+ * }
19
+ */
20
+ async function subscribeToMasternodeList(options = { }) {
21
+ // eslint-disable-next-line no-param-reassign
22
+ options = {
23
+ // Override global timeout option
24
+ // and timeout for this method by default
25
+ timeout: undefined,
26
+ ...options,
27
+ };
28
+
29
+ const request = new MasternodeListRequest();
30
+
31
+ return grpcTransport.request(
32
+ CorePromiseClient,
33
+ 'subscribeToMasternodeList',
34
+ request,
35
+ options,
36
+ );
37
+ }
38
+
39
+ return subscribeToMasternodeList;
40
+ }
41
+
42
+ /**
43
+ * @typedef {object} subscribeToMasternodeListOptions
44
+ */
45
+
46
+ module.exports = subscribeToMasternodeListFactory;
@@ -29,7 +29,7 @@ function subscribeToTransactionsWithProofsFactory(grpcTransport) {
29
29
  * EventEmitter|!grpc.web.ClientReadableStream<!TransactionsWithProofsResponse>
30
30
  * }
31
31
  */
32
- async function subscribeToTransactionsWithProofs(bloomFilter, options = { }) {
32
+ async function subscribeToTransactionsWithProofs(bloomFilter, options = {}) {
33
33
  // eslint-disable-next-line no-param-reassign
34
34
  options = {
35
35
  count: 0,
@@ -3,12 +3,16 @@ const getDataContractFactory = require('./getDataContract/getDataContractFactory
3
3
  const getDataContractHistoryFactory = require('./getDataContractHistory/getDataContractHistoryFactory');
4
4
  const getDocumentsFactory = require('./getDocuments/getDocumentsFactory');
5
5
  const getIdentityFactory = require('./getIdentity/getIdentityFactory');
6
- const getIdentitiesByPublicKeyHashesFactory = require('./getIdentitiesByPublicKeyHashes/getIdentitiesByPublicKeyHashesFactory');
6
+ const getIdentityByPublicKeyHashFactory = require('./getIdentityByPublicKeyHash/getIdentityByPublicKeyHashFactory');
7
+ const getIdentitiesContractKeysFactory = require('./getIdentitiesContractKeys/getIdentitiesContractKeysFactory');
7
8
  const waitForStateTransitionResultFactory = require('./waitForStateTransitionResult/waitForStateTransitionResultFactory');
8
9
  const getConsensusParamsFactory = require('./getConsensusParams/getConsensusParamsFactory');
9
10
  const getEpochsInfoFactory = require('./getEpochsInfo/getEpochsInfoFactory');
10
11
  const getProtocolVersionUpgradeVoteStatusFactory = require('./getProtocolVersionUpgradeVoteStatus/getProtocolVersionUpgradeVoteStatusFactory');
11
12
  const getProtocolVersionUpgradeStateFactory = require('./getProtocolVersionUpgradeState/getProtocolVersionUpgradeStateFactory');
13
+ const getIdentityContractNonceFactory = require('./getIdentityContractNonce/getIdentityContractNonceFactory');
14
+ const getIdentityNonceFactory = require('./getIdentityNonce/getIdentityNonceFactory');
15
+ const getIdentityKeysFactory = require('./getIdentityKeys/getIdentityKeysFactory');
12
16
 
13
17
  class PlatformMethodsFacade {
14
18
  /**
@@ -20,7 +24,8 @@ class PlatformMethodsFacade {
20
24
  this.getDataContractHistory = getDataContractHistoryFactory(grpcTransport);
21
25
  this.getDocuments = getDocumentsFactory(grpcTransport);
22
26
  this.getIdentity = getIdentityFactory(grpcTransport);
23
- this.getIdentitiesByPublicKeyHashes = getIdentitiesByPublicKeyHashesFactory(grpcTransport);
27
+ this.getIdentityByPublicKeyHash = getIdentityByPublicKeyHashFactory(grpcTransport);
28
+ this.getIdentitiesContractKeys = getIdentitiesContractKeysFactory(grpcTransport);
24
29
  this.waitForStateTransitionResult = waitForStateTransitionResultFactory(grpcTransport);
25
30
  this.getConsensusParams = getConsensusParamsFactory(grpcTransport);
26
31
  this.getEpochsInfo = getEpochsInfoFactory(grpcTransport);
@@ -28,6 +33,9 @@ class PlatformMethodsFacade {
28
33
  grpcTransport,
29
34
  );
30
35
  this.getProtocolVersionUpgradeState = getProtocolVersionUpgradeStateFactory(grpcTransport);
36
+ this.getIdentityContractNonce = getIdentityContractNonceFactory(grpcTransport);
37
+ this.getIdentityNonce = getIdentityNonceFactory(grpcTransport);
38
+ this.getIdentityKeys = getIdentityKeysFactory(grpcTransport);
31
39
  }
32
40
  }
33
41
 
@@ -0,0 +1,65 @@
1
+ const { Identifier } = require('@dashevo/wasm-dpp');
2
+ const AbstractResponse = require('../response/AbstractResponse');
3
+
4
+ class GetIdentitiesContractKeysResponse extends AbstractResponse {
5
+ /**
6
+ * @param {object} identitiesKeys
7
+ * @param {Metadata} metadata
8
+ * @param {Proof} [proof]
9
+ */
10
+ constructor(identitiesKeys, metadata, proof = undefined) {
11
+ super(metadata, proof);
12
+
13
+ this.identitiesKeys = identitiesKeys;
14
+ }
15
+
16
+ /**
17
+ * @returns {object}
18
+ */
19
+ getIdentitiesKeys() {
20
+ return this.identitiesKeys;
21
+ }
22
+
23
+ /**
24
+ * @param proto
25
+ * @returns {GetIdentitiesContractKeysResponse}
26
+ */
27
+ static createFromProto(proto) {
28
+ const { metadata, proof } = AbstractResponse.createMetadataAndProofFromProto(proto);
29
+
30
+ const identitiesKeys = proto.getV0().getIdentitiesKeys();
31
+
32
+ let identitiesKeysMap = {};
33
+ if (identitiesKeys) {
34
+ const keysEntries = identitiesKeys.getEntriesList();
35
+
36
+ identitiesKeysMap = keysEntries.reduce((acc, entry) => {
37
+ const identityId = Identifier.from(Buffer.from(entry.getIdentityId())).toString();
38
+ if (!acc[identityId]) {
39
+ acc[identityId] = {};
40
+ }
41
+
42
+ entry.getKeysList().forEach((key) => {
43
+ const purpose = key.getPurpose();
44
+ if (!acc[identityId][purpose]) {
45
+ // eslint-disable-next-line no-param-reassign
46
+ acc[identityId][purpose] = [];
47
+ }
48
+
49
+ // eslint-disable-next-line no-param-reassign
50
+ acc[identityId][purpose] = acc[identityId][purpose].concat(key.getKeysBytesList());
51
+ }, {});
52
+
53
+ return acc;
54
+ }, {});
55
+ }
56
+
57
+ return new GetIdentitiesContractKeysResponse(
58
+ identitiesKeysMap,
59
+ metadata,
60
+ proof,
61
+ );
62
+ }
63
+ }
64
+
65
+ module.exports = GetIdentitiesContractKeysResponse;
@@ -0,0 +1,93 @@
1
+ const {
2
+ v0: {
3
+ PlatformPromiseClient,
4
+ GetIdentitiesContractKeysRequest,
5
+ },
6
+ } = require('@dashevo/dapi-grpc');
7
+ const { IdentityPublicKey } = require('@dashevo/wasm-dpp');
8
+
9
+ const GetIdentitiesContractKeysResponse = require('./GetIdentitiesContractKeysResponse');
10
+ const InvalidResponseError = require('../response/errors/InvalidResponseError');
11
+
12
+ /**
13
+ * @param {GrpcTransport} grpcTransport
14
+ * @returns {getIdentitiesContractKeys}
15
+ */
16
+ function getIdentitiesContractKeysFactory(grpcTransport) {
17
+ /**
18
+ * Fetch the identities by public key hashes
19
+ * @typedef {getIdentitiesContractKeys}
20
+ * @param {Buffer[]} identitiesIds
21
+ * @param {Buffer} contractId
22
+ * @param {IdentityPublicKey.PURPOSES[]} keyPurposes
23
+ * @param {string | null} documentTypeName
24
+ * @param {DAPIClientOptions & {prove: boolean}} [options]
25
+ * @returns {Promise<GetIdentitiesContractKeysResponse>}
26
+ */
27
+ async function getIdentitiesContractKeys(
28
+ identitiesIds,
29
+ contractId,
30
+ keyPurposes,
31
+ documentTypeName = null,
32
+ options = {},
33
+ ) {
34
+ const { GetIdentitiesContractKeysRequestV0 } = GetIdentitiesContractKeysRequest;
35
+ const getIdentitiesContractKeysRequest = new GetIdentitiesContractKeysRequest();
36
+
37
+ // eslint-disable-next-line no-param-reassign
38
+ identitiesIds = identitiesIds.map((id) => {
39
+ if (Buffer.isBuffer(id)) {
40
+ // eslint-disable-next-line no-param-reassign
41
+ id = Buffer.from(id);
42
+ }
43
+
44
+ return id;
45
+ });
46
+
47
+ if (Buffer.isBuffer(contractId)) {
48
+ // eslint-disable-next-line no-param-reassign
49
+ contractId = Buffer.from(contractId);
50
+ }
51
+
52
+ getIdentitiesContractKeysRequest.setV0(
53
+ new GetIdentitiesContractKeysRequestV0()
54
+ .setProve(!!options.prove)
55
+ .setIdentitiesIdsList(identitiesIds)
56
+ .setContractId(contractId)
57
+ .setPurposesList(keyPurposes)
58
+ .setDocumentTypeName(documentTypeName),
59
+ );
60
+
61
+ let lastError;
62
+
63
+ // TODO: simple retry before the dapi versioning is properly implemented
64
+ for (let i = 0; i < 3; i += 1) {
65
+ try {
66
+ // eslint-disable-next-line no-await-in-loop
67
+ const getIdentitiesContractKeysResponse = await grpcTransport.request(
68
+ PlatformPromiseClient,
69
+ 'getIdentitiesContractKeys',
70
+ getIdentitiesContractKeysRequest,
71
+ options,
72
+ );
73
+
74
+ return GetIdentitiesContractKeysResponse
75
+ .createFromProto(getIdentitiesContractKeysResponse);
76
+ } catch (e) {
77
+ if (e instanceof InvalidResponseError) {
78
+ lastError = e;
79
+ } else {
80
+ throw e;
81
+ }
82
+ }
83
+ }
84
+
85
+ // If we made it past the cycle it means that the retry didn't work,
86
+ // and we're throwing the last error encountered
87
+ throw lastError;
88
+ }
89
+
90
+ return getIdentitiesContractKeys;
91
+ }
92
+
93
+ module.exports = getIdentitiesContractKeysFactory;
@@ -0,0 +1,40 @@
1
+ const AbstractResponse = require('../response/AbstractResponse');
2
+
3
+ class GetIdentityByPublicKeyHashResponse extends AbstractResponse {
4
+ /**
5
+ * @param {Buffer} identities
6
+ * @param identity
7
+ * @param {Metadata} metadata
8
+ * @param {Proof} [proof]
9
+ */
10
+ constructor(identity, metadata, proof = undefined) {
11
+ super(metadata, proof);
12
+
13
+ this.identity = identity;
14
+ }
15
+
16
+ /**
17
+ * @returns {Buffer[]}
18
+ */
19
+ getIdentity() {
20
+ return this.identity;
21
+ }
22
+
23
+ /**
24
+ * @param proto
25
+ * @returns {GetIdentityByPublicKeyHashResponse}
26
+ */
27
+ static createFromProto(proto) {
28
+ const { metadata, proof } = AbstractResponse.createMetadataAndProofFromProto(proto);
29
+
30
+ const identity = proto.getV0().getIdentity();
31
+
32
+ return new GetIdentityByPublicKeyHashResponse(
33
+ Buffer.from(identity),
34
+ metadata,
35
+ proof,
36
+ );
37
+ }
38
+ }
39
+
40
+ module.exports = GetIdentityByPublicKeyHashResponse;
@@ -0,0 +1,64 @@
1
+ const {
2
+ v0: {
3
+ PlatformPromiseClient,
4
+ GetIdentityByPublicKeyHashRequest,
5
+ },
6
+ } = require('@dashevo/dapi-grpc');
7
+
8
+ const GetIdentityByPublicKeyHashResponse = require('./GetIdentityByPublicKeyHashResponse');
9
+ const InvalidResponseError = require('../response/errors/InvalidResponseError');
10
+
11
+ /**
12
+ * @param {GrpcTransport} grpcTransport
13
+ * @returns {getIdentityByPublicKeyHash}
14
+ */
15
+ function getIdentityByPublicKeyHashFactory(grpcTransport) {
16
+ /**
17
+ * Fetch the identity by public key hash
18
+ * @typedef {getIdentityByPublicKeyHash}
19
+ * @param {Buffer} publicKeyHash
20
+ * @param {DAPIClientOptions & {prove: boolean}} [options]
21
+ * @returns {Promise<GetIdentityByPublicKeyHashResponse>}
22
+ */
23
+ async function getIdentityByPublicKeyHash(publicKeyHash, options = {}) {
24
+ const { GetIdentityByPublicKeyHashRequestV0 } = GetIdentityByPublicKeyHashRequest;
25
+ const getIdentityByPublicKeyHashRequest = new GetIdentityByPublicKeyHashRequest();
26
+ getIdentityByPublicKeyHashRequest.setV0(
27
+ new GetIdentityByPublicKeyHashRequestV0()
28
+ .setPublicKeyHash(publicKeyHash)
29
+ .setProve(!!options.prove),
30
+ );
31
+
32
+ let lastError;
33
+
34
+ // TODO: simple retry before the dapi versioning is properly implemented
35
+ for (let i = 0; i < 3; i += 1) {
36
+ try {
37
+ // eslint-disable-next-line no-await-in-loop
38
+ const getIdentityByPublicKeyHashResponse = await grpcTransport.request(
39
+ PlatformPromiseClient,
40
+ 'getIdentityByPublicKeyHash',
41
+ getIdentityByPublicKeyHashRequest,
42
+ options,
43
+ );
44
+
45
+ return GetIdentityByPublicKeyHashResponse
46
+ .createFromProto(getIdentityByPublicKeyHashResponse);
47
+ } catch (e) {
48
+ if (e instanceof InvalidResponseError) {
49
+ lastError = e;
50
+ } else {
51
+ throw e;
52
+ }
53
+ }
54
+ }
55
+
56
+ // If we made it past the cycle it means that the retry didn't work,
57
+ // and we're throwing the last error encountered
58
+ throw lastError;
59
+ }
60
+
61
+ return getIdentityByPublicKeyHash;
62
+ }
63
+
64
+ module.exports = getIdentityByPublicKeyHashFactory;
@@ -0,0 +1,49 @@
1
+ const AbstractResponse = require('../response/AbstractResponse');
2
+ const InvalidResponseError = require('../response/errors/InvalidResponseError');
3
+
4
+ const IDENTITY_CONTRACT_NONCE_VALUE_FILTER = 0xFFFFFFFFFF;
5
+
6
+ class GetIdentityContractNonceResponse extends AbstractResponse {
7
+ /**
8
+ * @param {number} identityContractNonce
9
+ * @param {Metadata} metadata
10
+ * @param {Proof} [proof]
11
+ */
12
+ constructor(identityContractNonce, metadata, proof = undefined) {
13
+ super(metadata, proof);
14
+
15
+ this.identityContractNonce = identityContractNonce;
16
+ }
17
+
18
+ /**
19
+ * @returns {number}
20
+ */
21
+ getIdentityContractNonce() {
22
+ return this.identityContractNonce;
23
+ }
24
+
25
+ /**
26
+ * @param proto
27
+ * @returns {GetIdentityContractNonceResponse}
28
+ */
29
+ static createFromProto(proto) {
30
+ // eslint-disable-next-line
31
+ const identityContractNonce = proto.getV0()
32
+ .getIdentityContractNonce() & IDENTITY_CONTRACT_NONCE_VALUE_FILTER;
33
+ const { metadata, proof } = AbstractResponse.createMetadataAndProofFromProto(
34
+ proto,
35
+ );
36
+
37
+ if ((typeof identityContractNonce === 'undefined' || identityContractNonce === null) && !proof) {
38
+ throw new InvalidResponseError('Contract nonce data is not defined');
39
+ }
40
+
41
+ return new GetIdentityContractNonceResponse(
42
+ identityContractNonce,
43
+ metadata,
44
+ proof,
45
+ );
46
+ }
47
+ }
48
+
49
+ module.exports = GetIdentityContractNonceResponse;
@@ -0,0 +1,81 @@
1
+ const {
2
+ v0: {
3
+ PlatformPromiseClient,
4
+ GetIdentityContractNonceRequest,
5
+ },
6
+ } = require('@dashevo/dapi-grpc');
7
+
8
+ const GetIdentityContractNonceResponse = require('./GetIdentityContractNonceResponse');
9
+ const InvalidResponseError = require('../response/errors/InvalidResponseError');
10
+
11
+ /**
12
+ * @param {GrpcTransport} grpcTransport
13
+ * @returns {getIdentityContractNonce}
14
+ */
15
+ function getIdentityContractNonceFactory(grpcTransport) {
16
+ /**
17
+ * Fetch the version upgrade votes status
18
+ * @typedef {getIdentityContractNonce}
19
+ * @param {Buffer} identityId
20
+ * @param {Buffer} contractId
21
+ * @param {DAPIClientOptions & {prove: boolean}} [options]
22
+ * @returns {Promise<GetIdentityContractNonceResponse>}
23
+ */
24
+ async function getIdentityContractNonce(identityId, contractId, options = {}) {
25
+ const {
26
+ GetIdentityContractNonceRequestV0,
27
+ } = GetIdentityContractNonceRequest;
28
+
29
+ // eslint-disable-next-line max-len
30
+ const getIdentityContractNonceRequest = new GetIdentityContractNonceRequest();
31
+
32
+ if (Buffer.isBuffer(identityId)) {
33
+ // eslint-disable-next-line no-param-reassign
34
+ identityId = Buffer.from(identityId);
35
+ }
36
+
37
+ if (Buffer.isBuffer(contractId)) {
38
+ // eslint-disable-next-line no-param-reassign
39
+ contractId = Buffer.from(contractId);
40
+ }
41
+
42
+ getIdentityContractNonceRequest.setV0(
43
+ new GetIdentityContractNonceRequestV0()
44
+ .setIdentityId(identityId)
45
+ .setContractId(contractId)
46
+ .setProve(!!options.prove),
47
+ );
48
+
49
+ let lastError;
50
+
51
+ // TODO: simple retry before the dapi versioning is properly implemented
52
+ for (let i = 0; i < 3; i += 1) {
53
+ try {
54
+ // eslint-disable-next-line no-await-in-loop
55
+ const getIdentityContractNonceResponse = await grpcTransport.request(
56
+ PlatformPromiseClient,
57
+ 'getIdentityContractNonce',
58
+ getIdentityContractNonceRequest,
59
+ options,
60
+ );
61
+
62
+ return GetIdentityContractNonceResponse
63
+ .createFromProto(getIdentityContractNonceResponse);
64
+ } catch (e) {
65
+ if (e instanceof InvalidResponseError) {
66
+ lastError = e;
67
+ } else {
68
+ throw e;
69
+ }
70
+ }
71
+ }
72
+
73
+ // If we made it past the cycle it means that the retry didn't work,
74
+ // and we're throwing the last error encountered
75
+ throw lastError;
76
+ }
77
+
78
+ return getIdentityContractNonce;
79
+ }
80
+
81
+ module.exports = getIdentityContractNonceFactory;
@@ -0,0 +1,52 @@
1
+ const AbstractResponse = require('../response/AbstractResponse');
2
+ const InvalidResponseError = require('../response/errors/InvalidResponseError');
3
+
4
+ class GetIdentityKeysResponse extends AbstractResponse {
5
+ /**
6
+ * @param {number} identityKeys
7
+ * @param {Metadata} metadata
8
+ * @param {Proof} [proof]
9
+ */
10
+ constructor(identityKeys, metadata, proof = undefined) {
11
+ super(metadata, proof);
12
+
13
+ this.identityKeys = identityKeys;
14
+ }
15
+
16
+ /**
17
+ * @returns {number}
18
+ */
19
+ getIdentityKeys() {
20
+ return this.identityKeys;
21
+ }
22
+
23
+ /**
24
+ * @param proto
25
+ * @returns {GetIdentityKeysResponse}
26
+ */
27
+ static createFromProto(proto) {
28
+ // eslint-disable-next-line
29
+ const keys = proto.getV0().getKeys();
30
+ const { metadata, proof } = AbstractResponse.createMetadataAndProofFromProto(
31
+ proto,
32
+ );
33
+
34
+ let identityKeys = [];
35
+ if ((typeof keys === 'undefined' || keys === null) && !proof) {
36
+ throw new InvalidResponseError('Identity keys are not defined');
37
+ } else if (!proof) {
38
+ identityKeys = keys.getKeysBytesList();
39
+ if ((typeof identityKeys === 'undefined' || identityKeys === null) && !proof) {
40
+ throw new InvalidResponseError('Identity keys are not defined');
41
+ }
42
+ }
43
+
44
+ return new GetIdentityKeysResponse(
45
+ identityKeys,
46
+ metadata,
47
+ proof,
48
+ );
49
+ }
50
+ }
51
+
52
+ module.exports = GetIdentityKeysResponse;