/
game_step.go
187 lines (148 loc) · 3.19 KB
/
game_step.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package state
import (
"bytes"
"encoding/json"
"fmt"
"time"
"github.com/pkg/errors"
"github.com/apiarian/go-ipgs/cachedshell"
)
type GameStep struct {
player *Player
data []byte
parent Commit
timestamp time.Time
signature []byte
hash string
}
type fileGameStep struct {
PlayerID string
Data []byte
ParentHash string
Timestamp IPGSTime
Signature []byte
Hash string
}
type ipfsGameStep struct {
Data []byte
}
func NewGameStep() *GameStep {
return &GameStep{}
}
func (g *GameStep) Player() *Player {
return g.player
}
func (g *GameStep) Data() []byte {
dat := make([]byte, len(g.data))
copy(dat, g.data)
return dat
}
func (g *GameStep) Type() string {
return CommitTypeGameStep
}
func (g *GameStep) Timestamp() time.Time {
return g.timestamp
}
func (g *GameStep) ID() string {
return g.Parent().ID()
}
func (g *GameStep) Committer() *Player {
return g.Player()
}
func (g *GameStep) Parent() Commit {
return g.parent
}
func (g *GameStep) Hash() string {
return g.hash
}
func (g *GameStep) Signature() []byte {
sig := make([]byte, len(g.signature))
copy(sig, g.signature)
return sig
}
func (g *GameStep) SignatureData() ([]byte, error) {
if g.Parent().Hash() == "" {
return nil, errors.New("the parent does not have a hash")
}
b := bytes.NewBufferString(fmt.Sprintf(
"%s|%s|",
g.ID(),
g.Timestamp().UTC().Format(time.RFC3339Nano),
))
_, err := b.Write(g.Data())
if err != nil {
return nil, errors.Wrap(err, "failed to write game data to buffer")
}
_, err = b.WriteString(fmt.Sprintf(
"|%s",
g.Parent().Hash(),
))
if err != nil {
return nil, errors.Wrap(err, "failed to write parent hash to buffer")
}
return b.Bytes(), nil
}
func (g *GameStep) Sign() error {
if len(g.signature) == 0 {
sig, err := signCommit(g)
if err != nil {
return errors.Wrap(err, "failed to sign game step")
}
g.signature = sig
}
return nil
}
func (g *GameStep) Verify() error {
err := verifyCommit(g)
if err != nil {
return errors.Wrap(err, "failed to verify the game step")
}
return nil
}
func (g *GameStep) IpfsJsonData() ([]byte, error) {
d, err := json.Marshal(
&ipfsGameStep{
Data: g.data,
},
)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal game step data to JSON")
}
return d, nil
}
func getIpfsGameStep(h string, s *cachedshell.Shell) (*ipfsGameStep, error) {
obj, err := s.ObjectGet(h)
if err != nil {
return nil, errors.Wrap(err, "failed to get game step data object")
}
var c ipfsGameStep
err = json.Unmarshal([]byte(obj.Data), &c)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal game step data JSON")
}
return &c, nil
}
func (g *GameStep) Publish(s *cachedshell.Shell) (string, error) {
if g.hash == "" {
h, err := publishCommit(g, s)
if err != nil {
return "", errors.Wrap(err, "failed to publish game step")
}
g.hash = h
}
return g.hash, nil
}
func (g *GameStep) clone() Commit {
dat := make([]byte, len(g.data))
copy(dat, g.data)
sig := make([]byte, len(g.signature))
copy(sig, g.signature)
return &GameStep{
player: g.player,
data: dat,
parent: g.parent.clone(),
timestamp: g.timestamp,
signature: sig,
hash: g.hash,
}
}