From c23943651f03b016e72cb5cd3bc6b76317454ad1 Mon Sep 17 00:00:00 2001 From: Vineet77 Date: Tue, 25 Feb 2020 13:15:43 -0600 Subject: [PATCH 1/9] Added checkstyle configuration --- build.gradle | 7 + .../projectred/client/Block.java | 194 ++++++----- config/checkstyle/checkstyle.xml | 319 ++++++++++++++++++ .../projectred/keys/KeyManager.java | 31 +- 4 files changed, 450 insertions(+), 101 deletions(-) create mode 100755 config/checkstyle/checkstyle.xml diff --git a/build.gradle b/build.gradle index d9bfad5..d324178 100755 --- a/build.gradle +++ b/build.gradle @@ -7,6 +7,13 @@ subprojects { apply plugin: 'java' apply plugin: 'org.javamodularity.moduleplugin' + apply plugin: 'checkstyle' + + checkstyle { + toolVersion = '8.29' + configFile = rootProject.file('config/checkstyle/checkstyle.xml') + } + repositories { jcenter() mavenCentral() diff --git a/client/src/main/java/sigblockchain/projectred/client/Block.java b/client/src/main/java/sigblockchain/projectred/client/Block.java index 82c1bcc..ed0af00 100644 --- a/client/src/main/java/sigblockchain/projectred/client/Block.java +++ b/client/src/main/java/sigblockchain/projectred/client/Block.java @@ -3,101 +3,125 @@ import java.math.BigInteger; public class Block { - private final int version; // uint16 - private final BigInteger height; // uint64 - private final long timestamp; // int64 - private final byte[] previousHash; // []byte - private final byte[] merkleRootHash; // []byte - private final int dataLen; // uint16 - private final byte[][] data; // [][]byte + private final int version; // uint16 + private final BigInteger height; // uint64 + private final long timestamp; // int64 + private final byte[] previousHash; // []byte + private final byte[] merkleRootHash; // []byte + private final int dataLen; // uint16 + private final byte[][] data; // [][]byte - /** - * @param version Version of the blockChain the block was created on - * @param height A hex encoded 2's complement 64 bit integer string (without the prefix '0x'). Note, the height cannot be negative Represents the height of the block in the blockChain. - * @param timestamp Unix time, representing the number of nanoseconds elapsed since January 1, 1970 UTC - * @param previousHash Hash of the previous block - * @param merkleRootHash Hash of the merkle-root of all inputs - * @param dataLen Number of objects in the Data section - * @param data Actual contents of block - */ - public Block(int version, String height, long timestamp, byte[] previousHash, byte[] merkleRootHash, int dataLen, byte[][] data) { - //Individual values Checking - if (checkUnsigned16(version)) - throw new IllegalArgumentException("Invalid Input version"); - if (!checkUnsigned64(new BigInteger(height, 16))) - throw new IllegalArgumentException("Invalid Input height"); - if (!checkSigned64(timestamp)) - throw new IllegalArgumentException("Invalid Input timeStamp"); - if (checkUnsigned16(dataLen)) - throw new IllegalArgumentException("Invalid Input dataLen"); + /** + * Stores data (typically contracts). The entire block is either added to the blockchain or the entirety + * of it is rejected. + * + * @param version Version of the blockChain the block was created on + * @param height A hex encoded 2's complement 64 bit integer string (without the prefix '0x'). Note, + * the height cannot be negative Represents the height of the block in the blockChain. + * @param timestamp Unix time, representing the number of nanoseconds elapsed since January 1, 1970 UTC + * @param previousHash Hash of the previous block + * @param merkleRootHash Hash of the merkle-root of all inputs + * @param dataLen Number of objects in the Data section + * @param data Actual contents of block + */ + public Block(int version, String height, long timestamp, byte[] previousHash, + byte[] merkleRootHash, int dataLen, byte[][] data) { + //Individual values Checking + if (checkUnsigned16(version)) { + throw new IllegalArgumentException("Invalid Input version"); + } + if (!checkUnsigned64(new BigInteger(height, 16))) { + throw new IllegalArgumentException("Invalid Input height"); + } + if (!checkSigned64(timestamp)) { + throw new IllegalArgumentException("Invalid Input timeStamp"); + } + if (checkUnsigned16(dataLen)) { + throw new IllegalArgumentException("Invalid Input dataLen"); + } - this.height = new BigInteger(height, 16); - this.version = version; - this.timestamp = timestamp; - this.previousHash = previousHash; - this.merkleRootHash = merkleRootHash; - this.dataLen = dataLen; - this.data = data; - } + this.height = new BigInteger(height, 16); + this.version = version; + this.timestamp = timestamp; + this.previousHash = previousHash; + this.merkleRootHash = merkleRootHash; + this.dataLen = dataLen; + this.data = data; + } - private boolean checkUnsigned16(int value) { - return (value < 0) || (value >= (65536)); - } + private boolean checkUnsigned16(int value) { + return (value < 0) || (value >= (65536)); + } - private boolean checkUnsigned64(BigInteger value) { - return (value.compareTo(BigInteger.ZERO) >= 0) && (value.compareTo(new BigInteger("ffffffffffffffff", 16)) <= 0); - } + private boolean checkUnsigned64(BigInteger value) { + return (value.compareTo(BigInteger.ZERO) >= 0) + && (value.compareTo(new BigInteger("ffffffffffffffff", 16)) <= 0); + } - private boolean checkSigned64(long value) { - return (value >= -(Math.pow(2, 63))) && value < (Math.pow(2, 63)); - } + private boolean checkSigned64(long value) { + return (value >= -(Math.pow(2, 63))) && value < (Math.pow(2, 63)); + } - /** - * @return This function returns the value of version from the data - */ - public int getVersion() { - return this.version; - } + /** + * Returns the version of Aurum in which the block was added/created. + * + * @return int value of version. Will be guaranteed to be unsigned 16bit value + */ + public int getVersion() { + return this.version; + } - /** - * @return This function returns the value of height from the data - */ - public BigInteger getHeight() { - return this.height; - } + /** + * Returns height of the block. + * + * @return int value of the height of the block. Will be positive value less than 2^64 + */ + public BigInteger getHeight() { + return this.height; + } - /** - * @return This function returns the value of timestamp from the data - */ - public long getTimestamp() { - return this.timestamp; - } + /** + * Returns the unix timestamp of block. + * + * @return long timestamp. + */ + public long getTimestamp() { + return this.timestamp; + } - /** - * @return This function returns the value of previousHash from the data - */ - public byte[] getPreviousHash() { - return this.previousHash; - } + /** + * The hash of the previous block. + * + * @return byte array representing the hash + */ + public byte[] getPreviousHash() { + return this.previousHash; + } - /** - * @return This function returns the value of merkleRootHash from the data - */ - public byte[] getMerkleRootHash() { - return this.merkleRootHash; - } + /** + * Returns the merkle root hash of the block. + * + * @return byte array representing the hash + */ + public byte[] getMerkleRootHash() { + return this.merkleRootHash; + } - /** - * @return This function returns the value of dataLen from the data - */ - public int getDataLen() { - return this.dataLen; - } + /** + * Returns the length of data present in the block. + * + * @return int value, garuanteed to be unsigned 16 bit value + */ + public int getDataLen() { + return this.dataLen; + } - /** - * @return This function returns the data from the data - */ - public byte[][] getData() { - return this.data; - } + /** + * Returns the data actually present in the block. + * + * @return An array of byte arrays. + */ + public byte[][] getData() { + return this.data; + } } diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml new file mode 100755 index 0000000..a83fc4f --- /dev/null +++ b/config/checkstyle/checkstyle.xml @@ -0,0 +1,319 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/keys/src/main/java/sigblockchain/projectred/keys/KeyManager.java b/keys/src/main/java/sigblockchain/projectred/keys/KeyManager.java index 1936b79..3a8b671 100644 --- a/keys/src/main/java/sigblockchain/projectred/keys/KeyManager.java +++ b/keys/src/main/java/sigblockchain/projectred/keys/KeyManager.java @@ -1,5 +1,6 @@ package sigblockchain.projectred.keys; +import java.security.SecureRandom; import org.bouncycastle.asn1.sec.SECNamedCurves; import org.bouncycastle.asn1.x9.X9ECParameters; import org.bouncycastle.crypto.AsymmetricCipherKeyPair; @@ -7,23 +8,21 @@ import org.bouncycastle.crypto.params.ECDomainParameters; import org.bouncycastle.crypto.params.ECKeyGenerationParameters; -import java.security.SecureRandom; - public class KeyManager { - public static final X9ECParameters ecp = SECNamedCurves.getByName("secp256r1"); - public static final ECDomainParameters domainParams = new ECDomainParameters(ecp.getCurve(), - ecp.getG(), ecp.getN(), ecp.getH(), ecp.getSeed()); + public static final X9ECParameters ecp = SECNamedCurves.getByName("secp256r1"); + public static final ECDomainParameters domainParams = new ECDomainParameters(ecp.getCurve(), + ecp.getG(), ecp.getN(), ecp.getH(), ecp.getSeed()); - /** - * Will generate a public/private key pair according to the NIST P-256 standards. - * - * @return a AsymmetricCipherKeyPair object - */ - public static AsymmetricCipherKeyPair generateKeyPair() { - ECKeyGenerationParameters keyGenParams = new ECKeyGenerationParameters(domainParams, new SecureRandom()); - ECKeyPairGenerator generator = new ECKeyPairGenerator(); - generator.init(keyGenParams); - return generator.generateKeyPair(); - } + /** + * Will generate a public/private key pair according to the NIST P-256 standards. + * + * @return a AsymmetricCipherKeyPair object + */ + public static AsymmetricCipherKeyPair generateKeyPair() { + var keyGenParams = new ECKeyGenerationParameters(domainParams, new SecureRandom()); + var generator = new ECKeyPairGenerator(); + generator.init(keyGenParams); + return generator.generateKeyPair(); + } } From 82d47ab31c5bc0297f5fed08387f709d5d513000 Mon Sep 17 00:00:00 2001 From: Vineet77 Date: Tue, 25 Feb 2020 13:20:51 -0600 Subject: [PATCH 2/9] Added checkstyle to github workflow. --- .github/workflows/gradle.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index ac5db85..b5e2bd4 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -24,3 +24,19 @@ jobs: - name: Test with Gradle run: xvfb-run ./gradlew test --info + code_quality: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Set up JDK + uses: actions/setup-java@v1 + with: + java-version: 11 + - name: Grant execute permission for gradlew + run: chmod +x gradlew + - name: run style check on main source sets + run: ./gradlew checkstyleMain --info + - name: run style check on test source sets + run: ./gradlew checkstyleTest --info + From 81d15e715c629bd7b527acc04a36a81ea0060cb3 Mon Sep 17 00:00:00 2001 From: Vineet77 Date: Tue, 25 Feb 2020 13:33:18 -0600 Subject: [PATCH 3/9] Fixed formatting on all test files. --- .../projectred/client/AccountTest.java | 9 +- .../projectred/client/BlockTest.java | 112 +++++++++--------- .../projectred/gui/MultiplePaneTest.java | 9 +- .../projectred/keys/KeyManagerTest.java | 57 ++++----- 4 files changed, 90 insertions(+), 97 deletions(-) diff --git a/client/src/test/java/sigblockchain/projectred/client/AccountTest.java b/client/src/test/java/sigblockchain/projectred/client/AccountTest.java index 70b8f2a..21df094 100644 --- a/client/src/test/java/sigblockchain/projectred/client/AccountTest.java +++ b/client/src/test/java/sigblockchain/projectred/client/AccountTest.java @@ -3,7 +3,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; - import java.math.BigInteger; import org.junit.jupiter.api.Test; @@ -39,11 +38,9 @@ public void testAccountTest3() { var addr = "81aaf16d7e4b626dc1c34c47bf9973496a3698a6e7ab0255af867169b43529fb"; var balance = new BigInteger("-2", 16); var nonce = new BigInteger("-2", 16); - try { - Account account = new Account(addr, balance, nonce); - fail("Account object was allowed to be made with invalid arguments"); - } catch (Exception ignored) { - } + + Account account = new Account(addr, balance, nonce); + fail("Account object was allowed to be made with invalid arguments"); } @Test diff --git a/client/src/test/java/sigblockchain/projectred/client/BlockTest.java b/client/src/test/java/sigblockchain/projectred/client/BlockTest.java index 20646cf..552534b 100644 --- a/client/src/test/java/sigblockchain/projectred/client/BlockTest.java +++ b/client/src/test/java/sigblockchain/projectred/client/BlockTest.java @@ -1,74 +1,70 @@ package sigblockchain.projectred.client; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + import java.math.BigInteger; -import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; class BlockTest { - private byte[] previousHash = "Previous Hash".getBytes(); - private byte[] hash = "Block hash".getBytes(); - private byte[][] data = - { - {1, 2, 3, 4}, - {'H', 'E', 'L', 'L', 'O'}, - {5, 6, 7, 8}, - {'W', 'O', 'R', 'L', 'D'} - }; + private byte[] previousHash = "Previous Hash".getBytes(); + private byte[] hash = "Block hash".getBytes(); + private byte[][] data = {{1, 2, 3, 4}, {'H', 'E', 'L', 'L', 'O'}, {5, 6, 7, 8}, {'W', 'O', 'R', 'L', 'D'}}; - @Test - void testConstructor() { - Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data); - assertNotNull(block); - } + @Test + void testConstructor() { + Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data); + assertNotNull(block); + } - @Test - void testGetVersion() { - Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data); - int v = block.getVersion(); - assertEquals(v, 10); - } + @Test + void testGetVersion() { + Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data); + int v = block.getVersion(); + assertEquals(v, 10); + } - @Test - void testGetHeight() { - Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data); - var v = block.getHeight(); - assertEquals(v, new BigInteger("2B", 16)); - } + @Test + void testGetHeight() { + Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data); + var v = block.getHeight(); + assertEquals(v, new BigInteger("2B", 16)); + } - @Test - void testGetTimestamp() { - Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data); - long v = block.getTimestamp(); - assertEquals(v, -3556L); - } + @Test + void testGetTimestamp() { + Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data); + long v = block.getTimestamp(); + assertEquals(v, -3556L); + } - @Test - void testGetPreviousHash() { - Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data); - byte[] v = block.getPreviousHash(); - assertEquals(v, previousHash); - } + @Test + void testGetPreviousHash() { + Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data); + byte[] v = block.getPreviousHash(); + assertEquals(v, previousHash); + } - @Test - void testGetMerkleRootHash() { - Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data); - byte[] v = block.getMerkleRootHash(); - assertEquals(v, hash); - } + @Test + void testGetMerkleRootHash() { + Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data); + byte[] v = block.getMerkleRootHash(); + assertEquals(v, hash); + } - @Test - void testGetDataLen() { - Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data); - int v = block.getDataLen(); - assertEquals(v, 10); - } + @Test + void testGetDataLen() { + Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data); + int v = block.getDataLen(); + assertEquals(v, 10); + } - @Test - void testGetData() { - Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data); - byte[][] v = block.getData(); - assertEquals(v, data); - } + @Test + void testGetData() { + Block block = new Block(10, "2B", -3556L, previousHash, hash, 10, data); + byte[][] v = block.getData(); + assertEquals(v, data); + } } diff --git a/gui/src/test/java/sigblockchain/projectred/gui/MultiplePaneTest.java b/gui/src/test/java/sigblockchain/projectred/gui/MultiplePaneTest.java index 5a2f25e..52cbc34 100644 --- a/gui/src/test/java/sigblockchain/projectred/gui/MultiplePaneTest.java +++ b/gui/src/test/java/sigblockchain/projectred/gui/MultiplePaneTest.java @@ -1,5 +1,8 @@ package sigblockchain.projectred.gui; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; @@ -13,16 +16,12 @@ import org.testfx.framework.junit5.Start; import org.testfx.matcher.control.LabeledMatchers; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - @ExtendWith(ApplicationExtension.class) public class MultiplePaneTest { - @Start - public void start(Stage stage) throws Exception { + private void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(Main.class.getResource("scene.fxml")); stage.setScene(new Scene(root)); diff --git a/keys/src/test/java/sigblockchain/projectred/keys/KeyManagerTest.java b/keys/src/test/java/sigblockchain/projectred/keys/KeyManagerTest.java index 0efe3fd..abc6b8e 100644 --- a/keys/src/test/java/sigblockchain/projectred/keys/KeyManagerTest.java +++ b/keys/src/test/java/sigblockchain/projectred/keys/KeyManagerTest.java @@ -1,48 +1,49 @@ package sigblockchain.projectred.keys; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.math.BigInteger; import org.bouncycastle.crypto.params.ECPrivateKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters; import org.bouncycastle.math.ec.ECPoint; import org.bouncycastle.math.ec.FixedPointCombMultiplier; import org.junit.jupiter.api.Test; -import java.math.BigInteger; - -import static org.junit.jupiter.api.Assertions.assertEquals; - class KeyManagerTest { - @Test - public void testGenerateKeyPair() { - var keyPair = KeyManager.generateKeyPair(); - var privateKey = (ECPrivateKeyParameters) keyPair.getPrivate(); - var actualPublicKey = (ECPublicKeyParameters) keyPair.getPublic(); + @Test + public void testGenerateKeyPair() { + var keyPair = KeyManager.generateKeyPair(); + var privateKey = (ECPrivateKeyParameters) keyPair.getPrivate(); + var actualPublicKey = (ECPublicKeyParameters) keyPair.getPublic(); - ECPoint Q = KeyManager.domainParams.getG().multiply(privateKey.getD()); - var expectedPublicKey = new ECPublicKeyParameters(Q, KeyManager.domainParams); - assertEquals(actualPublicKey.getQ().getRawXCoord().toBigInteger(), expectedPublicKey.getQ().getRawXCoord().toBigInteger()); - assertEquals(actualPublicKey.getQ().getRawYCoord().toBigInteger(), expectedPublicKey.getQ().getRawYCoord().toBigInteger()); + ECPoint q = KeyManager.domainParams.getG().multiply(privateKey.getD()); + var expectedPublicKey = new ECPublicKeyParameters(q, KeyManager.domainParams); + assertEquals(actualPublicKey.getQ().getRawXCoord().toBigInteger(), + expectedPublicKey.getQ().getRawXCoord().toBigInteger()); + assertEquals(actualPublicKey.getQ().getRawYCoord().toBigInteger(), + expectedPublicKey.getQ().getRawYCoord().toBigInteger()); - } + } - @Test - public void testEcParameters() { - // an example of the P-256 public private key pair. - var expectedPrivateKey = new BigInteger("6548d29d5533079e28d5c0edecb6394ed4c0342bfa475fdc96c7bf978492f23d", 16); - var expectedPublicKeyX = new BigInteger("fac843c2ccf0246eb00cb90e62dd96c64b669e2b867c0c2aaa835801aeae8295", 16); - var expectedPublicKeyY = new BigInteger("a797eb07a5d8eef80d5705a7a627c73cf4e683ab98c7245101025be935c4053b", 16); + @Test + public void testEcParameters() { + // an example of the P-256 public private key pair. + var expectedPrivateKey = new BigInteger("6548d29d5533079e28d5c0edecb6394ed4c0342bfa475fdc96c7bf978492f23d", 16); + var expectedPublicKeyX = new BigInteger("fac843c2ccf0246eb00cb90e62dd96c64b669e2b867c0c2aaa835801aeae8295", 16); + var expectedPublicKeyY = new BigInteger("a797eb07a5d8eef80d5705a7a627c73cf4e683ab98c7245101025be935c4053b", 16); - var fpMultiplier = new FixedPointCombMultiplier(); - ECPoint Q = fpMultiplier.multiply(KeyManager.domainParams.getG(), expectedPrivateKey); + var fpMultiplier = new FixedPointCombMultiplier(); + ECPoint q = fpMultiplier.multiply(KeyManager.domainParams.getG(), expectedPrivateKey); - var actualPublicKey = new ECPublicKeyParameters(Q, KeyManager.domainParams); - var actualPrivateKey = new ECPrivateKeyParameters(expectedPrivateKey, KeyManager.domainParams); + var actualPublicKey = new ECPublicKeyParameters(q, KeyManager.domainParams); + var actualPrivateKey = new ECPrivateKeyParameters(expectedPrivateKey, KeyManager.domainParams); - assertEquals(actualPrivateKey.getD(), expectedPrivateKey); - assertEquals(actualPublicKey.getQ().getRawXCoord().toBigInteger(), expectedPublicKeyX); - assertEquals(actualPublicKey.getQ().getRawYCoord().toBigInteger(), expectedPublicKeyY); + assertEquals(actualPrivateKey.getD(), expectedPrivateKey); + assertEquals(actualPublicKey.getQ().getRawXCoord().toBigInteger(), expectedPublicKeyX); + assertEquals(actualPublicKey.getQ().getRawYCoord().toBigInteger(), expectedPublicKeyY); - } + } } From cc08ad435380a39897a2bd94394a6e7277e1c22d Mon Sep 17 00:00:00 2001 From: Vineet77 Date: Tue, 25 Feb 2020 13:40:25 -0600 Subject: [PATCH 4/9] Fixed yml syntax error. --- .github/workflows/gradle.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index b5e2bd4..a6a3667 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -38,5 +38,5 @@ jobs: - name: run style check on main source sets run: ./gradlew checkstyleMain --info - name: run style check on test source sets - run: ./gradlew checkstyleTest --info + run: ./gradlew checkstyleTest --info From 295168e658f0d9e2def243f975de0f81d66791f6 Mon Sep 17 00:00:00 2001 From: Vineet77 Date: Tue, 25 Feb 2020 14:10:16 -0600 Subject: [PATCH 5/9] Fixed test sources to pass along with style check. --- .../projectred/client/AccountTest.java | 9 ++- config/checkstyle/checkstyle.xml | 56 ++++++++++++++++++- .../projectred/gui/MultiplePaneTest.java | 2 +- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/client/src/test/java/sigblockchain/projectred/client/AccountTest.java b/client/src/test/java/sigblockchain/projectred/client/AccountTest.java index 21df094..416f0d3 100644 --- a/client/src/test/java/sigblockchain/projectred/client/AccountTest.java +++ b/client/src/test/java/sigblockchain/projectred/client/AccountTest.java @@ -39,8 +39,13 @@ public void testAccountTest3() { var balance = new BigInteger("-2", 16); var nonce = new BigInteger("-2", 16); - Account account = new Account(addr, balance, nonce); - fail("Account object was allowed to be made with invalid arguments"); + try { + Account account = new Account(addr, balance, nonce); + fail("Account object was allowed to be made with invalid arguments"); + } catch (Exception e) { + return; + } + } @Test diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml index a83fc4f..c42a183 100755 --- a/config/checkstyle/checkstyle.xml +++ b/config/checkstyle/checkstyle.xml @@ -24,6 +24,7 @@ + + + + + + + + + + + + + @@ -93,6 +106,7 @@ value="CLASS_DEF, METHOD_DEF, CTOR_DEF, LITERAL_FOR, LITERAL_WHILE, STATIC_INIT, INSTANCE_INIT, ANNOTATION_DEF, ENUM_DEF"/> + @@ -112,8 +126,11 @@ + + + @@ -125,83 +142,99 @@ STATIC_INIT, INSTANCE_INIT, METHOD_DEF, CTOR_DEF, VARIABLE_DEF"/> + + + + + + + + + + + + + + + + + @@ -221,6 +255,7 @@ + @@ -228,6 +263,7 @@ value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, ANNOTATION_DEF, ANNOTATION_FIELD_DEF, PARAMETER_DEF, VARIABLE_DEF, METHOD_DEF"/> + @@ -236,16 +272,19 @@ + + + + + + + @@ -276,12 +319,14 @@ + + @@ -289,31 +334,40 @@ + - + + + + + + + + + diff --git a/gui/src/test/java/sigblockchain/projectred/gui/MultiplePaneTest.java b/gui/src/test/java/sigblockchain/projectred/gui/MultiplePaneTest.java index 52cbc34..cd45569 100644 --- a/gui/src/test/java/sigblockchain/projectred/gui/MultiplePaneTest.java +++ b/gui/src/test/java/sigblockchain/projectred/gui/MultiplePaneTest.java @@ -21,7 +21,7 @@ public class MultiplePaneTest { @Start - private void start(Stage stage) throws Exception { + public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(Main.class.getResource("scene.fxml")); stage.setScene(new Scene(root)); From 37c534bc4f31e7bbe8cc8f617d82e486e3c8a671 Mon Sep 17 00:00:00 2001 From: jldistefano <43453042+jldistefano@users.noreply.github.com> Date: Thu, 5 Mar 2020 18:50:16 -0600 Subject: [PATCH 6/9] Fixed Formatting --- .../projectred/keys/KeyManager.java | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/keys/src/main/java/sigblockchain/projectred/keys/KeyManager.java b/keys/src/main/java/sigblockchain/projectred/keys/KeyManager.java index 245dbb5..df2275a 100644 --- a/keys/src/main/java/sigblockchain/projectred/keys/KeyManager.java +++ b/keys/src/main/java/sigblockchain/projectred/keys/KeyManager.java @@ -1,7 +1,14 @@ package sigblockchain.projectred.keys; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.math.BigInteger; import java.security.SecureRandom; -import org.bouncycastle.asn1.*; +import org.bouncycastle.asn1.ASN1InputStream; +import org.bouncycastle.asn1.ASN1Integer; +import org.bouncycastle.asn1.ASN1Primitive; +import org.bouncycastle.asn1.DERSequenceGenerator; +import org.bouncycastle.asn1.DLSequence; import org.bouncycastle.asn1.sec.SECNamedCurves; import org.bouncycastle.asn1.x9.X9ECParameters; import org.bouncycastle.crypto.AsymmetricCipherKeyPair; @@ -13,16 +20,11 @@ import org.bouncycastle.crypto.signers.ECDSASigner; import org.bouncycastle.util.Properties; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.math.BigInteger; -import java.security.SecureRandom; - public class KeyManager { -public static final X9ECParameters ecp = SECNamedCurves.getByName("secp256r1"); -public static final ECDomainParameters domainParams = new ECDomainParameters(ecp.getCurve(), - ecp.getG(), ecp.getN(), ecp.getH(), ecp.getSeed()); + public static final X9ECParameters ecp = SECNamedCurves.getByName("secp256r1"); + public static final ECDomainParameters domainParams = new ECDomainParameters(ecp.getCurve(), + ecp.getG(), ecp.getN(), ecp.getH(), ecp.getSeed()); /** @@ -66,21 +68,22 @@ public static byte[] sign(ECPrivateKeyParameters privateKey, byte[] data) { */ public static boolean verify(ECPublicKeyParameters publicKey, byte[] signature, byte[] data) { - BigInteger[] rAndS = decodeDerSignature(signature); - if (rAndS == null) + BigInteger[] randS = decodeDerSignature(signature); + if (randS == null) { return false; - var r = rAndS[0]; - var s = rAndS[1]; + } + var r = randS[0]; + var s = randS[1]; ECDSASigner signer = new ECDSASigner(); signer.init(false, publicKey); - - return signer.verifySignature(data, r, s); + return signer.verifySignature(data, r, s); } - private static BigInteger[] decodeDerSignature(byte[] signature){ + private static BigInteger[] decodeDerSignature(byte[] signature) { //first we have to decode the the DER encoded byte array - BigInteger r, s; + BigInteger r; + BigInteger s; ASN1InputStream decoder = null; try { // BouncyCastle by default is strict about parsing ASN.1 integers. From 211772265c8c5dacd639387ffb33a18276adf03a Mon Sep 17 00:00:00 2001 From: jldistefano <43453042+jldistefano@users.noreply.github.com> Date: Thu, 5 Mar 2020 19:07:16 -0600 Subject: [PATCH 7/9] Fixed formatting for CheckSum.java --- .../java/sigblockchain/projectred/client/AurumClient.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/src/main/java/sigblockchain/projectred/client/AurumClient.java b/client/src/main/java/sigblockchain/projectred/client/AurumClient.java index 88128c5..663e1c2 100644 --- a/client/src/main/java/sigblockchain/projectred/client/AurumClient.java +++ b/client/src/main/java/sigblockchain/projectred/client/AurumClient.java @@ -1,11 +1,11 @@ package sigblockchain.projectred.client; +import com.google.gson.Gson; +import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; -import java.net.URI; -import com.google.gson.Gson; public class AurumClient { From 8fbde24939b06c1245f87eb78a95aab6ae10bb51 Mon Sep 17 00:00:00 2001 From: Jacob DiStefano Date: Thu, 5 Mar 2020 19:18:53 -0600 Subject: [PATCH 8/9] Fix? --- .../projectred/keys/KeyManagerTest.java | 70 ++++++------------- 1 file changed, 20 insertions(+), 50 deletions(-) diff --git a/keys/src/test/java/sigblockchain/projectred/keys/KeyManagerTest.java b/keys/src/test/java/sigblockchain/projectred/keys/KeyManagerTest.java index 9567392..05eb89b 100644 --- a/keys/src/test/java/sigblockchain/projectred/keys/KeyManagerTest.java +++ b/keys/src/test/java/sigblockchain/projectred/keys/KeyManagerTest.java @@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; + import java.math.BigInteger; import org.bouncycastle.crypto.params.ECPrivateKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters; @@ -14,36 +15,23 @@ class KeyManagerTest { - @Test - public void testGenerateKeyPair() { - var keyPair = KeyManager.generateKeyPair(); - var privateKey = (ECPrivateKeyParameters) keyPair.getPrivate(); - var actualPublicKey = (ECPublicKeyParameters) keyPair.getPublic(); - - ECPoint Q = KeyManager.domainParams.getG().multiply(privateKey.getD()); - var expectedPublicKey = new ECPublicKeyParameters(Q, KeyManager.domainParams); - assertEquals(actualPublicKey.getQ().getRawXCoord().toBigInteger(), expectedPublicKey.getQ().getRawXCoord().toBigInteger()); - assertEquals(actualPublicKey.getQ().getRawYCoord().toBigInteger(), expectedPublicKey.getQ().getRawYCoord().toBigInteger()); - - } - - - @Test - public void testEcParameters() { - // an example of the P-256 public private key pair. - var expectedPrivateKey = new BigInteger("6548d29d5533079e28d5c0edecb6394ed4c0342bfa475fdc96c7bf978492f23d", 16); - var expectedPublicKeyX = new BigInteger("fac843c2ccf0246eb00cb90e62dd96c64b669e2b867c0c2aaa835801aeae8295", 16); - var expectedPublicKeyY = new BigInteger("a797eb07a5d8eef80d5705a7a627c73cf4e683ab98c7245101025be935c4053b", 16); + @Test + public void testEcParameters() { + // an example of the P-256 public private key pair. + var expectedPrivateKey = new BigInteger("6548d29d5533079e28d5c0edecb6394ed4c0342bfa475fdc96c7bf978492f23d", 16); + var expectedPublicKeyX = new BigInteger("fac843c2ccf0246eb00cb90e62dd96c64b669e2b867c0c2aaa835801aeae8295", 16); + var expectedPublicKeyY = new BigInteger("a797eb07a5d8eef80d5705a7a627c73cf4e683ab98c7245101025be935c4053b", 16); - var fpMultiplier = new FixedPointCombMultiplier(); - ECPoint Q = fpMultiplier.multiply(KeyManager.domainParams.getG(), expectedPrivateKey); + var fpMultiplier = new FixedPointCombMultiplier(); + ECPoint Q = fpMultiplier.multiply(KeyManager.domainParams.getG(), expectedPrivateKey); - var actualPublicKey = new ECPublicKeyParameters(Q, KeyManager.domainParams); - var actualPrivateKey = new ECPrivateKeyParameters(expectedPrivateKey, KeyManager.domainParams); + var actualPublicKey = new ECPublicKeyParameters(Q, KeyManager.domainParams); + var actualPrivateKey = new ECPrivateKeyParameters(expectedPrivateKey, KeyManager.domainParams); - assertEquals(actualPrivateKey.getD(), expectedPrivateKey); - assertEquals(actualPublicKey.getQ().getRawXCoord().toBigInteger(), expectedPublicKeyX); - assertEquals(actualPublicKey.getQ().getRawYCoord().toBigInteger(), expectedPublicKeyY); + assertEquals(actualPrivateKey.getD(), expectedPrivateKey); + assertEquals(actualPublicKey.getQ().getRawXCoord().toBigInteger(), expectedPublicKeyX); + assertEquals(actualPublicKey.getQ().getRawYCoord().toBigInteger(), expectedPublicKeyY); + } @Test public void testGenerateKeyPair() { @@ -54,13 +42,13 @@ public void testGenerateKeyPair() { ECPoint q = KeyManager.domainParams.getG().multiply(privateKey.getD()); var expectedPublicKey = new ECPublicKeyParameters(q, KeyManager.domainParams); assertEquals(actualPublicKey.getQ().getRawXCoord().toBigInteger(), - expectedPublicKey.getQ().getRawXCoord().toBigInteger()); + expectedPublicKey.getQ().getRawXCoord().toBigInteger()); assertEquals(actualPublicKey.getQ().getRawYCoord().toBigInteger(), - expectedPublicKey.getQ().getRawYCoord().toBigInteger()); + expectedPublicKey.getQ().getRawYCoord().toBigInteger()); } - - @Test + + @Test public void testSigning1() { var keyPair = KeyManager.generateKeyPair(); var privateKey = (ECPrivateKeyParameters) keyPair.getPrivate(); @@ -72,25 +60,7 @@ public void testSigning1() { } @Test - public void testEcParameters() { - // an example of the P-256 public private key pair. - var expectedPrivateKey = new BigInteger("6548d29d5533079e28d5c0edecb6394ed4c0342bfa475fdc96c7bf978492f23d", 16); - var expectedPublicKeyX = new BigInteger("fac843c2ccf0246eb00cb90e62dd96c64b669e2b867c0c2aaa835801aeae8295", 16); - var expectedPublicKeyY = new BigInteger("a797eb07a5d8eef80d5705a7a627c73cf4e683ab98c7245101025be935c4053b", 16); - - var fpMultiplier = new FixedPointCombMultiplier(); - ECPoint q = fpMultiplier.multiply(KeyManager.domainParams.getG(), expectedPrivateKey); - - var actualPublicKey = new ECPublicKeyParameters(q, KeyManager.domainParams); - var actualPrivateKey = new ECPrivateKeyParameters(expectedPrivateKey, KeyManager.domainParams); - - assertEquals(actualPrivateKey.getD(), expectedPrivateKey); - assertEquals(actualPublicKey.getQ().getRawXCoord().toBigInteger(), expectedPublicKeyX); - assertEquals(actualPublicKey.getQ().getRawYCoord().toBigInteger(), expectedPublicKeyY); - - } - - public void testSigning2() { + public void testSigning2() { var signature = Hex.decode("3045022100ff2c146535e75f0b5c8fb3548077f799429f72b4eb2e5412e89678b29fb165e3022036332b243d0f28fd05e9dca1be6eb1ff577622148e245732951fdfb71a4e684b"); var fakeSignature = Hex.decode("1045022100ff2c146535e75f0b5c8fb3548077f799429f72b4eb2e5412e89678b29fb165e3022036332b243d0f28fd05e9dca1be6eb1ff577622148e245732951fdfb71a4e684b"); From 36644267fa50f86d08ed9e0fe7b59d904de61d2c Mon Sep 17 00:00:00 2001 From: Vineet77 Date: Sun, 22 Mar 2020 20:52:13 -0500 Subject: [PATCH 9/9] Fixed checkstyle errors. --- .../projectred/client/AurumClientTest.java | 9 +++++---- .../projectred/keys/KeyManagerTest.java | 13 ++++++++----- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/client/src/test/java/sigblockchain/projectred/client/AurumClientTest.java b/client/src/test/java/sigblockchain/projectred/client/AurumClientTest.java index a0c6ee2..86120c7 100644 --- a/client/src/test/java/sigblockchain/projectred/client/AurumClientTest.java +++ b/client/src/test/java/sigblockchain/projectred/client/AurumClientTest.java @@ -7,7 +7,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; - import io.specto.hoverfly.junit.core.Hoverfly; import io.specto.hoverfly.junit.rule.HoverflyRule; import io.specto.hoverfly.junit5.HoverflyExtension; @@ -30,6 +29,9 @@ public class AurumClientTest { private static HoverflyRule hoverflyRule; + /** + * Gets a JSON resources from the resources directory. + */ @BeforeAll public static void setUpJsonResponse() { var inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream(jsonFile); @@ -42,12 +44,11 @@ public static void setUpJsonResponse() { } - @Test public void testGetAccount(Hoverfly hoverfly) { hoverfly.simulate(dsl( - service(host). - get(startsWith("/accountinfo")) + service(host) + .get(startsWith("/accountinfo")) .queryParam("w", validAddress) .willReturn( success(validJson, "application/json")) diff --git a/keys/src/test/java/sigblockchain/projectred/keys/KeyManagerTest.java b/keys/src/test/java/sigblockchain/projectred/keys/KeyManagerTest.java index 05eb89b..0478b00 100644 --- a/keys/src/test/java/sigblockchain/projectred/keys/KeyManagerTest.java +++ b/keys/src/test/java/sigblockchain/projectred/keys/KeyManagerTest.java @@ -4,7 +4,6 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; - import java.math.BigInteger; import org.bouncycastle.crypto.params.ECPrivateKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters; @@ -23,9 +22,9 @@ public void testEcParameters() { var expectedPublicKeyY = new BigInteger("a797eb07a5d8eef80d5705a7a627c73cf4e683ab98c7245101025be935c4053b", 16); var fpMultiplier = new FixedPointCombMultiplier(); - ECPoint Q = fpMultiplier.multiply(KeyManager.domainParams.getG(), expectedPrivateKey); + ECPoint q = fpMultiplier.multiply(KeyManager.domainParams.getG(), expectedPrivateKey); - var actualPublicKey = new ECPublicKeyParameters(Q, KeyManager.domainParams); + var actualPublicKey = new ECPublicKeyParameters(q, KeyManager.domainParams); var actualPrivateKey = new ECPrivateKeyParameters(expectedPrivateKey, KeyManager.domainParams); assertEquals(actualPrivateKey.getD(), expectedPrivateKey); @@ -61,8 +60,12 @@ public void testSigning1() { @Test public void testSigning2() { - var signature = Hex.decode("3045022100ff2c146535e75f0b5c8fb3548077f799429f72b4eb2e5412e89678b29fb165e3022036332b243d0f28fd05e9dca1be6eb1ff577622148e245732951fdfb71a4e684b"); - var fakeSignature = Hex.decode("1045022100ff2c146535e75f0b5c8fb3548077f799429f72b4eb2e5412e89678b29fb165e3022036332b243d0f28fd05e9dca1be6eb1ff577622148e245732951fdfb71a4e684b"); + var signature = Hex.decode( + "3045022100ff2c146535e75f0b5c8fb3548077f799429f72b4eb2e5412e89678b29fb16" + + "5e3022036332b243d0f28fd05e9dca1be6eb1ff577622148e245732951fdfb71a4e684b"); + var fakeSignature = Hex.decode( + "1045022100ff2c146535e75f0b5c8fb3548077f799429f72b4eb2e5412e89678b29fb16" + + "5e3022036332b243d0f28fd05e9dca1be6eb1ff577622148e245732951fdfb71a4e684b"); var data = "Data".getBytes(); var privateKeyNum = new BigInteger("d8353db7fe564c633f94274d7bc1d7200740e3ac5b617dc1f438feecfebec562", 16);