Help us shape the future of Go! Take the 2026 Go Developer Survey

provingwork

package module
v0.0.0-...-b57e278 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2018 License: MIT Imports: 10 Imported by: 0

README

Proving Work

This is a collection of mechanisms for proving, verifying, and consuming various Proof of Work systems.

HashCash

This follows the official spec for HashCash. There is an oddity that may surprise users. When base64 encoding the counter value it will always encode a full 64 bit value where most implementations will only encode the minimum number of bytes. This won't effect verification or validation, but may effect other software that attempts to decode the value of the counter.

StrongWork

This is an implementation of a custom proof of work system. This uses the same type of data as hashcash, and looks for a hash collision in the same way, but uses a different encoding mechanism and switches to SHA256 for the digest.

It is primarily designed to exchange it's state through JSON encoding rather than a colon based mechanism. The resulting value could be decoded if so chosen but the order of fields may change.

Usage

You can find examples in the examples/ directory. Both versions are pretty straight forward to use. And breaks down to:

package main

import (
  "encoding/json"
  "fmt"
  "github.com/sstelfox/provingwork"
)

func main() {
	dataToVerify := []byte("Useful Identifier")

	hc := provingwork.NewHashCash(dataToVerify)
	hc.FindProof()
	fmt.Println(hc)

	sw := provingwork.NewStrongWork(dataToVerify)
	sw.FindProof()
	json, _ := json.Marshal(sw)
	fmt.Println(string(json))
}
Advanced Options

Both mechanisms support additional options and customizations. These are all passed as a second argument to the constructor function. The following shows how to create the additional options with the default values:

wo := WorkOptions{
	BitStrength: 22,
	Extension:   []byte{},
	Salt:        []byte{},    // 16 random bytes
	Timestamp:   *time.Time,  // Initializes to time.Now()
}

As the comments indicate if no Salt is provided it gets initialized to 16 random bytes, and the Timestamp field is the time at initialization. Extension is officially unused in the spec but can be used to hold additional data at the user's discretion, without effecting the ability to verify standard usage.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultBitStrength = 22
	DefaultSaltSize    = 16
)

Functions

This section is empty.

Types

type HashCash

type HashCash struct {
	Counter  int64  `json:"counter"`
	Resource []byte `json:"resource"`

	*WorkOptions
}

func NewHashCash

func NewHashCash(resource []byte, opts ...*WorkOptions) *HashCash

func (HashCash) Check

func (hc HashCash) Check() bool

func (HashCash) CounterBytes

func (hc HashCash) CounterBytes() []byte

func (*HashCash) FindProof

func (hc *HashCash) FindProof()

func (HashCash) MarshalJSON

func (wo HashCash) MarshalJSON() ([]byte, error)

func (HashCash) String

func (hc HashCash) String() string

func (HashCash) UnmarshalJSON

func (wo HashCash) UnmarshalJSON(data []byte) error

func (HashCash) ZeroCount

func (hc HashCash) ZeroCount() int

type HashCashExt

type HashCashExt struct {
	Timestamp int64 `json:"timestamp"`

	*RawHashCash
}

This is a special version of the HashCash that has the types we want to be importing / exporting.

type RawHashCash

type RawHashCash HashCash

An alias type that won't have any of functions (mostly to avoid an infinite loop with the overidden MarshalJSON function)

type RawStrongWork

type RawStrongWork StrongWork

An alias type that won't have any of functions (mostly to avoid an infinite loop with the overidden MarshalJSON function)

type StrongWork

type StrongWork struct {
	Counter  int64  `json:"counter"`
	Resource []byte `json:"resource"`

	*WorkOptions
}

func NewStrongWork

func NewStrongWork(resource []byte, opts ...*WorkOptions) *StrongWork

func (StrongWork) Check

func (sw StrongWork) Check() bool

func (StrongWork) ContentHash

func (sw StrongWork) ContentHash() []byte

func (*StrongWork) FindProof

func (sw *StrongWork) FindProof()

func (StrongWork) MarshalJSON

func (wo StrongWork) MarshalJSON() ([]byte, error)

func (StrongWork) UnmarshalJSON

func (wo StrongWork) UnmarshalJSON(data []byte) error

func (StrongWork) ZeroCount

func (sw StrongWork) ZeroCount() int

type StrongWorkExt

type StrongWorkExt struct {
	Timestamp int64 `json:"timestamp"`

	*RawStrongWork
}

This is a special version of the StrongWork that has the types we want to be importing / exporting.

type WorkOptions

type WorkOptions struct {
	BitStrength int        `json:"-"`
	Extension   []byte     `json:"extension,omitempty"`
	Salt        []byte     `json:"salt,omitempty"`
	Timestamp   *time.Time `json:"timestamp"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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