forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
credential.go
45 lines (38 loc) · 1.09 KB
/
credential.go
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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package secp256k1fx
import (
"encoding/json"
"errors"
"fmt"
"github.com/MetalBlockchain/metalgo/utils/crypto/secp256k1"
"github.com/MetalBlockchain/metalgo/utils/formatting"
)
var ErrNilCredential = errors.New("nil credential")
type Credential struct {
Sigs [][secp256k1.SignatureLen]byte `serialize:"true" json:"signatures"`
}
// MarshalJSON marshals [cr] to JSON
// The string representation of each signature is created using the hex formatter
func (cr *Credential) MarshalJSON() ([]byte, error) {
signatures := make([]string, len(cr.Sigs))
for i, sig := range cr.Sigs {
sigStr, err := formatting.Encode(formatting.HexNC, sig[:])
if err != nil {
return nil, fmt.Errorf("couldn't convert signature to string: %w", err)
}
signatures[i] = sigStr
}
jsonFieldMap := map[string]interface{}{
"signatures": signatures,
}
return json.Marshal(jsonFieldMap)
}
func (cr *Credential) Verify() error {
switch {
case cr == nil:
return ErrNilCredential
default:
return nil
}
}