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
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 @@ -100,8 +100,8 @@ public byte[] toCbor() {
InclusionProof.CBOR_TAG,
CborSerializer.encodeArray(
CborSerializer.encodeUnsignedInteger(InclusionProof.VERSION),
CborSerializer.encodeOptional(this.certificationData, CertificationData::toCbor),
CborSerializer.encodeOptional(this.inclusionCertificate, (inclusionCertificate) ->
CborSerializer.encodeNullable(this.certificationData, CertificationData::toCbor),
CborSerializer.encodeNullable(this.inclusionCertificate, (inclusionCertificate) ->
CborSerializer.encodeByteString(inclusionCertificate.encode())
),
this.unicityCertificate.toCbor()
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/unicitylabs/sdk/api/bft/InputRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,13 @@ public byte[] toCbor() {
CborSerializer.encodeUnsignedInteger(InputRecord.VERSION),
CborSerializer.encodeUnsignedInteger(this.roundNumber),
CborSerializer.encodeUnsignedInteger(this.epoch),
CborSerializer.encodeOptional(this.previousHash, CborSerializer::encodeByteString),
CborSerializer.encodeNullable(this.previousHash, CborSerializer::encodeByteString),
CborSerializer.encodeByteString(this.hash),
CborSerializer.encodeByteString(this.summaryValue),
CborSerializer.encodeUnsignedInteger(this.timestamp),
CborSerializer.encodeOptional(this.blockHash, CborSerializer::encodeByteString),
CborSerializer.encodeNullable(this.blockHash, CborSerializer::encodeByteString),
CborSerializer.encodeUnsignedInteger(this.sumOfEarnedFees),
CborSerializer.encodeOptional(this.executedTransactionsHash,
CborSerializer.encodeNullable(this.executedTransactionsHash,
CborSerializer::encodeByteString)
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static DataHash calculateShardTreeCertificateRootHash(
DataHash rootHash = new DataHasher(HashAlgorithm.SHA256)
.update(inputRecord.toCbor())
.update(
CborSerializer.encodeOptional(technicalRecordHash, CborSerializer::encodeByteString))
CborSerializer.encodeNullable(technicalRecordHash, CborSerializer::encodeByteString))
.update(CborSerializer.encodeByteString(shardConfigurationHash))
.digest();

Expand Down Expand Up @@ -201,7 +201,7 @@ public byte[] toCbor() {
CborSerializer.encodeArray(
CborSerializer.encodeUnsignedInteger(UnicityCertificate.VERSION),
this.inputRecord.toCbor(),
CborSerializer.encodeOptional(this.technicalRecordHash,
CborSerializer.encodeNullable(this.technicalRecordHash,
CborSerializer::encodeByteString),
CborSerializer.encodeByteString(this.shardConfigurationHash),
this.shardTreeCertificate.toCbor(),
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 @@ -211,9 +211,9 @@ public byte[] toCbor() {
CborSerializer.encodeUnsignedInteger(this.rootChainRoundNumber),
CborSerializer.encodeUnsignedInteger(this.epoch),
CborSerializer.encodeUnsignedInteger(this.timestamp),
CborSerializer.encodeOptional(this.previousHash, CborSerializer::encodeByteString),
CborSerializer.encodeNullable(this.previousHash, CborSerializer::encodeByteString),
CborSerializer.encodeByteString(this.hash),
CborSerializer.encodeOptional(
CborSerializer.encodeNullable(
this.signatures,
(signatures) -> CborSerializer.encodeMap(
new CborMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
import org.unicitylabs.sdk.smt.sum.SparseMerkleSumTreePath;

import java.util.List;
import java.util.Objects;

/**
* Proof material for one split reason entry.
*/
public class SplitReasonProof {
public final class SplitAssetProof {
private final AssetId assetId;
private final SparseMerkleTreePath aggregationPath;
private final SparseMerkleSumTreePath assetTreePath;

private SplitReasonProof(
private SplitAssetProof(
AssetId assetId,
SparseMerkleTreePath aggregationPath,
SparseMerkleSumTreePath assetTreePath
Expand Down Expand Up @@ -62,12 +63,12 @@ public SparseMerkleSumTreePath getAssetTreePath() {
*
* @return split reason proof
*/
public static SplitReasonProof create(
public static SplitAssetProof create(
AssetId assetId,
SparseMerkleTreePath aggregationPath,
SparseMerkleSumTreePath assetTreePath
) {
return new SplitReasonProof(assetId, aggregationPath, assetTreePath);
return new SplitAssetProof(assetId, aggregationPath, assetTreePath);
}

/**
Expand All @@ -77,10 +78,10 @@ public static SplitReasonProof create(
*
* @return split reason proof
*/
public static SplitReasonProof fromCbor(byte[] bytes) {
public static SplitAssetProof fromCbor(byte[] bytes) {
List<byte[]> data = CborDeserializer.decodeArray(bytes);

return new SplitReasonProof(
return new SplitAssetProof(
AssetId.fromCbor(data.get(0)),
SparseMerkleTreePath.fromCbor(data.get(1)),
SparseMerkleSumTreePath.fromCbor(data.get(2))
Expand All @@ -99,4 +100,16 @@ public byte[] toCbor() {
this.assetTreePath.toCbor()
);
}

@Override
public boolean equals(Object o) {
if (!(o instanceof SplitAssetProof)) return false;
SplitAssetProof that = (SplitAssetProof) o;
return Objects.equals(this.assetId, that.assetId);
}

@Override
public int hashCode() {
return Objects.hashCode(this.assetId);
}
}
101 changes: 101 additions & 0 deletions src/main/java/org/unicitylabs/sdk/payment/SplitMintJustification.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package org.unicitylabs.sdk.payment;

import org.unicitylabs.sdk.serializer.cbor.CborDeserializer;
import org.unicitylabs.sdk.serializer.cbor.CborSerializationException;
import org.unicitylabs.sdk.serializer.cbor.CborSerializer;
import org.unicitylabs.sdk.transaction.Token;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Mint justification for a split-output token, carrying the burn token of the source and the
* inclusion proofs that link each output asset back to the burned source aggregation tree.
*/
public final class SplitMintJustification {
public static final long CBOR_TAG = 39044;

private final Token token;
private final List<SplitAssetProof> proofs;

private SplitMintJustification(
Token token,
List<SplitAssetProof> proofs
) {
this.token = token;
this.proofs = proofs;
}

/**
* Get the burn token whose split produced this justification.
*
* @return burn token
*/
public Token getToken() {
return this.token;
}

/**
* Get the inclusion proofs supporting this split mint justification.
*
* @return proofs
*/
public List<SplitAssetProof> getProofs() {
return this.proofs;
}

/**
* Create a split mint justification.
*
* @param token burn token of the source token being split
* @param proofs inclusion proofs supporting split eligibility
*
* @return split mint justification
*/
public static SplitMintJustification create(Token token, Set<SplitAssetProof> proofs) {
Objects.requireNonNull(token, "token cannot be null");
Objects.requireNonNull(proofs, "proofs cannot be null");

if (proofs.isEmpty()) {
throw new IllegalArgumentException("proofs cannot be empty");
}

return new SplitMintJustification(token, List.copyOf(proofs));
}

/**
* Deserialize split mint justification from CBOR bytes.
*
* @param bytes CBOR bytes
*
* @return split mint justification
*/
public static SplitMintJustification fromCbor(byte[] bytes) {
CborDeserializer.CborTag tag = CborDeserializer.decodeTag(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());
return SplitMintJustification.create(
Token.fromCbor(data.get(0)),
CborDeserializer.decodeArray(data.get(1)).stream().map(SplitAssetProof::fromCbor).collect(Collectors.toSet())
);
}

/**
* Serialize split mint justification to CBOR bytes.
*
* @return CBOR bytes
*/
public byte[] toCbor() {
return CborSerializer.encodeTag(
SplitMintJustification.CBOR_TAG,
CborSerializer.encodeArray(
this.token.toCbor(),
CborSerializer.encodeArray(this.proofs.stream().map(SplitAssetProof::toCbor).toArray(byte[][]::new))
)
);
}
}
Loading
Loading