-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwalletCreation_interactif.js
More file actions
172 lines (157 loc) · 6.19 KB
/
walletCreation_interactif.js
File metadata and controls
172 lines (157 loc) · 6.19 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const crypto = require('crypto');
const fs = require('fs');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
//checksum step
/// Convert hexadecimal to a bit string
function hexToBitString(hex) {
let bitString = "";
for (let i = 0; i < hex.length; i++) {
const binary = parseInt(hex[i], 16).toString(2).padStart(4, '0');
bitString += binary;
}
return bitString;
}
//take the first 4 bits :
function checksum(SHA256_bitString, entropy_buffer_bit) {
let first4bits = SHA256_bitString.substring(0,4);
return entropy_buffer_bit+first4bits;
}
//Split in 12 segments of 11 bits
function split_hash(bit_132){
const segments = [];
for (let i = 0; i < 12; i++) {
const startIndex = i * 11;
const segment = bit_132.substring(startIndex, startIndex + 11);
segments.push(segment);
}
return segments;
}
//Do an arrays of the english words
function create_word_list(callback) {
fs.readFile('english.txt', 'utf8', (err, data) => {
if (err) {
console.error("Erreur lors de la lecture du fichier :", err);
return callback(err, null);
}
const wordList = data.split('\n');
return callback(null, wordList);
});
}
// 12 slot of 11-bit to one seed
function mnemonic_phrase(segments, wordList) {
// Convert each 11-bit part to decimal and get the corresponding word
const mnemonicWords = segments.map(part => {
const decimalValue = parseInt(part, 2);
return wordList[decimalValue];
});
// Join the words to form the mnemonic phrase
const mnemonicPhrase = mnemonicWords.join(" ");
return mnemonicPhrase;
}
//Q4 Import seed:
function createWordMap(callback) {
fs.readFile('english.txt', 'utf8', (err, data) => {
if (err) {
console.error("Error reading the file:", err);
return callback(err, null);
}
const wordList = data.split('\n');
const wordMap = new Map(wordList.map((word, index) => [word, index]));
return callback(null, wordMap);
});
}
function mnemonicToBits(mnemonic, wordMap) {
const bits = [];
mnemonic.split(' ').forEach(word => {
const index = wordMap.get(word);
if (index !== undefined) {
const binaryString = index.toString(2).padStart(11, '0');
bits.push(binaryString);
} else {
console.error(`Unknown word in mnemonic: ${word}`);
}
});
return bits.join('');
}
function main() {
// Q1
// Generate a 128-bit (16-byte) random number
const entropy_buffer = crypto.randomBytes(16);
console.log("Random number:",entropy_buffer);
// Convert buffer to hexadecimal
const buffer_hexString = entropy_buffer.toString('hex');
console.log("entropy_hex",buffer_hexString);
// Convert Entropy_hexadecimal in bit
entropy_buffer_bit = hexToBitString(buffer_hexString);
console.log("entropy_bit:",entropy_buffer_bit);
console.log("");
rl.question("Voulez-vous représenter cette seed en binaire et la découper en lots de 11 bits ? (o/n): ", (answer) => {
if (answer.toLowerCase() === 'o') {
//Q2
// Calculate the SHA-256 hash of the entropy_buffer
const sha256_hash = crypto.createHash('sha256').update(entropy_buffer).digest('hex');
console.log("Entropy SHA-256 Hash:", sha256_hash);
//Convert the SHA256 in Bit
const sha256BitString = hexToBitString(sha256_hash);
console.log("Entropy SHA-256 Bit String:", sha256BitString);
//Add to the entropy number the 4 bits of the SHA256 hash
const bit_132_hash = checksum(sha256BitString,entropy_buffer_bit);
console.log("132 bits:", bit_132_hash);
// Cut in 12 slots of 11 bits
const segments = split_hash(bit_132_hash);
console.log("132 bits divided", segments);
console.log("");
rl.question("Voulez-vous Attribuer à chaque lot un mot selon la liste BIP 39 et afficher la seed en mnémonique ? (o/n): ", (answer) => {
if (answer.toLowerCase() === 'o') {
//Q3
create_word_list((err, wordlist) => {
if (err) {
// Gérer l'erreur
console.error("Erreur lors de la création de la liste de mots :", err);
return;
}
const seed = mnemonic_phrase(segments, wordlist);
console.log("Mnemonic Phrase:", seed);
console.log("");
rl.question("Voulez-vous importer une seed mnémonique ? A partir de la seed précédement crée (o/n) :", (answer) => {
if (answer.toLowerCase() === 'o') {
//Q4
const mnemonic = seed;
createWordMap((err, wordMap) => {
if (err) {
console.error("Error creating word map:", err);
return;
}
const bitString = mnemonicToBits(mnemonic, wordMap);
console.log("132 bits Import:", bitString);
rl.close();
});
} else {
console.log("Programme terminé.");
rl.close();
}
});
});
} else {
console.log("Programme terminé.");
rl.close();
}
});
} else {
console.log("Programme terminé.");
rl.close();
}
});
}
rl.question("Voulez-vous générer un nombre aléatoire de 128 bits ? (o/n): ", (answer) => {
if (answer.toLowerCase() === 'o') {
main();;
} else {
console.log("Programme terminé.");
rl.close();
}
});