-
Notifications
You must be signed in to change notification settings - Fork 1
/
blox-keypair.js
88 lines (84 loc) · 1.99 KB
/
blox-keypair.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
import {html, PolymerElement} from '@polymer/polymer/polymer-element.js';
import './ecc.js';
/**
* `blox-keypair`
* Makes 2 EOS key-pairs
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class BloxKeypair extends PolymerElement {
static get template() {
return html`
<style>
:host {
display: block;
}
</style>
<template is="dom-if" if="{{debug}}">
{{keypairOne}} </br></br>
{{keypairTwo}}
</template>
`;
}
static get properties() {
return {
generate: {
type: Number,
observer: 'generate',
},
debug: {
type: Boolean,
value: false,
observer: 'generate',
},
keypairs: {
type: String,
notify: true,
reflectToAttribute: true,
},
};
}
generate(){
return new Promise((resolve, reject) => {
let keypair = [];
this._keypair()
.then((keypair1) => {
keypair.push(keypair1)
return this._keypair()
})
.then((keypair2) => {
keypair.push(keypair2)
this.keypairs = JSON.stringify(keypair);
this.keypairOne = JSON.stringify(keypair[0]);
this.keypairTwo = JSON.stringify(keypair[1]);
resolve(keypair)
})
.catch((err) => {
throw err
})
})
}
_keypair() {
return new Promise((resolve, reject) => {
const ecc = eosjs_ecc;
ecc.randomKey()
.then((privateKey) => {
if (!privateKey) {
reject(Error('No private key returned'));
} else {
let publicKey = ecc.privateToPublic(privateKey);
if (ecc.isValidPublic(publicKey) && ecc.isValidPrivate(privateKey)) {
resolve({publicKey: publicKey, privateKey: privateKey});
} else {
reject(Error('Invalid key returned'));
}
}
})
.catch((error) => {
reject(Error(error));
});
});
}
} window.customElements.define('blox-keypair', BloxKeypair);