-
Notifications
You must be signed in to change notification settings - Fork 52
/
account.js
99 lines (76 loc) · 2.46 KB
/
account.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
const bip32 = require('bip32')
const discovery = require('./discovery')
const Chain = require('./chain')
function Account (chains) {
this.chains = chains
}
Account.fromJSON = function (json, network, addressFunction) {
const chains = json.map(function (j) {
const node = bip32.fromBase58(j.node, network)
const chain = new Chain(node, j.k, addressFunction)
chain.map = j.map
// derive from k map
chain.addresses = Object.keys(chain.map).sort(function (a, b) {
return chain.map[a] - chain.map[b]
})
return chain
})
return new Account(chains)
}
Account.prototype.clone = function () {
return new Account(this.chains.map(function (chain) {
return chain.clone()
}))
}
Account.prototype.containsAddress = function (address) {
return this.chains.some(function (chain) {
return chain.find(address) !== undefined
})
}
// optional parents argument for private key escalation
Account.prototype.derive = function (address, parents) {
let derived
this.chains.some(function (chain, i) {
derived = chain.derive(address, parents && parents[i])
return derived
})
return derived
}
Account.prototype.discoverChain = function (i, gapLimit, queryCallback, callback) {
const chains = this.chains
const chain = chains[i].clone()
discovery(chain, gapLimit, queryCallback, function (err, used, checked) {
if (err) return callback(err)
// throw away EACH unused address AFTER the last unused address
const unused = checked - used
for (let j = 1; j < unused; ++j) chain.pop()
// override the internal chain
chains[i] = chain
callback()
})
}
Account.prototype.getAllAddresses = function () {
return [].concat.apply([], this.chains.map(function (chain) {
return chain.getAll()
}))
}
Account.prototype.getChain = function (i) { return this.chains[i] }
Account.prototype.getChains = function () { return this.chains }
Account.prototype.getChainAddress = function (i) { return this.chains[i].get() }
Account.prototype.getNetwork = function () { return this.chains[0].getParent().network }
Account.prototype.isChainAddress = function (i, address) {
return this.chains[i].find(address) !== undefined
}
Account.prototype.nextChainAddress = function (i) {
return this.chains[i].next()
}
Account.prototype.toJSON = function () {
return this.chains.map(function (chain) {
return {
k: chain.k,
map: chain.map,
node: chain.getParent().toBase58()
}
})
}
module.exports = Account