identity

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2021 License: Unlicense Imports: 7 Imported by: 0

Documentation

Overview

A Nyzo identity, represented by a private and public key pair. Offering some convenience conversions to Nyzo hex key notations and Nyzo strings. A nickname can be added optionally.

Index

Constants

View Source
const (
	NyzoStringTypePrivateKey   = 1
	NyzoStringPrivateKeyPrefix = "key_"
	NyzoStringTypePublicKey    = 2
	NyzoStringPublicKeyPrefix  = "id__"
	NyzoStringCharacters       = "0123456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ-.~_"
)

Variables

View Source
var NyzoStringCharacterMap map[byte]int

Functions

func BytesToNyzoHex

func BytesToNyzoHex(data []byte) string

BytesToNyzoHex converts the given byte array to a Nyzo style hex string with separating dashes.

func FromNyzoString added in v1.0.4

func FromNyzoString(data string) ([]byte, error)

func NyzoHexToBytes

func NyzoHexToBytes(data []byte, length int) ([]byte, error)

NyzoHexToBytes converts 'data' - a sequence of hex character bytes - to a byte array of the defined length. So, [102 54] (aka 'f6') will turn into [246]. The function mimics the somewhat idiosyncratic behavior of the original java function by ignoring any non-hex characters in the input data. Like that, separator dashes ('-') will be filtered out, but so would any other non-hex characters. Also like the original function, we ignore if we get too much input data for the desired result length. We do however NOT ignore if we don't get enough input data, so unless there is an error returned, the result will always have the desired length.

func ToNyzoString

func ToNyzoString(stringType int, content []byte) string

ToNyzoString converts the given content data to a Nyzo string of the given type. A Nyzo string consists of: prefix + content length + content + checksum. In terms of its underlying byte array, the prefix is always 3 bytes long, the content length is expressed in one byte, then comes the content, and finally, the checksum is a minimum of 4 bytes and a maximum of 6 bytes, widening the expanded array so that its length is always divisible by 3. This byte arrray is then mapped to a select set of characters for human readability, resulting in what is commonly known as a "Nyzo String". TODO: only public and private key string types are supported, add the others: micropay, prefilled and transactions.

Types

type Identity

type Identity struct {
	PrivateKey        ed25519.PrivateKey // the private key (native format)
	PublicKey         ed25519.PublicKey  // the public key (native format)
	PrivateHex        string             // hexadecimal dashed representation of the private key
	PublicHex         string             // hexadecimal dashed representation of the public key
	ShortId           string             // a short identifier based on the public key hex (ab03...de78)
	Nickname          string             // the user defined Nyzo nickname, used for display only
	NyzoStringPrivate string             // typed, error-protected encoding of the private key
	NyzoStringPublic  string             // typed, error-protected encoding of the public key
}

func FromPrivateKey

func FromPrivateKey(privateKey []byte) (*Identity, error)

Generate an ID from a private key directly.

func FromPrivateKeyFile

func FromPrivateKeyFile(privateKeyFile string) (*Identity, error)

Load an identity from a private key file.

func New

func New(privateKeyFile string, infoFile string) (*Identity, error)

Creates a new identity and saves info about it to the given files. Existing files will be overwritten, so the caller must make sure that no private key info gets unintentionally lost along the way.

func (*Identity) LoadNicknameFromFile

func (id *Identity) LoadNicknameFromFile(file string)

Loads the user-defined nickname from the given file (if not possible, the ShortId is taken as a nick).

func (*Identity) Sign

func (id *Identity) Sign(data []byte) []byte

Sign the given data with this identity, returns the signature.

Jump to

Keyboard shortcuts

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