This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 455
/
rounds.js
130 lines (109 loc) · 3.45 KB
/
rounds.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright © 2018 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/
'use strict';
/* eslint-disable no-plusplus */
const {
hash,
getPrivateAndPublicKeyBytesFromPassphrase,
decryptPassphraseWithPassword,
parseEncryptedPassphrase,
} = require('@liskhq/lisk-cryptography');
const { clone } = require('lodash');
const getKeysSortByVote = accountsState =>
clone(accountsState)
.filter(account => account.isDelegate)
.sort((accountA, accountB) => {
if (accountA.vote === accountB.vote) {
return accountA.publicKey > accountB.publicKey;
}
return accountA.vote < accountB.vote;
})
.map(account => account.publicKey);
/**
* Gets delegate list based on input function by vote and changes order.
*
* @param {number} round
* @param {function} source - Source function for get delegates
* @param {function} cb - Callback function
* @param {Object} tx - Database transaction/task object
* @returns {setImmediateCallback} cb, err, truncated delegate list
* @todo Add description for the params
*/
const generateDelegateList = (accountsState, round) => {
const truncDelegateList = getKeysSortByVote(accountsState);
const seedSource = round.toString();
let currentSeed = hash(seedSource, 'utf8');
for (let i = 0, delCount = truncDelegateList.length; i < delCount; i++) {
for (let x = 0; x < 4 && i < delCount; i++, x++) {
const newIndex = currentSeed[x] % delCount;
const b = truncDelegateList[newIndex];
truncDelegateList[newIndex] = truncDelegateList[i];
truncDelegateList[i] = b;
}
currentSeed = hash(currentSeed);
}
return truncDelegateList;
};
const decryptKeypairs = config => {
const encryptedList = config.forging.delegates;
const password = config.forging.defaultPassword;
const keypairs = {};
// eslint-disable-next-line no-restricted-syntax
for (const encryptedItem of encryptedList) {
let passphrase;
try {
passphrase = decryptPassphraseWithPassword(
parseEncryptedPassphrase(encryptedItem.encryptedPassphrase),
password,
);
} catch (e) {
throw new Error('Invalid password and public key combination');
}
const {
publicKeyBytes,
privateKeyBytes,
} = getPrivateAndPublicKeyBytesFromPassphrase(passphrase);
const keypair = {
publicKey: publicKeyBytes,
privateKey: privateKeyBytes,
};
if (keypair.publicKey.toString('hex') !== encryptedItem.publicKey) {
throw new Error(
`Invalid encryptedPassphrase for publicKey: ${
encryptedItem.publicKey
}. Public keys do not match`,
);
}
keypairs[keypair.publicKey.toString('hex')] = keypair;
}
return keypairs;
};
const getDelegateKeypairForCurrentSlot = (
config,
accountsState,
currentSlot,
round,
) => {
const keypairs = decryptKeypairs(config);
const activeDelegates = generateDelegateList(accountsState, round);
const currentSlotIndex = currentSlot % config.constants.ACTIVE_DELEGATES;
const currentSlotDelegate = activeDelegates[currentSlotIndex];
if (currentSlotDelegate && keypairs[currentSlotDelegate]) {
return keypairs[currentSlotDelegate];
}
return null;
};
module.exports = {
getDelegateKeypairForCurrentSlot,
};