From 3ea5c212ef53fa9a8538482fc2427f9fb057e8cc Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Thu, 26 Jul 2018 12:49:34 -0400 Subject: [PATCH] Add maps for converting from the string repr. to the code and back. --- multibase.go | 45 ++++++++++++++++++++++++++++++++++++++++++++- multibase_test.go | 15 +++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/multibase.go b/multibase.go index 4650376..478fa6d 100644 --- a/multibase.go +++ b/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") diff --git a/multibase_test.go b/multibase_test.go index a69d721..0b490df 100644 --- a/multibase_test.go +++ b/multibase_test.go @@ -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!!!",