forked from go-faast/ethereum-payments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (51 loc) · 1.75 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
const ethereumAddress = require('ethereum-address')
const bitcore = require('bitcore-lib')
const EthereumBip44 = require('./ethereum-bip44')()
function EthDepositUtils (options) {
if (!(this instanceof EthDepositUtils)) return new EthDepositUtils(options)
let self = this
self.options = Object.assign({}, options || {})
// if (!self.options.password) throw new Error('EthDepositUtils: password required')
return self
}
// https://github.com/trapp/ethereum-bip44
EthDepositUtils.prototype.bip44 = function (xpub, path) {
let self = this
let address = EthereumBip44.getAddress(xpub, path)
if (ethereumAddress.isAddress(address)) {
return address
} else {
return new Error('address validation failed')
}
}
// // https://github.com/trapp/ethereum-bip44
EthDepositUtils.prototype.getPrivateKey = function (xprv, path) {
let self = this
if (!xprv) throw new Error('Xprv is null. Bad things will happen to you.')
// create the hd wallet
let secretKey = EthereumBip44.getPrivateKey(xprv, path)
return secretKey
}
EthDepositUtils.prototype.privateToPublic = function (privateKey) {
let pub = EthereumBip44.privateToPublic(privateKey)
if (ethereumAddress.isAddress(pub)) {
return pub
} else {
return new Error('address validation failed')
}
}
EthDepositUtils.prototype.generateNewKeys = function () {
// to gererate a key:
let key = new bitcore.HDPrivateKey()
let derivedPubKey = key.derive("m/44'/60'/0'/0").hdPublicKey
return {
xpub: derivedPubKey.toString(),
xprv: key.toString()
}
}
EthDepositUtils.prototype.getXpubFromXprv = function (xprv) {
let key = new bitcore.HDPrivateKey(xprv)
let derivedPubKey = key.derive("m/44'/60'/0'/0").hdPublicKey
return derivedPubKey.toString()
}
module.exports = EthDepositUtils