func FindPeer(n *core.IpfsNode, cmdparts []string) (string, error) { out := new(bytes.Buffer) if len(cmdparts) < 3 { return fmt.Sprintln("findpeer: '# findpeer peerid'"), ErrArgCount } var search peer.ID if cmdparts[2][0] == '$' { n, err := strconv.Atoi(cmdparts[2][1:]) if err != nil { return "", err } if n >= len(controllers) { return "", errors.New("specified peernum out of range") } search = controllers[n].PeerID() } else { search = peer.ID(b58.Decode(cmdparts[2])) } fmt.Fprintf(out, "Searching for peer: %s\n", search) ctx, _ := context.WithDeadline(context.TODO(), time.Now().Add(time.Second*5)) p, err := n.Routing.FindPeer(ctx, search) if err != nil { return "", err } fmt.Fprintf(out, "%Got peer: %s\n", p) return out.String(), nil }
// 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 }
func parseOpts() (options, error) { o := options{} flag.StringVar(&o.keyS, "k", "", "key to use (in base58)") flag.BoolVar(&o.encrypt, "e", false, "encrypt mode") flag.BoolVar(&o.decrypt, "d", false, "decrypt mode") flag.BoolVar(&o.keygen, "key-gen", false, "generate a key") flag.Usage = usage flag.Parse() if !onlyOne(o.keygen, o.encrypt, o.decrypt) { return o, fmt.Errorf("must choose either -e or -d or --key-gen") } if !o.keygen { o.keyS = strings.TrimSpace(o.keyS) o.key = b58.Decode(o.keyS) if o.keyS == "" || o.key == nil { return o, fmt.Errorf("must provide a key in base58 with -k") } if len(o.key) < senc.KeyLength { return o, fmt.Errorf("key too short. must be 256 bits, decoded.") } else if len(o.key) > senc.KeyLength { return o, fmt.Errorf("key too short. must be 256 bits, decoded.") } } return o, nil }
func streamCipherReaderForKey(key string, r io.ReadCloser) (io.ReadCloser, error) { keyData := base58.Decode(key) if len(keyData) == 0 { return nil, ErrInvalidKey } return streamCipherReaderForKeyData(keyData[0], keyData[1:], r) }
// 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 }
// 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 readEntry(scanner *bufio.Scanner) (ent Entry, ent_hash string) { line := scanner.Text() if len(line) > 0 && line == "-" { // New style entries var key, val string for { if !scanner.Scan() { break } line = scanner.Text() if line == "" { break } if line[0] == '\t' { val += "\n" + line[1:] } else { if key != "" { readEntryAttr(&ent, key, val) key = "" } s := strings.SplitN(line, "=", 2) if len(s) == 2 { key = s[0] val = s[1] } } } if key != "" { readEntryAttr(&ent, key, val) } } else { elems := strings.SplitN(line, "\t", 2) f := DecodePath(elems[1]) ent = Entry{ Path: f, Hash: []byte(elems[0]), } } if ent.Path != "" { ent_hash = string(ent.Hash) ent.Hash = base58.Decode(ent_hash) } return }
func readEntryAttr(ent *Entry, key, val string) { switch key { case "p": ent.Path = val break case "h": ent.Hash = base58.Decode(val) break case "u": ent.Uuid = val break case "I": ent.Device = StringDevice(val) ent.Inode = StringInode(val) break default: break } }
// B58KeyDecode returns Key from a b58 encoded string func B58KeyDecode(s string) Key { return Key(string(b58.Decode(s))) }