about summary refs log tree commit diff
path: root/tvix/nar-bridge/pkg/http/narinfo.go
blob: 5d963c34f91df30f41d5ce34bdc51c2967852397 (plain) (blame)
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
package http

import (
	"fmt"

	storev1pb "code.tvl.fyi/tvix/store/protos"
	nixhash "github.com/nix-community/go-nix/pkg/hash"
	"github.com/nix-community/go-nix/pkg/narinfo"
	"github.com/nix-community/go-nix/pkg/narinfo/signature"
	"github.com/nix-community/go-nix/pkg/nixbase32"
)

// ToNixNarInfo converts the PathInfo to a narinfo.NarInfo.
func ToNixNarInfo(p *storev1pb.PathInfo) (*narinfo.NarInfo, error) {
	// ensure the PathInfo is valid, and extract the StorePath from the node in
	// there.
	storePath, err := p.Validate()
	if err != nil {
		return nil, fmt.Errorf("failed to validate PathInfo: %w", err)
	}

	// convert the signatures from storev1pb signatures to narinfo signatures
	narinfoSignatures := make([]signature.Signature, len(p.GetNarinfo().GetSignatures()))
	for i, pathInfoSignature := range p.GetNarinfo().GetSignatures() {
		narinfoSignatures[i] = signature.Signature{
			Name: pathInfoSignature.GetName(),
			Data: pathInfoSignature.GetData(),
		}
	}

	// produce nixhash for the narsha256.
	narHash, err := nixhash.FromHashTypeAndDigest(
		0x12, // SHA2_256
		p.GetNarinfo().GetNarSha256(),
	)
	if err != nil {
		return nil, fmt.Errorf("invalid narsha256: %w", err)
	}

	return &narinfo.NarInfo{
		StorePath:   storePath.Absolute(),
		URL:         "nar/" + nixbase32.EncodeToString(narHash.Digest()) + ".nar",
		Compression: "none",
		NarHash:     narHash,
		NarSize:     uint64(p.GetNarinfo().GetNarSize()),
		References:  p.GetNarinfo().GetReferenceNames(),
		Signatures:  narinfoSignatures,
	}, nil
}