auth

package
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

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.

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. 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.

Index

Constants

View Source
const (
	// EthPersonalSignAuth is the Ethereum "personal sign" authentication type,
	// which uses the secp256k1 signature scheme with a prefixed message and the
	// legacy 256-bit Keccak hash function to mimic most Ethereum wallets. This
	// is intended as the authenticator for the SDK-provided EthPersonalSigner,
	// and must be registered with that name.
	EthPersonalSignAuth = "secp256k1_ep"

	EthAddressIdentLength = 20
)
View Source
const (
	// Ed25519Auth is a plain ed25519 authenticator. This is intended as the authenticator for the
	// SDK-provided Ed25519Signer, and must be registered with that name.
	Ed25519Auth = "ed25519"
)
View Source
const (
	Secp256k1Auth = "secp256k1"
)

Variables

This section is empty.

Functions

func GetNodeIdentifier added in v0.4.0

func GetNodeIdentifier(pub crypto.PublicKey) (string, error)

func GetUserIdentifier added in v0.4.0

func GetUserIdentifier(pub crypto.PublicKey) (string, error)

Types

type Authenticator

type Authenticator interface {
	// Verify verifies whether a signature is valid for a given message and
	// "sender", which is the compactID from a Signer. It is meant to be used
	// with asymmetric signature algorithms such as ECDSA, Ed25519 RSA, etc. If
	// the signature is invalid, the method should return an error. If the
	// signature is valid, the method should return nil.
	Verify(compactID, msg, signature []byte) error

	// Identifier returns a string identifier for a given sender.
	// This string identifier is used to identify the sender when
	// interacting with the Kuneiform engine, and will be used as
	// the `@caller` variable in the engine.
	Identifier(compactID []byte) (string, error)

	// KeyType returns the type of key used by this Authenticator. This is
	// different from the AuthType of a Signer, although an AuthType will have a
	// corresponding key type (but not the other way around). For example, both
	// the EthSecp256k1Authenticator and Secp25k1Authenticator Authenticators
	// correspond to crypto.KeyTypeSecp256k1. This information is important to
	// determine the key type of a transaction Sender from the AuthType in the
	// signature.
	KeyType() crypto.KeyType
}

Authenticator is an interface for verifying signatures and deriving a string identifier from the sender bytes. Custom asymmetric signature algorithms may be implemented by developers by implementing this interface.

type Ed25519Authenticator

type Ed25519Authenticator struct{}

Ed25519Authenticator is an authenticator for ed25519 keys.

func (Ed25519Authenticator) Identifier

func (e Ed25519Authenticator) Identifier(publicKey []byte) (string, error)

Address simply returns the hexadecimal encoded public key as the address.

func (Ed25519Authenticator) KeyType added in v0.4.0

func (e Ed25519Authenticator) KeyType() crypto.KeyType

func (Ed25519Authenticator) Verify

func (e Ed25519Authenticator) Verify(publicKey []byte, msg []byte, signature []byte) error

Verify verifies the signature against the given user identifier and data. The identifier must be the ed25519 public key bytes.

type Ed25519Signer

type Ed25519Signer struct {
	crypto.Ed25519PrivateKey
}

Ed25519Signer is a signer that signs messages using the ed25519 curve, using the standard signature scheme.

func (*Ed25519Signer) AuthType

func (e *Ed25519Signer) AuthType() string

func (*Ed25519Signer) CompactID added in v0.4.0

func (e *Ed25519Signer) CompactID() []byte

CompactID returns the identity of the signer (public key for this signer).

func (*Ed25519Signer) PubKey added in v0.4.0

func (e *Ed25519Signer) PubKey() crypto.PublicKey

PubKey returns the public key of the signer.

func (*Ed25519Signer) Sign

func (e *Ed25519Signer) Sign(msg []byte) (*Signature, error)

Sign signs the given message(not hashed) according to standard signature scheme. It does not apply any special digests to the message.

type EthPersonalSigner

type EthPersonalSigner struct {
	Key crypto.Secp256k1PrivateKey
}

EthPersonalSecp256k1Signer is a signer that signs messages using the secp256k1 curve, using ethereum's personal_sign signature scheme.

func (*EthPersonalSigner) AuthType

func (e *EthPersonalSigner) AuthType() string

func (*EthPersonalSigner) CompactID added in v0.4.0

func (e *EthPersonalSigner) CompactID() []byte

CompactID returns the identity of the signer (ETH address for this signer).

func (*EthPersonalSigner) PubKey added in v0.4.0

func (e *EthPersonalSigner) PubKey() crypto.PublicKey

PubKey returns the public key of the signer.

func (*EthPersonalSigner) Sign

func (e *EthPersonalSigner) Sign(msg []byte) (*Signature, error)

Sign sign given message according to EIP-191 personal_sign. EIP-191 personal_sign prefix the message with "\x19Ethereum Signed Message:\n" and the message length, then hash the message with 'legacy' keccak256. The signature is in [R || S || V] format, 65 bytes. This method is used to sign an arbitrary message in the same manner in which a wallet like MetaMask would sign a text message. The message is defined by the object that is being serialized e.g. a Kwil Transaction.

type EthSecp256k1Authenticator

type EthSecp256k1Authenticator struct{}

EthSecp256k1Authenticator is the authenticator for the Ethereum "personal sign" signature type, which is the default signer for Kwil. As such, it is a default authenticator.

func (EthSecp256k1Authenticator) Identifier

func (EthSecp256k1Authenticator) Identifier(ident []byte) (string, error)

Identifier returns an ethereum address hex string from address bytes. It will include the 0x prefix, and the address will be checksum-able.

func (EthSecp256k1Authenticator) KeyType added in v0.4.0

func (EthSecp256k1Authenticator) Verify

func (EthSecp256k1Authenticator) Verify(identity []byte, msg []byte, signature []byte) error

Verify verifies applies the Ethereum TextHash digest and verifies the signature

type Secp25k1Authenticator added in v0.4.0

type Secp25k1Authenticator struct{}

func (Secp25k1Authenticator) Identifier added in v0.4.0

func (e Secp25k1Authenticator) Identifier(publicKey []byte) (string, error)

Identifier simply returns the hexadecimal encoded compressed public key.

func (Secp25k1Authenticator) KeyType added in v0.4.0

func (e Secp25k1Authenticator) KeyType() crypto.KeyType

func (Secp25k1Authenticator) Verify added in v0.4.0

func (e Secp25k1Authenticator) Verify(publicKey, msg, signature []byte) error

Verify verifies the signature against the given identifier (pubkey bytes) and data. The identifier must be the secp256k1 public key bytes.

type Secp256k1Signer added in v0.4.0

type Secp256k1Signer struct {
	crypto.Secp256k1PrivateKey
}

func (*Secp256k1Signer) AuthType added in v0.4.0

func (*Secp256k1Signer) AuthType() string

func (*Secp256k1Signer) CompactID added in v0.4.0

func (s *Secp256k1Signer) CompactID() []byte

CompactID returns the identity of the signer (public key for this signer). This is used with the Verify method of the authenticator. To get a text identifier, use the Authenticator's Identifier method.

func (*Secp256k1Signer) PubKey added in v0.4.0

func (s *Secp256k1Signer) PubKey() crypto.PublicKey

func (*Secp256k1Signer) Sign added in v0.4.0

func (s *Secp256k1Signer) Sign(msg []byte) (*Signature, error)

type Signature

type Signature struct {
	// Data is the raw signature bytes
	Data []byte `json:"sig"`
	// Type is the signature type, which must have a registered Authenticator of
	// the same name for the Verify method to be usable.
	Type string `json:"type"`
}

Signature is a signature with a designated AuthType, which should be used to determine how to verify the signature.

func (Signature) Bytes added in v0.4.0

func (s Signature) Bytes() []byte

func (Signature) MarshalBinary added in v0.4.0

func (s Signature) MarshalBinary() ([]byte, error)

func (*Signature) ReadFrom added in v0.4.0

func (s *Signature) ReadFrom(r io.Reader) (int64, error)

func (Signature) SerializeSize added in v0.4.0

func (s Signature) SerializeSize() int64

func (*Signature) UnmarshalBinary added in v0.4.0

func (s *Signature) UnmarshalBinary(data []byte) error

func (Signature) WriteTo added in v0.4.0

func (s Signature) WriteTo(w io.Writer) (int64, error)

type Signer

type Signer interface {
	// Sign signs a message and returns the signature
	Sign(msg []byte) (*Signature, error)

	// CompactID returns the signer's compact identifier, which is typically and
	// address or a public key. This value is recognized by the Verify method of
	// the corresponding Authenticator for the types of signatures generated by
	// this Signer. This is used to set the Sender field of a Transaction.
	// If you need the public key, use the PubKey method.
	CompactID() []byte

	// PubKey returns the public key associated with this signer. This may or
	// may not correspond directly to the Identity, which may be an address. The
	// purpose of Identity is to be used with the Verify method of the
	// Authenticator for the AuthType of this Signer, which may not take the
	// bytes of this pubkey.
	PubKey() crypto.PublicKey

	// AuthType is the type of Authenticator that should be used to verify the
	// signature created by this Signer; also to get the string identifier from
	// the Identity. This is used to set the AuthType field of a Transaction.
	AuthType() string
}

Signer is an interface for something that can sign messages. It returns signatures with a designated AuthType, which should be used to determine how to verify the signature.

func GetNodeSigner added in v0.4.0

func GetNodeSigner(key crypto.PrivateKey) Signer

func GetUserSigner added in v0.4.0

func GetUserSigner(key crypto.PrivateKey) Signer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL