@@ -58,6 +58,7 @@ type Chain interface {
58
58
59
59
// ConsenterSupport provides the resources available to a Consenter implementation
60
60
type ConsenterSupport interface {
61
+ Signer
61
62
BlockCutter () blockcutter.Receiver
62
63
SharedConfig () sharedconfig.Manager
63
64
CreateNextBlock (messages []* cb.Envelope ) * cb.Block
@@ -83,6 +84,7 @@ type chainSupport struct {
83
84
sharedConfigManager sharedconfig.Manager
84
85
ledger rawledger.ReadWriter
85
86
filters * filter.RuleSet
87
+ signer Signer
86
88
lastConfiguration uint64
87
89
lastConfigSeq uint64
88
90
}
@@ -94,6 +96,7 @@ func newChainSupport(
94
96
backing rawledger.ReadWriter ,
95
97
sharedConfigManager sharedconfig.Manager ,
96
98
consenters map [string ]Consenter ,
99
+ signer Signer ,
97
100
) * chainSupport {
98
101
99
102
cutter := blockcutter .NewReceiverImpl (sharedConfigManager , filters )
@@ -110,6 +113,7 @@ func newChainSupport(
110
113
cutter : cutter ,
111
114
filters : filters ,
112
115
ledger : backing ,
116
+ signer : signer ,
113
117
}
114
118
115
119
var err error
@@ -145,6 +149,14 @@ func (cs *chainSupport) start() {
145
149
cs .chain .Start ()
146
150
}
147
151
152
+ func (cs * chainSupport ) NewSignatureHeader () * cb.SignatureHeader {
153
+ return cs .signer .NewSignatureHeader ()
154
+ }
155
+
156
+ func (cs * chainSupport ) Sign (message []byte ) []byte {
157
+ return cs .signer .Sign (message )
158
+ }
159
+
148
160
func (cs * chainSupport ) SharedConfig () sharedconfig.Manager {
149
161
return cs .sharedConfigManager
150
162
}
@@ -181,21 +193,70 @@ func (cs *chainSupport) CreateNextBlock(messages []*cb.Envelope) *cb.Block {
181
193
return rawledger .CreateNextBlock (cs .ledger , messages )
182
194
}
183
195
184
- func (cs * chainSupport ) WriteBlock (block * cb.Block , committers []filter.Committer ) * cb.Block {
185
- for _ , committer := range committers {
186
- committer .Commit ()
196
+ // TODO, factor this out into common util code
197
+ func metadataSignatureBytes (value []byte , sigHeader []byte , blockHeader []byte ) []byte {
198
+ result := make ([]byte , len (value )+ len (sigHeader )+ len (blockHeader ))
199
+ last := 0
200
+ for _ , slice := range [][]byte {value , sigHeader , blockHeader } {
201
+ for i := range slice {
202
+ result [i + last ] = slice [i ]
203
+ }
204
+ last += len (slice )
187
205
}
206
+ return result
207
+ }
208
+
209
+ func (cs * chainSupport ) addBlockSignature (block * cb.Block ) {
210
+ logger .Debugf ("%+v" , cs )
211
+ logger .Debugf ("%+v" , cs .signer )
212
+ blockSignature := & cb.MetadataSignature {
213
+ SignatureHeader : utils .MarshalOrPanic (cs .signer .NewSignatureHeader ()),
214
+ }
215
+
216
+ // Note, this value is intentionally nil, as this metadata is only about the signature, there is no additional metadata
217
+ // information required beyond the fact that the metadata item is signed.
218
+ blockSignatureValue := []byte (nil )
219
+
220
+ blockSignature .Signature = cs .signer .Sign (metadataSignatureBytes (blockSignatureValue , blockSignature .SignatureHeader , block .Header .Bytes ()))
221
+
222
+ block .Metadata .Metadata [cb .BlockMetadataIndex_SIGNATURES ] = utils .MarshalOrPanic (& cb.Metadata {
223
+ Value : blockSignatureValue ,
224
+ Signatures : []* cb.MetadataSignature {
225
+ blockSignature ,
226
+ },
227
+ })
228
+ }
188
229
230
+ func (cs * chainSupport ) addLastConfigSignature (block * cb.Block ) {
189
231
configSeq := cs .configManager .Sequence ()
190
232
if configSeq > cs .lastConfigSeq {
191
233
cs .lastConfiguration = block .Header .Number
192
234
cs .lastConfigSeq = configSeq
193
235
}
194
236
237
+ lastConfigSignature := & cb.MetadataSignature {
238
+ SignatureHeader : utils .MarshalOrPanic (cs .signer .NewSignatureHeader ()),
239
+ }
240
+
241
+ lastConfigValue := utils .MarshalOrPanic (& cb.LastConfiguration {Index : cs .lastConfiguration })
242
+
243
+ lastConfigSignature .Signature = cs .signer .Sign (metadataSignatureBytes (lastConfigValue , lastConfigSignature .SignatureHeader , block .Header .Bytes ()))
244
+
195
245
block .Metadata .Metadata [cb .BlockMetadataIndex_LAST_CONFIGURATION ] = utils .MarshalOrPanic (& cb.Metadata {
196
- Value : utils .MarshalOrPanic (& cb.LastConfiguration {Index : cs .lastConfiguration }),
197
- // XXX Add signature once signing is available
246
+ Value : lastConfigValue ,
247
+ Signatures : []* cb.MetadataSignature {
248
+ lastConfigSignature ,
249
+ },
198
250
})
251
+ }
252
+
253
+ func (cs * chainSupport ) WriteBlock (block * cb.Block , committers []filter.Committer ) * cb.Block {
254
+ for _ , committer := range committers {
255
+ committer .Commit ()
256
+ }
257
+
258
+ cs .addBlockSignature (block )
259
+ cs .addLastConfigSignature (block )
199
260
200
261
err := cs .ledger .Append (block )
201
262
if err != nil {
0 commit comments