-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
210 lines (163 loc) · 7.29 KB
/
index.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
const StellarHDWallet = require("stellar-hd-wallet");
const each = require("array-each");
const loadInputFile = require("load-json-file");
const fse = require('fs-extra');
const jsonfile = require('jsonfile')
const Json2csvParser = require('json2csv').Parser;
const Json2csvTransform = require('json2csv').Transform;
const createCsvStringifier = require('csv-writer').createObjectCsvStringifier;
const CsvStream = require('json2csv-stream');
const stringToStream = require('string-to-stream');
const transform = require('stream-transform');
const replaceLast = require('replace-last');
var childDepthDepth = 6; //default valuemif not define in JSON
var depth_ParentRound = childDepthDepth;
var depth_Child1Round = childDepthDepth;
var depth_Child2Round = childDepthDepth;
const outputFile = './data/output.csv';
var allFoundKeysArray = [];
const basePath = `m/44'/148'/`;
const hideMe = "*****";
const opts = {
fields: [{
label: "Seed Index",
value: 'Seed.seed_index'
}, {
label: "Shortened Seed",
value: "Seed.seedShort"
}, {
label: "Password Index",
value: "Passphrase.passphrase_index"
}, {
label: "Shortened Password",
value: "Passphrase.passphrase"
}, {
label: "Derivation Path",
value: "DerivationPath.path"
}, {
label: "Derivation Parent Key",
value: "DerivationPath.parentkey"
}, {
label: "Stellar Public Key",
value: "Keys.pubkey"
}, {
label: "Matches Seeked Key",
value: "Keys.isMatch"
}],
delimiter: ", ",
quote: '',
unnwind: ['Seed.seed_index', 'Seed', 'Passphrase.passphrase_index', 'Passphrase.passphrase', 'DerivationPath.path_index', 'DerivationPath.path', 'DerivationPath.parentkey', 'Keys.pubkey', 'Keys.isMatch']
};
//Generate Derivation Path Object, 3 levels deep
var derivationCollection = [];
for (let i = 0; i < depth_ParentRound; i++) { //level 1
var parentIndex = i.toString() + "'";
derivationCollection.push(parentIndex);
for (let j = 0; j < depth_Child1Round; j++) { // level 2
var child1Index = j.toString() + "'";
derivationCollection.push(parentIndex + "/" + child1Index);
for (let k = 0; k < depth_Child2Round; k++) { // level 3
var child2Index = k.toString() + "'";
derivationCollection.push(parentIndex + "/" + child1Index + "/" + child2Index);
}
}
}
//Constructor for holding each one of the keys and corresponding data
function OutputKey(_Seed, _Passphrase, _DerivationPath, _Keys) {
this.Seed = _Seed
this.Passphrase = _Passphrase
this.DerivationPath = _DerivationPath
this.Keys = _Keys
}
function getFullDerivationPath(_pathObject) {
return basePath + _pathObject;
}
function getAddressFromSeed(_seed, _seed_index, _passphrase, _passphrase_index, _pathObject, _pathObjectIndex, _target) {
const wallet = (_passphrase != null) ? StellarHDWallet.fromMnemonic(_seed, _passphrase) : StellarHDWallet.fromMnemonic(_seed);
const fullDerivationPath = getFullDerivationPath(_pathObject);
//last child cannot be hardened for Public Key retrieval - stellar-hd-wallet library does not support it and throws an exception
var santitizedPath = replaceLast(_pathObject, "'", "");
var pubKey = wallet.getPublicKey(santitizedPath);
var derive = wallet.derive(fullDerivationPath);
let _s = {
seed_index: _seed_index,
seedShort: (_seed.substr(0, 5) + hideMe).substr(0, 5)
}
let _p = {
passphrase_index: _passphrase_index,
passphrase: (_passphrase.substr(0, 5) + hideMe).substr(0, 5)
}
let _d = {
path_index: _pathObjectIndex,
derive: derive,
parentkey: derive.toString('hex'),
//helps format the csv, so short derivation paths get extra padding and don't skew the columns
path: fullDerivationPath + (fullDerivationPath.length <= 13) ? fullDerivationPath + " \t" + "\t" : null,
}
let _k = {
pubkey: pubKey,
privkey: hideMe,
isMatch: (pubKey == _target)
}
console.log("Checking PublicKey @ [seedIndex: " + _s.seed_index + ", passphraseIndex: " + _p.passphrase_index + ", derivationPathIndex: " + _pathObjectIndex + "]: fullDerivationPath: " + fullDerivationPath + " - (" + pubKey.substr(0, 5) + hideMe + " == " + _target.substr(0, 5) + hideMe + ") ==> isMatch: " + _k.isMatch);
return new OutputKey(_s, _p, _d, _k);
}
//loads json file, parses and recursively populate the object with found keys
loadInputFile("./data/input.json")
.then(json => {
try {
const targets = json.targets;
console.log("INIT: Strating search with: " +
json.seeds.length +
" seed(s), " +
json.passwords.length +
" password(s) and " + derivationCollection.length + " derivation path combination(s). Attempting every possible combination... looking for " + targets.length + " target(s).");
childDepthDepth = (json.children != undefined) ? json.childre : childDepthDepth;
each(json.seeds, function(_seed, i) {
const seed = _seed;
if (StellarHDWallet.validateMnemonic(_seed)) {
const shortSeed = seed.substr(0, 30) + hideMe;
each(json.passwords, function(_passphrase, j) {
const passphrase = _passphrase;
each(derivationCollection, function(_pathObject, k) {
each(targets, function(_target, i) {
const key = getAddressFromSeed(seed, i, passphrase, j, _pathObject, k, _target);
allFoundKeysArray.push(key);
if (_target == key.Keys.pubkey) {
var found = "-------------------------------------------------------\n\r";
found += "\t target + "
_target + " FOUND!!!!!! ---> target at: " + key.Seed.seed_index + "_" + j + "_" + k + "_" + m + "\n\r";
found += "-------------------------------------------------------\n\r";
console.log(found);
allFoundKeysArray.push(found);
return;
};
});
});
})
} else {
console.log("invalid seed: " + seed.substr(0, 20) + hideMe);
}
})
} catch (error) {
console.log(error);
}
try {
console.log("please wait while output is written to file...."
)
const parser = new Json2csvParser(opts);
const outputCsvString = parser.parse(allFoundKeysArray)
console.log(outputCsvString);
fse.outputFile(outputFile, outputCsvString)
.then(() => fse.readFile(outputFile, 'utf8'))
.then(data => {
console.log(data)
console.log("done");
})
.catch(err => {
console.error(err)
})
} catch (err) {
console.error(err);
}
});