// printing self is special cased as we get values differently. func printSelf(node *core.IpfsNode) (interface{}, error) { info := new(IdOutput) info.ID = node.Identity.Pretty() if node.PrivateKey == nil { if err := node.LoadPrivateKey(); err != nil { return nil, err } } pk := node.PrivateKey.GetPublic() pkb, err := ic.MarshalPublicKey(pk) if err != nil { return nil, err } info.PublicKey = base64.StdEncoding.EncodeToString(pkb) if node.PeerHost != nil { for _, a := range node.PeerHost.Addrs() { s := a.String() + "/ipfs/" + info.ID info.Addresses = append(info.Addresses, s) } } info.ProtocolVersion = identify.IpfsVersion info.AgentVersion = identify.ClientVersion return info, nil }
func printPeer(ps peer.Peerstore, p peer.ID) (interface{}, error) { if p == "" { return nil, errors.New("Attempted to print nil peer!") } info := new(IdOutput) info.ID = p.Pretty() if pk := ps.PubKey(p); pk != nil { pkb, err := ic.MarshalPublicKey(pk) if err != nil { return nil, err } info.PublicKey = base64.StdEncoding.EncodeToString(pkb) } for _, a := range ps.Addrs(p) { info.Addresses = append(info.Addresses, a.String()) } if v, err := ps.Get(p, "ProtocolVersion"); err == nil { if vs, ok := v.(string); ok { info.ProtocolVersion = vs } } if v, err := ps.Get(p, "AgentVersion"); err == nil { if vs, ok := v.(string); ok { info.AgentVersion = vs } } return info, nil }
// createSignatureNode creates a node that contains a signature over its links // The signature is calculated on the sorted list of link hashes, so the signature // node's hash stays the same when using the same links and a deterministic signature. func createSignatureNode(ipfsNode *core.IpfsNode, nodes []*merkledag.Node) (*merkledag.Node, error) { signatureNode := &merkledag.Node{} // Add links to signature node and collect link hashes linkHashes := make(hashSort, len(nodes)) for i, node := range nodes { signatureNode.AddNodeLink(string(i), node) linkHashes[i] = []byte(node.Links[i].Hash) } // Create signature sort.Sort(linkHashes) signBytes := bytes.Join(linkHashes, []byte{}) signature, err := ipfsNode.PrivateKey.Sign(signBytes) if err != nil { return nil, err } // Marshal public key and finalize the signature node publicKey, err := crypto.MarshalPublicKey(ipfsNode.PrivateKey.GetPublic()) if err != nil { return nil, err } signatureNodeData, err := proto.Marshal(&dcpb.SignatureNodeData{Signature: signature, PublicKey: publicKey}) if err != nil { return nil, err } signatureNode.Data = signatureNodeData return signatureNode, nil }
//New returns Record obj with Thread name. func New(self *peer.Self, thread string, stamp time.Time, contents map[string][]byte, sign bool) (*Record, error) { r := &Record{ Contents: contents, self: self, Stamp: stamp, Thread: thread, } if err := r.check(false); log.If(err) { return nil, err } var err error r.Contents["thread"] = []byte(r.Thread) bstamp := make([]byte, 8) binary.BigEndian.PutUint64(bstamp, uint64(r.Stamp.UnixNano())) r.Contents["stamp"] = bstamp if sign { var errr error r.Contents["pubkey"], errr = crypto.MarshalPublicKey(r.self.Pubkey()) if log.If(errr) { return nil, errr } } err = r.toDAGNode() if log.If(err) { return nil, err } if sign { r.Sign, err = r.self.Sign([]byte(r.Key)) if log.If(err) { return nil, err } } log.If(err) return r, err }
func TestPeer(t *testing.T) { const ( rootPath = "test" ) jsonstr := ` { "max_connection":12, "file_dir":".", "run_dir":"rundir", "option":"option" } ` denied := ` QmZQJubLBGn1JUv8zx24TasR57LnyK1KjUiHWncVqrPxy7 QmU3eUhmUnZxtUJzMtq8YNVz2Q1U75fn2DEgoMXWcCNf6v QmPhL4RCK1h241eyMBbYxPBv4iBuguPbRB2gGbGjsuXLQv QmUt5G1QiJePEGKQvjZwcjisoUNUfmKZaATXQcDYJsoEEj ` if err := ioutil.WriteFile("denied_peers.txt", []byte(denied), 0644); err != nil { t.Error(err) } if err := ioutil.WriteFile("test.json", []byte(jsonstr), 0644); err != nil { t.Error(err) } var cfg config.Config config.Load(rootPath, ".", &cfg) cfg.SetDefaults() self := NewSelf(&cfg, rootPath) m := self.ToPeer() skeys := []string{ "QmZQJubLBGn1JUv8zx24TasR57LnyK1KjUiHWncVqrPxy7", "QmU3eUhmUnZxtUJzMtq8YNVz2Q1U75fn2DEgoMXWcCNf6v", "QmdrPVCaguLLJM2QnK5qdBDqYwQWQKuq4yHK8E9zX1Et8X", "QmPhL4RCK1h241eyMBbYxPBv4iBuguPbRB2gGbGjsuXLQv", } k := key.B58KeyDecode(skeys[0]) p, err := FromBytes([]byte(k), self) if err != nil { t.Fatal(err) } if skeys[0] != p.String() { t.Fatal("cannot new") } if m.Equals(p) { t.Fatal("illegal equals") } b, err := crypto.MarshalPublicKey(self.Pubkey()) if err != nil { t.Fatal(err) } p, err = FromPubkey(b, self) if err != nil { t.Fatal(err) } if self.NodeName().Pretty() != p.String() { t.Fatal("cannot new") } if !m.Equals(p) { t.Fatal("illegal equals") } p, err = New(skeys[0], self) if err != nil { t.Fatal(err) } if skeys[0] != p.String() { t.Fatal("cannot new") } if p.IsAllowed() { t.Fatal("illegally allowed") } p, err = New(skeys[2], self) if err != nil { t.Fatal(err) } if !p.IsAllowed() { t.Fatal("illegally denied") } ps := FromStringSlice(skeys, self) strs := ps.Strings() if len(strs) != len(ps) { t.Fatal("illegally strings") } for _, s := range strs { ok := false for _, s2 := range skeys { if s == s2 { ok = true } } if !ok { t.Fatal("illegally strings") } } sl := ps.Slice() ps2 := FromSlice(sl) if !ps.Equals(ps2) { t.Fatal("illegal slicing") } if len(sl) != len(skeys) { t.Fatal("illegal slicing") } ps3 := FromStringSlice(skeys[1:], self) ps4 := ps.Excludes(ps3, 0) if len(ps4) != 1 { t.Fatal("illegal excludes") } ps5 := FromStringSlice(skeys[3:], self) ps6 := ps.Excludes(ps5, 2) if len(ps6) != 2 { t.Fatal("illegal excludes") } ps7 := FromStringSlice(skeys[1:], self) ps8 := FromStringSlice(skeys[:2], self) ps8.Extend(ps7) if !ps.Equals(ps8) { t.Fatal("illegal extends") } }