dash-platform-sdk 1.6.0-dev.1 → 1.6.0-dev.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dash-platform-sdk",
3
- "version": "1.6.0-dev.1",
3
+ "version": "1.6.0-dev.2",
4
4
  "main": "index.js",
5
5
  "description": "Lightweight SDK for accessing Dash Platform blockchain",
6
6
  "repository": {
@@ -59,6 +59,6 @@
59
59
  "@scure/bip32": "^2.0.0",
60
60
  "@scure/bip39": "^2.0.0",
61
61
  "@scure/btc-signer": "^2.0.1",
62
- "pshenmic-dpp": "2.0.1-dev.2"
62
+ "pshenmic-dpp": "2.0.1-dev.3"
63
63
  }
64
64
  }
@@ -1,2 +1,2 @@
1
1
  import { DocumentWASM, IdentifierLike } from 'pshenmic-dpp';
2
- export default function createDocument(dataContractId: IdentifierLike, documentType: string, data: object, owner: IdentifierLike, revision?: bigint, documentId?: IdentifierLike): DocumentWASM;
2
+ export default function createDocument(dataContractId: IdentifierLike, documentType: string, data: object, owner: IdentifierLike, identityContractNonce: bigint, revision?: bigint, documentId?: IdentifierLike): DocumentWASM;
@@ -1,4 +1,4 @@
1
1
  import { DocumentWASM } from 'pshenmic-dpp';
2
- export default function createDocument(dataContractId, documentType, data, owner, revision, documentId) {
3
- return new DocumentWASM(data, documentType, revision ?? BigInt(1), dataContractId, owner, documentId, owner);
2
+ export default function createDocument(dataContractId, documentType, data, owner, identityContractNonce, revision, documentId) {
3
+ return new DocumentWASM(data, documentType, revision ?? BigInt(1), dataContractId, owner, documentId, owner, identityContractNonce);
4
4
  }
@@ -18,11 +18,12 @@ export declare class DocumentsController {
18
18
  * @param documentType {string} Document type as string
19
19
  * @param data {object} Data as JSON object
20
20
  * @param owner {IdentifierLike} Identifier of the document's owner
21
+ * @param identityContractNonce {bigint} Identity contract nonce of the transition that will create the document, used for document id generation
21
22
  * @param revision {bigint=} revision (optional)
22
23
  *
23
24
  * @return {DataContractWASM}
24
25
  */
25
- create(dataContractId: IdentifierLike, documentType: string, data: object, owner: IdentifierLike, revision?: bigint): DocumentWASM;
26
+ create(dataContractId: IdentifierLike, documentType: string, data: object, owner: IdentifierLike, identityContractNonce: bigint, revision?: bigint): DocumentWASM;
26
27
  /**
27
28
  * Make a query for documents retrieval from the network.
28
29
  *
@@ -24,12 +24,13 @@ export class DocumentsController {
24
24
  * @param documentType {string} Document type as string
25
25
  * @param data {object} Data as JSON object
26
26
  * @param owner {IdentifierLike} Identifier of the document's owner
27
+ * @param identityContractNonce {bigint} Identity contract nonce of the transition that will create the document, used for document id generation
27
28
  * @param revision {bigint=} revision (optional)
28
29
  *
29
30
  * @return {DataContractWASM}
30
31
  */
31
- create(dataContractId, documentType, data, owner, revision) {
32
- return createDocument(new IdentifierWASM(dataContractId), documentType, data, new IdentifierWASM(owner), revision);
32
+ create(dataContractId, documentType, data, owner, identityContractNonce, revision) {
33
+ return createDocument(new IdentifierWASM(dataContractId), documentType, data, new IdentifierWASM(owner), identityContractNonce, revision);
33
34
  }
34
35
  /**
35
36
  * Make a query for documents retrieval from the network.
@@ -43,8 +43,11 @@ export declare class NamesController {
43
43
  * @param name {string} username (ex. pshenmic.dash)
44
44
  * @param identityId {IdentifierLike} identity identifier
45
45
  * @param privateKey {PrivateKeyWASM} Authentication / High private key from your identity
46
+ * @param preorderSalt {Uint8Array=} Optional 32 bytes salt of already existing preorder document. If provided, preorder step is skipped
47
+ *
48
+ * @return {Promise<Uint8Array>} preorder salt (also attached as `preorderSalt` to the error if domain broadcast fails)
46
49
  */
47
- registerName(name: string, identityId: IdentifierLike, privateKey: PrivateKeyWASM): Promise<void>;
50
+ registerName(name: string, identityId: IdentifierLike, privateKey: PrivateKeyWASM, preorderSalt?: Uint8Array): Promise<Uint8Array>;
48
51
  /**
49
52
  * Converts DPNS name to normalized format (ex. alice.dash -> al1ce.dash)
50
53
  *
@@ -67,14 +67,17 @@ export class NamesController {
67
67
  * @param name {string} username (ex. pshenmic.dash)
68
68
  * @param identityId {IdentifierLike} identity identifier
69
69
  * @param privateKey {PrivateKeyWASM} Authentication / High private key from your identity
70
+ * @param preorderSalt {Uint8Array=} Optional 32 bytes salt of already existing preorder document. If provided, preorder step is skipped
71
+ *
72
+ * @return {Promise<Uint8Array>} preorder salt (also attached as `preorderSalt` to the error if domain broadcast fails)
70
73
  */
71
- async registerName(name, identityId, privateKey) {
74
+ async registerName(name, identityId, privateKey, preorderSalt) {
72
75
  const validation = validateName(name);
73
76
  if (validation != null) {
74
77
  throw new Error(validation);
75
78
  }
76
79
  const identity = await getIdentityByIdentifier(this.grpcPool, identityId);
77
- await registerName(this.grpcPool, name, identity, privateKey);
80
+ return await registerName(this.grpcPool, name, identity, privateKey, preorderSalt);
78
81
  }
79
82
  /**
80
83
  * Converts DPNS name to normalized format (ex. alice.dash -> al1ce.dash)
@@ -1,3 +1,3 @@
1
1
  import { IdentityWASM, PrivateKeyWASM } from 'pshenmic-dpp';
2
2
  import { GRPCPool } from '../grpcConnectionPool.js';
3
- export default function registerName(grpcPool: GRPCPool, name: string, identity: IdentityWASM, privateKey: PrivateKeyWASM): Promise<void>;
3
+ export default function registerName(grpcPool: GRPCPool, name: string, identity: IdentityWASM, privateKey: PrivateKeyWASM, preorderSalt?: Uint8Array): Promise<Uint8Array>;
@@ -9,7 +9,7 @@ import broadcast from '../stateTransitions/broadcast.js';
9
9
  import waitForStateTransitionResult from '../stateTransitions/waitForStateTransitionResult.js';
10
10
  import testNameContested from './testNameContested.js';
11
11
  import { DPNS_DATA_CONTRACT_ID } from '../constants.js';
12
- export default async function registerName(grpcPool, name, identity, privateKey) {
12
+ export default async function registerName(grpcPool, name, identity, privateKey, preorderSalt) {
13
13
  const [identityPublicKey] = identity.getPublicKeys().filter(identityPublicKey => identityPublicKey.getPublicKeyHash() === privateKey.getPublicKeyHash());
14
14
  if (identityPublicKey == null) {
15
15
  throw new Error('Private key does not match the identity');
@@ -17,8 +17,11 @@ export default async function registerName(grpcPool, name, identity, privateKey)
17
17
  if (identityPublicKey.securityLevel !== 'HIGH' && identityPublicKey.purpose !== 'AUTHENTICATION') {
18
18
  throw new Error('Wrong private key, must be from AUTHENTICATION HIGH identity public key');
19
19
  }
20
+ if (preorderSalt != null && preorderSalt.length !== 32) {
21
+ throw new Error('Preorder salt must be 32 bytes length');
22
+ }
20
23
  const [label, parentDomainName] = name.split('.');
21
- const salt = getRandomBytes(32);
24
+ const salt = preorderSalt ?? getRandomBytes(32);
22
25
  const normalizedParentDomainName = convertToHomographSafeChars(parentDomainName);
23
26
  const normalizedLabel = convertToHomographSafeChars(label);
24
27
  const normalizedFullDomainName = `${normalizedLabel}.${normalizedParentDomainName}`;
@@ -26,39 +29,56 @@ export default async function registerName(grpcPool, name, identity, privateKey)
26
29
  ...salt,
27
30
  ...new TextEncoder().encode(normalizedFullDomainName)
28
31
  ])));
29
- let document;
30
- let stateTransition;
31
- // 1. Create preorder document
32
- const preorderData = {
33
- saltedDomainHash: Array.from(saltedDomainHash)
34
- };
35
32
  const identityContractNonce = await getIdentityContractNonce(grpcPool, identity.id, DPNS_DATA_CONTRACT_ID);
36
- document = createDocument(DPNS_DATA_CONTRACT_ID, 'preorder', preorderData, identity.id.base58());
37
- stateTransition = createStateTransition(document, 'create', { identityContractNonce: identityContractNonce + BigInt(1) });
38
- await stateTransition.sign(privateKey, identityPublicKey);
39
- await broadcast(grpcPool, stateTransition);
40
- // wait for state transition confirmation before next broadcast
41
- await waitForStateTransitionResult(grpcPool, stateTransition);
42
- // 2. Create domain document
43
- const domainData = {
44
- label,
45
- normalizedLabel,
46
- parentDomainName,
47
- normalizedParentDomainName,
48
- preorderSalt: Array.from(salt),
49
- records: {
50
- identity: Array.from(identity.id.bytes())
51
- },
52
- subdomainRules: {
53
- allowSubdomains: false
33
+ // skip preorder if salt of existing preorder is provided
34
+ const domainNonce = identityContractNonce + BigInt(preorderSalt == null ? 2 : 1);
35
+ // salt is exposed in the error only once preorder exists (passed in or its broadcast accepted),
36
+ // so registration can be retried with it without creating (and paying for) a new preorder
37
+ let preorderSent = preorderSalt != null;
38
+ try {
39
+ // 1. Create preorder document
40
+ if (preorderSalt == null) {
41
+ const preorderData = {
42
+ saltedDomainHash
43
+ };
44
+ const preorderDocument = createDocument(DPNS_DATA_CONTRACT_ID, 'preorder', preorderData, identity.id.base58(), identityContractNonce + BigInt(1));
45
+ const preorderStateTransition = createStateTransition(preorderDocument, 'create', { identityContractNonce: identityContractNonce + BigInt(1) });
46
+ await preorderStateTransition.sign(privateKey, identityPublicKey);
47
+ await broadcast(grpcPool, preorderStateTransition);
48
+ preorderSent = true;
49
+ // wait for state transition confirmation before next broadcast
50
+ await waitForStateTransitionResult(grpcPool, preorderStateTransition);
51
+ }
52
+ // 2. Create domain document
53
+ const domainData = {
54
+ label,
55
+ normalizedLabel,
56
+ parentDomainName,
57
+ normalizedParentDomainName,
58
+ preorderSalt: salt,
59
+ records: {
60
+ identity: identity.id.bytes()
61
+ },
62
+ subdomainRules: {
63
+ allowSubdomains: false
64
+ }
65
+ };
66
+ const domainDocument = createDocument(DPNS_DATA_CONTRACT_ID, 'domain', domainData, identity.id.base58(), domainNonce);
67
+ const domainStateTransition = createStateTransition(domainDocument, 'create', {
68
+ identityContractNonce: domainNonce,
69
+ // @ts-expect-error
70
+ prefundedVotingBalance: testNameContested(normalizedLabel) ? new PrefundedVotingBalanceWASM('parentNameAndLabel', BigInt(20000000000)) : undefined
71
+ });
72
+ await domainStateTransition.sign(privateKey, identityPublicKey);
73
+ await broadcast(grpcPool, domainStateTransition);
74
+ // wait for the domain too, so the next transition reads a nonce past it
75
+ await waitForStateTransitionResult(grpcPool, domainStateTransition);
76
+ }
77
+ catch (e) {
78
+ if (preorderSent && e instanceof Error) {
79
+ Object.assign(e, { preorderSalt: salt });
54
80
  }
55
- };
56
- document = createDocument(DPNS_DATA_CONTRACT_ID, 'domain', domainData, identity.id.base58());
57
- stateTransition = createStateTransition(document, 'create', {
58
- identityContractNonce: identityContractNonce + BigInt(2),
59
- // @ts-expect-error
60
- prefundedVotingBalance: testNameContested(normalizedLabel) ? new PrefundedVotingBalanceWASM('parentNameAndLabel', BigInt(20000000000)) : undefined
61
- });
62
- await stateTransition.sign(privateKey, identityPublicKey);
63
- await broadcast(grpcPool, stateTransition);
81
+ throw e;
82
+ }
83
+ return salt;
64
84
  }
@@ -21,7 +21,7 @@ describe('Document', () => {
21
21
  };
22
22
  });
23
23
  test('should be able to create document', async () => {
24
- const document = sdk.documents.create(dataContract, documentType, data, identity, revision);
24
+ const document = sdk.documents.create(dataContract, documentType, data, identity, BigInt(1), revision);
25
25
  expect(document).toEqual(expect.any(DocumentWASM));
26
26
  });
27
27
  test('should be able to get documents', async () => {
@@ -73,14 +73,14 @@ describe('Document', () => {
73
73
  });
74
74
  describe('should be able to create state transition', () => {
75
75
  test('should be able to create a create transition', async () => {
76
- const document = sdk.documents.create(dataContract, documentType, data, identity);
77
76
  const identityContractNonce = BigInt(1);
77
+ const document = sdk.documents.create(dataContract, documentType, data, identity, identityContractNonce);
78
78
  const stateTransition = sdk.documents.createStateTransition(document, 'create', { identityContractNonce });
79
79
  expect(stateTransition).toEqual(expect.any(StateTransitionWASM));
80
80
  });
81
81
  test('should be able to create a contested document transition', async () => {
82
- const document = sdk.documents.create(dataContract, documentType, data, identity);
83
82
  const identityContractNonce = BigInt(1);
83
+ const document = sdk.documents.create(dataContract, documentType, data, identity, identityContractNonce);
84
84
  // 0.2 Dash typically
85
85
  const contestedFee = BigInt(20000000000);
86
86
  const indexName = 'parentNameAndLabel';
@@ -89,8 +89,8 @@ describe('Document', () => {
89
89
  expect(stateTransition).toEqual(expect.any(StateTransitionWASM));
90
90
  });
91
91
  test('should be able to create a document with token payment info', async () => {
92
- const document = sdk.documents.create(dataContract, documentType, data, identity);
93
92
  const identityContractNonce = BigInt(1);
93
+ const document = sdk.documents.create(dataContract, documentType, data, identity, identityContractNonce);
94
94
  const tokenPaymentInfo = {
95
95
  tokenContractId: '6hVQW16jyvZyGSQk2YVty4ND6bgFXozizYWnPt753uW5',
96
96
  tokenContractPosition: 0,
@@ -102,34 +102,34 @@ describe('Document', () => {
102
102
  expect(stateTransition).toEqual(expect.any(StateTransitionWASM));
103
103
  });
104
104
  test('should be able to create a replace transition', async () => {
105
- const document = sdk.documents.create(dataContract, documentType, data, identity, revision + BigInt(1));
106
105
  const identityContractNonce = BigInt(1);
106
+ const document = sdk.documents.create(dataContract, documentType, data, identity, identityContractNonce, revision + BigInt(1));
107
107
  const stateTransition = sdk.documents.createStateTransition(document, 'replace', { identityContractNonce });
108
108
  expect(stateTransition).toEqual(expect.any(StateTransitionWASM));
109
109
  });
110
110
  test('should be able to create a delete transition', async () => {
111
- const document = sdk.documents.create(dataContract, documentType, data, identity, revision + BigInt(1));
112
111
  const identityContractNonce = BigInt(1);
112
+ const document = sdk.documents.create(dataContract, documentType, data, identity, identityContractNonce, revision + BigInt(1));
113
113
  const stateTransition = sdk.documents.createStateTransition(document, 'delete', { identityContractNonce });
114
114
  expect(stateTransition).toEqual(expect.any(StateTransitionWASM));
115
115
  });
116
116
  test('should be able to create a transfer transition', async () => {
117
- const document = sdk.documents.create(dataContract, documentType, data, identity, revision + BigInt(1));
118
117
  const identityContractNonce = BigInt(1);
118
+ const document = sdk.documents.create(dataContract, documentType, data, identity, identityContractNonce, revision + BigInt(1));
119
119
  const recipientId = '6hVQW16jyvZyGSQk2YVty4ND6bgFXozizYWnPt753uW5';
120
120
  const stateTransition = sdk.documents.createStateTransition(document, 'transfer', { identityContractNonce, recipientId });
121
121
  expect(stateTransition).toEqual(expect.any(StateTransitionWASM));
122
122
  });
123
123
  test('should be able to create an updatePrice transition', async () => {
124
- const document = sdk.documents.create(dataContract, documentType, data, identity, revision + BigInt(1));
125
124
  const identityContractNonce = BigInt(1);
125
+ const document = sdk.documents.create(dataContract, documentType, data, identity, identityContractNonce, revision + BigInt(1));
126
126
  const price = BigInt(10000000);
127
127
  const stateTransition = sdk.documents.createStateTransition(document, 'updatePrice', { identityContractNonce, price });
128
128
  expect(stateTransition).toEqual(expect.any(StateTransitionWASM));
129
129
  });
130
130
  test('should be able to create a purchase transition', async () => {
131
- const document = sdk.documents.create(dataContract, documentType, data, identity, revision + BigInt(1));
132
131
  const identityContractNonce = BigInt(1);
132
+ const document = sdk.documents.create(dataContract, documentType, data, identity, identityContractNonce, revision + BigInt(1));
133
133
  const amount = BigInt(10000000);
134
134
  const stateTransition = sdk.documents.createStateTransition(document, 'purchase', { identityContractNonce, amount });
135
135
  expect(stateTransition).toEqual(expect.any(StateTransitionWASM));
@@ -70,14 +70,14 @@ describe('Document action fees', () => {
70
70
  expect(agreement.verify(multipliedContract, 'note', 'Purchase', 1000n)).toEqual(false);
71
71
  });
72
72
  test('should be able to create a transition with an agreement', () => {
73
- const document = sdk.documents.create(fixedContract.id, 'note', { message: 'hello' }, owner);
73
+ const document = sdk.documents.create(fixedContract.id, 'note', { message: 'hello' }, owner, 1n);
74
74
  const feeMultiplier = new AgreedFeeMultiplierWASM(1000n, 0);
75
75
  const actionFeeAgreement = DocumentActionFeeAgreementWASM.forDocumentTypeAction(fixedContract, 'note', 'Create', feeMultiplier);
76
76
  const stateTransition = sdk.documents.createStateTransition(document, 'create', { identityContractNonce: 1n, actionFeeAgreement });
77
77
  expect(agreementOf(stateTransition)?.owner).toEqual(10000000n);
78
78
  });
79
79
  test('should be able to create a transition with an agreement as a plain object', () => {
80
- const document = sdk.documents.create(multipliedContract.id, 'note', { message: 'hello' }, owner);
80
+ const document = sdk.documents.create(multipliedContract.id, 'note', { message: 'hello' }, owner, 1n);
81
81
  const stateTransition = sdk.documents.createStateTransition(document, 'create', {
82
82
  identityContractNonce: 1n,
83
83
  actionFeeAgreement: {
@@ -1,4 +1,5 @@
1
1
  import broadcast from '../../src/stateTransitions/broadcast.js';
2
+ import waitForStateTransitionResult from '../../src/stateTransitions/waitForStateTransitionResult.js';
2
3
  import { DocumentWASM, PrivateKeyWASM } from 'pshenmic-dpp';
3
4
  import { DashPlatformSDK } from '../../src/DashPlatformSDK.js';
4
5
  import sleep from '../../src/utils/sleep.js';
@@ -32,4 +33,33 @@ describe('DPNS names', () => {
32
33
  await sdk.names.registerName(fullName, identityId, privateKey);
33
34
  expect(broadcast).toHaveBeenCalledTimes(2);
34
35
  });
36
+ test('should be able to register a name with existing preorder salt', async () => {
37
+ const fullName = 'test-identity-050.dash';
38
+ const identityId = 'CKKYnVeKoxCbvuEhiT6MDoQaRyXgDECwtxoKL5cqucZE';
39
+ const privateKey = PrivateKeyWASM.fromWIF('XERm528aQWJ3hzEcb79KMQKewbSSJQtni3fWHbu75wDxg6xg6AA3');
40
+ const preorderSalt = new Uint8Array(32).fill(1);
41
+ jest.mocked(broadcast).mockClear();
42
+ const salt = await sdk.names.registerName(fullName, identityId, privateKey, preorderSalt);
43
+ expect(salt).toEqual(preorderSalt);
44
+ expect(broadcast).toHaveBeenCalledTimes(1);
45
+ });
46
+ test('should expose preorder salt in error if preorder confirmation fails', async () => {
47
+ const fullName = 'test-identity-050.dash';
48
+ const identityId = 'CKKYnVeKoxCbvuEhiT6MDoQaRyXgDECwtxoKL5cqucZE';
49
+ const privateKey = PrivateKeyWASM.fromWIF('XERm528aQWJ3hzEcb79KMQKewbSSJQtni3fWHbu75wDxg6xg6AA3');
50
+ jest.mocked(waitForStateTransitionResult).mockRejectedValueOnce(new Error('Timeout'));
51
+ const error = await sdk.names.registerName(fullName, identityId, privateKey).catch(e => e);
52
+ expect(error.message).toEqual('Timeout');
53
+ expect(error.preorderSalt).toBeInstanceOf(Uint8Array);
54
+ expect(error.preorderSalt.length).toEqual(32);
55
+ });
56
+ test('should not expose preorder salt in error if preorder broadcast is refused', async () => {
57
+ const fullName = 'test-identity-050.dash';
58
+ const identityId = 'CKKYnVeKoxCbvuEhiT6MDoQaRyXgDECwtxoKL5cqucZE';
59
+ const privateKey = PrivateKeyWASM.fromWIF('XERm528aQWJ3hzEcb79KMQKewbSSJQtni3fWHbu75wDxg6xg6AA3');
60
+ jest.mocked(broadcast).mockRejectedValueOnce(new Error('Refused'));
61
+ const error = await sdk.names.registerName(fullName, identityId, privateKey).catch(e => e);
62
+ expect(error.message).toEqual('Refused');
63
+ expect(error.preorderSalt).toBeUndefined();
64
+ });
35
65
  });
@@ -26,14 +26,17 @@ describe('Node', () => {
26
26
  expect(status.network?.chainId).toEqual(expect.any(String));
27
27
  expect(status.network?.peersCount).toEqual(expect.any(Number));
28
28
  expect(status.network?.listening).toEqual(expect.any(Boolean));
29
- expect(status.stateSync?.totalSyncedTime).toEqual(expect.any(String));
30
- expect(status.stateSync?.remainingTime).toEqual(expect.any(String));
31
- expect(status.stateSync?.totalSnapshots).toEqual(expect.any(Number));
32
- expect(status.stateSync?.chunkProcessAvgTime).toEqual(expect.any(String));
33
- expect(status.stateSync?.snapshotHeight).toEqual(expect.any(String));
34
- expect(status.stateSync?.snapshotChunksCount).toEqual(expect.any(String));
35
- expect(status.stateSync?.backfilledBlocks).toEqual(expect.any(String));
36
- expect(status.stateSync?.backfillBlocksTotal).toEqual(expect.any(String));
29
+ // rs-dapi (v5) returns state sync info only if tenderdash reports non-zero state sync counters
30
+ if (status.stateSync != null) {
31
+ expect(status.stateSync.totalSyncedTime).toEqual(expect.any(String));
32
+ expect(status.stateSync.remainingTime).toEqual(expect.any(String));
33
+ expect(status.stateSync.totalSnapshots).toEqual(expect.any(Number));
34
+ expect(status.stateSync.chunkProcessAvgTime).toEqual(expect.any(String));
35
+ expect(status.stateSync.snapshotHeight).toEqual(expect.any(String));
36
+ expect(status.stateSync.snapshotChunksCount).toEqual(expect.any(String));
37
+ expect(status.stateSync.backfilledBlocks).toEqual(expect.any(String));
38
+ expect(status.stateSync.backfillBlocksTotal).toEqual(expect.any(String));
39
+ }
37
40
  expect(status.time?.local).toEqual(expect.any(String));
38
41
  expect(status.time?.block).toEqual(expect.any(String));
39
42
  expect(status.time?.genesis).toEqual(expect.any(String));