Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exported NKeys Error Enhancement #32

Merged
merged 1 commit into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions crc16.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,8 @@

package nkeys

import (
"errors"
)

// An implementation of crc16 according to CCITT standards for XMODEM.

// ErrInvalidChecksum indicates a failed verification.
var ErrInvalidChecksum = errors.New("nkeys: invalid checksum")

var crc16tab = [256]uint16{
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
Expand Down
7 changes: 3 additions & 4 deletions creds_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package nkeys

import (
"bytes"
"errors"
"regexp"
)

Expand Down Expand Up @@ -42,12 +41,12 @@ func ParseDecoratedNKey(contents []byte) (KeyPair, error) {
}
}
if seed == nil {
return nil, errors.New("no nkey seed found")
return nil, ErrNoSeedFound
}
if !bytes.HasPrefix(seed, []byte("SO")) &&
!bytes.HasPrefix(seed, []byte("SA")) &&
!bytes.HasPrefix(seed, []byte("SU")) {
return nil, errors.New("doesn't contain a seed nkey")
return nil, ErrInvalidNkeySeed
}
kp, err := FromSeed(seed)
if err != nil {
Expand All @@ -68,7 +67,7 @@ func ParseDecoratedUserNKey(contents []byte) (KeyPair, error) {
return nil, err
}
if !bytes.HasPrefix(seed, []byte("SU")) {
return nil, errors.New("doesn't contain an user seed nkey")
return nil, ErrInvalidUserSeed
}
kp, err := FromSeed(seed)
if err != nil {
Expand Down
38 changes: 38 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2022 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package nkeys

// Errors
const (
ErrInvalidPrefixByte = nkeysError("nkeys: invalid prefix byte")
ErrInvalidKey = nkeysError("nkeys: invalid key")
ErrInvalidPublicKey = nkeysError("nkeys: invalid public key")
ErrInvalidSeedLen = nkeysError("nkeys: invalid seed length")
ErrInvalidSeed = nkeysError("nkeys: invalid seed")
ErrInvalidEncoding = nkeysError("nkeys: invalid encoded key")
ErrInvalidSignature = nkeysError("nkeys: signature verification failed")
ErrCannotSign = nkeysError("nkeys: can not sign, no private key available")
ErrPublicKeyOnly = nkeysError("nkeys: no seed or private key available")
ErrIncompatibleKey = nkeysError("nkeys: incompatible key")
ErrInvalidChecksum = nkeysError("nkeys: invalid checksum")
ErrNoSeedFound = nkeysError("no nkey seed found")
nanjj marked this conversation as resolved.
Show resolved Hide resolved
ErrInvalidNkeySeed = nkeysError("doesn't contain a seed nkey")
ErrInvalidUserSeed = nkeysError("doesn't contain an user seed nkey")
)

type nkeysError string

func (e nkeysError) Error() string {
return string(e)
}
18 changes: 0 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,9 @@
// and performs signing and verification.
package nkeys

import (
"errors"
)

// Version is our current version
const Version = "0.3.0"

// Errors
var (
ErrInvalidPrefixByte = errors.New("nkeys: invalid prefix byte")
ErrInvalidKey = errors.New("nkeys: invalid key")
ErrInvalidPublicKey = errors.New("nkeys: invalid public key")
ErrInvalidSeedLen = errors.New("nkeys: invalid seed length")
ErrInvalidSeed = errors.New("nkeys: invalid seed")
ErrInvalidEncoding = errors.New("nkeys: invalid encoded key")
ErrInvalidSignature = errors.New("nkeys: signature verification failed")
ErrCannotSign = errors.New("nkeys: can not sign, no private key available")
ErrPublicKeyOnly = errors.New("nkeys: no seed or private key available")
ErrIncompatibleKey = errors.New("nkeys: incompatible key")
)

// KeyPair provides the central interface to nkeys.
type KeyPair interface {
Seed() ([]byte, error)
Expand Down