-
Notifications
You must be signed in to change notification settings - Fork 4
/
KMMEdPublicKey.kt
54 lines (47 loc) · 1.67 KB
/
KMMEdPublicKey.kt
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
package org.hyperledger.identus.apollo.utils
import node.buffer.Buffer
import node.buffer.BufferEncoding
import org.hyperledger.identus.apollo.base64.base64UrlEncoded
import org.hyperledger.identus.apollo.utils.external.eddsa
/**
* Represents a public key in the KMMEd cryptographic system.
*
* @property raw The raw value of the public key.
* @property keyPair The key pair object associated with the public key.
* @constructor Creates a KMMEdPublicKey object.
* @param bytes The byte array representing the public key.
*/
@OptIn(ExperimentalJsExport::class)
@JsExport
actual class KMMEdPublicKey(bytes: ByteArray) {
val raw: Buffer
private val keyPair: eddsa.KeyPair
init {
val ed25519 = eddsa("ed25519")
raw = Curve25519Parser.parseRaw(bytes)
val pub = raw.toString(BufferEncoding.hex)
// TODO: Report a bug in elliptic, this method is not expecting a Buffer (bytes)
// Internally it expects to find an array, if not Buffer.slice.concat fails when Array.slice.concat doesn't
// Must keep this...
keyPair = ed25519.keyFromPublic(pub)
}
/**
* Base64 url encodes the raw value
* @return Buffer
*/
fun getEncoded(): Buffer {
return Buffer.from(raw.toByteArray().base64UrlEncoded.encodeToByteArray())
}
/**
* Confirm a message signature was signed with the corresponding PrivateKey
* @param message - the message that was signed
* @param sig - signature
* @return Boolean
*/
actual fun verify(message: ByteArray, sig: ByteArray): Boolean {
return keyPair.verify(
Buffer.from(message),
sig.toHexString()
)
}
}