-
Notifications
You must be signed in to change notification settings - Fork 0
/
offline.js
220 lines (186 loc) · 5.96 KB
/
offline.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { Keypair } from '@solana/web3.js';
import bip39 from 'bip39';
import { ethers } from 'ethers';
import fs from 'fs';
import QRCode from 'qrcode';
import readline from 'readline';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import * as ed25519 from 'ed25519-hd-key';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
async function getDiceRolls(useGuess = false) {
if (useGuess) {
console.log('Using random dice rolls for debugging...');
return Array.from({ length: 200 }, () => Math.floor(Math.random() * 6) + 1);
}
return new Promise((resolve) => {
let allRolls = '';
const processInput = (input) => {
allRolls += input.replace(/[^1-6]/g, '');
if (allRolls.length >= 200) {
rl.close();
resolve(allRolls.slice(0, 200).split('').map(Number));
} else {
console.log(`Received ${allRolls.length} valid rolls. Need ${200 - allRolls.length} more.`);
rl.question('Enter more dice rolls (1-6): ', processInput);
}
};
console.log('Enter at least 200 dice rolls (1-6) as a continuous string:');
rl.question('Dice rolls: ', processInput);
});
}
function diceRollsToBits(rolls) {
const rollToBits = {
1: '00',
2: '01',
3: '10',
4: '11',
5: '0',
6: '1',
};
let bits = rolls.map((roll) => rollToBits[roll]).join('');
bits = bits.padEnd(256, '0').slice(0, 256);
console.log('Generated bits length:', bits.length);
console.log('First 32 bits:', bits.slice(0, 32));
console.log('Last 32 bits:', bits.slice(-32));
return bits;
}
function bitsToMnemonic(bits) {
console.log('Bits length:', bits.length);
console.log('First 32 bits:', bits.slice(0, 32));
console.log('Last 32 bits:', bits.slice(-32));
if (bits.length !== 256) {
throw new Error(`Invalid bits length: ${bits.length}. Expected 256 bits.`);
}
if (!/^[01]+$/.test(bits)) {
throw new Error('Invalid bits: should only contain 0 and 1');
}
// Convert bits to bytes
const bytes = new Uint8Array(32);
for (let i = 0; i < 32; i++) {
bytes[i] = parseInt(bits.slice(i * 8, (i + 1) * 8), 2);
}
const entropy = Buffer.from(bytes);
console.log('Entropy buffer length:', entropy.length);
console.log('Entropy buffer (hex):', entropy.toString('hex'));
if (entropy.length !== 32) {
throw new Error(`Invalid entropy length: ${entropy.length}. Expected 32 bytes.`);
}
return bip39.entropyToMnemonic(entropy);
}
function generateAddresses(mnemonic) {
const seed = bip39.mnemonicToSeedSync(mnemonic);
const ethPaths = {
bip44: "m/44'/60'/0'/0/0",
ledgerLive: "m/44'/60'/0'/0/0",
ledgerLegacy: "m/44'/60'/0'",
};
const solanaPaths = {
bip44: "m/44'/501'/0'/0'",
bip44Change: "m/44'/501'/0'/0'/0'",
bip44ChangeIndex1: "m/44'/501'/0'/0'/1'",
ledgerLive: "m/44'/501'/0'",
ledgerLiveIndex1: "m/44'/501'/1'",
};
const ethAddresses = Object.fromEntries(
Object.entries(ethPaths).map(([key, path]) => [
key,
{ path, address: ethers.HDNodeWallet.fromPhrase(mnemonic, path).address },
])
);
const deriveSolanaAddress = (path) => {
const derivedSeed = ed25519.derivePath(path, Buffer.from(seed, 'hex')).key;
const keypair = Keypair.fromSeed(derivedSeed);
return keypair.publicKey.toBase58();
};
const solanaAddresses = Object.fromEntries(
Object.entries(solanaPaths).map(([key, path]) => [key, { path, address: deriveSolanaAddress(path) }])
);
return { ethAddresses, solanaAddresses };
}
async function generateQRCode(text) {
try {
return await QRCode.toDataURL(text);
} catch (err) {
console.error('Error generating QR code:', err);
return '';
}
}
async function generateHTML(addresses, mnemonic) {
const generateTable = async (title, addressData) => {
const qrCodes = await Promise.all(Object.values(addressData).map((data) => generateQRCode(data.address)));
return `
<h2>${title} Addresses</h2>
<table>
<tr>
<th>Type</th>
<th>Derivation Path</th>
<th>Address</th>
<th>QR Code</th>
</tr>
${Object.entries(addressData)
.map(
([type, data], index) => `
<tr>
<td>${type}</td>
<td>${data.path}</td>
<td>${data.address}</td>
<td><img src="${qrCodes[index]}" alt="${type} QR"></td>
</tr>
`
)
.join('')}
</table>
`;
};
const ethTable = await generateTable('Ethereum', addresses.ethAddresses);
const solanaTable = await generateTable('Solana', addresses.solanaAddresses);
return `
<html>
<head>
<style>
table { border-collapse: collapse; margin-bottom: 20px; }
th, td { border: 1px solid black; padding: 10px; text-align: center; }
img { width: 150px; height: 150px; }
</style>
</head>
<body>
${ethTable}
${solanaTable}
<h2>Mnemonic</h2>
<p>${mnemonic}</p>
</body>
</html>
`;
}
async function main() {
try {
const argv = yargs(hideBin(process.argv))
.option('guess', {
type: 'boolean',
description: 'Use random dice rolls for debugging',
})
.parse();
const diceRolls = await getDiceRolls(argv.guess);
const bits = diceRollsToBits(diceRolls);
console.log('Dice rolls:', diceRolls.join(''));
const mnemonic = bitsToMnemonic(bits);
const addresses = generateAddresses(mnemonic);
const html = await generateHTML(addresses, mnemonic);
// Create output directory if it doesn't exist
const outputDir = './output';
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
const filename = `${outputDir}/${new Date().toISOString().replace(/:/g, '-')}.html`;
fs.writeFileSync(filename, html);
console.log(`Output saved to ${filename}`);
} catch (error) {
console.error('An error occurred:', error);
console.error('Stack trace:', error.stack);
}
}
main();