forked from bitcoinjs/bip32-utils
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdiscovery.js
More file actions
41 lines (32 loc) · 774 Bytes
/
discovery.js
File metadata and controls
41 lines (32 loc) · 774 Bytes
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
// https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki#account-discovery
module.exports = function discovery (chain, gapLimit, queryCb, done) {
var gap = 0
var checked = 0
function cycle () {
var batch = [chain.get()]
checked++
while (batch.length < gapLimit) {
chain.next()
batch.push(chain.get())
checked++
}
queryCb(batch, function (err, results) {
if (err) return done(err)
results.forEach(function (isUsed) {
if (isUsed) {
gap = 0
} else {
gap += 1
}
})
if (gap >= gapLimit) {
var used = checked - gap
return done(undefined, used, checked)
} else {
chain.next()
}
cycle()
})
}
cycle()
}