dash-platform-sdk 1.5.0-dev.8 → 1.5.0-dev.9

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.
@@ -0,0 +1,3 @@
1
+ import GRPCConnectionPool from '../grpcConnectionPool.js';
2
+ import { VerifiedAddressesBranchState } from 'pshenmic-dpp';
3
+ export declare function getAddressesBranchState(grpcPool: GRPCConnectionPool, key: Uint8Array, depth: number, checkpointHeight: bigint, expectedRootHash: Uint8Array): Promise<VerifiedAddressesBranchState>;
@@ -0,0 +1,21 @@
1
+ import { verifyAddressesBranchState } from 'pshenmic-dpp';
2
+ import { GetAddressesBranchStateRequest } from '../../proto/generated/platform.js';
3
+ import { LATEST_PLATFORM_VERSION } from '../constants.js';
4
+ export async function getAddressesBranchState(grpcPool, key, depth, checkpointHeight, expectedRootHash) {
5
+ const getAddressesBranchStateRequest = GetAddressesBranchStateRequest.create({
6
+ version: {
7
+ oneofKind: 'v0',
8
+ v0: {
9
+ key,
10
+ depth,
11
+ checkpointHeight: checkpointHeight.toString()
12
+ }
13
+ }
14
+ });
15
+ const { response } = 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);
21
+ }
@@ -0,0 +1,3 @@
1
+ import GRPCConnectionPool from '../grpcConnectionPool.js';
2
+ import { PlatformAddressesTrunkState } from '../../types.js';
3
+ export declare function getAddressesTrunkState(grpcPool: GRPCConnectionPool): Promise<PlatformAddressesTrunkState>;
@@ -0,0 +1,33 @@
1
+ import { verifyAddressesTrunkState } from 'pshenmic-dpp';
2
+ import { GetAddressesTrunkStateRequest } 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 getAddressesTrunkState(grpcPool) {
8
+ const getAddressesTrunkStateRequest = GetAddressesTrunkStateRequest.create({
9
+ version: {
10
+ oneofKind: 'v0',
11
+ v0: {}
12
+ }
13
+ });
14
+ const { response } = 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 };
33
+ }
@@ -0,0 +1,3 @@
1
+ import GRPCConnectionPool from '../grpcConnectionPool.js';
2
+ import { BlockAddressBalanceChanges } from 'pshenmic-dpp';
3
+ export declare function getRecentAddressBalanceChanges(grpcPool: GRPCConnectionPool, startHeight: bigint, startHeightExclusive?: boolean): Promise<BlockAddressBalanceChanges[]>;
@@ -0,0 +1,38 @@
1
+ import { verifyRecentAddressBalanceChanges } from 'pshenmic-dpp';
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) {
8
+ const getRecentAddressBalanceChangesRequest = GetRecentAddressBalanceChangesRequest.create({
9
+ version: {
10
+ oneofKind: 'v0',
11
+ v0: {
12
+ startHeight: startHeight.toString(),
13
+ prove: true,
14
+ startHeightExclusive
15
+ }
16
+ }
17
+ });
18
+ const { response } = 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;
38
+ }
@@ -0,0 +1,3 @@
1
+ import GRPCConnectionPool from '../grpcConnectionPool.js';
2
+ import { CompactedBlockAddressBalanceChanges } from 'pshenmic-dpp';
3
+ export declare function getRecentCompactedAddressBalanceChanges(grpcPool: GRPCConnectionPool, startBlockHeight: bigint): Promise<CompactedBlockAddressBalanceChanges[]>;
@@ -0,0 +1,37 @@
1
+ import { verifyRecentCompactedAddressBalanceChanges } from 'pshenmic-dpp';
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) {
8
+ const getRecentCompactedAddressBalanceChangesRequest = GetRecentCompactedAddressBalanceChangesRequest.create({
9
+ version: {
10
+ oneofKind: 'v0',
11
+ v0: {
12
+ startBlockHeight: startBlockHeight.toString(),
13
+ prove: true
14
+ }
15
+ }
16
+ });
17
+ const { response } = 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;
37
+ }
@@ -1,6 +1,6 @@
1
1
  import GRPCConnectionPool from '../grpcConnectionPool.js';
2
- import { PlatformAddressLike, StateTransitionWASM } from 'pshenmic-dpp';
3
- import { PlatformAddressInfo, PlatformAddressTransitionParamsMap, PlatformAddressTransitionType } from '../../types.js';
2
+ import { BlockAddressBalanceChanges, CompactedBlockAddressBalanceChanges, PlatformAddressLike, StateTransitionWASM, VerifiedAddressesBranchState } from 'pshenmic-dpp';
3
+ import { PlatformAddressesTrunkState, PlatformAddressInfo, PlatformAddressTransitionParamsMap, PlatformAddressTransitionType } from '../../types.js';
4
4
  export declare class PlatformAddressesController {
5
5
  /** @ignore **/
6
6
  grpcPool: GRPCConnectionPool;
@@ -19,6 +19,49 @@ export declare class PlatformAddressesController {
19
19
  * @return {Promise<PlatformAddressInfo[]>}
20
20
  */
21
21
  getAddressesInfos(addresses: PlatformAddressLike[]): Promise<PlatformAddressInfo[]>;
22
+ /**
23
+ * Makes a query for the trunk chunk of the address funds tree, used as the entry point
24
+ * for client-side sync of the address funds state
25
+ *
26
+ * Returns the verified trunk state (elements, leaf keys and helpers for descending into
27
+ * branches) along with the response metadata, whose height is the checkpoint height for
28
+ * subsequent branch queries
29
+ *
30
+ * @return {Promise<PlatformAddressesTrunkState>}
31
+ */
32
+ getAddressesTrunkState(): Promise<PlatformAddressesTrunkState>;
33
+ /**
34
+ * Makes a query for a branch chunk of the address funds tree at the given key and depth,
35
+ * anchored to the checkpoint height returned by the trunk state query
36
+ *
37
+ * The branch proof carries no root hash of its own - it is verified against
38
+ * expectedRootHash, the hash the parent trunk (or branch) reported for this key
39
+ * via its leaf info
40
+ *
41
+ * @param key {Uint8Array} key that was navigated to in the tree
42
+ * @param depth {number} depth of the branch to return
43
+ * @param checkpointHeight {bigint} block height from the trunk response metadata
44
+ * @param expectedRootHash {Uint8Array} hash reported for this key by the parent trunk or branch
45
+ * @return {Promise<VerifiedAddressesBranchState>}
46
+ */
47
+ getAddressesBranchState(key: Uint8Array, depth: number, checkpointHeight: bigint, expectedRootHash: Uint8Array): Promise<VerifiedAddressesBranchState>;
48
+ /**
49
+ * Makes a query for recent address balance changes starting from the given block height,
50
+ * returns an array of per-block change sets
51
+ *
52
+ * @param startHeight {bigint} block height to start from
53
+ * @param startHeightExclusive {boolean} [startHeightExclusive=false] use exclusive start (RangeAfter) instead of inclusive (RangeFrom)
54
+ * @return {Promise<BlockAddressBalanceChanges[]>}
55
+ */
56
+ getRecentAddressBalanceChanges(startHeight: bigint, startHeightExclusive?: boolean): Promise<BlockAddressBalanceChanges[]>;
57
+ /**
58
+ * Makes a query for recent compacted address balance changes starting from the given
59
+ * block height, returns an array of compacted (block range) change sets
60
+ *
61
+ * @param startBlockHeight {bigint} block height to start from
62
+ * @return {Promise<CompactedBlockAddressBalanceChanges[]>}
63
+ */
64
+ getRecentCompactedAddressBalanceChanges(startBlockHeight: bigint): Promise<CompactedBlockAddressBalanceChanges[]>;
22
65
  /**
23
66
  * Helper function for creating {StateTransitionWASM} for Platform Address transitions
24
67
  *
@@ -1,5 +1,9 @@
1
1
  import { getAddressInfo } from './getAddressInfo.js';
2
2
  import { getAddressesInfos } from './getAddressesInfos.js';
3
+ import { getAddressesTrunkState } from './getAddressesTrunkState.js';
4
+ import { getAddressesBranchState } from './getAddressesBranchState.js';
5
+ import { getRecentAddressBalanceChanges } from './getRecentAddressBalanceChanges.js';
6
+ import { getRecentCompactedAddressBalanceChanges } from './getRecentCompactedAddressBalanceChanges.js';
3
7
  import createStateTransition from './createStateTransition.js';
4
8
  export class PlatformAddressesController {
5
9
  /** @ignore **/
@@ -25,6 +29,57 @@ export class PlatformAddressesController {
25
29
  async getAddressesInfos(addresses) {
26
30
  return await getAddressesInfos(this.grpcPool, addresses);
27
31
  }
32
+ /**
33
+ * Makes a query for the trunk chunk of the address funds tree, used as the entry point
34
+ * for client-side sync of the address funds state
35
+ *
36
+ * Returns the verified trunk state (elements, leaf keys and helpers for descending into
37
+ * branches) along with the response metadata, whose height is the checkpoint height for
38
+ * subsequent branch queries
39
+ *
40
+ * @return {Promise<PlatformAddressesTrunkState>}
41
+ */
42
+ async getAddressesTrunkState() {
43
+ return await getAddressesTrunkState(this.grpcPool);
44
+ }
45
+ /**
46
+ * Makes a query for a branch chunk of the address funds tree at the given key and depth,
47
+ * anchored to the checkpoint height returned by the trunk state query
48
+ *
49
+ * The branch proof carries no root hash of its own - it is verified against
50
+ * expectedRootHash, the hash the parent trunk (or branch) reported for this key
51
+ * via its leaf info
52
+ *
53
+ * @param key {Uint8Array} key that was navigated to in the tree
54
+ * @param depth {number} depth of the branch to return
55
+ * @param checkpointHeight {bigint} block height from the trunk response metadata
56
+ * @param expectedRootHash {Uint8Array} hash reported for this key by the parent trunk or branch
57
+ * @return {Promise<VerifiedAddressesBranchState>}
58
+ */
59
+ async getAddressesBranchState(key, depth, checkpointHeight, expectedRootHash) {
60
+ return await getAddressesBranchState(this.grpcPool, key, depth, checkpointHeight, expectedRootHash);
61
+ }
62
+ /**
63
+ * Makes a query for recent address balance changes starting from the given block height,
64
+ * returns an array of per-block change sets
65
+ *
66
+ * @param startHeight {bigint} block height to start from
67
+ * @param startHeightExclusive {boolean} [startHeightExclusive=false] use exclusive start (RangeAfter) instead of inclusive (RangeFrom)
68
+ * @return {Promise<BlockAddressBalanceChanges[]>}
69
+ */
70
+ async getRecentAddressBalanceChanges(startHeight, startHeightExclusive = false) {
71
+ return await getRecentAddressBalanceChanges(this.grpcPool, startHeight, startHeightExclusive);
72
+ }
73
+ /**
74
+ * Makes a query for recent compacted address balance changes starting from the given
75
+ * block height, returns an array of compacted (block range) change sets
76
+ *
77
+ * @param startBlockHeight {bigint} block height to start from
78
+ * @return {Promise<CompactedBlockAddressBalanceChanges[]>}
79
+ */
80
+ async getRecentCompactedAddressBalanceChanges(startBlockHeight) {
81
+ return await getRecentCompactedAddressBalanceChanges(this.grpcPool, startBlockHeight);
82
+ }
28
83
  /**
29
84
  * Helper function for creating {StateTransitionWASM} for Platform Address transitions
30
85
  *
@@ -16,11 +16,54 @@ describe('PlatformAddress', () => {
16
16
  const addresses = [
17
17
  'tdash1kzg5azscav69z7m6dfzr9ner0a5vt7pn9ca4sz8d',
18
18
  'tdash1kqr3cxhgel75ru0yrhj5eq8j8jt92m5enqrfajxw',
19
- 'tdash1kq9plfyacx9q26dtaxgwuw9lt78nyu2mzc3xwcxv'
19
+ 'tdash1kzpjkv364ez6yyaqyvxfdgzcdet2h4mumqcjd2yx'
20
20
  ];
21
21
  const info = await sdk.platformAddresses.getAddressesInfos(addresses);
22
22
  expect(info).toHaveLength(addresses.length);
23
23
  });
24
+ test('getAddressesTrunkState', async () => {
25
+ const { trunk, metadata } = await sdk.platformAddresses.getAddressesTrunkState();
26
+ expect(trunk.rootHash).toEqual(expect.any(Uint8Array));
27
+ expect(trunk.elements.length).toBeGreaterThan(0);
28
+ expect(trunk.chunkDepths.length).toBeGreaterThan(0);
29
+ expect(metadata.height).toBeDefined();
30
+ });
31
+ test('getAddressesBranchState', async () => {
32
+ const { trunk, metadata } = await sdk.platformAddresses.getAddressesTrunkState();
33
+ // The whole tree fits in the trunk when it is small enough - nothing to descend into
34
+ if (trunk.leafKeys.length === 0) {
35
+ return;
36
+ }
37
+ const [leaf] = trunk.leafKeys;
38
+ const depth = trunk.chunkDepths[1] ?? trunk.chunkDepths[0];
39
+ const branch = await sdk.platformAddresses.getAddressesBranchState(leaf.key, depth, BigInt(metadata.height), leaf.hash);
40
+ expect(branch.branchRootHash).toEqual(leaf.hash);
41
+ expect(branch.elements.length).toBeGreaterThan(0);
42
+ });
43
+ test('getRecentAddressBalanceChanges', async () => {
44
+ const blocks = await sdk.platformAddresses.getRecentAddressBalanceChanges(1n);
45
+ expect(Array.isArray(blocks)).toBe(true);
46
+ for (const block of blocks) {
47
+ expect(block.blockHeight).toEqual(expect.any(BigInt));
48
+ for (const change of block.changes) {
49
+ expect(change.address).toEqual(expect.any(PlatformAddressWASM));
50
+ expect(['setCredits', 'addToCredits']).toContain(change.operation);
51
+ expect(change.credits).toEqual(expect.any(BigInt));
52
+ }
53
+ }
54
+ });
55
+ test('getRecentCompactedAddressBalanceChanges', async () => {
56
+ const ranges = await sdk.platformAddresses.getRecentCompactedAddressBalanceChanges(1n);
57
+ expect(Array.isArray(ranges)).toBe(true);
58
+ for (const range of ranges) {
59
+ expect(range.startBlockHeight).toEqual(expect.any(BigInt));
60
+ expect(range.endBlockHeight).toBeGreaterThanOrEqual(range.startBlockHeight);
61
+ for (const change of range.changes) {
62
+ expect(change.address).toEqual(expect.any(PlatformAddressWASM));
63
+ expect(['setCredits', 'addToCreditsOperations']).toContain(change.operation);
64
+ }
65
+ }
66
+ });
24
67
  describe('createStateTransition', () => {
25
68
  const identityId = 'QMfCRPcjXoTnZa9sA9JR2KWgGxZXMRJ4akgS3Uia1Qv';
26
69
  const addressA = PlatformAddressWASM.fromBech32m('tdash1kzg5azscav69z7m6dfzr9ner0a5vt7pn9ca4sz8d');
package/types.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { AddressFundsFeeStrategyStepWASM, AssetLockProofWASM, CoreScriptWASM, DocumentWASM, GasFeesPaidByWASM, IdentifierLike, IdentifierWASM, InputAddressWASM, KeyType, OrchardAddressWASM, PlatformAddressLike, PlatformAddressWASM, PlatformVersionWASM, PoolingLike, PrivateKeyWASM, Purpose, SecurityLevel, SpendableNoteWASM, TokenEmergencyActionWASM, TokenPricingScheduleWASM, AddressWitnessWASM, IdentityPublicKeyInCreationWASM, OutputAddressWASM, OutputAddressNullableCreditsWASM } from 'pshenmic-dpp';
1
+ import { AddressFundsFeeStrategyStepWASM, AssetLockProofWASM, CoreScriptWASM, DocumentWASM, GasFeesPaidByWASM, IdentifierLike, IdentifierWASM, InputAddressWASM, KeyType, OrchardAddressWASM, PlatformAddressLike, PlatformAddressWASM, PlatformVersionWASM, PoolingLike, PrivateKeyWASM, Purpose, SecurityLevel, SpendableNoteWASM, TokenEmergencyActionWASM, TokenPricingScheduleWASM, AddressWitnessWASM, IdentityPublicKeyInCreationWASM, OutputAddressWASM, OutputAddressNullableCreditsWASM, VerifiedAddressesTrunkState } from 'pshenmic-dpp';
2
+ import { ResponseMetadata } from './proto/generated/platform.js';
2
3
  export { CoreScriptWASM, DocumentWASM, GasFeesPaidByWASM, IdentifierWASM, KeyType, Purpose, SecurityLevel, TokenEmergencyActionWASM, TokenPricingScheduleWASM, StateTransitionWASM, BatchTransitionWASM, IdentityPublicKeyWASM, PrivateKeyWASM, DataContractUpdateTransitionWASM, IdentityWASM, IdentityUpdateTransitionWASM, IdentityCreditTransferWASM, MasternodeVoteTransitionWASM, IdentifierLike, PlatformAddressLike, InputAddressWASM, OutputAddressWASM, OutputAddressNullableCreditsWASM, AddressFundsFeeStrategyStepWASM, AddressWitnessWASM, IdentityPublicKeyInCreationWASM, AssetLockProofWASM, IdentityCreditTransferToAddressesTransitionWASM, IdentityCreateFromAddressesTransitionWASM, IdentityTopUpFromAddressesTransitionWASM, AddressFundsTransferTransitionWASM, AddressFundingFromAssetLockTransitionWASM, AddressCreditWithdrawalTransitionWASM } from 'pshenmic-dpp';
3
4
  export type Network = 'mainnet' | 'testnet';
4
5
  export { DashPlatformSDK } from './src/DashPlatformSDK.js';
@@ -284,6 +285,10 @@ export interface PlatformAddressInfo {
284
285
  nonce: number;
285
286
  balance: bigint;
286
287
  }
288
+ export interface PlatformAddressesTrunkState {
289
+ trunk: VerifiedAddressesTrunkState;
290
+ metadata: ResponseMetadata;
291
+ }
287
292
  export interface ShieldedEncryptedNote {
288
293
  nullifier: Uint8Array;
289
294
  cmx: Uint8Array;