forked from FactomProject/factomd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anchorSigningKey.go
127 lines (106 loc) · 2.72 KB
/
anchorSigningKey.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package identity
import (
"bytes"
"fmt"
"os"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/primitives"
"github.com/FactomProject/factomd/common/primitives/random"
)
//https://github.com/FactomProject/FactomDocs/blob/master/Identity.md
// sort.Sort interface implementation
type AnchorSigningKeySort []AnchorSigningKey
func (p AnchorSigningKeySort) Len() int {
return len(p)
}
func (p AnchorSigningKeySort) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p AnchorSigningKeySort) Less(i, j int) bool {
return bytes.Compare(p[i].SigningKey[:], p[j].SigningKey[:]) < 0
}
type AnchorSigningKey struct {
BlockChain string `json:"blockchain"`
KeyLevel byte `json:"level"`
KeyType byte `json:"keytype"`
SigningKey primitives.ByteSlice20 `json:"key"` //if bytes, it is hex
}
var _ interfaces.BinaryMarshallable = (*AnchorSigningKey)(nil)
func RandomAnchorSigningKey() *AnchorSigningKey {
ask := new(AnchorSigningKey)
ask.BlockChain = random.RandomString()
ask.KeyLevel = random.RandByte()
ask.KeyType = random.RandByte()
copy(ask.SigningKey[:], random.RandByteSliceOfLen(20))
return ask
}
func (e *AnchorSigningKey) IsSameAs(b *AnchorSigningKey) bool {
if e.BlockChain != b.BlockChain {
return false
}
if e.KeyLevel != b.KeyLevel {
return false
}
if e.KeyType != b.KeyType {
return false
}
if primitives.AreBytesEqual(e.SigningKey[:], b.SigningKey[:]) == false {
return false
}
return true
}
func (e *AnchorSigningKey) MarshalBinary() (rval []byte, err error) {
defer func(pe *error) {
if *pe != nil {
fmt.Fprintf(os.Stderr, "AnchorSigningKey.MarshalBinary err:%v", *pe)
}
}(&err)
buf := primitives.NewBuffer(nil)
err = buf.PushString(e.BlockChain)
if err != nil {
return nil, err
}
err = buf.PushByte(e.KeyLevel)
if err != nil {
return nil, err
}
err = buf.PushByte(e.KeyType)
if err != nil {
return nil, err
}
err = buf.Push(e.SigningKey[:])
if err != nil {
return nil, err
}
return buf.DeepCopyBytes(), nil
}
func (e *AnchorSigningKey) UnmarshalBinaryData(p []byte) (newData []byte, err error) {
newData = p
buf := primitives.NewBuffer(p)
e.BlockChain, err = buf.PopString()
if err != nil {
return
}
e.KeyLevel, err = buf.PopByte()
if err != nil {
return
}
e.KeyType, err = buf.PopByte()
if err != nil {
return
}
h, err := buf.PopLen(20)
if err != nil {
return
}
copy(e.SigningKey[:], h)
newData = buf.DeepCopyBytes()
return
}
func (e *AnchorSigningKey) UnmarshalBinary(p []byte) error {
_, err := e.UnmarshalBinaryData(p)
return err
}