This repository has been archived by the owner on Mar 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
219 lines (183 loc) · 6.35 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
211
212
213
214
215
216
217
218
219
const EventEmitter = require('events').EventEmitter
const BITBOXSDK = require('bitbox-sdk').BITBOX
const BITBOX = new BITBOXSDK()
// Options:
const hdPathString = "m/44'/145'"
const slpHdPathString = "m/44'/245'"
const type = 'HD Key Tree'
class HdKeyring extends EventEmitter {
/* PUBLIC METHODS */
constructor (opts = {}) {
super()
this.type = type
this.deserialize(opts)
}
serialize () {
return Promise.resolve({
mnemonic: this.mnemonic,
numberOfAccounts: this.wallets.length,
hdPath: hdPathString,
slpHdPath: slpHdPathString,
})
}
deserialize (opts = {}) {
this.opts = opts || {}
this.wallets = []
this.slpWallets = []
this.mnemonic = null
this.root = null
this.hdPath = hdPathString
this.slpHdPath = slpHdPathString
if (opts.mnemonic) {
this._initFromMnemonic(opts.mnemonic)
}
if (opts.numberOfAccounts) {
return this.addAccounts(opts.numberOfAccounts)
}
return Promise.resolve([])
}
addAccounts (numberOfAccounts = 1) {
if (!this.root || !this.slpRoot) {
const mnemonic = this.mnemonic ? this.mnemonic : BITBOX.Mnemonic.generate(128)
this._initFromMnemonic(mnemonic)
}
const oldLen = this.wallets.length
const newWallets = []
for (let i = oldLen; i < numberOfAccounts + oldLen; i++) {
const child = BITBOX.HDNode.derivePath(this.root, `${i}'/0/0`)
const wallet = BITBOX.HDNode.toKeyPair(child)
newWallets.push(wallet)
this.wallets.push(wallet)
}
const hexWallets = newWallets.map((w) => {
return this._getAddress(w)
})
// Add matching amount of SLP accounts
const slpAccountsToAdd = this.wallets.length - this.slpWallets.length
if (slpAccountsToAdd > 0) {
this.addSlpAccounts(slpAccountsToAdd)
}
return Promise.resolve(hexWallets)
}
addSlpAccounts (numberOfAccounts = 1) {
if (!this.root || !this.slpRoot) {
const mnemonic = this.mnemonic ? this.mnemonic : BITBOX.Mnemonic.generate(128)
this._initFromMnemonic(mnemonic)
}
const oldLen = this.slpWallets.length
const newWallets = []
for (let i = oldLen; i < numberOfAccounts + oldLen; i++) {
const child = BITBOX.HDNode.derivePath(this.slpRoot, `${i}'/0/0`)
const wallet = BITBOX.HDNode.toKeyPair(child)
newWallets.push(wallet)
this.slpWallets.push(wallet)
}
const hexWallets = newWallets.map((w) => {
return this._getAddress(w)
})
return Promise.resolve(hexWallets)
}
getAccounts () {
return Promise.resolve(this.wallets.map((w) => {
return this._getAddress(w)
}))
}
getSlpAccounts () {
return Promise.resolve(this.slpWallets.map((w) => {
return this._getAddress(w)
}))
}
getAllAccounts () {
const allWallets = this.wallets.concat(this.slpWallets)
const allAccounts = allWallets.map((w) => {
return this._getAddress(w)
})
return Promise.resolve(allAccounts)
}
// tx is an instance of the ethereumjs-transaction class.
signTransaction (address, tx) {
const wallet = this._getWalletForAccount(address)
var privKey = wallet.getPrivateKey()
tx.sign(privKey)
return Promise.resolve(tx)
}
// For eth_sign, we need to sign transactions:
// hd
signMessage (withAccount, message) {
const wallet = this._getWalletForAccount(withAccount)
const privKey = BITBOX.ECPair.toWIF(wallet)
const signature = BITBOX.BitcoinCash.signMessageWithPrivKey(privKey, message)
return Promise.resolve(signature)
}
// signMessage (withAccount, data) {
// const wallet = this._getWalletForAccount(withAccount)
// const message = ethUtil.stripHexPrefix(data)
// var privKey = wallet.getPrivateKey()
// var msgSig = ethUtil.ecsign(new Buffer(message, 'hex'), privKey)
// var rawMsgSig = ethUtil.bufferToHex(sigUtil.concatSig(msgSig.v, msgSig.r, msgSig.s))
// return Promise.resolve(rawMsgSig)
// }
// // For personal_sign, we need to prefix the message:
// signPersonalMessage (withAccount, msgHex) {
// const wallet = this._getWalletForAccount(withAccount)
// const privKey = ethUtil.stripHexPrefix(wallet.getPrivateKey())
// const privKeyBuffer = new Buffer(privKey, 'hex')
// const sig = sigUtil.personalSign(privKeyBuffer, { data: msgHex })
// return Promise.resolve(sig)
// }
// // personal_signTypedData, signs data along with the schema
// signTypedData (withAccount, typedData) {
// const wallet = this._getWalletForAccount(withAccount)
// const privKey = ethUtil.toBuffer(wallet.getPrivateKey())
// const signature = sigUtil.signTypedData(privKey, { data: typedData })
// return Promise.resolve(signature)
// }
// // For eth_sign, we need to sign transactions:
// newGethSignMessage (withAccount, msgHex) {
// const wallet = this._getWalletForAccount(withAccount)
// const privKey = wallet.getPrivateKey()
// const msgBuffer = ethUtil.toBuffer(msgHex)
// const msgHash = ethUtil.hashPersonalMessage(msgBuffer)
// const msgSig = ethUtil.ecsign(msgHash, privKey)
// const rawMsgSig = ethUtil.bufferToHex(sigUtil.concatSig(msgSig.v, msgSig.r, msgSig.s))
// return Promise.resolve(rawMsgSig)
// }
exportKeyPair (address) {
const wallet = this._getWalletForAccount(address)
return Promise.resolve(wallet)
}
exportAccount (address) {
const wallet = this._getWalletForAccount(address)
const privateKey = BITBOX.ECPair.toWIF(wallet)
return Promise.resolve(privateKey)
}
/* PRIVATE METHODS */
_initFromMnemonic (mnemonic) {
this.mnemonic = mnemonic
const seed = BITBOX.Mnemonic.toSeed(mnemonic)
this.hdWallet = BITBOX.HDNode.fromSeed(seed, 'bitcoincash')
this.root = BITBOX.HDNode.derivePath(this.hdWallet, this.hdPath)
this.slpRoot = BITBOX.HDNode.derivePath(this.hdWallet, this.slpHdPath)
}
_getAddress(keypair) {
return BITBOX.ECPair.toCashAddress(keypair)
}
_getWalletForAccount (account) {
const targetAddress = account
const wallet = this.wallets.find((w) => {
const address = this._getAddress(w)
return (address === targetAddress)
})
// Check for SLP wallet if not found
if (!wallet) {
return this.slpWallets.find((w) => {
const address = this._getAddress(w)
return (address === targetAddress)
})
} else {
return wallet
}
}
}
HdKeyring.type = type
module.exports = HdKeyring