utils

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package utils provides utility functions for working with cryptographic messages

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotMessage is returned when trying to unmarshal invalid message bytes
	ErrNotMessage = errors.New("not a message")
	// ErrInvalidMessage is returned when trying to verify an invalid message
	ErrInvalidMessage = errors.New("invalid message")
	// ErrInvalidMessageSender is returned when trying to verify a message with an invalid sender
	ErrInvalidMessageSender = errors.New("invalid message sender")
	// ErrAlgorithmNotSupported is returned when trying to use an unsupported cryptographic algorithm
	ErrAlgorithmNotSupported = errors.New("algorithm not supported yet")
	// ErrInvalidSignature is returned when trying to verify a message with an invalid signature
	ErrInvalidSignature = errors.New("invalid signature")
)

Functions

func FromPublicKey

func FromPublicKey(pub interface{}) (string, error)

FromPublicKey generates an Ethereum address from the given public key. It serializes the public key, hashes it using Keccak-256, truncates the hash, and adds a prefix to get the final Ethereum address.

func GenerateHash

func GenerateHash(payload []byte) []byte

GenerateHash generates a SHA512 hash for the given payload.

func ParseTxError

func ParseTxError(err error) error

Types

type GoPDFTemplate

type GoPDFTemplate struct {
	// Path to image template
	Image string `json:"image,omitempty"`
	// Locations to write text
	Locations []Location `json:"locations,omitempty"`
}

GoPDFTemplate is a template for gopdf to render a pdf from a image

func (*GoPDFTemplate) Load

func (gop *GoPDFTemplate) Load(bytes []byte) error

Load loads a GoPDFTemplate from bytes

func (*GoPDFTemplate) LoadFromFile

func (gop *GoPDFTemplate) LoadFromFile(path string) error

LoadFromFile loads a GoPDFTemplate from a file

func (*GoPDFTemplate) Render

func (gop *GoPDFTemplate) Render(option RenderOpts) ([]byte, error)

Render renders a pdf from a GoPDFTemplate

type Location

type Location struct {
	// Text to be written
	Text string `json:"text"`
	// Inputs to be parsed into text with fmt.Sprintf()
	Inputs []string `json:"inputs"`
	// Position of text
	X float64 `json:"x"`
	Y float64 `json:"y"`
	// Font style and size
	Style string `json:"style"`
	Size  int    `json:"size"`
}

Location is a location to write text

type Message

type Message struct {
	Nonce     uint64 `json:"nonce"`
	PublicKey []byte `json:"publicKey"`
	Signature []byte `json:"signature"`
}

Message represents a cryptographic message

func (*Message) GeneratePayload

func (msg *Message) GeneratePayload(args ...string) []byte

GeneratePayload generates a payload for the message with the given arguments. The payload includes the message nonce and all the arguments appended together.

func (*Message) Marshal

func (msg *Message) Marshal() ([]byte, error)

Marshal returns the JSON encoding of the message. If the message is nil, a new message is created.

func (*Message) Unmarshal

func (msg *Message) Unmarshal(bytes []byte) error

Unmarshal unmarshals the given bytes into a Message struct. It returns an error if the unmarshalling fails.

func (*Message) UnmarshalBase64Str

func (msg *Message) UnmarshalBase64Str(str string) error

UnmarshalBase64Str unmarshals the given base64-encoded string into a Message struct. It returns an error if the unmarshalling fails.

func (*Message) VerifyAgainstArgs

func (msg *Message) VerifyAgainstArgs(args ...string) (string, error)

VerifyAgainstArgs verifies that the message signature is valid against the given arguments. It returns the Ethereum address of the message sender and an error, if any.

type RenderOpts

type RenderOpts struct {
	PageSize gopdf.Rect
	// Path to ttf font
	TtfFontPath string
	// Inputs to be parsed into text with fmt.Sprintf()
	Inputs map[string]string
	// Cell size for gopdf to render the pdf
	CellSize gopdf.Rect
}

RenderOpts is the options for GoPDFTemplate.Render()

Jump to

Keyboard shortcuts

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