Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d63641945d | ||
|
|
79803cc6b5 | ||
|
|
4819336788 | ||
|
|
f25b77813c | ||
|
|
be9178df09 | ||
|
|
f7396abfab | ||
|
|
fca1c65daf | ||
|
|
0a49bd57bb | ||
|
|
5d43951a20 | ||
|
|
4cd2fef284 | ||
|
|
916e8af3d6 | ||
|
|
007b57d388 | ||
|
|
3cdc462d3f | ||
|
|
5b7719f2f5 | ||
|
|
c53ff45d4d | ||
|
|
8ab2e3688b | ||
|
|
bb91b53e56 | ||
|
|
964f55ad40 | ||
|
|
3b3047873d | ||
|
|
2170058ef9 | ||
|
|
ac3d23441b | ||
|
|
ecd5d58562 | ||
|
|
b46f1c99f0 | ||
|
|
5fb339e88a |
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
[submodule "spec"]
|
||||
path = spec
|
||||
url = https://github.com/multiformats/multibase.git
|
||||
@@ -1 +1 @@
|
||||
0.2.7: QmSbvata2WqNkqGtZNg8MR3SKwnB8iQ7vTPJgWqB8bC5kR
|
||||
0.3.0: QmekxXDhCxCJRNuzmHreuaT3BsuJcsjcXWNrtV9C8DRHtd
|
||||
|
||||
33
.travis.yml
33
.travis.yml
@@ -1,25 +1,32 @@
|
||||
os:
|
||||
- linux
|
||||
- linux
|
||||
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.10.2
|
||||
- 1.11.x
|
||||
|
||||
env:
|
||||
global:
|
||||
- GOTFLAGS="-race"
|
||||
matrix:
|
||||
- BUILD_DEPTYPE=gx
|
||||
- BUILD_DEPTYPE=gomod
|
||||
|
||||
|
||||
# disable travis install
|
||||
install:
|
||||
- go get -u github.com/whyrusleeping/gx
|
||||
- go get -u github.com/whyrusleeping/gx-go
|
||||
- gx install
|
||||
|
||||
- true
|
||||
|
||||
script:
|
||||
- gx-go rewrite
|
||||
- go test -race -coverprofile=unittest.coverprofile -covermode=atomic .
|
||||
- bash <(curl -s https://raw.githubusercontent.com/ipfs/ci-helpers/master/travis-ci/run-standard-tests.sh)
|
||||
|
||||
|
||||
after_success:
|
||||
- bash <(curl -s https://codecov.io/bash) -f unittest.coverprofile -F unittest
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- $GOPATH/src/gx
|
||||
directories:
|
||||
- $GOPATH/src/gx
|
||||
- $GOPATH/pkg/mod
|
||||
- /home/travis/.cache/go-build
|
||||
|
||||
notifications:
|
||||
email: false
|
||||
|
||||
13
Makefile
Normal file
13
Makefile
Normal file
@@ -0,0 +1,13 @@
|
||||
test: deps
|
||||
go test -race -v ./...
|
||||
|
||||
export IPFS_API ?= v04x.ipfs.io
|
||||
|
||||
gx:
|
||||
go get -u github.com/whyrusleeping/gx
|
||||
go get -u github.com/whyrusleeping/gx-go
|
||||
|
||||
deps: gx
|
||||
gx --verbose install --global
|
||||
gx-go rewrite
|
||||
go get -t ./...
|
||||
@@ -36,10 +36,6 @@ gx-go --rewrite
|
||||
|
||||
Please check [Gx](https://github.com/whyrusleeping/gx) and [Gx-go](https://github.com/whyrusleeping/gx-go) documentation for more information.
|
||||
|
||||
## Maintainers
|
||||
|
||||
Captain: [@whyrusleeping](https://github.com/whyrusleeping).
|
||||
|
||||
## Contribute
|
||||
|
||||
Contributions welcome. Please check out [the issues](https://github.com/multiformats/go-multibase/issues).
|
||||
|
||||
@@ -6,15 +6,15 @@ func hexEncodeToStringUpper(src []byte) string {
|
||||
return string(dst)
|
||||
}
|
||||
|
||||
var hextableUpper = [16]byte{
|
||||
var hexTableUppers = [16]byte{
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'A', 'B', 'C', 'D', 'E', 'F',
|
||||
}
|
||||
|
||||
func hexEncodeUpper(dst, src []byte) int {
|
||||
for i, v := range src {
|
||||
dst[i*2] = hextableUpper[v>>4]
|
||||
dst[i*2+1] = hextableUpper[v&0x0f]
|
||||
dst[i*2] = hexTableUppers[v>>4]
|
||||
dst[i*2+1] = hexTableUppers[v&0x0f]
|
||||
}
|
||||
|
||||
return len(src) * 2
|
||||
|
||||
52
base2.go
Normal file
52
base2.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package multibase
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// binaryEncodeToString takes an array of bytes and returns
|
||||
// multibase binary representation
|
||||
func binaryEncodeToString(src []byte) string {
|
||||
dst := make([]byte, len(src)*8)
|
||||
encodeBinary(dst, src)
|
||||
return string(dst)
|
||||
}
|
||||
|
||||
// encodeBinary takes the src and dst bytes and converts each
|
||||
// byte to their binary rep using power reduction method
|
||||
func encodeBinary(dst []byte, src []byte) {
|
||||
for i, b := range src {
|
||||
for j := 0; j < 8; j++ {
|
||||
if b&(1<<uint(7-j)) == 0 {
|
||||
dst[i*8+j] = '0'
|
||||
} else {
|
||||
dst[i*8+j] = '1'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// decodeBinaryString takes multibase binary representation
|
||||
// and returns a byte array
|
||||
func decodeBinaryString(s string) ([]byte, error) {
|
||||
if len(s)&7 != 0 {
|
||||
// prepend the padding
|
||||
s = strings.Repeat("0", 8-len(s)&7) + s
|
||||
}
|
||||
|
||||
data := make([]byte, len(s)>>3)
|
||||
|
||||
for i, dstIndex := 0, 0; i < len(s); i = i + 8 {
|
||||
value, err := strconv.ParseInt(s[i:i+8], 2, 0)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error while conversion: %s", err)
|
||||
}
|
||||
|
||||
data[dstIndex] = byte(value)
|
||||
dstIndex++
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package multibase
|
||||
|
||||
import (
|
||||
b32 "github.com/whyrusleeping/base32"
|
||||
b32 "github.com/multiformats/go-base32"
|
||||
)
|
||||
|
||||
var base32StdLowerPad = b32.NewEncodingCI("abcdefghijklmnopqrstuvwxyz234567")
|
||||
|
||||
10
encoder.go
10
encoder.go
@@ -19,6 +19,16 @@ func NewEncoder(base Encoding) (Encoder, error) {
|
||||
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) {
|
||||
|
||||
@@ -4,30 +4,48 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestInvalidPrefix(t *testing.T) {
|
||||
func TestInvalidCode(t *testing.T) {
|
||||
_, err := NewEncoder('q')
|
||||
if err == nil {
|
||||
t.Error("expected failure")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrefix(t *testing.T) {
|
||||
for str, base := range Encodings {
|
||||
prefix, err := NewEncoder(base)
|
||||
if err != nil {
|
||||
t.Fatalf("NewEncoder(%c) failed: %v", base, err)
|
||||
}
|
||||
str1, err := Encode(base, sampleBytes)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
str2 := prefix.Encode(sampleBytes)
|
||||
if str1 != str2 {
|
||||
t.Errorf("encoded string mismatch: %s != %s", str1, str2)
|
||||
}
|
||||
_, err = EncoderByName(str)
|
||||
if err != nil {
|
||||
t.Fatalf("NewEncoder(%s) failed: %v", str, err)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
6
go.mod
Normal file
6
go.mod
Normal file
@@ -0,0 +1,6 @@
|
||||
module github.com/multiformats/go-multibase
|
||||
|
||||
require (
|
||||
github.com/mr-tron/base58 v1.1.0
|
||||
github.com/multiformats/go-base32 v0.0.3
|
||||
)
|
||||
4
go.sum
Normal file
4
go.sum
Normal file
@@ -0,0 +1,4 @@
|
||||
github.com/mr-tron/base58 v1.1.0 h1:Y51FGVJ91WBqCEabAi5OPUz38eAx8DakuAm5svLcsfQ=
|
||||
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
|
||||
github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp8Nq/kkI=
|
||||
github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA=
|
||||
@@ -14,8 +14,8 @@ func main() {
|
||||
}
|
||||
|
||||
var newBase multibase.Encoding
|
||||
if baseParm := os.Args[1]; len(baseParm) != 0 {
|
||||
newBase = multibase.Encoding(baseParm[0])
|
||||
if baseParam := os.Args[1]; len(baseParam) != 0 {
|
||||
newBase = multibase.Encoding(baseParam[0])
|
||||
} else {
|
||||
fmt.Fprintln(os.Stderr, "<new-base> is empty")
|
||||
os.Exit(1)
|
||||
|
||||
11
multibase.go
11
multibase.go
@@ -6,7 +6,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
b58 "github.com/mr-tron/base58/base58"
|
||||
b32 "github.com/whyrusleeping/base32"
|
||||
b32 "github.com/multiformats/go-base32"
|
||||
)
|
||||
|
||||
// Encoding identifies the type of base-encoding that a multibase is carrying.
|
||||
@@ -38,10 +38,11 @@ const (
|
||||
Base64urlPad = 'U'
|
||||
)
|
||||
|
||||
// Encodigs is a map of the supported encoding, unsupported encoding
|
||||
// Encodings is a map of the supported encoding, unsupported encoding
|
||||
// specified in standard are left out
|
||||
var Encodings = map[string]Encoding{
|
||||
"identity": 0x00,
|
||||
"base2": '0',
|
||||
"base16": 'f',
|
||||
"base16upper": 'F',
|
||||
"base32": 'b',
|
||||
@@ -62,6 +63,7 @@ var Encodings = map[string]Encoding{
|
||||
|
||||
var EncodingToStr = map[Encoding]string{
|
||||
0x00: "identity",
|
||||
'0': "base2",
|
||||
'f': "base16",
|
||||
'F': "base16upper",
|
||||
'b': "base32",
|
||||
@@ -92,6 +94,8 @@ func Encode(base Encoding, data []byte) (string, error) {
|
||||
case Identity:
|
||||
// 0x00 inside a string is OK in golang and causes no problems with the length calculation.
|
||||
return string(Identity) + string(data), nil
|
||||
case Base2:
|
||||
return string(Base2) + binaryEncodeToString(data), nil
|
||||
case Base16:
|
||||
return string(Base16) + hex.EncodeToString(data), nil
|
||||
case Base16Upper:
|
||||
@@ -141,6 +145,9 @@ func Decode(data string) (Encoding, []byte, error) {
|
||||
switch enc {
|
||||
case Identity:
|
||||
return Identity, []byte(data[1:]), nil
|
||||
case Base2:
|
||||
bytes, err := decodeBinaryString(data[1:])
|
||||
return enc, bytes, err
|
||||
case Base16, Base16Upper:
|
||||
bytes, err := hex.DecodeString(data[1:])
|
||||
return enc, bytes, err
|
||||
|
||||
@@ -7,13 +7,13 @@ import (
|
||||
)
|
||||
|
||||
func TestMap(t *testing.T) {
|
||||
for s,e := range Encodings {
|
||||
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 {
|
||||
for e, s := range EncodingToStr {
|
||||
e2 := Encodings[s]
|
||||
if e != e2 {
|
||||
t.Errorf("round trip failed on encoding map: '%c' != '%c'", e, e2)
|
||||
@@ -24,6 +24,7 @@ func TestMap(t *testing.T) {
|
||||
var sampleBytes = []byte("Decentralize everything!!!")
|
||||
var encodedSamples = map[Encoding]string{
|
||||
Identity: string(0x00) + "Decentralize everything!!!",
|
||||
Base2: "00100010001100101011000110110010101101110011101000111001001100001011011000110100101111010011001010010000001100101011101100110010101110010011110010111010001101000011010010110111001100111001000010010000100100001",
|
||||
Base16: "f446563656e7472616c697a652065766572797468696e67212121",
|
||||
Base16Upper: "F446563656E7472616C697A652065766572797468696E67212121",
|
||||
Base32: "birswgzloorzgc3djpjssazlwmvzhs5dinfxgoijbee",
|
||||
@@ -35,6 +36,8 @@ var encodedSamples = map[Encoding]string{
|
||||
Base32hexPad: "t8him6pbeehp62r39f9ii0pbmclp7it38d5n6e89144======",
|
||||
Base32hexPadUpper: "T8HIM6PBEEHP62R39F9II0PBMCLP7IT38D5N6E89144======",
|
||||
Base58BTC: "z36UQrhJq9fNDS7DiAHM9YXqDHMPfr4EMArvt",
|
||||
Base64: "mRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE",
|
||||
Base64url: "uRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE",
|
||||
Base64pad: "MRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE=",
|
||||
Base64urlPad: "URGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE=",
|
||||
}
|
||||
@@ -115,6 +118,7 @@ func BenchmarkRoundTrip(b *testing.B) {
|
||||
|
||||
bases := map[string]Encoding{
|
||||
"Identity": Identity,
|
||||
"Base2": Base2,
|
||||
"Base16": Base16,
|
||||
"Base16Upper": Base16Upper,
|
||||
"Base32": Base32,
|
||||
|
||||
14
package.json
14
package.json
@@ -7,17 +7,17 @@
|
||||
"dvcsimport": "github.com/multiformats/go-multibase"
|
||||
},
|
||||
"gxDependencies": [
|
||||
{
|
||||
"author": "whyrusleeping",
|
||||
"hash": "QmfVj3x4D6Jkq9SEoi5n2NmoUomLwoeiwnYz2KQa15wRw6",
|
||||
"name": "base32",
|
||||
"version": "0.0.2"
|
||||
},
|
||||
{
|
||||
"author": "mr-tron",
|
||||
"hash": "QmWFAMPqsEyUX7gDUsRVmMWz59FxSpJ1b2v6bJ1yYzo7jY",
|
||||
"name": "go-base58-fast",
|
||||
"version": "0.1.1"
|
||||
},
|
||||
{
|
||||
"author": "Golang",
|
||||
"hash": "QmPbbYin7KBd1Y1BfUe15vHzwJiioyi3wtKQTtXWWf8SC5",
|
||||
"name": "base32",
|
||||
"version": "0.0.3"
|
||||
}
|
||||
],
|
||||
"gxVersion": "0.8.0",
|
||||
@@ -25,6 +25,6 @@
|
||||
"license": "",
|
||||
"name": "go-multibase",
|
||||
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
|
||||
"version": "0.2.7"
|
||||
"version": "0.3.0"
|
||||
}
|
||||
|
||||
|
||||
1
spec
Submodule
1
spec
Submodule
Submodule spec added at f0792f7030
147
spec_test.go
Normal file
147
spec_test.go
Normal file
@@ -0,0 +1,147 @@
|
||||
package multibase
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func TestSpec(t *testing.T) {
|
||||
file, err := os.Open("spec/multibase.csv")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
reader := csv.NewReader(file)
|
||||
reader.LazyQuotes = false
|
||||
reader.FieldsPerRecord = 3
|
||||
reader.TrimLeadingSpace = true
|
||||
|
||||
values, err := reader.ReadAll()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
expectedEncodings := make(map[Encoding]string, len(values)-1)
|
||||
for _, v := range values[1:] {
|
||||
encoding := v[0]
|
||||
codeStr := v[1]
|
||||
|
||||
var code Encoding
|
||||
if strings.HasPrefix(codeStr, "0x") {
|
||||
i, err := strconv.ParseUint(codeStr[2:], 16, 64)
|
||||
if err != nil {
|
||||
t.Errorf("invalid multibase byte %q", codeStr)
|
||||
continue
|
||||
}
|
||||
code = Encoding(i)
|
||||
} else {
|
||||
codeRune, length := utf8.DecodeRuneInString(codeStr)
|
||||
if code == utf8.RuneError {
|
||||
t.Errorf("multibase %q wasn't valid utf8", codeStr)
|
||||
continue
|
||||
}
|
||||
if length != len(codeStr) {
|
||||
t.Errorf("multibase %q wasn't a single character", codeStr)
|
||||
continue
|
||||
}
|
||||
code = Encoding(codeRune)
|
||||
}
|
||||
expectedEncodings[code] = encoding
|
||||
}
|
||||
|
||||
for name, enc := range Encodings {
|
||||
expectedName, ok := expectedEncodings[enc]
|
||||
if !ok {
|
||||
t.Errorf("encoding %q (%c) not defined in the spec", name, enc)
|
||||
continue
|
||||
}
|
||||
if expectedName != name {
|
||||
t.Errorf("encoding %q (%c) has unexpected name %q", expectedName, enc, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestSpecVectors(t *testing.T) {
|
||||
files, err := filepath.Glob("spec/tests/test[0-9]*.csv")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, fname := range files {
|
||||
t.Run(fname, func(t *testing.T) {
|
||||
file, err := os.Open(fname)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
reader := csv.NewReader(file)
|
||||
reader.LazyQuotes = false
|
||||
reader.FieldsPerRecord = 2
|
||||
reader.TrimLeadingSpace = true
|
||||
|
||||
values, err := reader.ReadAll()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if len(values) == 0 {
|
||||
t.Error("no test values")
|
||||
return
|
||||
}
|
||||
header := values[0]
|
||||
|
||||
var decodeOnly bool
|
||||
switch header[0] {
|
||||
case "encoding":
|
||||
case "non-canonical encoding":
|
||||
decodeOnly = true
|
||||
default:
|
||||
t.Errorf("invalid test spec %q", fname)
|
||||
return
|
||||
}
|
||||
|
||||
testValue, err := strconv.Unquote("\"" + header[1] + "\"")
|
||||
if err != nil {
|
||||
t.Error("failed to unquote testcase:", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, testCase := range values[1:] {
|
||||
encodingName := testCase[0]
|
||||
expected := testCase[1]
|
||||
|
||||
t.Run(encodingName, func(t *testing.T) {
|
||||
encoder, err := EncoderByName(encodingName)
|
||||
if err != nil {
|
||||
t.Skipf("skipping %s: not supported", encodingName)
|
||||
return
|
||||
}
|
||||
if !decodeOnly {
|
||||
t.Logf("encoding %q with %s", testValue, encodingName)
|
||||
actual := encoder.Encode([]byte(testValue))
|
||||
if expected != actual {
|
||||
t.Errorf("expected %q, got %q", expected, actual)
|
||||
}
|
||||
}
|
||||
t.Logf("decoding %q", expected)
|
||||
encoding, decoded, err := Decode(expected)
|
||||
if err != nil {
|
||||
t.Error("failed to decode:", err)
|
||||
return
|
||||
}
|
||||
expectedEncoding := Encodings[encodingName]
|
||||
if encoding != expectedEncoding {
|
||||
t.Errorf("expected encoding to be %c, got %c", expectedEncoding, encoding)
|
||||
}
|
||||
if string(decoded) != testValue {
|
||||
t.Errorf("failed to decode %q to %q, got %q", expected, testValue, string(decoded))
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user