// InvertKey returns a b58 decoded Datastore key // TODO: this is hacky because it encodes every path component. some // path components may be proper strings already... func (b58KeyConverter) InvertKey(dsk ds.Key) ds.Key { k := ds.NewKey("/") for _, n := range dsk.Namespaces() { k = k.ChildString(string(b58.Decode(n))) } return k }
// IsValidHash checks whether a given hash is valid (b58 decodable, len > 0) func IsValidHash(s string) bool { out := b58.Decode(s) if out == nil || len(out) == 0 { return false } _, err := mh.Cast(out) if err != nil { return false } return true }
func ParseKeyToPath(txt string) (Path, error) { chk := b58.Decode(txt) if len(chk) == 0 { return "", errors.New("not a key") } _, err := mh.Cast(chk) if err != nil { return "", err } return FromKey(key.Key(chk)), nil }
func FromB58String(s string) (m Multihash, err error) { // panic handler, in case we try accessing bytes incorrectly. defer func() { if e := recover(); e != nil { m = Multihash{} err = e.(error) } }() //b58 smells like it can panic... b := b58.Decode(s) return Cast(b) }
// UnmarshalJSON returns a JSON-encoded Key (string) func (k *Key) UnmarshalJSON(mk []byte) error { var s string err := json.Unmarshal(mk, &s) if err != nil { return err } *k = Key(string(b58.Decode(s))) if len(*k) == 0 && len(s) > 2 { // if b58.Decode fails, k == "" return fmt.Errorf("Key.UnmarshalJSON: invalid b58 string: %v", mk) } return nil }
func Decode(encoding, digest string) (mh.Multihash, error) { switch encoding { case "raw": return mh.Cast([]byte(digest)) case "hex": return hex.DecodeString(digest) case "base58": return base58.Decode(digest), nil case "base64": return base64.StdEncoding.DecodeString(digest) default: return nil, fmt.Errorf("unknown encoding: %s", encoding) } }
func ParseKeyToPath(txt string) (Path, error) { if txt == "" { return "", ErrNoComponents } chk := b58.Decode(txt) if len(chk) == 0 { return "", errors.New("not a key") } if _, err := mh.Cast(chk); err != nil { return "", err } return FromKey(key.Key(chk)), nil }
func (n *IpfsNode) loadID() error { if n.Identity != "" { return errors.New("identity already loaded") } cid := n.Repo.Config().Identity.PeerID if cid == "" { return errors.New("Identity was not set in config (was ipfs init run?)") } if len(cid) == 0 { return errors.New("No peer ID in config! (was ipfs init run?)") } n.Identity = peer.ID(b58.Decode(cid)) return nil }
Arguments: []cmds.Argument{ cmds.StringArg("peerid", false, false, "peer.ID of node to look up").EnableStdin(), }, Options: []cmds.Option{ cmds.StringOption("format", "f", "optional output format"), }, Run: func(req cmds.Request, res cmds.Response) { node, err := req.InvocContext().GetNode() if err != nil { res.SetError(err, cmds.ErrNormal) return } var id peer.ID if len(req.Arguments()) > 0 { id = peer.ID(b58.Decode(req.Arguments()[0])) if len(id) == 0 { res.SetError(cmds.ClientError("Invalid peer id"), cmds.ErrClient) return } } else { id = node.Identity } if id == node.Identity { output, err := printSelf(node) if err != nil { res.SetError(err, cmds.ErrNormal) return } res.SetOutput(output)
return } if len(req.Arguments()) == 0 { output, err := printSelf(node) if err != nil { res.SetError(err, cmds.ErrNormal) return } res.SetOutput(output) return } pid := req.Arguments()[0] id := peer.ID(b58.Decode(pid)) if len(id) == 0 { res.SetError(cmds.ClientError("Invalid peer id"), cmds.ErrClient) return } // TODO handle offline mode with polymorphism instead of conditionals if !node.OnlineMode() { res.SetError(errors.New(offlineIdErrorMessage), cmds.ErrClient) return } p, err := node.Routing.FindPeer(req.Context().Context, id) if err == kb.ErrLookupFailure { res.SetError(errors.New(offlineIdErrorMessage), cmds.ErrClient) return
// B58KeyDecode returns Key from a b58 encoded string func B58KeyDecode(s string) Key { return Key(string(b58.Decode(s))) }