Skip to content

Commit 06127ee

Browse files
fix: #2 AdditionalData field now sorted into arrays
1 parent d5f5f96 commit 06127ee

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

dag/leaves.go

+32-8
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,14 @@ func (b *DagLeafBuilder) BuildLeaf(additionalData map[string]string) (*DagLeaf,
109109
MerkleRoot []byte
110110
CurrentLinkCount int
111111
ContentHash []byte
112-
AdditionalData map[string]string
112+
AdditionalData []keyValue
113113
}{
114114
ItemName: b.ItemName,
115115
Type: b.LeafType,
116116
MerkleRoot: merkleRoot,
117117
CurrentLinkCount: len(b.Links),
118118
ContentHash: nil,
119-
AdditionalData: additionalData,
119+
AdditionalData: sortMapForVerification(additionalData),
120120
}
121121

122122
if b.Data != nil {
@@ -190,7 +190,7 @@ func (b *DagLeafBuilder) BuildRootLeaf(dag *DagBuilder, additionalData map[strin
190190
LatestLabel string
191191
LeafCount int
192192
ContentHash []byte
193-
AdditionalData map[string]string
193+
AdditionalData []keyValue
194194
}{
195195
ItemName: b.ItemName,
196196
Type: b.LeafType,
@@ -199,7 +199,7 @@ func (b *DagLeafBuilder) BuildRootLeaf(dag *DagBuilder, additionalData map[strin
199199
LatestLabel: latestLabel,
200200
LeafCount: len(dag.Leafs),
201201
ContentHash: nil,
202-
AdditionalData: additionalData,
202+
AdditionalData: sortMapForVerification(additionalData),
203203
}
204204

205205
if b.Data != nil {
@@ -293,14 +293,14 @@ func (leaf *DagLeaf) VerifyLeaf() error {
293293
MerkleRoot []byte
294294
CurrentLinkCount int
295295
ContentHash []byte
296-
AdditionalData map[string]string
296+
AdditionalData []keyValue
297297
}{
298298
ItemName: leaf.ItemName,
299299
Type: leaf.Type,
300300
MerkleRoot: leaf.ClassicMerkleRoot,
301301
CurrentLinkCount: leaf.CurrentLinkCount,
302302
ContentHash: leaf.ContentHash,
303-
AdditionalData: additionalData,
303+
AdditionalData: sortMapForVerification(additionalData),
304304
}
305305

306306
serializedLeafData, err := cbor.Marshal(leafData)
@@ -363,7 +363,7 @@ func (leaf *DagLeaf) VerifyRootLeaf() error {
363363
LatestLabel string
364364
LeafCount int
365365
ContentHash []byte
366-
AdditionalData map[string]string
366+
AdditionalData []keyValue
367367
}{
368368
ItemName: leaf.ItemName,
369369
Type: leaf.Type,
@@ -372,7 +372,7 @@ func (leaf *DagLeaf) VerifyRootLeaf() error {
372372
LatestLabel: leaf.LatestLabel,
373373
LeafCount: leaf.LeafCount,
374374
ContentHash: leaf.ContentHash,
375-
AdditionalData: additionalData,
375+
AdditionalData: sortMapForVerification(additionalData),
376376
}
377377

378378
serializedLeafData, err := cbor.Marshal(leafData)
@@ -572,3 +572,27 @@ func sortMapByKeys(inputMap map[string]string) map[string]string {
572572

573573
return sortedMap
574574
}
575+
576+
type keyValue struct {
577+
Key string
578+
Value string
579+
}
580+
581+
func sortMapForVerification(inputMap map[string]string) []keyValue {
582+
if inputMap == nil {
583+
return nil
584+
}
585+
586+
keys := make([]string, 0, len(inputMap))
587+
for key := range inputMap {
588+
keys = append(keys, key)
589+
}
590+
sort.Strings(keys)
591+
592+
sortedPairs := make([]keyValue, 0, len(keys))
593+
for _, key := range keys {
594+
sortedPairs = append(sortedPairs, keyValue{Key: key, Value: inputMap[key]})
595+
}
596+
597+
return sortedPairs
598+
}

0 commit comments

Comments
 (0)