cryptography

package
v0.0.30 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2024 License: GPL-2.0, GPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Ed25519PublicKeySize   = 32
	Secp256k1PublicKeySize = 33
	Secp256r1PublicKeySize = 33
)
View Source
const (
	Ed25519CURVE   = "ed25519 seed"
	HardenedOffset = 0x80000000
)
View Source
const (
	PrivateKeySize      = 32
	SuiPrivateKeyPrefix = "suiprivkey"
)

Variables

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

Functions

func EncodeSuiPrivateKey

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

*

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

func GenerateMnemonic added in v0.0.18

func GenerateMnemonic() (string, error)

func IsValidBIP32Path

func IsValidBIP32Path(path string) bool

Parse and validate a path that is compliant to BIP-32 in form 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

Parse and validate a path that is compliant to SLIP-0010 in form m/44'/784'/{account_index}'/{change_index}'/{address_index}'. e.g. `m/44'/784'/0'/0'/0'`

func IsValidPath

func IsValidPath(path string) bool

func MessageWithIntent

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

func MnemonicToSeed

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

Uses 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)

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)

func ReplaceDerive

func ReplaceDerive(val string) string

func ToSerializedSignature

func ToSerializedSignature(input SerializeSignatureInput) (string, error)

*

  • Takes in a signature, its associated signing scheme and a public key, then serializes this data

Types

type AppId

type AppId = uint8
const (
	Sui AppId = iota
)

type BaseKeypair

type BaseKeypair struct {
	BaseSigner
}

func (*BaseKeypair) SetSelf

func (base *BaseKeypair) SetSelf(signer Signer)

type BasePublicKey

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

func (*BasePublicKey) Equals

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

Checks if two public keys are equal

func (*BasePublicKey) SetSelf

func (base *BasePublicKey) SetSelf(pubkey PublicKey)

To ensure thread memory safety, this method needs to be removed. This matter is temporarily added to the todo list

func (*BasePublicKey) ToBase64

func (base *BasePublicKey) ToBase64() string

Return the base-64 representation of the public key

func (*BasePublicKey) ToSuiAddress

func (base *BasePublicKey) ToSuiAddress() string

Return the Sui address associated with this Ed25519 public key

func (*BasePublicKey) ToSuiBytes

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

*

  • Returns the bytes representation of the public key
  • prefixed with the signature scheme flag

func (*BasePublicKey) ToSuiPublicKey

func (base *BasePublicKey) ToSuiPublicKey() string

* Return 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)

Verifies that the signature is valid for for the provided PersonalMessage

func (*BasePublicKey) VerifyTransactionBlock

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

Verifies that the signature is valid for for the provided TransactionBlock

func (*BasePublicKey) VerifyWithIntent

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

type BaseSigner

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

func (*BaseSigner) SignPersonalMessage

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

*

  • 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)

*

  • 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)

*

  • Sign messages with a specific intent. 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

type CompressedSignature

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

type Intent

type Intent = []uint8

func IntentWithScope

func IntentWithScope(scope IntentScope) Intent

type IntentScope

type IntentScope = uint8
const (
	TransactionData IntentScope = iota
	TransactionEffects
	CheckpointSummary
	PersonalMessage
)

type IntentVersion

type IntentVersion = uint8
const (
	V0 IntentVersion = iota
)

type Key

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

func CKDPriv

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

func DerivePath

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

func GetMasterKeyFromSeed

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

type Keypair

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

type MultiSigPublicKeyPair

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

type MultiSigPublicKeyStruct

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

type MultiSigStruct

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

type ParsedKeypair

type ParsedKeypair struct {
	Scheme    SignatureScheme
	SecretKey []byte
}

func DecodeSuiPrivateKey

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

*

  • This returns an ParsedKeypair object based by validating the
  • 33-byte Bech32 encoded string starting with `suiprivkey`, and
  • parse out the signature scheme and the private key in bytes.

type PubKeyEnumWeightPair

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

func (PubKeyEnumWeightPair) MarshalBCS

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

func (*PubKeyEnumWeightPair) UnmarshalBCS

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

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)
}

type SerializeSignatureInput

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

*

  • Pair of signature and corresponding public key

type SerializedSignature

type SerializedSignature = string

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"`
}

func ParseSerializedSignature

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

*

  • Decodes a serialized signature into its constituent components: the signature scheme, the actual signature, and the public key

type SignatureFlag

type SignatureFlag = uint8

type SignatureScheme

type SignatureScheme = string
var (
	Ed25519Scheme   SignatureScheme = "ED25519"
	Secp256k1Scheme SignatureScheme = "Secp256k1"
	Secp256r1Scheme SignatureScheme = "Secp256r1"
	MultiSigScheme  SignatureScheme = "MultiSig"
	ZkLoginScheme   SignatureScheme = "ZkLogin"
)

type SignatureWithBytes

type SignatureWithBytes struct {
	Bytes     string
	Signature SerializedSignature
}

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)
}

Jump to

Keyboard shortcuts

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