keystore

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package keystore manages the private keys OrbitDB identities sign with.

It mirrors src/key-store.js in @orbitdb/core: keys are secp256k1 keys from go-libp2p's crypto package, stored as their 32 raw bytes under "private_<id>", public keys are exchanged as the hex of their 33-byte compressed form, and signatures are the hex of a DER-encoded ECDSA signature over SHA-256 of the message. Signing is deterministic (RFC 6979), so Go and JavaScript produce byte-identical signatures for the same key and message.

Index

Constants

View Source
const DefaultPath = "./keystore"

DefaultPath is where New keeps keys when neither Storage nor Path is set.

Variables

View Source
var ErrNotFound = errors.New("keystore: key not found")

ErrNotFound is returned by GetKey when the keystore has no key for an id.

Functions

func PublicKey

func PublicKey(key crypto.PrivKey) (string, error)

PublicKey returns the hex encoding of key's raw public key, the form identities and entries carry.

func SignMessage

func SignMessage(key crypto.PrivKey, data []byte) (string, error)

SignMessage signs data with key and returns the hex-encoded signature.

func UnmarshalPrivateKey

func UnmarshalPrivateKey(raw []byte) (crypto.PrivKey, error)

UnmarshalPrivateKey parses a raw private key the way @libp2p/crypto's privateKeyFromRaw does: 32 bytes is secp256k1, 64 bytes is Ed25519 and anything else is tried as a PKCS#1 RSA key.

func UnmarshalPublicKey

func UnmarshalPublicKey(raw []byte) (crypto.PubKey, error)

UnmarshalPublicKey parses a raw public key the way @libp2p/crypto's publicKeyFromRaw does: 32 bytes is Ed25519, 33 bytes is compressed secp256k1 and anything else is tried as a DER (PKIX) ECDSA or RSA key.

func VerifyMessage

func VerifyMessage(signature, publicKey string, data []byte) bool

VerifyMessage reports whether signature (hex) is a valid signature of data by publicKey (hex of the raw public key). Malformed input verifies as false.

Types

type KeyStore

type KeyStore struct {
	// contains filtered or unexported fields
}

KeyStore stores private keys by id.

func New

func New(opts Options) (*KeyStore, error)

New opens a keystore.

func (*KeyStore) AddKey

func (ks *KeyStore) AddKey(ctx context.Context, id string, key crypto.PrivKey) error

AddKey stores key under id, replacing any key already stored for it.

func (*KeyStore) Clear

func (ks *KeyStore) Clear(ctx context.Context) error

Clear removes every key.

func (*KeyStore) Close

func (ks *KeyStore) Close() error

Close closes the underlying storage.

func (*KeyStore) CreateKey

func (ks *KeyStore) CreateKey(ctx context.Context, id string) (crypto.PrivKey, error)

CreateKey generates a secp256k1 key and stores it under id. Like @orbitdb/core it replaces any key already stored for id; callers that want get-or-create should call GetKey first.

func (*KeyStore) GetKey

func (ks *KeyStore) GetKey(ctx context.Context, id string) (crypto.PrivKey, error)

GetKey returns the key stored for id, or an error wrapping ErrNotFound.

func (*KeyStore) GetOrCreateKey

func (ks *KeyStore) GetOrCreateKey(ctx context.Context, id string) (crypto.PrivKey, error)

GetOrCreateKey returns the key stored for id, creating one if there is none.

func (*KeyStore) HasKey

func (ks *KeyStore) HasKey(ctx context.Context, id string) (bool, error)

HasKey reports whether a key is stored for id.

type Options

type Options struct {
	// Storage holds the keys. If nil, New uses an LRU cache in front of a
	// LevelStorage at Path.
	Storage storage.Storage
	// Path is the LevelDB directory used when Storage is nil. Defaults to
	// DefaultPath.
	Path string
}

Options configures New.

Jump to

Keyboard shortcuts

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