// 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 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 }
// B58KeyDecode returns Key from a b58 encoded string func B58KeyDecode(s string) Key { return Key(string(b58.Decode(s))) }