aboutsummaryrefslogtreecommitdiff
path: root/auction.go
blob: e4f53b75979029b835a1ad1fc3a8bc36975ff2ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package seal

import (
	"bytes"
	"crypto/ed25519"
	"crypto/sha512"
	"encoding/base32"
	"encoding/json"
	"time"
)

type Type int

const (
	TypHighest = iota
	TypSecondHighest
)

// Auction describes the asset of an auction and other
// relevant meta-data
type Description struct {
	Start           time.Time
	End             time.Time
	RoundTimeout    time.Duration // Timeout per round by which all responses must have arrived
	BitLength       uint8         // Length of the price encoding
	Currency        string
	Type            Type
	SellerPublicKey ed25519.PublicKey // Public key of the Seller
}

// The SignedAuction contains an Auction and the signature,
// signed by the seller's public key off the SHA512 hash of
// the normalized JSON-object.
type SignedDescription struct {
	Description
	SellerSignature []byte
}

func (sd *SignedDescription) Verify() (bool, error) {
	// TODO: need to normalize this encoding
	buf := &bytes.Buffer{}
	e := json.NewEncoder(buf).Encode(sd.Description)
	if e != nil {
		return false, e
	}
	r := ed25519.Verify(sd.SellerPublicKey, buf.Bytes(), sd.SellerSignature)
	return r, nil
}

func (d *Description) Hash() (hash string, e error) {
	buf := &bytes.Buffer{}
	e = json.NewEncoder(buf).Encode(d)
	if e != nil {
		return "", e
	}
	h := sha512.Sum512(buf.Bytes())
	return base32.StdEncoding.EncodeToString(h[:]), nil
}