Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/org/unicitylabs/sdk/StateTransitionClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public CompletableFuture<InclusionProofResponse> getInclusionProof(StateId state
return this.client.getInclusionProof(stateId);
}

/**
* Submits a certification request to the aggregator.
*
* @param certificationData The certification data to submit.
* @return certification response from the aggregator.
*/
public CompletableFuture<CertificationResponse> submitCertificationRequest(CertificationData certificationData) {
return this.client.submitCertificationRequest(certificationData);
}
Expand Down
42 changes: 40 additions & 2 deletions src/main/java/org/unicitylabs/sdk/api/CertificationData.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.unicitylabs.sdk.crypto.secp256k1.SigningService;
import org.unicitylabs.sdk.predicate.EncodedPredicate;
import org.unicitylabs.sdk.predicate.Predicate;
import org.unicitylabs.sdk.predicate.UnlockScript;
import org.unicitylabs.sdk.predicate.builtin.PayToPublicKeyPredicateUnlockScript;
import org.unicitylabs.sdk.serializer.cbor.CborDeserializer;
import org.unicitylabs.sdk.serializer.cbor.CborSerializer;
Expand Down Expand Up @@ -39,6 +40,11 @@ public class CertificationData {
this.unlockScript = Arrays.copyOf(unlockScript, unlockScript.length);
}

/**
* Get lock script of certified transaction output.
*
* @return lock script
*/
public Predicate getLockScript() {
return this.lockScript;
}
Expand All @@ -61,12 +67,17 @@ public DataHash getTransactionHash() {
return this.transactionHash;
}

/**
* Get unlock script used for certification.
*
* @return unlock script bytes
*/
public byte[] getUnlockScript() {
return Arrays.copyOf(this.unlockScript, this.unlockScript.length);
}

/**
* Create CertificationData from CBOR bytes.
* Deserialize CertificationData from CBOR bytes.
*
* @param bytes CBOR bytes
* @return CertificationData
Expand All @@ -82,6 +93,13 @@ public static CertificationData fromCbor(byte[] bytes) {
);
}

/**
* Build certification data for a mint transaction using the deterministic mint signing service.
*
* @param transaction mint transaction
*
* @return certification data
*/
public static CertificationData fromMintTransaction(MintTransaction transaction) {
SigningService signingService = MintSigningService.create(transaction.getTokenId());

Expand All @@ -92,6 +110,26 @@ public static CertificationData fromMintTransaction(MintTransaction transaction)
);
}

/**
* Build certification data from a transaction and unlock script object.
*
* @param transaction transaction to certify
* @param unlockScript unlock script
*
* @return certification data
*/
public static CertificationData fromTransaction(Transaction transaction, UnlockScript unlockScript) {
return CertificationData.fromTransaction(transaction, unlockScript.encode());
}

/**
* Build certification data from a transaction and encoded unlock script bytes.
*
* @param transaction transaction to certify
* @param unlockScript encoded unlock script bytes
*
* @return certification data
*/
public static CertificationData fromTransaction(Transaction transaction, byte[] unlockScript) {
return new CertificationData(
transaction.getLockScript(),
Expand All @@ -113,7 +151,7 @@ public DataHash calculateLeafValue() {
}

/**
* Convert the certification data to CBOR bytes.
* Serialize certification data to CBOR bytes.
*
* @return CBOR bytes
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ public static CertificationRequest create(CertificationData certificationData) {
}

/**
* Convert the request to a CBOR bytes.
* Serialize request to a CBOR bytes.
*
* @return CBOR bytes
*/
public byte[] toCBOR() {
public byte[] toCbor() {
return CborSerializer.encodeArray(
this.stateId.toCbor(),
this.certificationData.toCbor(),
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/org/unicitylabs/sdk/api/InclusionProof.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ public class InclusionProof {
CertificationData certificationData,
UnicityCertificate unicityCertificate
) {
Objects.requireNonNull(merkleTreePath, "Merkle tree path cannot be null.");
Objects.requireNonNull(unicityCertificate, "Unicity certificate cannot be null.");

this.merkleTreePath = merkleTreePath;
this.merkleTreePath = Objects.requireNonNull(merkleTreePath, "Merkle tree path cannot be null.");;
this.certificationData = certificationData;
this.unicityCertificate = unicityCertificate;
this.unicityCertificate = Objects.requireNonNull(unicityCertificate, "Unicity certificate cannot be null.");;
}

/**
Expand Down Expand Up @@ -58,7 +55,7 @@ public Optional<CertificationData> getCertificationData() {
}

/**
* Create inclusion proof from CBOR bytes.
* Deserialize inclusion proof from CBOR bytes.
*
* @param bytes CBOR bytes
* @return inclusion proof
Expand All @@ -74,7 +71,7 @@ public static InclusionProof fromCbor(byte[] bytes) {
}

/**
* Convert inclusion proof to CBOR bytes.
* Serialize inclusion proof to CBOR bytes.
*
* @return CBOR bytes
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public InclusionProofRequest(
this.stateId = stateId.getData();
}

/**
* Get state id.
*
* @return state id
*/
public byte[] getStateId() {
return Arrays.copyOf(this.stateId, this.stateId.length);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public InclusionProof getInclusionProof() {
}

/**
* Create response from CBOR bytes.
* Deserialize response from CBOR bytes.
*
* @param bytes CBOR bytes
* @return inclusion proof response
Expand All @@ -48,6 +48,11 @@ public static InclusionProofResponse fromCbor(byte[] bytes) {
);
}

/**
* Serialize inclusion proof response to CBOR bytes.
*
* @return CBOR bytes
*/
public byte[] toCbor() {
return CborSerializer.encodeArray(
CborSerializer.encodeUnsignedInteger(this.blockNumber),
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/org/unicitylabs/sdk/api/JsonRpcAggregatorClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,31 @@ public JsonRpcAggregatorClient(String url) {
*
*/
public JsonRpcAggregatorClient(String url, String apiKey) {
Objects.requireNonNull(url, "url cannot be null");

this.transport = new JsonRpcHttpTransport(url);
this.transport = new JsonRpcHttpTransport(Objects.requireNonNull(url, "url cannot be null"));
this.apiKey = apiKey;
}

/**
* Submit a certification request for a transaction state transition.
*
* @param certificationData certification payload
*
* @return asynchronous certification response
*/
@Override
public CompletableFuture<CertificationResponse> submitCertificationRequest(
CertificationData certificationData
) {
Objects.requireNonNull(certificationData, "certificationData cannot be null");

CertificationRequest request = CertificationRequest.create(certificationData);
CertificationRequest request = CertificationRequest.create(
Objects.requireNonNull(certificationData, "certificationData cannot be null"));

Map<String, List<String>> headers = this.apiKey == null
? Map.of()
: Map.of(AUTHORIZATION, List.of(String.format("Bearer %s", this.apiKey)));

return this.transport.request(
"certification_request",
HexConverter.encode(request.toCBOR()),
HexConverter.encode(request.toCbor()),
CertificationResponse.class,
headers
);
Expand All @@ -66,10 +71,10 @@ public CompletableFuture<CertificationResponse> submitCertificationRequest(
* @param stateId state id
* @return inclusion / non inclusion proof
*/
@Override
public CompletableFuture<InclusionProofResponse> getInclusionProof(StateId stateId) {
Objects.requireNonNull(stateId, "stateId cannot be null");

InclusionProofRequest request = new InclusionProofRequest(stateId);
InclusionProofRequest request = new InclusionProofRequest(
Objects.requireNonNull(stateId, "stateId cannot be null"));

return this.transport
.request("get_inclusion_proof.v2", request, String.class)
Expand All @@ -81,8 +86,9 @@ public CompletableFuture<InclusionProofResponse> getInclusionProof(StateId state
*
* @return block height
*/
@Override
public CompletableFuture<Long> getBlockHeight() {
return this.transport.request("get_block_height", Map.of(), BlockHeightResponse.class)
.thenApply(BlockHeightResponse::getBlockNumber);
}
}
}
53 changes: 43 additions & 10 deletions src/main/java/org/unicitylabs/sdk/api/StateId.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,67 @@
import org.unicitylabs.sdk.util.BitString;
import org.unicitylabs.sdk.util.HexConverter;

public class StateId {
/**
* Represents a state identifier for requests.
*/
public final class StateId {

private final DataHash hash;

private StateId(DataHash hash) {
this.hash = hash;
}

/**
* Returns the raw hash bytes of this state id.
*
* @return state id hash bytes
*/
public byte[] getData() {
return this.hash.getData();
}

/**
* Returns the hash imprint bytes.
*
* @return state id imprint bytes
*/
public byte[] getImprint() {
return this.hash.getImprint();
}

/**
* Deserializes a state id from CBOR.
*
* @param bytes CBOR byte string containing SHA-256 hash bytes
* @return decoded state id
*/
public static StateId fromCbor(byte[] bytes) {
return new StateId(
new DataHash(HashAlgorithm.SHA256, CborDeserializer.decodeByteString(bytes)));
}

/**
* Creates a state id from certification data.
*
* @param certificationData certification data carrying lock script and source state hash
* @return created state id
* @throws NullPointerException if {@code certificationData} is {@code null}
*/
public static StateId fromCertificationData(CertificationData certificationData) {
Objects.requireNonNull(certificationData, "Certification data cannot be null");

return StateId.create(certificationData.getLockScript(),
certificationData.getSourceStateHash());
}

/**
* Creates a state id from transaction data.
*
* @param transaction transaction carrying lock script and source state hash
* @return created state id
* @throws NullPointerException if {@code transaction} is {@code null}
*/
public static StateId fromTransaction(Transaction transaction) {
Objects.requireNonNull(transaction, "Transaction cannot be null");

Expand All @@ -59,17 +92,22 @@ private static StateId create(Predicate predicate, DataHash stateHash) {
return new StateId(hash);
}

/**
* Serializes this state id as a CBOR bytes.
*
* @return CBOR-encoded state id
*/
public byte[] toCbor() {
return CborSerializer.encodeByteString(this.getData());
}

/**
* Converts the StateId to a BitString.
* Converts this state id to a {@link BitString}.
*
* @return The BitString representation of the StateId.
* @return bit string representation of this state id
*/
public BitString toBitString() {
return BitString.fromStateId(this);
return new BitString(this.getImprint());
}

@Override
Expand All @@ -86,13 +124,8 @@ public int hashCode() {
return Objects.hashCode(this.hash);
}

/**
* Returns a string representation of the StateId.
*
* @return The string representation.
*/
@Override
public String toString() {
return String.format("StateId[%s]", HexConverter.encode(this.getImprint()));
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/unicitylabs/sdk/api/bft/InputRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public byte[] getExecutedTransactionsHash() {
}

/**
* Create InputRecord from CBOR bytes.
* Deserialize InputRecord from CBOR bytes.
*
* @param bytes CBOR bytes
* @return input record
Expand All @@ -168,7 +168,7 @@ public static InputRecord fromCbor(byte[] bytes) {
}

/**
* Convert InputRecord to CBOR bytes.
* Serialize InputRecord to CBOR bytes.
*
* @return CBOR bytes
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public List<byte[]> getSiblingHashList() {
}

/**
* Create shard tree certificate from CBOR bytes.
* Deserialize shard tree certificate from CBOR bytes.
*
* @param bytes CBOR bytes
* @return shard tree certificate
Expand All @@ -64,7 +64,7 @@ public static ShardTreeCertificate fromCbor(byte[] bytes) {
}

/**
* Convert shard tree certificate to CBOR bytes.
* Serialize shard tree certificate to CBOR bytes.
*
* @return CBOR bytes
*/
Expand Down
Loading
Loading