1 | const { assert } = require('../util');
|
2 | const Account = require('./Account');
|
3 | const PrivateKeyAccount = require('./PrivateKeyAccount');
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | class Wallet extends Map {
|
9 | |
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | has(key) {
|
16 | return super.has(key);
|
17 | }
|
18 |
|
19 | |
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | delete(key) {
|
26 | return super.delete(key);
|
27 | }
|
28 |
|
29 | |
30 |
|
31 |
|
32 | clear() {
|
33 | return super.clear();
|
34 | }
|
35 |
|
36 | |
37 |
|
38 |
|
39 |
|
40 |
|
41 | set(key, account) {
|
42 | assert(!this.has(key), `Wallet already has account "${key}"`);
|
43 | assert(account instanceof Account, `value not instance of Account, got ${account}`);
|
44 | return super.set(key, account);
|
45 | }
|
46 |
|
47 | |
48 |
|
49 |
|
50 |
|
51 | get(key) {
|
52 | const account = super.get(key);
|
53 | assert(account instanceof Account, `can not found Account by "${key}"`);
|
54 | return account;
|
55 | }
|
56 |
|
57 | |
58 |
|
59 |
|
60 |
|
61 | addPrivateKey(privateKey) {
|
62 | const account = new PrivateKeyAccount(privateKey);
|
63 | this.set(account.address, account);
|
64 | return account;
|
65 | }
|
66 |
|
67 | |
68 |
|
69 |
|
70 |
|
71 | addRandom(entropy) {
|
72 | const account = PrivateKeyAccount.random(entropy);
|
73 | this.set(account.address, account);
|
74 | return account;
|
75 | }
|
76 |
|
77 | |
78 |
|
79 |
|
80 |
|
81 |
|
82 | addKeystore(keystore, password) {
|
83 | const account = PrivateKeyAccount.decrypt(keystore, password);
|
84 | this.set(account.address, account);
|
85 | return account;
|
86 | }
|
87 | }
|
88 |
|
89 | module.exports = Wallet;
|