@dashevo/dapi-client 0.25.0-dev.15 → 0.25.0-dev.16

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.
@@ -1,6 +1,4 @@
1
1
  const EventEmitter = require('events');
2
- const { configure: configureDashcore } = require('@dashevo/dashcore-lib');
3
- const X11 = require('wasm-x11-hash');
4
2
  const { SpvChain, SPVError } = require('@dashevo/dash-spv');
5
3
 
6
4
  const BlockHeadersReader = require('./BlockHeadersReader');
@@ -51,7 +49,6 @@ class BlockHeadersProvider extends EventEmitter {
51
49
  this.spvChain = new SpvChain(this.options.network, 100);
52
50
 
53
51
  this.state = STATES.IDLE;
54
- this.wasmX11Ready = false;
55
52
 
56
53
  this.errorHandler = this.errorHandler.bind(this);
57
54
  this.headersHandler = this.headersHandler.bind(this);
@@ -79,13 +76,7 @@ class BlockHeadersProvider extends EventEmitter {
79
76
  * @private
80
77
  */
81
78
  async init() {
82
- if (!this.wasmX11Ready) {
83
- const x11Hash = await X11();
84
- configureDashcore({
85
- x11hash: x11Hash,
86
- });
87
- this.wasmX11Ready = true;
88
- }
79
+ await SpvChain.wasmX11Ready();
89
80
 
90
81
  if (!this.blockHeadersReader) {
91
82
  this.blockHeadersReader = new BlockHeadersReader(
@@ -113,12 +104,18 @@ class BlockHeadersProvider extends EventEmitter {
113
104
  * Initializes SPV chain with a list of headers and a known lastSyncedHeaderHeight
114
105
  *
115
106
  * @param headers
116
- * @param lastSyncedHeaderHeight
107
+ * @param firstHeaderHeight
117
108
  */
118
- initializeWith(headers, lastSyncedHeaderHeight) {
119
- const firstHeaderHeight = lastSyncedHeaderHeight - headers.length + 1;
120
- this.spvChain.reset(firstHeaderHeight);
121
- this.spvChain.addHeaders(headers, firstHeaderHeight);
109
+ async initializeChainWith(headers, firstHeaderHeight) {
110
+ await SpvChain.wasmX11Ready();
111
+
112
+ if (headers.length === 0) {
113
+ // If there are no headers, initialize chain from genesis
114
+ this.spvChain.initialize();
115
+ } else {
116
+ this.spvChain.initialize(headers[0], firstHeaderHeight);
117
+ this.spvChain.addHeaders(headers);
118
+ }
122
119
  }
123
120
 
124
121
  /**
@@ -130,7 +127,8 @@ class BlockHeadersProvider extends EventEmitter {
130
127
  ensureChainRoot(height) {
131
128
  // Flush spv chain in case header at specified height was not found
132
129
  if (!this.spvChain.hashesByHeight.has(height - 1)) {
133
- this.spvChain.reset(height);
130
+ this.spvChain.reset();
131
+ this.spvChain.pendingStartBlockHeight = height;
134
132
  }
135
133
  }
136
134
 
@@ -1,5 +1,6 @@
1
1
  const broadcastStateTransitionFactory = require('./broadcastStateTransition/broadcastStateTransitionFactory');
2
2
  const getDataContractFactory = require('./getDataContract/getDataContractFactory');
3
+ const getDataContractHistoryFactory = require('./getDataContractHistory/getDataContractHistoryFactory');
3
4
  const getDocumentsFactory = require('./getDocuments/getDocumentsFactory');
4
5
  const getIdentityFactory = require('./getIdentity/getIdentityFactory');
5
6
  const getIdentitiesByPublicKeyHashesFactory = require('./getIdentitiesByPublicKeyHashes/getIdentitiesByPublicKeyHashesFactory');
@@ -13,6 +14,7 @@ class PlatformMethodsFacade {
13
14
  constructor(grpcTransport) {
14
15
  this.broadcastStateTransition = broadcastStateTransitionFactory(grpcTransport);
15
16
  this.getDataContract = getDataContractFactory(grpcTransport);
17
+ this.getDataContractHistory = getDataContractHistoryFactory(grpcTransport);
16
18
  this.getDocuments = getDocumentsFactory(grpcTransport);
17
19
  this.getIdentity = getIdentityFactory(grpcTransport);
18
20
  this.getIdentitiesByPublicKeyHashes = getIdentitiesByPublicKeyHashesFactory(grpcTransport);
@@ -0,0 +1,55 @@
1
+ const AbstractResponse = require('../response/AbstractResponse');
2
+ const InvalidResponseError = require('../response/errors/InvalidResponseError');
3
+
4
+ class GetDataContractHistoryResponse extends AbstractResponse {
5
+ /**
6
+ * @param {object.<number, Buffer>} dataContractHistory
7
+ * @param {Metadata} metadata
8
+ * @param {Proof} [proof]
9
+ */
10
+ constructor(dataContractHistory, metadata, proof = undefined) {
11
+ super(metadata, proof);
12
+
13
+ this.dataContractHistory = dataContractHistory;
14
+ }
15
+
16
+ /**
17
+ * @returns {object.<number, Buffer>}
18
+ */
19
+ getDataContractHistory() {
20
+ return this.dataContractHistory;
21
+ }
22
+
23
+ /**
24
+ * @param proto
25
+ * @returns {GetDataContractHistoryResponse}
26
+ */
27
+ static createFromProto(proto) {
28
+ // History is something that we need to call a method to get a list of entries on
29
+ const dataContractHistory = proto.getDataContractHistory();
30
+ const { metadata, proof } = AbstractResponse.createMetadataAndProofFromProto(proto);
31
+
32
+ if (!dataContractHistory && !proof) {
33
+ throw new InvalidResponseError('DataContract is not defined');
34
+ }
35
+
36
+ const history = {};
37
+
38
+ if (dataContractHistory) {
39
+ const dataContractHistoryEntries = dataContractHistory.getDataContractEntriesList();
40
+
41
+ // eslint-disable-next-line no-restricted-syntax
42
+ for (const historyEntry of dataContractHistoryEntries) {
43
+ history[historyEntry.getDate()] = historyEntry.getValue();
44
+ }
45
+ }
46
+
47
+ return new GetDataContractHistoryResponse(
48
+ history,
49
+ metadata,
50
+ proof,
51
+ );
52
+ }
53
+ }
54
+
55
+ module.exports = GetDataContractHistoryResponse;
@@ -0,0 +1,81 @@
1
+ const {
2
+ v0: {
3
+ PlatformPromiseClient,
4
+ GetDataContractHistoryRequest,
5
+ },
6
+ } = require('@dashevo/dapi-grpc');
7
+
8
+ const GetDataContractHistoryResponse = require('./GetDataContractHistoryResponse');
9
+ const InvalidResponseError = require('../response/errors/InvalidResponseError');
10
+
11
+ /**
12
+ * @param {GrpcTransport} grpcTransport
13
+ * @returns {getDataContractHistory}
14
+ */
15
+ function getDataContractHistoryFactory(grpcTransport) {
16
+ /**
17
+ * Fetch Data Contract by id
18
+ *
19
+ * @typedef {getDataContractHistory}
20
+ * @param {Buffer} contractId
21
+ * @param {number} [startAtMs]
22
+ * @param {number} [limit]
23
+ * @param {number} [offset]
24
+ * @param {DAPIClientOptions & {prove: boolean}} [options]
25
+ * @returns {Promise<GetDataContractHistoryResponse>}
26
+ */
27
+ async function getDataContractHistory(
28
+ contractId,
29
+ startAtMs = 0,
30
+ limit = 10,
31
+ offset = 0,
32
+ options = {},
33
+ ) {
34
+ const getDataContractHistoryRequest = new GetDataContractHistoryRequest();
35
+
36
+ // need to convert objects inherited from Buffer to pure buffer as google protobuf
37
+ // doesn't support extended buffers
38
+ // https://github.com/protocolbuffers/protobuf/blob/master/js/binary/utils.js#L1049
39
+ if (Buffer.isBuffer(contractId)) {
40
+ // eslint-disable-next-line no-param-reassign
41
+ contractId = Buffer.from(contractId);
42
+ }
43
+
44
+ getDataContractHistoryRequest.setId(contractId);
45
+ getDataContractHistoryRequest.setStartAtMs(startAtMs);
46
+ getDataContractHistoryRequest.setLimit(limit);
47
+ getDataContractHistoryRequest.setOffset(offset);
48
+ getDataContractHistoryRequest.setProve(!!options.prove);
49
+
50
+ let lastError;
51
+
52
+ // TODO: simple retry before the dapi versioning is properly implemented
53
+ for (let i = 0; i < 3; i += 1) {
54
+ try {
55
+ // eslint-disable-next-line no-await-in-loop
56
+ const getDataContractHistoryResponse = await grpcTransport.request(
57
+ PlatformPromiseClient,
58
+ 'getDataContractHistory',
59
+ getDataContractHistoryRequest,
60
+ options,
61
+ );
62
+
63
+ return GetDataContractHistoryResponse.createFromProto(getDataContractHistoryResponse);
64
+ } catch (e) {
65
+ if (e instanceof InvalidResponseError) {
66
+ lastError = e;
67
+ } else {
68
+ throw e;
69
+ }
70
+ }
71
+ }
72
+
73
+ // If we made it past the cycle it means that the retry didn't work,
74
+ // and we're throwing the last error encountered
75
+ throw lastError;
76
+ }
77
+
78
+ return getDataContractHistory;
79
+ }
80
+
81
+ module.exports = getDataContractHistoryFactory;
@@ -26,8 +26,11 @@ class GetDocumentsResponse extends AbstractResponse {
26
26
  static createFromProto(proto) {
27
27
  const { metadata, proof } = AbstractResponse.createMetadataAndProofFromProto(proto);
28
28
 
29
+ const documents = proto.getDocuments();
30
+
29
31
  return new GetDocumentsResponse(
30
- proto.getDocumentsList().map((document) => Buffer.from(document)),
32
+ documents !== undefined
33
+ ? documents.getDocumentsList().map((document) => Buffer.from(document)) : [],
31
34
  metadata,
32
35
  proof,
33
36
  );
@@ -26,8 +26,11 @@ class GetIdentitiesByPublicKeyHashesResponse extends AbstractResponse {
26
26
  static createFromProto(proto) {
27
27
  const { metadata, proof } = AbstractResponse.createMetadataAndProofFromProto(proto);
28
28
 
29
+ const identitiesList = proto.getIdentities();
30
+
29
31
  return new GetIdentitiesByPublicKeyHashesResponse(
30
- proto.getIdentitiesList_asU8().map((identity) => Buffer.from(identity)),
32
+ identitiesList !== undefined
33
+ ? identitiesList.getIdentitiesList_asU8().map((identity) => Buffer.from(identity)) : [],
31
34
  metadata,
32
35
  proof,
33
36
  );
@@ -65,12 +65,13 @@ async function requestJsonRpc(protocol, host, port, selfSigned, method, params,
65
65
  if (typeof requestTimeoutId !== 'undefined') {
66
66
  clearTimeout(requestTimeoutId);
67
67
  }
68
- const data = await response.json();
69
68
 
70
69
  if (!response.ok) {
71
70
  throw new WrongHttpCodeError(requestInfo, response.status, response.statusText);
72
71
  }
73
72
 
73
+ const data = await response.json();
74
+
74
75
  if (data.error) {
75
76
  throw new JsonRpcError(requestInfo, data.error);
76
77
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dashevo/dapi-client",
3
- "version": "0.25.0-dev.15",
3
+ "version": "0.25.0-dev.16",
4
4
  "description": "Client library used to access Dash DAPI endpoints",
5
5
  "main": "lib/index.js",
6
6
  "contributors": [
@@ -26,12 +26,12 @@
26
26
  }
27
27
  ],
28
28
  "dependencies": {
29
- "@dashevo/dapi-grpc": "0.25.0-dev.15",
30
- "@dashevo/dash-spv": "0.25.0-dev.15",
31
- "@dashevo/dashcore-lib": "~0.20.6",
32
- "@dashevo/dpp": "0.25.0-dev.15",
33
- "@dashevo/grpc-common": "0.25.0-dev.15",
34
- "@dashevo/wasm-dpp": "0.25.0-dev.15",
29
+ "@dashevo/dapi-grpc": "0.25.0-dev.16",
30
+ "@dashevo/dash-spv": "0.25.0-dev.16",
31
+ "@dashevo/dashcore-lib": "~0.20.9",
32
+ "@dashevo/dpp": "0.25.0-dev.16",
33
+ "@dashevo/grpc-common": "0.25.0-dev.16",
34
+ "@dashevo/wasm-dpp": "0.25.0-dev.16",
35
35
  "bs58": "^4.0.1",
36
36
  "cbor": "^8.0.0",
37
37
  "lodash": "^4.17.21",