dash-platform-sdk 1.6.0-dev.7 → 1.6.0-dev.8

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 (56) hide show
  1. package/README.md +14 -14
  2. package/bundle.min.js +1 -1
  3. package/package.json +1 -1
  4. package/src/QueryExecutor.d.ts +40 -4
  5. package/src/QueryExecutor.js +61 -8
  6. package/src/constants.d.ts +7 -1
  7. package/src/constants.js +9 -2
  8. package/src/contestedResources/getContestedResourceVoteState.js +14 -6
  9. package/src/contestedResources/getVotePollsByEndDate.js +7 -2
  10. package/src/dataContracts/getDataContractByIdentifier.js +3 -3
  11. package/src/documents/index.d.ts +4 -1
  12. package/src/documents/index.js +6 -2
  13. package/src/documents/query.js +4 -4
  14. package/src/documents/queryAggregate.js +2 -3
  15. package/src/documents/queryJoined.js +6 -7
  16. package/src/documents/queryRanked.js +2 -3
  17. package/src/documents/v1.d.ts +2 -2
  18. package/src/documents/v1.js +2 -3
  19. package/src/grpcConnectionPool.d.ts +9 -2
  20. package/src/grpcConnectionPool.js +7 -8
  21. package/src/identities/getIdentityBalance.js +1 -2
  22. package/src/identities/getIdentityByIdentifier.js +1 -2
  23. package/src/identities/getIdentityByNonUniquePublicKeyHash.js +2 -3
  24. package/src/identities/getIdentityByPublicKeyHash.js +1 -2
  25. package/src/identities/getIdentityContractNonce.js +1 -2
  26. package/src/identities/getIdentityNonce.js +1 -2
  27. package/src/identities/getIdentityPublicKeys.js +1 -2
  28. package/src/names/index.d.ts +1 -1
  29. package/src/names/index.js +1 -1
  30. package/src/names/registerName.js +5 -2
  31. package/src/node/epochInfos.js +1 -2
  32. package/src/node/finalizedEpochInfos.js +1 -2
  33. package/src/node/getProtocolVersion.d.ts +9 -0
  34. package/src/node/getProtocolVersion.js +29 -0
  35. package/src/node/index.d.ts +9 -0
  36. package/src/node/index.js +11 -0
  37. package/src/node/totalCredits.js +3 -3
  38. package/src/platformAddresses/getAddressInfo.js +1 -2
  39. package/src/platformAddresses/getAddressesBranchState.js +1 -2
  40. package/src/platformAddresses/getAddressesInfos.js +1 -2
  41. package/src/platformAddresses/getAddressesTrunkState.js +1 -2
  42. package/src/platformAddresses/getRecentAddressBalanceChanges.js +2 -2
  43. package/src/platformAddresses/getRecentCompactedAddressBalanceChanges.js +2 -2
  44. package/src/shielded/getMostRecentShieldedAnchor.js +1 -2
  45. package/src/shielded/getShieldedAnchors.js +1 -2
  46. package/src/shielded/getShieldedEncryptedNotes.js +2 -2
  47. package/src/shielded/getShieldedNotesCount.js +1 -2
  48. package/src/shielded/getShieldedNullifiers.js +1 -2
  49. package/src/shielded/getShieldedPoolState.js +1 -2
  50. package/src/stateTransitions/waitForStateTransitionResult.js +13 -11
  51. package/src/tokens/getIdentitiesTokenBalances.js +1 -2
  52. package/src/tokens/getIdentityTokensBalances.js +1 -2
  53. package/src/tokens/getTokenContractInfo.js +1 -2
  54. package/src/tokens/getTokenDirectPurchasePrices.js +1 -2
  55. package/src/tokens/getTokenTotalSupply.js +1 -2
  56. package/test/unit/SDK.spec.js +38 -0
@@ -3,7 +3,7 @@ import hexToBytes from '../utils/hexToBytes.js';
3
3
  import { WaitForStateTransitionResultRequest } from '../../proto/generated/platform.js';
4
4
  import QueryExecutor, { getResponseProof } from '../QueryExecutor.js';
5
5
  import { base64 } from '@scure/base';
6
- import { LATEST_PLATFORM_VERSION } from '../constants.js';
6
+ import { LATEST_PLATFORM_VERSION, WITHDRAWALS_DATA_CONTRACT_ID } from '../constants.js';
7
7
  import getDataContractByIdentifier from '../dataContracts/getDataContractByIdentifier.js';
8
8
  function toStateTransitionError(txHash, { code, message, data }) {
9
9
  return new Error(`State transition ${txHash} failed to execute with code ${String(code)}, message: ${String(message)}, data: ${base64.encode(data)}`);
@@ -16,8 +16,7 @@ export default async function waitForStateTransitionResult(grpcPool, quorumKeyPr
16
16
  v0: {
17
17
  stateTransitionHash: hexToBytes(txHash),
18
18
  prove: true,
19
- // without proof the owner balance is returned only if requested
20
- requestUserBalance: grpcPool.proofs === false
19
+ requestUserBalance: false
21
20
  }
22
21
  }
23
22
  });
@@ -32,8 +31,14 @@ export default async function waitForStateTransitionResult(grpcPool, quorumKeyPr
32
31
  const masternodeVote = MasternodeVoteTransitionWASM.fromStateTransition(stateTransition);
33
32
  knownContracts.push(await getDataContractByIdentifier(grpcPool, quorumKeyProvider, masternodeVote.vote.votePoll.contractId));
34
33
  }
34
+ // withdrawal documents of the shielded pool are created in the withdrawals system contract
35
+ if (stateTransition.getActionType() === 'SHIELDED_WITHDRAWAL') {
36
+ knownContracts.push(await getDataContractByIdentifier(grpcPool, quorumKeyProvider, WITHDRAWALS_DATA_CONTRACT_ID));
37
+ }
35
38
  return await QueryExecutor.build(grpcPool, quorumKeyProvider)
36
39
  .setGrpcRequest('waitForStateTransitionResult', waitForStateTransitionResultRequest)
40
+ // the result is always verified, also if proofs are disabled in the pool
41
+ .setRequireProof(true)
37
42
  .mapProof(response => {
38
43
  const { version } = response;
39
44
  if (version.oneofKind !== 'v0') {
@@ -56,16 +61,13 @@ export default async function waitForStateTransitionResult(grpcPool, quorumKeyPr
56
61
  return [stateTransition, blockInfo, knownContracts, Math.min(metadata.protocolVersion, LATEST_PLATFORM_VERSION)];
57
62
  })
58
63
  .mapResponse(data => {
59
- if (data.proved) {
60
- if (data.verified.result == null) {
61
- throw new Error('State transition result was null');
62
- }
63
- return data.verified.ownerBalance;
64
+ if (!data.proved) {
65
+ throw new Error('State transition result must be proved');
64
66
  }
65
- if (data.result.oneofKind === 'error') {
66
- throw toStateTransitionError(txHash, data.result.error);
67
+ if (data.verified.result == null) {
68
+ throw new Error('State transition result was null');
67
69
  }
68
- return BigInt(data.result.successWithOwnerBalance.ownerBalance);
70
+ return data.verified.ownerBalance;
69
71
  })
70
72
  .execute();
71
73
  }
@@ -1,7 +1,6 @@
1
1
  import { GetIdentitiesTokenBalancesRequest } from '../../proto/generated/platform.js';
2
2
  import { IdentifierWASM, verifyTokenBalancesForIdentitiesProof } from 'pshenmic-dpp';
3
3
  import QueryExecutor from '../QueryExecutor.js';
4
- import { LATEST_PLATFORM_VERSION } from '../constants.js';
5
4
  export default async function getIdentitiesTokenBalances(grpcPool, quorumKeyProvider, identifiers, tokenIdentifier) {
6
5
  const ids = identifiers.map(identifier => new IdentifierWASM(identifier));
7
6
  const tokenId = new IdentifierWASM(tokenIdentifier);
@@ -17,7 +16,7 @@ export default async function getIdentitiesTokenBalances(grpcPool, quorumKeyProv
17
16
  });
18
17
  return await QueryExecutor.build(grpcPool, quorumKeyProvider)
19
18
  .setGrpcRequest('getIdentitiesTokenBalances', getIdentitiesTokenBalancesRequest)
20
- .setProofVerifier(verifyTokenBalancesForIdentitiesProof, tokenId, true, ids, LATEST_PLATFORM_VERSION)
19
+ .setProofVerifier(verifyTokenBalancesForIdentitiesProof, (_, platformVersion) => [tokenId, true, ids, platformVersion])
21
20
  .mapResponse(data => {
22
21
  const balances = data.proved
23
22
  ? data.verified.balances
@@ -1,7 +1,6 @@
1
1
  import { IdentifierWASM, verifyTokensBalancesForIdentityProof } from 'pshenmic-dpp';
2
2
  import { GetIdentityTokenBalancesRequest } from '../../proto/generated/platform.js';
3
3
  import QueryExecutor from '../QueryExecutor.js';
4
- import { LATEST_PLATFORM_VERSION } from '../constants.js';
5
4
  export default async function getIdentityTokensBalances(grpcPool, quorumKeyProvider, identifier, tokenIdentifiers) {
6
5
  const id = new IdentifierWASM(identifier);
7
6
  const tokenIds = tokenIdentifiers.map(tokenIdentifier => new IdentifierWASM(tokenIdentifier));
@@ -17,7 +16,7 @@ export default async function getIdentityTokensBalances(grpcPool, quorumKeyProvi
17
16
  });
18
17
  return await QueryExecutor.build(grpcPool, quorumKeyProvider)
19
18
  .setGrpcRequest('getIdentityTokenBalances', getIdentityTokenBalancesRequest)
20
- .setProofVerifier(verifyTokensBalancesForIdentityProof, tokenIds, id, true, LATEST_PLATFORM_VERSION)
19
+ .setProofVerifier(verifyTokensBalancesForIdentityProof, (_, platformVersion) => [tokenIds, id, true, platformVersion])
21
20
  .mapResponse(data => {
22
21
  const balances = data.proved
23
22
  ? data.verified.balances
@@ -1,7 +1,6 @@
1
1
  import { GetTokenContractInfoRequest } from '../../proto/generated/platform.js';
2
2
  import { IdentifierWASM, verifyTokenContractInfoProof } from 'pshenmic-dpp';
3
3
  import QueryExecutor from '../QueryExecutor.js';
4
- import { LATEST_PLATFORM_VERSION } from '../constants.js';
5
4
  export default async function getTokenContractInfo(grpcPool, quorumKeyProvider, tokenIdentifier) {
6
5
  const tokenId = new IdentifierWASM(tokenIdentifier);
7
6
  const getTokenContractInfoRequest = GetTokenContractInfoRequest.create({
@@ -15,7 +14,7 @@ export default async function getTokenContractInfo(grpcPool, quorumKeyProvider,
15
14
  });
16
15
  return await QueryExecutor.build(grpcPool, quorumKeyProvider)
17
16
  .setGrpcRequest('getTokenContractInfo', getTokenContractInfoRequest)
18
- .setProofVerifier(verifyTokenContractInfoProof, tokenId, true, LATEST_PLATFORM_VERSION)
17
+ .setProofVerifier(verifyTokenContractInfoProof, (_, platformVersion) => [tokenId, true, platformVersion])
19
18
  .mapResponse(data => {
20
19
  const contractInfo = data.proved ? data.verified.contractInfo : data.result.data;
21
20
  if (contractInfo == null) {
@@ -1,7 +1,6 @@
1
1
  import { IdentifierWASM, verifyTokenDirectPurchasePrices, TokenPricingScheduleWASM } from 'pshenmic-dpp';
2
2
  import QueryExecutor from '../QueryExecutor.js';
3
3
  import { GetTokenDirectPurchasePricesRequest } from '../../proto/generated/platform.js';
4
- import { LATEST_PLATFORM_VERSION } from '../constants.js';
5
4
  export default async function getTokenDirectPurchasePrices(grpcPool, quorumKeyProvider, tokenIdentifiers) {
6
5
  const tokenIds = tokenIdentifiers.map(tokenId => new IdentifierWASM(tokenId).bytes());
7
6
  const request = GetTokenDirectPurchasePricesRequest.create({
@@ -15,7 +14,7 @@ export default async function getTokenDirectPurchasePrices(grpcPool, quorumKeyPr
15
14
  });
16
15
  return await QueryExecutor.build(grpcPool, quorumKeyProvider)
17
16
  .setGrpcRequest('getTokenDirectPurchasePrices', request)
18
- .setProofVerifier(verifyTokenDirectPurchasePrices, tokenIds, true, LATEST_PLATFORM_VERSION)
17
+ .setProofVerifier(verifyTokenDirectPurchasePrices, (_, platformVersion) => [tokenIds, true, platformVersion])
19
18
  .mapResponse(data => {
20
19
  if (data.proved) {
21
20
  if (data.verified.prices == null) {
@@ -1,7 +1,6 @@
1
1
  import { GetTokenTotalSupplyRequest } from '../../proto/generated/platform.js';
2
2
  import { IdentifierWASM, verifyTokenTotalSupplyProof } from 'pshenmic-dpp';
3
3
  import QueryExecutor from '../QueryExecutor.js';
4
- import { LATEST_PLATFORM_VERSION } from '../constants.js';
5
4
  export default async function getTokenTotalSupply(grpcPool, quorumKeyProvider, tokenIdentifier) {
6
5
  const tokenId = new IdentifierWASM(tokenIdentifier);
7
6
  const getTokenTotalSupplyRequest = GetTokenTotalSupplyRequest.create({
@@ -15,7 +14,7 @@ export default async function getTokenTotalSupply(grpcPool, quorumKeyProvider, t
15
14
  });
16
15
  return await QueryExecutor.build(grpcPool, quorumKeyProvider)
17
16
  .setGrpcRequest('getTokenTotalSupply', getTokenTotalSupplyRequest)
18
- .setProofVerifier(verifyTokenTotalSupplyProof, tokenId, true, LATEST_PLATFORM_VERSION)
17
+ .setProofVerifier(verifyTokenTotalSupplyProof, (_, platformVersion) => [tokenId, true, platformVersion])
19
18
  .mapResponse(data => {
20
19
  const [totalSystemAmount, totalAggregatedAmountInUserAccounts] = data.proved
21
20
  ? [data.verified.totalBalance.tokenSupply, data.verified.totalBalance.aggregatedTokenAccountBalances]
@@ -1,5 +1,8 @@
1
1
  import { DashPlatformSDK } from '../../src/DashPlatformSDK.js';
2
2
  import { PlatformExplorerQuorumKeyProvider } from '../../src/quorumKeyProvider.js';
3
+ import { getVerifierVersion } from '../../src/QueryExecutor.js';
4
+ import { DocumentWASM, PlatformVersionWASM } from 'pshenmic-dpp';
5
+ import { DPNS_DATA_CONTRACT_ID, LATEST_PLATFORM_VERSION, MIN_PROTOCOL_VERSION } from '../../src/constants.js';
3
6
  let sdk;
4
7
  const identifier = 'QMfCRPcjXoTnZa9sA9JR2KWgGxZXMRJ4akgS3Uia1Qv';
5
8
  // takes quorum public keys from DAPI validator sets, so proofs can be verified without Platform Explorer
@@ -49,6 +52,41 @@ describe('DashPlatformSDK', () => {
49
52
  customSdk.setQuorumKeyProvider();
50
53
  expect(customSdk.quorumKeyProvider).toEqual(expect.any(PlatformExplorerQuorumKeyProvider));
51
54
  });
55
+ test('should learn protocol version of the network from verified responses', async () => {
56
+ const customSdk = new DashPlatformSDK({ network: 'testnet' });
57
+ expect(customSdk.grpcPool.protocolVersion).toEqual(MIN_PROTOCOL_VERSION.testnet);
58
+ await customSdk.identities.getIdentityBalance(identifier);
59
+ expect(customSdk.grpcPool.protocolVersion).toEqual(LATEST_PLATFORM_VERSION);
60
+ });
61
+ test('should not learn protocol version from unverified responses', async () => {
62
+ const customSdk = new DashPlatformSDK({ network: 'testnet', proofs: false });
63
+ await customSdk.identities.getIdentityBalance(identifier);
64
+ expect(customSdk.grpcPool.protocolVersion).toEqual(MIN_PROTOCOL_VERSION.testnet);
65
+ });
66
+ test('should learn protocol version of the network with proofs disabled', async () => {
67
+ const customSdk = new DashPlatformSDK({ network: 'testnet', proofs: false });
68
+ expect(await customSdk.node.getProtocolVersion()).toEqual(LATEST_PLATFORM_VERSION);
69
+ expect(customSdk.grpcPool.protocolVersion).toEqual(LATEST_PLATFORM_VERSION);
70
+ });
71
+ test('should create documents with id of the protocol version of the network', () => {
72
+ const customSdk = new DashPlatformSDK({ network: 'testnet' });
73
+ const nonce = 5n;
74
+ for (const protocolVersion of [PlatformVersionWASM.PLATFORM_V13, PlatformVersionWASM.PLATFORM_V14]) {
75
+ customSdk.grpcPool.protocolVersion = protocolVersion;
76
+ const document = customSdk.documents.create(DPNS_DATA_CONTRACT_ID, 'preorder', { saltedDomainHash: new Uint8Array(32) }, identifier, nonce);
77
+ const expectedId = DocumentWASM.generateId('preorder', identifier, DPNS_DATA_CONTRACT_ID, document.entropy, nonce, protocolVersion);
78
+ expect(document.id.base58()).toEqual(expectedId.base58());
79
+ }
80
+ });
81
+ test('should select platform version to verify proofs with', () => {
82
+ // newer signed version is used
83
+ expect(getVerifierVersion(13, 14)).toEqual(14);
84
+ // older signed version is not
85
+ expect(getVerifierVersion(14, 13)).toEqual(14);
86
+ expect(getVerifierVersion(13, 0)).toEqual(13);
87
+ // unknown version falls back to the latest known one
88
+ expect(getVerifierVersion(13, LATEST_PLATFORM_VERSION + 1)).toEqual(LATEST_PLATFORM_VERSION);
89
+ });
52
90
  test('should be able to deserialize consensus error', () => {
53
91
  const err = sdk.utils.deserializeConsensusError('AwIiSW52YWxpZCBTdGF0ZSBUcmFuc2l0aW9uIHNpZ25hdHVyZQ');
54
92
  expect(err).toEqual('Invalid State Transition signature');