Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
5d86ce56
Commit
5d86ce56
authored
Oct 22, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
key marshalling + b58 encoding
parent
edb5a14f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
0 deletions
+42
-0
util/key.go
util/key.go
+42
-0
No files found.
util/key.go
View file @
5d86ce56
package
util
package
util
import
(
import
(
"encoding/json"
"fmt"
b58
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
b58
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
ds
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
ds
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
mh
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
mh
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
...
@@ -15,7 +18,23 @@ func (k Key) String() string {
...
@@ -15,7 +18,23 @@ func (k Key) String() string {
}
}
// Pretty returns Key in a b58 encoded string
// Pretty returns Key in a b58 encoded string
// TODO: deprecate Pretty. bad name.
func
(
k
Key
)
Pretty
()
string
{
func
(
k
Key
)
Pretty
()
string
{
return
k
.
B58String
()
}
// B58String returns Key in a b58 encoded string
func
(
k
Key
)
B58String
()
string
{
return
B58KeyEncode
(
k
)
}
// B58KeyDecode returns Key from a b58 encoded string
func
B58KeyDecode
(
s
string
)
Key
{
return
Key
(
string
(
b58
.
Decode
(
s
)))
}
// B58KeyEncode returns Key in a b58 encoded string
func
B58KeyEncode
(
k
Key
)
string
{
return
b58
.
Encode
([]
byte
(
k
))
return
b58
.
Encode
([]
byte
(
k
))
}
}
...
@@ -24,6 +43,29 @@ func (k Key) DsKey() ds.Key {
...
@@ -24,6 +43,29 @@ func (k Key) DsKey() ds.Key {
return
ds
.
NewKey
(
string
(
k
))
return
ds
.
NewKey
(
string
(
k
))
}
}
// 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
)
}
log
.
Info
(
"Unmarshal in: %s
\"
%s
\"
"
,
*
k
,
s
)
return
nil
}
// MarshalJSON returns a JSON-encoded Key (string)
func
(
k
*
Key
)
MarshalJSON
()
([]
byte
,
error
)
{
b
,
err
:=
json
.
Marshal
(
b58
.
Encode
([]
byte
(
*
k
)))
log
.
Info
(
"Marshal out: %s %s"
,
*
k
,
b
)
return
b
,
err
}
// KeyFromDsKey returns a Datastore key
// KeyFromDsKey returns a Datastore key
func
KeyFromDsKey
(
dsk
ds
.
Key
)
Key
{
func
KeyFromDsKey
(
dsk
ds
.
Key
)
Key
{
return
Key
(
dsk
.
BaseNamespace
())
return
Key
(
dsk
.
BaseNamespace
())
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment