This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ipns_test.go
63 lines (54 loc) · 1.48 KB
/
ipns_test.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
60
61
62
63
package ipns
import (
"fmt"
"testing"
"time"
u "github.com/ipfs/go-ipfs-util"
ci "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
)
func TestEmbedPublicKey(t *testing.T) {
sr := u.NewTimeSeededRand()
priv, pub, err := ci.GenerateKeyPairWithReader(ci.RSA, 2048, sr)
if err != nil {
t.Fatal(err)
}
pid, err := peer.IDFromPublicKey(pub)
if err != nil {
t.Fatal(err)
}
e, err := Create(priv, []byte("/a/b"), 0, time.Now().Add(1*time.Hour), 0)
if err != nil {
t.Fatal(err)
}
if err := EmbedPublicKey(pub, e); err != nil {
t.Fatal(err)
}
embeddedPk, err := ci.UnmarshalPublicKey(e.PubKey)
if err != nil {
t.Fatal(err)
}
embeddedPid, err := peer.IDFromPublicKey(embeddedPk)
if err != nil {
t.Fatal(err)
}
if embeddedPid != pid {
t.Fatalf("pid mismatch: %s != %s", pid, embeddedPid)
}
}
func ExampleCreate() {
// Generate a private key to sign the IPNS record with. Most of the time,
// however, you'll want to retrieve an already-existing key from IPFS using
// go-ipfs/core/coreapi CoreAPI.KeyAPI() interface.
privateKey, _, err := ci.GenerateKeyPair(ci.RSA, 2048)
if err != nil {
panic(err)
}
// Create an IPNS record that expires in one hour and points to the IPFS address
// /ipfs/Qme1knMqwt1hKZbc1BmQFmnm9f36nyQGwXxPGVpVJ9rMK5
ipnsRecord, err := Create(privateKey, []byte("/ipfs/Qme1knMqwt1hKZbc1BmQFmnm9f36nyQGwXxPGVpVJ9rMK5"), 0, time.Now().Add(1*time.Hour), 0)
if err != nil {
panic(err)
}
fmt.Println(ipnsRecord)
}