Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ecc3acc587 | ||
|
|
4c4da582c9 | ||
|
|
7a4928f460 | ||
|
|
dc8f758855 | ||
|
|
5dd3f1a01a | ||
|
|
2c89197b5a |
27
.github/workflows/automerge.yml
vendored
Normal file
27
.github/workflows/automerge.yml
vendored
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# File managed by web3-bot. DO NOT EDIT.
|
||||||
|
# See https://github.com/protocol/.github/ for details.
|
||||||
|
|
||||||
|
# Automatically merge pull requests opened by web3-bot, as soon as (and only if) all tests pass.
|
||||||
|
# This reduces the friction associated with updating with our workflows.
|
||||||
|
|
||||||
|
on: [ pull_request ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
automerge:
|
||||||
|
if: github.event.pull_request.user.login == 'web3-bot'
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Wait on tests
|
||||||
|
uses: lewagon/wait-on-check-action@bafe56a6863672c681c3cf671f5e10b20abf2eaa # v0.2
|
||||||
|
with:
|
||||||
|
ref: ${{ github.event.pull_request.head.sha }}
|
||||||
|
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
wait-interval: 10
|
||||||
|
running-workflow-name: 'automerge' # the name of this job
|
||||||
|
- name: Merge PR
|
||||||
|
uses: pascalgn/automerge-action@741c311a47881be9625932b0a0de1b0937aab1ae # v0.13.1
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
|
||||||
|
MERGE_LABELS: ""
|
||||||
|
MERGE_METHOD: "squash"
|
||||||
|
MERGE_DELETE_BRANCH: true
|
||||||
14
.github/workflows/generated-pr.yml
vendored
14
.github/workflows/generated-pr.yml
vendored
@@ -1,14 +0,0 @@
|
|||||||
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
|
|
||||||
55
.github/workflows/go-check.yml
vendored
55
.github/workflows/go-check.yml
vendored
@@ -1,18 +1,43 @@
|
|||||||
name: Go Checks
|
# File managed by web3-bot. DO NOT EDIT.
|
||||||
|
# See https://github.com/protocol/.github/ for details.
|
||||||
|
|
||||||
on:
|
on: [push, pull_request]
|
||||||
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:
|
jobs:
|
||||||
go-check:
|
unit:
|
||||||
uses: ipdxco/unified-github-workflows/.github/workflows/go-check.yml@v1.0
|
runs-on: ubuntu-latest
|
||||||
|
name: Go checks
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
submodules: recursive
|
||||||
|
- uses: actions/setup-go@v2
|
||||||
|
with:
|
||||||
|
go-version: "1.16.x"
|
||||||
|
- name: Install staticcheck
|
||||||
|
run: go install honnef.co/go/tools/cmd/staticcheck@be534f007836a777104a15f2456cd1fffd3ddee8 # v2020.2.2
|
||||||
|
- name: Check that go.mod is tidy
|
||||||
|
run: |
|
||||||
|
go mod tidy
|
||||||
|
if [[ -n $(git ls-files --other --exclude-standard --directory -- go.sum) ]]; then
|
||||||
|
echo "go.sum was added by go mod tidy"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
git diff --exit-code -- go.sum go.mod
|
||||||
|
- name: gofmt
|
||||||
|
if: ${{ success() || failure() }} # run this step even if the previous one failed
|
||||||
|
run: |
|
||||||
|
out=$(gofmt -s -l .)
|
||||||
|
if [[ -n "$out" ]]; then
|
||||||
|
echo $out | awk '{print "::error file=" $0 ",line=0,col=0::File is not gofmt-ed."}'
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
- name: go vet
|
||||||
|
if: ${{ success() || failure() }} # run this step even if the previous one failed
|
||||||
|
run: go vet ./...
|
||||||
|
- name: staticcheck
|
||||||
|
if: ${{ success() || failure() }} # run this step even if the previous one failed
|
||||||
|
run: |
|
||||||
|
set -o pipefail
|
||||||
|
staticcheck ./... | sed -e 's@\(.*\)\.go@./\1.go@g'
|
||||||
|
|
||||||
|
|||||||
54
.github/workflows/go-test.yml
vendored
54
.github/workflows/go-test.yml
vendored
@@ -1,20 +1,40 @@
|
|||||||
name: Go Test
|
# File managed by web3-bot. DO NOT EDIT.
|
||||||
|
# See https://github.com/protocol/.github/ for details.
|
||||||
|
|
||||||
on:
|
on: [push, pull_request]
|
||||||
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:
|
jobs:
|
||||||
go-test:
|
unit:
|
||||||
uses: ipdxco/unified-github-workflows/.github/workflows/go-test.yml@v1.0
|
strategy:
|
||||||
secrets:
|
fail-fast: false
|
||||||
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
|
matrix:
|
||||||
|
os: [ "ubuntu", "windows", "macos" ]
|
||||||
|
go: [ "1.15.x", "1.16.x" ]
|
||||||
|
runs-on: ${{ matrix.os }}-latest
|
||||||
|
name: Unit tests (${{ matrix.os}}, Go ${{ matrix.go }})
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
submodules: recursive
|
||||||
|
- uses: actions/setup-go@v2
|
||||||
|
with:
|
||||||
|
go-version: ${{ matrix.go }}
|
||||||
|
- name: Go information
|
||||||
|
run: |
|
||||||
|
go version
|
||||||
|
go env
|
||||||
|
- name: Run tests
|
||||||
|
run: go test -v -coverprofile coverage.txt ./...
|
||||||
|
- name: Run tests (32 bit)
|
||||||
|
if: ${{ matrix.os != 'macos' }} # can't run 32 bit tests on OSX.
|
||||||
|
env:
|
||||||
|
GOARCH: 386
|
||||||
|
run: go test -v ./...
|
||||||
|
- name: Run tests with race detector
|
||||||
|
if: ${{ matrix.os == 'ubuntu' }} # speed things up. Windows and OSX VMs are slow
|
||||||
|
run: go test -v -race ./...
|
||||||
|
- name: Upload coverage to Codecov
|
||||||
|
uses: codecov/codecov-action@967e2b38a85a62bd61be5529ada27ebc109948c2 # v1.4.1
|
||||||
|
with:
|
||||||
|
file: coverage.txt
|
||||||
|
env_vars: OS=${{ matrix.os }}, GO=${{ matrix.go }}
|
||||||
|
|||||||
19
.github/workflows/release-check.yml
vendored
19
.github/workflows/release-check.yml
vendored
@@ -1,19 +0,0 @@
|
|||||||
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
17
.github/workflows/releaser.yml
vendored
@@ -1,17 +0,0 @@
|
|||||||
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
14
.github/workflows/stale.yml
vendored
@@ -1,14 +0,0 @@
|
|||||||
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
18
.github/workflows/tagpush.yml
vendored
@@ -1,18 +0,0 @@
|
|||||||
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,95 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
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{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,7 +2,6 @@ package multibase
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"unicode/utf8"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Encoder is a multibase encoding that is verified to be supported and
|
// Encoder is a multibase encoding that is verified to be supported and
|
||||||
@@ -37,9 +36,8 @@ func EncoderByName(str string) (Encoder, error) {
|
|||||||
var ok bool
|
var ok bool
|
||||||
if len(str) == 0 {
|
if len(str) == 0 {
|
||||||
return Encoder{-1}, fmt.Errorf("empty multibase encoding")
|
return Encoder{-1}, fmt.Errorf("empty multibase encoding")
|
||||||
} else if utf8.RuneCountInString(str) == 1 {
|
} else if len(str) == 1 {
|
||||||
r, _ := utf8.DecodeRuneInString(str)
|
base = Encoding(str[0])
|
||||||
base = Encoding(r)
|
|
||||||
_, ok = EncodingToStr[base]
|
_, ok = EncodingToStr[base]
|
||||||
} else {
|
} else {
|
||||||
base, ok = Encodings[str]
|
base, ok = Encodings[str]
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package multibase
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"unicode/utf8"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestInvalidCode(t *testing.T) {
|
func TestInvalidCode(t *testing.T) {
|
||||||
@@ -44,10 +43,9 @@ func TestEncoder(t *testing.T) {
|
|||||||
}
|
}
|
||||||
// Test that an encoder can be created from the single letter
|
// Test that an encoder can be created from the single letter
|
||||||
// prefix
|
// prefix
|
||||||
r, _ := utf8.DecodeRuneInString(str)
|
_, err = EncoderByName(str[0:1])
|
||||||
_, err = EncoderByName(string(r))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("EncoderByName(%s) failed: %v", string(r), err)
|
t.Fatalf("EncoderByName(%s) failed: %v", str[0:1], err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -1,6 +1,6 @@
|
|||||||
module github.com/multiformats/go-multibase
|
module github.com/multiformats/go-multibase
|
||||||
|
|
||||||
go 1.24
|
go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/mr-tron/base58 v1.1.0
|
github.com/mr-tron/base58 v1.1.0
|
||||||
|
|||||||
11
multibase.go
11
multibase.go
@@ -4,7 +4,6 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"unicode/utf8"
|
|
||||||
|
|
||||||
b58 "github.com/mr-tron/base58/base58"
|
b58 "github.com/mr-tron/base58/base58"
|
||||||
b32 "github.com/multiformats/go-base32"
|
b32 "github.com/multiformats/go-base32"
|
||||||
@@ -39,7 +38,6 @@ const (
|
|||||||
Base64url = 'u'
|
Base64url = 'u'
|
||||||
Base64pad = 'M'
|
Base64pad = 'M'
|
||||||
Base64urlPad = 'U'
|
Base64urlPad = 'U'
|
||||||
Base256Emoji = '🚀'
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// EncodingToStr is a map of the supported encoding, unsupported encoding
|
// EncodingToStr is a map of the supported encoding, unsupported encoding
|
||||||
@@ -65,7 +63,6 @@ var EncodingToStr = map[Encoding]string{
|
|||||||
'u': "base64url",
|
'u': "base64url",
|
||||||
'M': "base64pad",
|
'M': "base64pad",
|
||||||
'U': "base64urlpad",
|
'U': "base64urlpad",
|
||||||
Base256Emoji: "base256emoji",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var Encodings = map[string]Encoding{}
|
var Encodings = map[string]Encoding{}
|
||||||
@@ -126,8 +123,6 @@ func Encode(base Encoding, data []byte) (string, error) {
|
|||||||
return string(Base64url) + base64.RawURLEncoding.EncodeToString(data), nil
|
return string(Base64url) + base64.RawURLEncoding.EncodeToString(data), nil
|
||||||
case Base64:
|
case Base64:
|
||||||
return string(Base64) + base64.RawStdEncoding.EncodeToString(data), nil
|
return string(Base64) + base64.RawStdEncoding.EncodeToString(data), nil
|
||||||
case Base256Emoji:
|
|
||||||
return string(Base256Emoji) + base256emojiEncode(data), nil
|
|
||||||
default:
|
default:
|
||||||
return "", ErrUnsupportedEncoding
|
return "", ErrUnsupportedEncoding
|
||||||
}
|
}
|
||||||
@@ -140,8 +135,7 @@ func Decode(data string) (Encoding, []byte, error) {
|
|||||||
return 0, nil, fmt.Errorf("cannot decode multibase for zero length string")
|
return 0, nil, fmt.Errorf("cannot decode multibase for zero length string")
|
||||||
}
|
}
|
||||||
|
|
||||||
r, _ := utf8.DecodeRuneInString(data)
|
enc := Encoding(data[0])
|
||||||
enc := Encoding(r)
|
|
||||||
|
|
||||||
switch enc {
|
switch enc {
|
||||||
case Identity:
|
case Identity:
|
||||||
@@ -185,9 +179,6 @@ func Decode(data string) (Encoding, []byte, error) {
|
|||||||
case Base64url:
|
case Base64url:
|
||||||
bytes, err := base64.RawURLEncoding.DecodeString(data[1:])
|
bytes, err := base64.RawURLEncoding.DecodeString(data[1:])
|
||||||
return Base64url, bytes, err
|
return Base64url, bytes, err
|
||||||
case Base256Emoji:
|
|
||||||
bytes, err := base256emojiDecode(data[4:])
|
|
||||||
return Base256Emoji, bytes, err
|
|
||||||
default:
|
default:
|
||||||
return -1, nil, ErrUnsupportedEncoding
|
return -1, nil, ErrUnsupportedEncoding
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package multibase
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/rand"
|
"math/rand"
|
||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -44,7 +44,6 @@ var encodedSamples = map[Encoding]string{
|
|||||||
Base64url: "uRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE",
|
Base64url: "uRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE",
|
||||||
Base64pad: "MRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE=",
|
Base64pad: "MRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE=",
|
||||||
Base64urlPad: "URGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE=",
|
Base64urlPad: "URGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE=",
|
||||||
Base256Emoji: "🚀💛✋💃✋😻😈🥺🤤🍀🌟💐✋😅✋💦✋🥺🏃😈😴🌟😻😝👏👏👏",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func testEncode(t *testing.T, encoding Encoding, bytes []byte, expected string) {
|
func testEncode(t *testing.T, encoding Encoding, bytes []byte, expected string) {
|
||||||
|
|||||||
2
spec
2
spec
Submodule spec updated: 4c8344e378...3806057e6f
35
spec_test.go
35
spec_test.go
@@ -66,7 +66,7 @@ func TestSpec(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
func TestSpecVectors(t *testing.T) {
|
func TestSpecVectors(t *testing.T) {
|
||||||
files, err := filepath.Glob("spec/tests/*.csv")
|
files, err := filepath.Glob("spec/tests/test[0-9]*.csv")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -145,36 +145,3 @@ 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)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"version": "v0.2.0"
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user