This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
wallet.factory.ts
230 lines (180 loc) · 7.79 KB
/
wallet.factory.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
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
220
221
222
223
224
225
226
227
228
229
230
/* istanbul ignore file */
import { Coins, Exceptions } from "@arkecosystem/platform-sdk";
import { BIP39 } from "@arkecosystem/platform-sdk-crypto";
import { decrypt, encrypt } from "bip38";
import { v4 as uuidv4 } from "uuid";
import { decode } from "wif";
import {
IAddressOptions,
IAddressWithDerivationPathOptions,
IGenerateOptions,
IMnemonicOptions,
IPrivateKeyOptions,
IProfile,
IPublicKeyOptions,
IReadWriteWallet,
IWalletFactory,
IWifOptions,
IWifWithEncryptionOptions,
WalletData,
WalletImportMethod,
} from "../../../contracts";
import { Wallet } from "./wallet";
export class WalletFactory implements IWalletFactory {
readonly #profile: IProfile;
public constructor(profile: IProfile) {
this.#profile = profile;
}
/** {@inheritDoc IWalletFactory.generate} */
public async generate({
coin,
network,
locale,
}: IGenerateOptions): Promise<{ mnemonic: string; wallet: IReadWriteWallet }> {
const mnemonic: string = BIP39.generate(locale);
return { mnemonic, wallet: await this.fromMnemonicWithBIP39({ mnemonic, coin, network }) };
}
/** {@inheritDoc IWalletFactory.fromMnemonicWithBIP39} */
public async fromMnemonicWithBIP39({ coin, network, mnemonic, password }: IMnemonicOptions): Promise<IReadWriteWallet> {
const wallet: IReadWriteWallet = new Wallet(uuidv4(), {}, this.#profile);
wallet.data().set(WalletData.ImportMethod, WalletImportMethod.MnemonicBIP39);
await wallet.mutator().coin(coin, network);
if (wallet.network().usesExtendedPublicKey()) {
throw new Error("The configured network uses extended public keys with BIP44 for derivation.");
}
if (! this.#allowsDeriveWithBIP39(wallet)) {
throw new Error("The configured network does not support BIP39.");
}
await wallet.mutator().identity(mnemonic);
if (password) {
await this.#encryptWallet(
wallet,
password,
async () => await wallet.coin().identity().wif().fromMnemonic(mnemonic),
);
}
return wallet;
}
/** {@inheritDoc IWalletFactory.fromMnemonicWithBIP44} */
public async fromMnemonicWithBIP44({ coin, network, mnemonic }: IMnemonicOptions): Promise<IReadWriteWallet> {
const wallet: IReadWriteWallet = new Wallet(uuidv4(), {}, this.#profile);
wallet.data().set(WalletData.ImportMethod, WalletImportMethod.MnemonicBIP44);
await wallet.mutator().coin(coin, network);
if (! this.#allowsDeriveWithBIP44(wallet)) {
throw new Error("The configured network does not support BIP44.");
}
const { publicKey } = await wallet
.coin()
.identity()
.publicKey()
// @TODO: the account index should be configurable
.fromMnemonic(mnemonic, { bip44: { account: 0 } });
/**
* @remarks
* Coins like ADA use extended public keys for the wallets derivation
*
* This means that we need to use the EPK internally and let the coin
* implementation handle the derivation of addresses for transactions
* listing and UTXO collection. This is important so that the clients
* don't have to deal with any coin specifics and guarantees that the
* coin is in full control of the wanted behaviours and insurances it
* needs to guarantee the specification conform derivation of wallets
*/
if (wallet.network().usesExtendedPublicKey()) {
await wallet.mutator().extendedPublicKey(publicKey);
} else {
// @TODO: the address index should be configurable
await wallet.mutator().identity(mnemonic, { bip44: { account: 0, addressIndex: 0 } });
}
return wallet;
}
/** {@inheritDoc IWalletFactory.fromMnemonicWithBIP49} */
public async fromMnemonicWithBIP49({ coin, network, mnemonic }: IMnemonicOptions): Promise<IReadWriteWallet> {
throw new Exceptions.NotImplemented(this.constructor.name, this.fromMnemonicWithBIP49.name);
}
/** {@inheritDoc IWalletFactory.fromMnemonicWithBIP84} */
public async fromMnemonicWithBIP84({ coin, network, mnemonic }: IMnemonicOptions): Promise<IReadWriteWallet> {
throw new Exceptions.NotImplemented(this.constructor.name, this.fromMnemonicWithBIP84.name);
}
/** {@inheritDoc IWalletFactory.fromAddress} */
public async fromAddress({ coin, network, address }: IAddressOptions): Promise<IReadWriteWallet> {
const wallet: IReadWriteWallet = new Wallet(uuidv4(), {}, this.#profile);
wallet.data().set(WalletData.ImportMethod, WalletImportMethod.Address);
await wallet.mutator().coin(coin, network);
await wallet.mutator().address({ address });
return wallet;
}
/** {@inheritDoc IWalletFactory.fromPublicKey} */
public async fromPublicKey({ coin, network, publicKey }: IPublicKeyOptions): Promise<IReadWriteWallet> {
const wallet: IReadWriteWallet = new Wallet(uuidv4(), {}, this.#profile);
wallet.data().set(WalletData.ImportMethod, WalletImportMethod.PublicKey);
await wallet.mutator().coin(coin, network);
await wallet.mutator().address(await wallet.coin().identity().address().fromPublicKey(publicKey));
return wallet;
}
/** {@inheritDoc IWalletFactory.fromPrivateKey} */
public async fromPrivateKey({ coin, network, privateKey }: IPrivateKeyOptions): Promise<IReadWriteWallet> {
const wallet: IReadWriteWallet = new Wallet(uuidv4(), {}, this.#profile);
wallet.data().set(WalletData.ImportMethod, WalletImportMethod.PrivateKey);
await wallet.mutator().coin(coin, network);
await wallet.mutator().address(await wallet.coin().identity().address().fromPrivateKey(privateKey));
return wallet;
}
/** {@inheritDoc IWalletFactory.fromAddressWithDerivationPath} */
public async fromAddressWithDerivationPath({
coin,
network,
address,
path,
}: IAddressWithDerivationPathOptions): Promise<IReadWriteWallet> {
const wallet: IReadWriteWallet = await this.fromAddress({ coin, network, address });
wallet.data().set(WalletData.ImportMethod, WalletImportMethod.AddressWithDerivationPath);
wallet.data().set(WalletData.DerivationPath, path);
return wallet;
}
/** {@inheritDoc IWalletFactory.fromWIF} */
public async fromWIF({ coin, network, wif }: IWifOptions): Promise<IReadWriteWallet> {
const wallet: IReadWriteWallet = new Wallet(uuidv4(), {}, this.#profile);
wallet.data().set(WalletData.ImportMethod, WalletImportMethod.WIF);
await wallet.mutator().coin(coin, network);
await wallet.mutator().address(await wallet.coin().identity().address().fromWIF(wif));
return wallet;
}
/** {@inheritDoc IWalletFactory.fromWIFWithEncryption} */
public async fromWIFWithEncryption({
coin,
network,
wif,
password,
}: IWifWithEncryptionOptions): Promise<IReadWriteWallet> {
const wallet: IReadWriteWallet = new Wallet(uuidv4(), {}, this.#profile);
wallet.data().set(WalletData.ImportMethod, WalletImportMethod.WIFWithEncryption);
await wallet.mutator().coin(coin, network);
const { compressed, privateKey } = decrypt(wif, password);
await wallet
.mutator()
.address(await wallet.coin().identity().address().fromPrivateKey(privateKey.toString("hex")));
wallet.data().set(WalletData.Bip38EncryptedKey, encrypt(privateKey, compressed, password));
return wallet;
}
#allowsDeriveWithBIP39(wallet: IReadWriteWallet): boolean {
return wallet.gate().allows(Coins.FeatureFlag.IdentityAddressMnemonicBip39);
}
#allowsDeriveWithBIP44(wallet: IReadWriteWallet): boolean {
return wallet.gate().allows(Coins.FeatureFlag.IdentityAddressMnemonicBip44);
}
/* istanbul ignore next */
#allowsDeriveWithBIP49(wallet: IReadWriteWallet): boolean {
/* istanbul ignore next */
return wallet.gate().allows(Coins.FeatureFlag.IdentityAddressMnemonicBip49);
}
/* istanbul ignore next */
#allowsDeriveWithBIP84(wallet: IReadWriteWallet): boolean {
/* istanbul ignore next */
return wallet.gate().allows(Coins.FeatureFlag.IdentityAddressMnemonicBip84);
}
async #encryptWallet(wallet: IReadWriteWallet, password: string, derive: Function): Promise<void> {
const { compressed, privateKey } = decode((await derive()).wif);
wallet.data().set(WalletData.Bip38EncryptedKey, encrypt(privateKey, compressed, password));
}
}