dash-platform-sdk 1.6.0-dev.2 → 1.6.0-dev.3
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.
- package/README.md +33 -15
- package/bundle.min.js +1 -1
- package/index.d.ts +3 -1
- package/index.js +2 -1
- package/package.json +2 -2
- package/proto/generated/platform.d.ts +128 -2
- package/proto/generated/platform.js +266 -8
- package/src/DashPlatformSDK.d.ts +19 -1
- package/src/DashPlatformSDK.js +28 -10
- package/src/QueryExecutor.d.ts +122 -0
- package/src/QueryExecutor.js +205 -0
- package/src/constants.d.ts +6 -0
- package/src/constants.js +6 -0
- package/src/contestedResources/getContestedResourceVoteState.d.ts +2 -1
- package/src/contestedResources/getContestedResourceVoteState.js +69 -49
- package/src/contestedResources/index.d.ts +4 -1
- package/src/contestedResources/index.js +5 -2
- package/src/dataContracts/getDataContractByIdentifier.d.ts +2 -1
- package/src/dataContracts/getDataContractByIdentifier.js +15 -27
- package/src/dataContracts/index.d.ts +4 -1
- package/src/dataContracts/index.js +5 -2
- package/src/documents/getActionFeeAgreement.d.ts +2 -1
- package/src/documents/getActionFeeAgreement.js +3 -3
- package/src/documents/index.d.ts +12 -8
- package/src/documents/index.js +16 -13
- package/src/documents/query.d.ts +2 -1
- package/src/documents/query.js +16 -8
- package/src/documents/queryAggregate.d.ts +8 -1
- package/src/documents/queryAggregate.js +49 -15
- package/src/documents/queryJoined.d.ts +3 -2
- package/src/documents/queryJoined.js +54 -13
- package/src/documents/queryRanked.d.ts +3 -2
- package/src/documents/queryRanked.js +49 -15
- package/src/documents/v1.d.ts +25 -8
- package/src/documents/v1.js +50 -14
- package/src/grpcConnectionPool.d.ts +24 -4
- package/src/grpcConnectionPool.js +153 -37
- package/src/identities/getIdentityBalance.d.ts +2 -1
- package/src/identities/getIdentityBalance.js +13 -27
- package/src/identities/getIdentityByIdentifier.d.ts +2 -1
- package/src/identities/getIdentityByIdentifier.js +14 -28
- package/src/identities/getIdentityByNonUniquePublicKeyHash.d.ts +2 -1
- package/src/identities/getIdentityByNonUniquePublicKeyHash.js +49 -28
- package/src/identities/getIdentityByPublicKeyHash.d.ts +2 -1
- package/src/identities/getIdentityByPublicKeyHash.js +14 -28
- package/src/identities/getIdentityContractNonce.d.ts +2 -1
- package/src/identities/getIdentityContractNonce.js +10 -27
- package/src/identities/getIdentityNonce.d.ts +2 -1
- package/src/identities/getIdentityNonce.js +10 -27
- package/src/identities/getIdentityPublicKeys.d.ts +2 -1
- package/src/identities/getIdentityPublicKeys.js +16 -29
- package/src/identities/index.d.ts +4 -1
- package/src/identities/index.js +11 -8
- package/src/names/index.d.ts +4 -1
- package/src/names/index.js +8 -5
- package/src/names/registerName.d.ts +2 -1
- package/src/names/registerName.js +4 -4
- package/src/names/searchByIdentity.d.ts +2 -1
- package/src/names/searchByIdentity.js +2 -2
- package/src/names/searchByName.d.ts +2 -1
- package/src/names/searchByName.js +2 -2
- package/src/node/epochInfos.d.ts +2 -1
- package/src/node/epochInfos.js +27 -31
- package/src/node/finalizedEpochInfos.d.ts +4 -3
- package/src/node/finalizedEpochInfos.js +45 -39
- package/src/node/index.d.ts +4 -1
- package/src/node/index.js +7 -4
- package/src/node/totalCredits.d.ts +2 -1
- package/src/node/totalCredits.js +10 -25
- package/src/platformAddresses/getAddressInfo.d.ts +2 -1
- package/src/platformAddresses/getAddressInfo.js +17 -31
- package/src/platformAddresses/getAddressesBranchState.d.ts +2 -1
- package/src/platformAddresses/getAddressesBranchState.js +22 -8
- package/src/platformAddresses/getAddressesInfos.d.ts +2 -1
- package/src/platformAddresses/getAddressesInfos.js +18 -29
- package/src/platformAddresses/getAddressesTrunkState.d.ts +2 -1
- package/src/platformAddresses/getAddressesTrunkState.js +25 -23
- package/src/platformAddresses/getRecentAddressBalanceChanges.d.ts +2 -1
- package/src/platformAddresses/getRecentAddressBalanceChanges.js +26 -26
- package/src/platformAddresses/getRecentCompactedAddressBalanceChanges.d.ts +2 -1
- package/src/platformAddresses/getRecentCompactedAddressBalanceChanges.js +30 -26
- package/src/platformAddresses/index.d.ts +4 -1
- package/src/platformAddresses/index.js +10 -7
- package/src/quorumKeyProvider.d.ts +23 -0
- package/src/quorumKeyProvider.js +28 -0
- package/src/shielded/getMostRecentShieldedAnchor.d.ts +2 -1
- package/src/shielded/getMostRecentShieldedAnchor.js +8 -25
- package/src/shielded/getShieldedAnchors.d.ts +2 -1
- package/src/shielded/getShieldedAnchors.js +8 -25
- package/src/shielded/getShieldedEncryptedNotes.d.ts +2 -1
- package/src/shielded/getShieldedEncryptedNotes.js +16 -30
- package/src/shielded/getShieldedNotesCount.d.ts +2 -1
- package/src/shielded/getShieldedNotesCount.js +8 -25
- package/src/shielded/getShieldedNullifiers.d.ts +2 -1
- package/src/shielded/getShieldedNullifiers.js +14 -28
- package/src/shielded/getShieldedPoolState.d.ts +2 -1
- package/src/shielded/getShieldedPoolState.js +8 -25
- package/src/shielded/index.d.ts +4 -1
- package/src/shielded/index.js +10 -7
- package/src/stateTransitions/index.d.ts +4 -1
- package/src/stateTransitions/index.js +5 -2
- package/src/stateTransitions/waitForStateTransitionResult.d.ts +2 -1
- package/src/stateTransitions/waitForStateTransitionResult.js +42 -44
- package/src/tokens/getIdentitiesTokenBalances.d.ts +2 -1
- package/src/tokens/getIdentitiesTokenBalances.js +17 -28
- package/src/tokens/getIdentityTokensBalances.d.ts +2 -1
- package/src/tokens/getIdentityTokensBalances.js +17 -28
- package/src/tokens/getTokenContractInfo.d.ts +2 -1
- package/src/tokens/getTokenContractInfo.js +16 -30
- package/src/tokens/getTokenDirectPurchasePrices.d.ts +2 -1
- package/src/tokens/getTokenDirectPurchasePrices.js +26 -28
- package/src/tokens/getTokenTotalSupply.d.ts +2 -1
- package/src/tokens/getTokenTotalSupply.js +16 -28
- package/src/tokens/index.d.ts +4 -1
- package/src/tokens/index.js +11 -8
- package/src/utils/ProofError.d.ts +9 -0
- package/src/utils/ProofError.js +10 -0
- package/src/utils/verifyTenderdashProof.js +2 -1
- package/test/unit/SDK.spec.js +42 -0
- package/src/utils/getQuorumPublicKey.d.ts +0 -1
- package/src/utils/getQuorumPublicKey.js +0 -19
package/src/DashPlatformSDK.js
CHANGED
|
@@ -12,6 +12,7 @@ import { TokensController } from './tokens/index.js';
|
|
|
12
12
|
import { VotingController } from './voting/index.js';
|
|
13
13
|
import { PlatformAddressesController } from './platformAddresses/index.js';
|
|
14
14
|
import { ShieldedController } from './shielded/index.js';
|
|
15
|
+
import { PlatformExplorerQuorumKeyProvider } from './quorumKeyProvider.js';
|
|
15
16
|
/**
|
|
16
17
|
* Javascript SDK for that let you interact with a Dash Platform blockchain
|
|
17
18
|
*/
|
|
@@ -20,6 +21,8 @@ export class DashPlatformSDK {
|
|
|
20
21
|
/** @ignore **/
|
|
21
22
|
grpcPool;
|
|
22
23
|
/** @ignore **/
|
|
24
|
+
quorumKeyProvider;
|
|
25
|
+
/** @ignore **/
|
|
23
26
|
options;
|
|
24
27
|
contestedResources;
|
|
25
28
|
platformAddresses;
|
|
@@ -67,18 +70,23 @@ export class DashPlatformSDK {
|
|
|
67
70
|
* @param network
|
|
68
71
|
*/
|
|
69
72
|
_initialize(grpcPool) {
|
|
73
|
+
if (this.options?.proofs != null) {
|
|
74
|
+
grpcPool.proofs = this.options.proofs;
|
|
75
|
+
}
|
|
76
|
+
const quorumKeyProvider = this.options?.quorumKeyProvider ?? new PlatformExplorerQuorumKeyProvider(grpcPool.network);
|
|
70
77
|
this.grpcPool = grpcPool;
|
|
71
|
-
this.
|
|
72
|
-
this.
|
|
73
|
-
this.
|
|
74
|
-
this.
|
|
75
|
-
this.
|
|
76
|
-
this.
|
|
77
|
-
this.
|
|
78
|
+
this.quorumKeyProvider = quorumKeyProvider;
|
|
79
|
+
this.contestedResources = new ContestedResourcesController(grpcPool, quorumKeyProvider);
|
|
80
|
+
this.platformAddresses = new PlatformAddressesController(grpcPool, quorumKeyProvider);
|
|
81
|
+
this.shielded = new ShieldedController(grpcPool, quorumKeyProvider);
|
|
82
|
+
this.stateTransitions = new StateTransitionsController(grpcPool, quorumKeyProvider);
|
|
83
|
+
this.dataContracts = new DataContractsController(grpcPool, quorumKeyProvider);
|
|
84
|
+
this.identities = new IdentitiesController(grpcPool, quorumKeyProvider);
|
|
85
|
+
this.documents = new DocumentsController(grpcPool, quorumKeyProvider);
|
|
78
86
|
this.voting = new VotingController();
|
|
79
|
-
this.node = new NodeController(grpcPool);
|
|
80
|
-
this.tokens = new TokensController(grpcPool);
|
|
81
|
-
this.names = new NamesController(grpcPool);
|
|
87
|
+
this.node = new NodeController(grpcPool, quorumKeyProvider);
|
|
88
|
+
this.tokens = new TokensController(grpcPool, quorumKeyProvider);
|
|
89
|
+
this.names = new NamesController(grpcPool, quorumKeyProvider);
|
|
82
90
|
this.keyPair = new KeyPairController();
|
|
83
91
|
}
|
|
84
92
|
/**
|
|
@@ -105,6 +113,16 @@ export class DashPlatformSDK {
|
|
|
105
113
|
this.network = network;
|
|
106
114
|
this._initialize(customPool ?? new GRPCConnectionPool(this.network, this.options?.grpc));
|
|
107
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Replaces source of quorum public keys used for proof verification.
|
|
118
|
+
* Pass nothing to switch back to Platform Explorer
|
|
119
|
+
*
|
|
120
|
+
* @param quorumKeyProvider {QuorumKeyProvider=}
|
|
121
|
+
*/
|
|
122
|
+
setQuorumKeyProvider(quorumKeyProvider) {
|
|
123
|
+
this.options = { ...(this.options ?? { network: this.network }), quorumKeyProvider };
|
|
124
|
+
this._initialize(this.grpcPool);
|
|
125
|
+
}
|
|
108
126
|
/**
|
|
109
127
|
* Replaces GRPC connection pool used by the SDK with a custom one.
|
|
110
128
|
* SDK network is switched to the pool's network
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { PlatformClient } from '../proto/generated/platform.client.js';
|
|
2
|
+
import { Proof, ResponseMetadata } from '../proto/generated/platform.js';
|
|
3
|
+
import { GRPCPool } from './grpcConnectionPool.js';
|
|
4
|
+
import { QuorumKeyProvider } from './quorumKeyProvider.js';
|
|
5
|
+
type SubType<Base, Condition> = Pick<Base, {
|
|
6
|
+
[Key in keyof Base]: Base[Key] extends Condition ? Key : never;
|
|
7
|
+
}[keyof Base]>;
|
|
8
|
+
type PlatformClientMethods = SubType<PlatformClient, (...args: any[]) => any>;
|
|
9
|
+
/**
|
|
10
|
+
* Proof and metadata of a response, `proof` and `metadata` are required to verify the quorum signature of the proof
|
|
11
|
+
*/
|
|
12
|
+
export interface ResponseProof {
|
|
13
|
+
proof: Proof;
|
|
14
|
+
metadata: ResponseMetadata;
|
|
15
|
+
}
|
|
16
|
+
type MapProof<Response, P extends ResponseProof = ResponseProof> = (response: Response) => P;
|
|
17
|
+
type GroveDBVerifier<Args extends unknown[], V> = (grovedbProof: Uint8Array, ...args: Args) => V;
|
|
18
|
+
/**
|
|
19
|
+
* Body of the version (e.g. `v0`) of a versioned response, distributed over all versions
|
|
20
|
+
*/
|
|
21
|
+
type VersionBody<Version> = Version extends {
|
|
22
|
+
oneofKind: infer K extends string;
|
|
23
|
+
} ? K extends keyof Version ? Version[K] : never : never;
|
|
24
|
+
/**
|
|
25
|
+
* Result of a response requested without proof, `version.v0.result` without the proof case
|
|
26
|
+
*/
|
|
27
|
+
export type ResponseResult<Response> = Response extends {
|
|
28
|
+
version: infer Version;
|
|
29
|
+
} ? VersionBody<Version> extends infer Body ? Body extends {
|
|
30
|
+
result: infer R;
|
|
31
|
+
} ? Exclude<R, {
|
|
32
|
+
oneofKind: 'proof' | undefined;
|
|
33
|
+
}> : never : never : never;
|
|
34
|
+
/**
|
|
35
|
+
* Data of a query passed to `mapResponse`, either verified from the proof (`proved: true`)
|
|
36
|
+
* or the result of the response requested without proof (`proved: false`)
|
|
37
|
+
*/
|
|
38
|
+
export type QueryData<Response, V> = {
|
|
39
|
+
proved: true;
|
|
40
|
+
verified: V;
|
|
41
|
+
metadata: ResponseMetadata;
|
|
42
|
+
} | {
|
|
43
|
+
proved: false;
|
|
44
|
+
result: ResponseResult<Response>;
|
|
45
|
+
metadata: ResponseMetadata;
|
|
46
|
+
};
|
|
47
|
+
type MapResponse<Response, V, T> = (data: QueryData<Response, V>) => T | Promise<T>;
|
|
48
|
+
/**
|
|
49
|
+
* Minimal shape of a v0 response that may contain a proof
|
|
50
|
+
*/
|
|
51
|
+
interface ProvedResponse {
|
|
52
|
+
version: {
|
|
53
|
+
oneofKind: string | undefined;
|
|
54
|
+
v0?: {
|
|
55
|
+
result: {
|
|
56
|
+
oneofKind: string | undefined;
|
|
57
|
+
proof?: Proof;
|
|
58
|
+
};
|
|
59
|
+
metadata?: ResponseMetadata;
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Returns the proof and the metadata of a v0 response, requested with `prove: true`
|
|
65
|
+
*/
|
|
66
|
+
export declare function getResponseProof(response: ProvedResponse): ResponseProof;
|
|
67
|
+
/**
|
|
68
|
+
* Sends a GRPC request to a node from the pool and returns the mapped result of the response.
|
|
69
|
+
*
|
|
70
|
+
* Queries are requested with proofs, unless proofs are disabled in the pool (`grpcPool.proofs === false`):
|
|
71
|
+
* - with proofs: request (`prove: true`) -> proof mapping -> GroveDB proof verification -> quorum signature verification -> mapping
|
|
72
|
+
* - without proofs: request (`prove: false`) -> mapping of the response result
|
|
73
|
+
*
|
|
74
|
+
* `mapResponse` handles both cases, the verified data or the response result, see {QueryData}.
|
|
75
|
+
* Requests without the `prove` field are always proved
|
|
76
|
+
*
|
|
77
|
+
* If the proof is invalid (ProofError), the node is banned and the request is sent again to another node,
|
|
78
|
+
* the node that returned a valid proof is unbanned. Any other error (including "not found") is thrown immediately
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* const balance = await QueryExecutor.build(grpcPool, quorumKeyProvider)
|
|
82
|
+
* .setGrpcRequest('getIdentityBalance', request)
|
|
83
|
+
* .setProofVerifier(verifyIdentityBalanceProof, id.bytes(), true, LATEST_PLATFORM_VERSION)
|
|
84
|
+
* .mapResponse(data => data.proved ? data.verified.balance : BigInt(data.result.balance))
|
|
85
|
+
* .execute()
|
|
86
|
+
*/
|
|
87
|
+
export default class QueryExecutor<Response = unknown, V = unknown, T = unknown, P extends ResponseProof = ResponseProof> {
|
|
88
|
+
#private;
|
|
89
|
+
private constructor();
|
|
90
|
+
static build(grpcPool: GRPCPool, quorumKeyProvider: QuorumKeyProvider): QueryExecutor;
|
|
91
|
+
/**
|
|
92
|
+
* Sets the GRPC request, it is sent again on every retry.
|
|
93
|
+
* The `prove` field of the request is set by the executor
|
|
94
|
+
*/
|
|
95
|
+
setGrpcRequest<M extends keyof PlatformClientMethods>(method: M, request: Parameters<PlatformClientMethods[M]>[0]): QueryExecutor<Awaited<ReturnType<PlatformClientMethods[M]>> extends {
|
|
96
|
+
response: infer R;
|
|
97
|
+
} ? R : never, V, T, P>;
|
|
98
|
+
/**
|
|
99
|
+
* Overrides the mapping of the response to the proof and the metadata (v0 response by default).
|
|
100
|
+
* Additional data of the proof (e.g. a second proof) is passed to the args callback of the proof verifier
|
|
101
|
+
*/
|
|
102
|
+
mapProof<E extends ResponseProof>(mapProof: MapProof<Response, E>): QueryExecutor<Response, V, T, E>;
|
|
103
|
+
/**
|
|
104
|
+
* Sets the GroveDB proof verification function, which is called with the GroveDB proof of the response
|
|
105
|
+
* followed by the given args. Args depending on the response (e.g. metadata) can be passed as a callback.
|
|
106
|
+
* Failure of the verification is thrown as ProofError, so the request is retried on another node
|
|
107
|
+
*/
|
|
108
|
+
setProofVerifier<Args extends unknown[] | [], U>(verify: GroveDBVerifier<Args, U>, getArgs: (responseProof: P) => NoInfer<Args>): QueryExecutor<Response, U, T, P>;
|
|
109
|
+
setProofVerifier<Args extends unknown[] | [], U>(verify: GroveDBVerifier<Args, U>, ...args: Args): QueryExecutor<Response, U, T, P>;
|
|
110
|
+
/**
|
|
111
|
+
* Sets the mapping of the query data to the result of the query, also the place for "not found" checks.
|
|
112
|
+
* The data is either verified from the proof or the result of the response requested without proof
|
|
113
|
+
*/
|
|
114
|
+
mapResponse<U>(mapResponse: MapResponse<Response, V, U>): QueryExecutor<Response, V, U, P>;
|
|
115
|
+
/**
|
|
116
|
+
* Enables (default) or disables the verification of the quorum signature of the proof,
|
|
117
|
+
* e.g. if the GroveDB proof is verified against an already verified root hash
|
|
118
|
+
*/
|
|
119
|
+
setValidateProof(validateProof: boolean): this;
|
|
120
|
+
execute(): Promise<T>;
|
|
121
|
+
}
|
|
122
|
+
export {};
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import bytesToHex from './utils/bytesToHex.js';
|
|
2
|
+
import verifyTenderdashProof from './utils/verifyTenderdashProof.js';
|
|
3
|
+
import ProofError from './utils/ProofError.js';
|
|
4
|
+
import sleep from './utils/sleep.js';
|
|
5
|
+
const RETRY_DELAY_MS = 10;
|
|
6
|
+
/**
|
|
7
|
+
* Returns the versioned body of a request or a response (e.g. `version.v0`)
|
|
8
|
+
*/
|
|
9
|
+
function getVersionBody(message) {
|
|
10
|
+
const { version } = (message ?? {});
|
|
11
|
+
if (version?.oneofKind == null) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
return version[version.oneofKind];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Returns the proof and the metadata of a v0 response, requested with `prove: true`
|
|
18
|
+
*/
|
|
19
|
+
export function getResponseProof(response) {
|
|
20
|
+
const { version } = response;
|
|
21
|
+
if (version.oneofKind !== 'v0' || version.v0 == null) {
|
|
22
|
+
throw new Error('Unexpected oneOf type returned from DAPI (must be v0)');
|
|
23
|
+
}
|
|
24
|
+
const { result, metadata } = version.v0;
|
|
25
|
+
if (result.oneofKind !== 'proof' || result.proof == null) {
|
|
26
|
+
throw new Error('Unexpected oneOf type returned from DAPI (must be proof)');
|
|
27
|
+
}
|
|
28
|
+
if (metadata == null) {
|
|
29
|
+
throw new Error('Metadata not found');
|
|
30
|
+
}
|
|
31
|
+
return { proof: result.proof, metadata };
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Sends a GRPC request to a node from the pool and returns the mapped result of the response.
|
|
35
|
+
*
|
|
36
|
+
* Queries are requested with proofs, unless proofs are disabled in the pool (`grpcPool.proofs === false`):
|
|
37
|
+
* - with proofs: request (`prove: true`) -> proof mapping -> GroveDB proof verification -> quorum signature verification -> mapping
|
|
38
|
+
* - without proofs: request (`prove: false`) -> mapping of the response result
|
|
39
|
+
*
|
|
40
|
+
* `mapResponse` handles both cases, the verified data or the response result, see {QueryData}.
|
|
41
|
+
* Requests without the `prove` field are always proved
|
|
42
|
+
*
|
|
43
|
+
* If the proof is invalid (ProofError), the node is banned and the request is sent again to another node,
|
|
44
|
+
* the node that returned a valid proof is unbanned. Any other error (including "not found") is thrown immediately
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* const balance = await QueryExecutor.build(grpcPool, quorumKeyProvider)
|
|
48
|
+
* .setGrpcRequest('getIdentityBalance', request)
|
|
49
|
+
* .setProofVerifier(verifyIdentityBalanceProof, id.bytes(), true, LATEST_PLATFORM_VERSION)
|
|
50
|
+
* .mapResponse(data => data.proved ? data.verified.balance : BigInt(data.result.balance))
|
|
51
|
+
* .execute()
|
|
52
|
+
*/
|
|
53
|
+
export default class QueryExecutor {
|
|
54
|
+
#grpcPool;
|
|
55
|
+
#quorumKeyProvider;
|
|
56
|
+
#request;
|
|
57
|
+
#requestMessage;
|
|
58
|
+
#mapProof = getResponseProof;
|
|
59
|
+
#verifyProof;
|
|
60
|
+
#mapResponse;
|
|
61
|
+
#validateProof = true;
|
|
62
|
+
constructor(grpcPool, quorumKeyProvider) {
|
|
63
|
+
this.#grpcPool = grpcPool;
|
|
64
|
+
this.#quorumKeyProvider = quorumKeyProvider;
|
|
65
|
+
}
|
|
66
|
+
static build(grpcPool, quorumKeyProvider) {
|
|
67
|
+
return new QueryExecutor(grpcPool, quorumKeyProvider);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Sets the GRPC request, it is sent again on every retry.
|
|
71
|
+
* The `prove` field of the request is set by the executor
|
|
72
|
+
*/
|
|
73
|
+
setGrpcRequest(method, request) {
|
|
74
|
+
const executor = this;
|
|
75
|
+
executor.#requestMessage = request;
|
|
76
|
+
executor.#request = (client) => {
|
|
77
|
+
const fn = client[method];
|
|
78
|
+
return fn.call(client, request);
|
|
79
|
+
};
|
|
80
|
+
return executor;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Overrides the mapping of the response to the proof and the metadata (v0 response by default).
|
|
84
|
+
* Additional data of the proof (e.g. a second proof) is passed to the args callback of the proof verifier
|
|
85
|
+
*/
|
|
86
|
+
mapProof(mapProof) {
|
|
87
|
+
const executor = this;
|
|
88
|
+
executor.#mapProof = mapProof;
|
|
89
|
+
return executor;
|
|
90
|
+
}
|
|
91
|
+
setProofVerifier(verify, ...args) {
|
|
92
|
+
const executor = this;
|
|
93
|
+
const getArgs = args.length === 1 && typeof args[0] === 'function'
|
|
94
|
+
? args[0]
|
|
95
|
+
: () => args;
|
|
96
|
+
executor.#verifyProof = (responseProof) => {
|
|
97
|
+
try {
|
|
98
|
+
return verify(responseProof.proof.grovedbProof, ...getArgs(responseProof));
|
|
99
|
+
}
|
|
100
|
+
catch (e) {
|
|
101
|
+
throw new ProofError(`Failed to verify GroveDB proof: ${String(e)}`, { cause: e });
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
return executor;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Sets the mapping of the query data to the result of the query, also the place for "not found" checks.
|
|
108
|
+
* The data is either verified from the proof or the result of the response requested without proof
|
|
109
|
+
*/
|
|
110
|
+
mapResponse(mapResponse) {
|
|
111
|
+
const executor = this;
|
|
112
|
+
executor.#mapResponse = mapResponse;
|
|
113
|
+
return executor;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Enables (default) or disables the verification of the quorum signature of the proof,
|
|
117
|
+
* e.g. if the GroveDB proof is verified against an already verified root hash
|
|
118
|
+
*/
|
|
119
|
+
setValidateProof(validateProof) {
|
|
120
|
+
this.#validateProof = validateProof;
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
async execute() {
|
|
124
|
+
const request = this.#request;
|
|
125
|
+
const mapResponse = this.#mapResponse;
|
|
126
|
+
if (request == null) {
|
|
127
|
+
throw new Error('GRPC request is not set, call setGrpcRequest() before execute()');
|
|
128
|
+
}
|
|
129
|
+
if (this.#verifyProof == null) {
|
|
130
|
+
throw new Error('Proof verification is not set, call setProofVerifier() before execute()');
|
|
131
|
+
}
|
|
132
|
+
if (mapResponse == null) {
|
|
133
|
+
throw new Error('Response mapping is not set, call mapResponse() before execute()');
|
|
134
|
+
}
|
|
135
|
+
const requestBody = getVersionBody(this.#requestMessage);
|
|
136
|
+
const canSkipProof = requestBody != null && 'prove' in requestBody;
|
|
137
|
+
const prove = this.#grpcPool.proofs !== false || !canSkipProof;
|
|
138
|
+
if (canSkipProof) {
|
|
139
|
+
requestBody.prove = prove;
|
|
140
|
+
}
|
|
141
|
+
return await mapResponse(prove ? await this.#executeWithProof(request) : await this.#executeWithoutProof(request));
|
|
142
|
+
}
|
|
143
|
+
async #executeWithoutProof(request) {
|
|
144
|
+
const client = await this.#grpcPool.getClient();
|
|
145
|
+
const { response } = await request(client);
|
|
146
|
+
const body = getVersionBody(response);
|
|
147
|
+
const result = body?.result;
|
|
148
|
+
const metadata = body?.metadata;
|
|
149
|
+
if (result?.oneofKind == null || result.oneofKind === 'proof') {
|
|
150
|
+
throw new Error(`Unexpected oneOf type returned from DAPI (${String(result?.oneofKind)})`);
|
|
151
|
+
}
|
|
152
|
+
if (metadata == null) {
|
|
153
|
+
throw new Error('Metadata not found');
|
|
154
|
+
}
|
|
155
|
+
return { proved: false, result: result, metadata };
|
|
156
|
+
}
|
|
157
|
+
async #executeWithProof(request) {
|
|
158
|
+
const verifyProof = this.#verifyProof;
|
|
159
|
+
const grpcPool = this.#grpcPool;
|
|
160
|
+
// custom pools without retries are not retried
|
|
161
|
+
const retries = grpcPool.retries ?? 0;
|
|
162
|
+
let lastError;
|
|
163
|
+
for (let retry = 0;; retry++) {
|
|
164
|
+
let client;
|
|
165
|
+
try {
|
|
166
|
+
client = await grpcPool.getClient();
|
|
167
|
+
}
|
|
168
|
+
catch (e) {
|
|
169
|
+
// every node is banned, the last proof error is more useful than "no available nodes"
|
|
170
|
+
throw lastError ?? e;
|
|
171
|
+
}
|
|
172
|
+
try {
|
|
173
|
+
const { response } = await request(client);
|
|
174
|
+
const responseProof = this.#mapProof(response);
|
|
175
|
+
const verified = verifyProof(responseProof);
|
|
176
|
+
if (this.#validateProof) {
|
|
177
|
+
await this.#verifySignature(responseProof, verified.rootHash);
|
|
178
|
+
grpcPool.unbanNode?.(client);
|
|
179
|
+
}
|
|
180
|
+
return { proved: true, verified, metadata: responseProof.metadata };
|
|
181
|
+
}
|
|
182
|
+
catch (e) {
|
|
183
|
+
if (!(e instanceof ProofError)) {
|
|
184
|
+
throw e;
|
|
185
|
+
}
|
|
186
|
+
grpcPool.banNode?.(client);
|
|
187
|
+
if (retry >= retries) {
|
|
188
|
+
throw e;
|
|
189
|
+
}
|
|
190
|
+
lastError = e;
|
|
191
|
+
await sleep(RETRY_DELAY_MS);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
async #verifySignature({ proof, metadata }, rootHash) {
|
|
196
|
+
if (rootHash == null) {
|
|
197
|
+
throw new Error('Root hash is required to validate the proof, return it from the proof verifier or call setValidateProof(false)');
|
|
198
|
+
}
|
|
199
|
+
const quorumPublicKey = await this.#quorumKeyProvider.getQuorumPublicKey(proof.quorumType, bytesToHex(proof.quorumHash));
|
|
200
|
+
const verify = await verifyTenderdashProof(proof, metadata, rootHash, quorumPublicKey);
|
|
201
|
+
if (!verify) {
|
|
202
|
+
throw new ProofError('Failed to verify query');
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
package/src/constants.d.ts
CHANGED
|
@@ -8,6 +8,12 @@ export declare const HALVING_INTERVAL = 210240;
|
|
|
8
8
|
export declare const TESTNET_ACTIVATION_HEIGHT = 1066900;
|
|
9
9
|
export declare const MAINNET_ACTIVATION_HEIGHT = 2128896;
|
|
10
10
|
export declare const GRPC_DEFAULT_POOL_LIMIT = 5;
|
|
11
|
+
/**
|
|
12
|
+
* Limits of entries returned by Drive in address balance changes queries, hardcoded in the query handlers
|
|
13
|
+
* (rs-drive-abci/src/query/address_funds), proofs must be verified with the same limit
|
|
14
|
+
*/
|
|
15
|
+
export declare const RECENT_ADDRESS_BALANCE_CHANGES_LIMIT = 100;
|
|
16
|
+
export declare const RECENT_COMPACTED_ADDRESS_BALANCE_CHANGES_LIMIT = 25;
|
|
11
17
|
export declare const DPNS_DATA_CONTRACT_ID = "GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec";
|
|
12
18
|
export declare const DPNS_DATA_CONTRACT_BYTES = "AeZoxlmvZq7h5ywYbd57W34KHXEqCcQNVyH2Ir9TxTFVAAAAAAABAQAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIGZG9tYWluFgsSEGRvY3VtZW50c011dGFibGUTABIMY2FuQmVEZWxldGVkEwESDHRyYW5zZmVyYWJsZQIBEgl0cmFkZU1vZGUCARIEdHlwZRIGb2JqZWN0EgdpbmRpY2VzFQIWBBIEbmFtZRIScGFyZW50TmFtZUFuZExhYmVsEgpwcm9wZXJ0aWVzFQIWARIabm9ybWFsaXplZFBhcmVudERvbWFpbk5hbWUSA2FzYxYBEg9ub3JtYWxpemVkTGFiZWwSA2FzYxIGdW5pcXVlEwESCWNvbnRlc3RlZBYDEgxmaWVsZE1hdGNoZXMVARYCEgVmaWVsZBIPbm9ybWFsaXplZExhYmVsEgxyZWdleFBhdHRlcm4SE15bYS16QS1aMDEtXXszLDE5fSQSCnJlc29sdXRpb24CABILZGVzY3JpcHRpb24SqklmIHRoZSBub3JtYWxpemVkIGxhYmVsIHBhcnQgb2YgdGhpcyBpbmR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VxdWlyZWQVARIQc2FsdGVkRG9tYWluSGFzaBIUYWRkaXRpb25hbFByb3BlcnRpZXMTABIIJGNvbW1lbnQSSlByZW9yZGVyIGRvY3VtZW50cyBhcmUgaW1tdXRhYmxlOiBtb2RpZmljYXRpb24gYW5kIGRlbGV0aW9uIGFyZSByZXN0cmljdGVkAAAAAAAAAAAAAA==";
|
|
13
19
|
export declare const LATEST_PLATFORM_VERSION = PlatformVersionWASM.PLATFORM_V14;
|
package/src/constants.js
CHANGED
|
@@ -8,6 +8,12 @@ export const HALVING_INTERVAL = 210240;
|
|
|
8
8
|
export const TESTNET_ACTIVATION_HEIGHT = 1066900;
|
|
9
9
|
export const MAINNET_ACTIVATION_HEIGHT = 2128896;
|
|
10
10
|
export const GRPC_DEFAULT_POOL_LIMIT = 5;
|
|
11
|
+
/**
|
|
12
|
+
* Limits of entries returned by Drive in address balance changes queries, hardcoded in the query handlers
|
|
13
|
+
* (rs-drive-abci/src/query/address_funds), proofs must be verified with the same limit
|
|
14
|
+
*/
|
|
15
|
+
export const RECENT_ADDRESS_BALANCE_CHANGES_LIMIT = 100;
|
|
16
|
+
export const RECENT_COMPACTED_ADDRESS_BALANCE_CHANGES_LIMIT = 25;
|
|
11
17
|
export const DPNS_DATA_CONTRACT_ID = 'GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec';
|
|
12
18
|
export const DPNS_DATA_CONTRACT_BYTES = 'AeZoxlmvZq7h5ywYbd57W34KHXEqCcQNVyH2Ir9TxTFVAAAAAAABAQAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIGZG9tYWluFgsSEGRvY3VtZW50c011dGFibGUTABIMY2FuQmVEZWxldGVkEwESDHRyYW5zZmVyYWJsZQIBEgl0cmFkZU1vZGUCARIEdHlwZRIGb2JqZWN0EgdpbmRpY2VzFQIWBBIEbmFtZRIScGFyZW50TmFtZUFuZExhYmVsEgpwcm9wZXJ0aWVzFQIWARIabm9ybW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IGdW5pcXVlEwESCnByb3BlcnRpZXMWARIQc2FsdGVkRG9tYWluSGFzaBYGEgR0eXBlEgVhcnJheRIJYnl0ZUFycmF5EwESCG1pbkl0ZW1zAiASCG1heEl0ZW1zAiASCHBvc2l0aW9uAgASC2Rlc2NyaXB0aW9uEllEb3VibGUgc2hhLTI1NiBvZiB0aGUgY29uY2F0ZW5hdGlvbiBvZiBhIDMyIGJ5dGUgcmFuZG9tIHNhbHQgYW5kIGEgbm9ybWFsaXplZCBkb21haW4gbmFtZRIIcmVxdWlyZWQVARIQc2FsdGVkRG9tYWluSGFzaBIUYWRkaXRpb25hbFByb3BlcnRpZXMTABIIJGNvbW1lbnQSSlByZW9yZGVyIGRvY3VtZW50cyBhcmUgaW1tdXRhYmxlOiBtb2RpZmljYXRpb24gYW5kIGRlbGV0aW9uIGFyZSByZXN0cmljdGVkAAAAAAAAAAAAAA==';
|
|
13
19
|
export const LATEST_PLATFORM_VERSION = PlatformVersionWASM.PLATFORM_V14;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ContestedResourceVoteState, ContestedStateResultType } from '../../types.js';
|
|
2
2
|
import { GRPCPool } from '../grpcConnectionPool.js';
|
|
3
|
+
import { QuorumKeyProvider } from '../quorumKeyProvider.js';
|
|
3
4
|
import { GetContestedResourceVoteStateRequest_GetContestedResourceVoteStateRequestV0_StartAtIdentifierInfo } from '../../proto/generated/platform.js';
|
|
4
5
|
import { DataContractWASM } from 'pshenmic-dpp';
|
|
5
6
|
export type StartAtIdentifierInfo = GetContestedResourceVoteStateRequest_GetContestedResourceVoteStateRequestV0_StartAtIdentifierInfo;
|
|
6
|
-
export default function getContestedResourceVoteState(grpcPool: GRPCPool, contract: DataContractWASM, documentTypeName: string, indexName: string, indexValues: Array<Uint8Array<ArrayBufferLike>>, resultType: ContestedStateResultType, allowIncludeLockedAndAbstainingVoteTally: boolean, startAtIdentifierInfo?: StartAtIdentifierInfo, count?: number): Promise<ContestedResourceVoteState>;
|
|
7
|
+
export default function getContestedResourceVoteState(grpcPool: GRPCPool, quorumKeyProvider: QuorumKeyProvider, contract: DataContractWASM, documentTypeName: string, indexName: string, indexValues: Array<Uint8Array<ArrayBufferLike>>, resultType: ContestedStateResultType, allowIncludeLockedAndAbstainingVoteTally: boolean, startAtIdentifierInfo?: StartAtIdentifierInfo, count?: number): Promise<ContestedResourceVoteState>;
|
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import { GetContestedResourceVoteStateRequest } from '../../proto/generated/platform.js';
|
|
1
|
+
import { GetContestedResourceVoteStateRequest, GetContestedResourceVoteStateResponse_GetContestedResourceVoteStateResponseV0_FinishedVoteInfo_FinishedVoteOutcome as FinishedVoteOutcome } from '../../proto/generated/platform.js';
|
|
2
2
|
import { DocumentWASM, IdentifierWASM, verifyVotePollVoteStateProof } from 'pshenmic-dpp';
|
|
3
|
-
import
|
|
4
|
-
import { getQuorumPublicKey } from '../utils/getQuorumPublicKey.js';
|
|
5
|
-
import bytesToHex from '../utils/bytesToHex.js';
|
|
3
|
+
import QueryExecutor from '../QueryExecutor.js';
|
|
6
4
|
import { LATEST_PLATFORM_VERSION } from '../constants.js';
|
|
7
|
-
|
|
5
|
+
// winner types of the proof verification by finished vote outcome of the response
|
|
6
|
+
const FINISHED_VOTE_OUTCOMES = {
|
|
7
|
+
[FinishedVoteOutcome.TOWARDS_IDENTITY]: 'WonByIdentity',
|
|
8
|
+
[FinishedVoteOutcome.LOCKED]: 'Locked',
|
|
9
|
+
[FinishedVoteOutcome.NO_PREVIOUS_WINNER]: 'NoWinner'
|
|
10
|
+
};
|
|
11
|
+
export default async function getContestedResourceVoteState(grpcPool, quorumKeyProvider, contract, documentTypeName, indexName, indexValues, resultType, allowIncludeLockedAndAbstainingVoteTally, startAtIdentifierInfo, count) {
|
|
8
12
|
if (startAtIdentifierInfo != null) {
|
|
9
13
|
startAtIdentifierInfo = {
|
|
10
14
|
startIdentifier: (new IdentifierWASM(startAtIdentifierInfo.startIdentifier)).bytes(),
|
|
@@ -27,51 +31,67 @@ export default async function getContestedResourceVoteState(grpcPool, contract,
|
|
|
27
31
|
}
|
|
28
32
|
}
|
|
29
33
|
});
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
throw new Error('Unexpected oneOf type returned from DAPI (must be v0)');
|
|
34
|
-
}
|
|
35
|
-
const { v0 } = version;
|
|
36
|
-
if (v0.result.oneofKind !== 'proof') {
|
|
37
|
-
throw new Error('Unexpected oneOf type returned from DAPI (must be proof)');
|
|
38
|
-
}
|
|
39
|
-
const { result: { proof }, metadata } = v0;
|
|
40
|
-
if (metadata == null) {
|
|
41
|
-
throw new Error('Metadata not found');
|
|
42
|
-
}
|
|
43
|
-
const { rootHash, result } = verifyVotePollVoteStateProof(proof.grovedbProof, contract, documentTypeName, indexName, indexValues,
|
|
34
|
+
return await QueryExecutor.build(grpcPool, quorumKeyProvider)
|
|
35
|
+
.setGrpcRequest('getContestedResourceVoteState', getContestedResourceVoteStateRequest)
|
|
36
|
+
.setProofVerifier(verifyVotePollVoteStateProof, contract, documentTypeName, indexName, indexValues,
|
|
44
37
|
// same enums
|
|
45
38
|
// @ts-expect-error
|
|
46
|
-
resultType, allowIncludeLockedAndAbstainingVoteTally, count, startAtIdentifierInfo, LATEST_PLATFORM_VERSION)
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
throw new Error('Vote state not found');
|
|
56
|
-
}
|
|
57
|
-
return {
|
|
58
|
-
contenders: contenders.map(contender => ({
|
|
59
|
-
identifier: contender.identityId,
|
|
60
|
-
document: contender.serializedDocument != null ? DocumentWASM.fromBytes(contender.serializedDocument, contract, documentTypeName, LATEST_PLATFORM_VERSION) : undefined,
|
|
61
|
-
voteTally: contender.voteTally
|
|
62
|
-
})),
|
|
63
|
-
abstainVoteTally: result?.abstainingVoteTally ?? 0,
|
|
64
|
-
lockVoteTally: result?.lockedVoteTally ?? 0,
|
|
65
|
-
skipped: result?.skipped === 1,
|
|
66
|
-
finishedVoteInfo: (winner != null)
|
|
67
|
-
? {
|
|
68
|
-
type: winner.type,
|
|
69
|
-
wonByIdentityId: 'identityId' in winner && winner.identityId != null ? new IdentifierWASM(winner.identityId) : undefined,
|
|
70
|
-
finishedAtBlockHeight: winner.blockInfo.height,
|
|
71
|
-
finishedAtCoreBlockHeight: winner.blockInfo.coreHeight,
|
|
72
|
-
finishedAtBlockTimeMs: winner.blockInfo.timeMs,
|
|
73
|
-
finishedAtEpoch: winner.blockInfo.epoch
|
|
39
|
+
resultType, allowIncludeLockedAndAbstainingVoteTally, count, startAtIdentifierInfo, LATEST_PLATFORM_VERSION)
|
|
40
|
+
.mapResponse(data => {
|
|
41
|
+
const toDocument = (bytes) => bytes != null ? DocumentWASM.fromBytes(bytes, contract, documentTypeName, LATEST_PLATFORM_VERSION) : undefined;
|
|
42
|
+
if (data.proved) {
|
|
43
|
+
const { result: voteState } = data.verified;
|
|
44
|
+
const { contenders } = voteState ?? { contenders: [] };
|
|
45
|
+
const { winner } = voteState ?? { winner: undefined };
|
|
46
|
+
if (contenders.length === 0) {
|
|
47
|
+
throw new Error('Vote state not found');
|
|
74
48
|
}
|
|
75
|
-
|
|
76
|
-
|
|
49
|
+
return {
|
|
50
|
+
contenders: contenders.map(contender => ({
|
|
51
|
+
identifier: contender.identityId,
|
|
52
|
+
document: toDocument(contender.serializedDocument),
|
|
53
|
+
voteTally: contender.voteTally
|
|
54
|
+
})),
|
|
55
|
+
abstainVoteTally: voteState?.abstainingVoteTally ?? 0,
|
|
56
|
+
lockVoteTally: voteState?.lockedVoteTally ?? 0,
|
|
57
|
+
skipped: voteState?.skipped === 1,
|
|
58
|
+
finishedVoteInfo: (winner != null)
|
|
59
|
+
? {
|
|
60
|
+
type: winner.type,
|
|
61
|
+
wonByIdentityId: 'identityId' in winner && winner.identityId != null ? new IdentifierWASM(winner.identityId) : undefined,
|
|
62
|
+
finishedAtBlockHeight: winner.blockInfo.height,
|
|
63
|
+
finishedAtCoreBlockHeight: winner.blockInfo.coreHeight,
|
|
64
|
+
finishedAtBlockTimeMs: winner.blockInfo.timeMs,
|
|
65
|
+
finishedAtEpoch: winner.blockInfo.epoch
|
|
66
|
+
}
|
|
67
|
+
: undefined
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
const { contenders, abstainVoteTally, lockVoteTally, finishedVoteInfo } = data.result.contestedResourceContenders;
|
|
71
|
+
if (contenders.length === 0) {
|
|
72
|
+
throw new Error('Vote state not found');
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
contenders: contenders.map(contender => ({
|
|
76
|
+
identifier: new IdentifierWASM(contender.identifier),
|
|
77
|
+
document: toDocument(contender.document),
|
|
78
|
+
voteTally: contender.voteCount
|
|
79
|
+
})),
|
|
80
|
+
abstainVoteTally: abstainVoteTally ?? 0,
|
|
81
|
+
lockVoteTally: lockVoteTally ?? 0,
|
|
82
|
+
// the number of skipped items is returned only with proof
|
|
83
|
+
skipped: false,
|
|
84
|
+
finishedVoteInfo: (finishedVoteInfo != null)
|
|
85
|
+
? {
|
|
86
|
+
type: FINISHED_VOTE_OUTCOMES[finishedVoteInfo.finishedVoteOutcome],
|
|
87
|
+
wonByIdentityId: finishedVoteInfo.wonByIdentityId != null ? new IdentifierWASM(finishedVoteInfo.wonByIdentityId) : undefined,
|
|
88
|
+
finishedAtBlockHeight: BigInt(finishedVoteInfo.finishedAtBlockHeight),
|
|
89
|
+
finishedAtCoreBlockHeight: finishedVoteInfo.finishedAtCoreBlockHeight,
|
|
90
|
+
finishedAtBlockTimeMs: BigInt(finishedVoteInfo.finishedAtBlockTimeMs),
|
|
91
|
+
finishedAtEpoch: finishedVoteInfo.finishedAtEpoch
|
|
92
|
+
}
|
|
93
|
+
: undefined
|
|
94
|
+
};
|
|
95
|
+
})
|
|
96
|
+
.execute();
|
|
77
97
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { GRPCPool } from '../grpcConnectionPool.js';
|
|
2
|
+
import { QuorumKeyProvider } from '../quorumKeyProvider.js';
|
|
2
3
|
import { ContestedResourceVoteState, ContestedStateResultType } from '../../types.js';
|
|
3
4
|
import { DataContractWASM } from 'pshenmic-dpp';
|
|
4
5
|
import { StartAtIdentifierInfo } from './getContestedResourceVoteState.js';
|
|
@@ -10,7 +11,9 @@ import { StartAtIdentifierInfo } from './getContestedResourceVoteState.js';
|
|
|
10
11
|
export declare class ContestedResourcesController {
|
|
11
12
|
/** @ignore **/
|
|
12
13
|
grpcPool: GRPCPool;
|
|
13
|
-
|
|
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
|
*
|
|
@@ -7,8 +7,11 @@ import getContestedResourceVoteState from './getContestedResourceVoteState.js';
|
|
|
7
7
|
export class ContestedResourcesController {
|
|
8
8
|
/** @ignore **/
|
|
9
9
|
grpcPool;
|
|
10
|
-
|
|
10
|
+
/** @ignore **/
|
|
11
|
+
quorumKeyProvider;
|
|
12
|
+
constructor(grpcPool, quorumKeyProvider) {
|
|
11
13
|
this.grpcPool = grpcPool;
|
|
14
|
+
this.quorumKeyProvider = quorumKeyProvider;
|
|
12
15
|
}
|
|
13
16
|
/**
|
|
14
17
|
* Retrieves an info about vote state for contested resource
|
|
@@ -25,6 +28,6 @@ export class ContestedResourcesController {
|
|
|
25
28
|
* @return {Promise<ContestedResourceVoteState>}
|
|
26
29
|
*/
|
|
27
30
|
async getContestedResourceVoteState(contract, documentTypeName, indexName, indexValuesBytes, resultType, allowIncludeLockedAndAbstainingVoteTally, startAtIdentifierInfo, count) {
|
|
28
|
-
return await getContestedResourceVoteState(this.grpcPool, contract, documentTypeName, indexName, indexValuesBytes, resultType, allowIncludeLockedAndAbstainingVoteTally, startAtIdentifierInfo, count);
|
|
31
|
+
return await getContestedResourceVoteState(this.grpcPool, this.quorumKeyProvider, contract, documentTypeName, indexName, indexValuesBytes, resultType, allowIncludeLockedAndAbstainingVoteTally, startAtIdentifierInfo, count);
|
|
29
32
|
}
|
|
30
33
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { DataContractWASM, IdentifierLike } from 'pshenmic-dpp';
|
|
2
2
|
import { GRPCPool } from '../grpcConnectionPool.js';
|
|
3
|
-
|
|
3
|
+
import { QuorumKeyProvider } from '../quorumKeyProvider.js';
|
|
4
|
+
export default function getByIdentifier(grpcPool: GRPCPool, quorumKeyProvider: QuorumKeyProvider, identifier: IdentifierLike): Promise<DataContractWASM>;
|