Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bb91b53e56 | ||
|
|
964f55ad40 | ||
|
|
3b3047873d | ||
|
|
2170058ef9 | ||
|
|
ac3d23441b | ||
|
|
ecd5d58562 | ||
|
|
b46f1c99f0 | ||
|
|
5fb339e88a | ||
|
|
caebba6233 | ||
|
|
83915a874d | ||
|
|
03643c33f5 | ||
|
|
2eb83a994b | ||
|
|
5547437445 | ||
|
|
a0557075ec | ||
|
|
3ea5c212ef |
@@ -1 +1 @@
|
||||
0.2.6: QmexBtiTTEwwn42Yi6ouKt6VqzpA6wjJgiW1oh9VfaRrup
|
||||
0.3.0: QmekxXDhCxCJRNuzmHreuaT3BsuJcsjcXWNrtV9C8DRHtd
|
||||
|
||||
@@ -4,7 +4,7 @@ os:
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.8.3
|
||||
- 1.10.2
|
||||
|
||||
install:
|
||||
- go get -u github.com/whyrusleeping/gx
|
||||
|
||||
63
encoder.go
Normal file
63
encoder.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package multibase
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Encoder is a multibase encoding that is verified to be supported and
|
||||
// supports an Encode method that does not return an error
|
||||
type Encoder struct {
|
||||
enc Encoding
|
||||
}
|
||||
|
||||
// NewEncoder create a new Encoder from an Encoding
|
||||
func NewEncoder(base Encoding) (Encoder, error) {
|
||||
_, ok := EncodingToStr[base]
|
||||
if !ok {
|
||||
return Encoder{-1}, fmt.Errorf("Unsupported multibase encoding: %d", base)
|
||||
}
|
||||
return Encoder{base}, nil
|
||||
}
|
||||
|
||||
// MustNewEncoder is like NewEncoder but will panic if the encoding is
|
||||
// invalid.
|
||||
func MustNewEncoder(base Encoding) Encoder {
|
||||
_, ok := EncodingToStr[base]
|
||||
if !ok {
|
||||
panic("Unsupported multibase encoding")
|
||||
}
|
||||
return Encoder{base}
|
||||
}
|
||||
|
||||
// EncoderByName creates an encoder from a string, the string can
|
||||
// either be the multibase name or single character multibase prefix
|
||||
func EncoderByName(str string) (Encoder, error) {
|
||||
var base Encoding
|
||||
ok := true
|
||||
if len(str) == 0 {
|
||||
return Encoder{-1}, fmt.Errorf("Empty multibase encoding")
|
||||
} else if len(str) == 1 {
|
||||
base = Encoding(str[0])
|
||||
_, ok = EncodingToStr[base]
|
||||
} else {
|
||||
base, ok = Encodings[str]
|
||||
}
|
||||
if !ok {
|
||||
return Encoder{-1}, fmt.Errorf("Unsupported multibase encoding: %s", str)
|
||||
}
|
||||
return Encoder{base}, nil
|
||||
}
|
||||
|
||||
func (p Encoder) Encoding() Encoding {
|
||||
return p.enc
|
||||
}
|
||||
|
||||
// Encode encodes the multibase using the given Encoder.
|
||||
func (p Encoder) Encode(data []byte) string {
|
||||
str, err := Encode(p.enc, data)
|
||||
if err != nil {
|
||||
// should not happen
|
||||
panic(err)
|
||||
}
|
||||
return str
|
||||
}
|
||||
51
encoder_test.go
Normal file
51
encoder_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package multibase
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestInvalidCode(t *testing.T) {
|
||||
_, err := NewEncoder('q')
|
||||
if err == nil {
|
||||
t.Error("expected failure")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidName(t *testing.T) {
|
||||
values := []string{"invalid", "", "q"}
|
||||
for _, val := range values {
|
||||
_, err := EncoderByName(val)
|
||||
if err == nil {
|
||||
t.Errorf("EncoderByName(%v) expected failure", val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncoder(t *testing.T) {
|
||||
for name, code := range Encodings {
|
||||
encoder, err := NewEncoder(code)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Make sure the MustNewEncoder doesn't panic
|
||||
MustNewEncoder(code)
|
||||
str, err := Encode(code, sampleBytes)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
str2 := encoder.Encode(sampleBytes)
|
||||
if str != str2 {
|
||||
t.Errorf("encoded string mismatch: %s != %s", str, str2)
|
||||
}
|
||||
_, err = EncoderByName(name)
|
||||
if err != nil {
|
||||
t.Fatalf("EncoderByName(%s) failed: %v", name, err)
|
||||
}
|
||||
// Test that an encoder can be created from the single letter
|
||||
// prefix
|
||||
_, err = EncoderByName(str[0:1])
|
||||
if err != nil {
|
||||
t.Fatalf("EncoderByName(%s) failed: %v", str[0:1], err)
|
||||
}
|
||||
}
|
||||
}
|
||||
45
multibase.go
45
multibase.go
@@ -12,7 +12,8 @@ import (
|
||||
// Encoding identifies the type of base-encoding that a multibase is carrying.
|
||||
type Encoding int
|
||||
|
||||
// These are the supported encodings
|
||||
// These are the encodings specified in the standard, not are all
|
||||
// supported yet
|
||||
const (
|
||||
Identity = 0x00
|
||||
Base1 = '1'
|
||||
@@ -37,6 +38,48 @@ const (
|
||||
Base64urlPad = 'U'
|
||||
)
|
||||
|
||||
// Encodigs is a map of the supported encoding, unsupported encoding
|
||||
// specified in standard are left out
|
||||
var Encodings = map[string]Encoding{
|
||||
"identity": 0x00,
|
||||
"base16": 'f',
|
||||
"base16upper": 'F',
|
||||
"base32": 'b',
|
||||
"base32upper": 'B',
|
||||
"base32pad": 'c',
|
||||
"base32padupper": 'C',
|
||||
"base32hex": 'v',
|
||||
"base32hexupper": 'V',
|
||||
"base32hexpad": 't',
|
||||
"base32hexpadupper": 'T',
|
||||
"base58flickr": 'Z',
|
||||
"base58btc": 'z',
|
||||
"base64": 'm',
|
||||
"base64url": 'u',
|
||||
"base64pad": 'M',
|
||||
"base64urlpad": 'U',
|
||||
}
|
||||
|
||||
var EncodingToStr = map[Encoding]string{
|
||||
0x00: "identity",
|
||||
'f': "base16",
|
||||
'F': "base16upper",
|
||||
'b': "base32",
|
||||
'B': "base32upper",
|
||||
'c': "base32pad",
|
||||
'C': "base32padupper",
|
||||
'v': "base32hex",
|
||||
'V': "base32hexupper",
|
||||
't': "base32hexpad",
|
||||
'T': "base32hexpadupper",
|
||||
'Z': "base58flickr",
|
||||
'z': "base58btc",
|
||||
'm': "base64",
|
||||
'u': "base64url",
|
||||
'M': "base64pad",
|
||||
'U': "base64urlpad",
|
||||
}
|
||||
|
||||
// ErrUnsupportedEncoding is returned when the selected encoding is not known or
|
||||
// implemented.
|
||||
var ErrUnsupportedEncoding = fmt.Errorf("selected encoding not supported")
|
||||
|
||||
@@ -6,6 +6,21 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMap(t *testing.T) {
|
||||
for s, e := range Encodings {
|
||||
s2 := EncodingToStr[e]
|
||||
if s != s2 {
|
||||
t.Errorf("round trip failed on encoding map: %s != %s", s, s2)
|
||||
}
|
||||
}
|
||||
for e, s := range EncodingToStr {
|
||||
e2 := Encodings[s]
|
||||
if e != e2 {
|
||||
t.Errorf("round trip failed on encoding map: '%c' != '%c'", e, e2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var sampleBytes = []byte("Decentralize everything!!!")
|
||||
var encodedSamples = map[Encoding]string{
|
||||
Identity: string(0x00) + "Decentralize everything!!!",
|
||||
@@ -20,6 +35,8 @@ var encodedSamples = map[Encoding]string{
|
||||
Base32hexPad: "t8him6pbeehp62r39f9ii0pbmclp7it38d5n6e89144======",
|
||||
Base32hexPadUpper: "T8HIM6PBEEHP62R39F9II0PBMCLP7IT38D5N6E89144======",
|
||||
Base58BTC: "z36UQrhJq9fNDS7DiAHM9YXqDHMPfr4EMArvt",
|
||||
Base64: "mRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE",
|
||||
Base64url: "uRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE",
|
||||
Base64pad: "MRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE=",
|
||||
Base64urlPad: "URGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE=",
|
||||
}
|
||||
|
||||
@@ -25,6 +25,6 @@
|
||||
"license": "",
|
||||
"name": "go-multibase",
|
||||
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
|
||||
"version": "0.2.6"
|
||||
"version": "0.3.0"
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user