-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Handle serialization and deserialization of bigint/bytestring wh…
…en length > 64 (#398) * fix: Handle serialization and deserialization of bigint as ByteString * chore: Remove encodeAsByteString() and only handle BS to BigInt decoding * fix: #399 Handle encoding/decoding of BigInt and ByteString when length > 64
- Loading branch information
Showing
10 changed files
with
396 additions
and
23 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
common/src/main/java/com/bloxbean/cardano/client/common/cbor/custom/ChunkedByteString.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.bloxbean.cardano.client.common.cbor.custom; | ||
|
||
import co.nstant.in.cbor.model.ByteString; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* This class is used to represent a ByteString which is chunked into multiple byte arrays. Each chunk length is less than or equal to 64 bytes | ||
* This is used in PlutusData serialization/deserialization when the byte array is greater than 64 bytes and need to be chunked. | ||
* For more details, how chunking is done in Cardano, refer the below link | ||
* https://github.com/IntersectMBO/plutus/blob/441b76d9e9745dfedb2afc29920498bdf632f162/plutus-core/plutus-core/src/PlutusCore/Data.hs#L243 | ||
*/ | ||
public class ChunkedByteString extends ByteString { | ||
private List<byte[]> chunks; | ||
|
||
public ChunkedByteString(List<byte[]> chunks) { | ||
super(new byte[0]); | ||
this.chunks = chunks; | ||
setChunked(true); | ||
} | ||
|
||
public List<byte[]> getChunks() { | ||
return chunks; | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...src/main/java/com/bloxbean/cardano/client/common/cbor/custom/CustomByteStringEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.bloxbean.cardano.client.common.cbor.custom; | ||
|
||
import co.nstant.in.cbor.CborEncoder; | ||
import co.nstant.in.cbor.CborException; | ||
import co.nstant.in.cbor.encoder.ByteStringEncoder; | ||
import co.nstant.in.cbor.model.ByteString; | ||
import co.nstant.in.cbor.model.MajorType; | ||
import co.nstant.in.cbor.model.SimpleValue; | ||
|
||
import java.io.OutputStream; | ||
import java.util.List; | ||
|
||
/** | ||
* A custom ByteStringEncoder impl to handle serialization of {@link ChunkedByteString} in PlutusData. | ||
*/ | ||
public class CustomByteStringEncoder extends ByteStringEncoder { | ||
public CustomByteStringEncoder(CborEncoder encoder, OutputStream outputStream) { | ||
super(encoder, outputStream); | ||
} | ||
|
||
@Override | ||
public void encode(ByteString byteString) throws CborException { | ||
if (byteString instanceof ChunkedByteString) { | ||
List<byte[]> chunks = ((ChunkedByteString) byteString).getChunks(); | ||
this.encodeTypeChunked(MajorType.BYTE_STRING); | ||
for (byte[] chunk : chunks) { | ||
this.encode(new ByteString(chunk)); | ||
} | ||
this.encoder.encode(SimpleValue.BREAK); | ||
} else { | ||
super.encode(byteString); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.