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
@@ -1,35 +1,20 @@
1
1
  import { GetTotalCreditsInPlatformRequest } from '../../proto/generated/platform.js';
2
2
  import { HALVING_INTERVAL, LATEST_PLATFORM_VERSION, MAINNET_ACTIVATION_HEIGHT, TESTNET_ACTIVATION_HEIGHT } from '../constants.js';
3
3
  import { verifyTotalCreditsProof } from 'pshenmic-dpp';
4
- import { getQuorumPublicKey } from '../utils/getQuorumPublicKey.js';
5
- import bytesToHex from '../utils/bytesToHex.js';
6
- import verifyTenderdashProof from '../utils/verifyTenderdashProof.js';
7
- export default async function totalCredits(grpcPool) {
4
+ import QueryExecutor from '../QueryExecutor.js';
5
+ export default async function totalCredits(grpcPool, quorumKeyProvider) {
8
6
  const getTotalCreditsInPlatformRequest = GetTotalCreditsInPlatformRequest.create({
9
7
  version: {
10
8
  oneofKind: 'v0',
11
9
  v0: { prove: true }
12
10
  }
13
11
  });
14
- const { response } = await (await grpcPool.getClient()).getTotalCreditsInPlatform(getTotalCreditsInPlatformRequest);
15
- const { version } = response;
16
- if (version.oneofKind !== 'v0') {
17
- throw new Error('Unexpected oneOf type returned from DAPI (must be v0)');
18
- }
19
- const { v0 } = version;
20
- if (v0.result.oneofKind !== 'proof') {
21
- throw new Error('Unexpected oneOf type returned from DAPI (must be proof)');
22
- }
23
- const { result: { proof }, metadata } = v0;
24
- if (metadata == null) {
25
- throw new Error('Metadata not found');
26
- }
27
- const activationHeight = grpcPool.network === 'testnet' ? TESTNET_ACTIVATION_HEIGHT : MAINNET_ACTIVATION_HEIGHT;
28
- const { rootHash, totalCredits } = verifyTotalCreditsProof(proof.grovedbProof, HALVING_INTERVAL, activationHeight, Number(metadata.height), LATEST_PLATFORM_VERSION);
29
- const quorumPublicKey = await getQuorumPublicKey(grpcPool.network, proof.quorumType, bytesToHex(proof.quorumHash));
30
- const verify = await verifyTenderdashProof(proof, metadata, rootHash, quorumPublicKey);
31
- if (!verify) {
32
- throw new Error('Failed to verify query');
33
- }
34
- return totalCredits;
12
+ return await QueryExecutor.build(grpcPool, quorumKeyProvider)
13
+ .setGrpcRequest('getTotalCreditsInPlatform', getTotalCreditsInPlatformRequest)
14
+ .setProofVerifier(verifyTotalCreditsProof, ({ metadata }) => {
15
+ const activationHeight = grpcPool.network === 'testnet' ? TESTNET_ACTIVATION_HEIGHT : MAINNET_ACTIVATION_HEIGHT;
16
+ return [HALVING_INTERVAL, activationHeight, metadata.coreChainLockedHeight, LATEST_PLATFORM_VERSION];
17
+ })
18
+ .mapResponse(data => data.proved ? data.verified.totalCredits : BigInt(data.result.credits))
19
+ .execute();
35
20
  }
@@ -1,4 +1,5 @@
1
1
  import { GRPCPool } from '../grpcConnectionPool.js';
2
+ import { QuorumKeyProvider } from '../quorumKeyProvider.js';
2
3
  import { PlatformAddressLike } from 'pshenmic-dpp';
3
4
  import { PlatformAddressInfo } from '../../types.js';
4
- export declare function getAddressInfo(grpcPool: GRPCPool, platformAddress: PlatformAddressLike): Promise<PlatformAddressInfo>;
5
+ export declare function getAddressInfo(grpcPool: GRPCPool, quorumKeyProvider: QuorumKeyProvider, platformAddress: PlatformAddressLike): Promise<PlatformAddressInfo>;
@@ -1,10 +1,8 @@
1
1
  import { PlatformAddressWASM, verifyPlatformAddressInfo } from 'pshenmic-dpp';
2
2
  import { GetAddressInfoRequest } from '../../proto/generated/platform.js';
3
3
  import { LATEST_PLATFORM_VERSION } from '../constants.js';
4
- import { getQuorumPublicKey } from '../utils/getQuorumPublicKey.js';
5
- import bytesToHex from '../utils/bytesToHex.js';
6
- import verifyTenderdashProof from '../utils/verifyTenderdashProof.js';
7
- export async function getAddressInfo(grpcPool, platformAddress) {
4
+ import QueryExecutor from '../QueryExecutor.js';
5
+ export async function getAddressInfo(grpcPool, quorumKeyProvider, platformAddress) {
8
6
  const platformAddressWASM = new PlatformAddressWASM(platformAddress);
9
7
  const getAddressInfoRequest = GetAddressInfoRequest.create({
10
8
  version: {
@@ -15,31 +13,19 @@ export async function getAddressInfo(grpcPool, platformAddress) {
15
13
  }
16
14
  }
17
15
  });
18
- const { response } = await (await grpcPool.getClient()).getAddressInfo(getAddressInfoRequest);
19
- const { version } = response;
20
- if (version.oneofKind !== 'v0') {
21
- throw new Error('Unexpected oneOf type returned from DAPI (must be v0)');
22
- }
23
- const { v0 } = version;
24
- if (v0.result.oneofKind !== 'proof') {
25
- throw new Error('Unexpected oneOf type returned from DAPI (must be proof)');
26
- }
27
- const { result: { proof }, metadata } = v0;
28
- if (metadata == null) {
29
- throw new Error('Metadata not found');
30
- }
31
- const { rootHash, address, nonce, balance } = verifyPlatformAddressInfo(proof.grovedbProof, platformAddressWASM, true, LATEST_PLATFORM_VERSION);
32
- if (address == null || nonce == null || balance == null) {
33
- throw new Error(`Failed to fetch info for address ${platformAddressWASM.toBech32m(grpcPool.network)}`);
34
- }
35
- const quorumPublicKey = await getQuorumPublicKey(grpcPool.network, proof.quorumType, bytesToHex(proof.quorumHash));
36
- const verify = await verifyTenderdashProof(proof, metadata, rootHash, quorumPublicKey);
37
- if (!verify) {
38
- throw new Error('Failed to verify query');
39
- }
40
- return {
41
- address,
42
- nonce,
43
- balance
44
- };
16
+ return await QueryExecutor.build(grpcPool, quorumKeyProvider)
17
+ .setGrpcRequest('getAddressInfo', getAddressInfoRequest)
18
+ .setProofVerifier(verifyPlatformAddressInfo, platformAddressWASM, true, LATEST_PLATFORM_VERSION)
19
+ .mapResponse(data => {
20
+ const balanceAndNonce = data.proved ? data.verified : data.result.addressInfoEntry.balanceAndNonce;
21
+ if (balanceAndNonce?.nonce == null || balanceAndNonce.balance == null) {
22
+ throw new Error(`Failed to fetch info for address ${platformAddressWASM.toBech32m(grpcPool.network)}`);
23
+ }
24
+ return {
25
+ address: platformAddressWASM,
26
+ nonce: balanceAndNonce.nonce,
27
+ balance: BigInt(balanceAndNonce.balance)
28
+ };
29
+ })
30
+ .execute();
45
31
  }
@@ -1,3 +1,4 @@
1
1
  import { GRPCPool } from '../grpcConnectionPool.js';
2
+ import { QuorumKeyProvider } from '../quorumKeyProvider.js';
2
3
  import { VerifiedAddressesBranchState } from 'pshenmic-dpp';
3
- export declare function getAddressesBranchState(grpcPool: GRPCPool, key: Uint8Array, depth: number, checkpointHeight: bigint, expectedRootHash: Uint8Array): Promise<VerifiedAddressesBranchState>;
4
+ export declare function getAddressesBranchState(grpcPool: GRPCPool, quorumKeyProvider: QuorumKeyProvider, key: Uint8Array, depth: number, checkpointHeight: bigint, expectedRootHash: Uint8Array): Promise<VerifiedAddressesBranchState>;
@@ -1,7 +1,8 @@
1
1
  import { verifyAddressesBranchState } from 'pshenmic-dpp';
2
- import { GetAddressesBranchStateRequest } from '../../proto/generated/platform.js';
2
+ import { GetAddressesBranchStateRequest, Proof, ResponseMetadata } from '../../proto/generated/platform.js';
3
3
  import { LATEST_PLATFORM_VERSION } from '../constants.js';
4
- export async function getAddressesBranchState(grpcPool, key, depth, checkpointHeight, expectedRootHash) {
4
+ import QueryExecutor from '../QueryExecutor.js';
5
+ export async function getAddressesBranchState(grpcPool, quorumKeyProvider, key, depth, checkpointHeight, expectedRootHash) {
5
6
  const getAddressesBranchStateRequest = GetAddressesBranchStateRequest.create({
6
7
  version: {
7
8
  oneofKind: 'v0',
@@ -12,10 +13,23 @@ export async function getAddressesBranchState(grpcPool, key, depth, checkpointHe
12
13
  }
13
14
  }
14
15
  });
15
- const { response } = await (await grpcPool.getClient()).getAddressesBranchState(getAddressesBranchStateRequest);
16
- const { version } = response;
17
- if (version.oneofKind !== 'v0') {
18
- throw new Error('Unexpected oneOf type returned from DAPI (must be v0)');
19
- }
20
- return verifyAddressesBranchState(version.v0.merkProof, key, depth, expectedRootHash, LATEST_PLATFORM_VERSION);
16
+ // the proof is verified against the root hash of the already verified trunk state, so it has no quorum signature
17
+ return await QueryExecutor.build(grpcPool, quorumKeyProvider)
18
+ .setGrpcRequest('getAddressesBranchState', getAddressesBranchStateRequest)
19
+ // the response has no proof message, only the merk proof
20
+ .mapProof(({ version }) => {
21
+ if (version.oneofKind !== 'v0') {
22
+ throw new Error('Unexpected oneOf type returned from DAPI (must be v0)');
23
+ }
24
+ return { proof: Proof.create({ grovedbProof: version.v0.merkProof }), metadata: ResponseMetadata.create() };
25
+ })
26
+ .setProofVerifier(verifyAddressesBranchState, key, depth, expectedRootHash, LATEST_PLATFORM_VERSION)
27
+ .mapResponse(data => {
28
+ if (!data.proved) {
29
+ throw new Error('Addresses branch state can only be requested with proof');
30
+ }
31
+ return data.verified;
32
+ })
33
+ .setValidateProof(false)
34
+ .execute();
21
35
  }
@@ -1,4 +1,5 @@
1
1
  import { GRPCPool } from '../grpcConnectionPool.js';
2
+ import { QuorumKeyProvider } from '../quorumKeyProvider.js';
2
3
  import { PlatformAddressLike } from 'pshenmic-dpp';
3
4
  import { PlatformAddressInfo } from '../../types.js';
4
- export declare function getAddressesInfos(grpcPool: GRPCPool, platformAddresses: PlatformAddressLike[]): Promise<PlatformAddressInfo[]>;
5
+ export declare function getAddressesInfos(grpcPool: GRPCPool, quorumKeyProvider: QuorumKeyProvider, platformAddresses: PlatformAddressLike[]): Promise<PlatformAddressInfo[]>;
@@ -1,10 +1,8 @@
1
1
  import { PlatformAddressWASM, verifyPlatformAddressesInfos } from 'pshenmic-dpp';
2
2
  import { GetAddressesInfosRequest } from '../../proto/generated/platform.js';
3
3
  import { LATEST_PLATFORM_VERSION } from '../constants.js';
4
- import { getQuorumPublicKey } from '../utils/getQuorumPublicKey.js';
5
- import bytesToHex from '../utils/bytesToHex.js';
6
- import verifyTenderdashProof from '../utils/verifyTenderdashProof.js';
7
- export async function getAddressesInfos(grpcPool, platformAddresses) {
4
+ import QueryExecutor from '../QueryExecutor.js';
5
+ export async function getAddressesInfos(grpcPool, quorumKeyProvider, platformAddresses) {
8
6
  const platformAddressesWASM = platformAddresses.map(addr => new PlatformAddressWASM(addr));
9
7
  const getAddressInfoRequest = GetAddressesInfosRequest.create({
10
8
  version: {
@@ -15,29 +13,20 @@ export async function getAddressesInfos(grpcPool, platformAddresses) {
15
13
  }
16
14
  }
17
15
  });
18
- const { response } = await (await grpcPool.getClient()).getAddressesInfos(getAddressInfoRequest);
19
- const { version } = response;
20
- if (version.oneofKind !== 'v0') {
21
- throw new Error('Unexpected oneOf type returned from DAPI (must be v0)');
22
- }
23
- const { v0 } = version;
24
- if (v0.result.oneofKind !== 'proof') {
25
- throw new Error('Unexpected oneOf type returned from DAPI (must be proof)');
26
- }
27
- const { result: { proof }, metadata } = v0;
28
- if (metadata == null) {
29
- throw new Error('Metadata not found');
30
- }
31
- const { rootHash, infos } = verifyPlatformAddressesInfos(proof.grovedbProof, platformAddressesWASM, true, LATEST_PLATFORM_VERSION);
32
- const quorumPublicKey = await getQuorumPublicKey(grpcPool.network, proof.quorumType, bytesToHex(proof.quorumHash));
33
- const verify = await verifyTenderdashProof(proof, metadata, rootHash, quorumPublicKey);
34
- if (!verify) {
35
- throw new Error('Failed to verify query');
36
- }
37
- return infos
38
- .map(info => ({
39
- address: info.address,
40
- nonce: info.nonce ?? 0,
41
- balance: BigInt(info.balance ?? 0)
42
- }));
16
+ return await QueryExecutor.build(grpcPool, quorumKeyProvider)
17
+ .setGrpcRequest('getAddressesInfos', getAddressInfoRequest)
18
+ .setProofVerifier(verifyPlatformAddressesInfos, platformAddressesWASM, true, LATEST_PLATFORM_VERSION)
19
+ .mapResponse(data => {
20
+ const infos = data.proved
21
+ ? data.verified.infos
22
+ : data.result.addressInfoEntries.addressInfoEntries
23
+ .map(({ address, balanceAndNonce }) => ({ address: PlatformAddressWASM.fromBytes(address), ...balanceAndNonce }));
24
+ return infos
25
+ .map(info => ({
26
+ address: info.address,
27
+ nonce: info.nonce ?? 0,
28
+ balance: BigInt(info.balance ?? 0)
29
+ }));
30
+ })
31
+ .execute();
43
32
  }
@@ -1,3 +1,4 @@
1
1
  import { GRPCPool } from '../grpcConnectionPool.js';
2
+ import { QuorumKeyProvider } from '../quorumKeyProvider.js';
2
3
  import { PlatformAddressesTrunkState } from '../../types.js';
3
- export declare function getAddressesTrunkState(grpcPool: GRPCPool): Promise<PlatformAddressesTrunkState>;
4
+ export declare function getAddressesTrunkState(grpcPool: GRPCPool, quorumKeyProvider: QuorumKeyProvider): Promise<PlatformAddressesTrunkState>;
@@ -1,33 +1,35 @@
1
1
  import { verifyAddressesTrunkState } from 'pshenmic-dpp';
2
2
  import { GetAddressesTrunkStateRequest } from '../../proto/generated/platform.js';
3
3
  import { LATEST_PLATFORM_VERSION } from '../constants.js';
4
- import { getQuorumPublicKey } from '../utils/getQuorumPublicKey.js';
5
- import bytesToHex from '../utils/bytesToHex.js';
6
- import verifyTenderdashProof from '../utils/verifyTenderdashProof.js';
7
- export async function getAddressesTrunkState(grpcPool) {
4
+ import QueryExecutor from '../QueryExecutor.js';
5
+ export async function getAddressesTrunkState(grpcPool, quorumKeyProvider) {
8
6
  const getAddressesTrunkStateRequest = GetAddressesTrunkStateRequest.create({
9
7
  version: {
10
8
  oneofKind: 'v0',
11
9
  v0: {}
12
10
  }
13
11
  });
14
- const { response } = await (await grpcPool.getClient()).getAddressesTrunkState(getAddressesTrunkStateRequest);
15
- const { version } = response;
16
- if (version.oneofKind !== 'v0') {
17
- throw new Error('Unexpected oneOf type returned from DAPI (must be v0)');
18
- }
19
- const { v0: { proof, metadata } } = version;
20
- if (proof == null) {
21
- throw new Error('Proof not found');
22
- }
23
- if (metadata == null) {
24
- throw new Error('Metadata not found');
25
- }
26
- const trunk = verifyAddressesTrunkState(proof.grovedbProof, LATEST_PLATFORM_VERSION);
27
- const quorumPublicKey = await getQuorumPublicKey(grpcPool.network, proof.quorumType, bytesToHex(proof.quorumHash));
28
- const verify = await verifyTenderdashProof(proof, metadata, trunk.rootHash, quorumPublicKey);
29
- if (!verify) {
30
- throw new Error('Failed to verify query');
31
- }
32
- return { trunk, metadata };
12
+ return await QueryExecutor.build(grpcPool, quorumKeyProvider)
13
+ .setGrpcRequest('getAddressesTrunkState', getAddressesTrunkStateRequest)
14
+ .mapProof(({ version }) => {
15
+ if (version.oneofKind !== 'v0') {
16
+ throw new Error('Unexpected oneOf type returned from DAPI (must be v0)');
17
+ }
18
+ const { v0: { proof, metadata } } = version;
19
+ if (proof == null) {
20
+ throw new Error('Proof not found');
21
+ }
22
+ if (metadata == null) {
23
+ throw new Error('Metadata not found');
24
+ }
25
+ return { proof, metadata };
26
+ })
27
+ .setProofVerifier(verifyAddressesTrunkState, LATEST_PLATFORM_VERSION)
28
+ .mapResponse(data => {
29
+ if (!data.proved) {
30
+ throw new Error('Addresses trunk state can only be requested with proof');
31
+ }
32
+ return { trunk: data.verified, metadata: data.metadata };
33
+ })
34
+ .execute();
33
35
  }
@@ -1,3 +1,4 @@
1
1
  import { GRPCPool } from '../grpcConnectionPool.js';
2
+ import { QuorumKeyProvider } from '../quorumKeyProvider.js';
2
3
  import { BlockAddressBalanceChanges } from 'pshenmic-dpp';
3
- export declare function getRecentAddressBalanceChanges(grpcPool: GRPCPool, startHeight: bigint, startHeightExclusive?: boolean): Promise<BlockAddressBalanceChanges[]>;
4
+ export declare function getRecentAddressBalanceChanges(grpcPool: GRPCPool, quorumKeyProvider: QuorumKeyProvider, startHeight: bigint, startHeightExclusive?: boolean): Promise<BlockAddressBalanceChanges[]>;
@@ -1,10 +1,8 @@
1
- import { verifyRecentAddressBalanceChanges } from 'pshenmic-dpp';
1
+ import { verifyRecentAddressBalanceChanges, PlatformAddressWASM } from 'pshenmic-dpp';
2
2
  import { GetRecentAddressBalanceChangesRequest } from '../../proto/generated/platform.js';
3
- import { LATEST_PLATFORM_VERSION } from '../constants.js';
4
- import { getQuorumPublicKey } from '../utils/getQuorumPublicKey.js';
5
- import bytesToHex from '../utils/bytesToHex.js';
6
- import verifyTenderdashProof from '../utils/verifyTenderdashProof.js';
7
- export async function getRecentAddressBalanceChanges(grpcPool, startHeight, startHeightExclusive = false) {
3
+ import { LATEST_PLATFORM_VERSION, RECENT_ADDRESS_BALANCE_CHANGES_LIMIT } from '../constants.js';
4
+ import QueryExecutor from '../QueryExecutor.js';
5
+ export async function getRecentAddressBalanceChanges(grpcPool, quorumKeyProvider, startHeight, startHeightExclusive = false) {
8
6
  const getRecentAddressBalanceChangesRequest = GetRecentAddressBalanceChangesRequest.create({
9
7
  version: {
10
8
  oneofKind: 'v0',
@@ -15,24 +13,26 @@ export async function getRecentAddressBalanceChanges(grpcPool, startHeight, star
15
13
  }
16
14
  }
17
15
  });
18
- const { response } = await (await grpcPool.getClient()).getRecentAddressBalanceChanges(getRecentAddressBalanceChangesRequest);
19
- const { version } = response;
20
- if (version.oneofKind !== 'v0') {
21
- throw new Error('Unexpected oneOf type returned from DAPI (must be v0)');
22
- }
23
- const { v0 } = version;
24
- if (v0.result.oneofKind !== 'proof') {
25
- throw new Error('Unexpected oneOf type returned from DAPI (must be proof)');
26
- }
27
- const { result: { proof }, metadata } = v0;
28
- if (metadata == null) {
29
- throw new Error('Metadata not found');
30
- }
31
- const { rootHash, blocks } = verifyRecentAddressBalanceChanges(proof.grovedbProof, startHeight, startHeightExclusive, undefined, true, LATEST_PLATFORM_VERSION);
32
- const quorumPublicKey = await getQuorumPublicKey(grpcPool.network, proof.quorumType, bytesToHex(proof.quorumHash));
33
- const verify = await verifyTenderdashProof(proof, metadata, rootHash, quorumPublicKey);
34
- if (!verify) {
35
- throw new Error('Failed to verify query');
36
- }
37
- return blocks;
16
+ return await QueryExecutor.build(grpcPool, quorumKeyProvider)
17
+ .setGrpcRequest('getRecentAddressBalanceChanges', getRecentAddressBalanceChangesRequest)
18
+ .setProofVerifier(verifyRecentAddressBalanceChanges, startHeight, startHeightExclusive, RECENT_ADDRESS_BALANCE_CHANGES_LIMIT, true, LATEST_PLATFORM_VERSION)
19
+ .mapResponse(data => {
20
+ if (data.proved) {
21
+ return data.verified.blocks;
22
+ }
23
+ return data.result.addressBalanceUpdateEntries.blockChanges.map(({ blockHeight, changes }) => ({
24
+ blockHeight: BigInt(blockHeight),
25
+ changes: changes.map(({ address, operation }) => {
26
+ if (operation.oneofKind == null) {
27
+ throw new Error('Unexpected address balance change operation');
28
+ }
29
+ return {
30
+ address: PlatformAddressWASM.fromBytes(address),
31
+ operation: operation.oneofKind === 'setBalance' ? 'setCredits' : 'addToCredits',
32
+ credits: BigInt(operation.oneofKind === 'setBalance' ? operation.setBalance : operation.addToBalance)
33
+ };
34
+ })
35
+ }));
36
+ })
37
+ .execute();
38
38
  }
@@ -1,3 +1,4 @@
1
1
  import { GRPCPool } from '../grpcConnectionPool.js';
2
+ import { QuorumKeyProvider } from '../quorumKeyProvider.js';
2
3
  import { CompactedBlockAddressBalanceChanges } from 'pshenmic-dpp';
3
- export declare function getRecentCompactedAddressBalanceChanges(grpcPool: GRPCPool, startBlockHeight: bigint): Promise<CompactedBlockAddressBalanceChanges[]>;
4
+ export declare function getRecentCompactedAddressBalanceChanges(grpcPool: GRPCPool, quorumKeyProvider: QuorumKeyProvider, startBlockHeight: bigint): Promise<CompactedBlockAddressBalanceChanges[]>;
@@ -1,10 +1,8 @@
1
- import { verifyRecentCompactedAddressBalanceChanges } from 'pshenmic-dpp';
1
+ import { verifyRecentCompactedAddressBalanceChanges, PlatformAddressWASM } from 'pshenmic-dpp';
2
2
  import { GetRecentCompactedAddressBalanceChangesRequest } from '../../proto/generated/platform.js';
3
- import { LATEST_PLATFORM_VERSION } from '../constants.js';
4
- import { getQuorumPublicKey } from '../utils/getQuorumPublicKey.js';
5
- import bytesToHex from '../utils/bytesToHex.js';
6
- import verifyTenderdashProof from '../utils/verifyTenderdashProof.js';
7
- export async function getRecentCompactedAddressBalanceChanges(grpcPool, startBlockHeight) {
3
+ import { LATEST_PLATFORM_VERSION, RECENT_COMPACTED_ADDRESS_BALANCE_CHANGES_LIMIT } from '../constants.js';
4
+ import QueryExecutor from '../QueryExecutor.js';
5
+ export async function getRecentCompactedAddressBalanceChanges(grpcPool, quorumKeyProvider, startBlockHeight) {
8
6
  const getRecentCompactedAddressBalanceChangesRequest = GetRecentCompactedAddressBalanceChangesRequest.create({
9
7
  version: {
10
8
  oneofKind: 'v0',
@@ -14,24 +12,30 @@ export async function getRecentCompactedAddressBalanceChanges(grpcPool, startBlo
14
12
  }
15
13
  }
16
14
  });
17
- const { response } = await (await grpcPool.getClient()).getRecentCompactedAddressBalanceChanges(getRecentCompactedAddressBalanceChangesRequest);
18
- const { version } = response;
19
- if (version.oneofKind !== 'v0') {
20
- throw new Error('Unexpected oneOf type returned from DAPI (must be v0)');
21
- }
22
- const { v0 } = version;
23
- if (v0.result.oneofKind !== 'proof') {
24
- throw new Error('Unexpected oneOf type returned from DAPI (must be proof)');
25
- }
26
- const { result: { proof }, metadata } = v0;
27
- if (metadata == null) {
28
- throw new Error('Metadata not found');
29
- }
30
- const { rootHash, ranges } = verifyRecentCompactedAddressBalanceChanges(proof.grovedbProof, startBlockHeight, undefined, LATEST_PLATFORM_VERSION);
31
- const quorumPublicKey = await getQuorumPublicKey(grpcPool.network, proof.quorumType, bytesToHex(proof.quorumHash));
32
- const verify = await verifyTenderdashProof(proof, metadata, rootHash, quorumPublicKey);
33
- if (!verify) {
34
- throw new Error('Failed to verify query');
35
- }
36
- return ranges;
15
+ return await QueryExecutor.build(grpcPool, quorumKeyProvider)
16
+ .setGrpcRequest('getRecentCompactedAddressBalanceChanges', getRecentCompactedAddressBalanceChangesRequest)
17
+ .setProofVerifier(verifyRecentCompactedAddressBalanceChanges, startBlockHeight, RECENT_COMPACTED_ADDRESS_BALANCE_CHANGES_LIMIT, LATEST_PLATFORM_VERSION)
18
+ .mapResponse(data => {
19
+ if (data.proved) {
20
+ return data.verified.ranges;
21
+ }
22
+ return data.result.compactedAddressBalanceUpdateEntries.compactedBlockChanges.map(({ startBlockHeight, endBlockHeight, changes }) => ({
23
+ startBlockHeight: BigInt(startBlockHeight),
24
+ endBlockHeight: BigInt(endBlockHeight),
25
+ changes: changes.map(({ address, operation }) => {
26
+ if (operation.oneofKind == null) {
27
+ throw new Error('Unexpected address balance change operation');
28
+ }
29
+ return {
30
+ address: PlatformAddressWASM.fromBytes(address),
31
+ operation: operation.oneofKind,
32
+ setCredits: operation.oneofKind === 'setCredits' ? BigInt(operation.setCredits) : undefined,
33
+ addToCreditsOperations: operation.oneofKind === 'addToCreditsOperations'
34
+ ? operation.addToCreditsOperations.entries.map(({ blockHeight, credits }) => ({ blockHeight: BigInt(blockHeight), credits: BigInt(credits) }))
35
+ : []
36
+ };
37
+ })
38
+ }));
39
+ })
40
+ .execute();
37
41
  }
@@ -1,10 +1,13 @@
1
1
  import { GRPCPool } from '../grpcConnectionPool.js';
2
+ import { QuorumKeyProvider } from '../quorumKeyProvider.js';
2
3
  import { BlockAddressBalanceChanges, CompactedBlockAddressBalanceChanges, PlatformAddressLike, StateTransitionWASM, VerifiedAddressesBranchState } from 'pshenmic-dpp';
3
4
  import { PlatformAddressesTrunkState, PlatformAddressInfo, PlatformAddressTransitionParamsMap, PlatformAddressTransitionType } from '../../types.js';
4
5
  export declare class PlatformAddressesController {
5
6
  /** @ignore **/
6
7
  grpcPool: GRPCPool;
7
- constructor(grpcPool: GRPCPool);
8
+ /** @ignore **/
9
+ quorumKeyProvider: QuorumKeyProvider;
10
+ constructor(grpcPool: GRPCPool, quorumKeyProvider: QuorumKeyProvider);
8
11
  /**
9
12
  * Makes a query for platform address info, returns an object with address, balance and nonce
10
13
  *
@@ -8,8 +8,11 @@ import createStateTransition from './createStateTransition.js';
8
8
  export class PlatformAddressesController {
9
9
  /** @ignore **/
10
10
  grpcPool;
11
- constructor(grpcPool) {
11
+ /** @ignore **/
12
+ quorumKeyProvider;
13
+ constructor(grpcPool, quorumKeyProvider) {
12
14
  this.grpcPool = grpcPool;
15
+ this.quorumKeyProvider = quorumKeyProvider;
13
16
  }
14
17
  /**
15
18
  * Makes a query for platform address info, returns an object with address, balance and nonce
@@ -18,7 +21,7 @@ export class PlatformAddressesController {
18
21
  * @return {Promise<PlatformAddressInfo>}
19
22
  */
20
23
  async getAddressInfo(address) {
21
- return await getAddressInfo(this.grpcPool, address);
24
+ return await getAddressInfo(this.grpcPool, this.quorumKeyProvider, address);
22
25
  }
23
26
  /**
24
27
  * Makes a query for platform addresses infos, returns array with an object with address, balance and nonce
@@ -27,7 +30,7 @@ export class PlatformAddressesController {
27
30
  * @return {Promise<PlatformAddressInfo[]>}
28
31
  */
29
32
  async getAddressesInfos(addresses) {
30
- return await getAddressesInfos(this.grpcPool, addresses);
33
+ return await getAddressesInfos(this.grpcPool, this.quorumKeyProvider, addresses);
31
34
  }
32
35
  /**
33
36
  * Makes a query for the trunk chunk of the address funds tree, used as the entry point
@@ -40,7 +43,7 @@ export class PlatformAddressesController {
40
43
  * @return {Promise<PlatformAddressesTrunkState>}
41
44
  */
42
45
  async getAddressesTrunkState() {
43
- return await getAddressesTrunkState(this.grpcPool);
46
+ return await getAddressesTrunkState(this.grpcPool, this.quorumKeyProvider);
44
47
  }
45
48
  /**
46
49
  * Makes a query for a branch chunk of the address funds tree at the given key and depth,
@@ -57,7 +60,7 @@ export class PlatformAddressesController {
57
60
  * @return {Promise<VerifiedAddressesBranchState>}
58
61
  */
59
62
  async getAddressesBranchState(key, depth, checkpointHeight, expectedRootHash) {
60
- return await getAddressesBranchState(this.grpcPool, key, depth, checkpointHeight, expectedRootHash);
63
+ return await getAddressesBranchState(this.grpcPool, this.quorumKeyProvider, key, depth, checkpointHeight, expectedRootHash);
61
64
  }
62
65
  /**
63
66
  * Makes a query for recent address balance changes starting from the given block height,
@@ -68,7 +71,7 @@ export class PlatformAddressesController {
68
71
  * @return {Promise<BlockAddressBalanceChanges[]>}
69
72
  */
70
73
  async getRecentAddressBalanceChanges(startHeight, startHeightExclusive = false) {
71
- return await getRecentAddressBalanceChanges(this.grpcPool, startHeight, startHeightExclusive);
74
+ return await getRecentAddressBalanceChanges(this.grpcPool, this.quorumKeyProvider, startHeight, startHeightExclusive);
72
75
  }
73
76
  /**
74
77
  * Makes a query for recent compacted address balance changes starting from the given
@@ -78,7 +81,7 @@ export class PlatformAddressesController {
78
81
  * @return {Promise<CompactedBlockAddressBalanceChanges[]>}
79
82
  */
80
83
  async getRecentCompactedAddressBalanceChanges(startBlockHeight) {
81
- return await getRecentCompactedAddressBalanceChanges(this.grpcPool, startBlockHeight);
84
+ return await getRecentCompactedAddressBalanceChanges(this.grpcPool, this.quorumKeyProvider, startBlockHeight);
82
85
  }
83
86
  /**
84
87
  * Helper function for creating {StateTransitionWASM} for Platform Address transitions
@@ -0,0 +1,23 @@
1
+ import { Network } from '../types.js';
2
+ /**
3
+ * Source of quorum public keys used to verify proof signatures.
4
+ * Implement it to supply keys from your own source (SPV client, local Dash Core node, etc.)
5
+ */
6
+ export interface QuorumKeyProvider {
7
+ /**
8
+ * @param quorumType {number} LLMQ type of the quorum
9
+ * @param quorumHash {string} hex of proof.quorumHash bytes, same as in Dash Core RPC (`quorum info`)
10
+ *
11
+ * @return {Promise<string>} BLS public key of the quorum in hex
12
+ */
13
+ getQuorumPublicKey: (quorumType: number, quorumHash: string) => Promise<string>;
14
+ }
15
+ /**
16
+ * Default quorum key provider, fetches quorum public keys from Platform Explorer API
17
+ */
18
+ export declare class PlatformExplorerQuorumKeyProvider implements QuorumKeyProvider {
19
+ #private;
20
+ network: Network;
21
+ constructor(network: Network);
22
+ getQuorumPublicKey(quorumType: number, quorumHash: string): Promise<string>;
23
+ }
@@ -0,0 +1,28 @@
1
+ import { fetchWithDetails } from './utils/fetchWithDetails.js';
2
+ /**
3
+ * Default quorum key provider, fetches quorum public keys from Platform Explorer API
4
+ */
5
+ export class PlatformExplorerQuorumKeyProvider {
6
+ network;
7
+ #cache = {};
8
+ constructor(network) {
9
+ this.network = network;
10
+ }
11
+ async getQuorumPublicKey(quorumType, quorumHash) {
12
+ const cached = this.#cache[`${quorumType}_${quorumHash}`];
13
+ if (cached != null) {
14
+ return cached;
15
+ }
16
+ const url = `https://${this.network === 'mainnet' ? '' : 'testnet.'}platform-explorer.pshenmic.dev/quorum/info?quorumType=${quorumType}&quorumHash=${quorumHash}`;
17
+ const resp = await fetchWithDetails(url, {
18
+ method: 'GET'
19
+ });
20
+ if (resp.status !== 200) {
21
+ throw new Error('Failed to query Platform Explorer for quorum public keys');
22
+ }
23
+ const data = await resp.json();
24
+ const { quorumPublicKey } = data;
25
+ this.#cache[`${quorumType}_${quorumHash}`] = quorumPublicKey;
26
+ return quorumPublicKey;
27
+ }
28
+ }
@@ -1,3 +1,4 @@
1
1
  import { GRPCPool } from '../grpcConnectionPool.js';
2
+ import { QuorumKeyProvider } from '../quorumKeyProvider.js';
2
3
  import { IdentifierLike } from 'pshenmic-dpp';
3
- export default function getMostRecentShieldedAnchor(grpcPool: GRPCPool, tokenIdentifier?: IdentifierLike): Promise<Uint8Array | undefined>;
4
+ export default function getMostRecentShieldedAnchor(grpcPool: GRPCPool, quorumKeyProvider: QuorumKeyProvider, tokenIdentifier?: IdentifierLike): Promise<Uint8Array | undefined>;