@theqrl/wallet-helpers 1.0.0 → 3.1.0

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 CHANGED
@@ -14,4 +14,8 @@ TODO
14
14
 
15
15
  ## Tests
16
16
 
17
- `npm test`
17
+ `npm test`
18
+
19
+ ## Coverage
20
+
21
+ `npm run cover`
package/dist/index.js ADDED
@@ -0,0 +1,207 @@
1
+ "use strict";
2
+
3
+ var sha256 = require('js-sha256');
4
+
5
+ var isEqual = require('lodash.isequal');
6
+
7
+ var _v3Wallet = require('./v3wallet').v3Wallet;
8
+
9
+ function hexToBytes(hex) {
10
+ for (var bytes = [], c = 0; c < hex.length; c += 2) {
11
+ bytes.push(parseInt(hex.substr(c, 2), 16));
12
+ }
13
+
14
+ return bytes;
15
+ }
16
+
17
+ function bytesToHex(bytes) {
18
+ for (var hex = [], i = 0; i < bytes.length; i++) {
19
+ hex.push((bytes[i] >>> 4).toString(16));
20
+ hex.push((bytes[i] & 0xf).toString(16));
21
+ }
22
+
23
+ return hex.join('');
24
+ }
25
+
26
+ function hashPKandformat(ePK) {
27
+ var h = hexToBytes(ePK);
28
+ return sha256(h);
29
+ }
30
+
31
+ function doConvert(ePK) {
32
+ var a = hashPKandformat(ePK);
33
+ var qx = sha256(hexToBytes('000400' + a));
34
+ var qm = qx.slice(56, 64);
35
+ return 'Q' + bytesToHex([0, 4, 0]) + a + qm;
36
+ }
37
+
38
+ function isArrayOfInts(arr) {
39
+ var result = 0;
40
+ arr.forEach(function (element) {
41
+ if (element === parseInt(element, 10)) {
42
+ result += 1;
43
+ }
44
+ });
45
+
46
+ if (result === arr.length) {
47
+ return true;
48
+ }
49
+
50
+ return false;
51
+ }
52
+
53
+ function _checkWeightsAndThreshold(arr, threshold) {
54
+ // check length > 0
55
+ if (arr.length === 0) {
56
+ return {
57
+ result: false,
58
+ error: 'Array has length 0'
59
+ };
60
+ } // check each element in array, then threshold, is an integer
61
+
62
+
63
+ if (!isArrayOfInts(arr)) {
64
+ return {
65
+ result: false,
66
+ error: 'Array has non-integer values'
67
+ };
68
+ }
69
+
70
+ if (threshold !== parseInt(threshold, 10)) {
71
+ return {
72
+ result: false,
73
+ error: 'Threshold is not an integer'
74
+ };
75
+ } // check threshold can actually be reacher
76
+
77
+
78
+ var sum = arr.reduce(function (accumulator, currentValue) {
79
+ return accumulator + currentValue;
80
+ });
81
+
82
+ if (threshold > sum) {
83
+ return {
84
+ result: false,
85
+ error: 'Threshold can never be reached with these weights'
86
+ };
87
+ }
88
+
89
+ return {
90
+ result: true
91
+ };
92
+ }
93
+
94
+ function _getWalletFileType(wallet) {
95
+ // determines source of wallet file
96
+ var webWallet = ['address', 'addressB32', 'pk', 'hexseed', 'mnemonic', 'height', 'hashFunction', 'signatureType', 'index', 'encrypted'];
97
+ var pythonNode = ['addresses', 'encrypted', 'version'];
98
+ var convertedWebWallet = ['pk', 'hexseed', 'mnemonic', 'height', 'hashFunction', 'signatureType', 'index', 'address', 'encrypted'];
99
+
100
+ if (isEqual(pythonNode, Object.keys(wallet))) {
101
+ return 'PYTHON-NODE';
102
+ }
103
+
104
+ if (wallet instanceof Array) {
105
+ if (isEqual(webWallet, Object.keys(wallet[0]))) {
106
+ return 'WEB-WALLET';
107
+ }
108
+
109
+ if (isEqual(convertedWebWallet, Object.keys(wallet[0]))) {
110
+ return 'CONVERTED-WEB-WALLET';
111
+ }
112
+ }
113
+
114
+ return 'UNKNOWN';
115
+ }
116
+
117
+ function _isWalletFileDeprecated(wallet) {
118
+ if (_getWalletFileType(wallet) === 'PYTHON-NODE') return true; // There are three characteristics that describe a deprecated encrypted wallet file
119
+ // 1: The encrypted field is true and
120
+ // 2: The addressB32 field is unencrypted and
121
+ // 3: The pk field is unencrypted.
122
+ // Whilst neither of these fields risk funds being lost, they do reveal a users public
123
+ // address if their wallet file is stolen. This is a privacy concern.
124
+ // We can determine if they are unencrypted by checking their lengths.
125
+ // If addressB32 field is 64 characters in length, and pk field is 134 characters in length.
126
+
127
+ if (typeof wallet[0].encrypted !== 'undefined' && typeof wallet[0].addressB32 !== 'undefined' && typeof wallet[0].pk !== 'undefined') {
128
+ if (wallet[0].encrypted === true && wallet[0].addressB32.length === 64 && wallet[0].pk.length === 134) {
129
+ return true;
130
+ }
131
+ }
132
+
133
+ return false;
134
+ }
135
+
136
+ function _pythonNodeToWebWallet(wallet) {
137
+ var output = [];
138
+ wallet.addresses.forEach(function (element) {
139
+ var e = element;
140
+ e.encrypted = wallet.encrypted;
141
+ output.push(e);
142
+ });
143
+ return output;
144
+ }
145
+
146
+ module.exports = {
147
+ /**
148
+ * Reports the current module version
149
+ * @return {string} version
150
+ */
151
+ version: function version() {
152
+ return "3.1.0";
153
+ },
154
+ QRLAddressFromEPKHex: function QRLAddressFromEPKHex(ePK) {
155
+ if (ePK === undefined) {
156
+ throw new Error("No ePK parameter");
157
+ }
158
+
159
+ if (ePK.length !== 134) {
160
+ throw new Error("ePK length invalid");
161
+ }
162
+
163
+ return doConvert(ePK);
164
+ },
165
+ checkWeightsAndThreshold: function checkWeightsAndThreshold(arr, threshold) {
166
+ if (arr === undefined || threshold === undefined) {
167
+ throw new Error("Missing parameter");
168
+ }
169
+
170
+ return _checkWeightsAndThreshold(arr, threshold);
171
+ },
172
+ isWalletFileDeprecated: function isWalletFileDeprecated(wallet) {
173
+ if (wallet === undefined) {
174
+ throw new Error("Missing parameter");
175
+ }
176
+
177
+ return _isWalletFileDeprecated(wallet);
178
+ },
179
+ getWalletFileType: function getWalletFileType(wallet) {
180
+ if (wallet === undefined) {
181
+ throw new Error("Missing parameter");
182
+ }
183
+
184
+ return _getWalletFileType(wallet);
185
+ },
186
+ pythonNodeToWebWallet: function pythonNodeToWebWallet(wallet) {
187
+ if (wallet === undefined) {
188
+ throw new Error("Missing parameter");
189
+ }
190
+
191
+ return _pythonNodeToWebWallet(wallet);
192
+ },
193
+ v3Wallet: function v3Wallet(json, encrypted, password) {
194
+ if (json === undefined || encrypted === undefined) {
195
+ throw new Error("Missing parameter");
196
+ }
197
+
198
+ if (encrypted === true && password === undefined) {
199
+ throw new Error("Missing password");
200
+ }
201
+
202
+ var newWallet = _v3Wallet(json, encrypted, password);
203
+
204
+ return newWallet;
205
+ }
206
+ };
207
+ //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,14 +1,17 @@
1
1
  {
2
2
  "name": "@theqrl/wallet-helpers",
3
- "version": "1.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "QRL wallet helpers",
5
- "main": "index.js",
6
- "directories": {
7
- "test": "test"
8
- },
5
+ "main": "dist/index.js",
6
+ "files": [
7
+ "dist/index.js"
8
+ ],
9
9
  "scripts": {
10
- "test": "node_modules/.bin/mocha --reporter spec",
11
- "cover": "node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha -- -R spec test/*"
10
+ "build": "babel src --out-dir ./dist --source-maps",
11
+ "lint": "eslint ./src",
12
+ "test": "nyc ./node_modules/.bin/mocha --timeout 10000 --require @babel/register",
13
+ "debug": "node --inspect-brk ./dist/index.js",
14
+ "cover": "mkdir -p coverage && nyc --reporter=text ./node_modules/mocha/bin/_mocha test/*.js --timeout 10000 && nyc --reporter=text-lcov ./node_modules/mocha/bin/_mocha --timeout 10000 test/*.js > coverage/lcov.info"
12
15
  },
13
16
  "repository": {
14
17
  "type": "git",
@@ -24,12 +27,43 @@
24
27
  },
25
28
  "homepage": "https://github.com/theqrl/wallet-helpers#readme",
26
29
  "devDependencies": {
27
- "chai": "^4.1.2",
28
- "coveralls": "^3.0.4",
30
+ "@babel/cli": "^7.17.10",
31
+ "@babel/core": "^7.18.5",
32
+ "@babel/eslint-parser": "^7.18.2",
33
+ "@babel/node": "^7.18.5",
34
+ "@babel/plugin-transform-async-to-generator": "^7.17.12",
35
+ "@babel/preset-env": "^7.18.2",
36
+ "@babel/register": "^7.17.7",
37
+ "babel-plugin-istanbul": "^6.1.1",
38
+ "chai": "^4.3.6",
39
+ "chai-as-promised": "^7.1.1",
40
+ "coveralls": "^3.1.1",
41
+ "eslint-config-airbnb-base": "^15.0.0",
42
+ "eslint-plugin-import": "^2.26.0",
43
+ "eslint-plugin-modules": "^1.1.1",
29
44
  "istanbul": "^0.4.5",
30
- "mocha": "^5.1.0"
45
+ "mocha": "^10.0.0",
46
+ "mocha-lcov-reporter": "^1.3.0",
47
+ "npm-run-all": "^4.1.5",
48
+ "nyc": "^15.1.0"
31
49
  },
32
50
  "dependencies": {
33
- "js-sha256": "^0.9.0"
51
+ "js-sha256": "^0.9.0",
52
+ "lodash.isequal": "^4.5.0"
53
+ },
54
+ "nyc": {
55
+ "reporter": [
56
+ "text-summary",
57
+ "html"
58
+ ],
59
+ "require": [
60
+ "@babel/register"
61
+ ],
62
+ "extension": [
63
+ ".js"
64
+ ],
65
+ "cache": false,
66
+ "temp-dir": "./test/.nyc-temp",
67
+ "report-dir": "./test/nyc"
34
68
  }
35
69
  }
package/.travis.yml DELETED
@@ -1,16 +0,0 @@
1
- language: node_js
2
- node_js:
3
- - stable
4
- install:
5
- - npm install
6
- script:
7
- - npm run cover
8
- after_script: cat coverage/lcov.info | node_modules/coveralls/bin/coveralls.js
9
- deploy:
10
- provider: npm
11
- email: jp@theqrl.org
12
- api_key:
13
- secure: 0zCPBLRbP4w86uad10HGW1kk36nWknULBhZguZtQNsEkeaoMP6fH/RG8hdOUR8yFHgu/7z6tFZHj6FuhtQtMBRt+58lsJyuFkmVskGtz51Us+lRseB+MVeYt4Csd5LlLmpy6bRwbup6j8AQwFEXLn2BmbV4UNsxQsw+z3VE45atfhiQw0Kc/Vo6wcNxwp1mOyAV+IUbEXc++iti1Gywf+ZGdG9IzkIZ2h82O/UBJd6lJ6x0kp9o0UP+MSJe0Czum276UHGB+EL6WLTHJv0AzR4eOX2DS1AQVFMn65DPVBuEUnPaF8WZKfhXaQht7/aV5I2TtsfM+jsX/fWg2GAgUxj3hcYyvErKQv+MAq2WHwTnp5rlaHWM7DjJ43X/hEELMRG5WlzCuNlmjtFI8p4jn7SS5MhwnyTunieGb6PpKf6u0Vs/r4VE6qThrInxamCq/rGoZY8mbMyuub3PpIreWVxvUWPy2WJ2kr/5CoUxFtbSuRxkkxAx589f7+XdA16lZwprldgmacQr5x8UYezZ09dAAe2xoSCialdKXOAvq82cA2jsqaRkNqRfO4cJi9Dwjoy+FstpvFHUe+WKKxebkDiN58PN3MuFwwbNOjgdsoegt/UL0G6jHZTu0qVJXtgKY0cq+K1FX//kLpH4YoUzPb7QdRW7uDRbKpPDEsux5Uz4=
14
- on:
15
- tags: true
16
- branch: master
package/index.js DELETED
@@ -1,46 +0,0 @@
1
- var sha256 = require("js-sha256");
2
-
3
- function hexToBytes(hex) {
4
- for (var bytes = [], c = 0; c < hex.length; c += 2)
5
- bytes.push(parseInt(hex.substr(c, 2), 16));
6
- return bytes;
7
- }
8
-
9
- function bytesToHex(bytes) {
10
- for (var hex = [], i = 0; i < bytes.length; i++) {
11
- hex.push((bytes[i] >>> 4).toString(16));
12
- hex.push((bytes[i] & 0xf).toString(16));
13
- }
14
- return hex.join("");
15
- }
16
-
17
- function hashPKandformat(ePK){
18
- const h = hexToBytes(ePK);
19
- return sha256(h);
20
- }
21
-
22
- function doConvert(ePK) {
23
- const a = hashPKandformat(ePK);
24
- const qx = sha256(hexToBytes('000400' + a));
25
- const qm = qx.slice(56, 64);
26
- return `Q${bytesToHex([0,4,0])}${a}${qm}`;
27
- }
28
-
29
- module.exports = {
30
- /**
31
- * Reports the current module version
32
- * @return {string} version
33
- */
34
- version: function() {
35
- return '1.0.0'
36
- },
37
- QRLAddressFromEPKHex: function (ePK) {
38
- if (ePK === undefined) {
39
- throw new Error('No ePK parameter')
40
- }
41
- if (ePK.length !== 134) {
42
- throw new Error('ePK length invalid')
43
- }
44
- return doConvert(ePK)
45
- }
46
- };
package/test/test.js DELETED
@@ -1,27 +0,0 @@
1
- var expect = require('chai').expect;
2
- var assert = require('chai').assert;
3
- var helpers = require('../index.js');
4
-
5
- describe('#deploy', function() {
6
- it(`.version should report same version as in npm package.json file (=${process.env.npm_package_version})`, function() {
7
- var result = helpers.version();
8
- expect(result).to.equal(process.env.npm_package_version);
9
- });
10
- });
11
-
12
- describe('#QRLAddressFromEPKHex', function() {
13
- it('calling with no parameter throws an error', function () {
14
- expect(() => {
15
- helpers.QRLAddressFromEPKHex()
16
- }).to.throw();
17
- })
18
- });
19
-
20
- describe('#QRLAddressFromEPKHex', function() {
21
- it('test ePK results in result of Q000400846365cd097082ce4404329d143959c8e4557d19b866ce8bf5ad7c9eb409d036651f62bd', function () {
22
- var result = helpers.QRLAddressFromEPKHex(
23
- '000400106D0856A5198967360B6BDFCA4976A433FA48DEA2A726FDAF30EA8CD3FAD2113191DA3442686282B3D5160F25CF162A517FD2131F83FBF2698A58F9C46AFC5D'
24
- );
25
- expect(result).to.equal('Q000400846365cd097082ce4404329d143959c8e4557d19b866ce8bf5ad7c9eb409d036651f62bd');
26
- })
27
- });