cryptography

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2025 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Ed25519PublicKeySize is the size of an Ed25519 public key in bytes.
	Ed25519PublicKeySize = 32
	// Secp256k1PublicKeySize is the size of a Secp256k1 public key in bytes.
	Secp256k1PublicKeySize = 33
	// Secp256r1PublicKeySize is the size of a Secp256r1 public key in bytes.
	Secp256r1PublicKeySize = 33
)
View Source
const (
	// Ed25519CURVE is the curve used for Ed25519 key derivation.
	Ed25519CURVE = "ed25519 seed"
	// HardenedOffset is the offset used for hardened derivation.
	HardenedOffset = 0x80000000
)
View Source
const (
	// PrivateKeySize is the size of a Sui private key in bytes.
	PrivateKeySize = 32
	// SuiPrivateKeyPrefix is the Bech32 prefix for Sui private keys.
	SuiPrivateKeyPrefix = "suiprivkey"
)

Variables

View Source
var SignatureFlagToScheme = map[SignatureFlag]SignatureScheme{
	0x00: Ed25519Scheme,
	0x01: Secp256k1Scheme,
	0x02: Secp256r1Scheme,
	0x03: MultiSigScheme,
	0x05: ZkLoginScheme,
}

SignatureFlagToScheme is a map that associates signature flags with their corresponding signature schemes.

View Source
var SignatureSchemeToFlag = map[SignatureScheme]SignatureFlag{
	Ed25519Scheme:   0x00,
	Secp256k1Scheme: 0x01,
	Secp256r1Scheme: 0x02,
	MultiSigScheme:  0x03,
	ZkLoginScheme:   0x05,
}

SignatureSchemeToFlag is a map that associates signature schemes with their corresponding flags.

View Source
var SignatureSchemeToSize = map[SignatureScheme]int{
	Ed25519Scheme:   32,
	Secp256k1Scheme: 33,
	Secp256r1Scheme: 33,
}

SignatureSchemeToSize is a map that associates signature schemes with their corresponding signature sizes in bytes.

Functions

func EncodeSuiPrivateKey

func EncodeSuiPrivateKey(bytes []byte, scheme string) (string, error)

EncodeSuiPrivateKey returns a Bech32 encoded string starting with `suiprivkey`, encoding 33-byte `flag || bytes` for the given 32-byte private key and its signature scheme.

func GenerateMnemonic

func GenerateMnemonic() (string, error)

GenerateMnemonic generates a new mnemonic phrase with 12 words.

func IsValidBIP32Path

func IsValidBIP32Path(path string) bool

IsValidBIP32Path checks if a given path is compliant with BIP-32 for Secp256k1 and Secp256r1. m/54'/784'/{account_index}'/{change_index}/{address_index} for Secp256k1 and m/74'/784'/{account_index}'/{change_index}/{address_index} for Secp256r1. Note that the purpose for Secp256k1 is registered as 54, to differentiate from Ed25519 with purpose 44. e.g. `m/54'/784'/0'/0/0`

func IsValidHardenedPath

func IsValidHardenedPath(path string) bool

IsValidHardenedPath checks if a given path is compliant with SLIP-0010 m/44'/784'/{account_index}'/{change_index}'/{address_index}'. e.g. `m/44'/784'/0'/0'/0'`

func IsValidPath

func IsValidPath(path string) bool

IsValidPath checks if the given path is a valid derivation path.

func MessageWithIntent

func MessageWithIntent(scope IntentScope, message []byte) []byte

MessageWithIntent combines the intent with the provided message, returning a new byte slice that includes both.

func MnemonicToSeed

func MnemonicToSeed(mnemonics string) ([]byte, error)

MnemonicToSeed converts a mnemonic phrase to a seed using KDF to derive 64 bytes of key data from mnemonic with empty password. mnemonics 12 words string split by spaces.

func MnemonicToSeedHex

func MnemonicToSeedHex(mnemonics string) (string, error)

MnemonicToSeedHex converts a mnemonic phrase to a seed in hex format. Derive the seed in hex format from a 12-word mnemonic string. mnemonics 12 words string split by spaces.

func ReadByte

func ReadByte(r io.Reader) (byte, int, error)

ReadByte reads a single byte from the provided io.Reader.

func ReplaceDerive

func ReplaceDerive(val string) string

ReplaceDerive removes the quote character from the derivation path segment.

func ToSerializedSignature

func ToSerializedSignature(input SerializeSignatureInput) (string, error)

ToSerializedSignature serializes a signature along with its signing scheme and public key into a base64 encoded string. Takes in a signature, its associated signing scheme and a public key, then serializes this data

Types

type AppID added in v0.0.6

type AppID = uint8

AppID is the application identifier for the intent.

const (
	// Sui is the AppID for Sui.
	Sui AppID = iota
)

type BaseKeypair

type BaseKeypair struct {
	BaseSigner
}

BaseKeypair defines a struct that implements the Keypair interface.

func (*BaseKeypair) SetSelf

func (base *BaseKeypair) SetSelf(signer Signer)

SetSelf sets the signer for the BaseKeypair.

type BasePublicKey

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

BasePublicKey defines a struct that implements the PublicKey interface.

func (*BasePublicKey) Equals

func (base *BasePublicKey) Equals(publicKey PublicKey) bool

Equals checks if the provided public key is equal to the base public key.

func (*BasePublicKey) SetSelf

func (base *BasePublicKey) SetSelf(pubkey PublicKey)

SetSelf sets the self reference for the BasePublicKey.

func (*BasePublicKey) ToBase64

func (base *BasePublicKey) ToBase64() string

ToBase64 returns the base-64 representation of the public key.

func (*BasePublicKey) ToSuiAddress

func (base *BasePublicKey) ToSuiAddress() string

ToSuiAddress returns the Sui address associated with this public key.

func (*BasePublicKey) ToSuiBytes

func (base *BasePublicKey) ToSuiBytes() []byte

ToSuiBytes returns the bytes representation of the public key prefixed with the signature scheme flag.

func (*BasePublicKey) ToSuiPublicKey

func (base *BasePublicKey) ToSuiPublicKey() string

ToSuiPublicKey returns the Sui representation of the public key encoded in base-64. A Sui public key is formed by the concatenation of the scheme flag with the raw bytes of the public key

func (*BasePublicKey) VerifyPersonalMessage

func (base *BasePublicKey) VerifyPersonalMessage(message []byte, signature SerializedSignature) (bool, error)

VerifyPersonalMessage verifies that the signature is valid for the provided personal message.

func (*BasePublicKey) VerifyTransactionBlock

func (base *BasePublicKey) VerifyTransactionBlock(transactionBlock []byte, signature SerializedSignature) (bool, error)

VerifyTransactionBlock verifies that the signature is valid for the provided transaction block.

func (*BasePublicKey) VerifyWithIntent

func (base *BasePublicKey) VerifyWithIntent(bs []byte, signature SerializedSignature, intent IntentScope) (bool, error)

VerifyWithIntent verifies that the signature is valid for the provided message bytes and intent.

type BaseSigner

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

BaseSigner defines a struct that implements the Signer interface.

func (*BaseSigner) SignPersonalMessage

func (signer *BaseSigner) SignPersonalMessage(bs []byte) (*SignatureWithBytes, error)

SignPersonalMessage signs provided personal message by calling `signWithIntent()` with a `PersonalMessage` provided as intent scope

func (*BaseSigner) SignTransactionBlock

func (signer *BaseSigner) SignTransactionBlock(bs []byte) (*SignatureWithBytes, error)

SignTransactionBlock signs provided transaction block by calling `signWithIntent()` with a `TransactionData` provided as intent scope

func (*BaseSigner) SignWithIntent

func (signer *BaseSigner) SignWithIntent(bs []byte, intent IntentScope) (*SignatureWithBytes, error)

SignWithIntent signs a message with a specific intent scope. By combining the message bytes with the intent before hashing and signing, it ensures that a signed message is tied to a specific purpose and domain separator is provided

func (*BaseSigner) ToSuiAddress

func (signer *BaseSigner) ToSuiAddress() string

ToSuiAddress converts the public key of the signer to a Sui address.

type CompressedSignature

type CompressedSignature struct {
	Signature [65]byte `json:"signature"`
}

CompressedSignature defines a type for compressed signatures.

type Intent

type Intent = []uint8

Intent defines the structure of an intent, which includes the scope, version, and application identifier.

func IntentWithScope

func IntentWithScope(scope IntentScope) Intent

IntentWithScope creates an intent with the specified scope, using the default version and application identifier.

type IntentScope

type IntentScope = uint8

IntentScope is the scope of the intent, indicating what type of data it applies to.

const (
	// TransactionData is the IntentScope for transaction data.
	TransactionData IntentScope = iota
	// TransactionEffects is the IntentScope for transaction effects.
	TransactionEffects
	// CheckpointSummary is the IntentScope for checkpoint summaries.
	CheckpointSummary
	// PersonalMessage is the IntentScope for personal messages.
	PersonalMessage
)

type IntentVersion

type IntentVersion = uint8

IntentVersion is the version of the intent, which is used to manage changes in the intent structure over time.

const (
	// V0 is the IntentVersion for the initial version of Sui.
	V0 IntentVersion = iota
)

type Key

type Key struct {
	Key       []byte
	ChainCode []byte
}

Key defines a structure that holds a key and its associated chain code.

func CKDPriv

func CKDPriv(parent *Key, index uint32) (*Key, error)

CKDPriv performs child key derivation for a private key using the parent key and index.

func DerivePath

func DerivePath(path string, seed string) (*Key, error)

DerivePath derives a key from the given derivation path and seed.

func GetMasterKeyFromSeed

func GetMasterKeyFromSeed(seed string) (*Key, error)

GetMasterKeyFromSeed derives the master key from the given seed.

type Keypair

type Keypair interface {
	Signer
	GetSecretKey() (string, error)
}

Keypair is an interface that extends the Signer interface and adds a method to retrieve the secret key.

type MultiSigPublicKeyPair

type MultiSigPublicKeyPair struct {
	Weight    uint8     `json:"weight"`
	PublicKey PublicKey `json:"publicKey"`
}

MultiSigPublicKeyPair defines a structure that holds a weight and a public key for multisig operations.

type MultiSigPublicKeyStruct

type MultiSigPublicKeyStruct struct {
	PubKeyMap []*PubKeyEnumWeightPair `json:"pubKeymap"`
	Threshold uint16                  `json:"threshold"`
}

MultiSigPublicKeyStruct defines a structure that holds a map of public keys and their weights, along with a threshold.

type MultiSigStruct

type MultiSigStruct struct {
	Sigs           []CompressedSignature   `json:"sigs"`
	Bitmap         uint16                  `json:"bitmap"`
	MultisigPubKey MultiSigPublicKeyStruct `json:"multisigPubKey"`
}

MultiSigStruct defines a structure that holds a list of compressed signatures, a bitmap, and a multisig public key.

type ParsedKeypair

type ParsedKeypair struct {
	Scheme    SignatureScheme
	SecretKey []byte
}

ParsedKeypair defines a structure that holds the signature scheme and the secret key in bytes.

func DecodeSuiPrivateKey

func DecodeSuiPrivateKey(value string) (*ParsedKeypair, error)

DecodeSuiPrivateKey decodes a Bech32 encoded Sui private key string into a ParsedKeypair object. This returns a ParsedKeypair object by validating the 33-byte Bech32 encoded string starting with `suiprivkey`, and parses out the signature scheme and the private key in bytes.

type PubKeyEnumWeightPair

type PubKeyEnumWeightPair struct {
	PubKey []byte `json:"pubKey"`
	Weight uint8  `json:"weight"`
}

PubKeyEnumWeightPair defines a structure that holds a public key and its associated weight.

func (PubKeyEnumWeightPair) MarshalBCS

func (p PubKeyEnumWeightPair) MarshalBCS() ([]byte, error)

MarshalBCS serializes the PubKeyEnumWeightPair into a BCS format.

func (*PubKeyEnumWeightPair) UnmarshalBCS

func (p *PubKeyEnumWeightPair) UnmarshalBCS(r io.Reader) (int, error)

UnmarshalBCS deserializes the PubKeyEnumWeightPair from a BCS format.

type PublicKey

type PublicKey interface {
	Equals(publicKey PublicKey) bool
	ToBase64() string
	ToSuiPublicKey() string
	VerifyWithIntent(bs []byte, signature SerializedSignature, intent IntentScope) (bool, error)
	VerifyPersonalMessage(message []byte, signature SerializedSignature) (bool, error)
	VerifyTransactionBlock(transactionBlock []byte, signature SerializedSignature) (bool, error)
	ToSuiBytes() []byte
	ToSuiAddress() string
	ToRawBytes() []byte
	Flag() uint8
	Verify(message []byte, signature SerializedSignature) (bool, error)
}

PublicKey is an interface that defines the methods for a public key in the Sui cryptography system.

type SerializeSignatureInput

type SerializeSignatureInput struct {
	SignatureScheme SignatureScheme
	Signature       []byte
	PublicKey       PublicKey
}

SerializeSignatureInput defines the input required to serialize a signature, including the signature, scheme, and public key.

type SerializedSignature

type SerializedSignature = string

SerializedSignature defines a type for serialized signatures.

type SerializedSignatureParsedData

type SerializedSignatureParsedData struct {
	SerializedSignature SerializedSignature `json:"serializedSignature"`
	SignatureScheme     string              `json:"signatureScheme"`
	Signature           []byte              `json:"signature,omitempty"`
	PubKey              []byte              `json:"pubKey,omitempty"`
	Bytes               []byte              `json:"bytes"`
	Multisig            *MultiSigStruct     `json:"multisig,omitempty"`
}

SerializedSignatureParsedData defines a struct that holds the parsed data from a serialized signature.

func ParseSerializedSignature

func ParseSerializedSignature(serializedSignature SerializedSignature) (*SerializedSignatureParsedData, error)

ParseSerializedSignature decodes a serialized signature into its constituent components: the signature scheme, the actual signature, and the public key

type SignatureFlag

type SignatureFlag = uint8

SignatureFlag defines the type for signature flags, which are used to identify the signature scheme in serialized signatures.

type SignatureScheme

type SignatureScheme = string

SignatureScheme defines the type for signature schemes used in Sui.

var (
	// Ed25519Scheme is the signature scheme for Ed25519.
	Ed25519Scheme SignatureScheme = "ED25519"
	// Secp256k1Scheme is the signature scheme for Secp256k1.
	Secp256k1Scheme SignatureScheme = "Secp256k1"
	// Secp256r1Scheme is the signature scheme for Secp256r1.
	Secp256r1Scheme SignatureScheme = "Secp256r1"
	// MultiSigScheme is the signature scheme for MultiSig.
	MultiSigScheme SignatureScheme = "MultiSig"
	// ZkLoginScheme is the signature scheme for ZkLogin.
	ZkLoginScheme SignatureScheme = "ZkLogin"
)

type SignatureWithBytes

type SignatureWithBytes struct {
	Bytes     string
	Signature SerializedSignature
}

SignatureWithBytes defines a structure that holds the base64 encoded bytes of the signed message and the serialized signature.

type Signer

type Signer interface {
	Sign(bs []byte) ([]byte, error)
	SignWithIntent(bs []byte, intent IntentScope) (*SignatureWithBytes, error)
	SignTransactionBlock(bs []byte) (*SignatureWithBytes, error)
	SignPersonalMessage(bs []byte) (*SignatureWithBytes, error)
	ToSuiAddress() string
	SignData(data []byte) ([]byte, error)
	GetKeyScheme() SignatureScheme
	GetPublicKey() (PublicKey, error)
}

Signer is an interface that defines the methods for signing messages and retrieving public keys.

Jump to

Keyboard shortcuts

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