keys

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package keys provides Canton key generation and encryption for custodial key management. This package is used to generate Canton keypairs for users and encrypt them for secure storage. Uses secp256k1 (same curve as Ethereum) for compatibility with user wallets.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeriveEVMAddressFromPublicKey

func DeriveEVMAddressFromPublicKey(compressedPubKey []byte) string

DeriveEVMAddressFromPublicKey derives an EVM address from a compressed secp256k1 public key. This is used for Canton native users to generate an EVM-compatible address for MetaMask access.

If decompression fails (e.g., invalid public key), it falls back to using the Keccak256 hash of the compressed key (last 20 bytes with 0x prefix).

func GenerateMasterKey

func GenerateMasterKey() ([]byte, error)

GenerateMasterKey generates a new random 32-byte master key for encrypting Canton keys. This should be stored securely (environment variable, secrets manager, etc.)

func MarshalSPKIPublicKey

func MarshalSPKIPublicKey(x, y *big.Int) ([]byte, error)

MarshalSPKIPublicKey encodes an uncompressed EC point (x, y) on secp256k1 as a DER-encoded X.509 SubjectPublicKeyInfo structure.

func MasterKeyFromBase64

func MasterKeyFromBase64(encoded string) ([]byte, error)

MasterKeyFromBase64 decodes a base64-encoded master key

func MasterKeyToBase64

func MasterKeyToBase64(key []byte) string

MasterKeyToBase64 encodes a master key as base64 for storage

func Secp256k1

func Secp256k1() elliptic.Curve

Secp256k1 returns the secp256k1 elliptic curve.

func VerifyDER

func VerifyDER(compressedPubKey []byte, hash []byte, derSig []byte) error

VerifyDER verifies a DER-encoded ECDSA signature against a compressed public key and a pre-hashed 32-byte digest. Used by the transfer execute endpoint to validate client-provided signatures before forwarding to Canton.

Types

type CantonKeyPair

type CantonKeyPair struct {
	PublicKey  []byte // 33-byte compressed secp256k1 public key
	PrivateKey []byte // 32-byte secp256k1 private key
}

CantonKeyPair represents a Canton signing keypair using secp256k1

func CantonKeyPairFromPrivateKey

func CantonKeyPairFromPrivateKey(privKey []byte) (*CantonKeyPair, error)

CantonKeyPairFromPrivateKey reconstructs a full keypair from a 32-byte private key.

func DeriveCantonKeyPair

func DeriveCantonKeyPair(evmAddress string, serverSeed []byte) (*CantonKeyPair, error)

DeriveCantonKeyPair deterministically derives a Canton keypair from an EVM address and server seed. This allows the same keypair to be regenerated if needed (though the encrypted key is preferred). Uses HKDF with SHA-256 for key derivation.

func GenerateCantonKeyPair

func GenerateCantonKeyPair() (*CantonKeyPair, error)

GenerateCantonKeyPair generates a new secp256k1 keypair for Canton signing This uses the same curve as Ethereum for potential wallet integration

func (*CantonKeyPair) Fingerprint

func (kp *CantonKeyPair) Fingerprint() (string, error)

Fingerprint returns the Canton key fingerprint: multihash-encoded SHA-256 of the SPKI public key bytes with hash purpose 12.

func (*CantonKeyPair) PrivateKeyHex

func (kp *CantonKeyPair) PrivateKeyHex() string

PrivateKeyHex returns the private key as a hex string with 0x prefix (for MetaMask import)

func (*CantonKeyPair) PublicKeyBase64

func (kp *CantonKeyPair) PublicKeyBase64() string

PublicKeyBase64 returns the public key as a base64 string

func (*CantonKeyPair) PublicKeyHex

func (kp *CantonKeyPair) PublicKeyHex() string

PublicKeyHex returns the public key as a hex string (for display/logging)

func (*CantonKeyPair) SPKIPublicKey

func (kp *CantonKeyPair) SPKIPublicKey() ([]byte, error)

SPKIPublicKey returns the public key in X.509 SubjectPublicKeyInfo DER format.

func (*CantonKeyPair) Sign

func (kp *CantonKeyPair) Sign(message []byte) ([]byte, error)

Sign signs a message with the private key using ECDSA with SHA-256 Returns the signature in DER format (compatible with Canton)

func (*CantonKeyPair) SignDER

func (kp *CantonKeyPair) SignDER(message []byte) ([]byte, error)

SignDER signs a message with SHA-256 and returns an ASN.1 DER-encoded ECDSA signature. This is the format Canton requires for Interactive Submission and topology signing.

func (*CantonKeyPair) SignHash

func (kp *CantonKeyPair) SignHash(hash []byte) ([]byte, error)

SignHash signs a pre-hashed message (useful when Canton provides the hash)

func (*CantonKeyPair) SignHashDER

func (kp *CantonKeyPair) SignHashDER(hash []byte) ([]byte, error)

SignHashDER signs a pre-hashed 32-byte digest and returns an ASN.1 DER-encoded signature. Use this when Canton provides the hash directly (PrepareSubmission, GenerateExternalPartyTopology).

func (*CantonKeyPair) Verify

func (kp *CantonKeyPair) Verify(message, signature []byte) bool

Verify verifies a signature against a message

type KeyCipher

type KeyCipher interface {
	Encrypt(key []byte) (string, error)
	Decrypt(encryptedKey string) ([]byte, error)
}

KeyCipher encrypts and decrypts Canton private keys.

type MasterKeyCipher

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

MasterKeyCipher implements KeyCipher using AES-256-GCM with a 32-byte master key.

func NewMasterKeyCipher

func NewMasterKeyCipher(masterKey []byte) *MasterKeyCipher

NewMasterKeyCipher creates a MasterKeyCipher from a 32-byte master key.

func (*MasterKeyCipher) Decrypt

func (c *MasterKeyCipher) Decrypt(encryptedKey string) ([]byte, error)

Decrypt decrypts an encrypted private key using AES-256-GCM.

func (*MasterKeyCipher) Encrypt

func (c *MasterKeyCipher) Encrypt(key []byte) (string, error)

Encrypt encrypts a private key using AES-256-GCM.

Jump to

Keyboard shortcuts

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