Skip to content

Commit

Permalink
fix: #2 AdditionalData field now sorted into arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
f7f376a1fcd0d0e11a10ed1b6577c9 committed Jun 16, 2024
1 parent d5f5f96 commit 06127ee
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions dag/leaves.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ func (b *DagLeafBuilder) BuildLeaf(additionalData map[string]string) (*DagLeaf,
MerkleRoot []byte
CurrentLinkCount int
ContentHash []byte
AdditionalData map[string]string
AdditionalData []keyValue
}{
ItemName: b.ItemName,
Type: b.LeafType,
MerkleRoot: merkleRoot,
CurrentLinkCount: len(b.Links),
ContentHash: nil,
AdditionalData: additionalData,
AdditionalData: sortMapForVerification(additionalData),
}

if b.Data != nil {
Expand Down Expand Up @@ -190,7 +190,7 @@ func (b *DagLeafBuilder) BuildRootLeaf(dag *DagBuilder, additionalData map[strin
LatestLabel string
LeafCount int
ContentHash []byte
AdditionalData map[string]string
AdditionalData []keyValue
}{
ItemName: b.ItemName,
Type: b.LeafType,
Expand All @@ -199,7 +199,7 @@ func (b *DagLeafBuilder) BuildRootLeaf(dag *DagBuilder, additionalData map[strin
LatestLabel: latestLabel,
LeafCount: len(dag.Leafs),
ContentHash: nil,
AdditionalData: additionalData,
AdditionalData: sortMapForVerification(additionalData),
}

if b.Data != nil {
Expand Down Expand Up @@ -293,14 +293,14 @@ func (leaf *DagLeaf) VerifyLeaf() error {
MerkleRoot []byte
CurrentLinkCount int
ContentHash []byte
AdditionalData map[string]string
AdditionalData []keyValue
}{
ItemName: leaf.ItemName,
Type: leaf.Type,
MerkleRoot: leaf.ClassicMerkleRoot,
CurrentLinkCount: leaf.CurrentLinkCount,
ContentHash: leaf.ContentHash,
AdditionalData: additionalData,
AdditionalData: sortMapForVerification(additionalData),
}

serializedLeafData, err := cbor.Marshal(leafData)
Expand Down Expand Up @@ -363,7 +363,7 @@ func (leaf *DagLeaf) VerifyRootLeaf() error {
LatestLabel string
LeafCount int
ContentHash []byte
AdditionalData map[string]string
AdditionalData []keyValue
}{
ItemName: leaf.ItemName,
Type: leaf.Type,
Expand All @@ -372,7 +372,7 @@ func (leaf *DagLeaf) VerifyRootLeaf() error {
LatestLabel: leaf.LatestLabel,
LeafCount: leaf.LeafCount,
ContentHash: leaf.ContentHash,
AdditionalData: additionalData,
AdditionalData: sortMapForVerification(additionalData),
}

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

return sortedMap
}

type keyValue struct {
Key string
Value string
}

func sortMapForVerification(inputMap map[string]string) []keyValue {
if inputMap == nil {
return nil
}

keys := make([]string, 0, len(inputMap))
for key := range inputMap {
keys = append(keys, key)
}
sort.Strings(keys)

sortedPairs := make([]keyValue, 0, len(keys))
for _, key := range keys {
sortedPairs = append(sortedPairs, keyValue{Key: key, Value: inputMap[key]})
}

return sortedPairs
}

0 comments on commit 06127ee

Please sign in to comment.