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
15 changes: 14 additions & 1 deletion src/main/java/org/unicitylabs/sdk/api/CertificationData.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ public static CertificationData fromCbor(byte[] bytes) {
* @return certification data
*/
public static CertificationData fromMintTransaction(MintTransaction transaction) {
Objects.requireNonNull(transaction, "transaction cannot be null");

SigningService signingService = MintSigningService.create(transaction.getTokenId());

return CertificationData.fromTransaction(
Expand All @@ -135,6 +137,9 @@ 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 All @@ -147,6 +152,9 @@ public static CertificationData fromTransaction(Transaction transaction, UnlockS
* @return certification data
*/
public static CertificationData fromTransaction(Transaction transaction, byte[] unlockScript) {
Objects.requireNonNull(transaction, "transaction cannot be null");
Objects.requireNonNull(unlockScript, "unlockScript cannot be null");

return new CertificationData(
transaction.getLockScript(),
transaction.getSourceStateHash(),
Expand Down Expand Up @@ -179,12 +187,17 @@ public boolean equals(Object o) {
return false;
}
CertificationData that = (CertificationData) o;
return this.lockScript.isEqualTo(that.lockScript)
return Predicate.areEqual(this.lockScript, that.lockScript)
&& Objects.equals(this.sourceStateHash, that.sourceStateHash)
&& Objects.equals(this.transactionHash, that.transactionHash)
&& Arrays.equals(this.unlockScript, that.unlockScript);
}

@Override
public int hashCode() {
return Objects.hash(EncodedPredicate.fromPredicate(this.lockScript), this.sourceStateHash, this.transactionHash, Arrays.hashCode(this.unlockScript));
}

@Override
public String toString() {
return String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static InclusionCertificate decode(byte[] bytes) {

int siblingsCount = 0;
for (int i = 0; i < InclusionCertificate.BITMAP_SIZE; i++) {
int x = bytes[i];
int x = bytes[i] & 0xff;
x = x - ((x >>> 1) & 0x55);
x = (x & 0x33) + ((x >>> 2) & 0x33);
x = (x + (x >>> 4)) & 0x0f;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/unicitylabs/sdk/api/InclusionProof.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,21 @@ public boolean equals(Object o) {
return false;
}
InclusionProof that = (InclusionProof) o;
return Objects.equals(this.inclusionCertificate, that.inclusionCertificate) && Objects.equals(
this.certificationData,
that.certificationData);
return Objects.equals(this.inclusionCertificate, that.inclusionCertificate) && Objects.equals(this.certificationData, that.certificationData) && Objects.equals(this.unicityCertificate, that.unicityCertificate);
}

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

@Override
public String toString() {
return String.format(
"InclusionProof{certificationData=%s, inclusionCertificate=%s, unicityCertificate=%s}",
this.inclusionCertificate,
this.certificationData, this.unicityCertificate);
this.certificationData,
this.unicityCertificate
);
}
}
15 changes: 8 additions & 7 deletions src/main/java/org/unicitylabs/sdk/api/bft/InputRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,15 @@ public boolean equals(Object o) {
return false;
}
InputRecord that = (InputRecord) o;
return Objects.equals(this.roundNumber,
that.roundNumber) && Objects.equals(this.epoch, that.epoch)
return Objects.equals(this.roundNumber, that.roundNumber)
&& Objects.equals(this.epoch, that.epoch)
&& Objects.deepEquals(this.previousHash, that.previousHash)
&& Objects.deepEquals(this.hash, that.hash) && Objects.deepEquals(this.summaryValue,
that.summaryValue) && Objects.equals(this.timestamp, that.timestamp)
&& Objects.deepEquals(this.blockHash, that.blockHash) && Objects.equals(
this.sumOfEarnedFees, that.sumOfEarnedFees) && Objects.deepEquals(
this.executedTransactionsHash, that.executedTransactionsHash);
&& Objects.deepEquals(this.hash, that.hash)
&& Objects.deepEquals(this.summaryValue, that.summaryValue)
&& Objects.equals(this.timestamp, that.timestamp)
&& Objects.deepEquals(this.blockHash, that.blockHash)
&& Objects.equals(this.sumOfEarnedFees, that.sumOfEarnedFees)
&& Objects.deepEquals(this.executedTransactionsHash, that.executedTransactionsHash);
}

@Override
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/unicitylabs/sdk/api/bft/ShardId.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public int getBit(int index) {
}

public boolean isPrefixOf(byte[] data) {
if (data.length * 8 < this.length) {
return false;
}

int fullBytes = this.length / 8;
int remainingBits = this.length % 8;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ public boolean equals(Object o) {
return false;
}
ShardTreeCertificate that = (ShardTreeCertificate) o;
return Objects.deepEquals(this.shard, that.shard) && Objects.equals(
this.siblingHashList, that.siblingHashList);
return Objects.deepEquals(this.shard, that.shard)
&& Objects.equals(this.siblingHashList, that.siblingHashList);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@ public boolean equals(Object o) {
return false;
}
UnicityCertificate that = (UnicityCertificate) o;
return Objects.equals(this.inputRecord,
that.inputRecord) && Objects.deepEquals(this.technicalRecordHash,
that.technicalRecordHash) && Objects.deepEquals(this.shardConfigurationHash,
that.shardConfigurationHash) && Objects.equals(this.shardTreeCertificate,
that.shardTreeCertificate) && Objects.equals(this.unicityTreeCertificate,
that.unicityTreeCertificate) && Objects.equals(this.unicitySeal, that.unicitySeal);
return Objects.equals(this.inputRecord, that.inputRecord)
&& Objects.deepEquals(this.technicalRecordHash, that.technicalRecordHash)
&& Objects.deepEquals(this.shardConfigurationHash, that.shardConfigurationHash)
&& Objects.equals(this.shardTreeCertificate, that.shardTreeCertificate)
&& Objects.equals(this.unicityTreeCertificate, that.unicityTreeCertificate)
&& Objects.equals(this.unicitySeal, that.unicitySeal);
}

@Override
Expand Down
110 changes: 56 additions & 54 deletions src/main/java/org/unicitylabs/sdk/api/bft/UnicitySeal.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class UnicitySeal {
private final long timestamp;
private final byte[] previousHash; // nullable
private final byte[] hash;
private final LinkedHashMap<String, byte[]> signatures;
private final Set<SignatureEntry> signatures;

UnicitySeal(
short networkId,
Expand All @@ -32,7 +32,7 @@ public class UnicitySeal {
long timestamp,
byte[] previousHash,
byte[] hash,
Map<String, byte[]> signatures
Set<SignatureEntry> signatures
) {
Objects.requireNonNull(hash, "Hash cannot be null");

Expand All @@ -44,20 +44,7 @@ public class UnicitySeal {
this.hash = hash;
this.signatures = signatures == null
? null
: signatures.entrySet().stream()
.map(entry -> Map.entry(
entry.getKey(),
Arrays.copyOf(entry.getValue(), entry.getValue().length)
)
)
.collect(
Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(e1, e2) -> e1,
LinkedHashMap::new
)
);
: Set.copyOf(signatures);
}

/**
Expand All @@ -66,7 +53,7 @@ public class UnicitySeal {
* @param signatures the signatures to include in the new UnicitySeal
* @return a new UnicitySeal instance with the specified signatures
*/
public UnicitySeal withSignatures(Map<String, byte[]> signatures) {
public UnicitySeal withSignatures(Set<SignatureEntry> signatures) {
return new UnicitySeal(
this.networkId,
this.rootChainRoundNumber,
Expand Down Expand Up @@ -142,23 +129,8 @@ public byte[] getHash() {
*
* @return signatures
*/
public Map<String, byte[]> getSignatures() {
return this.signatures == null
? null
: this.signatures.entrySet().stream()
.map(entry -> Map.entry(
entry.getKey(),
Arrays.copyOf(entry.getValue(), entry.getValue().length)
)
)
.collect(
Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(e1, e2) -> e1,
LinkedHashMap::new
)
);
public Set<SignatureEntry> getSignatures() {
return this.signatures;
}

/**
Expand Down Expand Up @@ -187,13 +159,11 @@ public static UnicitySeal fromCbor(byte[] bytes) {
CborDeserializer.decodeNullable(data.get(5), CborDeserializer::decodeByteString),
CborDeserializer.decodeByteString(data.get(6)),
CborDeserializer.decodeMap(data.get(7)).stream()
.collect(
Collectors.toMap(
entry -> CborDeserializer.decodeTextString(entry.getKey()),
entry -> CborDeserializer.decodeByteString(entry.getValue()
)
)
)
.map(entry -> new SignatureEntry(
CborDeserializer.decodeTextString(entry.getKey()),
CborDeserializer.decodeByteString(entry.getValue())
))
.collect(Collectors.toSet())
);
}

Expand All @@ -217,10 +187,10 @@ public byte[] toCbor() {
this.signatures,
(signatures) -> CborSerializer.encodeMap(
new CborMap(
signatures.entrySet().stream()
signatures.stream()
.map(entry -> new CborMap.Entry(
CborSerializer.encodeTextString(entry.getKey()),
CborSerializer.encodeByteString(entry.getValue())
CborSerializer.encodeByteString(entry.getSignature())
)
)
.collect(Collectors.toSet())
Expand Down Expand Up @@ -254,12 +224,13 @@ public boolean equals(Object o) {
return false;
}
UnicitySeal that = (UnicitySeal) o;
return Objects.equals(this.networkId,
that.networkId) && Objects.equals(this.rootChainRoundNumber, that.rootChainRoundNumber)
&& Objects.equals(this.epoch, that.epoch) && Objects.equals(this.timestamp,
that.timestamp) && Objects.deepEquals(this.previousHash, that.previousHash)
&& Objects.deepEquals(this.hash, that.hash) && Objects.equals(this.signatures,
that.signatures);
return Objects.equals(this.networkId, that.networkId)
&& Objects.equals(this.rootChainRoundNumber, that.rootChainRoundNumber)
&& Objects.equals(this.epoch, that.epoch)
&& Objects.equals(this.timestamp, that.timestamp)
&& Objects.deepEquals(this.previousHash, that.previousHash)
&& Objects.deepEquals(this.hash, that.hash)
&& Objects.equals(this.signatures, that.signatures);
}

@Override
Expand All @@ -280,11 +251,42 @@ public String toString() {
this.timestamp,
this.previousHash != null ? HexConverter.encode(this.previousHash) : null,
HexConverter.encode(this.hash),
this.signatures.entrySet()
.stream()
.map(entry -> String.format("%s: %s", entry.getKey(),
HexConverter.encode(entry.getValue())))
.collect(Collectors.toList())
this.signatures.stream().map(SignatureEntry::toString).collect(Collectors.toList())
);
}

public static final class SignatureEntry {
private final String key;
private final byte[] signature;

SignatureEntry(String key, byte[] signature) {
this.key = key;
this.signature = signature;
}

public String getKey() {
return this.key;
}

public byte[] getSignature() {
return Arrays.copyOf(this.signature, this.signature.length);
}

@Override
public boolean equals(Object o) {
if (!(o instanceof SignatureEntry)) return false;
SignatureEntry that = (SignatureEntry) o;
return Objects.equals(this.key, that.key) && Objects.deepEquals(this.signature, that.signature);
}

@Override
public int hashCode() {
return Objects.hash(this.key);
}

@Override
public String toString() {
return String.format("SignatureEntry{key=%s, signature=%s}", this.key, HexConverter.encode(this.signature));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,8 @@ public boolean equals(Object o) {
return false;
}
UnicityTreeCertificate that = (UnicityTreeCertificate) o;
return Objects.equals(
this.partitionIdentifier, that.partitionIdentifier) && Objects.equals(this.steps,
that.steps);
return Objects.equals(this.partitionIdentifier, that.partitionIdentifier)
&& Objects.equals(this.steps, that.steps);
}

@Override
Expand Down Expand Up @@ -187,8 +186,8 @@ public boolean equals(Object o) {
return false;
}
HashStep hashStep = (HashStep) o;
return Objects.equals(this.key, hashStep.key) && Objects.deepEquals(this.hash,
hashStep.hash);
return Objects.equals(this.key, hashStep.key)
&& Objects.deepEquals(this.hash, hashStep.hash);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* Rule to verify that the UnicitySeal contains valid quorum signatures.
Expand All @@ -37,9 +36,9 @@ public static VerificationResult<VerificationStatus> verify(RootTrustBase trustB
.update(unicitySeal.toCborWithoutSignatures())
.digest();
int successful = 0;
for (Map.Entry<String, byte[]> entry : unicitySeal.getSignatures().entrySet()) {
for (UnicitySeal.SignatureEntry entry : unicitySeal.getSignatures()) {
String nodeId = entry.getKey();
byte[] signature = entry.getValue();
byte[] signature = entry.getSignature();

VerificationResult<?> result = UnicitySealQuorumSignaturesVerificationRule.verifySignature(
trustBase,
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/org/unicitylabs/sdk/predicate/Predicate.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.unicitylabs.sdk.predicate;

import java.util.Arrays;
import java.util.Objects;

/**
* Base contract for all predicate implementations.
Expand Down Expand Up @@ -29,18 +30,13 @@ public interface Predicate {
byte[] encodeParameters();

/**
* Compares this predicate with another predicate using encoded representation.
*
* @param other the predicate to compare against
* @return {@code true} when engine, code, and parameters are equal; otherwise {@code false}
* Checks if two predicates are equal.
* @param a first predicate
* @param b second predicate
* @return {@code true} if predicates are equal, {@code false} otherwise
*/
default boolean isEqualTo(Predicate other) {
if (other == null) {
return false;
}

return this.getEngine() == other.getEngine()
&& Arrays.equals(this.encodeCode(), other.encodeCode())
&& Arrays.equals(this.encodeParameters(), other.encodeParameters());
static boolean areEqual(Predicate a, Predicate b) {
return a.getEngine() == b.getEngine() && Arrays.equals(a.encodeCode(), b.encodeCode()) && Arrays.equals(
a.encodeParameters(), b.encodeParameters());
}
}
Loading
Loading