Skip to content

Commit 0428d27

Browse files
committed
Core Refactor 1
1 parent 2a720e2 commit 0428d27

File tree

11 files changed

+186
-46
lines changed

11 files changed

+186
-46
lines changed

.github/workflows/core-main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
build:
1313
strategy:
1414
matrix:
15-
tc: [ 8, 17, 21, 25 ]
15+
tc: [ 17, 21, 25 ]
1616
runs-on: ubuntu-latest
1717
defaults:
1818
run:
@@ -62,4 +62,4 @@ jobs:
6262
with:
6363
github-token: ${{ secrets.GITHUB_TOKEN }}
6464
parallel-finished: true
65-
carryforward: "tc8"
65+
carryforward: "tc17"

.github/workflows/core-pr.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
build:
1212
strategy:
1313
matrix:
14-
tc: [ 8, 17, 21, 25 ]
14+
tc: [ 17, 21, 25 ]
1515
runs-on: ubuntu-latest
1616
defaults:
1717
run:
@@ -54,4 +54,4 @@ jobs:
5454
with:
5555
github-token: ${{ secrets.GITHUB_TOKEN }}
5656
parallel-finished: true
57-
carryforward: "tc8"
57+
carryforward: "tc17"

.github/workflows/core-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
build:
1010
strategy:
1111
matrix:
12-
tc: [ 8, 17, 21, 25 ]
12+
tc: [ 17, 21, 25 ]
1313
runs-on: ubuntu-latest
1414
defaults:
1515
run:

core/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ group = 'io.nats.nkeys'
1515

1616
def isRelease = System.getenv("BUILD_EVENT") == "release"
1717
def tc = System.getenv("TARGET_COMPATIBILITY")
18-
def targetJavaVersion = tc == null ? JavaVersion.VERSION_1_8 : JavaVersion.toVersion(tc)
18+
def targetJavaVersion = tc == null ? JavaVersion.VERSION_17 : JavaVersion.toVersion(tc)
1919
def targetId = targetJavaVersion.toString()
20-
def artifact = "core" + (targetJavaVersion == JavaVersion.VERSION_1_8 ? "" : "-jdk" + targetId)
21-
def bundleName = "io.nats.nkeys.core" + (targetJavaVersion == JavaVersion.VERSION_1_8 ? "" : ".jdk" + targetId)
20+
def artifact = "core-jdk" + targetId
21+
def bundleName = "io.nats.nkeys.core.jdk" + targetId
2222

2323
System.out.println("target compatibility: Java " + targetJavaVersion)
2424
System.out.println("artifact id : " + artifact)
@@ -27,7 +27,7 @@ System.out.println("bundle name : " + bundleName)
2727
version = isRelease ? jarVersion : jarVersion + "-SNAPSHOT" // version is the variable the gradle uses.
2828

2929
java {
30-
sourceCompatibility = JavaVersion.VERSION_1_8
30+
sourceCompatibility = JavaVersion.VERSION_17
3131
targetCompatibility = targetJavaVersion
3232
withSourcesJar()
3333
withJavadocJar()

core/src/main/java/io/nats/nkey/NKey.java

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import static io.nats.nkey.NKeyConstants.ED25519_SEED_SIZE;
2020
import static io.nats.nkey.NKeyInternalUtils.*;
2121

22+
/**
23+
* The NKey class
24+
*/
2225
public class NKey {
2326

2427
private final NKeyProvider provider;
@@ -35,9 +38,16 @@ public class NKey {
3538

3639
private final NKeyType type;
3740

38-
public NKey(NKeyProvider provider, NKeyType t, char[] publicKey, char[] privateKey) {
41+
/**
42+
* Construct an NKey
43+
* @param provider the NKeyProvider
44+
* @param type the NKeyType
45+
* @param publicKey the public key characters
46+
* @param privateKey the private key characters
47+
*/
48+
public NKey(NKeyProvider provider, NKeyType type, char[] publicKey, char[] privateKey) {
3949
this.provider = provider;
40-
this.type = t;
50+
this.type = type;
4151
this.privateKeyAsSeed = privateKey;
4252
this.publicKey = publicKey;
4353
}
@@ -63,7 +73,8 @@ public void clear() {
6373
}
6474

6575
/**
66-
* @return the string encoded seed for this NKey
76+
* Get the string encoded seed for this NKey
77+
* @return the seed characters
6778
*/
6879
public char[] getSeed() {
6980
NKeyDecodedSeed decoded = getDecodedSeed();
@@ -76,27 +87,46 @@ public char[] getSeed() {
7687
}
7788
}
7889

79-
private void ensurePair() {
90+
/**
91+
* Ensures that the NKey is a pair, not public only
92+
* @throws IllegalStateException if the NKey is a public only key
93+
*/
94+
public void ensurePair() {
8095
if (isPublicOnly()) {
8196
throw new IllegalStateException("Public-only NKey");
8297
}
8398
}
8499

100+
/**
101+
* Get the decoded seed
102+
* @return the decoded seed
103+
* @throws IllegalStateException if the NKey is a public only key
104+
*/
85105
public NKeyDecodedSeed getDecodedSeed() {
86106
ensurePair();
87107
return decodeSeed(privateKeyAsSeed);
88108
}
89109

110+
/**
111+
* Does this NKey represent both the public and private key
112+
* @return true if is a pair
113+
*/
90114
public boolean isPair() {
91115
return privateKeyAsSeed != null;
92116
}
93117

118+
/**
119+
* Does this NKey represent only the public key half,
120+
* meaning does not have the private key half
121+
* @return true if is public only
122+
*/
94123
public boolean isPublicOnly() {
95124
return privateKeyAsSeed == null;
96125
}
97126

98127
/**
99-
* @return the encoded public key for this NKey
128+
* Get the encoded public key for this NKey
129+
* @return the encoded characters
100130
*/
101131
public char[] getPublicKey() {
102132
if (publicKey != null) {
@@ -106,23 +136,26 @@ public char[] getPublicKey() {
106136
}
107137

108138
/**
109-
* @return the encoded private key for this NKey
139+
* Get the encoded private key for this NKey
140+
* @return the encoded characters
110141
*/
111142
public char[] getPrivateKey() {
112143
NKeyDecodedSeed decoded = getDecodedSeed();
113144
return encode(NKeyType.PRIVATE, decoded.bytes);
114145
}
115146

116147
/**
117-
* @return A Java security keypair that represents this NKey in Java security form.
148+
* Get the Java security keypair that represents this NKey in Java security form.
149+
* @return the KeyPair
118150
*/
119151
public KeyPair getKeyPair() {
120152
ensurePair();
121153
return provider.getKeyPair(this);
122154
}
123155

124156
/**
125-
* @return the Type of this NKey
157+
* Get the NKeyType of this NKey
158+
* @return the NKeyType
126159
*/
127160
public NKeyType getType() {
128161
return type;

core/src/main/java/io/nats/nkey/NKeyConstants.java

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,73 @@
1313

1414
package io.nats.nkey;
1515

16+
/**
17+
* Constants use in the NKey project
18+
*/
1619
public interface NKeyConstants {
20+
/**
21+
* name of system property used for NKeyProvider class
22+
*/
1723
String NKEY_PROVIDER_CLASS_SYSTEM_PROPERTY = "nkey.provider.class";
24+
25+
/**
26+
* name of environment variable used for NKeyProvider class
27+
*/
1828
String NKEY_PROVIDER_CLASS_ENVIRONMENT_VAR = "NKEY_PROVIDER_CLASS";
1929

20-
// PrefixByteSeed is the prefix byte used for encoded NATS Seeds
30+
/**
31+
* PREFIX_BYTE_SEED is the prefix byte used for encoded NATS Seeds
32+
*/
2133
int PREFIX_BYTE_SEED = 18 << 3; // Base32-encodes to 'S...'
2234

23-
// PrefixBytePrivate is the prefix byte used for encoded NATS Private keys
35+
/**
36+
* PREFIX_BYTE_PRIVATE is the prefix byte used for encoded NATS Private keys
37+
*/
2438
int PREFIX_BYTE_PRIVATE = 15 << 3; // Base32-encodes to 'P...'
2539

26-
// PrefixByteServer is the prefix byte used for encoded NATS Servers
40+
/**
41+
* PREFIX_BYTE_SERVER is the prefix byte used for encoded NATS Servers
42+
*/
2743
int PREFIX_BYTE_SERVER = 13 << 3; // Base32-encodes to 'N...'
2844

29-
// PrefixByteCluster is the prefix byte used for encoded NATS Clusters
45+
/**
46+
* PREFIX_BYTE_CLUSTER is the prefix byte used for encoded NATS Clusters
47+
*/
3048
int PREFIX_BYTE_CLUSTER = 2 << 3; // Base32-encodes to 'C...'
3149

32-
// PrefixByteAccount is the prefix byte used for encoded NATS Accounts
50+
/**
51+
* PREFIX_BYTE_ACCOUNT is the prefix byte used for encoded NATS Accounts
52+
*/
3353
int PREFIX_BYTE_ACCOUNT = 0; // Base32-encodes to 'A...'
3454

35-
// PrefixByteUser is the prefix byte used for encoded NATS Users
55+
/**
56+
* PREFIX_BYTE_USER is the prefix byte used for encoded NATS Users
57+
*/
3658
int PREFIX_BYTE_USER = 20 << 3; // Base32-encodes to 'U...'
3759

38-
// PrefixByteOperator is the prefix byte used for encoded NATS Operators
60+
/**
61+
* PREFIX_BYTE_OPERATOR is the prefix byte used for encoded NATS Operators
62+
*/
3963
int PREFIX_BYTE_OPERATOR = 14 << 3; // Base32-encodes to 'O...'
4064

65+
/**
66+
* Number of bytes in the public key
67+
*/
4168
int ED25519_PUBLIC_KEYSIZE = 32;
69+
70+
/**
71+
* Number of bytes in the private key
72+
*/
4273
int ED25519_PRIVATE_KEYSIZE = 64;
74+
75+
/**
76+
* Number of bytes in the seed
77+
*/
4378
int ED25519_SEED_SIZE = 32;
4479

45-
// XModem CRC based on the go version of NKeys
80+
/**
81+
* XModem CRC based on the go version of NKeys
82+
*/
4683
int[] CRC_16_TABLE = { 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108,
4784
0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294,
4885
0x72f7, 0x62d6, 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, 0x2462, 0x3443, 0x0420,

core/src/main/java/io/nats/nkey/NKeyDecodedSeed.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,26 @@
1313

1414
package io.nats.nkey;
1515

16+
/**
17+
* A decoded version of the NKey seed
18+
* Used for internal and NKeyProvider implementations, not really intended to be public
19+
*/
1620
public class NKeyDecodedSeed {
21+
/**
22+
* The prefix
23+
*/
1724
public final int prefix;
25+
26+
/**
27+
* The bytes
28+
*/
1829
public final byte[] bytes;
1930

31+
/**
32+
* Construct an NKeyDecodedSeed
33+
* @param prefix the prefix
34+
* @param bytes the bytes
35+
*/
2036
public NKeyDecodedSeed(int prefix, byte[] bytes) {
2137
this.prefix = prefix;
2238
this.bytes = bytes;

core/src/main/java/io/nats/nkey/NKeyInternalUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import static io.nats.nkey.NKeyConstants.*;
2525

26-
public abstract class NKeyInternalUtils {
26+
abstract class NKeyInternalUtils {
2727
private NKeyInternalUtils() {} /* ensures cannot be constructed */
2828

2929
private static final String BASE32_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";

0 commit comments

Comments
 (0)