-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathKeyPairService.js
More file actions
64 lines (53 loc) · 2.23 KB
/
KeyPairService.js
File metadata and controls
64 lines (53 loc) · 2.23 KB
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
import {BlockchainsArray, Blockchains} from '../models/Blockchains';
import PluginRepository from '../plugins/PluginRepository'
import AlertMsg from '../models/alerts/AlertMsg'
import * as Actions from '../store/constants';
export default class KeyPairService {
/***
* Tries to make a keypair in place from a private key
* @param keypair
* @returns {Promise.<void>}
*/
static async makePublicKey(keypair){
return new Promise((resolve) => {
setTimeout(() => {
if(keypair.privateKey.length < 50) {
resolve(false);
return false;
}
let publicKey = '';
try{
const plugin = PluginRepository.plugin(keypair.blockchain);
if (plugin && plugin.validPrivateKey(keypair.privateKey)) {
publicKey = plugin.privateToPublic(keypair.privateKey);
keypair.blockchain = blockchain;
}
} catch(e){}
if(publicKey) keypair.publicKey = publicKey;
resolve(true);
},100)
})
}
static async generateKeyPair(keypair){
const plugin = PluginRepository.plugin(keypair.blockchain);
if(!plugin) return false;
plugin.randomPrivateKey().then(privateKey => {
const publicKey = plugin.privateToPublic(privateKey);
if(plugin.validPublicKey(publicKey) && plugin.validPrivateKey(privateKey)){
keypair.publicKey = publicKey;
keypair.privateKey = privateKey;
}
});
return true;
}
static saveKeyPair(keypair, context, callback){
if(!keypair.name.length) return context[Actions.PUSH_ALERT](AlertMsg.BadKeyPairName());
const scatter = context.scatter.clone();
if(scatter.keychain.getKeyPair(keypair))
return context[Actions.PUSH_ALERT](AlertMsg.KeyPairExists());
if(scatter.keychain.getKeyPairByName(keypair.name))
return context[Actions.PUSH_ALERT](AlertMsg.KeyPairExists());
scatter.keychain.keypairs.push(keypair);
context[Actions.UPDATE_STORED_SCATTER](scatter).then(() => callback());
}
}