11package org.ton.sdk.cell.boc
22
3+ import kotlinx.io.Buffer
4+ import kotlinx.io.readByteArray
35import org.ton.sdk.cell.Cell
6+ import org.ton.sdk.cell.CellContext
7+ import kotlin.jvm.JvmField
48import kotlin.jvm.JvmOverloads
59import kotlin.jvm.JvmStatic
610
@@ -37,6 +41,72 @@ public abstract class BagOfCells {
3741 }
3842 }
3943
44+ public class EncodeOptions internal constructor(
45+ /* *
46+ * Enables bag-of-cells index creation
47+ *
48+ * (useful for lazy deserialization of large bags of cells).
49+ */
50+ public val withIndex : Boolean ,
51+
52+ /* *
53+ * includes the CRC32-C of all data into the serialization
54+ *
55+ * (useful for checking data integrity).
56+ */
57+ public val withCrc32c : Boolean ,
58+
59+ /* *
60+ * Explicitly stores the hash of the root cell into the serialization
61+ * (so that it can be quickly recovered afterwards without a complete deserialization).
62+ */
63+ public val withTopHashes : Boolean ,
64+
65+ /* *
66+ * Stores hashes of some intermediate (non-leaf) cells
67+ * (useful for lazy deserialization of large bags of cells).
68+ */
69+ public val withInternalHashes : Boolean ,
70+
71+ /* *
72+ * Stores cell cache bits to control caching of deserialized cells.
73+ */
74+ public val withCacheBits : Boolean
75+ ) {
76+ public class Builder {
77+ public var withIndex: Boolean = false
78+
79+ public var withCrc32c: Boolean = false
80+
81+ public var withTopHash: Boolean = false
82+
83+ public var withInternalHashes: Boolean = false
84+
85+ public var withCacheBits: Boolean = false
86+
87+ public fun build (): EncodeOptions {
88+ return EncodeOptions (
89+ withIndex = withIndex,
90+ withCrc32c = withCrc32c,
91+ withTopHashes = withTopHash,
92+ withInternalHashes = withInternalHashes,
93+ withCacheBits = withCacheBits
94+ )
95+ }
96+ }
97+
98+ public companion object {
99+ @JvmField
100+ public val Default : EncodeOptions = EncodeOptions (
101+ withIndex = false ,
102+ withCrc32c = false ,
103+ withTopHashes = false ,
104+ withInternalHashes = false ,
105+ withCacheBits = false
106+ )
107+ }
108+ }
109+
40110 public companion object {
41111 @JvmStatic
42112 @JvmOverloads
@@ -49,5 +119,28 @@ public abstract class BagOfCells {
49119 boc.getRootCell(it)
50120 }
51121 }
122+
123+ @JvmStatic
124+ @JvmOverloads
125+ public fun encodeToByteArray (
126+ cell : Cell ,
127+ options : EncodeOptions = EncodeOptions .Default
128+ ): ByteArray = encodeToByteArray(arrayOf(cell), options)
129+
130+ @JvmStatic
131+ @JvmOverloads
132+ public fun encodeToByteArray (
133+ rootCells : Array <Cell >,
134+ options : EncodeOptions = EncodeOptions .Default
135+ ): ByteArray {
136+ val serializer = BagOfCellSerializer (CellContext .EMPTY )
137+ for (cell in rootCells) {
138+ serializer.addRoot(cell)
139+ }
140+ serializer.importCells()
141+ val buffer = Buffer ()
142+ serializer.serialize(buffer, options)
143+ return buffer.readByteArray()
144+ }
52145 }
53146}
0 commit comments