dash-platform-sdk 1.3.0-dev.5 → 1.3.0-dev.7

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 (140) hide show
  1. package/bundle.min.js +17 -17
  2. package/index.d.ts +1 -1
  3. package/index.js +2 -0
  4. package/package.json +7 -5
  5. package/proto/generated/google/protobuf/wrappers.js +615 -0
  6. package/proto/generated/platform.client.d.ts +46 -46
  7. package/proto/generated/platform.client.js +175 -0
  8. package/proto/generated/platform.d.ts +1 -1
  9. package/proto/generated/platform.js +8277 -0
  10. package/src/DashPlatformSDK.d.ts +14 -14
  11. package/src/DashPlatformSDK.js +101 -0
  12. package/src/constants.js +10 -0
  13. package/src/contestedResources/createStateTransition.js +6 -0
  14. package/src/contestedResources/getContestedResourceVoteState.d.ts +3 -3
  15. package/src/contestedResources/getContestedResourceVoteState.js +72 -0
  16. package/src/contestedResources/index.d.ts +3 -3
  17. package/src/contestedResources/index.js +30 -0
  18. package/src/dataContracts/create.d.ts +1 -1
  19. package/src/dataContracts/create.js +11 -0
  20. package/src/dataContracts/createStateTransition.js +19 -0
  21. package/src/dataContracts/getDataContractByIdentifier.d.ts +2 -2
  22. package/src/dataContracts/getDataContractByIdentifier.js +44 -0
  23. package/src/dataContracts/index.d.ts +2 -2
  24. package/src/dataContracts/index.js +57 -0
  25. package/src/documents/create.d.ts +1 -1
  26. package/src/documents/create.js +4 -0
  27. package/src/documents/createStateTransition.d.ts +1 -1
  28. package/src/documents/createStateTransition.js +52 -0
  29. package/src/documents/index.d.ts +2 -2
  30. package/src/documents/index.js +83 -0
  31. package/src/documents/query.d.ts +2 -2
  32. package/src/documents/query.js +62 -0
  33. package/src/grpcConnectionPool.d.ts +2 -2
  34. package/src/grpcConnectionPool.js +79 -0
  35. package/src/identities/createStateTransition.d.ts +1 -1
  36. package/src/identities/createStateTransition.js +34 -0
  37. package/src/identities/getIdentityBalance.d.ts +2 -2
  38. package/src/identities/getIdentityBalance.js +40 -0
  39. package/src/identities/getIdentityByIdentifier.d.ts +2 -2
  40. package/src/identities/getIdentityByIdentifier.js +40 -0
  41. package/src/identities/getIdentityByNonUniquePublicKeyHash.d.ts +1 -1
  42. package/src/identities/getIdentityByNonUniquePublicKeyHash.js +44 -0
  43. package/src/identities/getIdentityByPublicKeyHash.d.ts +1 -1
  44. package/src/identities/getIdentityByPublicKeyHash.js +40 -0
  45. package/src/identities/getIdentityContractNonce.d.ts +2 -2
  46. package/src/identities/getIdentityContractNonce.js +43 -0
  47. package/src/identities/getIdentityNonce.d.ts +2 -2
  48. package/src/identities/getIdentityNonce.js +41 -0
  49. package/src/identities/getIdentityPublicKeys.d.ts +2 -2
  50. package/src/identities/getIdentityPublicKeys.js +47 -0
  51. package/src/identities/index.d.ts +2 -2
  52. package/src/identities/index.js +141 -0
  53. package/src/keyPair/deriveChild.js +3 -0
  54. package/src/keyPair/derivePath.js +3 -0
  55. package/src/keyPair/index.d.ts +1 -1
  56. package/src/keyPair/index.js +89 -0
  57. package/src/keyPair/mnemonicToSeed.js +4 -0
  58. package/src/names/index.d.ts +2 -2
  59. package/src/names/index.js +102 -0
  60. package/src/names/registerName.d.ts +1 -1
  61. package/src/names/registerName.js +64 -0
  62. package/src/names/searchByIdentity.d.ts +1 -1
  63. package/src/names/searchByIdentity.js +5 -0
  64. package/src/names/searchByName.d.ts +1 -1
  65. package/src/names/searchByName.js +16 -0
  66. package/src/names/testNameContested.js +3 -0
  67. package/src/names/validateName.js +11 -0
  68. package/src/node/epochs.d.ts +1 -1
  69. package/src/node/epochs.js +46 -0
  70. package/src/node/index.d.ts +3 -3
  71. package/src/node/index.js +43 -0
  72. package/src/node/status.d.ts +2 -2
  73. package/src/node/status.js +41 -0
  74. package/src/node/totalCredits.d.ts +2 -2
  75. package/src/node/totalCredits.js +35 -0
  76. package/src/signer/AbstractSigner.js +1 -0
  77. package/src/signer/PrivateKeySigner.d.ts +1 -0
  78. package/src/signer/PrivateKeySigner.js +65 -0
  79. package/src/signer/setSigner.d.ts +1 -1
  80. package/src/signer/setSigner.js +5 -0
  81. package/src/stateTransitions/broadcast.d.ts +1 -1
  82. package/src/stateTransitions/broadcast.js +10 -0
  83. package/src/stateTransitions/index.d.ts +1 -1
  84. package/src/stateTransitions/index.js +34 -0
  85. package/src/stateTransitions/waitForStateTransitionResult.d.ts +1 -1
  86. package/src/stateTransitions/waitForStateTransitionResult.js +8 -0
  87. package/src/tokens/createStateTransition.d.ts +1 -1
  88. package/src/tokens/createStateTransition.js +67 -0
  89. package/src/tokens/getIdentitiesTokenBalances.d.ts +2 -2
  90. package/src/tokens/getIdentitiesTokenBalances.js +43 -0
  91. package/src/tokens/getIdentityTokensBalances.d.ts +2 -2
  92. package/src/tokens/getIdentityTokensBalances.js +43 -0
  93. package/src/tokens/getTokenContractInfo.d.ts +2 -2
  94. package/src/tokens/getTokenContractInfo.js +43 -0
  95. package/src/tokens/getTokenDirectPurchasePrices.d.ts +2 -2
  96. package/src/tokens/getTokenDirectPurchasePrices.js +40 -0
  97. package/src/tokens/getTokenTotalSupply.d.ts +2 -2
  98. package/src/tokens/getTokenTotalSupply.js +41 -0
  99. package/src/tokens/index.d.ts +5 -5
  100. package/src/tokens/index.js +108 -0
  101. package/src/types.d.ts +2 -2
  102. package/src/types.js +15 -0
  103. package/src/utils/base58ToBytes.js +4 -0
  104. package/src/utils/bytesToHex.js +3 -0
  105. package/src/utils/bytesToTypedArray.js +3 -0
  106. package/src/utils/calculateMsgHash.js +31 -0
  107. package/src/utils/calculateSignHash.d.ts +1 -1
  108. package/src/utils/calculateSignHash.js +8 -0
  109. package/src/utils/calculateStateIdHash.d.ts +1 -1
  110. package/src/utils/calculateStateIdHash.js +10 -0
  111. package/src/utils/convertToHomographSafeChars.js +11 -0
  112. package/src/utils/createVoterIdentityId.js +13 -0
  113. package/src/utils/getEvonodeList.d.ts +1 -1
  114. package/src/utils/getEvonodeList.js +12 -0
  115. package/src/utils/getQuorumPublicKey.js +18 -0
  116. package/src/utils/getRandomArrayItem.js +3 -0
  117. package/src/utils/getRandomBytes.js +4 -0
  118. package/src/utils/hexToBytes.js +3 -0
  119. package/src/utils/index.js +61 -0
  120. package/src/utils/indexBytesToString.js +4 -0
  121. package/src/utils/sha256.js +15 -0
  122. package/src/utils/signHash.js +22 -0
  123. package/src/utils/signRequestId.js +19 -0
  124. package/src/utils/sleep.js +3 -0
  125. package/src/utils/stringToIndexValueBytes.js +10 -0
  126. package/src/utils/verifyTenderdashProof.d.ts +1 -1
  127. package/src/utils/verifyTenderdashProof.js +26 -0
  128. package/src/voting/createStateTransition.js +6 -0
  129. package/src/voting/createVote.js +5 -0
  130. package/src/voting/index.d.ts +1 -1
  131. package/src/voting/index.js +55 -0
  132. package/test/unit/ContestedResources.spec.js +258 -0
  133. package/test/unit/DataContract.spec.js +75 -0
  134. package/test/unit/Document.spec.js +109 -0
  135. package/test/unit/Identity.spec.js +232 -0
  136. package/test/unit/KeyPair.spec.js +34 -0
  137. package/test/unit/Names.spec.js +33 -0
  138. package/test/unit/Node.spec.js +58 -0
  139. package/test/unit/SDK.spec.js +10 -0
  140. package/test/unit/Tokens.spec.js +121 -0
@@ -0,0 +1,62 @@
1
+ import { GetDocumentsRequest } from '../../proto/generated/platform.js';
2
+ import { IdentifierWASM, PlatformVersionWASM, verifyDocumentsProof } from 'pshenmic-dpp';
3
+ import { DAPI_DEFAULT_LIMIT } 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
+ import { encode } from 'cbor-x';
8
+ import getDataContractByIdentifier from '../dataContracts/getDataContractByIdentifier.js';
9
+ export default async function query(grpcPool, dataContractId, documentTypeName, where, orderBy, limit = 100, startAt, startAfter) {
10
+ if ([startAt, startAfter].filter(e => e != null).length === 2) {
11
+ throw new Error('Only startAt or startAfter could be specified at one time');
12
+ }
13
+ let start;
14
+ if (startAt != null) {
15
+ start = {
16
+ oneofKind: 'startAt',
17
+ startAt: startAt.base58()
18
+ };
19
+ }
20
+ if (startAfter != null) {
21
+ start = {
22
+ oneofKind: 'startAfter',
23
+ startAt: startAfter.base58()
24
+ };
25
+ }
26
+ const getDocumentsRequest = GetDocumentsRequest.create({
27
+ version: {
28
+ oneofKind: 'v0',
29
+ v0: {
30
+ dataContractId: (new IdentifierWASM(dataContractId)).bytes(),
31
+ documentType: documentTypeName,
32
+ where: encode(where),
33
+ orderBy: encode(orderBy),
34
+ limit: limit ?? DAPI_DEFAULT_LIMIT,
35
+ start,
36
+ prove: true
37
+ }
38
+ }
39
+ });
40
+ const dataContract = await getDataContractByIdentifier(grpcPool, dataContractId);
41
+ const { response } = await grpcPool.getClient().getDocuments(getDocumentsRequest);
42
+ const { version } = response;
43
+ if (version.oneofKind !== 'v0') {
44
+ throw new Error('Unexpected oneOf type returned from DAPI (must be v0)');
45
+ }
46
+ const { v0 } = version;
47
+ if (v0.result.oneofKind !== 'proof') {
48
+ throw new Error('Unexpected oneOf type returned from DAPI (must be proof)');
49
+ }
50
+ const { result: { proof }, metadata } = v0;
51
+ if (metadata == null) {
52
+ throw new Error('Metadata not found');
53
+ }
54
+ const startAtIncluded = startAt != null;
55
+ const { rootHash, documents } = verifyDocumentsProof(proof.grovedbProof, dataContract, documentTypeName, where, orderBy, limit, startAt?.bytes(), startAtIncluded, BigInt(metadata?.timeMs), PlatformVersionWASM.PLATFORM_V9);
56
+ const quorumPublicKey = await getQuorumPublicKey(grpcPool.network, proof.quorumType, bytesToHex(proof.quorumHash));
57
+ const verify = await verifyTenderdashProof(proof, metadata, rootHash, quorumPublicKey);
58
+ if (!verify) {
59
+ throw new Error('Failed to verify query');
60
+ }
61
+ return documents ?? [];
62
+ }
@@ -1,5 +1,5 @@
1
- import { PlatformClient } from '../proto/generated/platform.client';
2
- import { Network } from './types';
1
+ import { PlatformClient } from '../proto/generated/platform.client.js';
2
+ import { Network } from './types.js';
3
3
  export type MasternodeList = Record<string, MasternodeInfo>;
4
4
  export interface GRPCOptions {
5
5
  poolLimit: 5;
@@ -0,0 +1,79 @@
1
+ import { GrpcWebFetchTransport } from '@protobuf-ts/grpcweb-transport';
2
+ import { PlatformClient } from '../proto/generated/platform.client.js';
3
+ import getEvonodeList from './utils/getEvonodeList.js';
4
+ import { GetStatusRequest } from '../proto/generated/platform.js';
5
+ import getRandomArrayItem from './utils/getRandomArrayItem.js';
6
+ const GRPC_DEFAULT_POOL_LIMIT = 5;
7
+ const seedNodes = {
8
+ testnet: [
9
+ // seed-1.pshenmic.dev
10
+ 'https://158.160.14.115:1443'
11
+ ],
12
+ mainnet: [
13
+ // seed-1.pshenmic.dev
14
+ 'https://158.160.14.115:443'
15
+ // mainnet dcg seeds
16
+ // 'https://158.160.14.115',
17
+ // 'https://3.0.60.103',
18
+ // 'https://34.211.174.194'
19
+ ]
20
+ };
21
+ const createClient = (url, abortController) => {
22
+ return new PlatformClient(new GrpcWebFetchTransport({
23
+ baseUrl: url,
24
+ abort: abortController?.signal
25
+ }));
26
+ };
27
+ export default class GRPCConnectionPool {
28
+ dapiUrls;
29
+ network;
30
+ constructor(network, grpcOptions) {
31
+ const grpcPoolLimit = grpcOptions?.poolLimit ?? GRPC_DEFAULT_POOL_LIMIT;
32
+ this.network = network;
33
+ this._initialize(network, grpcPoolLimit, grpcOptions?.dapiUrl).catch(console.error);
34
+ }
35
+ async _initialize(network, poolLimit, dapiUrl) {
36
+ if (typeof dapiUrl === 'string') {
37
+ this.dapiUrls = [dapiUrl];
38
+ return;
39
+ }
40
+ if (Array.isArray(dapiUrl)) {
41
+ this.dapiUrls = dapiUrl;
42
+ return;
43
+ }
44
+ if (dapiUrl != null) {
45
+ throw new Error('Unrecognized DAPI URL');
46
+ }
47
+ // Add default seed nodes
48
+ this.dapiUrls = seedNodes[network];
49
+ // retrieve last evonodes list
50
+ const evonodeList = await getEvonodeList(network);
51
+ // map it to array of dapiUrls
52
+ const networkDAPIUrls = Object.entries(evonodeList)
53
+ .map(([, info]) => info)
54
+ .filter((info) => info.status === 'ENABLED')
55
+ .map((info) => {
56
+ const [host] = info.address.split(':');
57
+ return `https://${host}:${info.platformHTTPPort}`;
58
+ });
59
+ // healthcheck nodes
60
+ for (const url of networkDAPIUrls) {
61
+ if (this.dapiUrls.length > poolLimit) {
62
+ break;
63
+ }
64
+ try {
65
+ const client = createClient(url);
66
+ const { response } = await client.getStatus(GetStatusRequest.create({}));
67
+ if (response.version.oneofKind === 'v0' && response.version.v0.chain != null) {
68
+ this.dapiUrls.push(url);
69
+ }
70
+ }
71
+ catch (e) {
72
+ }
73
+ }
74
+ }
75
+ getClient(abortController) {
76
+ const dapiUrl = getRandomArrayItem(this.dapiUrls);
77
+ return createClient(dapiUrl, abortController);
78
+ }
79
+ }
@@ -1,3 +1,3 @@
1
1
  import { StateTransitionWASM } from 'pshenmic-dpp';
2
- import { IdentityTransitionParams } from '../types';
2
+ import { IdentityTransitionParams } from '../types.js';
3
3
  export default function createStateTransition(type: 'create' | 'update' | 'topUp', params: IdentityTransitionParams): StateTransitionWASM;
@@ -0,0 +1,34 @@
1
+ import { IdentityCreateTransitionWASM, IdentityTopUpTransitionWASM, IdentityUpdateTransitionWASM } from 'pshenmic-dpp';
2
+ const identityTransitionsMap = {
3
+ create: {
4
+ class: IdentityCreateTransitionWASM,
5
+ arguments: ['publicKeys', 'assetLockProof'],
6
+ optionalArguments: ['signature', 'userFeeIncrease']
7
+ },
8
+ topUp: {
9
+ class: IdentityTopUpTransitionWASM,
10
+ arguments: ['assetLockProof', 'identityId'],
11
+ optionalArguments: ['userFeeIncrease']
12
+ },
13
+ update: {
14
+ class: IdentityUpdateTransitionWASM,
15
+ arguments: ['identityId', 'revision', 'identityNonce', 'addPublicKeys', 'disablePublicKeyIds'],
16
+ optionalArguments: ['userFeeIncrease']
17
+ }
18
+ };
19
+ export default function createStateTransition(type, params) {
20
+ const { class: TransitionClass, arguments: classArguments, optionalArguments } = identityTransitionsMap[type];
21
+ if (TransitionClass == null) {
22
+ throw new Error(`Unimplemented transition type: ${type}`);
23
+ }
24
+ const [missingArgument] = classArguments
25
+ .filter((classArgument) => params[classArgument] == null &&
26
+ !(optionalArguments).includes(classArgument));
27
+ if (missingArgument != null) {
28
+ throw new Error(`Token transition param "${missingArgument}" is missing`);
29
+ }
30
+ const transitionParams = classArguments.map((classArgument) => params[classArgument]);
31
+ // @ts-expect-error
32
+ const identityTransition = new TransitionClass(...transitionParams);
33
+ return identityTransition.toStateTransition();
34
+ }
@@ -1,3 +1,3 @@
1
- import { IdentifierLike } from '../types';
2
- import GRPCConnectionPool from '../grpcConnectionPool';
1
+ import { IdentifierLike } from '../types.js';
2
+ import GRPCConnectionPool from '../grpcConnectionPool.js';
3
3
  export default function getIdentityBalance(grpcPool: GRPCConnectionPool, identifier: IdentifierLike): Promise<bigint>;
@@ -0,0 +1,40 @@
1
+ import { GetIdentityBalanceRequest } from '../../proto/generated/platform.js';
2
+ import { IdentifierWASM, PlatformVersionWASM, verifyIdentityBalanceProof } from 'pshenmic-dpp';
3
+ import verifyTenderdashProof from '../utils/verifyTenderdashProof.js';
4
+ import { getQuorumPublicKey } from '../utils/getQuorumPublicKey.js';
5
+ import bytesToHex from '../utils/bytesToHex.js';
6
+ export default async function getIdentityBalance(grpcPool, identifier) {
7
+ const id = new IdentifierWASM(identifier);
8
+ const getIdentityBalanceRequest = GetIdentityBalanceRequest.create({
9
+ version: {
10
+ oneofKind: 'v0',
11
+ v0: {
12
+ id: id.bytes(),
13
+ prove: true
14
+ }
15
+ }
16
+ });
17
+ const { response } = await grpcPool.getClient().getIdentityBalance(getIdentityBalanceRequest);
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, balance } = verifyIdentityBalanceProof(proof.grovedbProof, id.bytes(), true, PlatformVersionWASM.PLATFORM_V9);
31
+ if (balance == null) {
32
+ throw new Error(`Failed to fetch balance for identifier ${id.base58()}`);
33
+ }
34
+ const quorumPublicKey = await getQuorumPublicKey(grpcPool.network, proof.quorumType, bytesToHex(proof.quorumHash));
35
+ const verify = await verifyTenderdashProof(proof, metadata, rootHash, quorumPublicKey);
36
+ if (!verify) {
37
+ throw new Error('Failed to verify query');
38
+ }
39
+ return BigInt(balance);
40
+ }
@@ -1,4 +1,4 @@
1
1
  import { IdentityWASM } from 'pshenmic-dpp';
2
- import { IdentifierLike } from '../types';
3
- import GRPCConnectionPool from '../grpcConnectionPool';
2
+ import { IdentifierLike } from '../types.js';
3
+ import GRPCConnectionPool from '../grpcConnectionPool.js';
4
4
  export default function getIdentityByIdentifier(grpcPool: GRPCConnectionPool, identifier: IdentifierLike): Promise<IdentityWASM>;
@@ -0,0 +1,40 @@
1
+ import { GetIdentityRequest } from '../../proto/generated/platform.js';
2
+ import { IdentifierWASM, PlatformVersionWASM, verifyIdentityByIdentifierProof } from 'pshenmic-dpp';
3
+ import { getQuorumPublicKey } from '../utils/getQuorumPublicKey.js';
4
+ import bytesToHex from '../utils/bytesToHex.js';
5
+ import verifyTenderdashProof from '../utils/verifyTenderdashProof.js';
6
+ export default async function getIdentityByIdentifier(grpcPool, identifier) {
7
+ const id = new IdentifierWASM(identifier);
8
+ const getIdentityRequest = GetIdentityRequest.create({
9
+ version: {
10
+ oneofKind: 'v0',
11
+ v0: {
12
+ id: id.bytes(),
13
+ prove: true
14
+ }
15
+ }
16
+ });
17
+ const { response } = await grpcPool.getClient().getIdentity(getIdentityRequest);
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, identity } = verifyIdentityByIdentifierProof(proof.grovedbProof, id, true, PlatformVersionWASM.PLATFORM_V9);
31
+ if (identity == null) {
32
+ throw new Error(`Identity with identifier ${id.base58()} not found`);
33
+ }
34
+ const quorumPublicKey = await getQuorumPublicKey(grpcPool.network, proof.quorumType, bytesToHex(proof.quorumHash));
35
+ const verify = await verifyTenderdashProof(proof, metadata, rootHash, quorumPublicKey);
36
+ if (!verify) {
37
+ throw new Error('Failed to verify query');
38
+ }
39
+ return identity;
40
+ }
@@ -1,3 +1,3 @@
1
1
  import { IdentityWASM } from 'pshenmic-dpp';
2
- import GRPCConnectionPool from '../grpcConnectionPool';
2
+ import GRPCConnectionPool from '../grpcConnectionPool.js';
3
3
  export default function getIdentityByNonUniquePublicKeyHash(grpcPool: GRPCConnectionPool, hex: string): Promise<IdentityWASM>;
@@ -0,0 +1,44 @@
1
+ import { GetIdentityByNonUniquePublicKeyHashRequest } from '../../proto/generated/platform.js';
2
+ import { PlatformVersionWASM, verifyIdentifierByNonUniquePublicKeyHashProof } from 'pshenmic-dpp';
3
+ import hexToBytes from '../utils/hexToBytes.js';
4
+ import { getQuorumPublicKey } from '../utils/getQuorumPublicKey.js';
5
+ import bytesToHex from '../utils/bytesToHex.js';
6
+ import verifyTenderdashProof from '../utils/verifyTenderdashProof.js';
7
+ import getIdentityByIdentifier from './getIdentityByIdentifier.js';
8
+ export default async function getIdentityByNonUniquePublicKeyHash(grpcPool, hex) {
9
+ const getIdentityByNonUniquePublicKeyHashRequest = GetIdentityByNonUniquePublicKeyHashRequest.create({
10
+ version: {
11
+ oneofKind: 'v0',
12
+ v0: {
13
+ publicKeyHash: hexToBytes(hex),
14
+ prove: true
15
+ }
16
+ }
17
+ });
18
+ const { response } = await grpcPool.getClient().getIdentityByNonUniquePublicKeyHash(getIdentityByNonUniquePublicKeyHashRequest);
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
+ if (proof.grovedbIdentityPublicKeyHashProof == null) {
32
+ throw new Error('GroveDB proof not found for identity by non unique public key request');
33
+ }
34
+ const { rootHash, identifier } = verifyIdentifierByNonUniquePublicKeyHashProof(proof.grovedbIdentityPublicKeyHashProof.grovedbProof, false, hexToBytes(hex), undefined, PlatformVersionWASM.PLATFORM_V9);
35
+ if (identifier == null) {
36
+ throw new Error(`Identity with non unique public key hash ${hex} not found`);
37
+ }
38
+ const quorumPublicKey = await getQuorumPublicKey(grpcPool.network, proof.grovedbIdentityPublicKeyHashProof.quorumType, bytesToHex(proof.grovedbIdentityPublicKeyHashProof.quorumHash));
39
+ const verify = await verifyTenderdashProof(proof.grovedbIdentityPublicKeyHashProof, metadata, rootHash, quorumPublicKey);
40
+ if (!verify) {
41
+ throw new Error('Failed to verify query');
42
+ }
43
+ return await getIdentityByIdentifier(grpcPool, identifier);
44
+ }
@@ -1,3 +1,3 @@
1
1
  import { IdentityWASM } from 'pshenmic-dpp';
2
- import GRPCConnectionPool from '../grpcConnectionPool';
2
+ import GRPCConnectionPool from '../grpcConnectionPool.js';
3
3
  export default function getIdentityByPublicKeyHash(grpcPool: GRPCConnectionPool, hex: string): Promise<IdentityWASM>;
@@ -0,0 +1,40 @@
1
+ import { GetIdentityByPublicKeyHashRequest } from '../../proto/generated/platform.js';
2
+ import { PlatformVersionWASM, verifyIdentityByUniqueKeyHashProof } from 'pshenmic-dpp';
3
+ import hexToBytes from '../utils/hexToBytes.js';
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 getIdentityByPublicKeyHash(grpcPool, hex) {
8
+ const getIdentityByPublicKeyHashRequest = GetIdentityByPublicKeyHashRequest.create({
9
+ version: {
10
+ oneofKind: 'v0',
11
+ v0: {
12
+ publicKeyHash: hexToBytes(hex),
13
+ prove: true
14
+ }
15
+ }
16
+ });
17
+ const { response } = await grpcPool.getClient().getIdentityByPublicKeyHash(getIdentityByPublicKeyHashRequest);
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, identity } = verifyIdentityByUniqueKeyHashProof(proof.grovedbProof, hexToBytes(hex), PlatformVersionWASM.PLATFORM_V9);
31
+ if (identity == null) {
32
+ throw new Error(`Identity with public key hash ${hex} not found`);
33
+ }
34
+ const quorumPublicKey = await getQuorumPublicKey(grpcPool.network, proof.quorumType, bytesToHex(proof.quorumHash));
35
+ const verify = await verifyTenderdashProof(proof, metadata, rootHash, quorumPublicKey);
36
+ if (!verify) {
37
+ throw new Error('Failed to verify query');
38
+ }
39
+ return identity;
40
+ }
@@ -1,3 +1,3 @@
1
- import { IdentifierLike } from '../types';
2
- import GRPCConnectionPool from '../grpcConnectionPool';
1
+ import { IdentifierLike } from '../types.js';
2
+ import GRPCConnectionPool from '../grpcConnectionPool.js';
3
3
  export default function getIdentityContractNonce(grpcPool: GRPCConnectionPool, identity: IdentifierLike, dataContract: IdentifierLike): Promise<bigint>;
@@ -0,0 +1,43 @@
1
+ import { GetIdentityContractNonceRequest } from '../../proto/generated/platform.js';
2
+ import { IdentifierWASM, PlatformVersionWASM, verifyIdentityContractNonceProof } from 'pshenmic-dpp';
3
+ import { getQuorumPublicKey } from '../utils/getQuorumPublicKey.js';
4
+ import bytesToHex from '../utils/bytesToHex.js';
5
+ import verifyTenderdashProof from '../utils/verifyTenderdashProof.js';
6
+ const IDENTITY_CONTRACT_NONCE_VALUE_FILTER = BigInt(0xFFFFFFFFFF);
7
+ export default async function getIdentityContractNonce(grpcPool, identity, dataContract) {
8
+ const identityIdentifier = new IdentifierWASM(identity);
9
+ const dataContractIdentifier = new IdentifierWASM(dataContract);
10
+ const getIdentityContractNonceRequest = GetIdentityContractNonceRequest.create({
11
+ version: {
12
+ oneofKind: 'v0',
13
+ v0: {
14
+ identityId: identityIdentifier.bytes(),
15
+ contractId: dataContractIdentifier.bytes(),
16
+ prove: true
17
+ }
18
+ }
19
+ });
20
+ const { response } = await grpcPool.getClient().getIdentityContractNonce(getIdentityContractNonceRequest);
21
+ const { version } = response;
22
+ if (version.oneofKind !== 'v0') {
23
+ throw new Error('Unexpected oneOf type returned from DAPI (must be v0)');
24
+ }
25
+ const { v0 } = version;
26
+ if (v0.result.oneofKind !== 'proof') {
27
+ throw new Error('Unexpected oneOf type returned from DAPI (must be proof)');
28
+ }
29
+ const { result: { proof }, metadata } = v0;
30
+ if (metadata == null) {
31
+ throw new Error('Metadata not found');
32
+ }
33
+ const { rootHash, contractNonce } = verifyIdentityContractNonceProof(proof.grovedbProof, identityIdentifier.bytes(), dataContractIdentifier.bytes(), true, PlatformVersionWASM.PLATFORM_V9);
34
+ if (contractNonce == null) {
35
+ return BigInt(0);
36
+ }
37
+ const quorumPublicKey = await getQuorumPublicKey(grpcPool.network, proof.quorumType, bytesToHex(proof.quorumHash));
38
+ const verify = await verifyTenderdashProof(proof, metadata, rootHash, quorumPublicKey);
39
+ if (!verify) {
40
+ throw new Error('Failed to verify query');
41
+ }
42
+ return BigInt(contractNonce) & IDENTITY_CONTRACT_NONCE_VALUE_FILTER;
43
+ }
@@ -1,3 +1,3 @@
1
- import { IdentifierLike } from '../types';
2
- import GRPCConnectionPool from '../grpcConnectionPool';
1
+ import { IdentifierLike } from '../types.js';
2
+ import GRPCConnectionPool from '../grpcConnectionPool.js';
3
3
  export default function getIdentityNonce(grpcPool: GRPCConnectionPool, identifier: IdentifierLike): Promise<bigint>;
@@ -0,0 +1,41 @@
1
+ import { GetIdentityNonceRequest } from '../../proto/generated/platform.js';
2
+ import { IdentifierWASM, PlatformVersionWASM, verifyIdentityNonceProof } from 'pshenmic-dpp';
3
+ import { getQuorumPublicKey } from '../utils/getQuorumPublicKey.js';
4
+ import bytesToHex from '../utils/bytesToHex.js';
5
+ import verifyTenderdashProof from '../utils/verifyTenderdashProof.js';
6
+ const IDENTITY_NONCE_VALUE_FILTER = BigInt(0xFFFFFFFFFF);
7
+ export default async function getIdentityNonce(grpcPool, identifier) {
8
+ const id = new IdentifierWASM(identifier);
9
+ const getIdentityNonceRequest = GetIdentityNonceRequest.create({
10
+ version: {
11
+ oneofKind: 'v0',
12
+ v0: {
13
+ identityId: id.bytes(),
14
+ prove: true
15
+ }
16
+ }
17
+ });
18
+ const { response } = await grpcPool.getClient().getIdentityNonce(getIdentityNonceRequest);
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, nonce } = verifyIdentityNonceProof(proof.grovedbProof, id.bytes(), true, PlatformVersionWASM.PLATFORM_V9);
32
+ if (nonce == null) {
33
+ return BigInt(0);
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 BigInt(nonce) & IDENTITY_NONCE_VALUE_FILTER;
41
+ }
@@ -1,4 +1,4 @@
1
1
  import { IdentityPublicKeyWASM } from 'pshenmic-dpp';
2
- import { IdentifierLike } from '../types';
3
- import GRPCConnectionPool from '../grpcConnectionPool';
2
+ import { IdentifierLike } from '../types.js';
3
+ import GRPCConnectionPool from '../grpcConnectionPool.js';
4
4
  export default function getIdentityPublicKeys(grpcPool: GRPCConnectionPool, identifier: IdentifierLike): Promise<IdentityPublicKeyWASM[]>;
@@ -0,0 +1,47 @@
1
+ import { GetIdentityKeysRequest, KeyRequestType } from '../../proto/generated/platform.js';
2
+ import { IdentifierWASM, PlatformVersionWASM, verifyIdentityKeysByIdentifierProof } from 'pshenmic-dpp';
3
+ import { getQuorumPublicKey } from '../utils/getQuorumPublicKey.js';
4
+ import bytesToHex from '../utils/bytesToHex.js';
5
+ import verifyTenderdashProof from '../utils/verifyTenderdashProof.js';
6
+ export default async function getIdentityPublicKeys(grpcPool, identifier) {
7
+ const id = new IdentifierWASM(identifier);
8
+ const getIdentityKeysRequest = GetIdentityKeysRequest.create({
9
+ version: {
10
+ oneofKind: 'v0',
11
+ v0: {
12
+ identityId: id.bytes(),
13
+ requestType: KeyRequestType.create({
14
+ request: {
15
+ oneofKind: 'allKeys',
16
+ allKeys: {}
17
+ }
18
+ }),
19
+ prove: true
20
+ }
21
+ }
22
+ });
23
+ const { response } = await grpcPool.getClient().getIdentityKeys(getIdentityKeysRequest);
24
+ const { version } = response;
25
+ if (version.oneofKind !== 'v0') {
26
+ throw new Error('Unexpected oneOf type returned from DAPI (must be v0)');
27
+ }
28
+ const { v0 } = version;
29
+ if (v0.result.oneofKind !== 'proof') {
30
+ throw new Error('Unexpected oneOf type returned from DAPI (must be proof)');
31
+ }
32
+ const { result: { proof }, metadata } = v0;
33
+ if (metadata == null) {
34
+ throw new Error('Metadata not found');
35
+ }
36
+ const { rootHash, identity } = verifyIdentityKeysByIdentifierProof(proof.grovedbProof, id.bytes(), null, false, false, true, null, null, PlatformVersionWASM.PLATFORM_V9);
37
+ if (identity == null) {
38
+ throw new Error(`Identity with identifier ${id.base58()} not found`);
39
+ }
40
+ const quorumPublicKey = await getQuorumPublicKey(grpcPool.network, proof.quorumType, bytesToHex(proof.quorumHash));
41
+ const verify = await verifyTenderdashProof(proof, metadata, rootHash, quorumPublicKey);
42
+ if (!verify) {
43
+ throw new Error('Failed to verify query');
44
+ }
45
+ const loadedKeysIds = Object.keys(identity.loadedPublicKeys);
46
+ return loadedKeysIds.map((id) => identity.loadedPublicKeys[id]);
47
+ }
@@ -1,5 +1,5 @@
1
- import { IdentifierLike, IdentityTransitionParams } from '../types';
2
- import GRPCConnectionPool from '../grpcConnectionPool';
1
+ import { IdentifierLike, IdentityTransitionParams } from '../types.js';
2
+ import GRPCConnectionPool from '../grpcConnectionPool.js';
3
3
  import { IdentityPublicKeyWASM, IdentityWASM, StateTransitionWASM } from 'pshenmic-dpp';
4
4
  /**
5
5
  * Collection of methods to query identities and its related data