forked from sergeych/crypto2
+Multikey.AnyKey
This commit is contained in:
1 parent
1191de284e
commit
4cbc17334c
2 files changed
+58
-32
No files matched your search
@@ -3,6 +3,7 @@ package net.sergeych.crypto2
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import net.sergeych.bipack.Unsigned
|
||||
import net.sergeych.crypto2.Multikey.AnyKey
|
||||
import net.sergeych.crypto2.Multikey.Companion.allOf
|
||||
import net.sergeych.crypto2.Multikey.Companion.allOfMultikeys
|
||||
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
|
||||
* - [anyOfMultikeys], [anyOf], [allOf], and [allOfMultikeys]
|
||||
* - [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
|
||||
* it compatible with any combination.
|
||||
@@ -53,17 +55,17 @@ sealed class Multikey {
|
||||
*/
|
||||
fun check(vararg verifyingKeys: VerifyingPublicKey): Boolean = check(verifyingKeys.asIterable())
|
||||
|
||||
infix fun or(mk: Multikey): Multikey = SomeOf(1, listOf(this,mk))
|
||||
infix fun or(mk: Multikey): Multikey = SomeOf(1, listOf(this, mk))
|
||||
|
||||
infix fun or(k: VerifyingPublicKey) = SomeOf( 1, listOf(this, Multikey(k)))
|
||||
infix fun or(k: VerifyingPublicKey) = SomeOf(1, listOf(this, Multikey(k)))
|
||||
|
||||
infix fun or(k: SigningSecretKey) = SomeOf( 1, listOf(this, Multikey(k.verifyingKey)))
|
||||
infix fun or(k: SigningSecretKey) = SomeOf(1, listOf(this, Multikey(k.verifyingKey)))
|
||||
|
||||
infix fun and(mk: Multikey): Multikey = SomeOf(2, listOf(this,mk))
|
||||
infix fun and(mk: Multikey): Multikey = SomeOf(2, listOf(this, mk))
|
||||
|
||||
infix fun and(k: VerifyingPublicKey) = SomeOf( 2, listOf(this, Multikey(k)))
|
||||
infix fun and(k: VerifyingPublicKey) = SomeOf(2, listOf(this, Multikey(k)))
|
||||
|
||||
infix fun and(k: SigningSecretKey) = SomeOf( 2, listOf(this, Multikey(k.verifyingKey)))
|
||||
infix fun and(k: SigningSecretKey) = SomeOf(2, listOf(this, Multikey(k.verifyingKey)))
|
||||
|
||||
/**
|
||||
* Multikey instance implementing `m of N` logic against [VerifyingPublicKey] set. Do not use
|
||||
@@ -74,13 +76,13 @@ sealed class Multikey {
|
||||
class Keys internal constructor(
|
||||
@Unsigned
|
||||
val requiredMinimum: Int,
|
||||
val validKeys: Set<VerifyingPublicKey>
|
||||
val validKeys: Set<VerifyingPublicKey>,
|
||||
) : Multikey() {
|
||||
override fun check(keys: Iterable<VerifyingPublicKey>): Boolean {
|
||||
var matches = 0
|
||||
for( signer in keys ) {
|
||||
if( signer in validKeys) {
|
||||
if( ++matches >= requiredMinimum ) return true
|
||||
for (signer in keys) {
|
||||
if (signer in validKeys) {
|
||||
if (++matches >= requiredMinimum) return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
@@ -96,24 +98,34 @@ sealed class Multikey {
|
||||
class SomeOf internal constructor(
|
||||
@Unsigned
|
||||
val requiredMinimum: Int,
|
||||
val validKeys: List<Multikey>
|
||||
val validKeys: List<Multikey>,
|
||||
) : Multikey() {
|
||||
override fun check(keys: Iterable<VerifyingPublicKey>): Boolean {
|
||||
var matches = 0
|
||||
for( k in validKeys ) {
|
||||
if( k.check(keys) ) {
|
||||
if( ++matches >= requiredMinimum ) return true
|
||||
for (k in validKeys) {
|
||||
if (k.check(keys)) {
|
||||
if (++matches >= requiredMinimum) return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
|
||||
operator fun invoke(k: SigningSecretKey): Multikey = Keys(1, setOf( k.verifyingKey))
|
||||
operator fun invoke(k: VerifyingPublicKey): Multikey = Keys(1, setOf( k))
|
||||
operator fun invoke(k: SigningSecretKey): Multikey = Keys(1, setOf(k.verifyingKey))
|
||||
operator fun invoke(k: VerifyingPublicKey): Multikey = Keys(1, setOf(k))
|
||||
|
||||
/**
|
||||
* Create a multikey instance that requires some keys from a list
|
||||
@@ -142,7 +154,7 @@ sealed class Multikey {
|
||||
/**
|
||||
* Create a multikey instance that requires any key from a list
|
||||
*/
|
||||
fun anyOf(vararg keys: VerifyingPublicKey): Multikey = someOf(1, *keys)
|
||||
fun anyOf(vararg keys: VerifyingPublicKey): Multikey = someOf(1, *keys)
|
||||
|
||||
/**
|
||||
* Create a multikey instance that requires any key from a list
|
||||
@@ -163,7 +175,7 @@ sealed class Multikey {
|
||||
/**
|
||||
* Create a multikey instance that requires all keys from a list
|
||||
*/
|
||||
fun allOf(vararg keys: VerifyingPublicKey): Multikey = someOf(keys.size, *keys)
|
||||
fun allOf(vararg keys: VerifyingPublicKey): Multikey = someOf(keys.size, *keys)
|
||||
|
||||
/**
|
||||
* Create a multikey instance that requires all keys from a list
|
||||
@@ -181,7 +193,5 @@ sealed class Multikey {
|
||||
fun allOf(keys: List<VerifyingPublicKey>): Multikey = someOf(keys.size, keys)
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user