-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (31 loc) · 1.15 KB
/
index.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
const crypto = require('crypto')
const secp256k1 = require('secp256k1/elliptic')
const Amorph = require('amorph')
const arguguard = require('arguguard')
const amorphBufferPlugin = require('amorph-buffer')
Amorph.loadPlugin(amorphBufferPlugin)
Amorph.ready()
let privateKeyBuffer = crypto.randomBytes(32)
while (!secp256k1.privateKeyVerify(privateKeyBuffer)) {
privateKeyBuffer = crypto.randomBytes(32)
}
function EccKeypair(privateKey) {
arguguard('EccKeypair', ['Amorph'], arguments)
this.privateKey = privateKey
this.publicKey = privateKey.as('buffer', (privateKeyBuffer) => {
return secp256k1.publicKeyCreate(privateKeyBuffer, false)
})
this.publicKeyCompressed = privateKey.as('buffer', (privateKeyBuffer) => {
return secp256k1.publicKeyCreate(privateKeyBuffer, true)
})
}
EccKeypair.generate = function generate() {
arguguard('EccKeypair.generate', [], arguments)
let privateKeyBuffer = crypto.randomBytes(32)
while (!secp256k1.privateKeyVerify(privateKeyBuffer)) {
privateKeyBuffer = crypto.randomBytes(32)
}
const privateKey = new Amorph(privateKeyBuffer, 'buffer')
return new EccKeypair(privateKey)
}
module.exports = EccKeypair