-
Notifications
You must be signed in to change notification settings - Fork 22
/
wallet.ts
120 lines (100 loc) · 3.41 KB
/
wallet.ts
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
const Buffer = require('buffer/').Buffer
const bip39 = require('bip39');
const blake = require('blakejs/blake2b');
import { checkParams, isNonNegativeInteger } from '~@vite/vitejs-utils';
import * as hdKey from './hdKey';
import * as addressLib from './address';
import { Hex, Address } from './type';
interface AddressObj {
publicKey: Hex;
privateKey: Hex;
originalAddress: Hex;
address: Address;
path: string;
}
class Wallet {
readonly rootPath: string;
readonly mnemonics: string;
readonly entropy: Hex;
readonly wordlist: Array<string>;
readonly passphrase: string;
readonly seed: Buffer;
readonly seedHex: Hex;
private addressList: Object;
constructor(mnemonics: string, wordlist: Array<string> = bip39.wordlists.EN, passphrase = '') {
if (!hdKey.validateMnemonics(mnemonics, wordlist)) {
throw new Error('Illegal mnemonic');
}
this.rootPath = hdKey.ROOT_PATH;
this.mnemonics = mnemonics;
this.wordlist = wordlist;
this.passphrase = passphrase;
this.entropy = hdKey.getEntropyFromMnemonics(mnemonics, wordlist);
const { seed, seedHex } = hdKey.getSeedFromMnemonics(mnemonics, passphrase, wordlist);
this.seed = seed;
this.seedHex = seedHex;
this.addressList = {};
}
get id(): Hex {
let address = '';
if (this.addressList[0]) {
address = this.addressList[0].address;
} else {
const account = this.deriveAddress(0);
address = account.address;
}
const addressBuffer = Buffer.from(address);
const idBuffer = blake.blake2b(addressBuffer, null, 32);
return Buffer.from(idBuffer).toString('hex');
}
getAddressList() {
return this.addressList;
}
deriveAddress(index: number): AddressObj {
const err = checkParams({ index }, ['index'], [{
name: 'index',
func: isNonNegativeInteger
}]);
if (err) {
throw new Error(err.message);
}
if (this.addressList[index]) {
return this.addressList[index];
}
const path = hdKey.getPath(index);
const { privateKey, publicKey } = hdKey.deriveKeyPairByPath(this.seedHex, path);
const address = addressLib.getAddressFromPublicKey(publicKey);
const originalAddress = addressLib.getOriginalAddressFromAddress(address);
const account: AddressObj = {
privateKey,
publicKey,
address,
originalAddress,
path
};
this.addressList[index] = account;
return account;
}
deriveAddressList(startIndex: number, endIndex: number): Array<AddressObj> {
const err = checkParams({ startIndex, endIndex }, [ 'startIndex', 'endIndex' ], [ {
name: 'startIndex',
func: isNonNegativeInteger
}, {
name: 'endIndex',
func: isNonNegativeInteger
} ]);
if (err) {
throw new Error(err.message);
}
if (startIndex > endIndex) {
throw new Error('Illegal index');
}
const addressList: Array<AddressObj> = [];
for (let i = startIndex; i <= endIndex; i++) {
const account: AddressObj = this.deriveAddress(i);
addressList.push(account);
}
return addressList;
}
}
export default Wallet;