2020-07-14 00:46:05 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import "errors"
|
|
|
|
|
2020-07-22 17:14:32 +00:00
|
|
|
// KeyType provides a wrapper for a generic public key type identifier (could be an onion address, a zcash address etc.)
|
|
|
|
type KeyType string
|
|
|
|
|
2020-07-14 00:46:05 +00:00
|
|
|
const (
|
2020-07-22 17:14:32 +00:00
|
|
|
// KeyTypeServerOnion - a cwtch address
|
|
|
|
KeyTypeServerOnion = KeyType("bulletin_board_onion") // bulletin board
|
2020-07-14 00:46:05 +00:00
|
|
|
|
|
|
|
// KeyTypeTokenOnion - a cwtch peer with a PoW based token protocol
|
2020-07-22 17:14:32 +00:00
|
|
|
KeyTypeTokenOnion = KeyType("token_service_onion")
|
2020-07-14 00:46:05 +00:00
|
|
|
|
|
|
|
//KeyTypePrivacyPass - a privacy pass based token server
|
2020-07-22 17:14:32 +00:00
|
|
|
KeyTypePrivacyPass = KeyType("privacy_pass_public_key")
|
2020-07-14 00:46:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Key provides a wrapper for a generic public key identifier (could be an onion address, a zcash address etc.)
|
|
|
|
type Key string
|
|
|
|
|
|
|
|
// KeyBundle manages a collection of related keys for various different services.
|
|
|
|
type KeyBundle struct {
|
2020-07-22 17:14:32 +00:00
|
|
|
Keys map[KeyType]Key
|
2020-07-14 00:46:05 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 17:14:32 +00:00
|
|
|
// HasKeyType returns true if the bundle has a public key of a given type.
|
|
|
|
func (kb *KeyBundle) HasKeyType(keytype KeyType) bool {
|
|
|
|
_, exists := kb.Keys[keytype]
|
2020-07-14 00:46:05 +00:00
|
|
|
return exists
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetKey retrieves a key with a given type from the bundle
|
2020-07-22 17:14:32 +00:00
|
|
|
func (kb *KeyBundle) GetKey(keytype KeyType) (Key, error) {
|
|
|
|
key, exists := kb.Keys[keytype]
|
2020-07-14 00:46:05 +00:00
|
|
|
if exists {
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
return "", errors.New("no such key")
|
|
|
|
}
|
|
|
|
|
|
|
|
// AttributeBundle returns a map that can be used as part of a peer attribute bundle
|
|
|
|
func (kb *KeyBundle) AttributeBundle() map[string]string {
|
|
|
|
ab := make(map[string]string)
|
|
|
|
for k, v := range kb.Keys {
|
2020-07-22 17:14:32 +00:00
|
|
|
ab[string(k)] = string(v)
|
2020-07-14 00:46:05 +00:00
|
|
|
}
|
|
|
|
return ab
|
|
|
|
}
|