-
Notifications
You must be signed in to change notification settings - Fork 13
/
ipns.go
59 lines (47 loc) · 1.18 KB
/
ipns.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
package ipns
import (
"fmt"
"os"
"path"
"path/filepath"
"regexp"
"github.com/ipfs/gateway-conformance/tooling/fixtures"
)
/**
* Extracts the public key from the path of an IPNS record.
* The path is expected to be in the format of:
* some/path/then/[pubkey](_anything)?.ipns-record
*/
func extractPubkeyFromPath(path string) (string, error) {
filename := filepath.Base(path)
r := regexp.MustCompile(`^(.+?)(_.*|)\.ipns-record$`)
matches := r.FindStringSubmatch(filename)
if len(matches) < 2 {
return "", fmt.Errorf("failed to extract pubkey from path: %s", path)
}
return matches[1], nil
}
func OpenIPNSRecordWithKey(absPath string) (*IpnsRecord, error) {
// name is [pubkey](_anything)?.ipns-record
pubkey, err := extractPubkeyFromPath(absPath)
if err != nil {
return nil, err
}
data, err := os.ReadFile(absPath)
if err != nil {
return nil, err
}
r, err := UnmarshalIpnsRecord(data, pubkey)
if err != nil {
return nil, err
}
return r, nil
}
func MustOpenIPNSRecordWithKey(file string) *IpnsRecord {
fixturePath := path.Join(fixtures.Dir(), file)
ipnsRecord, err := OpenIPNSRecordWithKey(fixturePath)
if err != nil {
panic(err)
}
return ipnsRecord
}