Skip to content

Instantly share code, notes, and snippets.

View davidawad's full-sized avatar
🇪🇬
البحث عن الحقيقة

David Awad davidawad

🇪🇬
البحث عن الحقيقة
View GitHub Profile
progressTracker.setCurrentStep(OTHER_TX_COMPONENTS);
// Reference input states are constructed from StateAndRefs.
ReferencedStateAndRef referenceState = ourStateAndRef.referenced();
// Output states are constructed from scratch.
DummyState ourOutputState = new DummyState();
// Or as copies of other states with some properties changed.
DummyState ourOtherOutputState = ourOutputState.copy(77);
progressTracker.setCurrentStep(EXTRACTING_VAULT_STATES);
// Let's assume there are already some DummyState in our
// node's vault, stored there as a result of running past flows,
// and we want to consume them in a transaction. There are many
// ways to extract these states from our vault.
// For example, we would extract any unconsumed DummyState
// from our vault as follows:
VaultQueryCriteria criteria = new VaultQueryCriteria(Vault.StateStatus.UNCONSUMED);
UntrustworthyData<Boolean> packet2 = counterpartySession.sendAndReceive(Boolean.class, "You can send and receive any class!");
Boolean bool = packet2.unwrap(data -> {
// Perform checking on the object received.
// T O D O: Check the received object.
// Return the object.
return data;
});
FlowSession counterpartySession = initiateFlow(counterparty);
// We can send arbitrary data to a counterparty.
// If this is the first ``send``, the counterparty will either:
// 1. Ignore the message if they are not registered to respond
// to messages from this flow.
// 2. Start the flow they have registered to respond to this flow,
// and run the flow until the first call to ``receive``, at
// which point they process the message.
// In other words, we are assuming that the counterparty is
CordaX500Name counterPartyName = new CordaX500Name("NodeA", "London", "GB");
Party namedCounterparty = getServiceHub().getIdentityService().wellKnownPartyFromX500Name(counterPartyName);
Party keyedCounterparty = getServiceHub().getIdentityService().partyFromKey(dummyPubKey);
CordaX500Name notaryName = new CordaX500Name("Notary Service", "London", "GB");
Party specificNotary = Objects.requireNonNull(getServiceHub().getNetworkMapCache().getNotary(notaryName));
// Alternatively, we can pick an arbitrary notary from the notary
// list. However, it is always preferable to specify the notary
// explicitly, as the notary list might change when new notaries are
// introduced, or old ones decommissioned.
Party firstNotary = getServiceHub().getNetworkMapCache().getNotaryIdentities().get(0);
/**
* Simply reverses the bytes that are passed in.
*/
public class ReverseEnclave extends Enclave {
// We store the previous result to showcase that the enclave internals can be examined in a mock test.
byte[] previousResult;
@Override
protected byte[] receiveFromUntrustedHost(byte[] bytes) {
// This is used for host->enclave calls so we don't have to think about authentication.
// This is the client that will upload secrets to the enclave for processing.
// In this simple hello world app, we just connect to a TCP socket, take the EnclaveInstanceInfo we're sent
// and transmit an encrypted string. The enclave will reverse it and send it back. You can use this sample
// as a basis for your own apps.
// assume we've gotten our args from outside this code
String toReverse = String.join(" ", args);
// Connect to the host, it will send us a remote attestation (EnclaveInstanceInfo).
@davidawad
davidawad / host.java
Created June 2, 2021 19:26
conclave host sample
// set up server for clients to reach enclave
int port = 9999;
System.out.println("Listening on port " + port + ". Use the client app to send strings for reversal.");
ServerSocket acceptor = new ServerSocket(port);
Socket connection = acceptor.accept();
// Just send the attestation straight to whoever connects. It's signed so that is MITM-safe.
DataOutputStream output = new DataOutputStream(connection.getOutputStream());
// load our enclave app
public class MyContract extends StandardContract implements Contract {
/* Verification logic here. Typically following the lines of: */
if (commandData.equals(new Commands.Action())) {
requireThat(require -> {
require.using("No inputs should be consumed.", tx.getInputStates().size() == 0);
require.using( "Only one output states should be created.", tx.getOutputStates().size() == 1);
}
}
// . . .