forked from ethereumjs/ethereumjs-wallet
-
Notifications
You must be signed in to change notification settings - Fork 4
/
hdkey.js
52 lines (42 loc) · 1.28 KB
/
hdkey.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
const HDKey = require('hdkey')
const Wallet = require('./index.js')
function EthereumHDKey () {
}
/*
* Horrible wrapping.
*/
function fromHDKey (hdkey) {
var ret = new EthereumHDKey()
ret._hdkey = hdkey
return ret
}
EthereumHDKey.fromMasterSeed = function (seedBuffer) {
return fromHDKey(HDKey.fromMasterSeed(seedBuffer))
}
EthereumHDKey.fromExtendedKey = function (base58key) {
return fromHDKey(HDKey.fromExtendedKey(base58key))
}
EthereumHDKey.prototype.privateExtendedKey = function () {
// FIXME: change this according to the outcome of https://github.com/cryptocoinjs/hdkey/issues/7
if (!this._hdkey._privateKey) {
throw new Error('Private key is not available')
}
return this._hdkey.privateExtendedKey
}
EthereumHDKey.prototype.publicExtendedKey = function () {
return this._hdkey.publicExtendedKey
}
EthereumHDKey.prototype.derivePath = function (path) {
return fromHDKey(this._hdkey.derive(path))
}
EthereumHDKey.prototype.deriveChild = function (index) {
return fromHDKey(this._hdkey.deriveChild(index))
}
EthereumHDKey.prototype.getWallet = function () {
if (this._hdkey._privateKey) {
return Wallet.fromPrivateKey(this._hdkey._privateKey)
} else {
return Wallet.fromPublicKey(this._hdkey._publicKey, true)
}
}
module.exports = EthereumHDKey