dash-platform-sdk 1.6.0-dev.2 → 1.6.0-dev.4

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 (125) hide show
  1. package/README.md +33 -15
  2. package/bundle.min.js +1 -1
  3. package/index.d.ts +3 -1
  4. package/index.js +2 -1
  5. package/package.json +2 -2
  6. package/proto/generated/platform.client.d.ts +10 -0
  7. package/proto/generated/platform.client.js +27 -20
  8. package/proto/generated/platform.d.ts +350 -2
  9. package/proto/generated/platform.js +737 -8
  10. package/src/DashPlatformSDK.d.ts +19 -1
  11. package/src/DashPlatformSDK.js +28 -10
  12. package/src/QueryExecutor.d.ts +122 -0
  13. package/src/QueryExecutor.js +205 -0
  14. package/src/constants.d.ts +6 -0
  15. package/src/constants.js +6 -0
  16. package/src/contestedResources/getContestedResourceVoteState.d.ts +2 -1
  17. package/src/contestedResources/getContestedResourceVoteState.js +69 -49
  18. package/src/contestedResources/getVotePollsByEndDate.d.ts +4 -0
  19. package/src/contestedResources/getVotePollsByEndDate.js +39 -0
  20. package/src/contestedResources/index.d.ts +16 -2
  21. package/src/contestedResources/index.js +19 -2
  22. package/src/dataContracts/getDataContractByIdentifier.d.ts +2 -1
  23. package/src/dataContracts/getDataContractByIdentifier.js +15 -27
  24. package/src/dataContracts/index.d.ts +4 -1
  25. package/src/dataContracts/index.js +5 -2
  26. package/src/documents/getActionFeeAgreement.d.ts +2 -1
  27. package/src/documents/getActionFeeAgreement.js +3 -3
  28. package/src/documents/index.d.ts +12 -8
  29. package/src/documents/index.js +16 -13
  30. package/src/documents/query.d.ts +2 -1
  31. package/src/documents/query.js +16 -8
  32. package/src/documents/queryAggregate.d.ts +8 -1
  33. package/src/documents/queryAggregate.js +49 -15
  34. package/src/documents/queryJoined.d.ts +3 -2
  35. package/src/documents/queryJoined.js +54 -13
  36. package/src/documents/queryRanked.d.ts +3 -2
  37. package/src/documents/queryRanked.js +49 -15
  38. package/src/documents/v1.d.ts +25 -8
  39. package/src/documents/v1.js +50 -14
  40. package/src/grpcConnectionPool.d.ts +30 -4
  41. package/src/grpcConnectionPool.js +169 -39
  42. package/src/identities/getIdentityBalance.d.ts +2 -1
  43. package/src/identities/getIdentityBalance.js +13 -27
  44. package/src/identities/getIdentityByIdentifier.d.ts +2 -1
  45. package/src/identities/getIdentityByIdentifier.js +14 -28
  46. package/src/identities/getIdentityByNonUniquePublicKeyHash.d.ts +2 -1
  47. package/src/identities/getIdentityByNonUniquePublicKeyHash.js +49 -28
  48. package/src/identities/getIdentityByPublicKeyHash.d.ts +2 -1
  49. package/src/identities/getIdentityByPublicKeyHash.js +14 -28
  50. package/src/identities/getIdentityContractNonce.d.ts +2 -1
  51. package/src/identities/getIdentityContractNonce.js +10 -27
  52. package/src/identities/getIdentityNonce.d.ts +2 -1
  53. package/src/identities/getIdentityNonce.js +10 -27
  54. package/src/identities/getIdentityPublicKeys.d.ts +2 -1
  55. package/src/identities/getIdentityPublicKeys.js +16 -29
  56. package/src/identities/index.d.ts +4 -1
  57. package/src/identities/index.js +11 -8
  58. package/src/names/index.d.ts +4 -1
  59. package/src/names/index.js +8 -5
  60. package/src/names/registerName.d.ts +2 -1
  61. package/src/names/registerName.js +4 -4
  62. package/src/names/searchByIdentity.d.ts +2 -1
  63. package/src/names/searchByIdentity.js +2 -2
  64. package/src/names/searchByName.d.ts +2 -1
  65. package/src/names/searchByName.js +2 -2
  66. package/src/node/epochInfos.d.ts +2 -1
  67. package/src/node/epochInfos.js +27 -31
  68. package/src/node/finalizedEpochInfos.d.ts +4 -3
  69. package/src/node/finalizedEpochInfos.js +45 -39
  70. package/src/node/index.d.ts +4 -1
  71. package/src/node/index.js +7 -4
  72. package/src/node/totalCredits.d.ts +2 -1
  73. package/src/node/totalCredits.js +10 -25
  74. package/src/platformAddresses/getAddressInfo.d.ts +2 -1
  75. package/src/platformAddresses/getAddressInfo.js +17 -31
  76. package/src/platformAddresses/getAddressesBranchState.d.ts +2 -1
  77. package/src/platformAddresses/getAddressesBranchState.js +22 -8
  78. package/src/platformAddresses/getAddressesInfos.d.ts +2 -1
  79. package/src/platformAddresses/getAddressesInfos.js +18 -29
  80. package/src/platformAddresses/getAddressesTrunkState.d.ts +2 -1
  81. package/src/platformAddresses/getAddressesTrunkState.js +25 -23
  82. package/src/platformAddresses/getRecentAddressBalanceChanges.d.ts +2 -1
  83. package/src/platformAddresses/getRecentAddressBalanceChanges.js +26 -26
  84. package/src/platformAddresses/getRecentCompactedAddressBalanceChanges.d.ts +2 -1
  85. package/src/platformAddresses/getRecentCompactedAddressBalanceChanges.js +30 -26
  86. package/src/platformAddresses/index.d.ts +4 -1
  87. package/src/platformAddresses/index.js +10 -7
  88. package/src/quorumKeyProvider.d.ts +23 -0
  89. package/src/quorumKeyProvider.js +28 -0
  90. package/src/shielded/getMostRecentShieldedAnchor.d.ts +2 -1
  91. package/src/shielded/getMostRecentShieldedAnchor.js +8 -25
  92. package/src/shielded/getShieldedAnchors.d.ts +2 -1
  93. package/src/shielded/getShieldedAnchors.js +8 -25
  94. package/src/shielded/getShieldedEncryptedNotes.d.ts +2 -1
  95. package/src/shielded/getShieldedEncryptedNotes.js +16 -30
  96. package/src/shielded/getShieldedNotesCount.d.ts +2 -1
  97. package/src/shielded/getShieldedNotesCount.js +8 -25
  98. package/src/shielded/getShieldedNullifiers.d.ts +2 -1
  99. package/src/shielded/getShieldedNullifiers.js +14 -28
  100. package/src/shielded/getShieldedPoolState.d.ts +2 -1
  101. package/src/shielded/getShieldedPoolState.js +8 -25
  102. package/src/shielded/index.d.ts +4 -1
  103. package/src/shielded/index.js +10 -7
  104. package/src/stateTransitions/index.d.ts +4 -1
  105. package/src/stateTransitions/index.js +5 -2
  106. package/src/stateTransitions/waitForStateTransitionResult.d.ts +2 -1
  107. package/src/stateTransitions/waitForStateTransitionResult.js +42 -44
  108. package/src/tokens/getIdentitiesTokenBalances.d.ts +2 -1
  109. package/src/tokens/getIdentitiesTokenBalances.js +17 -28
  110. package/src/tokens/getIdentityTokensBalances.d.ts +2 -1
  111. package/src/tokens/getIdentityTokensBalances.js +17 -28
  112. package/src/tokens/getTokenContractInfo.d.ts +2 -1
  113. package/src/tokens/getTokenContractInfo.js +16 -30
  114. package/src/tokens/getTokenDirectPurchasePrices.d.ts +2 -1
  115. package/src/tokens/getTokenDirectPurchasePrices.js +26 -28
  116. package/src/tokens/getTokenTotalSupply.d.ts +2 -1
  117. package/src/tokens/getTokenTotalSupply.js +16 -28
  118. package/src/tokens/index.d.ts +4 -1
  119. package/src/tokens/index.js +11 -8
  120. package/src/utils/ProofError.d.ts +9 -0
  121. package/src/utils/ProofError.js +10 -0
  122. package/src/utils/verifyTenderdashProof.js +2 -1
  123. package/test/unit/SDK.spec.js +42 -0
  124. package/src/utils/getQuorumPublicKey.d.ts +0 -1
  125. package/src/utils/getQuorumPublicKey.js +0 -19
@@ -0,0 +1,39 @@
1
+ import { VotePollWASM, verifyVotePollsByEndDateProof } from 'pshenmic-dpp';
2
+ import { GetVotePollsByEndDateRequest } from '../../proto/generated/platform.js';
3
+ import QueryExecutor from '../QueryExecutor.js';
4
+ import { LATEST_PLATFORM_VERSION } from '../constants.js';
5
+ export default async function getVotePollsByEndDate(grpcPool, quorumKeyProvider, startTime, endTime, limit, ascending = true) {
6
+ const getVotePollsByEndDateRequest = GetVotePollsByEndDateRequest.create({
7
+ version: {
8
+ oneofKind: 'v0',
9
+ v0: {
10
+ startTimeInfo: startTime != null
11
+ ? { startTimeMs: startTime.timeMs.toString(), startTimeIncluded: startTime.included }
12
+ : undefined,
13
+ endTimeInfo: endTime != null
14
+ ? { endTimeMs: endTime.timeMs.toString(), endTimeIncluded: endTime.included }
15
+ : undefined,
16
+ limit,
17
+ ascending,
18
+ prove: true
19
+ }
20
+ }
21
+ });
22
+ return await QueryExecutor.build(grpcPool, quorumKeyProvider)
23
+ .setGrpcRequest('getVotePollsByEndDate', getVotePollsByEndDateRequest)
24
+ .setProofVerifier(verifyVotePollsByEndDateProof, startTime, endTime, limit, ascending, LATEST_PLATFORM_VERSION)
25
+ .mapResponse(data => {
26
+ if (data.proved) {
27
+ return data.verified.votePolls;
28
+ }
29
+ const { result } = data;
30
+ if (result.oneofKind !== 'votePollsByTimestamps') {
31
+ return [];
32
+ }
33
+ return result.votePollsByTimestamps.votePollsByTimestamps.map(group => ({
34
+ timestampMs: BigInt(group.timestamp),
35
+ votePolls: group.serializedVotePolls.map(bytes => VotePollWASM.fromBytes(bytes))
36
+ }));
37
+ })
38
+ .execute();
39
+ }
@@ -1,6 +1,7 @@
1
1
  import { GRPCPool } from '../grpcConnectionPool.js';
2
+ import { QuorumKeyProvider } from '../quorumKeyProvider.js';
2
3
  import { ContestedResourceVoteState, ContestedStateResultType } from '../../types.js';
3
- import { DataContractWASM } from 'pshenmic-dpp';
4
+ import { DataContractWASM, VotePollsAtEndDate, VotePollsByEndDateTimeBound } from 'pshenmic-dpp';
4
5
  import { StartAtIdentifierInfo } from './getContestedResourceVoteState.js';
5
6
  /**
6
7
  * Contested Resources controller for requesting information about contested resources
@@ -10,7 +11,9 @@ import { StartAtIdentifierInfo } from './getContestedResourceVoteState.js';
10
11
  export declare class ContestedResourcesController {
11
12
  /** @ignore **/
12
13
  grpcPool: GRPCPool;
13
- constructor(grpcPool: GRPCPool);
14
+ /** @ignore **/
15
+ quorumKeyProvider: QuorumKeyProvider;
16
+ constructor(grpcPool: GRPCPool, quorumKeyProvider: QuorumKeyProvider);
14
17
  /**
15
18
  * Retrieves an info about vote state for contested resource
16
19
  *
@@ -26,4 +29,15 @@ export declare class ContestedResourcesController {
26
29
  * @return {Promise<ContestedResourceVoteState>}
27
30
  */
28
31
  getContestedResourceVoteState(contract: DataContractWASM, documentTypeName: string, indexName: string, indexValuesBytes: Uint8Array[], resultType: ContestedStateResultType, allowIncludeLockedAndAbstainingVoteTally: boolean, startAtIdentifierInfo?: StartAtIdentifierInfo, count?: number): Promise<ContestedResourceVoteState>;
32
+ /**
33
+ * Retrieves vote polls grouped by their end time
34
+ *
35
+ * @param startTime {VotePollsByEndDateTimeBound=} - lower bound of the end time in milliseconds
36
+ * @param endTime {VotePollsByEndDateTimeBound=} - upper bound of the end time in milliseconds
37
+ * @param limit {number=}
38
+ * @param ascending {boolean=} - order by end time, ascending by default
39
+ *
40
+ * @return {Promise<VotePollsAtEndDate[]>}
41
+ */
42
+ getVotePollsByEndDate(startTime?: VotePollsByEndDateTimeBound, endTime?: VotePollsByEndDateTimeBound, limit?: number, ascending?: boolean): Promise<VotePollsAtEndDate[]>;
29
43
  }
@@ -1,4 +1,5 @@
1
1
  import getContestedResourceVoteState from './getContestedResourceVoteState.js';
2
+ import getVotePollsByEndDate from './getVotePollsByEndDate.js';
2
3
  /**
3
4
  * Contested Resources controller for requesting information about contested resources
4
5
  *
@@ -7,8 +8,11 @@ import getContestedResourceVoteState from './getContestedResourceVoteState.js';
7
8
  export class ContestedResourcesController {
8
9
  /** @ignore **/
9
10
  grpcPool;
10
- constructor(grpcPool) {
11
+ /** @ignore **/
12
+ quorumKeyProvider;
13
+ constructor(grpcPool, quorumKeyProvider) {
11
14
  this.grpcPool = grpcPool;
15
+ this.quorumKeyProvider = quorumKeyProvider;
12
16
  }
13
17
  /**
14
18
  * Retrieves an info about vote state for contested resource
@@ -25,6 +29,19 @@ export class ContestedResourcesController {
25
29
  * @return {Promise<ContestedResourceVoteState>}
26
30
  */
27
31
  async getContestedResourceVoteState(contract, documentTypeName, indexName, indexValuesBytes, resultType, allowIncludeLockedAndAbstainingVoteTally, startAtIdentifierInfo, count) {
28
- return await getContestedResourceVoteState(this.grpcPool, contract, documentTypeName, indexName, indexValuesBytes, resultType, allowIncludeLockedAndAbstainingVoteTally, startAtIdentifierInfo, count);
32
+ return await getContestedResourceVoteState(this.grpcPool, this.quorumKeyProvider, contract, documentTypeName, indexName, indexValuesBytes, resultType, allowIncludeLockedAndAbstainingVoteTally, startAtIdentifierInfo, count);
33
+ }
34
+ /**
35
+ * Retrieves vote polls grouped by their end time
36
+ *
37
+ * @param startTime {VotePollsByEndDateTimeBound=} - lower bound of the end time in milliseconds
38
+ * @param endTime {VotePollsByEndDateTimeBound=} - upper bound of the end time in milliseconds
39
+ * @param limit {number=}
40
+ * @param ascending {boolean=} - order by end time, ascending by default
41
+ *
42
+ * @return {Promise<VotePollsAtEndDate[]>}
43
+ */
44
+ async getVotePollsByEndDate(startTime, endTime, limit, ascending = true) {
45
+ return await getVotePollsByEndDate(this.grpcPool, this.quorumKeyProvider, startTime, endTime, limit, ascending);
29
46
  }
30
47
  }
@@ -1,3 +1,4 @@
1
1
  import { DataContractWASM, IdentifierLike } from 'pshenmic-dpp';
2
2
  import { GRPCPool } from '../grpcConnectionPool.js';
3
- export default function getByIdentifier(grpcPool: GRPCPool, identifier: IdentifierLike): Promise<DataContractWASM>;
3
+ import { QuorumKeyProvider } from '../quorumKeyProvider.js';
4
+ export default function getByIdentifier(grpcPool: GRPCPool, quorumKeyProvider: QuorumKeyProvider, identifier: IdentifierLike): Promise<DataContractWASM>;
@@ -1,10 +1,8 @@
1
1
  import { DataContractWASM, IdentifierWASM, verifyContractProof } from 'pshenmic-dpp';
2
2
  import { GetDataContractRequest } from '../../proto/generated/platform.js';
3
- import { getQuorumPublicKey } from '../utils/getQuorumPublicKey.js';
4
- import bytesToHex from '../utils/bytesToHex.js';
5
- import verifyTenderdashProof from '../utils/verifyTenderdashProof.js';
3
+ import QueryExecutor from '../QueryExecutor.js';
6
4
  import { DPNS_DATA_CONTRACT_BYTES, DPNS_DATA_CONTRACT_ID, LATEST_PLATFORM_VERSION } from '../constants.js';
7
- export default async function getByIdentifier(grpcPool, identifier) {
5
+ export default async function getByIdentifier(grpcPool, quorumKeyProvider, identifier) {
8
6
  const id = new IdentifierWASM(identifier);
9
7
  if (id.base58() === DPNS_DATA_CONTRACT_ID) {
10
8
  return DataContractWASM.fromBase64(DPNS_DATA_CONTRACT_BYTES, true, 9);
@@ -18,27 +16,17 @@ export default async function getByIdentifier(grpcPool, identifier) {
18
16
  }
19
17
  }
20
18
  });
21
- const { response } = await (await grpcPool.getClient()).getDataContract(getDataContractRequest);
22
- const { version } = response;
23
- if (version.oneofKind !== 'v0') {
24
- throw new Error('Unexpected oneOf type returned from DAPI (must be v0)');
25
- }
26
- const { v0 } = version;
27
- if (v0.result.oneofKind !== 'proof') {
28
- throw new Error('Unexpected oneOf type returned from DAPI (must be proof)');
29
- }
30
- const { result: { proof }, metadata } = v0;
31
- if (metadata == null) {
32
- throw new Error('Metadata not found');
33
- }
34
- const { rootHash, dataContract } = verifyContractProof(proof.grovedbProof, undefined, false, false, id.bytes(), LATEST_PLATFORM_VERSION);
35
- if (dataContract == null) {
36
- throw new Error(`Data Contract with identifier ${id.base58()} not found`);
37
- }
38
- const quorumPublicKey = await getQuorumPublicKey(grpcPool.network, proof.quorumType, bytesToHex(proof.quorumHash));
39
- const verify = await verifyTenderdashProof(proof, metadata, rootHash, quorumPublicKey);
40
- if (!verify) {
41
- throw new Error('Failed to verify query');
42
- }
43
- return dataContract;
19
+ return await QueryExecutor.build(grpcPool, quorumKeyProvider)
20
+ .setGrpcRequest('getDataContract', getDataContractRequest)
21
+ .setProofVerifier(verifyContractProof, undefined, false, false, id.bytes(), LATEST_PLATFORM_VERSION)
22
+ .mapResponse(data => {
23
+ const dataContract = data.proved
24
+ ? data.verified.dataContract
25
+ : DataContractWASM.fromBytes(data.result.dataContract, false, LATEST_PLATFORM_VERSION);
26
+ if (dataContract == null) {
27
+ throw new Error(`Data Contract with identifier ${id.base58()} not found`);
28
+ }
29
+ return dataContract;
30
+ })
31
+ .execute();
44
32
  }
@@ -1,5 +1,6 @@
1
1
  import { DataContractTokens, DataContractWASM, IdentifierLike, PlatformVersionWASM, StateTransitionWASM } from 'pshenmic-dpp';
2
2
  import { GRPCPool } from '../grpcConnectionPool.js';
3
+ import { QuorumKeyProvider } from '../quorumKeyProvider.js';
3
4
  import { DataContractConfig } from '../../types.js';
4
5
  /**
5
6
  * Collection of methods necessary to work with Data Contracts in the network,
@@ -10,7 +11,9 @@ import { DataContractConfig } from '../../types.js';
10
11
  export declare class DataContractsController {
11
12
  /** @ignore */
12
13
  grpcPool: GRPCPool;
13
- constructor(grpcPool: GRPCPool);
14
+ /** @ignore */
15
+ quorumKeyProvider: QuorumKeyProvider;
16
+ constructor(grpcPool: GRPCPool, quorumKeyProvider: QuorumKeyProvider);
14
17
  /**
15
18
  * Creates an instance of {DataContractWASM} that can be used to
16
19
  * register a data contract in the network.
@@ -10,8 +10,11 @@ import createStateTransition from './createStateTransition.js';
10
10
  export class DataContractsController {
11
11
  /** @ignore */
12
12
  grpcPool;
13
- constructor(grpcPool) {
13
+ /** @ignore */
14
+ quorumKeyProvider;
15
+ constructor(grpcPool, quorumKeyProvider) {
14
16
  this.grpcPool = grpcPool;
17
+ this.quorumKeyProvider = quorumKeyProvider;
15
18
  }
16
19
  /**
17
20
  * Creates an instance of {DataContractWASM} that can be used to
@@ -38,7 +41,7 @@ export class DataContractsController {
38
41
  * @return {Promise<DataContractWASM>}
39
42
  */
40
43
  async getDataContractByIdentifier(identifier) {
41
- return await getDataContractByIdentifier(this.grpcPool, identifier);
44
+ return await getDataContractByIdentifier(this.grpcPool, this.quorumKeyProvider, identifier);
42
45
  }
43
46
  /**
44
47
  * Helper function to create a state transition from a {DataContractWASM} instance
@@ -1,4 +1,5 @@
1
1
  import { DocumentActionFeeAgreementWASM, IdentifierLike } from 'pshenmic-dpp';
2
2
  import { GRPCPool } from '../grpcConnectionPool.js';
3
+ import { QuorumKeyProvider } from '../quorumKeyProvider.js';
3
4
  import { DocumentTransitionType } from '../../types.js';
4
- export default function getActionFeeAgreement(grpcPool: GRPCPool, dataContractId: IdentifierLike, documentTypeName: string, action: DocumentTransitionType, increaseTolerancePercent: number): Promise<DocumentActionFeeAgreementWASM | undefined>;
5
+ export default function getActionFeeAgreement(grpcPool: GRPCPool, quorumKeyProvider: QuorumKeyProvider, dataContractId: IdentifierLike, documentTypeName: string, action: DocumentTransitionType, increaseTolerancePercent: number): Promise<DocumentActionFeeAgreementWASM | undefined>;
@@ -9,10 +9,10 @@ const BATCH_TYPES = {
9
9
  transfer: 'Transfer',
10
10
  purchase: 'Purchase'
11
11
  };
12
- export default async function getActionFeeAgreement(grpcPool, dataContractId, documentTypeName, action, increaseTolerancePercent) {
13
- const dataContract = await getDataContractByIdentifier(grpcPool, dataContractId);
12
+ export default async function getActionFeeAgreement(grpcPool, quorumKeyProvider, dataContractId, documentTypeName, action, increaseTolerancePercent) {
13
+ const dataContract = await getDataContractByIdentifier(grpcPool, quorumKeyProvider, dataContractId);
14
14
  // the current epoch: descending from the latest one
15
- const [currentEpoch] = await getEpochsInfo(grpcPool, 1, false);
15
+ const [currentEpoch] = await getEpochsInfo(grpcPool, quorumKeyProvider, 1, false);
16
16
  if (currentEpoch == null) {
17
17
  throw new Error('Current epoch not found');
18
18
  }
@@ -1,6 +1,8 @@
1
1
  import { DocumentTransitionParams, DocumentTransitionType } from '../../types.js';
2
- import { AggregateDocumentsQuery, AggregateProofQuery, CompositeSubQuery, DocumentActionFeeAgreementWASM, DocumentsQuery, DocumentWASM, HavingClause, IdentifierLike, IdentifierWASM, ProvenAggregate, ProvenRankedEntry, ProvenRankedPage, StateTransitionWASM, WhereClause } from 'pshenmic-dpp';
2
+ import { AggregateDocumentsQuery, AggregateProofQuery, CompositeSubQuery, DocumentActionFeeAgreementWASM, DocumentsQuery, DocumentWASM, HavingClause, IdentifierLike, IdentifierWASM, ProvenRankedEntry, ProvenRankedPage, StateTransitionWASM, WhereClause } from 'pshenmic-dpp';
3
3
  import { GRPCPool } from '../grpcConnectionPool.js';
4
+ import { QuorumKeyProvider } from '../quorumKeyProvider.js';
5
+ import { DocumentsAggregate } from './queryAggregate.js';
4
6
  import { ChainedDocuments, CompositeDocuments } from './queryJoined.js';
5
7
  /**
6
8
  * Collection of methods to work with documents like creation, querying or preparing a transition action
@@ -10,7 +12,9 @@ import { ChainedDocuments, CompositeDocuments } from './queryJoined.js';
10
12
  export declare class DocumentsController {
11
13
  /** @ignore **/
12
14
  grpcPool: GRPCPool;
13
- constructor(grpcPool: GRPCPool);
15
+ /** @ignore **/
16
+ quorumKeyProvider: QuorumKeyProvider;
17
+ constructor(grpcPool: GRPCPool, quorumKeyProvider: QuorumKeyProvider);
14
18
  /**
15
19
  * Creates a {DocumentWASM} instance that can be used to submit a document in the network.
16
20
  *
@@ -43,9 +47,9 @@ export declare class DocumentsController {
43
47
  * @param dataContractId {IdentifierLike} Identifier of the Data Contract
44
48
  * @param query {AggregateProofQuery} documentTypeName, where, orderBy, groupBy and limit of the query
45
49
  *
46
- * @return {Promise<ProvenAggregate>}
50
+ * @return {Promise<DocumentsAggregate>}
47
51
  */
48
- count(dataContractId: IdentifierLike, query: AggregateProofQuery): Promise<ProvenAggregate>;
52
+ count(dataContractId: IdentifierLike, query: AggregateProofQuery): Promise<DocumentsAggregate>;
49
53
  /**
50
54
  * Sums a field over documents matching a query. Without `groupBy` returns one entry with the total,
51
55
  * with `groupBy` one entry per group
@@ -54,9 +58,9 @@ export declare class DocumentsController {
54
58
  * @param query {AggregateProofQuery} documentTypeName, where, orderBy, groupBy and limit of the query
55
59
  * @param field {string} Integer field to sum
56
60
  *
57
- * @return {Promise<ProvenAggregate>}
61
+ * @return {Promise<DocumentsAggregate>}
58
62
  */
59
- sum(dataContractId: IdentifierLike, query: AggregateProofQuery, field: string): Promise<ProvenAggregate>;
63
+ sum(dataContractId: IdentifierLike, query: AggregateProofQuery, field: string): Promise<DocumentsAggregate>;
60
64
  /**
61
65
  * Averages a field over documents matching a query. Entries carry `count` and `sum`, the average is `sum / count`.
62
66
  * Without `groupBy` returns one entry with the total, with `groupBy` one entry per group
@@ -65,9 +69,9 @@ export declare class DocumentsController {
65
69
  * @param query {AggregateProofQuery} documentTypeName, where, orderBy, groupBy and limit of the query
66
70
  * @param field {string} Integer field to average
67
71
  *
68
- * @return {Promise<ProvenAggregate>}
72
+ * @return {Promise<DocumentsAggregate>}
69
73
  */
70
- average(dataContractId: IdentifierLike, query: AggregateProofQuery, field: string): Promise<ProvenAggregate>;
74
+ average(dataContractId: IdentifierLike, query: AggregateProofQuery, field: string): Promise<DocumentsAggregate>;
71
75
  /**
72
76
  * Top-K query: groups of `groupBy` ranked by the selected aggregate. Order by the selected field,
73
77
  * or by `$count` for count, e.g. `orderBy: [['$count', 'desc']]`. Supports `limit` and `offset`
@@ -14,8 +14,11 @@ import { queryChained, queryComposite } from './queryJoined.js';
14
14
  export class DocumentsController {
15
15
  /** @ignore **/
16
16
  grpcPool;
17
- constructor(grpcPool) {
17
+ /** @ignore **/
18
+ quorumKeyProvider;
19
+ constructor(grpcPool, quorumKeyProvider) {
18
20
  this.grpcPool = grpcPool;
21
+ this.quorumKeyProvider = quorumKeyProvider;
19
22
  }
20
23
  /**
21
24
  * Creates a {DocumentWASM} instance that can be used to submit a document in the network.
@@ -47,7 +50,7 @@ export class DocumentsController {
47
50
  if (startAfter != null && startAt != null) {
48
51
  throw new Error('You may only set either startAfter or startAt at once');
49
52
  }
50
- return await query(this.grpcPool, new IdentifierWASM(dataContractId), documentType, where, orderBy, limit ?? 100, startAt, startAfter);
53
+ return await query(this.grpcPool, this.quorumKeyProvider, new IdentifierWASM(dataContractId), documentType, where, orderBy, limit ?? 100, startAt, startAfter);
51
54
  }
52
55
  /**
53
56
  * Counts documents matching a query. Without `groupBy` returns one entry with the total,
@@ -56,10 +59,10 @@ export class DocumentsController {
56
59
  * @param dataContractId {IdentifierLike} Identifier of the Data Contract
57
60
  * @param query {AggregateProofQuery} documentTypeName, where, orderBy, groupBy and limit of the query
58
61
  *
59
- * @return {Promise<ProvenAggregate>}
62
+ * @return {Promise<DocumentsAggregate>}
60
63
  */
61
64
  async count(dataContractId, query) {
62
- return await queryAggregate(this.grpcPool, dataContractId, 'count', query);
65
+ return await queryAggregate(this.grpcPool, this.quorumKeyProvider, dataContractId, 'count', query);
63
66
  }
64
67
  /**
65
68
  * Sums a field over documents matching a query. Without `groupBy` returns one entry with the total,
@@ -69,10 +72,10 @@ export class DocumentsController {
69
72
  * @param query {AggregateProofQuery} documentTypeName, where, orderBy, groupBy and limit of the query
70
73
  * @param field {string} Integer field to sum
71
74
  *
72
- * @return {Promise<ProvenAggregate>}
75
+ * @return {Promise<DocumentsAggregate>}
73
76
  */
74
77
  async sum(dataContractId, query, field) {
75
- return await queryAggregate(this.grpcPool, dataContractId, 'sum', query, field);
78
+ return await queryAggregate(this.grpcPool, this.quorumKeyProvider, dataContractId, 'sum', query, field);
76
79
  }
77
80
  /**
78
81
  * Averages a field over documents matching a query. Entries carry `count` and `sum`, the average is `sum / count`.
@@ -82,10 +85,10 @@ export class DocumentsController {
82
85
  * @param query {AggregateProofQuery} documentTypeName, where, orderBy, groupBy and limit of the query
83
86
  * @param field {string} Integer field to average
84
87
  *
85
- * @return {Promise<ProvenAggregate>}
88
+ * @return {Promise<DocumentsAggregate>}
86
89
  */
87
90
  async average(dataContractId, query, field) {
88
- return await queryAggregate(this.grpcPool, dataContractId, 'avg', query, field);
91
+ return await queryAggregate(this.grpcPool, this.quorumKeyProvider, dataContractId, 'avg', query, field);
89
92
  }
90
93
  /**
91
94
  * Top-K query: groups of `groupBy` ranked by the selected aggregate. Order by the selected field,
@@ -97,7 +100,7 @@ export class DocumentsController {
97
100
  * @return {Promise<ProvenRankedPage>}
98
101
  */
99
102
  async ranked(dataContractId, query) {
100
- return await queryRanked(this.grpcPool, dataContractId, query);
103
+ return await queryRanked(this.grpcPool, this.quorumKeyProvider, dataContractId, query);
101
104
  }
102
105
  /**
103
106
  * Having query: groups of `groupBy` whose aggregate falls within the `having` bounds
@@ -108,7 +111,7 @@ export class DocumentsController {
108
111
  * @return {Promise<ProvenRankedEntry[]>}
109
112
  */
110
113
  async having(dataContractId, query) {
111
- return await queryHaving(this.grpcPool, dataContractId, query);
114
+ return await queryHaving(this.grpcPool, this.quorumKeyProvider, dataContractId, query);
112
115
  }
113
116
  /**
114
117
  * Chained join: documents of the query, then the documents of `outerDocumentType`
@@ -122,7 +125,7 @@ export class DocumentsController {
122
125
  * @return {Promise<ChainedDocuments>}
123
126
  */
124
127
  async chained(dataContractId, query, joinProperty, outerDocumentType) {
125
- return await queryChained(this.grpcPool, dataContractId, query, joinProperty, outerDocumentType);
128
+ return await queryChained(this.grpcPool, this.quorumKeyProvider, dataContractId, query, joinProperty, outerDocumentType);
126
129
  }
127
130
  /**
128
131
  * Composite query: a page of documents plus up to 10 sub-queries, each optionally bound
@@ -135,7 +138,7 @@ export class DocumentsController {
135
138
  * @return {Promise<CompositeDocuments>}
136
139
  */
137
140
  async composite(dataContractId, query, subQueries) {
138
- return await queryComposite(this.grpcPool, dataContractId, query, subQueries);
141
+ return await queryComposite(this.grpcPool, this.quorumKeyProvider, dataContractId, query, subQueries);
139
142
  }
140
143
  /**
141
144
  * Helper function for creating {StateTransitionWASM} from documents. It may be used to create any of 6 Document Transition actions:
@@ -192,6 +195,6 @@ export class DocumentsController {
192
195
  * @return {Promise<DocumentActionFeeAgreementWASM | undefined>}
193
196
  */
194
197
  async getActionFeeAgreement(dataContractId, documentType, action, increaseTolerancePercent) {
195
- return await getActionFeeAgreement(this.grpcPool, dataContractId, documentType, action, increaseTolerancePercent ?? 0);
198
+ return await getActionFeeAgreement(this.grpcPool, this.quorumKeyProvider, dataContractId, documentType, action, increaseTolerancePercent ?? 0);
196
199
  }
197
200
  }
@@ -1,3 +1,4 @@
1
1
  import { DocumentWASM, IdentifierLike, IdentifierWASM, WhereClause } from 'pshenmic-dpp';
2
2
  import { GRPCPool } from '../grpcConnectionPool.js';
3
- export default function query(grpcPool: GRPCPool, dataContractId: IdentifierLike, documentTypeName: string, where?: WhereClause[], orderBy?: string[][], limit?: number | undefined, startAt?: IdentifierWASM, startAfter?: IdentifierWASM): Promise<DocumentWASM[]>;
3
+ import { QuorumKeyProvider } from '../quorumKeyProvider.js';
4
+ export default function query(grpcPool: GRPCPool, quorumKeyProvider: QuorumKeyProvider, dataContractId: IdentifierLike, documentTypeName: string, where?: WhereClause[], orderBy?: string[][], limit?: number | undefined, startAt?: IdentifierWASM, startAfter?: IdentifierWASM): Promise<DocumentWASM[]>;
@@ -1,12 +1,13 @@
1
1
  import { IdentifierWASM, verifyDocumentsProof } from 'pshenmic-dpp';
2
2
  import { DAPI_DEFAULT_LIMIT, LATEST_PLATFORM_VERSION } from '../constants.js';
3
3
  import getDataContractByIdentifier from '../dataContracts/getDataContractByIdentifier.js';
4
- import { getDocumentsV1Proof, toOrderClauses, toStart, toWhereClauses, verifyDocumentsV1Signature } from './v1.js';
5
- export default async function query(grpcPool, dataContractId, documentTypeName, where, orderBy, limit = 100, startAt, startAfter) {
6
- const dataContract = await getDataContractByIdentifier(grpcPool, dataContractId);
4
+ import QueryExecutor from '../QueryExecutor.js';
5
+ import { createDocumentsV1Request, getDocumentsV1Data, getDocumentsV1Proof, toDocuments, toOrderClauses, toStart, toWhereClauses } from './v1.js';
6
+ export default async function query(grpcPool, quorumKeyProvider, dataContractId, documentTypeName, where, orderBy, limit = 100, startAt, startAfter) {
7
+ const dataContract = await getDataContractByIdentifier(grpcPool, quorumKeyProvider, dataContractId);
7
8
  const startIdentifier = startAt ?? startAfter;
8
9
  const startAtIncluded = startAt != null;
9
- const { proof, metadata } = await getDocumentsV1Proof(grpcPool, {
10
+ const request = createDocumentsV1Request({
10
11
  dataContractId: (new IdentifierWASM(dataContractId)).bytes(),
11
12
  documentType: documentTypeName,
12
13
  whereClauses: toWhereClauses(where),
@@ -14,8 +15,15 @@ export default async function query(grpcPool, dataContractId, documentTypeName,
14
15
  limit: limit ?? DAPI_DEFAULT_LIMIT,
15
16
  start: toStart(startIdentifier, startAtIncluded)
16
17
  });
17
- const metadataTimeMs = BigInt(metadata.timeMs);
18
- const { rootHash, documents } = verifyDocumentsProof(proof.grovedbProof, dataContract, documentTypeName, where, orderBy, limit, startIdentifier?.bytes(), startAtIncluded, metadataTimeMs, LATEST_PLATFORM_VERSION, metadataTimeMs);
19
- await verifyDocumentsV1Signature(grpcPool, proof, metadata, rootHash);
20
- return documents ?? [];
18
+ return await QueryExecutor.build(grpcPool, quorumKeyProvider)
19
+ .setGrpcRequest('getDocuments', request)
20
+ .mapProof(getDocumentsV1Proof)
21
+ .setProofVerifier(verifyDocumentsProof, ({ metadata }) => {
22
+ const metadataTimeMs = BigInt(metadata.timeMs);
23
+ return [dataContract, documentTypeName, where, orderBy, limit, startIdentifier?.bytes(), startAtIncluded, metadataTimeMs, LATEST_PLATFORM_VERSION, metadataTimeMs];
24
+ })
25
+ .mapResponse(data => data.proved
26
+ ? data.verified.documents ?? []
27
+ : toDocuments(getDocumentsV1Data(data.result, 'documents').documents, dataContract, documentTypeName))
28
+ .execute();
21
29
  }
@@ -1,4 +1,11 @@
1
1
  import { AggregateProofQuery, IdentifierLike, ProvenAggregate } from 'pshenmic-dpp';
2
2
  import { GRPCPool } from '../grpcConnectionPool.js';
3
+ import { QuorumKeyProvider } from '../quorumKeyProvider.js';
3
4
  export type DocumentsAggregateFunction = 'count' | 'sum' | 'avg';
4
- export default function queryAggregate(grpcPool: GRPCPool, dataContractId: IdentifierLike, aggregateFunction: DocumentsAggregateFunction, query: AggregateProofQuery, field?: string): Promise<ProvenAggregate>;
5
+ /**
6
+ * Aggregate of the documents, `mode` (the shape of the proof) is known only with proof
7
+ */
8
+ export type DocumentsAggregate = Omit<ProvenAggregate, 'mode'> & {
9
+ mode?: ProvenAggregate['mode'];
10
+ };
11
+ export default function queryAggregate(grpcPool: GRPCPool, quorumKeyProvider: QuorumKeyProvider, dataContractId: IdentifierLike, aggregateFunction: DocumentsAggregateFunction, query: AggregateProofQuery, field?: string): Promise<DocumentsAggregate>;
@@ -1,13 +1,46 @@
1
1
  import { IdentifierWASM, verifyDocumentsAverageProof, verifyDocumentsCountProof, verifyDocumentsSumProof } from 'pshenmic-dpp';
2
2
  import { LATEST_PLATFORM_VERSION } from '../constants.js';
3
3
  import getDataContractByIdentifier from '../dataContracts/getDataContractByIdentifier.js';
4
- import { getDocumentsV1Proof, toOrderClauses, toSelect, toWhereClauses, verifyDocumentsV1Signature } from './v1.js';
5
- export default async function queryAggregate(grpcPool, dataContractId, aggregateFunction, query, field) {
4
+ import QueryExecutor from '../QueryExecutor.js';
5
+ import { createDocumentsV1Request, getDocumentsV1Data, getDocumentsV1Proof, toOrderClauses, toSelect, toWhereClauses } from './v1.js';
6
+ /**
7
+ * Converts the aggregate of a response requested without proof to the one of the proof verification.
8
+ * Aggregate without grouping is returned as a single entry with an empty key, groups without documents
9
+ * are skipped like in the proof. The `in` value of a carrier aggregate (`inKey`) is not returned without proof
10
+ */
11
+ function toDocumentsAggregate(result, aggregateFunction) {
12
+ const toEntry = ({ inKey, key, count, sum }) => ({
13
+ inKey: inKey ?? null,
14
+ key: key ?? new Uint8Array(),
15
+ count: count != null ? BigInt(count) : undefined,
16
+ sum: sum != null ? BigInt(sum) : undefined
17
+ });
18
+ if (aggregateFunction === 'count') {
19
+ const { variant } = getDocumentsV1Data(result, 'counts');
20
+ if (variant.oneofKind === 'aggregateCount') {
21
+ return { entries: [toEntry({ count: variant.aggregateCount })] };
22
+ }
23
+ return { entries: variant.oneofKind === 'entries' ? variant.entries.entries.filter(({ count }) => count !== '0').map(toEntry) : [] };
24
+ }
25
+ if (aggregateFunction === 'sum') {
26
+ const { variant } = getDocumentsV1Data(result, 'sums');
27
+ if (variant.oneofKind === 'aggregateSum') {
28
+ return { entries: [toEntry({ sum: variant.aggregateSum })] };
29
+ }
30
+ return { entries: variant.oneofKind === 'entries' ? variant.entries.entries.map(toEntry) : [] };
31
+ }
32
+ const { variant } = getDocumentsV1Data(result, 'averages');
33
+ if (variant.oneofKind === 'aggregateAverage') {
34
+ return { entries: [toEntry(variant.aggregateAverage)] };
35
+ }
36
+ return { entries: variant.oneofKind === 'entries' ? variant.entries.entries.filter(({ count }) => count !== '0').map(toEntry) : [] };
37
+ }
38
+ export default async function queryAggregate(grpcPool, quorumKeyProvider, dataContractId, aggregateFunction, query, field) {
6
39
  if (aggregateFunction !== 'count' && field == null) {
7
40
  throw new Error(`Field is required for ${aggregateFunction} query`);
8
41
  }
9
- const dataContract = await getDataContractByIdentifier(grpcPool, dataContractId);
10
- const { proof, metadata } = await getDocumentsV1Proof(grpcPool, {
42
+ const dataContract = await getDataContractByIdentifier(grpcPool, quorumKeyProvider, dataContractId);
43
+ const request = createDocumentsV1Request({
11
44
  dataContractId: (new IdentifierWASM(dataContractId)).bytes(),
12
45
  documentType: query.documentTypeName,
13
46
  whereClauses: toWhereClauses(query.where),
@@ -16,17 +49,18 @@ export default async function queryAggregate(grpcPool, dataContractId, aggregate
16
49
  selects: toSelect({ function: aggregateFunction, field }),
17
50
  groupBy: query.groupBy ?? []
18
51
  });
19
- const metadataTimeMs = BigInt(metadata.timeMs);
20
- let verified;
52
+ const executor = QueryExecutor.build(grpcPool, quorumKeyProvider)
53
+ .setGrpcRequest('getDocuments', request)
54
+ .mapProof(getDocumentsV1Proof);
21
55
  if (aggregateFunction === 'count') {
22
- verified = verifyDocumentsCountProof(proof.grovedbProof, dataContract, query, LATEST_PLATFORM_VERSION, metadataTimeMs);
23
- }
24
- else if (aggregateFunction === 'sum') {
25
- verified = verifyDocumentsSumProof(proof.grovedbProof, dataContract, query, field, LATEST_PLATFORM_VERSION, metadataTimeMs);
26
- }
27
- else {
28
- verified = verifyDocumentsAverageProof(proof.grovedbProof, dataContract, query, field, LATEST_PLATFORM_VERSION, metadataTimeMs);
56
+ return await executor
57
+ .setProofVerifier(verifyDocumentsCountProof, ({ metadata }) => [dataContract, query, LATEST_PLATFORM_VERSION, BigInt(metadata.timeMs)])
58
+ .mapResponse(data => data.proved ? data.verified.value : toDocumentsAggregate(data.result, aggregateFunction))
59
+ .execute();
29
60
  }
30
- await verifyDocumentsV1Signature(grpcPool, proof, metadata, verified.rootHash);
31
- return verified.value;
61
+ const verify = aggregateFunction === 'sum' ? verifyDocumentsSumProof : verifyDocumentsAverageProof;
62
+ return await executor
63
+ .setProofVerifier(verify, ({ metadata }) => [dataContract, query, field, LATEST_PLATFORM_VERSION, BigInt(metadata.timeMs)])
64
+ .mapResponse(data => data.proved ? data.verified.value : toDocumentsAggregate(data.result, aggregateFunction))
65
+ .execute();
32
66
  }
@@ -1,16 +1,17 @@
1
1
  import { CompositeSubQuery, DocumentsQuery, IdentifierLike, VerifiedChainedDocuments, VerifiedCompositeDocuments } from 'pshenmic-dpp';
2
2
  import { GRPCPool } from '../grpcConnectionPool.js';
3
+ import { QuorumKeyProvider } from '../quorumKeyProvider.js';
3
4
  export type ChainedDocuments = Omit<VerifiedChainedDocuments, 'rootHash'>;
4
5
  export type CompositeDocuments = Omit<VerifiedCompositeDocuments, 'rootHash'>;
5
6
  /**
6
7
  * Chained join: documents of `query`, then the documents of `outerDocumentTypeName`
7
8
  * their `joinProperty` refers to
8
9
  */
9
- export declare function queryChained(grpcPool: GRPCPool, dataContractId: IdentifierLike, query: DocumentsQuery, joinProperty: string, outerDocumentTypeName: string): Promise<ChainedDocuments>;
10
+ export declare function queryChained(grpcPool: GRPCPool, quorumKeyProvider: QuorumKeyProvider, dataContractId: IdentifierLike, query: DocumentsQuery, joinProperty: string, outerDocumentTypeName: string): Promise<ChainedDocuments>;
10
11
  /**
11
12
  * Composite query: a page of documents plus up to 10 sub-queries, each optionally bound
12
13
  * to values the page or an earlier sub-query proved
13
14
  */
14
- export declare function queryComposite(grpcPool: GRPCPool, dataContractId: IdentifierLike, query: DocumentsQuery & {
15
+ export declare function queryComposite(grpcPool: GRPCPool, quorumKeyProvider: QuorumKeyProvider, dataContractId: IdentifierLike, query: DocumentsQuery & {
15
16
  offset?: number;
16
17
  }, subQueries: CompositeSubQuery[]): Promise<CompositeDocuments>;