Compare commits
39 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
701f9d5222 | ||
|
|
eabda15156 | ||
|
|
766dbc86ae | ||
|
|
44b28418c8 | ||
|
|
989fa114c3 | ||
|
|
02ed95de81 | ||
|
|
a0303a6f45 | ||
|
|
8c53981821 | ||
|
|
8b61179671 | ||
|
|
58a41f7df1 | ||
|
|
a791fa9589 | ||
|
|
c36c36f88b | ||
|
|
3bbbe822b0 | ||
|
|
b85d20169d | ||
|
|
8170d966f8 | ||
|
|
c442d70f87 | ||
|
|
7160a7347e | ||
|
|
c1bc15b22e | ||
|
|
801f90a945 | ||
|
|
6e0e420356 | ||
|
|
df5b7bc6ee | ||
|
|
0bd72a8c32 | ||
|
|
714f5c0130 | ||
|
|
f067816be3 | ||
|
|
66b6f5f82a | ||
|
|
c1e5d4e95b | ||
|
|
f68598dd02 | ||
|
|
daffaef0db | ||
|
|
95cb7074c4 | ||
|
|
2985033078 | ||
|
|
e2260b5ff3 | ||
|
|
158c1deff1 | ||
|
|
ee5c23343b | ||
|
|
aa5d547a81 | ||
|
|
c03399abc2 | ||
|
|
f5fced06c2 | ||
|
|
be9e91119a | ||
|
|
6519131ca4 | ||
|
|
ef04c6a3db |
14
.github/workflows/generated-pr.yml
vendored
Normal file
14
.github/workflows/generated-pr.yml
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
name: Close Generated PRs
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 0 * * *'
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
issues: write
|
||||
pull-requests: write
|
||||
|
||||
jobs:
|
||||
stale:
|
||||
uses: ipdxco/unified-github-workflows/.github/workflows/reusable-generated-pr.yml@v1
|
||||
18
.github/workflows/go-check.yml
vendored
Normal file
18
.github/workflows/go-check.yml
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
name: Go Checks
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
push:
|
||||
branches: ["master"]
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event_name == 'push' && github.sha || github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
go-check:
|
||||
uses: ipdxco/unified-github-workflows/.github/workflows/go-check.yml@v1.0
|
||||
20
.github/workflows/go-test.yml
vendored
Normal file
20
.github/workflows/go-test.yml
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
name: Go Test
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
push:
|
||||
branches: ["master"]
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event_name == 'push' && github.sha || github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
go-test:
|
||||
uses: ipdxco/unified-github-workflows/.github/workflows/go-test.yml@v1.0
|
||||
secrets:
|
||||
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
|
||||
19
.github/workflows/release-check.yml
vendored
Normal file
19
.github/workflows/release-check.yml
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
name: Release Checker
|
||||
|
||||
on:
|
||||
pull_request_target:
|
||||
paths: [ 'version.json' ]
|
||||
types: [ opened, synchronize, reopened, labeled, unlabeled ]
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
pull-requests: write
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
release-check:
|
||||
uses: ipdxco/unified-github-workflows/.github/workflows/release-check.yml@v1.0
|
||||
17
.github/workflows/releaser.yml
vendored
Normal file
17
.github/workflows/releaser.yml
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
name: Releaser
|
||||
|
||||
on:
|
||||
push:
|
||||
paths: [ 'version.json' ]
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.sha }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
releaser:
|
||||
uses: ipdxco/unified-github-workflows/.github/workflows/releaser.yml@v1.0
|
||||
14
.github/workflows/stale.yml
vendored
Normal file
14
.github/workflows/stale.yml
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
name: Close Stale Issues
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 0 * * *'
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
issues: write
|
||||
pull-requests: write
|
||||
|
||||
jobs:
|
||||
stale:
|
||||
uses: ipdxco/unified-github-workflows/.github/workflows/reusable-stale-issue.yml@v1
|
||||
18
.github/workflows/tagpush.yml
vendored
Normal file
18
.github/workflows/tagpush.yml
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
name: Tag Push Checker
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- v*
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
issues: write
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
releaser:
|
||||
uses: ipdxco/unified-github-workflows/.github/workflows/tagpush.yml@v1.0
|
||||
@@ -1 +0,0 @@
|
||||
0.3.0: QmekxXDhCxCJRNuzmHreuaT3BsuJcsjcXWNrtV9C8DRHtd
|
||||
32
.travis.yml
32
.travis.yml
@@ -1,32 +0,0 @@
|
||||
os:
|
||||
- linux
|
||||
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.11.x
|
||||
|
||||
env:
|
||||
global:
|
||||
- GOTFLAGS="-race"
|
||||
matrix:
|
||||
- BUILD_DEPTYPE=gx
|
||||
- BUILD_DEPTYPE=gomod
|
||||
|
||||
|
||||
# disable travis install
|
||||
install:
|
||||
- true
|
||||
|
||||
script:
|
||||
- bash <(curl -s https://raw.githubusercontent.com/ipfs/ci-helpers/master/travis-ci/run-standard-tests.sh)
|
||||
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- $GOPATH/src/gx
|
||||
- $GOPATH/pkg/mod
|
||||
- /home/travis/.cache/go-build
|
||||
|
||||
notifications:
|
||||
email: false
|
||||
10
Makefile
10
Makefile
@@ -1,13 +1,7 @@
|
||||
test: deps
|
||||
go test -race -v ./...
|
||||
go test -count=1 -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
|
||||
deps:
|
||||
go get -t ./...
|
||||
|
||||
18
README.md
18
README.md
@@ -18,24 +18,6 @@
|
||||
go get github.com/multiformats/go-multibase
|
||||
```
|
||||
|
||||
Note that `go-multibase` is packaged with Gx, so it is recommended to use Gx to install and use it (see Usage section).
|
||||
|
||||
## Usage
|
||||
|
||||
This module is packaged with [Gx](https://github.com/whyrusleeping/gx). In order to use it in your own project it is recommended that you:
|
||||
|
||||
```sh
|
||||
go get -u github.com/whyrusleeping/gx
|
||||
go get -u github.com/whyrusleeping/gx-go
|
||||
cd <your-project-repository>
|
||||
gx init
|
||||
gx import github.com/multiformats/go-multibase
|
||||
gx install --global
|
||||
gx-go --rewrite
|
||||
```
|
||||
|
||||
Please check [Gx](https://github.com/whyrusleeping/gx) and [Gx-go](https://github.com/whyrusleeping/gx-go) documentation for more information.
|
||||
|
||||
## Contribute
|
||||
|
||||
Contributions welcome. Please check out [the issues](https://github.com/multiformats/go-multibase/issues).
|
||||
|
||||
95
base256emoji.go
Normal file
95
base256emoji.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package multibase
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
var base256emojiTable = [256]rune{
|
||||
// Curated list, this is just a list of things that *somwhat* are related to our comunity
|
||||
'🚀', '🪐', '☄', '🛰', '🌌', // Space
|
||||
'🌑', '🌒', '🌓', '🌔', '🌕', '🌖', '🌗', '🌘', // Moon
|
||||
'🌍', '🌏', '🌎', // Our Home, for now (earth)
|
||||
'🐉', // Dragon!!!
|
||||
'☀', // Our Garden, for now (sol)
|
||||
'💻', '🖥', '💾', '💿', // Computer
|
||||
// The rest is completed from https://home.unicode.org/emoji/emoji-frequency/ at the time of creation (december 2021) (the data is from 2019), most used first until we reach 256.
|
||||
// We exclude modifier based emojies (such as flags) as they are bigger than one single codepoint.
|
||||
// Some other emojies were removed adhoc for various reasons.
|
||||
'😂', '❤', '😍', '🤣', '😊', '🙏', '💕', '😭', '😘', '👍',
|
||||
'😅', '👏', '😁', '🔥', '🥰', '💔', '💖', '💙', '😢', '🤔',
|
||||
'😆', '🙄', '💪', '😉', '☺', '👌', '🤗', '💜', '😔', '😎',
|
||||
'😇', '🌹', '🤦', '🎉', '💞', '✌', '✨', '🤷', '😱', '😌',
|
||||
'🌸', '🙌', '😋', '💗', '💚', '😏', '💛', '🙂', '💓', '🤩',
|
||||
'😄', '😀', '🖤', '😃', '💯', '🙈', '👇', '🎶', '😒', '🤭',
|
||||
'❣', '😜', '💋', '👀', '😪', '😑', '💥', '🙋', '😞', '😩',
|
||||
'😡', '🤪', '👊', '🥳', '😥', '🤤', '👉', '💃', '😳', '✋',
|
||||
'😚', '😝', '😴', '🌟', '😬', '🙃', '🍀', '🌷', '😻', '😓',
|
||||
'⭐', '✅', '🥺', '🌈', '😈', '🤘', '💦', '✔', '😣', '🏃',
|
||||
'💐', '☹', '🎊', '💘', '😠', '☝', '😕', '🌺', '🎂', '🌻',
|
||||
'😐', '🖕', '💝', '🙊', '😹', '🗣', '💫', '💀', '👑', '🎵',
|
||||
'🤞', '😛', '🔴', '😤', '🌼', '😫', '⚽', '🤙', '☕', '🏆',
|
||||
'🤫', '👈', '😮', '🙆', '🍻', '🍃', '🐶', '💁', '😲', '🌿',
|
||||
'🧡', '🎁', '⚡', '🌞', '🎈', '❌', '✊', '👋', '😰', '🤨',
|
||||
'😶', '🤝', '🚶', '💰', '🍓', '💢', '🤟', '🙁', '🚨', '💨',
|
||||
'🤬', '✈', '🎀', '🍺', '🤓', '😙', '💟', '🌱', '😖', '👶',
|
||||
'🥴', '▶', '➡', '❓', '💎', '💸', '⬇', '😨', '🌚', '🦋',
|
||||
'😷', '🕺', '⚠', '🙅', '😟', '😵', '👎', '🤲', '🤠', '🤧',
|
||||
'📌', '🔵', '💅', '🧐', '🐾', '🍒', '😗', '🤑', '🌊', '🤯',
|
||||
'🐷', '☎', '💧', '😯', '💆', '👆', '🎤', '🙇', '🍑', '❄',
|
||||
'🌴', '💣', '🐸', '💌', '📍', '🥀', '🤢', '👅', '💡', '💩',
|
||||
'👐', '📸', '👻', '🤐', '🤮', '🎼', '🥵', '🚩', '🍎', '🍊',
|
||||
'👼', '💍', '📣', '🥂',
|
||||
}
|
||||
|
||||
var base256emojiReverseTable map[rune]byte
|
||||
|
||||
func init() {
|
||||
base256emojiReverseTable = make(map[rune]byte, len(base256emojiTable))
|
||||
for i, v := range base256emojiTable {
|
||||
base256emojiReverseTable[v] = byte(i)
|
||||
}
|
||||
}
|
||||
|
||||
func base256emojiEncode(in []byte) string {
|
||||
var l int
|
||||
for _, v := range in {
|
||||
l += utf8.RuneLen(base256emojiTable[v])
|
||||
}
|
||||
var out strings.Builder
|
||||
out.Grow(l)
|
||||
for _, v := range in {
|
||||
out.WriteRune(base256emojiTable[v])
|
||||
}
|
||||
return out.String()
|
||||
}
|
||||
|
||||
type base256emojiCorruptInputError struct {
|
||||
index int
|
||||
char rune
|
||||
}
|
||||
|
||||
func (e base256emojiCorruptInputError) Error() string {
|
||||
return "illegal base256emoji data at input byte " + strconv.FormatInt(int64(e.index), 10) + ", char: '" + string(e.char) + "'"
|
||||
}
|
||||
|
||||
func (e base256emojiCorruptInputError) String() string {
|
||||
return e.Error()
|
||||
}
|
||||
|
||||
func base256emojiDecode(in string) ([]byte, error) {
|
||||
out := make([]byte, utf8.RuneCountInString(in))
|
||||
var stri int
|
||||
for i := 0; len(in) > 0; i++ {
|
||||
r, n := utf8.DecodeRuneInString(in)
|
||||
in = in[n:]
|
||||
var ok bool
|
||||
out[i], ok = base256emojiReverseTable[r]
|
||||
if !ok {
|
||||
return nil, base256emojiCorruptInputError{stri, r}
|
||||
}
|
||||
stri += n
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
26
base256emoji_test.go
Normal file
26
base256emoji_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package multibase
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestBase256EmojiAlphabet(t *testing.T) {
|
||||
var c uint
|
||||
for _, v := range base256emojiTable {
|
||||
if v != rune(0) {
|
||||
c++
|
||||
}
|
||||
}
|
||||
if c != 256 {
|
||||
t.Errorf("Base256Emoji count is wrong, expected 256, got %d.", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBase256EmojiUniq(t *testing.T) {
|
||||
m := make(map[rune]struct{}, len(base256emojiTable))
|
||||
for i, v := range base256emojiTable {
|
||||
_, ok := m[v]
|
||||
if ok {
|
||||
t.Errorf("Base256Emoji duplicate %s at index %d.", string(v), i)
|
||||
}
|
||||
m[v] = struct{}{}
|
||||
}
|
||||
}
|
||||
14
encoder.go
14
encoder.go
@@ -2,6 +2,7 @@ package multibase
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// Encoder is a multibase encoding that is verified to be supported and
|
||||
@@ -14,7 +15,7 @@ type Encoder struct {
|
||||
func NewEncoder(base Encoding) (Encoder, error) {
|
||||
_, ok := EncodingToStr[base]
|
||||
if !ok {
|
||||
return Encoder{-1}, fmt.Errorf("Unsupported multibase encoding: %d", base)
|
||||
return Encoder{-1}, fmt.Errorf("unsupported multibase encoding: %d", base)
|
||||
}
|
||||
return Encoder{base}, nil
|
||||
}
|
||||
@@ -33,17 +34,18 @@ func MustNewEncoder(base Encoding) Encoder {
|
||||
// either be the multibase name or single character multibase prefix
|
||||
func EncoderByName(str string) (Encoder, error) {
|
||||
var base Encoding
|
||||
ok := true
|
||||
var ok bool
|
||||
if len(str) == 0 {
|
||||
return Encoder{-1}, fmt.Errorf("Empty multibase encoding")
|
||||
} else if len(str) == 1 {
|
||||
base = Encoding(str[0])
|
||||
return Encoder{-1}, fmt.Errorf("empty multibase encoding")
|
||||
} else if utf8.RuneCountInString(str) == 1 {
|
||||
r, _ := utf8.DecodeRuneInString(str)
|
||||
base = Encoding(r)
|
||||
_, ok = EncodingToStr[base]
|
||||
} else {
|
||||
base, ok = Encodings[str]
|
||||
}
|
||||
if !ok {
|
||||
return Encoder{-1}, fmt.Errorf("Unsupported multibase encoding: %s", str)
|
||||
return Encoder{-1}, fmt.Errorf("unsupported multibase encoding: %s", str)
|
||||
}
|
||||
return Encoder{base}, nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package multibase
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func TestInvalidCode(t *testing.T) {
|
||||
@@ -43,9 +44,10 @@ func TestEncoder(t *testing.T) {
|
||||
}
|
||||
// Test that an encoder can be created from the single letter
|
||||
// prefix
|
||||
_, err = EncoderByName(str[0:1])
|
||||
r, _ := utf8.DecodeRuneInString(str)
|
||||
_, err = EncoderByName(string(r))
|
||||
if err != nil {
|
||||
t.Fatalf("EncoderByName(%s) failed: %v", str[0:1], err)
|
||||
t.Fatalf("EncoderByName(%s) failed: %v", string(r), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
3
go.mod
3
go.mod
@@ -1,6 +1,9 @@
|
||||
module github.com/multiformats/go-multibase
|
||||
|
||||
go 1.24
|
||||
|
||||
require (
|
||||
github.com/mr-tron/base58 v1.1.0
|
||||
github.com/multiformats/go-base32 v0.0.3
|
||||
github.com/multiformats/go-base36 v0.1.0
|
||||
)
|
||||
|
||||
2
go.sum
2
go.sum
@@ -2,3 +2,5 @@ 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=
|
||||
github.com/multiformats/go-base36 v0.1.0 h1:JR6TyF7JjGd3m6FbLU2cOxhC0Li8z8dLNGQ89tUg4F4=
|
||||
github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoRdUUOENyW/Vv6sM=
|
||||
|
||||
92
multibase.go
92
multibase.go
@@ -4,9 +4,11 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"unicode/utf8"
|
||||
|
||||
b58 "github.com/mr-tron/base58/base58"
|
||||
b32 "github.com/multiformats/go-base32"
|
||||
b36 "github.com/multiformats/go-base36"
|
||||
)
|
||||
|
||||
// Encoding identifies the type of base-encoding that a multibase is carrying.
|
||||
@@ -29,56 +31,49 @@ const (
|
||||
Base32hexUpper = 'V'
|
||||
Base32hexPad = 't'
|
||||
Base32hexPadUpper = 'T'
|
||||
Base58Flickr = 'Z'
|
||||
Base36 = 'k'
|
||||
Base36Upper = 'K'
|
||||
Base58BTC = 'z'
|
||||
Base58Flickr = 'Z'
|
||||
Base64 = 'm'
|
||||
Base64url = 'u'
|
||||
Base64pad = 'M'
|
||||
Base64urlPad = 'U'
|
||||
Base256Emoji = '🚀'
|
||||
)
|
||||
|
||||
// Encodings is a map of the supported encoding, unsupported encoding
|
||||
// EncodingToStr 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',
|
||||
"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",
|
||||
'0': "base2",
|
||||
'f': "base16",
|
||||
'F': "base16upper",
|
||||
'b': "base32",
|
||||
'B': "base32upper",
|
||||
'c': "base32pad",
|
||||
'C': "base32padupper",
|
||||
'v': "base32hex",
|
||||
'V': "base32hexupper",
|
||||
't': "base32hexpad",
|
||||
'T': "base32hexpadupper",
|
||||
'k': "base36",
|
||||
'K': "base36upper",
|
||||
'z': "base58btc",
|
||||
'Z': "base58flickr",
|
||||
'm': "base64",
|
||||
'u': "base64url",
|
||||
'M': "base64pad",
|
||||
'U': "base64urlpad",
|
||||
Base256Emoji: "base256emoji",
|
||||
}
|
||||
|
||||
var EncodingToStr = map[Encoding]string{
|
||||
0x00: "identity",
|
||||
'0': "base2",
|
||||
'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",
|
||||
var Encodings = map[string]Encoding{}
|
||||
|
||||
func init() {
|
||||
for e, n := range EncodingToStr {
|
||||
Encodings[n] = e
|
||||
}
|
||||
}
|
||||
|
||||
// ErrUnsupportedEncoding is returned when the selected encoding is not known or
|
||||
@@ -92,7 +87,7 @@ func Encode(base Encoding, data []byte) (string, error) {
|
||||
switch base {
|
||||
case Identity:
|
||||
// 0x00 inside a string is OK in golang and causes no problems with the length calculation.
|
||||
return string(Identity) + string(data), nil
|
||||
return string(rune(Identity)) + string(data), nil
|
||||
case Base2:
|
||||
return string(Base2) + binaryEncodeToString(data), nil
|
||||
case Base16:
|
||||
@@ -115,6 +110,10 @@ func Encode(base Encoding, data []byte) (string, error) {
|
||||
return string(Base32hexPad) + base32HexLowerPad.EncodeToString(data), nil
|
||||
case Base32hexPadUpper:
|
||||
return string(Base32hexPadUpper) + base32HexUpperPad.EncodeToString(data), nil
|
||||
case Base36:
|
||||
return string(Base36) + b36.EncodeToStringLc(data), nil
|
||||
case Base36Upper:
|
||||
return string(Base36Upper) + b36.EncodeToStringUc(data), nil
|
||||
case Base58BTC:
|
||||
return string(Base58BTC) + b58.EncodeAlphabet(data, b58.BTCAlphabet), nil
|
||||
case Base58Flickr:
|
||||
@@ -127,6 +126,8 @@ func Encode(base Encoding, data []byte) (string, error) {
|
||||
return string(Base64url) + base64.RawURLEncoding.EncodeToString(data), nil
|
||||
case Base64:
|
||||
return string(Base64) + base64.RawStdEncoding.EncodeToString(data), nil
|
||||
case Base256Emoji:
|
||||
return string(Base256Emoji) + base256emojiEncode(data), nil
|
||||
default:
|
||||
return "", ErrUnsupportedEncoding
|
||||
}
|
||||
@@ -139,7 +140,8 @@ func Decode(data string) (Encoding, []byte, error) {
|
||||
return 0, nil, fmt.Errorf("cannot decode multibase for zero length string")
|
||||
}
|
||||
|
||||
enc := Encoding(data[0])
|
||||
r, _ := utf8.DecodeRuneInString(data)
|
||||
enc := Encoding(r)
|
||||
|
||||
switch enc {
|
||||
case Identity:
|
||||
@@ -162,6 +164,9 @@ func Decode(data string) (Encoding, []byte, error) {
|
||||
case Base32hexPad, Base32hexPadUpper:
|
||||
bytes, err := b32.HexEncoding.DecodeString(data[1:])
|
||||
return enc, bytes, err
|
||||
case Base36, Base36Upper:
|
||||
bytes, err := b36.DecodeString(data[1:])
|
||||
return enc, bytes, err
|
||||
case Base58BTC:
|
||||
bytes, err := b58.DecodeAlphabet(data[1:], b58.BTCAlphabet)
|
||||
return Base58BTC, bytes, err
|
||||
@@ -180,6 +185,9 @@ func Decode(data string) (Encoding, []byte, error) {
|
||||
case Base64url:
|
||||
bytes, err := base64.RawURLEncoding.DecodeString(data[1:])
|
||||
return Base64url, bytes, err
|
||||
case Base256Emoji:
|
||||
bytes, err := base256emojiDecode(data[4:])
|
||||
return Base256Emoji, bytes, err
|
||||
default:
|
||||
return -1, nil, ErrUnsupportedEncoding
|
||||
}
|
||||
|
||||
@@ -2,7 +2,8 @@ package multibase
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math/rand"
|
||||
"crypto/rand"
|
||||
"sort"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -23,7 +24,7 @@ func TestMap(t *testing.T) {
|
||||
|
||||
var sampleBytes = []byte("Decentralize everything!!!")
|
||||
var encodedSamples = map[Encoding]string{
|
||||
Identity: string(0x00) + "Decentralize everything!!!",
|
||||
Identity: string(rune(0x00)) + "Decentralize everything!!!",
|
||||
Base2: "00100010001100101011000110110010101101110011101000111001001100001011011000110100101111010011001010010000001100101011101100110010101110010011110010111010001101000011010010110111001100111001000010010000100100001",
|
||||
Base16: "f446563656e7472616c697a652065766572797468696e67212121",
|
||||
Base16Upper: "F446563656E7472616C697A652065766572797468696E67212121",
|
||||
@@ -35,11 +36,15 @@ var encodedSamples = map[Encoding]string{
|
||||
Base32hexUpper: "V8HIM6PBEEHP62R39F9II0PBMCLP7IT38D5N6E89144",
|
||||
Base32hexPad: "t8him6pbeehp62r39f9ii0pbmclp7it38d5n6e89144======",
|
||||
Base32hexPadUpper: "T8HIM6PBEEHP62R39F9II0PBMCLP7IT38D5N6E89144======",
|
||||
Base36: "km552ng4dabi4neu1oo8l4i5mndwmpc3mkukwtxy9",
|
||||
Base36Upper: "KM552NG4DABI4NEU1OO8L4I5MNDWMPC3MKUKWTXY9",
|
||||
Base58BTC: "z36UQrhJq9fNDS7DiAHM9YXqDHMPfr4EMArvt",
|
||||
Base58Flickr: "Z36tpRGiQ9Endr7dHahm9xwQdhmoER4emaRVT",
|
||||
Base64: "mRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE",
|
||||
Base64url: "uRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE",
|
||||
Base64pad: "MRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE=",
|
||||
Base64urlPad: "URGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE=",
|
||||
Base256Emoji: "🚀💛✋💃✋😻😈🥺🤤🍀🌟💐✋😅✋💦✋🥺🏃😈😴🌟😻😝👏👏👏",
|
||||
}
|
||||
|
||||
func testEncode(t *testing.T, encoding Encoding, bytes []byte, expected string) {
|
||||
@@ -49,7 +54,7 @@ func testEncode(t *testing.T, encoding Encoding, bytes []byte, expected string)
|
||||
return
|
||||
}
|
||||
if actual != expected {
|
||||
t.Errorf("encoding failed for %c (%d), expected: %s, got: %s", encoding, encoding, expected, actual)
|
||||
t.Errorf("encoding failed for %c (%d / %s), expected: %s, got: %s", encoding, encoding, EncodingToStr[encoding], expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,79 +73,178 @@ func testDecode(t *testing.T, expectedEncoding Encoding, expectedBytes []byte, d
|
||||
}
|
||||
|
||||
func TestEncode(t *testing.T) {
|
||||
for encoding, data := range encodedSamples {
|
||||
testEncode(t, encoding, sampleBytes, data)
|
||||
for encoding := range EncodingToStr {
|
||||
testEncode(t, encoding, sampleBytes, encodedSamples[encoding])
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecode(t *testing.T) {
|
||||
for encoding, data := range encodedSamples {
|
||||
testDecode(t, encoding, sampleBytes, data)
|
||||
for encoding := range EncodingToStr {
|
||||
testDecode(t, encoding, sampleBytes, encodedSamples[encoding])
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoundTrip(t *testing.T) {
|
||||
buf := make([]byte, 17)
|
||||
rand.Read(buf)
|
||||
|
||||
baseList := []Encoding{Identity, Base16, Base32, Base32hex, Base32pad, Base32hexPad, Base58BTC, Base58Flickr, Base64pad, Base64urlPad}
|
||||
|
||||
for _, base := range baseList {
|
||||
enc, err := Encode(base, buf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
for base := range EncodingToStr {
|
||||
if int(base) == 0 {
|
||||
// skip identity: any byte goes there
|
||||
continue
|
||||
}
|
||||
|
||||
e, out, err := Decode(enc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
_, _, err := Decode(string(rune(base)) + "\u00A0")
|
||||
if err == nil {
|
||||
t.Fatal(EncodingToStr[base] + " decode should fail on low-unicode")
|
||||
}
|
||||
|
||||
if e != base {
|
||||
t.Fatal("got wrong encoding out")
|
||||
_, _, err = Decode(string(rune(base)) + "\u1F4A8")
|
||||
if err == nil {
|
||||
t.Fatal(EncodingToStr[base] + " decode should fail on emoji")
|
||||
}
|
||||
|
||||
if !bytes.Equal(buf, out) {
|
||||
t.Fatal("input wasnt the same as output", buf, out)
|
||||
_, _, err = Decode(string(rune(base)) + "!")
|
||||
if err == nil {
|
||||
t.Fatal(EncodingToStr[base] + " decode should fail on punctuation")
|
||||
}
|
||||
|
||||
_, _, err = Decode(string(rune(base)) + "\xA0")
|
||||
if err == nil {
|
||||
t.Fatal(EncodingToStr[base] + " decode should fail on high-latin1")
|
||||
}
|
||||
}
|
||||
|
||||
buf := make([]byte, 137+16) // sufficiently large prime number of bytes + another 16 to test leading 0s
|
||||
rand.Read(buf[16:])
|
||||
|
||||
for base := range EncodingToStr {
|
||||
|
||||
// test roundtrip from the full zero-prefixed buffer down to a single byte
|
||||
for i := 0; i < len(buf); i++ {
|
||||
|
||||
// use a copy to verify we are not overwriting the supplied buffer
|
||||
newBuf := make([]byte, len(buf)-i)
|
||||
copy(newBuf, buf[i:])
|
||||
|
||||
enc, err := Encode(base, newBuf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
e, out, err := Decode(enc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if e != base {
|
||||
t.Fatal("got wrong encoding out")
|
||||
}
|
||||
|
||||
if !bytes.Equal(newBuf, buf[i:]) {
|
||||
t.Fatal("the provided buffer was modified", buf[i:], out)
|
||||
}
|
||||
|
||||
if !bytes.Equal(buf[i:], out) {
|
||||
t.Fatal("input wasnt the same as output", buf[i:], out)
|
||||
}
|
||||
|
||||
// When we have 3 leading zeroes, do a few extra tests
|
||||
// ( choice of leading zeroes is arbitrary - just cutting down on test permutations )
|
||||
|
||||
if i == 13 {
|
||||
|
||||
// if this is a case-insensitive codec semi-randomly swap case in enc and try again
|
||||
name := EncodingToStr[base]
|
||||
if name[len(name)-5:] == "upper" || Encodings[name+"upper"] > 0 {
|
||||
caseTamperedEnc := []byte(enc)
|
||||
|
||||
for _, j := range []int{3, 5, 8, 13, 21, 23, 29, 47, 52} {
|
||||
if caseTamperedEnc[j] >= 65 && caseTamperedEnc[j] <= 90 {
|
||||
caseTamperedEnc[j] += 32
|
||||
} else if caseTamperedEnc[j] >= 97 && caseTamperedEnc[j] <= 122 {
|
||||
caseTamperedEnc[j] -= 32
|
||||
}
|
||||
}
|
||||
|
||||
e, out, err := Decode(string(caseTamperedEnc))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if e != base {
|
||||
t.Fatal("got wrong encoding out")
|
||||
}
|
||||
if !bytes.Equal(buf[i:], out) {
|
||||
t.Fatal("input wasn't the same as output", buf[i:], out)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test that nothing overflows
|
||||
maxValueBuf := make([]byte, 131)
|
||||
for i := 0; i < len(maxValueBuf); i++ {
|
||||
maxValueBuf[i] = 0xFF
|
||||
}
|
||||
|
||||
for base := range EncodingToStr {
|
||||
|
||||
// test roundtrip from the complete buffer down to a single byte
|
||||
for i := 0; i < len(maxValueBuf); i++ {
|
||||
|
||||
enc, err := Encode(base, maxValueBuf[i:])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
e, out, err := Decode(enc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if e != base {
|
||||
t.Fatal("got wrong encoding out")
|
||||
}
|
||||
|
||||
if !bytes.Equal(maxValueBuf[i:], out) {
|
||||
t.Fatal("input wasn't the same as output", maxValueBuf[i:], out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_, _, err := Decode("")
|
||||
if err == nil {
|
||||
t.Fatal("shouldnt be able to decode empty string")
|
||||
t.Fatal("shouldn't be able to decode empty string")
|
||||
}
|
||||
}
|
||||
|
||||
var benchmarkBuf [36]byte // typical CID size
|
||||
var benchmarkCodecs []string
|
||||
|
||||
func init() {
|
||||
rand.Read(benchmarkBuf[:])
|
||||
|
||||
benchmarkCodecs = make([]string, 0, len(Encodings))
|
||||
for n := range Encodings {
|
||||
|
||||
// // Only bench b36 and b58
|
||||
// if len(n) < 6 || (n[4:6] != "36" && n[4:6] != "58") {
|
||||
// continue
|
||||
// }
|
||||
|
||||
benchmarkCodecs = append(benchmarkCodecs, n)
|
||||
}
|
||||
sort.Strings(benchmarkCodecs)
|
||||
}
|
||||
|
||||
func BenchmarkRoundTrip(b *testing.B) {
|
||||
buf := make([]byte, 32)
|
||||
rand.Read(buf)
|
||||
b.ResetTimer()
|
||||
|
||||
bases := map[string]Encoding{
|
||||
"Identity": Identity,
|
||||
"Base2": Base2,
|
||||
"Base16": Base16,
|
||||
"Base16Upper": Base16Upper,
|
||||
"Base32": Base32,
|
||||
"Base32Upper": Base32Upper,
|
||||
"Base32pad": Base32pad,
|
||||
"Base32padUpper": Base32padUpper,
|
||||
"Base32hex": Base32hex,
|
||||
"Base32hexUpper": Base32hexUpper,
|
||||
"Base32hexPad": Base32hexPad,
|
||||
"Base32hexPadUpper": Base32hexPadUpper,
|
||||
"Base58Flickr": Base58Flickr,
|
||||
"Base58BTC": Base58BTC,
|
||||
"Base64": Base64,
|
||||
"Base64url": Base64url,
|
||||
"Base64pad": Base64pad,
|
||||
"Base64urlPad": Base64urlPad,
|
||||
}
|
||||
|
||||
for name, base := range bases {
|
||||
for _, name := range benchmarkCodecs {
|
||||
b.Run(name, func(b *testing.B) {
|
||||
base := Encodings[name]
|
||||
for i := 0; i < b.N; i++ {
|
||||
enc, err := Encode(base, buf)
|
||||
enc, err := Encode(base, benchmarkBuf[:])
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
@@ -154,8 +258,40 @@ func BenchmarkRoundTrip(b *testing.B) {
|
||||
b.Fatal("got wrong encoding out")
|
||||
}
|
||||
|
||||
if !bytes.Equal(buf, out) {
|
||||
b.Fatal("input wasnt the same as output", buf, out)
|
||||
if !bytes.Equal(benchmarkBuf[:], out) {
|
||||
b.Fatal("input wasnt the same as output", benchmarkBuf, out)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkEncode(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
|
||||
for _, name := range benchmarkCodecs {
|
||||
b.Run(name, func(b *testing.B) {
|
||||
base := Encodings[name]
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := Encode(base, benchmarkBuf[:])
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkDecode(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
|
||||
for _, name := range benchmarkCodecs {
|
||||
b.Run(name, func(b *testing.B) {
|
||||
enc, _ := Encode(Encodings[name], benchmarkBuf[:])
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _, err := Decode(enc)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
20
package.json
20
package.json
@@ -3,28 +3,8 @@
|
||||
"bugs": {
|
||||
"url": "https://github.com/multiformats/go-multibase"
|
||||
},
|
||||
"gx": {
|
||||
"dvcsimport": "github.com/multiformats/go-multibase"
|
||||
},
|
||||
"gxDependencies": [
|
||||
{
|
||||
"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",
|
||||
"language": "go",
|
||||
"license": "",
|
||||
"name": "go-multibase",
|
||||
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
|
||||
"version": "0.3.0"
|
||||
}
|
||||
|
||||
|
||||
2
spec
2
spec
Submodule spec updated: 65d3fff834...4c8344e378
37
spec_test.go
37
spec_test.go
@@ -19,7 +19,7 @@ func TestSpec(t *testing.T) {
|
||||
|
||||
reader := csv.NewReader(file)
|
||||
reader.LazyQuotes = false
|
||||
reader.FieldsPerRecord = 3
|
||||
reader.FieldsPerRecord = 4
|
||||
reader.TrimLeadingSpace = true
|
||||
|
||||
values, err := reader.ReadAll()
|
||||
@@ -66,7 +66,7 @@ func TestSpec(t *testing.T) {
|
||||
}
|
||||
}
|
||||
func TestSpecVectors(t *testing.T) {
|
||||
files, err := filepath.Glob("spec/tests/test[0-9]*.csv")
|
||||
files, err := filepath.Glob("spec/tests/*.csv")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -145,3 +145,36 @@ func TestSpecVectors(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func FuzzDecode(f *testing.F) {
|
||||
files, err := filepath.Glob("spec/tests/*.csv")
|
||||
if err != nil {
|
||||
f.Fatal(err)
|
||||
}
|
||||
for _, fname := range files {
|
||||
func() {
|
||||
file, err := os.Open(fname)
|
||||
if err != nil {
|
||||
f.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
reader := csv.NewReader(file)
|
||||
reader.LazyQuotes = false
|
||||
reader.FieldsPerRecord = 2
|
||||
reader.TrimLeadingSpace = true
|
||||
|
||||
values, err := reader.ReadAll()
|
||||
if err != nil {
|
||||
f.Fatal(err)
|
||||
}
|
||||
|
||||
for _, tc := range values[1:] {
|
||||
f.Add(tc[1])
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
f.Fuzz(func(_ *testing.T, data string) {
|
||||
Decode(data)
|
||||
})
|
||||
}
|
||||
|
||||
3
version.json
Normal file
3
version.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"version": "v0.2.0"
|
||||
}
|
||||
Reference in New Issue
Block a user