-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
56 lines (49 loc) · 1.37 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
import hdAddress from 'hd-address';
import * as cli from "./lib/cli.js";
const keyType = hdAddress.keyType
class Wallet {
constructor(hd, hdpath = '') {
this.__hdwallet = hd
this.__hdpath = hdpath
}
getAll() {
return this.__hdwallet.getAddressByPath(this.__hdpath)
}
hdpath() {
return this.getAll().path
}
getAddress() {
return this.getAll().address
}
getPublicKey() {
return this.getAll().pub
}
getPrivateKey() {
return this.getAll().pri
}
derive(hdpath) {
if (typeof hdpath === undefined) return this
const clone = Object.assign(Object.create(Object.getPrototypeOf(this)), this)
if (/^[0-9]+'?$/.test(hdpath)) {
hdpath = `/${hdpath}`
}
clone.__hdpath = this.__hdpath + hdpath
return clone
}
}
export default {
cli,
fromMnemonic: (mnemonic, coin, pwd) => {
let wallet = hdAddress.HD(mnemonic, keyType.mnemonic, pwd)
return new Wallet(wallet[coin])
},
fromSeed: (seed, coin, pwd) => {
let seedBuf = Buffer.from(seed, "hex")
let wallet = hdAddress.HD(seedBuf, keyType.seed, pwd)
return new Wallet(wallet[coin])
},
fromBase58: (base58, coin, pwd) => {
let wallet = hdAddress.HD(base58, keyType.base58, pwd)
return new Wallet(wallet[coin])
}
}