forked from corda/corda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CORDA-3750: Reimplement Corda's Crypto object for use inside the sand…
…box. (corda#6193) * CORDA-3750: Use hand-written sandbox Crypto object that delegates to the node. * CORDA-3750: Add integration test for deterministic CashIssueAndPayment flow. * Tidy up generics for Array instances. * Upgrade to DJVM 1.1-RC04.
- Loading branch information
Showing
30 changed files
with
694 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...c/integration-test/kotlin/net/corda/node/services/DeterministicCashIssueAndPaymentTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package net.corda.node.services | ||
|
||
import net.corda.core.messaging.startFlow | ||
import net.corda.core.utilities.OpaqueBytes | ||
import net.corda.core.utilities.getOrThrow | ||
import net.corda.core.utilities.loggerFor | ||
import net.corda.finance.DOLLARS | ||
import net.corda.finance.flows.CashIssueAndPaymentFlow | ||
import net.corda.node.DeterministicSourcesRule | ||
import net.corda.testing.core.ALICE_NAME | ||
import net.corda.testing.core.DUMMY_NOTARY_NAME | ||
import net.corda.testing.core.singleIdentity | ||
import net.corda.testing.driver.DriverParameters | ||
import net.corda.testing.driver.driver | ||
import net.corda.testing.driver.internal.incrementalPortAllocation | ||
import net.corda.testing.node.NotarySpec | ||
import net.corda.testing.node.internal.findCordapp | ||
import org.junit.ClassRule | ||
import org.junit.Test | ||
import org.junit.jupiter.api.assertDoesNotThrow | ||
|
||
@Suppress("FunctionName") | ||
class DeterministicCashIssueAndPaymentTest { | ||
companion object { | ||
val logger = loggerFor<DeterministicCashIssueAndPaymentTest>() | ||
|
||
@ClassRule | ||
@JvmField | ||
val djvmSources = DeterministicSourcesRule() | ||
|
||
@JvmField | ||
val CASH_AMOUNT = 500.DOLLARS | ||
|
||
fun parametersFor(djvmSources: DeterministicSourcesRule): DriverParameters { | ||
return DriverParameters( | ||
portAllocation = incrementalPortAllocation(), | ||
startNodesInProcess = false, | ||
notarySpecs = listOf(NotarySpec(DUMMY_NOTARY_NAME, validating = true)), | ||
cordappsForAllNodes = listOf( | ||
findCordapp("net.corda.finance.contracts"), | ||
findCordapp("net.corda.finance.workflows") | ||
), | ||
djvmBootstrapSource = djvmSources.bootstrap, | ||
djvmCordaSource = djvmSources.corda | ||
) | ||
} | ||
} | ||
|
||
@Test(timeout = 300_000) | ||
fun `test DJVM can issue cash`() { | ||
val reference = OpaqueBytes.of(0x01) | ||
driver(parametersFor(djvmSources)) { | ||
val alice = startNode(providedName = ALICE_NAME).getOrThrow() | ||
val aliceParty = alice.nodeInfo.singleIdentity() | ||
val notaryParty = notaryHandles.single().identity | ||
val txId = assertDoesNotThrow { | ||
alice.rpc.startFlow(::CashIssueAndPaymentFlow, | ||
CASH_AMOUNT, | ||
reference, | ||
aliceParty, | ||
false, | ||
notaryParty | ||
).returnValue.getOrThrow() | ||
} | ||
logger.info("TX-ID: {}", txId) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package sandbox.java.lang; | ||
|
||
/** | ||
* This is a dummy class that implements just enough of {@link java.lang.CharSequence} | ||
* to allow us to compile {@link sandbox.net.corda.core.crypto.Crypto}. | ||
*/ | ||
public interface CharSequence extends java.lang.CharSequence { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package sandbox.java.lang; | ||
|
||
/** | ||
* This is a dummy class that implements just enough of {@link java.lang.Comparable} | ||
* to allow us to compile {@link sandbox.net.corda.core.crypto.Crypto}. | ||
*/ | ||
public interface Comparable<T> extends java.lang.Comparable<T> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package sandbox.java.lang; | ||
|
||
/** | ||
* This is a dummy class that implements just enough of {@link java.lang.Number} | ||
* to allow us to compile {@link sandbox.net.corda.core.crypto.Crypto}. | ||
*/ | ||
public class Number extends Object { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package sandbox.java.math; | ||
|
||
/** | ||
* This is a dummy class that implements just enough of {@link java.math.BigInteger} | ||
* to allow us to compile {@link sandbox.net.corda.core.crypto.Crypto}. | ||
*/ | ||
public class BigInteger extends sandbox.java.lang.Number { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package sandbox.java.security; | ||
|
||
import sandbox.java.lang.String; | ||
|
||
/** | ||
* This is a dummy class that implements just enough of {@link java.security.Key} | ||
* to allow us to compile {@link sandbox.net.corda.core.crypto.Crypto}. | ||
*/ | ||
public interface Key { | ||
String getAlgorithm(); | ||
String getFormat(); | ||
byte[] getEncoded(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package sandbox.java.security; | ||
|
||
/** | ||
* This is a dummy class that implements just enough of {@link java.security.KeyPair} | ||
* to allow us to compile {@link sandbox.net.corda.core.crypto.Crypto}. | ||
*/ | ||
public final class KeyPair extends sandbox.java.lang.Object implements java.io.Serializable { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package sandbox.java.security; | ||
|
||
/** | ||
* This is a dummy class that implements just enough of {@link java.security.PrivateKey} | ||
* to allow us to compile {@link sandbox.net.corda.core.crypto.Crypto}. | ||
*/ | ||
public interface PrivateKey extends Key { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package sandbox.java.security; | ||
|
||
/** | ||
* This is a dummy class that implements just enough of {@link java.security.PublicKey} | ||
* to allow us to compile {@link sandbox.net.corda.core.crypto.Crypto}. | ||
*/ | ||
public interface PublicKey extends Key { | ||
} |
8 changes: 8 additions & 0 deletions
8
node/src/main/java/sandbox/java/security/spec/AlgorithmParameterSpec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package sandbox.java.security.spec; | ||
|
||
/** | ||
* This is a dummy class that implements just enough of {@link java.security.spec.AlgorithmParameterSpec} | ||
* to allow us to compile {@link sandbox.net.corda.core.crypto.Crypto}. | ||
*/ | ||
public interface AlgorithmParameterSpec { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package sandbox.java.util; | ||
|
||
/** | ||
* This is a dummy class that implements just enough of {@link java.util.ArrayList} | ||
* to allow us to compile {@link sandbox.net.corda.core.crypto.Crypto}. | ||
*/ | ||
@SuppressWarnings("unused") | ||
public class ArrayList<T> extends sandbox.java.lang.Object implements List<T> { | ||
public ArrayList(int size) { | ||
} | ||
|
||
@Override | ||
public boolean add(T item) { | ||
throw new UnsupportedOperationException("Dummy class - not implemented"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package sandbox.java.util; | ||
|
||
/** | ||
* This is a dummy class that implements just enough of {@link java.util.List} | ||
* to allow us to compile {@link sandbox.net.corda.core.crypto.Crypto}. | ||
*/ | ||
public interface List<T> { | ||
boolean add(T item); | ||
} |
62 changes: 62 additions & 0 deletions
62
node/src/main/java/sandbox/net/corda/core/crypto/DJVM.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package sandbox.net.corda.core.crypto; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import sandbox.java.lang.Integer; | ||
import sandbox.java.lang.String; | ||
import sandbox.java.util.ArrayList; | ||
import sandbox.java.util.List; | ||
import sandbox.org.bouncycastle.asn1.x509.AlgorithmIdentifier; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Helper class for {@link sandbox.net.corda.core.crypto.Crypto}. | ||
* Deliberately package-private. | ||
*/ | ||
final class DJVM { | ||
private DJVM() {} | ||
|
||
@NotNull | ||
static SignatureScheme toDJVM(@NotNull net.corda.core.crypto.SignatureScheme scheme) { | ||
// The AlgorithmParameterSpec is deliberately left as null | ||
// because it is computationally expensive to generate these | ||
// objects inside the sandbox every time. | ||
return new SignatureScheme( | ||
scheme.getSchemeNumberID(), | ||
String.toDJVM(scheme.getSchemeCodeName()), | ||
toDJVM(scheme.getSignatureOID()), | ||
toDJVM(scheme.getAlternativeOIDs()), | ||
String.toDJVM(scheme.getProviderName()), | ||
String.toDJVM(scheme.getAlgorithmName()), | ||
String.toDJVM(scheme.getSignatureName()), | ||
null, | ||
Integer.toDJVM(scheme.getKeySize()), | ||
String.toDJVM(scheme.getDesc()) | ||
); | ||
} | ||
|
||
static org.bouncycastle.asn1.x509.AlgorithmIdentifier fromDJVM(@NotNull AlgorithmIdentifier oid) { | ||
try { | ||
return org.bouncycastle.asn1.x509.AlgorithmIdentifier.getInstance(oid.getEncoded()); | ||
} catch (IOException e) { | ||
throw sandbox.java.lang.DJVM.toRuleViolationError(e); | ||
} | ||
} | ||
|
||
static AlgorithmIdentifier toDJVM(@NotNull org.bouncycastle.asn1.x509.AlgorithmIdentifier oid) { | ||
try { | ||
return AlgorithmIdentifier.getInstance(oid.getEncoded()); | ||
} catch (IOException e) { | ||
throw sandbox.java.lang.DJVM.toRuleViolationError(e); | ||
} | ||
} | ||
|
||
@NotNull | ||
static List<AlgorithmIdentifier> toDJVM(@NotNull java.util.List<org.bouncycastle.asn1.x509.AlgorithmIdentifier> list) { | ||
ArrayList<AlgorithmIdentifier> djvmList = new ArrayList<>(list.size()); | ||
for (org.bouncycastle.asn1.x509.AlgorithmIdentifier oid : list) { | ||
djvmList.add(toDJVM(oid)); | ||
} | ||
return djvmList; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
node/src/main/java/sandbox/net/corda/core/crypto/DJVMPublicKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package sandbox.net.corda.core.crypto; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import sandbox.java.lang.Object; | ||
import sandbox.java.lang.String; | ||
import sandbox.java.security.PublicKey; | ||
|
||
/** | ||
* We shall delegate as much cryptography as possible to Corda's | ||
* underlying {@link net.corda.core.crypto.Crypto} object and its | ||
* {@link java.security.Provider} classes. This wrapper only needs | ||
* to implement {@link #equals} and {@link #hashCode}. | ||
*/ | ||
final class DJVMPublicKey extends Object implements PublicKey { | ||
private final java.security.PublicKey underlying; | ||
private final String algorithm; | ||
private final String format; | ||
private final int hashCode; | ||
|
||
DJVMPublicKey(@NotNull java.security.PublicKey underlying) { | ||
this.underlying = underlying; | ||
this.algorithm = String.toDJVM(underlying.getAlgorithm()); | ||
this.format = String.toDJVM(underlying.getFormat()); | ||
this.hashCode = underlying.hashCode(); | ||
} | ||
|
||
java.security.PublicKey getUnderlying() { | ||
return underlying; | ||
} | ||
|
||
@Override | ||
public boolean equals(java.lang.Object other) { | ||
if (this == other) { | ||
return true; | ||
} else if (!(other instanceof DJVMPublicKey)) { | ||
return false; | ||
} else { | ||
return underlying.equals(((DJVMPublicKey) other).underlying); | ||
} | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return hashCode; | ||
} | ||
|
||
@Override | ||
public String getAlgorithm() { | ||
return algorithm; | ||
} | ||
|
||
@Override | ||
public String getFormat() { | ||
return format; | ||
} | ||
|
||
@Override | ||
public byte[] getEncoded() { | ||
return underlying.getEncoded(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
node/src/main/java/sandbox/org/bouncycastle/asn1/ASN1Encodable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package sandbox.org.bouncycastle.asn1; | ||
|
||
/** | ||
* This is a dummy class that implements just enough of {@link org.bouncycastle.asn1.ASN1Encodable} | ||
* to allow us to compile {@link sandbox.net.corda.core.crypto.Crypto}. | ||
*/ | ||
@SuppressWarnings("WeakerAccess") | ||
public interface ASN1Encodable { | ||
} |
16 changes: 16 additions & 0 deletions
16
node/src/main/java/sandbox/org/bouncycastle/asn1/ASN1Object.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package sandbox.org.bouncycastle.asn1; | ||
|
||
import sandbox.java.lang.Object; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* This is a dummy class that implements just enough of {@link org.bouncycastle.asn1.ASN1Object} | ||
* to allow us to compile {@link sandbox.net.corda.core.crypto.Crypto}. | ||
*/ | ||
@SuppressWarnings("RedundantThrows") | ||
public class ASN1Object extends Object implements ASN1Encodable { | ||
public byte[] getEncoded() throws IOException { | ||
throw new UnsupportedOperationException("Dummy class - not implemented"); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
node/src/main/java/sandbox/org/bouncycastle/asn1/x509/AlgorithmIdentifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package sandbox.org.bouncycastle.asn1.x509; | ||
|
||
import sandbox.org.bouncycastle.asn1.ASN1Object; | ||
|
||
/** | ||
* This is a dummy class that implements just enough of {@link org.bouncycastle.asn1.x509.AlgorithmIdentifier} | ||
* to allow us to compile {@link sandbox.net.corda.core.crypto.Crypto}. | ||
*/ | ||
@SuppressWarnings("unused") | ||
public class AlgorithmIdentifier extends ASN1Object { | ||
public static AlgorithmIdentifier getInstance(Object obj) { | ||
throw new UnsupportedOperationException("Dummy class - not implemented"); | ||
} | ||
} |
Oops, something went wrong.