keys

package
v0.106.3 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2024 License: MIT Imports: 25 Imported by: 183

Documentation

Overview

Package keys wraps public/private keys and implements NEP-2 and WIF.

Index

Constants

View Source
const SignatureLen = 64

SignatureLen is the length of a standard signature for 256-bit EC key.

View Source
const (
	// WIFVersion is the version used to decode and encode WIF keys.
	WIFVersion = 0x80
)

Variables

This section is empty.

Functions

func NEP2Encrypt

func NEP2Encrypt(priv *PrivateKey, passphrase string, params ScryptParams) (s string, err error)

NEP2Encrypt encrypts a the PrivateKey using the given passphrase under the NEP-2 standard.

func WIFEncode

func WIFEncode(key []byte, version byte, compressed bool) (s string, err error)

WIFEncode encodes the given private key into a WIF string.

Types

type PrivateKey

type PrivateKey struct {
	ecdsa.PrivateKey
}

PrivateKey represents a Neo private key and provides a high level API around ecdsa.PrivateKey.

func NEP2Decrypt

func NEP2Decrypt(key, passphrase string, params ScryptParams) (*PrivateKey, error)

NEP2Decrypt decrypts an encrypted key using the given passphrase under the NEP-2 standard.

func NewPrivateKey

func NewPrivateKey() (*PrivateKey, error)

NewPrivateKey creates a new random Secp256r1 private key.

func NewPrivateKeyFromASN1

func NewPrivateKeyFromASN1(b []byte) (*PrivateKey, error)

NewPrivateKeyFromASN1 returns a NEO Secp256k1 PrivateKey from the ASN.1 serialized key.

func NewPrivateKeyFromBytes

func NewPrivateKeyFromBytes(b []byte) (*PrivateKey, error)

NewPrivateKeyFromBytes returns a NEO Secp256r1 PrivateKey from the given byte slice.

func NewPrivateKeyFromHex

func NewPrivateKeyFromHex(str string) (*PrivateKey, error)

NewPrivateKeyFromHex returns a Secp256k1 PrivateKey created from the given hex string.

func NewPrivateKeyFromWIF

func NewPrivateKeyFromWIF(wif string) (*PrivateKey, error)

NewPrivateKeyFromWIF returns a NEO PrivateKey from the given WIF (wallet import format).

func NewSecp256k1PrivateKey added in v0.92.0

func NewSecp256k1PrivateKey() (*PrivateKey, error)

NewSecp256k1PrivateKey creates a new random Secp256k1 private key.

func (*PrivateKey) Address

func (p *PrivateKey) Address() string

Address derives the public NEO address that is coupled with the private key, and returns it as a string.

func (*PrivateKey) Bytes

func (p *PrivateKey) Bytes() []byte

Bytes returns the underlying bytes of the PrivateKey.

func (*PrivateKey) Destroy added in v0.99.3

func (p *PrivateKey) Destroy()

Destroy wipes the contents of the private key from memory. Any operations with the key after call to Destroy have undefined behavior.

func (*PrivateKey) GetScriptHash

func (p *PrivateKey) GetScriptHash() util.Uint160

GetScriptHash returns verification script hash for the public key associated with the private key.

func (*PrivateKey) PublicKey

func (p *PrivateKey) PublicKey() *PublicKey

PublicKey derives the public key from the private key.

func (*PrivateKey) Sign

func (p *PrivateKey) Sign(data []byte) []byte

Sign signs arbitrary length data using the private key. It uses SHA256 to calculate hash and then SignHash to create a signature (so you can save on hash calculation if you already have it).

func (*PrivateKey) SignHash added in v0.92.0

func (p *PrivateKey) SignHash(digest util.Uint256) []byte

SignHash signs a particular hash with the private key.

func (*PrivateKey) SignHashable added in v0.94.1

func (p *PrivateKey) SignHashable(net uint32, hh hash.Hashable) []byte

SignHashable signs some Hashable item for the network specified using hash.NetSha256() with the private key.

func (*PrivateKey) String

func (p *PrivateKey) String() string

String implements the stringer interface.

func (*PrivateKey) WIF

func (p *PrivateKey) WIF() string

WIF returns the (wallet import format) of the PrivateKey. Good documentation about this process can be found here: https://en.bitcoin.it/wiki/Wallet_import_format

type PublicKey

type PublicKey ecdsa.PublicKey

PublicKey represents a public key and provides a high level API around ecdsa.PublicKey.

func NewPublicKeyFromASN1

func NewPublicKeyFromASN1(data []byte) (*PublicKey, error)

NewPublicKeyFromASN1 returns a NEO PublicKey from the ASN.1 serialized key.

func NewPublicKeyFromBytes added in v0.90.0

func NewPublicKeyFromBytes(b []byte, curve elliptic.Curve) (*PublicKey, error)

NewPublicKeyFromBytes returns a public key created from b using the given EC.

func NewPublicKeyFromString

func NewPublicKeyFromString(s string) (*PublicKey, error)

NewPublicKeyFromString returns a public key created from the given hex string public key representation in compressed form.

func (*PublicKey) Address

func (p *PublicKey) Address() string

Address returns a base58-encoded NEO-specific address based on the key hash.

func (*PublicKey) Bytes

func (p *PublicKey) Bytes() []byte

Bytes returns byte array representation of the public key in compressed form (33 bytes with 0x02 or 0x03 prefix, except infinity which is always 0).

func (*PublicKey) Cmp

func (p *PublicKey) Cmp(key *PublicKey) int

Cmp compares two keys.

func (*PublicKey) DecodeBinary

func (p *PublicKey) DecodeBinary(r *io.BinReader)

DecodeBinary decodes a PublicKey from the given BinReader using information about the EC curve to decompress Y point. Secp256r1 is a default value for EC curve.

func (*PublicKey) DecodeBytes

func (p *PublicKey) DecodeBytes(data []byte) error

DecodeBytes decodes a PublicKey from the given slice of bytes.

func (*PublicKey) EncodeBinary

func (p *PublicKey) EncodeBinary(w *io.BinWriter)

EncodeBinary encodes a PublicKey to the given BinWriter.

func (*PublicKey) Equal

func (p *PublicKey) Equal(key *PublicKey) bool

Equal returns true in case public keys are equal.

func (*PublicKey) GetScriptHash

func (p *PublicKey) GetScriptHash() util.Uint160

GetScriptHash returns a Hash160 of verification script for the key.

func (*PublicKey) GetVerificationScript

func (p *PublicKey) GetVerificationScript() []byte

GetVerificationScript returns NEO VM bytecode with CHECKSIG command for the public key.

func (*PublicKey) IsInfinity

func (p *PublicKey) IsInfinity() bool

IsInfinity checks if the key is infinite (null, basically).

func (PublicKey) MarshalJSON added in v0.75.0

func (p PublicKey) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*PublicKey) MarshalYAML added in v0.103.0

func (p *PublicKey) MarshalYAML() (any, error)

MarshalYAML implements the YAML marshaler interface.

func (*PublicKey) String

func (p *PublicKey) String() string

String implements the Stringer interface.

func (*PublicKey) StringCompressed added in v0.106.0

func (p *PublicKey) StringCompressed() string

StringCompressed returns the hex string representation of the public key in its compressed form.

func (*PublicKey) UncompressedBytes added in v0.76.0

func (p *PublicKey) UncompressedBytes() []byte

UncompressedBytes returns byte array representation of the public key in uncompressed form (65 bytes with 0x04 prefix, except infinity which is always 0).

func (*PublicKey) UnmarshalJSON added in v0.75.0

func (p *PublicKey) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*PublicKey) UnmarshalYAML added in v0.103.0

func (p *PublicKey) UnmarshalYAML(unmarshal func(any) error) error

UnmarshalYAML implements the YAML unmarshaler interface.

func (*PublicKey) Verify

func (p *PublicKey) Verify(signature []byte, hash []byte) bool

Verify returns true if the signature is valid and corresponds to the hash and public key.

func (*PublicKey) VerifyHashable added in v0.94.1

func (p *PublicKey) VerifyHashable(signature []byte, net uint32, hh hash.Hashable) bool

VerifyHashable returns true if the signature is valid and corresponds to the hash and public key.

type PublicKeys

type PublicKeys []*PublicKey

PublicKeys is a list of public keys.

func NewPublicKeysFromStrings added in v0.98.2

func NewPublicKeysFromStrings(ss []string) (PublicKeys, error)

NewPublicKeysFromStrings converts an array of string-encoded P256 public keys into an array of PublicKeys.

func (*PublicKeys) Bytes added in v0.90.0

func (keys *PublicKeys) Bytes() []byte

Bytes encodes PublicKeys to the new slice of bytes.

func (PublicKeys) Contains

func (keys PublicKeys) Contains(pKey *PublicKey) bool

Contains checks whether the passed param is contained in PublicKeys.

func (PublicKeys) Copy added in v0.90.0

func (keys PublicKeys) Copy() PublicKeys

Copy returns a shallow copy of the PublicKeys slice. It creates a new slice with the same elements, but does not perform a deep copy of the elements themselves.

func (*PublicKeys) DecodeBytes

func (keys *PublicKeys) DecodeBytes(data []byte) error

DecodeBytes decodes a PublicKeys from the given slice of bytes.

func (PublicKeys) Len

func (keys PublicKeys) Len() int

func (PublicKeys) Less

func (keys PublicKeys) Less(i, j int) bool

func (PublicKeys) Swap

func (keys PublicKeys) Swap(i, j int)

func (PublicKeys) Unique

func (keys PublicKeys) Unique() PublicKeys

Unique returns a set of public keys.

type ScryptParams

type ScryptParams struct {
	N int `json:"n"`
	R int `json:"r"`
	P int `json:"p"`
}

ScryptParams is a json-serializable container for scrypt KDF parameters.

func NEP2ScryptParams

func NEP2ScryptParams() ScryptParams

NEP2ScryptParams returns scrypt parameters specified in the NEP-2.

type WIF

type WIF struct {
	// Version of the wallet import format. Default to 0x80.
	Version byte

	// Bool to determine if the WIF is compressed or not.
	Compressed bool

	// A reference to the PrivateKey which this WIF is created from.
	PrivateKey *PrivateKey

	// A string representation of the WIF.
	S string
}

WIF represents a wallet import format.

func WIFDecode

func WIFDecode(wif string, version byte) (*WIF, error)

WIFDecode decodes the given WIF string into a WIF struct.

Jump to

Keyboard shortcuts

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