-
Notifications
You must be signed in to change notification settings - Fork 21
/
WalletManager.js
136 lines (120 loc) · 3.9 KB
/
WalletManager.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
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
'use strict'
const LWSClient = require('@mymonero/mymonero-lws-client')
const Wallet = require('./Wallet')
const ContactManager = require('./ContactManager')
class WalletManager {
constructor (netType = 'MAINNET', url = 'https://api.mymonero.com') {
this.netType = netType
this.url = url
this.bridgeClass = {}
this.wallets = []
this.contactManager = new ContactManager()
this.appName = 'MyMonero'
this.appVersion = '1.1.24'
this.apiClient = new LWSClient({ url: url })
}
/**
* Loads the WASM and Initilizes the WASM Bridge.
*/
async init () {
const self = this
try {
self.bridgeClass = await require('@mymonero/mymonero-monero-client')({})
} catch (e) {
console.error(e)
}
}
/**
* Opens Wallet and adds it to the wallet array.
* @param {array} options
* @returns {Wallet}
*/
openWallet (options) {
const self = this
const wallet = new Wallet(self.bridgeClass, self.apiClient, self.contactManager, options)
self.wallets.push(wallet)
return wallet
}
/**
* Creates a new wallet by generating a new mnemonic phrase.
* @param {string} name - The Wallet name.
* @returns {Wallet}
*/
createWallet (name) {
const self = this
const walletDetails = self.bridgeClass.generateWallet('en-US', self.netType)
const options = {
name: name,
netType: self.netType,
url: self.url,
appName: self.appName,
appVersion: self.appVersion,
mnemonic: walletDetails.mnemonic,
mnemonicLanguage: walletDetails.mnemonicLanguage,
seed: walletDetails.seed,
address: walletDetails.address,
publicViewKey: walletDetails.publicViewKey,
privateViewKey: walletDetails.privateViewKey,
publicSpendKey: walletDetails.publicSpendKey,
privateSpendKey: walletDetails.privateSpendKey
}
const wallet = new Wallet(self.bridgeClass, self.apiClient, self.contactManager, options)
self.wallets.push(wallet)
return wallet
}
/**
* Imports wallet from known mnemonic phrase.
* @param {string} name - Name given to the wallet.
* @param {string} mnemonic - The mnemonic phrase.
* @returns {Wallet}
*/
importWallet (name, mnemonic) {
const self = this
const keyStore = this.bridgeClass.seedAndKeysFromMnemonic(mnemonic, self.netType)
const options = {
name: name,
netType: self.netType,
url: self.url,
appName: self.appName,
appVersion: self.appVersion,
mnemonic: mnemonic,
seed: keyStore.seed,
address: keyStore.address,
publicViewKey: keyStore.publicViewKey,
privateViewKey: keyStore.privateViewKey,
publicSpendKey: keyStore.publicSpendKey,
privateSpendKey: keyStore.privateSpendKey
}
const wallet = new Wallet(self.bridgeClass, self.apiClient, self.contactManager, options)
self.wallets.push(wallet)
return wallet
}
/**
* Imports wallet from known private keys.
* @param {string} name - Name giben to the wallet.
* @param {string} address - The Wallets primary address.
* @param {string} privateViewKey - The Wallets private view key.
* @param {string} privateSpendKey - The Wallets private spend key.
* @returns {Wallet}
*/
importWalletKeys (name, address, privateViewKey, privateSpendKey) {
const self = this
const result = this.bridgeClass.isValidKeys(address, privateViewKey, privateSpendKey, '', self.netType)
const options = {
name: name,
netType: self.netType,
url: self.url,
appName: self.appName,
appVersion: self.appVersion,
address: address,
publicViewKey: result.publicViewKey,
privateViewKey: privateViewKey,
publicSpendKey: result.publicSpendKey,
privateSpendKey: privateSpendKey
}
const wallet = new Wallet(self.bridgeClass, self.apiClient, self.contactManager, options)
self.wallets.push(wallet)
return wallet
}
}
module.exports = WalletManager