-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/ident, and no Verify automation (#352)
* internal/ident, and no Verify automation * just remove the pointless tx/msg Verify methods
- Loading branch information
1 parent
e437714
commit 33b9871
Showing
21 changed files
with
243 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,39 @@ | ||
/* | ||
Package auth provides an Authenticator interface for developers to implement | ||
their own Kwil authentication drivers. Authenticator extensions may be used to | ||
expand the type of signatures that may be verified on transactions and messages | ||
via the (*Signature).Verify method, where the Signature.Type field corresponds | ||
to the unique Authenticator name. It also provides the ability to derive an | ||
address from a public key for a certain network. | ||
Package auth provides the standard signing and verification methods used in | ||
Kwil. These are Ethereum "personal sign" used by wallets to sign a customized | ||
readable message, and plain Ed25519 signing used for validator node signatures. | ||
Similar to Go's database/sql package, developers can implement the Authenticator | ||
interface and register it with the RegisterAuthenticator function. The name used | ||
to register is to be set as the signature type creating signatures. | ||
It also defines an Authenticator interface for developers to implement their own | ||
Kwil authentication drivers. See the extensions/auth package in the kwil-db main | ||
module. Authenticator extensions may be used to expand the type of signatures | ||
that may be verified on transactions and messages. It also provides the ability | ||
to derive an address from a public key for a certain network. | ||
There are presently two Signers defined in the Kwil Go SDK with pre-registered | ||
Authenticators with the same type: EthPersonalSigType and Ed25519SigType. When | ||
registering a new Authenticator, the values of these may not be used. | ||
registering a new Authenticator, the values of these may not be used. This is | ||
the primary reason that the Authenticator interface is defined in this package | ||
instead of the kwil-db main module under extensions/auth. We may consider moving | ||
these two Authenticator implementations out of the SDK and into the main module | ||
where they are only available to the application that needs them, but it may be | ||
awkward to have complementary verification defined in the same place as the | ||
signing. | ||
*/ | ||
package auth | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Authenticator is an interface for authenticating an incoming call | ||
// It is made to work with keypair authentication | ||
type Authenticator interface { | ||
// Verifier is satisfied by types that can verify a signature against a public | ||
// key and message. A Verifier implementation will generally pertain to a | ||
// certain message serialization scheme and key type. | ||
type Verifier interface { | ||
// Verify verifies the signature against the given public key and data. | ||
Verify(sender, msg, signature []byte) error | ||
|
||
// Address returns an address from a public key | ||
Address(sender []byte) (string, error) | ||
} | ||
|
||
var registeredAuthenticators = make(map[string]Authenticator) | ||
|
||
// RegisterAuthenticator registers an authenticator with a given name | ||
func RegisterAuthenticator(name string, auth Authenticator) error { | ||
name = strings.ToLower(name) | ||
if _, ok := registeredAuthenticators[name]; ok { | ||
return fmt.Errorf("%w: %s", ErrAuthenticatorExists, name) | ||
} | ||
|
||
registeredAuthenticators[name] = auth | ||
return nil | ||
} | ||
|
||
// getAuthenticator returns an authenticator by the name it was registered with | ||
func getAuthenticator(name string) (Authenticator, error) { | ||
name = strings.ToLower(name) | ||
auth, ok := registeredAuthenticators[name] | ||
if !ok { | ||
return nil, fmt.Errorf("%w: %s", ErrAuthenticatorNotFound, name) | ||
} | ||
|
||
return auth, nil | ||
} | ||
|
||
// GetAddress returns an address from a public key and authenticator type | ||
func GetAddress(authType string, sender []byte) (string, error) { | ||
auth, err := getAuthenticator(authType) | ||
if err != nil { | ||
return "", err | ||
} | ||
// Authenticator is an interface for authenticating a message and deriving an | ||
// encoded address for a public key. | ||
type Authenticator interface { | ||
Verifier | ||
|
||
return auth.Address(sender) | ||
// Address returns an address from a public key | ||
Address(sender []byte) (string, error) | ||
} | ||
|
||
var ( | ||
// ErrAuthenticatorExists is returned when an authenticator is already registered | ||
ErrAuthenticatorExists = errors.New("authenticator already exists") | ||
// ErrAuthenticatorNotFound is returned when an authenticator is not found | ||
ErrAuthenticatorNotFound = errors.New("authenticator not found") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.