Skip to content

Commit a15bf48

Browse files
authored
Merge pull request #27 from Adamant-im/dev
Dev
2 parents 00174ff + ed91952 commit a15bf48

File tree

6 files changed

+56
-26
lines changed

6 files changed

+56
-26
lines changed

package.json

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "adamant-api",
3-
"version": "1.6.0",
3+
"version": "1.7.0",
44
"description": "REST API for ADAMANT Blockchain",
55
"main": "src/index.js",
66
"scripts": {
@@ -13,18 +13,20 @@
1313
"license": "GPL-3.0",
1414
"dependencies": {
1515
"@liskhq/lisk-cryptography": "3.2.1",
16-
"axios": "^1.1.3",
17-
"bignumber.js": "^9.1.0",
18-
"bitcoinjs-lib": "^5.2.0",
16+
"axios": "^1.2.1",
17+
"bignumber.js": "^9.1.1",
18+
"bitcoinjs-lib": "^6.1.0",
1919
"bitcore-mnemonic": "^8.25.40",
2020
"bytebuffer": "^5.0.1",
2121
"coininfo": "^5.2.1",
22+
"ecpair": "^2.1.0",
2223
"ed2curve": "^0.3.0",
2324
"ethereumjs-util": "^7.1.5",
2425
"hdkey": "^2.0.1",
2526
"pbkdf2": "^3.1.2",
26-
"socket.io-client": "^4.5.3",
27-
"sodium-browserify-tweetnacl": "^0.2.6"
27+
"socket.io-client": "^4.5.4",
28+
"sodium-browserify-tweetnacl": "^0.2.6",
29+
"tiny-secp256k1": "^2.2.1"
2830
},
2931
"publishConfig": {
3032
"access": "public"
@@ -63,11 +65,11 @@
6365
},
6466
"homepage": "https://github.com/Adamant-im/adamant-api-jsclient#readme",
6567
"devDependencies": {
66-
"@commitlint/cli": "^17.2.0",
67-
"@commitlint/config-conventional": "^17.2.0",
68-
"eslint": "^8.27.0",
68+
"@commitlint/cli": "^17.3.0",
69+
"@commitlint/config-conventional": "^17.3.0",
70+
"eslint": "^8.30.0",
6971
"eslint-config-google": "^0.14.0",
70-
"eslint-plugin-jest": "^27.1.5",
72+
"eslint-plugin-jest": "^27.1.7",
7173
"husky": "^8.0.2",
7274
"jest": "^29.3.1"
7375
}

src/groups/btc.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
const bitcoin = require('bitcoinjs-lib');
2+
const {ECPairFactory} = require('ecpair');
3+
const tinysecp = require('tiny-secp256k1');
4+
25
const coinNetworks = require('./coinNetworks');
3-
const btc = { };
6+
const btc = {};
47

58
/**
69
* Generates a BTC account from the passphrase specified.
710
* @param {string} passphrase ADAMANT account passphrase
8-
* @returns {object} network info, keyPair, privateKey, privateKeyWIF
11+
* @return {object} network info, keyPair, privateKey, privateKeyWIF
912
*/
10-
1113
btc.keys = (passphrase) => {
1214
const network = coinNetworks.BTC;
1315
const pwHash = bitcoin.crypto.sha256(Buffer.from(passphrase));
14-
const keyPair = bitcoin.ECPair.fromPrivateKey(pwHash, {network});
16+
17+
const ECPairAPI = new ECPairFactory(tinysecp);
18+
const keyPair = ECPairAPI.fromPrivateKey(pwHash, {network});
1519

1620
return {
1721
network,

src/groups/dash.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
const bitcoin = require('bitcoinjs-lib');
2+
const {ECPairFactory} = require('ecpair');
3+
const tinysecp = require('tiny-secp256k1');
4+
25
const coinNetworks = require('./coinNetworks');
36
const dash = { };
47

58
/**
69
* Generates a DASH account from the passphrase specified.
710
* @param {string} passphrase ADAMANT account passphrase
8-
* @returns {object} network info, keyPair, privateKey, privateKeyWIF
11+
* @return {object} network info, keyPair, privateKey, privateKeyWIF
912
*/
10-
1113
dash.keys = (passphrase) => {
1214
const network = coinNetworks.DASH;
1315
const pwHash = bitcoin.crypto.sha256(Buffer.from(passphrase));
14-
const keyPair = bitcoin.ECPair.fromPrivateKey(pwHash, {network});
16+
17+
const ECPairAPI = new ECPairFactory(tinysecp);
18+
const keyPair = ECPairAPI.fromPrivateKey(pwHash, {network});
1519

1620
return {
1721
network,

src/groups/doge.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
const bitcoin = require('bitcoinjs-lib');
2+
const {ECPairFactory} = require('ecpair');
3+
const tinysecp = require('tiny-secp256k1');
4+
25
const coinNetworks = require('./coinNetworks');
3-
const doge = { };
6+
const doge = {};
47

58
/**
69
* Generates a DOGE account from the passphrase specified.
710
* @param {string} passphrase ADAMANT account passphrase
8-
* @returns {object} network info, keyPair, privateKey, privateKeyWIF
11+
* @return {object} network info, keyPair, privateKey, privateKeyWIF
912
*/
10-
1113
doge.keys = (passphrase) => {
1214
const network = coinNetworks.DOGE;
1315
const pwHash = bitcoin.crypto.sha256(Buffer.from(passphrase));
14-
const keyPair = bitcoin.ECPair.fromPrivateKey(pwHash, {network});
16+
17+
const ECPairAPI = new ECPairFactory(tinysecp);
18+
const keyPair = ECPairAPI.fromPrivateKey(pwHash, {network});
1519

1620
return {
1721
network,

src/helpers/healthCheck.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const {RE_IP, RE_HTTP_URL} = require('./constants');
1010
const CHECK_NODES_INTERVAL = 60 * 5 * 1000; // Update active nodes every 5 minutes
1111
const HEIGHT_EPSILON = 5; // Used to group nodes by height and choose synced
1212

13-
module.exports = (nodes, checkHealthAtStartup = true) => {
13+
module.exports = (nodes, checkHealthAtStartup = true, checkHealthAtStartupCallback) => {
1414
const nodesList = nodes;
1515
let isCheckingNodes = false;
1616

@@ -37,8 +37,9 @@ module.exports = (nodes, checkHealthAtStartup = true) => {
3737
/**
3838
* Requests every ADAMANT node for its status, makes a list of live nodes, and chooses one active
3939
* @param {boolean} forceChangeActiveNode
40+
* @param {function?} checkNodesCallback
4041
*/
41-
async function checkNodes(forceChangeActiveNode) {
42+
async function checkNodes(forceChangeActiveNode, checkNodesCallback) {
4243
isCheckingNodes = true;
4344

4445
const liveNodes = [];
@@ -163,15 +164,18 @@ module.exports = (nodes, checkHealthAtStartup = true) => {
163164
}
164165

165166
isCheckingNodes = false;
167+
checkNodesCallback?.();
166168
}
167169

168170
if (checkHealthAtStartup) {
169-
changeNodes(true);
171+
changeNodes(true, checkHealthAtStartupCallback);
170172

171173
setInterval(
172174
() => changeNodes(true),
173175
CHECK_NODES_INTERVAL,
174176
);
177+
} else {
178+
checkHealthAtStartupCallback?.();
175179
}
176180

177181
return {

src/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,24 @@ const constants = require('./helpers/constants.js');
2222
const transactionFormer = require('./helpers/transactionFormer');
2323
const keys = require('./helpers/keys');
2424

25-
module.exports = (params, customLogger) => {
25+
/**
26+
* Create new API instance
27+
* @param {{ node: string, checkHealthAtStartup: boolean, logLevel: string}} params
28+
* @param {{ error: function, warn: function, info: function, log: function }?} customLogger
29+
* @param {function?} checkHealthAtStartupCallback callback which is called after the first health check or
30+
* just at startup when params.checkHealthAtStartup is passed
31+
* @return {object} API methods and helpers
32+
*/
33+
module.exports = (params, customLogger, checkHealthAtStartupCallback) => {
2634
const log = customLogger || console;
2735

2836
logger.initLogger(params.logLevel, log);
2937

30-
const nodeManager = healthCheck(params.node, params.checkHealthAtStartup);
38+
const nodeManager = healthCheck(
39+
params.node,
40+
params.checkHealthAtStartup,
41+
checkHealthAtStartupCallback,
42+
);
3143

3244
return {
3345
get: get(nodeManager),

0 commit comments

Comments
 (0)