postchain-client 0.9.2 → 0.10.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.
@@ -0,0 +1,17 @@
1
+ /*!
2
+ * The buffer module from node.js, for the browser.
3
+ *
4
+ * @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
5
+ * @license MIT
6
+ */
7
+
8
+ /*!
9
+ * The buffer module from node.js, for the browser.
10
+ *
11
+ * @author Feross Aboukhadijeh <https://feross.org>
12
+ * @license MIT
13
+ */
14
+
15
+ /*! https://mths.be/punycode v1.3.2 by @mathias */
16
+
17
+ /*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
package/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "postchain-client",
3
- "version": "0.9.2",
3
+ "version": "0.10.3",
4
4
  "description": "Client library for accessing a Postchain node through REST.",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "ssh://git@bitbucket.org/chromawallet/postchain-client.git"
8
8
  },
9
- "main": "index.js",
9
+ "main": "dist/postchain-client.js",
10
10
  "scripts": {
11
11
  "test": "npm run test:regtest",
12
12
  "build": "webpack --config webpack.config.js",
13
- "test:regtest": "find test/ -type f -name \"*.js\" | grep -v \"manual\\.js\"| xargs mocha --reporter spec"
13
+ "test:regtest": "find test/ -type f -name \"*.js\" | grep -v \"manual\\.js\"| xargs mocha --reporter spec",
14
+ "prepare": "npm run build"
14
15
  },
15
16
  "author": "ChromaWay AB",
16
17
  "contributors": [
@@ -23,7 +24,6 @@
23
24
  "dependencies": {
24
25
  "asn1.js": "^4.10.1",
25
26
  "browser-request": "^0.3.3",
26
- "collections": "^5.1.8",
27
27
  "request": "^2.81.0",
28
28
  "secp256k1": "^3.5.0"
29
29
  },
@@ -31,11 +31,15 @@
31
31
  "request": "browser-request"
32
32
  },
33
33
  "devDependencies": {
34
- "babel-preset-es2015": "^6.24.1",
34
+ "@babel/core": "^7.17.0",
35
+ "@babel/preset-env": "^7.16.11",
36
+ "babel-loader": "^8.2.3",
35
37
  "body-parser": "^1.17.1",
36
38
  "chai": "^3.5.0",
37
39
  "express": "^4.15.2",
38
40
  "mocha": "^6.1.4",
39
- "webpack": "^3.11.0"
41
+ "node-polyfill-webpack-plugin": "^1.1.4",
42
+ "webpack": "^5.68.0",
43
+ "webpack-cli": "^4.9.2"
40
44
  }
41
45
  }
@@ -1,8 +1,5 @@
1
1
  "use strict"
2
2
 
3
- var Set = require("collections/set").Set;
4
- var List = require("collections/list").List;
5
-
6
3
  function PathElement(previous) {
7
4
  this.previous = previous
8
5
  }
@@ -83,7 +80,7 @@ DictPathElement.prototype.equals = function(other) {
83
80
 
84
81
  /**
85
82
  *
86
- * @param {List} pathElements
83
+ * @param {Array} pathElements
87
84
  */
88
85
  function Path(pathElements) {
89
86
  this.pathElements = pathElements
@@ -93,7 +90,7 @@ function Path(pathElements) {
93
90
  *
94
91
  */
95
92
  Path.prototype.getCurrentPathElement = function() {
96
- return this.pathElements.peek()
93
+ return this.pathElements[0]
97
94
  }
98
95
 
99
96
  /**
@@ -163,7 +160,7 @@ var getTail = function(searchKey, path) {
163
160
  }
164
161
 
165
162
  try {
166
- var firstElement = path.pathElements.peek()
163
+ var firstElement = path.pathElements[0]
167
164
  if (firstElement instanceof SearchablePathElement) {
168
165
  if (firstElement.getSearchKey() == searchKey) {
169
166
  return path.tail()
@@ -178,12 +175,10 @@ var getTail = function(searchKey, path) {
178
175
 
179
176
  /**
180
177
  *
181
- * @param {Array} paths
178
+ * @param {Array} paths
182
179
  */
183
180
  function PathSet(paths) {
184
- // convert inside path into Set from input array path
185
- var set = new Set(paths)
186
- this.paths = set
181
+ this.paths = paths
187
182
  }
188
183
 
189
184
  /**
@@ -253,9 +248,9 @@ PathSet.prototype.errorCheckUnequalParent = function(currPath, currElem, prevPat
253
248
  */
254
249
  PathSet.prototype.keepOnlyArrayPaths = function() {
255
250
  var filteredPaths = this.paths.filter(path => {
256
- return path.pathElements.peek() instanceof ArrayPathElement
251
+ return path.pathElements[0] instanceof ArrayPathElement
257
252
  })
258
- return new PathSet(filteredPaths.toArray())
253
+ return new PathSet(filteredPaths)
259
254
  }
260
255
 
261
256
  /**
@@ -263,9 +258,9 @@ PathSet.prototype.keepOnlyArrayPaths = function() {
263
258
  */
264
259
  PathSet.prototype.keepOnlyDictPaths = function() {
265
260
  var filteredPaths = this.paths.filter(path => {
266
- return path.pathElements.peek() instanceof DictPathElement
261
+ return path.pathElements[0] instanceof DictPathElement
267
262
  })
268
- return new PathSet(filteredPaths.toArray())
263
+ return new PathSet(filteredPaths)
269
264
  }
270
265
 
271
266
  /**
@@ -301,7 +296,7 @@ PathSet.prototype.getTailFromList = function(searchKey, filterFunc) {
301
296
  * @param {Array} arr
302
297
  */
303
298
  var buildPathFromArray = function(arr) {
304
- var pathElementList = new List([])
299
+ var pathElements = new Array()
305
300
  var lastPathElem = null
306
301
 
307
302
  arr.forEach(item => {
@@ -313,12 +308,12 @@ var buildPathFromArray = function(arr) {
313
308
  } else {
314
309
  throw new Error("A path structure must only consist of Ints and Strings, not " + item)
315
310
  }
316
- pathElementList.add(newElem)
311
+ pathElements.push(newElem)
317
312
  lastPathElem = newElem
318
313
  })
319
314
  var lastOne = lastPathElem
320
- pathElementList.add(new PathLeafElement(lastOne))
321
- return new Path(pathElementList)
315
+ pathElements.push(new PathLeafElement(lastOne))
316
+ return new Path(pathElements)
322
317
  }
323
318
 
324
319
  module.exports = {Path, PathElement, PathLeafElement, ArrayPathElement, SearchablePathElement, DictPathElement, PathSet,
@@ -15,7 +15,7 @@ var ArrayHeadNode = require('../binarytree').ArrayHeadNode
15
15
  var DictHeadNode = require('../binarytree').DictHeadNode
16
16
  var ProofNodeSimple = require('./merkleprooftree').ProofNodeSimple
17
17
 
18
- const EMPTY_HASH = new Buffer(32)
18
+ const EMPTY_HASH = new Buffer.alloc(32)
19
19
  /**
20
20
  *
21
21
  */
@@ -97,7 +97,7 @@ function createTypedArg(value) {
97
97
  if (!Number.isInteger(value)) {
98
98
  throw Error("User error: Only integers are supported");
99
99
  }
100
- return {type: 'integer', value: new BN(value)}
100
+ return {type: 'integer', value: new BN(value.toString())}
101
101
  }
102
102
  if (value.constructor === Array) {
103
103
  return {type: 'array', value: value.map(item => createTypedArg(item))};
package/src/gtx/gtx.js CHANGED
@@ -104,13 +104,20 @@ exports.serialize = function (gtx) {
104
104
  exports.deserialize = function (gtxBytes) {
105
105
  const deserializedTx = gtv.decodeGtv(gtxBytes);
106
106
  const body = deserializedTx[0];
107
- const signatures = deserializedTx[1];
108
- return {
107
+ const gtvTxBody = {
109
108
  blockchainRID: body[0],
110
109
  operations: body[1].map(operation => ({opName: operation[0], args: operation[1]})),
111
- signers: body[2],
112
- signatures
110
+ signers: body[2]
113
111
  };
112
+ const signatures = deserializedTx[1];
113
+
114
+ return {
115
+ //...gtvTxBody,
116
+ blockchainRID: gtvTxBody.blockchainRID,
117
+ operations: gtvTxBody.operations,
118
+ signers: gtvTxBody.signers,
119
+ signatures
120
+ }
114
121
  };
115
122
 
116
123
  exports.checkGTXSignatures = function(txHash, gtx) {
package/src/restclient.js CHANGED
@@ -2,6 +2,21 @@
2
2
 
3
3
  var url = require('url');
4
4
  var rq = require('request');
5
+ var util = require('./util');
6
+
7
+ class TxRejectedError extends Error {
8
+ constructor(rejectReason) {
9
+ super("Transaction was rejected");
10
+ this.name = "TxRejectedError";
11
+ this.fullReason = rejectReason;
12
+ const parsed = rejectReason.match(/^\[(.+)\] Operation '(.+)' failed: (.+)$/)
13
+ if (parsed) {
14
+ this.shortReason = parsed[3];
15
+ this.rellLine = parsed[1];
16
+ this.operation = parsed[2];
17
+ }
18
+ }
19
+ }
5
20
 
6
21
  exports.createRestClient = function (urlBase, blockchainRID, maxSockets) {
7
22
  return {
@@ -132,7 +147,7 @@ exports.createRestClient = function (urlBase, blockchainRID, maxSockets) {
132
147
  const status = res.status;
133
148
  switch (status) {
134
149
  case "confirmed": resolve(null); break;
135
- case "rejected": reject(Error("Message was rejected")); break;
150
+ case "rejected": reject(new TxRejectedError(res.rejectReason)); break;
136
151
  case "unknown": reject(Error("Server lost our message")); break;
137
152
  case "waiting":
138
153
  setTimeout(
@@ -141,7 +156,7 @@ exports.createRestClient = function (urlBase, blockchainRID, maxSockets) {
141
156
  );
142
157
  break;
143
158
  default:
144
- console.log(status);
159
+ util.logError(status);
145
160
  reject(Error("got unexpected response from server"));
146
161
  }
147
162
  }
@@ -156,7 +171,10 @@ exports.createRestClient = function (urlBase, blockchainRID, maxSockets) {
156
171
  this.postTransaction(serializedTransaction, (err) => {
157
172
  if (err) reject(err);
158
173
  else {
159
- this.waitConfirmation(txRID).then(resolve, reject);
174
+ setTimeout(
175
+ () => this.waitConfirmation(txRID).then(resolve, reject),
176
+ 1011
177
+ )
160
178
  }
161
179
  })
162
180
 
@@ -168,24 +186,25 @@ exports.createRestClient = function (urlBase, blockchainRID, maxSockets) {
168
186
  function doPost(config, path, jsonObject, responseCallback) {
169
187
  post(config, path, jsonObject, (error, statusCode, responseObject) => {
170
188
  if (error) {
171
- console.log("In restclient doPost().", error);
189
+ util.logError("In restclient doPost().", error);
172
190
  responseCallback(error);
173
191
  } else if (statusCode != 200) {
174
- console.log(`Unexpected status code from server: ${statusCode}`);
192
+ util.logError(`Unexpected status code from server: ${statusCode}`);
175
193
  responseCallback(new Error(`Unexpected status code from server: ${statusCode}`), responseObject);
176
194
  } else {
177
195
  try {
178
- console.log(`Ok calling responseCallback with responseObject: ${JSON.stringify(responseObject)}`);
196
+ util.logInfo(`Ok calling responseCallback with responseObject: ${JSON.stringify(responseObject)}`);
179
197
  responseCallback(null, responseObject);
180
198
  } catch (error) {
181
- console.error("restclient.doPost(): Failed to call callback function", error);
199
+ util.logError("restclient.doPost(): Failed to call callback function", error);
182
200
  }
183
201
  }
184
202
  });
185
203
  }
186
204
 
187
205
  function get(config, path, callback) {
188
- console.log("GET URL " + url.resolve(config.urlBase, path));
206
+ util.logDebug("GET URL " + url.resolve(config.urlBase, path));
207
+
189
208
  var options = {
190
209
  method: 'GET', url: url.resolve(config.urlBase, path),
191
210
  json: true, pool: config.pool
@@ -194,18 +213,18 @@ function get(config, path, callback) {
194
213
  if (err) callback(err);
195
214
  else {
196
215
  try {
197
- console.log("BODY", body);
198
- console.log(typeof body);
216
+ util.logDebug("BODY", body);
199
217
  callback(null, response.statusCode, body);
200
218
  } catch (e) {
201
- console.error("restclient.get(): Failed to call callback function", e);
219
+ util.logError("restclient.get(): Failed to call callback function", e);
202
220
  }
203
221
  }
204
222
  });
205
223
  }
206
224
 
207
225
  function post(config, path, jsonBody, callback) {
208
- console.log("POST URL " + url.resolve(config.urlBase, path));
226
+ util.logDebug("POST URL " + url.resolve(config.urlBase, path));
227
+
209
228
  let options = {
210
229
  method: 'POST', url: url.resolve(config.urlBase, path),
211
230
  json: true, body: jsonBody, pool: config.pool
@@ -214,8 +233,7 @@ function post(config, path, jsonBody, callback) {
214
233
  if (err) callback(err);
215
234
  else {
216
235
  try {
217
- console.log(body);
218
- console.log(typeof body);
236
+ util.logDebug(body);
219
237
  callback(null, response.statusCode, body !== null ? body : null);
220
238
  } catch (e) {
221
239
  callback(e);
@@ -238,7 +256,7 @@ function handleGetResponse(error, statusCode, responseObject, callback) {
238
256
  if (error) {
239
257
  callback(error);
240
258
  } else if (statusCode === 404) {
241
- console.log("404 received");
259
+ util.logError("404 received");
242
260
  callback(null, null);
243
261
  } else if (statusCode != 200) {
244
262
  callback(new Error("Unexpected status code from server: " + statusCode));
@@ -246,7 +264,7 @@ function handleGetResponse(error, statusCode, responseObject, callback) {
246
264
  try {
247
265
  callback(null, responseObject);
248
266
  } catch (error) {
249
- console.error("restclient.handleGetResponse(): Failed to call callback function", error);
267
+ util.logError("restclient.handleGetResponse(): Failed to call callback function", error);
250
268
  }
251
269
  }
252
270
  }
package/src/util.js CHANGED
@@ -21,6 +21,10 @@ module.exports = {
21
21
  makeTuid: makeTuid,
22
22
  toBuffer: toBuffer,
23
23
  verifyKeyPair: verifyKeyPair,
24
+ logDebug: logDebug,
25
+ logError: logError,
26
+ logInfo: logInfo,
27
+ setLogLevel: setLogLevel
24
28
  };
25
29
 
26
30
  function createPublicKey(privateKey) {
@@ -275,4 +279,20 @@ function toBuffer(key) {
275
279
  function verifyKeyPair(privKey) {
276
280
  const pubKey = secp256k1.publicKeyCreate(privKey);
277
281
  return {pubKey, privKey};
278
- }
282
+ }
283
+
284
+ let logLevel = 2;
285
+
286
+ function logDebug() {
287
+ if (logLevel >= 3) console.log.apply(console, arguments);
288
+ }
289
+
290
+ function logError() {
291
+ if (logLevel != -1)console.error.apply(console, arguments);
292
+ }
293
+
294
+ function logInfo () {
295
+ if (logLevel >= 1) console.log.apply(console, arguments);
296
+ }
297
+
298
+ function setLogLevel(level) { logLevel = level; }
@@ -27,10 +27,10 @@ describe('path test', () => {
27
27
  it('test path get tail', () => {
28
28
  var org = path.buildPathFromArray([0, 7])
29
29
  assert.equal(org.size(), 3)
30
- assert.equal(0, org.pathElements.peek().index)
30
+ assert.equal(0, org.pathElements[0].index)
31
31
  var tail = path.getTailIfFirstElementIsArrayOfThisIndex(0, org)
32
32
  assert.equal(2, tail.size())
33
- assert.equal(7, tail.pathElements.peek().index)
33
+ assert.equal(7, tail.pathElements[0].index)
34
34
  var tail2 = path.getTailIfFirstElementIsArrayOfThisIndex(1, org) // won't find anything at index = 1
35
35
  assert.isNull(tail2)
36
36
  var tail7 = path.getTailIfFirstElementIsArrayOfThisIndex(7, tail)
@@ -54,7 +54,7 @@ describe('pathset test', () => {
54
54
 
55
55
  // ------------ Build the set --------------
56
56
  var paths = new path.PathSet([path1, path2, path3])
57
- assert.equal(3, paths.paths.size)
57
+ assert.equal(3, paths.paths.length)
58
58
 
59
59
  // ------------ Dig down and extract --------------
60
60
  var elem = paths.getPathLeafOrElseAnyCurrentPathElement()
@@ -63,37 +63,37 @@ describe('pathset test', () => {
63
63
 
64
64
  // ---- 0. The short path ---
65
65
  var pathsWithIndex0 = paths.getTailIfFirstElementIsArrayOfThisIndexFromList(0)
66
- assert.equal(1, pathsWithIndex0.paths.size)
66
+ assert.equal(1, pathsWithIndex0.paths.length)
67
67
  var elemIndex0 = pathsWithIndex0.getPathLeafOrElseAnyCurrentPathElement()
68
68
  expect(new path.ArrayPathElement(null, 7).equals(elemIndex0)).to.be.true
69
69
 
70
70
  // ---- 1. Dead end ---
71
71
  var pathsWithIndex1 = paths.getTailIfFirstElementIsArrayOfThisIndexFromList(1)
72
- assert.equal(0, pathsWithIndex1.paths.size)
72
+ assert.equal(0, pathsWithIndex1.paths.length)
73
73
  var elemIndex1 = pathsWithIndex1.getPathLeafOrElseAnyCurrentPathElement()
74
74
  assert.equal(null, elemIndex1)
75
75
 
76
76
  // ---- 3. The long path ---
77
77
  var pathsWithIndex3 = paths.getTailIfFirstElementIsArrayOfThisIndexFromList(3)
78
- assert.equal(2, pathsWithIndex3.paths.size)
78
+ assert.equal(2, pathsWithIndex3.paths.length)
79
79
  var elemIndex3 = pathsWithIndex3.getPathLeafOrElseAnyCurrentPathElement()
80
80
  expect(new path.DictPathElement(null, "myKey").equals(elemIndex3)).to.be.true
81
81
 
82
82
  var pathWithIndexMyKey = pathsWithIndex3.getTailIfFirstElementIsDictOfThisKeyFromList("myKey")
83
- assert.equal(2, pathWithIndexMyKey.paths.size)
83
+ assert.equal(2, pathWithIndexMyKey.paths.length)
84
84
  var elemIndex3myKey = pathWithIndexMyKey.getPathLeafOrElseAnyCurrentPathElement()
85
85
  expect(new path.ArrayPathElement(null, 2).equals(elemIndex3myKey)).to.be.true
86
86
 
87
87
  // Look for First leaf
88
88
  var myKeyPathsWithIndex2 = pathWithIndexMyKey.getTailIfFirstElementIsArrayOfThisIndexFromList(2)
89
- assert.equal(2, myKeyPathsWithIndex2.paths.size)
89
+ assert.equal(2, myKeyPathsWithIndex2.paths.length)
90
90
  var elemIndex3myKeyLeaf = myKeyPathsWithIndex2.getPathLeafOrElseAnyCurrentPathElement()
91
91
  // Even though there are two possible elements, one is a leaf and that one must be returned.
92
92
  expect(new path.PathLeafElement(dummyPrevElem).equals(elemIndex3myKeyLeaf)).to.be.true
93
93
 
94
94
  // Look for Second leaf
95
95
  var myKeyPathsWithIndex2And5 = myKeyPathsWithIndex2.getTailIfFirstElementIsArrayOfThisIndexFromList(5)
96
- assert.equal(1, myKeyPathsWithIndex2And5.paths.size)
96
+ assert.equal(1, myKeyPathsWithIndex2And5.paths.length)
97
97
  var elemIndex3myKey2Leaf = myKeyPathsWithIndex2And5.getPathLeafOrElseAnyCurrentPathElement()
98
98
  // One leaf left, should be a trivial check
99
99
  expect(new path.PathLeafElement(dummyPrevElem).equals(elemIndex3myKey2Leaf)).to.be.true
package/webpack.config.js CHANGED
@@ -1,4 +1,4 @@
1
- var path = require('path');
1
+ const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")
2
2
 
3
3
  module.exports = {
4
4
  entry: './index.js',
@@ -6,6 +6,23 @@ module.exports = {
6
6
  filename: 'postchain-client.js',
7
7
  libraryTarget: "umd",
8
8
  library: "PostchainClient"
9
- }
9
+ },
10
+ module: {
11
+ rules: [
12
+ {
13
+ test: /\.m?js$/,
14
+ exclude: /(node_modules|bower_components)/,
15
+ use: {
16
+ loader: 'babel-loader',
17
+ options: {
18
+ presets: ['@babel/preset-env']
19
+ }
20
+ }
21
+ }
22
+ ]
23
+ },
24
+ plugins: [
25
+ new NodePolyfillPlugin()
26
+ ]
10
27
  };
11
28