message

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var IPFSGateway = "http://localhost:5001" // IPFS Gateway used to connect to the IPFS daemon

Functions

func AddJSONToIPFS

func AddJSONToIPFS(json []byte) (string, error)

func CountLeadingZeroes

func CountLeadingZeroes(b [32]byte) int

Bitwise count leading zeroes in a byte slice

func InitIPFS

func InitIPFS() *ipfs.Shell

Set up an IPFS instance based on the IPFSGateway

Types

type Message

type Message struct {
	Message   string
	Timestamp int64
	Nonce     int
}

Messages on Infodump use a "stamp" using the hashcash algorithm to prevent spam and enable storing messages by importance The Message type contains the message itself and a nonce that is used to verify the stamp

func New

func New(msg string, n int, timestamp int64, timeout time.Duration) (*Message, error)

func (*Message) Hash

func (m *Message) Hash() [32]byte

Get the SHA256 hash of a message plus the timestamp plus the nonce as a byte slice

func (*Message) Lead

func (m *Message) Lead() int

Lead is a method that returns the number of leading zeroes in the hash of a message plus its nonce

func (*Message) ProofOfWork

func (msg *Message) ProofOfWork(n int, timeout time.Duration) error

Proof of Work: Find the nonce for a message by hashing the message and checking for at least n initial zeroes in the binary representation of the resulting hash If it takes too long, return an error

func (*Message) SortNum

func (m *Message) SortNum() int64

SortNum of a Message returns a number that can be used to sort messages by importance The number is calculated by taking the timestamp of the message and adding an importance factor of 2^(leading zeros of hash / 8) to it If the timestamp is in the future, return 0 instead so the message will be discarded unless there are almost no messages

func (*Message) Stamp

func (m *Message) Stamp() string

Get the stamp of the message

func (*Message) String

func (m *Message) String() string

String method for Message: "Message *hash* sent at *human readable timestamp* with nonce *nonce*:\n*message*"

type Messages

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

Map Messages maps the stamp of the message to the message itself

func MessagesFromIPFS

func MessagesFromIPFS(cid string) (*Messages, error)

MessagesFromIPFS takes a CID and returns a Messages map

func (*Messages) Add

func (m *Messages) Add(msg *Message)

Add a message to the Messages map

func (*Messages) AddMany

func (m *Messages) AddMany(msgs *Messages)

AddMany adds another Messages map to the current Messages map

func (*Messages) AddToIPFS

func (m *Messages) AddToIPFS() (string, error)

Add the messages as a JSON object to IPFS

func (*Messages) Each

func (m *Messages) Each(f func(msg *Message))

Do something with each message in the Messages map

func (*Messages) JSON

func (m *Messages) JSON() ([]byte, error)

Return a JSON representation of the Messages map

func (*Messages) MessageList

func (m *Messages) MessageList() []*Message

MessageList returns a slice of Messages sorted by importance The slice is sorted by the SortNum method of the Message type It can be created from a Messages map by calling the Messages.Sorted method

func (*Messages) Remove

func (m *Messages) Remove(stamp string)

Remove a message from the Messages map by stamp

func (*Messages) Trim

func (m *Messages) Trim(n int)

Trim the Messages map to the given number of messages based on the importance of the messages

Jump to

Keyboard shortcuts

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