attributes.go 1.07 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
package irmago

import (
	"crypto/sha256"
	"math/big"

	"github.com/mhe/gabi"
)

// AttributeList contains attributes, excluding the secret key,
// providing convenient access to the metadata attribute.
type AttributeList struct {
13
14
15
	Ints    []*big.Int
	strings []string
	*gabi.MetadataAttribute `json:"-"`
16
17
18
19
20
}

// NewAttributeListFromInts initializes a new AttributeList from a list of bigints.
func NewAttributeListFromInts(ints []*big.Int) *AttributeList {
	return &AttributeList{
21
		Ints:              ints,
22
23
24
25
26
27
28
		MetadataAttribute: gabi.MetadataFromInt(ints[0]),
	}
}

// TODO maybe remove
func (al *AttributeList) hash() string {
	bytes := make([]byte, 20)
29
	for _, i := range al.Ints {
30
31
32
33
34
35
36
37
38
		bytes = append(bytes, i.Bytes()...)
	}
	shasum := sha256.Sum256(bytes)
	return string(shasum[:])
}

// Strings converts the current instance to human-readable strings.
func (al *AttributeList) Strings() []string {
	if al.strings == nil {
39
40
		al.strings = make([]string, len(al.Ints)-1)
		for index, num := range al.Ints[1:] { // skip metadata
41
42
43
44
45
			al.strings[index] = string(num.Bytes())
		}
	}
	return al.strings
}