Skip to content
Open
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
3 changes: 1 addition & 2 deletions src/main/java/org/unicitylabs/sdk/api/CertificationData.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static CertificationData fromCbor(byte[] bytes) {
if (tag.getTag() != CertificationData.CBOR_TAG) {
throw new CborSerializationException(String.format("Invalid CBOR tag: %s", tag.getTag()));
}
List<byte[]> data = CborDeserializer.decodeArray(tag.getData());
List<byte[]> data = CborDeserializer.decodeArray(tag.getData(), 5);

int version = CborDeserializer.decodeUnsignedInteger(data.get(0)).asInt();
if (version != CertificationData.VERSION) {
Expand Down Expand Up @@ -137,7 +137,6 @@ public static CertificationData fromMintTransaction(MintTransaction transaction)
* @return certification data
*/
public static CertificationData fromTransaction(Transaction transaction, UnlockScript unlockScript) {
Objects.requireNonNull(transaction, "transaction cannot be null");
Objects.requireNonNull(unlockScript, "unlockScript cannot be null");

return CertificationData.fromTransaction(transaction, unlockScript.encode());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/unicitylabs/sdk/api/InclusionProof.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static InclusionProof fromCbor(byte[] bytes) {
if (tag.getTag() != InclusionProof.CBOR_TAG) {
throw new CborSerializationException(String.format("Invalid CBOR tag: %s", tag.getTag()));
}
List<byte[]> data = CborDeserializer.decodeArray(tag.getData());
List<byte[]> data = CborDeserializer.decodeArray(tag.getData(), 4);

int version = CborDeserializer.decodeUnsignedInteger(data.get(0)).asInt();
if (version != InclusionProof.VERSION) {
Expand Down Expand Up @@ -120,7 +120,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(InclusionProof.VERSION, this.inclusionCertificate, this.certificationData, this.unicityCertificate);
return Objects.hash(this.inclusionCertificate, this.certificationData, this.unicityCertificate);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public InclusionProof getInclusionProof() {
* @return inclusion proof response
*/
public static InclusionProofResponse fromCbor(byte[] bytes) {
List<byte[]> data = CborDeserializer.decodeArray(bytes);
List<byte[]> data = CborDeserializer.decodeArray(bytes, 2);
return new InclusionProofResponse(
CborDeserializer.decodeUnsignedInteger(data.get(0)).asLong(),
InclusionProof.fromCbor(data.get(1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.unicitylabs.sdk.api.jsonrpc.JsonRpcHttpTransport;
import org.unicitylabs.sdk.util.HexConverter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -14,6 +15,7 @@
* Default aggregator client.
*/
public class JsonRpcAggregatorClient implements AggregatorClient {
private static final String STATE_ID_HEADER = "X-State-ID";

private final JsonRpcHttpTransport transport;
private final String apiKey;
Expand Down Expand Up @@ -54,9 +56,11 @@ public CompletableFuture<CertificationResponse> submitCertificationRequest(
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)));
Map<String, List<String>> headers = new HashMap<>();
headers.put(STATE_ID_HEADER, List.of(request.getStateId().toString()));
if (this.apiKey != null) {
headers.put(AUTHORIZATION, List.of(String.format("Bearer %s", this.apiKey)));
}

return this.transport.request(
"certification_request",
Expand Down
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 @@ -150,7 +150,7 @@ public static InputRecord fromCbor(byte[] bytes) {
if (tag.getTag() != InputRecord.CBOR_TAG) {
throw new CborSerializationException(String.format("Invalid CBOR tag: %s", tag.getTag()));
}
List<byte[]> data = CborDeserializer.decodeArray(tag.getData());
List<byte[]> data = CborDeserializer.decodeArray(tag.getData(), 10);

int version = CborDeserializer.decodeUnsignedInteger(data.get(0)).asInt();
if (version != InputRecord.VERSION) {
Expand Down Expand Up @@ -212,7 +212,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(InputRecord.VERSION, this.roundNumber, this.epoch,
return Objects.hash(this.roundNumber, this.epoch,
Arrays.hashCode(this.previousHash),
Arrays.hashCode(this.hash), Arrays.hashCode(this.summaryValue), this.timestamp,
Arrays.hashCode(this.blockHash),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static ShardTreeCertificate fromCbor(byte[] bytes) {
if (tag.getTag() != ShardTreeCertificate.CBOR_TAG) {
throw new CborSerializationException(String.format("Invalid CBOR tag: %s", tag.getTag()));
}
List<byte[]> data = CborDeserializer.decodeArray(tag.getData());
List<byte[]> data = CborDeserializer.decodeArray(tag.getData(), 3);

int version = CborDeserializer.decodeUnsignedInteger(data.get(0)).asInt();
if (version != ShardTreeCertificate.VERSION) {
Expand Down Expand Up @@ -111,7 +111,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(ShardTreeCertificate.VERSION, this.shard, this.siblingHashList);
return Objects.hash(this.shard, this.siblingHashList);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public static UnicityCertificate fromCbor(byte[] bytes) {
if (tag.getTag() != UnicityCertificate.CBOR_TAG) {
throw new CborSerializationException(String.format("Invalid CBOR tag: %s", tag.getTag()));
}
List<byte[]> data = CborDeserializer.decodeArray(tag.getData());
List<byte[]> data = CborDeserializer.decodeArray(tag.getData(), 7);

int version = CborDeserializer.decodeUnsignedInteger(data.get(0)).asInt();
if (version != UnicityCertificate.VERSION) {
Expand Down Expand Up @@ -226,7 +226,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(UnicityCertificate.VERSION, this.inputRecord, Arrays.hashCode(this.technicalRecordHash),
return Objects.hash(this.inputRecord, Arrays.hashCode(this.technicalRecordHash),
Arrays.hashCode(this.shardConfigurationHash), this.shardTreeCertificate,
this.unicityTreeCertificate, this.unicitySeal);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/unicitylabs/sdk/api/bft/UnicitySeal.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static UnicitySeal fromCbor(byte[] bytes) {
if (tag.getTag() != UnicitySeal.CBOR_TAG) {
throw new CborSerializationException(String.format("Invalid CBOR tag: %s", tag.getTag()));
}
List<byte[]> data = CborDeserializer.decodeArray(tag.getData());
List<byte[]> data = CborDeserializer.decodeArray(tag.getData(), 8);

int version = CborDeserializer.decodeUnsignedInteger(data.get(0)).asInt();
if (version != UnicitySeal.VERSION) {
Expand Down Expand Up @@ -235,7 +235,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(UnicitySeal.VERSION, this.networkId, this.rootChainRoundNumber, this.epoch,
return Objects.hash(this.networkId, this.rootChainRoundNumber, this.epoch,
this.timestamp,
Arrays.hashCode(this.previousHash), Arrays.hashCode(this.hash), this.signatures);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static UnicityTreeCertificate fromCbor(byte[] bytes) {
if (tag.getTag() != UnicityTreeCertificate.CBOR_TAG) {
throw new CborSerializationException(String.format("Invalid CBOR tag: %s", tag.getTag()));
}
List<byte[]> data = CborDeserializer.decodeArray(tag.getData());
List<byte[]> data = CborDeserializer.decodeArray(tag.getData(), 3);

int version = CborDeserializer.decodeUnsignedInteger(data.get(0)).asInt();
if (version != UnicityTreeCertificate.VERSION) {
Expand Down Expand Up @@ -112,7 +112,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(UnicityTreeCertificate.VERSION, this.partitionIdentifier, this.steps);
return Objects.hash(this.partitionIdentifier, this.steps);
}

@Override
Expand Down Expand Up @@ -160,7 +160,7 @@ public byte[] getHash() {
* @return hash step
*/
public static HashStep fromCbor(byte[] bytes) {
List<byte[]> data = CborDeserializer.decodeArray(bytes);
List<byte[]> data = CborDeserializer.decodeArray(bytes, 2);

return new HashStep(
CborDeserializer.decodeUnsignedInteger(data.get(0)).asInt(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static SplitAssetProof create(
* @return split reason proof
*/
public static SplitAssetProof fromCbor(byte[] bytes) {
List<byte[]> data = CborDeserializer.decodeArray(bytes);
List<byte[]> data = CborDeserializer.decodeArray(bytes, 3);

return new SplitAssetProof(
AssetId.fromCbor(data.get(0)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static SplitMintJustification fromCbor(byte[] bytes) {
if (tag.getTag() != SplitMintJustification.CBOR_TAG) {
throw new CborSerializationException(String.format("Invalid CBOR tag: %s", tag.getTag()));
}
List<byte[]> data = CborDeserializer.decodeArray(tag.getData());
List<byte[]> data = CborDeserializer.decodeArray(tag.getData(), 2);
return SplitMintJustification.create(
Token.fromCbor(data.get(0)),
CborDeserializer.decodeArray(data.get(1)).stream().map(SplitAssetProof::fromCbor).collect(Collectors.toSet())
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/unicitylabs/sdk/payment/asset/Asset.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public BigInteger getValue() {
* @return asset
*/
public static Asset fromCbor(byte[] bytes) {
List<byte[]> data = CborDeserializer.decodeArray(bytes);
List<byte[]> data = CborDeserializer.decodeArray(bytes, 2);

return new Asset(
AssetId.fromCbor(data.get(0)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static EncodedPredicate fromCbor(byte[] bytes) {
if (tag.getTag() != EncodedPredicate.CBOR_TAG) {
throw new CborSerializationException(String.format("Invalid CBOR tag: %s", tag.getTag()));
}
List<byte[]> data = CborDeserializer.decodeArray(tag.getData());
List<byte[]> data = CborDeserializer.decodeArray(tag.getData(), 3);
PredicateEngine engine = PredicateEngine.fromId(
CborDeserializer.decodeUnsignedInteger(data.get(0)).asInt());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@ public static List<byte[]> decodeArray(byte[] data) {
return result;
}

/**
* Read a fixed-size CBOR array from bytes. Throws when the encoded array length does not match
* the expected length.
*
* @param data bytes
* @param expectedLength expected number of array elements
* @return CBOR element array
*
* @throws CborSerializationException when the array length differs from {@code expectedLength}
*/
public static List<byte[]> decodeArray(byte[] data, long expectedLength) {
List<byte[]> result = decodeArray(data);
if (result.size() != expectedLength) {
throw new CborSerializationException(
String.format("Expected array of %d elements, got %d", expectedLength, result.size()));
}
return result;
}

/**
* Read elements as raw CBOR element map from CBOR bytes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public MerkleTreePathVerificationResult verify(BigInteger stateId) {
* @return path
*/
public static SparseMerkleTreePath fromCbor(byte[] bytes) {
List<byte[]> data = CborDeserializer.decodeArray(bytes);
List<byte[]> data = CborDeserializer.decodeArray(bytes, 2);

return new SparseMerkleTreePath(
DataHash.fromCbor(data.get(0)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Optional<byte[]> getData() {
* @return sparse Merkle tree path step
*/
public static SparseMerkleTreePathStep fromCbor(byte[] bytes) {
List<byte[]> data = CborDeserializer.decodeArray(bytes);
List<byte[]> data = CborDeserializer.decodeArray(bytes, 2);

return new SparseMerkleTreePathStep(
BigIntegerConverter.decode(CborDeserializer.decodeByteString(data.get(0))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public MerkleTreePathVerificationResult verify(BigInteger stateId) {
* @return path
*/
public static SparseMerkleSumTreePath fromCbor(byte[] bytes) {
List<byte[]> data = CborDeserializer.decodeArray(bytes);
List<byte[]> data = CborDeserializer.decodeArray(bytes, 2);

return new SparseMerkleSumTreePath(
DataHash.fromCbor(data.get(0)),
Expand Down Expand Up @@ -218,7 +218,7 @@ public BigInteger getCounter() {
* @return root
*/
public static Root fromCbor(byte[] bytes) {
List<byte[]> data = CborDeserializer.decodeArray(bytes);
List<byte[]> data = CborDeserializer.decodeArray(bytes, 2);

return new Root(
DataHash.fromCbor(data.get(0)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public BigInteger getValue() {
* @return step
*/
public static SparseMerkleSumTreePathStep fromCbor(byte[] bytes) {
List<byte[]> data = CborDeserializer.decodeArray(bytes);
List<byte[]> data = CborDeserializer.decodeArray(bytes, 3);

return new SparseMerkleSumTreePathStep(
BigIntegerConverter.decode(CborDeserializer.decodeByteString(data.get(0))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public InclusionProof getInclusionProof() {
* @return decoded certified mint transaction
*/
public static CertifiedMintTransaction fromCbor(byte[] bytes) {
List<byte[]> data = CborDeserializer.decodeArray(bytes);
List<byte[]> data = CborDeserializer.decodeArray(bytes, 2);
return new CertifiedMintTransaction(MintTransaction.fromCbor(data.get(0)),
InclusionProof.fromCbor(data.get(1)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public InclusionProof getInclusionProof() {
* @return certified transfer transaction
*/
public static CertifiedTransferTransaction fromCbor(byte[] bytes, Token token) {
List<byte[]> data = CborDeserializer.decodeArray(bytes);
List<byte[]> data = CborDeserializer.decodeArray(bytes, 2);

return new CertifiedTransferTransaction(
TransferTransaction.fromCbor(data.get(0), token),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public static MintTransaction fromCbor(byte[] bytes) {
if (tag.getTag() != MintTransaction.CBOR_TAG) {
throw new CborSerializationException(String.format("Invalid CBOR tag: %s", tag.getTag()));
}
List<byte[]> data = CborDeserializer.decodeArray(tag.getData());
List<byte[]> data = CborDeserializer.decodeArray(tag.getData(), 6);

int version = CborDeserializer.decodeUnsignedInteger(data.get(0)).asInt();
if (version != MintTransaction.VERSION) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/unicitylabs/sdk/transaction/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static Token fromCbor(byte[] bytes) {
if (tag.getTag() != Token.CBOR_TAG) {
throw new CborSerializationException(String.format("Invalid CBOR tag: %s", tag.getTag()));
}
List<byte[]> data = CborDeserializer.decodeArray(tag.getData());
List<byte[]> data = CborDeserializer.decodeArray(tag.getData(), 3);

int version = CborDeserializer.decodeUnsignedInteger(data.get(0)).asInt();
if (version != Token.VERSION) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static TransferTransaction fromCbor(byte[] bytes, Token token) {
if (tag.getTag() != TransferTransaction.CBOR_TAG) {
throw new CborSerializationException(String.format("Invalid CBOR tag: %s", tag.getTag()));
}
List<byte[]> data = CborDeserializer.decodeArray(tag.getData());
List<byte[]> data = CborDeserializer.decodeArray(tag.getData(), 4);

int version = CborDeserializer.decodeUnsignedInteger(data.get(0)).asInt();
if (version != TransferTransaction.VERSION) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.unicitylabs.sdk.transaction.verification;

import org.unicitylabs.sdk.api.bft.RootTrustBase;
import org.unicitylabs.sdk.predicate.verification.PredicateVerifierService;
import org.unicitylabs.sdk.unicityid.CertifiedUnicityIdMintTransaction;
import org.unicitylabs.sdk.util.verification.VerificationResult;
import org.unicitylabs.sdk.util.verification.VerificationStatus;

import java.util.ArrayList;
import java.util.List;

/**
* Verification rule for the genesis (mint) of a unicity id token. Validates the inclusion proof of
* the certified mint transaction.
*/
public class CertifiedUnicityIdMintTransactionVerificationRule {

private CertifiedUnicityIdMintTransactionVerificationRule() {
}

/**
* Verify the certified unicity id mint transaction.
*
* @param trustBase root trust base
* @param predicateVerifier predicate verifier
* @param genesis certified unicity id mint transaction to verify
*
* @return verification result
*/
public static VerificationResult<VerificationStatus> verify(
RootTrustBase trustBase,
PredicateVerifierService predicateVerifier,
CertifiedUnicityIdMintTransaction genesis
) {
List<VerificationResult<?>> results = new ArrayList<>();

VerificationResult<InclusionProofVerificationStatus> result = InclusionProofVerificationRule.verify(
trustBase,
predicateVerifier,
genesis.getInclusionProof(),
genesis
);
results.add(result);
if (result.getStatus() != InclusionProofVerificationStatus.OK) {
return new VerificationResult<>(
"CertifiedUnicityIdMintTransactionVerificationRule",
VerificationStatus.FAIL,
String.format("Inclusion proof verification failed: %s", result.getStatus()),
results
);
}

return new VerificationResult<>(
"CertifiedUnicityIdMintTransactionVerificationRule",
VerificationStatus.OK,
"",
results
);
}
}
Loading
Loading