Skip to content

Commit 9d08106

Browse files
authored
XBR Code cleaning (#473)
* move files into sub-packages * dont fail if write thread is null * more cleaning
1 parent 3e4a30f commit 9d08106

File tree

9 files changed

+33
-25
lines changed

9 files changed

+33
-25
lines changed

autobahn/src/main/java/io/crossbar/autobahn/websocket/WebSocketConnection.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ private void closeReaderThread(boolean waitForQuit) {
245245
}
246246
}
247247

248-
private void closeUnderlyingSocket() throws IOException, InterruptedException {
248+
private void closeUnderlyingSocket() throws InterruptedException {
249249
Thread cleaner = new Thread(() -> {
250250
if (isConnected()) {
251251
try {
@@ -260,15 +260,16 @@ private void closeUnderlyingSocket() throws IOException, InterruptedException {
260260
}
261261

262262
private void closeWriterThread() {
263-
try {
264-
mWriterThread.shutdown();
265-
mWriterThread.awaitTermination(5, TimeUnit.SECONDS);
266-
} catch (InterruptedException e) {
267-
LOGGER.v(e.getMessage(), e);
263+
if (mWriterThread != null) {
264+
try {
265+
mWriterThread.shutdown();
266+
mWriterThread.awaitTermination(5, TimeUnit.SECONDS);
267+
} catch (InterruptedException e) {
268+
LOGGER.v(e.getMessage(), e);
269+
}
268270
}
269271
}
270272

271-
272273
private void failConnection(int code, String reason) {
273274
LOGGER.d("fail connection [code = " + code + ", reason = " + reason);
274275

@@ -279,7 +280,7 @@ private void failConnection(int code, String reason) {
279280
if (isConnected()) {
280281
try {
281282
closeUnderlyingSocket();
282-
} catch (IOException | InterruptedException e) {
283+
} catch (InterruptedException e) {
283284
LOGGER.v(e.getMessage(), e);
284285
}
285286
} else {
@@ -498,7 +499,7 @@ private void closeAndCleanup() {
498499
if (isConnected()) {
499500
try {
500501
closeUnderlyingSocket();
501-
} catch (IOException | InterruptedException e) {
502+
} catch (InterruptedException e) {
502503
LOGGER.v(e.getMessage(), e);
503504
}
504505
}
@@ -730,7 +731,6 @@ public void run() {
730731
});
731732
}
732733

733-
734734
/**
735735
* Create WebSockets background reader.
736736
*/

autobahn/src/main/java/xbr/network/KeySeries.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@
2727
import java.util.TimerTask;
2828
import java.util.function.Consumer;
2929

30+
import xbr.network.crypto.SealedBox;
31+
import xbr.network.crypto.SecretBox;
32+
3033

3134
public class KeySeries {
3235

3336
private final byte[] mAPIID;
3437
private final BigInteger mPrice;
3538
private final int mInterval;
36-
private final SecureRandom mRandom;
3739

3840
private byte[] mID;
3941
private byte[] mKey;
@@ -44,15 +46,14 @@ public class KeySeries {
4446
private Timer mTimer;
4547
private String mPrefix;
4648

47-
private boolean mRunning = false;
49+
private boolean mRunning;
4850

4951
KeySeries(byte[] apiID, BigInteger price, int interval, String prefix,
5052
Consumer<KeySeries> onRotate) {
5153
mAPIID = apiID;
5254
mPrice = price;
5355
mInterval = interval;
5456
mCBOR = new ObjectMapper(new CBORFactory());
55-
mRandom = new SecureRandom();
5657
mOnRotateCallback = onRotate;
5758
mArchive = new HashMap<>();
5859
mTimer = new Timer();
@@ -71,8 +72,10 @@ public void run() {
7172
}
7273

7374
void stop() {
74-
mTimer.cancel();
75-
mRunning = false;
75+
if (mRunning) {
76+
mTimer.cancel();
77+
mRunning = false;
78+
}
7679
}
7780

7881
byte[] getID() {

autobahn/src/main/java/xbr/network/SimpleBuyer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
import io.crossbar.autobahn.wamp.Session;
3333
import io.crossbar.autobahn.wamp.exceptions.ApplicationError;
34+
import xbr.network.crypto.SealedBox;
35+
import xbr.network.crypto.SecretBox;
3436

3537
import static org.libsodium.jni.NaCl.sodium;
3638

autobahn/src/main/java/xbr/network/Util.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@
2424
import java.math.BigInteger;
2525
import java.util.Arrays;
2626

27+
import xbr.network.web3.StructuredDataEncoder;
28+
2729
public class Util {
2830

31+
private static final String VERIFYING_ADDR = "0x254dffcd3277C0b1660F6d42EFbB754edaBAbC2B";
32+
2933
public static BigInteger toXBR(int xbr) {
3034
return BigInteger.valueOf(xbr).multiply(BigInteger.valueOf(10).pow(18));
3135
}
@@ -87,8 +91,7 @@ static byte[] signEIP712Data(ECKeyPair keyPair, byte[] channelAddr, int channelS
8791
BigInteger balance, boolean isFinal)
8892
throws IOException, JSONException {
8993

90-
String verifyingAddr = "0x254dffcd3277C0b1660F6d42EFbB754edaBAbC2B";
91-
JSONObject data = createEIP712Data(verifyingAddr, channelAddr, channelSeq, balance,
94+
JSONObject data = createEIP712Data(VERIFYING_ADDR, channelAddr, channelSeq, balance,
9295
isFinal);
9396
StructuredDataEncoder encoder = new StructuredDataEncoder(data.toString());
9497
byte[] message = encoder.hashStructuredData();
@@ -106,10 +109,8 @@ static byte[] signEIP712Data(ECKeyPair keyPair, byte[] channelAddr, int channelS
106109

107110
static String recoverEIP712Signer(byte[] channelAddr, int channelSeq, BigInteger balance,
108111
boolean isFinal, byte[] signature) {
109-
String verifyingAddr = "0x254dffcd3277C0b1660F6d42EFbB754edaBAbC2B";
110-
111112
try {
112-
JSONObject data = createEIP712Data(verifyingAddr, channelAddr, channelSeq, balance,
113+
JSONObject data = createEIP712Data(VERIFYING_ADDR, channelAddr, channelSeq, balance,
113114
isFinal);
114115
StructuredDataEncoder encoder = new StructuredDataEncoder(data.toString());
115116
byte[] message = encoder.hashStructuredData();

autobahn/src/main/java/xbr/network/SealedBox.java renamed to autobahn/src/main/java/xbr/network/crypto/SealedBox.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package xbr.network;
1+
package xbr.network.crypto;
22

33
import org.libsodium.jni.encoders.Encoder;
44

autobahn/src/main/java/xbr/network/SecretBox.java renamed to autobahn/src/main/java/xbr/network/crypto/SecretBox.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package xbr.network;
1+
package xbr.network.crypto;
22

33
import org.libsodium.jni.crypto.Random;
44
import org.libsodium.jni.crypto.Util;

autobahn/src/main/java/xbr/network/Pair.java renamed to autobahn/src/main/java/xbr/network/web3/Pair.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
1111
* specific language governing permissions and limitations under the License.
1212
*/
13-
package xbr.network;
13+
14+
package xbr.network.web3;
1415

1516
public class Pair {
1617
private final Object first;

autobahn/src/main/java/xbr/network/StructuredData.java renamed to autobahn/src/main/java/xbr/network/web3/StructuredData.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
1111
* specific language governing permissions and limitations under the License.
1212
*/
13-
package xbr.network;
13+
14+
package xbr.network.web3;
1415

1516
import java.util.HashMap;
1617
import java.util.List;

autobahn/src/main/java/xbr/network/StructuredDataEncoder.java renamed to autobahn/src/main/java/xbr/network/web3/StructuredDataEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
1111
* specific language governing permissions and limitations under the License.
1212
*/
13-
package xbr.network;
13+
package xbr.network.web3;
1414

1515
import com.fasterxml.jackson.databind.ObjectMapper;
1616

0 commit comments

Comments
 (0)