-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
web3_base_wallet.ts
120 lines (108 loc) · 3.4 KB
/
web3_base_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
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
import { Transaction } from './eth_types.js';
import { HexString } from './primitives_types.js';
export type Cipher = 'aes-128-ctr' | 'aes-128-cbc' | 'aes-256-cbc';
export type CipherOptions = {
salt?: Uint8Array | string;
iv?: Uint8Array | string;
kdf?: 'scrypt' | 'pbkdf2';
dklen?: number;
c?: number; // iterrations
n?: number; // cpu/memory cost
r?: number; // block size
p?: number; // parallelization cost
};
export type ScryptParams = {
dklen: number;
n: number;
p: number;
r: number;
salt: Uint8Array | string;
};
export type PBKDF2SHA256Params = {
c: number; // iterations
dklen: number;
prf: 'hmac-sha256';
salt: Uint8Array | string;
};
export type KeyStore = {
crypto: {
cipher: Cipher;
ciphertext: string;
cipherparams: {
iv: string;
};
kdf: 'pbkdf2' | 'scrypt';
kdfparams: ScryptParams | PBKDF2SHA256Params;
mac: HexString;
};
id: string;
version: 3;
address: string;
};
export interface Web3BaseWalletAccount {
[key: string]: unknown;
readonly address: string;
readonly privateKey: string;
readonly signTransaction: (tx: Transaction) => Promise<{
readonly messageHash: HexString;
readonly r: HexString;
readonly s: HexString;
readonly v: HexString;
readonly rawTransaction: HexString;
readonly transactionHash: HexString;
}>;
readonly sign: (data: Record<string, unknown> | string) => {
readonly messageHash: HexString;
readonly r: HexString;
readonly s: HexString;
readonly v: HexString;
readonly message?: string;
readonly signature: HexString;
};
readonly encrypt: (password: string, options?: Record<string, unknown>) => Promise<KeyStore>;
}
export interface Web3AccountProvider<T> {
privateKeyToAccount: (privateKey: string) => T;
create: () => T;
decrypt: (
keystore: KeyStore | string,
password: string,
options?: Record<string, unknown>,
) => Promise<T>;
}
export abstract class Web3BaseWallet<T extends Web3BaseWalletAccount> extends Array<T> {
protected readonly _accountProvider: Web3AccountProvider<T>;
public constructor(accountProvider: Web3AccountProvider<T>) {
super();
this._accountProvider = accountProvider;
}
public abstract create(numberOfAccounts: number): this;
public abstract add(account: T | string): this;
public abstract get(addressOrIndex: string | number): T | undefined;
public abstract remove(addressOrIndex: string | number): boolean;
public abstract clear(): this;
public abstract encrypt(
password: string,
options?: Record<string, unknown>,
): Promise<KeyStore[]>;
public abstract decrypt(
encryptedWallet: KeyStore[],
password: string,
options?: Record<string, unknown>,
): Promise<this>;
public abstract save(password: string, keyName?: string): Promise<boolean | never>;
public abstract load(password: string, keyName?: string): Promise<this | never>;
}