Skip to content

Commit

Permalink
Merge pull request #32 from nanjj/master
Browse files Browse the repository at this point in the history
Exported NKeys Error Enhancement
  • Loading branch information
kozlovic committed Feb 14, 2022
2 parents 4438441 + 876105b commit adbe4f7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 29 deletions.
7 changes: 0 additions & 7 deletions crc16.go
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
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
@@ -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")
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
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

0 comments on commit adbe4f7

Please sign in to comment.