forked from sergeych/crypto2
+Multikey.AnyKey
This commit is contained in:
parent
1191de284e
commit
4cbc17334c
@ -3,6 +3,7 @@ package net.sergeych.crypto2
|
|||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
import net.sergeych.bipack.Unsigned
|
import net.sergeych.bipack.Unsigned
|
||||||
|
import net.sergeych.crypto2.Multikey.AnyKey
|
||||||
import net.sergeych.crypto2.Multikey.Companion.allOf
|
import net.sergeych.crypto2.Multikey.Companion.allOf
|
||||||
import net.sergeych.crypto2.Multikey.Companion.allOfMultikeys
|
import net.sergeych.crypto2.Multikey.Companion.allOfMultikeys
|
||||||
import net.sergeych.crypto2.Multikey.Companion.anyOf
|
import net.sergeych.crypto2.Multikey.Companion.anyOf
|
||||||
@ -36,6 +37,7 @@ import net.sergeych.crypto2.Multikey.Companion.someOfMultikeys
|
|||||||
* - [someOfMultikeys], [someOf] family for `n of M` logic
|
* - [someOfMultikeys], [someOf] family for `n of M` logic
|
||||||
* - [anyOfMultikeys], [anyOf], [allOf], and [allOfMultikeys]
|
* - [anyOfMultikeys], [anyOf], [allOf], and [allOfMultikeys]
|
||||||
* - [invoke] for a single-key multikey
|
* - [invoke] for a single-key multikey
|
||||||
|
* - [AnyKey] when you need effectively match any key, useful when you need a `var` `Multikey`.
|
||||||
*
|
*
|
||||||
* __Important__. When serializing, always serialize as root [Multikey] instance to keep
|
* __Important__. When serializing, always serialize as root [Multikey] instance to keep
|
||||||
* it compatible with any combination.
|
* it compatible with any combination.
|
||||||
@ -74,7 +76,7 @@ sealed class Multikey {
|
|||||||
class Keys internal constructor(
|
class Keys internal constructor(
|
||||||
@Unsigned
|
@Unsigned
|
||||||
val requiredMinimum: Int,
|
val requiredMinimum: Int,
|
||||||
val validKeys: Set<VerifyingPublicKey>
|
val validKeys: Set<VerifyingPublicKey>,
|
||||||
) : Multikey() {
|
) : Multikey() {
|
||||||
override fun check(keys: Iterable<VerifyingPublicKey>): Boolean {
|
override fun check(keys: Iterable<VerifyingPublicKey>): Boolean {
|
||||||
var matches = 0
|
var matches = 0
|
||||||
@ -96,7 +98,7 @@ sealed class Multikey {
|
|||||||
class SomeOf internal constructor(
|
class SomeOf internal constructor(
|
||||||
@Unsigned
|
@Unsigned
|
||||||
val requiredMinimum: Int,
|
val requiredMinimum: Int,
|
||||||
val validKeys: List<Multikey>
|
val validKeys: List<Multikey>,
|
||||||
) : Multikey() {
|
) : Multikey() {
|
||||||
override fun check(keys: Iterable<VerifyingPublicKey>): Boolean {
|
override fun check(keys: Iterable<VerifyingPublicKey>): Boolean {
|
||||||
var matches = 0
|
var matches = 0
|
||||||
@ -109,6 +111,16 @@ sealed class Multikey {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Special `AnyKey`: no restrictions, any key will satisfy this. In the rare case to mark
|
||||||
|
* publicly available operations, etc. Please note it is an object, not a class, and can't
|
||||||
|
* be instantiated.
|
||||||
|
*/
|
||||||
|
@Serializable
|
||||||
|
object AnyKey : Multikey() {
|
||||||
|
override fun check(keys: Iterable<VerifyingPublicKey>): Boolean = true
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
@ -181,7 +193,5 @@ sealed class Multikey {
|
|||||||
fun allOf(keys: List<VerifyingPublicKey>): Multikey = someOf(keys.size, keys)
|
fun allOf(keys: List<VerifyingPublicKey>): Multikey = someOf(keys.size, keys)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -278,7 +278,7 @@ class KeysTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun multiKeyTestSom() = runTest {
|
fun multiKeyTestSome() = runTest {
|
||||||
initCrypto()
|
initCrypto()
|
||||||
val k1 = SigningSecretKey.new()
|
val k1 = SigningSecretKey.new()
|
||||||
val k2 = SigningSecretKey.new()
|
val k2 = SigningSecretKey.new()
|
||||||
@ -342,6 +342,22 @@ class KeysTest {
|
|||||||
assertFalse { s3.check(k1.verifyingKey) }
|
assertFalse { s3.check(k1.verifyingKey) }
|
||||||
assertFalse { s3.check(k2.verifyingKey) }
|
assertFalse { s3.check(k2.verifyingKey) }
|
||||||
assertFalse { s3.check(k1.verifyingKey, k4.verifyingKey) }
|
assertFalse { s3.check(k1.verifyingKey, k4.verifyingKey) }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun multiKeyTestAny() = runTest {
|
||||||
|
initCrypto()
|
||||||
|
val k1 = SigningSecretKey.new()
|
||||||
|
val k2 = SigningSecretKey.new()
|
||||||
|
val k3 = SigningSecretKey.new()
|
||||||
|
val k4 = SigningSecretKey.new()
|
||||||
|
val k5 = SigningSecretKey.new()
|
||||||
|
// val k6 = SigningSecretKey.new()
|
||||||
|
val mk: Multikey = Multikey.AnyKey
|
||||||
|
assertTrue { mk.check(k1.verifyingKey) }
|
||||||
|
assertTrue { mk.check(k2.verifyingKey) }
|
||||||
|
assertTrue { mk.check(k3.verifyingKey) }
|
||||||
|
assertTrue { mk.check(k4.verifyingKey) }
|
||||||
|
assertTrue { mk.check(k5.verifyingKey) }
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user