├── LICENSE ├── README.md ├── package.json ├── samples ├── SerialitySample.sol └── StringsReturn.sol ├── src ├── BytesToTypes.sol ├── Seriality.sol ├── SizeOf.sol └── TypesToBytes.sol └── tests ├── SerialitySample.sol └── StringsReturn.sol /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Seriality 2 | 3 | Seriality is a library for serializing and de-serializing all the Solidity types in a very efficient way which mostly written in solidity-assembly. 4 | 5 | --- 6 | 7 | #### Important points you need to know befor using this library 8 | 9 | - Seriality supports all the solidity types. 10 | 11 | - Please be aware that this library is written with assembly and could be unsafe, so use it if you really know what you are doing and how you should use the library. 12 | 13 | - The buffer size must be chosen very carefully, if you are not sure about that choose your biggest guess. 14 | 15 | - You must start serializing from the end of the buffer as well as deserializing. 16 | 17 | - You can use the SizeOf library independently to get the size of your variables. 18 | 19 | --- 20 | 21 | #### What kind of problems can be addressed by Seriality? 22 | 23 | (Proposal if the solidity allows passing dynamically sized type through contracts and libraries) 24 | 25 | - By means of Seriality you can easily serialize and deserialize your variables, structs, arrays, tuples, ... and pass them through the contracts and libraries. 26 | 27 | - You can decouple your contract from libraries by serializing parameters into a byte array. 28 | 29 | - It also can be used as an alternative for RLP protocol in Solidity. 30 | 31 | #### Example 32 | 33 | ```solidity 34 | 35 | pragma solidity ^0.5.0; 36 | 37 | import {Seriality} from "./Seriality.sol"; 38 | 39 | 40 | contract SerialitySample is Seriality { 41 | 42 | function testSample1() public pure returns(int n1, int8 n2, uint24 n3, string memory n4, string memory n5) { 43 | 44 | bytes memory buffer = new bytes(200); 45 | string memory out4 = new string(32); 46 | string memory out5 = new string(32); 47 | n4 = new string(32); 48 | n5 = new string(32); 49 | int out1 = 34444445; 50 | int8 out2 = 87; 51 | uint24 out3 = 76545; 52 | out4 = "Copy kon lashi"; 53 | out5 = "Bia inja dahan service"; 54 | 55 | // Serializing 56 | uint offset = 200; 57 | 58 | intToBytes(offset, out2, buffer); 59 | offset -= sizeOfInt(8); 60 | 61 | uintToBytes(offset, out3, buffer); 62 | offset -= sizeOfUint(24); 63 | 64 | stringToBytes(offset, bytes(out5), buffer); 65 | offset -= sizeOfString(out5); 66 | 67 | stringToBytes(offset, bytes(out4), buffer); 68 | offset -= sizeOfString(out4); 69 | 70 | intToBytes(offset, out1, buffer); 71 | offset -= sizeOfInt(256); 72 | 73 | // Deserializing 74 | offset = 200; 75 | 76 | n2 = bytesToInt8(offset, buffer); 77 | offset -= sizeOfInt(8); 78 | 79 | n3 = bytesToUint24(offset, buffer); 80 | offset -= sizeOfUint(24); 81 | 82 | bytesToString(offset, buffer, bytes(n5)); 83 | offset -= sizeOfString(out5); 84 | 85 | bytesToString(offset, buffer, bytes(n4)); 86 | offset -= sizeOfString(out4); 87 | 88 | n1 = bytesToInt256(offset, buffer); 89 | 90 | } 91 | 92 | function testSample2() public pure returns(int8 n1, int24 n2, uint32 n3, int128 n4, address n5, address n6) { 93 | 94 | bytes memory buffer = new bytes(64); 95 | int8 out1 = -12; 96 | int24 out2 = 838860; 97 | uint32 out3 = 123; 98 | int128 out4 = -44444444444; 99 | address out5 = 0x90c2EA76336410195498E76E28c80922Bb495B53; 100 | address out6 = 0x313b1C7b270492214e011B7D36fde0Eb072A2798; 101 | 102 | // Serializing 103 | uint offset = 64; 104 | 105 | intToBytes(offset, out1, buffer); 106 | offset -= sizeOfInt(8); 107 | 108 | intToBytes(offset, out2, buffer); 109 | offset -= sizeOfUint(24); 110 | 111 | uintToBytes(offset, out3, buffer); 112 | offset -= sizeOfInt(32); 113 | 114 | intToBytes(offset, out4, buffer); 115 | offset -= sizeOfUint(128); 116 | 117 | addressToBytes(offset, out5, buffer); 118 | offset -= sizeOfAddress(); 119 | 120 | addressToBytes(offset, out6, buffer); 121 | 122 | // Deserializing 123 | offset = 64; 124 | 125 | n1 = bytesToInt8(offset, buffer); 126 | offset -= sizeOfInt(8); 127 | 128 | n2 = bytesToInt24(offset, buffer); 129 | offset -= sizeOfUint(24); 130 | 131 | n3 = bytesToUint8(offset, buffer); 132 | offset -= sizeOfInt(32); 133 | 134 | n4 = bytesToInt128(offset, buffer); 135 | offset -= sizeOfUint(128); 136 | 137 | n5 = bytesToAddress(offset, buffer); 138 | offset -= sizeOfAddress(); 139 | 140 | n6 = bytesToAddress(offset, buffer); 141 | 142 | } 143 | } 144 | 145 | ``` 146 | 147 | ```bash 148 | // testSample1 output: 149 | { 150 | "0": "int256: n1 34444445", 151 | "1": "int8: n2 87", 152 | "2": "uint24: n3 76545", 153 | "3": "string: n4 Copy kon lashi", 154 | "4": "string: n5 Bia inja dahan service" 155 | } 156 | 157 | // testSample2: 158 | { 159 | "0": "int8: n1 -12", 160 | "1": "int24: n2 838860", 161 | "2": "uint32: n3 123", 162 | "3": "int128: n4 -44444444444", 163 | "4": "address: n5 0x90c2EA76336410195498E76E28c80922Bb495B53", 164 | "5": "address: n6 0x313b1C7b270492214e011B7D36fde0Eb072A2798" 165 | } 166 | 167 | ``` 168 | 169 | #### Serializing types including strings 170 | 171 | ```solidity 172 | pragma solidity ^0.5.0; 173 | 174 | import "./Seriality.sol"; 175 | 176 | contract StringsReturn is Seriality { 177 | 178 | function stringCaller() public pure returns( string memory out1, 179 | string memory out2, 180 | string memory out3, 181 | string memory out4, 182 | string memory out5) 183 | { 184 | 185 | bytes memory buffer = new bytes(320); 186 | uint offset = stringCallee(buffer); 187 | 188 | //deserializing 189 | out1 = new string (getStringSize(offset, buffer)); 190 | bytesToString(offset, buffer, bytes(out1)); 191 | offset -= sizeOfString(out1); 192 | 193 | out2 = new string (getStringSize(offset, buffer)); 194 | bytesToString(offset, buffer, bytes(out2)); 195 | offset -= sizeOfString(out2); 196 | 197 | out3 = new string (getStringSize(offset, buffer)); 198 | bytesToString(offset, buffer, bytes(out3)); 199 | offset -= sizeOfString(out3); 200 | 201 | out4 = new string (getStringSize(offset, buffer)); 202 | bytesToString(offset, buffer, bytes(out4)); 203 | offset -= sizeOfString(out4); 204 | 205 | out5 = new string (getStringSize(offset, buffer)); 206 | bytesToString(offset, buffer, bytes(out5)); 207 | 208 | } 209 | 210 | function stringCallee(bytes memory buffer) public pure returns (uint buffer_size) { 211 | 212 | string memory out1 = new string(32); 213 | string memory out2 = new string(32); 214 | string memory out3 = new string(32); 215 | string memory out4 = new string(32); 216 | string memory out5 = new string(32); 217 | 218 | out1 = "Come on baby lets dance!"; 219 | out2 = "May I buy you a drink?"; 220 | out3 = "I am an itinerant programmer"; 221 | out4 = "Inam javab lashi!"; 222 | out5 = "Bia inja dahan service"; 223 | 224 | // Serializing 225 | buffer_size = sizeOfString(out5) + 226 | sizeOfString(out4) + 227 | sizeOfString(out3) + 228 | sizeOfString(out2) + 229 | sizeOfString(out1); 230 | 231 | uint offset = buffer_size; 232 | 233 | stringToBytes(offset, bytes(out1), buffer); 234 | offset -= sizeOfString(out1); 235 | 236 | stringToBytes(offset, bytes(out2), buffer); 237 | offset -= sizeOfString(out2); 238 | 239 | stringToBytes(offset, bytes(out3), buffer); 240 | offset -= sizeOfString(out3); 241 | 242 | stringToBytes(offset, bytes(out4), buffer); 243 | offset -= sizeOfString(out4); 244 | 245 | stringToBytes(offset, bytes(out5), buffer); 246 | 247 | return buffer_size; 248 | 249 | } 250 | 251 | } 252 | 253 | ``` 254 | 255 | ```bash 256 | // output buffer : 257 | 258 | [42696120696e6a6120646168616e207365727669636500000000000000000000 259 | 0000000000000000000000000000000000000000000000000000000000000016 260 | 496e616d206a61766162206c6173686921000000000000000000000000000000 261 | 0000000000000000000000000000000000000000000000000000000000000011 262 | 4920616d20616e206974696e6572616e742070726f6772616d6d657200000000 263 | 000000000000000000000000000000000000000000000000000000000000001c 264 | 4d617920492062757920796f752061206472696e6b3f00000000000000000000 265 | 0000000000000000000000000000000000000000000000000000000000000016 266 | 436f6d65206f6e2062616279206c6574732064616e6365210000000000000000 267 | 0000000000000000000000000000000000000000000000000000000000000018] 268 | 269 | "0": "string: out1 Come on baby lets dance!", 270 | "1": "string: out2 May I buy you a drink?", 271 | "2": "string: out3 I am an itinerant programmer", 272 | "3": "string: out4 Inam javab lashi!", 273 | "4": "string: out5 Bia inja dahan service" 274 | ``` 275 | 276 | There is also an article in Hacker Noon written by Vasa which explains how you can use seriality for your project. 277 | 278 | Thanks to Vasa! 279 | 280 | #### Using Seriality in your Hardhat project? 281 | 282 | Install Seriality 283 | 284 | ```bash 285 | // npm 286 | npm install git+https://github.com/pouladzade/Seriality.git 287 | 288 | // pnpm 289 | pnpm install git+https://github.com/pouladzade/Seriality.git 290 | 291 | // yarn 292 | yarn add git+https://github.com/pouladzade/Seriality.git 293 | ``` 294 | 295 | Use Seriality as a library in your Solidity code 296 | 297 | ```solidity 298 | pragma solidity ^0.5.0; 299 | 300 | import { Seriality } from "seriality/src/Seriality.sol"; 301 | ``` 302 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "seriality", 3 | "description": "Seriality is a library for serializing and de-serializing all the Solidity types in a very efficient way which mostly written in solidity-assembly.", 4 | "version": "1.0.0", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/pouladzade/Seriality.git" 8 | }, 9 | "author": "https://github.com/pouladzade", 10 | "license": "Apache License 2.0", 11 | "keywords": [ 12 | "ethereum", 13 | "serialization" 14 | ] 15 | } 16 | -------------------------------------------------------------------------------- /samples/SerialitySample.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: Apache-2.0 2 | pragma solidity ^0.8.0; 3 | 4 | import {Seriality} from "../src/Seriality.sol"; 5 | 6 | 7 | contract SerialitySample is Seriality { 8 | 9 | function testSample1() public pure returns(int n1, int8 n2, uint24 n3, string memory n4, string memory n5) { 10 | 11 | bytes memory buffer = new bytes(200); 12 | string memory out4 = new string(32); 13 | string memory out5 = new string(32); 14 | n4 = new string(32); 15 | n5 = new string(32); 16 | int out1 = 34444445; 17 | int8 out2 = 87; 18 | uint24 out3 = 76545; 19 | out4 = "Copy kon lashi"; 20 | out5 = "Bia inja dahan service"; 21 | 22 | // Serializing 23 | uint offset = 200; 24 | 25 | intToBytes(offset, out2, buffer); 26 | offset -= sizeOfInt(8); 27 | 28 | uintToBytes(offset, out3, buffer); 29 | offset -= sizeOfUint(24); 30 | 31 | stringToBytes(offset, bytes(out5), buffer); 32 | offset -= sizeOfString(out5); 33 | 34 | stringToBytes(offset, bytes(out4), buffer); 35 | offset -= sizeOfString(out4); 36 | 37 | intToBytes(offset, out1, buffer); 38 | offset -= sizeOfInt(256); 39 | 40 | // Deserializing 41 | offset = 200; 42 | 43 | n2 = bytesToInt8(offset, buffer); 44 | offset -= sizeOfInt(8); 45 | 46 | n3 = bytesToUint24(offset, buffer); 47 | offset -= sizeOfUint(24); 48 | 49 | bytesToString(offset, buffer, bytes(n5)); 50 | offset -= sizeOfString(out5); 51 | 52 | bytesToString(offset, buffer, bytes(n4)); 53 | offset -= sizeOfString(out4); 54 | 55 | n1 = bytesToInt256(offset, buffer); 56 | 57 | } 58 | 59 | function testSample2() public pure returns(int8 n1, int24 n2, uint32 n3, int128 n4, address n5, address n6) { 60 | 61 | bytes memory buffer = new bytes(64); 62 | int8 out1 = -12; 63 | int24 out2 = 838860; 64 | uint32 out3 = 123; 65 | int128 out4 = -44444444444; 66 | address out5 = 0x90c2EA76336410195498E76E28c80922Bb495B53; 67 | address out6 = 0x313b1C7b270492214e011B7D36fde0Eb072A2798; 68 | 69 | // Serializing 70 | uint offset = 64; 71 | 72 | intToBytes(offset, out1, buffer); 73 | offset -= sizeOfInt(8); 74 | 75 | intToBytes(offset, out2, buffer); 76 | offset -= sizeOfUint(24); 77 | 78 | uintToBytes(offset, out3, buffer); 79 | offset -= sizeOfInt(32); 80 | 81 | intToBytes(offset, out4, buffer); 82 | offset -= sizeOfUint(128); 83 | 84 | addressToBytes(offset, out5, buffer); 85 | offset -= sizeOfAddress(); 86 | 87 | addressToBytes(offset, out6, buffer); 88 | 89 | // Deserializing 90 | offset = 64; 91 | 92 | n1 = bytesToInt8(offset, buffer); 93 | offset -= sizeOfInt(8); 94 | 95 | n2 = bytesToInt24(offset, buffer); 96 | offset -= sizeOfUint(24); 97 | 98 | n3 = bytesToUint8(offset, buffer); 99 | offset -= sizeOfInt(32); 100 | 101 | n4 = bytesToInt128(offset, buffer); 102 | offset -= sizeOfUint(128); 103 | 104 | n5 = bytesToAddress(offset, buffer); 105 | offset -= sizeOfAddress(); 106 | 107 | n6 = bytesToAddress(offset, buffer); 108 | 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /samples/StringsReturn.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: Apache-2.0 2 | pragma solidity ^0.8.0; 3 | 4 | import "../src/Seriality.sol"; 5 | 6 | contract StringsReturn is Seriality { 7 | 8 | function stringCaller() public pure returns( string memory out1, 9 | string memory out2, 10 | string memory out3, 11 | string memory out4, 12 | string memory out5) 13 | { 14 | 15 | bytes memory buffer = new bytes(320); 16 | uint offset = stringCallee(buffer); 17 | 18 | //deserializing 19 | out1 = new string (getStringSize(offset, buffer)); 20 | bytesToString(offset, buffer, bytes(out1)); 21 | offset -= sizeOfString(out1); 22 | 23 | out2 = new string (getStringSize(offset, buffer)); 24 | bytesToString(offset, buffer, bytes(out2)); 25 | offset -= sizeOfString(out2); 26 | 27 | out3 = new string (getStringSize(offset, buffer)); 28 | bytesToString(offset, buffer, bytes(out3)); 29 | offset -= sizeOfString(out3); 30 | 31 | out4 = new string (getStringSize(offset, buffer)); 32 | bytesToString(offset, buffer, bytes(out4)); 33 | offset -= sizeOfString(out4); 34 | 35 | out5 = new string (getStringSize(offset, buffer)); 36 | bytesToString(offset, buffer, bytes(out5)); 37 | 38 | } 39 | 40 | function stringCallee(bytes memory buffer) public pure returns (uint buffer_size) { 41 | 42 | string memory out1 = new string(32); 43 | string memory out2 = new string(32); 44 | string memory out3 = new string(32); 45 | string memory out4 = new string(32); 46 | string memory out5 = new string(32); 47 | 48 | out1 = "Come on baby lets dance!"; 49 | out2 = "May I buy you a drink?"; 50 | out3 = "I am an itinerant programmer"; 51 | out4 = "Inam javab lashi!"; 52 | out5 = "Bia inja dahan service"; 53 | 54 | // Serializing 55 | buffer_size = sizeOfString(out5) + 56 | sizeOfString(out4) + 57 | sizeOfString(out3) + 58 | sizeOfString(out2) + 59 | sizeOfString(out1); 60 | 61 | uint offset = buffer_size; 62 | 63 | stringToBytes(offset, bytes(out1), buffer); 64 | offset -= sizeOfString(out1); 65 | 66 | stringToBytes(offset, bytes(out2), buffer); 67 | offset -= sizeOfString(out2); 68 | 69 | stringToBytes(offset, bytes(out3), buffer); 70 | offset -= sizeOfString(out3); 71 | 72 | stringToBytes(offset, bytes(out4), buffer); 73 | offset -= sizeOfString(out4); 74 | 75 | stringToBytes(offset, bytes(out5), buffer); 76 | 77 | return buffer_size; 78 | 79 | } 80 | 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/BytesToTypes.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: Apache-2.0 2 | pragma solidity ^0.8.0; 3 | 4 | /** 5 | * @title BytesToTypes 6 | * @dev The BytesToTypes contract converts the memory byte arrays to the standard solidity types 7 | * @author pouladzade@gmail.com 8 | */ 9 | 10 | contract BytesToTypes { 11 | 12 | 13 | function bytesToAddress(uint _offst, bytes memory _input) internal pure returns (address _output) { 14 | 15 | assembly { 16 | _output := mload(add(_input, _offst)) 17 | } 18 | } 19 | 20 | function bytesToBool(uint _offst, bytes memory _input) internal pure returns (bool _output) { 21 | 22 | uint8 x; 23 | assembly { 24 | x := mload(add(_input, _offst)) 25 | } 26 | x==0 ? _output = false : _output = true; 27 | } 28 | 29 | function getStringSize(uint _offst, bytes memory _input) internal pure returns(uint size){ 30 | 31 | assembly{ 32 | 33 | size := mload(add(_input,_offst)) 34 | let chunk_count := add(div(size,32),1) // chunk_count = size/32 + 1 35 | 36 | if gt(mod(size,32),0) {// if size%32 > 0 37 | chunk_count := add(chunk_count,1) 38 | } 39 | 40 | size := mul(chunk_count,32)// first 32 bytes reseves for size in strings 41 | } 42 | } 43 | 44 | function bytesToString(uint _offst, bytes memory _input, bytes memory _output) internal pure { 45 | 46 | uint size = 32; 47 | assembly { 48 | 49 | let chunk_count 50 | 51 | size := mload(add(_input,_offst)) 52 | chunk_count := add(div(size,32),1) // chunk_count = size/32 + 1 53 | 54 | if gt(mod(size,32),0) { 55 | chunk_count := add(chunk_count,1) // chunk_count++ 56 | } 57 | 58 | for { let index:= 0 } lt(index , chunk_count){ index := add(index,1) } { 59 | mstore(add(_output,mul(index,32)),mload(add(_input,_offst))) 60 | _offst := sub(_offst,32) // _offst -= 32 61 | } 62 | } 63 | } 64 | 65 | function bytesToBytes32(uint _offst, bytes memory _input, bytes32 _output) internal pure { 66 | 67 | assembly { 68 | mstore(_output , add(_input, _offst)) 69 | mstore(add(_output,32) , add(add(_input, _offst),32)) 70 | } 71 | } 72 | 73 | function bytesToInt8(uint _offst, bytes memory _input) internal pure returns (int8 _output) { 74 | 75 | assembly { 76 | _output := mload(add(_input, _offst)) 77 | } 78 | } 79 | 80 | function bytesToInt16(uint _offst, bytes memory _input) internal pure returns (int16 _output) { 81 | 82 | assembly { 83 | _output := mload(add(_input, _offst)) 84 | } 85 | } 86 | 87 | function bytesToInt24(uint _offst, bytes memory _input) internal pure returns (int24 _output) { 88 | 89 | assembly { 90 | _output := mload(add(_input, _offst)) 91 | } 92 | } 93 | 94 | function bytesToInt32(uint _offst, bytes memory _input) internal pure returns (int32 _output) { 95 | 96 | assembly { 97 | _output := mload(add(_input, _offst)) 98 | } 99 | } 100 | 101 | function bytesToInt40(uint _offst, bytes memory _input) internal pure returns (int40 _output) { 102 | 103 | assembly { 104 | _output := mload(add(_input, _offst)) 105 | } 106 | } 107 | 108 | function bytesToInt48(uint _offst, bytes memory _input) internal pure returns (int48 _output) { 109 | 110 | assembly { 111 | _output := mload(add(_input, _offst)) 112 | } 113 | } 114 | 115 | function bytesToInt56(uint _offst, bytes memory _input) internal pure returns (int56 _output) { 116 | 117 | assembly { 118 | _output := mload(add(_input, _offst)) 119 | } 120 | } 121 | 122 | function bytesToInt64(uint _offst, bytes memory _input) internal pure returns (int64 _output) { 123 | 124 | assembly { 125 | _output := mload(add(_input, _offst)) 126 | } 127 | } 128 | 129 | function bytesToInt72(uint _offst, bytes memory _input) internal pure returns (int72 _output) { 130 | 131 | assembly { 132 | _output := mload(add(_input, _offst)) 133 | } 134 | } 135 | 136 | function bytesToInt80(uint _offst, bytes memory _input) internal pure returns (int80 _output) { 137 | 138 | assembly { 139 | _output := mload(add(_input, _offst)) 140 | } 141 | } 142 | 143 | function bytesToInt88(uint _offst, bytes memory _input) internal pure returns (int88 _output) { 144 | 145 | assembly { 146 | _output := mload(add(_input, _offst)) 147 | } 148 | } 149 | 150 | function bytesToInt96(uint _offst, bytes memory _input) internal pure returns (int96 _output) { 151 | 152 | assembly { 153 | _output := mload(add(_input, _offst)) 154 | } 155 | } 156 | 157 | function bytesToInt104(uint _offst, bytes memory _input) internal pure returns (int104 _output) { 158 | 159 | assembly { 160 | _output := mload(add(_input, _offst)) 161 | } 162 | } 163 | 164 | function bytesToInt112(uint _offst, bytes memory _input) internal pure returns (int112 _output) { 165 | 166 | assembly { 167 | _output := mload(add(_input, _offst)) 168 | } 169 | } 170 | 171 | function bytesToInt120(uint _offst, bytes memory _input) internal pure returns (int120 _output) { 172 | 173 | assembly { 174 | _output := mload(add(_input, _offst)) 175 | } 176 | } 177 | 178 | function bytesToInt128(uint _offst, bytes memory _input) internal pure returns (int128 _output) { 179 | 180 | assembly { 181 | _output := mload(add(_input, _offst)) 182 | } 183 | } 184 | 185 | function bytesToInt136(uint _offst, bytes memory _input) internal pure returns (int136 _output) { 186 | 187 | assembly { 188 | _output := mload(add(_input, _offst)) 189 | } 190 | } 191 | 192 | function bytesToInt144(uint _offst, bytes memory _input) internal pure returns (int144 _output) { 193 | 194 | assembly { 195 | _output := mload(add(_input, _offst)) 196 | } 197 | } 198 | 199 | function bytesToInt152(uint _offst, bytes memory _input) internal pure returns (int152 _output) { 200 | 201 | assembly { 202 | _output := mload(add(_input, _offst)) 203 | } 204 | } 205 | 206 | function bytesToInt160(uint _offst, bytes memory _input) internal pure returns (int160 _output) { 207 | 208 | assembly { 209 | _output := mload(add(_input, _offst)) 210 | } 211 | } 212 | 213 | function bytesToInt168(uint _offst, bytes memory _input) internal pure returns (int168 _output) { 214 | 215 | assembly { 216 | _output := mload(add(_input, _offst)) 217 | } 218 | } 219 | 220 | function bytesToInt176(uint _offst, bytes memory _input) internal pure returns (int176 _output) { 221 | 222 | assembly { 223 | _output := mload(add(_input, _offst)) 224 | } 225 | } 226 | 227 | function bytesToInt184(uint _offst, bytes memory _input) internal pure returns (int184 _output) { 228 | 229 | assembly { 230 | _output := mload(add(_input, _offst)) 231 | } 232 | } 233 | 234 | function bytesToInt192(uint _offst, bytes memory _input) internal pure returns (int192 _output) { 235 | 236 | assembly { 237 | _output := mload(add(_input, _offst)) 238 | } 239 | } 240 | 241 | function bytesToInt200(uint _offst, bytes memory _input) internal pure returns (int200 _output) { 242 | 243 | assembly { 244 | _output := mload(add(_input, _offst)) 245 | } 246 | } 247 | 248 | function bytesToInt208(uint _offst, bytes memory _input) internal pure returns (int208 _output) { 249 | 250 | assembly { 251 | _output := mload(add(_input, _offst)) 252 | } 253 | } 254 | 255 | function bytesToInt216(uint _offst, bytes memory _input) internal pure returns (int216 _output) { 256 | 257 | assembly { 258 | _output := mload(add(_input, _offst)) 259 | } 260 | } 261 | 262 | function bytesToInt224(uint _offst, bytes memory _input) internal pure returns (int224 _output) { 263 | 264 | assembly { 265 | _output := mload(add(_input, _offst)) 266 | } 267 | } 268 | 269 | function bytesToInt232(uint _offst, bytes memory _input) internal pure returns (int232 _output) { 270 | 271 | assembly { 272 | _output := mload(add(_input, _offst)) 273 | } 274 | } 275 | 276 | function bytesToInt240(uint _offst, bytes memory _input) internal pure returns (int240 _output) { 277 | 278 | assembly { 279 | _output := mload(add(_input, _offst)) 280 | } 281 | } 282 | 283 | function bytesToInt248(uint _offst, bytes memory _input) internal pure returns (int248 _output) { 284 | 285 | assembly { 286 | _output := mload(add(_input, _offst)) 287 | } 288 | } 289 | 290 | function bytesToInt256(uint _offst, bytes memory _input) internal pure returns (int256 _output) { 291 | 292 | assembly { 293 | _output := mload(add(_input, _offst)) 294 | } 295 | } 296 | 297 | function bytesToUint8(uint _offst, bytes memory _input) internal pure returns (uint8 _output) { 298 | 299 | assembly { 300 | _output := mload(add(_input, _offst)) 301 | } 302 | } 303 | 304 | function bytesToUint16(uint _offst, bytes memory _input) internal pure returns (uint16 _output) { 305 | 306 | assembly { 307 | _output := mload(add(_input, _offst)) 308 | } 309 | } 310 | 311 | function bytesToUint24(uint _offst, bytes memory _input) internal pure returns (uint24 _output) { 312 | 313 | assembly { 314 | _output := mload(add(_input, _offst)) 315 | } 316 | } 317 | 318 | function bytesToUint32(uint _offst, bytes memory _input) internal pure returns (uint32 _output) { 319 | 320 | assembly { 321 | _output := mload(add(_input, _offst)) 322 | } 323 | } 324 | 325 | function bytesToUint40(uint _offst, bytes memory _input) internal pure returns (uint40 _output) { 326 | 327 | assembly { 328 | _output := mload(add(_input, _offst)) 329 | } 330 | } 331 | 332 | function bytesToUint48(uint _offst, bytes memory _input) internal pure returns (uint48 _output) { 333 | 334 | assembly { 335 | _output := mload(add(_input, _offst)) 336 | } 337 | } 338 | 339 | function bytesToUint56(uint _offst, bytes memory _input) internal pure returns (uint56 _output) { 340 | 341 | assembly { 342 | _output := mload(add(_input, _offst)) 343 | } 344 | } 345 | 346 | function bytesToUint64(uint _offst, bytes memory _input) internal pure returns (uint64 _output) { 347 | 348 | assembly { 349 | _output := mload(add(_input, _offst)) 350 | } 351 | } 352 | 353 | function bytesToUint72(uint _offst, bytes memory _input) internal pure returns (uint72 _output) { 354 | 355 | assembly { 356 | _output := mload(add(_input, _offst)) 357 | } 358 | } 359 | 360 | function bytesToUint80(uint _offst, bytes memory _input) internal pure returns (uint80 _output) { 361 | 362 | assembly { 363 | _output := mload(add(_input, _offst)) 364 | } 365 | } 366 | 367 | function bytesToUint88(uint _offst, bytes memory _input) internal pure returns (uint88 _output) { 368 | 369 | assembly { 370 | _output := mload(add(_input, _offst)) 371 | } 372 | } 373 | 374 | function bytesToUint96(uint _offst, bytes memory _input) internal pure returns (uint96 _output) { 375 | 376 | assembly { 377 | _output := mload(add(_input, _offst)) 378 | } 379 | } 380 | 381 | function bytesToUint104(uint _offst, bytes memory _input) internal pure returns (uint104 _output) { 382 | 383 | assembly { 384 | _output := mload(add(_input, _offst)) 385 | } 386 | } 387 | 388 | function bytesToUint112(uint _offst, bytes memory _input) internal pure returns (uint112 _output) { 389 | 390 | assembly { 391 | _output := mload(add(_input, _offst)) 392 | } 393 | } 394 | 395 | function bytesToUint120(uint _offst, bytes memory _input) internal pure returns (uint120 _output) { 396 | 397 | assembly { 398 | _output := mload(add(_input, _offst)) 399 | } 400 | } 401 | 402 | function bytesToUint128(uint _offst, bytes memory _input) internal pure returns (uint128 _output) { 403 | 404 | assembly { 405 | _output := mload(add(_input, _offst)) 406 | } 407 | } 408 | 409 | function bytesToUint136(uint _offst, bytes memory _input) internal pure returns (uint136 _output) { 410 | 411 | assembly { 412 | _output := mload(add(_input, _offst)) 413 | } 414 | } 415 | 416 | function bytesToUint144(uint _offst, bytes memory _input) internal pure returns (uint144 _output) { 417 | 418 | assembly { 419 | _output := mload(add(_input, _offst)) 420 | } 421 | } 422 | 423 | function bytesToUint152(uint _offst, bytes memory _input) internal pure returns (uint152 _output) { 424 | 425 | assembly { 426 | _output := mload(add(_input, _offst)) 427 | } 428 | } 429 | 430 | function bytesToUint160(uint _offst, bytes memory _input) internal pure returns (uint160 _output) { 431 | 432 | assembly { 433 | _output := mload(add(_input, _offst)) 434 | } 435 | } 436 | 437 | function bytesToUint168(uint _offst, bytes memory _input) internal pure returns (uint168 _output) { 438 | 439 | assembly { 440 | _output := mload(add(_input, _offst)) 441 | } 442 | } 443 | 444 | function bytesToUint176(uint _offst, bytes memory _input) internal pure returns (uint176 _output) { 445 | 446 | assembly { 447 | _output := mload(add(_input, _offst)) 448 | } 449 | } 450 | 451 | function bytesToUint184(uint _offst, bytes memory _input) internal pure returns (uint184 _output) { 452 | 453 | assembly { 454 | _output := mload(add(_input, _offst)) 455 | } 456 | } 457 | 458 | function bytesToUint192(uint _offst, bytes memory _input) internal pure returns (uint192 _output) { 459 | 460 | assembly { 461 | _output := mload(add(_input, _offst)) 462 | } 463 | } 464 | 465 | function bytesToUint200(uint _offst, bytes memory _input) internal pure returns (uint200 _output) { 466 | 467 | assembly { 468 | _output := mload(add(_input, _offst)) 469 | } 470 | } 471 | 472 | function bytesToUint208(uint _offst, bytes memory _input) internal pure returns (uint208 _output) { 473 | 474 | assembly { 475 | _output := mload(add(_input, _offst)) 476 | } 477 | } 478 | 479 | function bytesToUint216(uint _offst, bytes memory _input) internal pure returns (uint216 _output) { 480 | 481 | assembly { 482 | _output := mload(add(_input, _offst)) 483 | } 484 | } 485 | 486 | function bytesToUint224(uint _offst, bytes memory _input) internal pure returns (uint224 _output) { 487 | 488 | assembly { 489 | _output := mload(add(_input, _offst)) 490 | } 491 | } 492 | 493 | function bytesToUint232(uint _offst, bytes memory _input) internal pure returns (uint232 _output) { 494 | 495 | assembly { 496 | _output := mload(add(_input, _offst)) 497 | } 498 | } 499 | 500 | function bytesToUint240(uint _offst, bytes memory _input) internal pure returns (uint240 _output) { 501 | 502 | assembly { 503 | _output := mload(add(_input, _offst)) 504 | } 505 | } 506 | 507 | function bytesToUint248(uint _offst, bytes memory _input) internal pure returns (uint248 _output) { 508 | 509 | assembly { 510 | _output := mload(add(_input, _offst)) 511 | } 512 | } 513 | 514 | function bytesToUint256(uint _offst, bytes memory _input) internal pure returns (uint256 _output) { 515 | 516 | assembly { 517 | _output := mload(add(_input, _offst)) 518 | } 519 | } 520 | 521 | } 522 | -------------------------------------------------------------------------------- /src/Seriality.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: Apache-2.0 2 | pragma solidity ^0.8.0; 3 | 4 | /** 5 | * @title Seriality 6 | * @dev The Seriality contract is the main interface for serializing data using the TypeToBytes, BytesToType and SizeOf 7 | * @author pouladzade@gmail.com 8 | */ 9 | 10 | import "./BytesToTypes.sol"; 11 | import "./TypesToBytes.sol"; 12 | import "./SizeOf.sol"; 13 | 14 | contract Seriality is BytesToTypes, TypesToBytes, SizeOf { 15 | 16 | constructor() {} 17 | } 18 | -------------------------------------------------------------------------------- /src/SizeOf.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: Apache-2.0 2 | pragma solidity ^0.8.0; 3 | 4 | /** 5 | * @title SizeOf 6 | * @dev The SizeOf return the size of the solidity types in byte 7 | * @author pouladzade@gmail.com 8 | */ 9 | 10 | contract SizeOf { 11 | 12 | function sizeOfString(string memory _in) internal pure returns(uint _size){ 13 | _size = bytes(_in).length / 32; 14 | if(bytes(_in).length % 32 != 0) 15 | _size++; 16 | 17 | _size++; // first 32 bytes is reserved for the size of the string 18 | _size *= 32; 19 | } 20 | 21 | function sizeOfInt(uint16 _postfix) internal pure returns(uint size){ 22 | 23 | assembly{ 24 | switch _postfix 25 | case 8 { size := 1 } 26 | case 16 { size := 2 } 27 | case 24 { size := 3 } 28 | case 32 { size := 4 } 29 | case 40 { size := 5 } 30 | case 48 { size := 6 } 31 | case 56 { size := 7 } 32 | case 64 { size := 8 } 33 | case 72 { size := 9 } 34 | case 80 { size := 10 } 35 | case 88 { size := 11 } 36 | case 96 { size := 12 } 37 | case 104 { size := 13 } 38 | case 112 { size := 14 } 39 | case 120 { size := 15 } 40 | case 128 { size := 16 } 41 | case 136 { size := 17 } 42 | case 144 { size := 18 } 43 | case 152 { size := 19 } 44 | case 160 { size := 20 } 45 | case 168 { size := 21 } 46 | case 176 { size := 22 } 47 | case 184 { size := 23 } 48 | case 192 { size := 24 } 49 | case 200 { size := 25 } 50 | case 208 { size := 26 } 51 | case 216 { size := 27 } 52 | case 224 { size := 28 } 53 | case 232 { size := 29 } 54 | case 240 { size := 30 } 55 | case 248 { size := 31 } 56 | case 256 { size := 32 } 57 | default { size := 32 } 58 | } 59 | 60 | } 61 | 62 | function sizeOfUint(uint16 _postfix) internal pure returns(uint size){ 63 | return sizeOfInt(_postfix); 64 | } 65 | 66 | function sizeOfAddress() internal pure returns(uint8){ 67 | return 20; 68 | } 69 | 70 | function sizeOfBool() internal pure returns(uint8){ 71 | return 1; 72 | } 73 | 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/TypesToBytes.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: Apache-2.0 2 | pragma solidity ^0.8.0; 3 | 4 | /** 5 | * @title TypesToBytes 6 | * @dev The TypesToBytes contract converts the standard solidity types to the byte array 7 | * @author pouladzade@gmail.com 8 | */ 9 | 10 | contract TypesToBytes { 11 | 12 | constructor() { 13 | 14 | } 15 | function addressToBytes(uint _offst, address _input, bytes memory _output) internal pure { 16 | 17 | assembly { 18 | mstore(add(_output, _offst), _input) 19 | } 20 | } 21 | 22 | function bytes32ToBytes(uint _offst, bytes32 _input, bytes memory _output) internal pure { 23 | 24 | assembly { 25 | mstore(add(_output, _offst), _input) 26 | mstore(add(add(_output, _offst),32), add(_input,32)) 27 | } 28 | } 29 | 30 | function boolToBytes(uint _offst, bool _input, bytes memory _output) internal pure { 31 | uint8 x = _input == false ? 0 : 1; 32 | assembly { 33 | mstore(add(_output, _offst), x) 34 | } 35 | } 36 | 37 | function stringToBytes(uint _offst, bytes memory _input, bytes memory _output) internal pure { 38 | uint256 stack_size = _input.length / 32; 39 | if(_input.length % 32 > 0) stack_size++; 40 | 41 | assembly { 42 | stack_size := add(stack_size,1)//adding because of 32 first bytes memory as the length 43 | for { let index := 0 } lt(index,stack_size){ index := add(index ,1) } { 44 | mstore(add(_output, _offst), mload(add(_input,mul(index,32)))) 45 | _offst := sub(_offst , 32) 46 | } 47 | } 48 | } 49 | 50 | function intToBytes(uint _offst, int _input, bytes memory _output) internal pure { 51 | 52 | assembly { 53 | mstore(add(_output, _offst), _input) 54 | } 55 | } 56 | 57 | function uintToBytes(uint _offst, uint _input, bytes memory _output) internal pure { 58 | 59 | assembly { 60 | mstore(add(_output, _offst), _input) 61 | } 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /tests/SerialitySample.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: Apache-2.0 2 | pragma solidity ^0.8.0; 3 | 4 | import {Seriality} from "../src/Seriality.sol"; 5 | 6 | 7 | contract SerialitySample is Seriality { 8 | 9 | function testSample1() public pure returns(int n1, int8 n2, uint24 n3, string memory n4, string memory n5) { 10 | 11 | bytes memory buffer = new bytes(200); 12 | string memory out4 = new string(32); 13 | string memory out5 = new string(32); 14 | n4 = new string(32); 15 | n5 = new string(32); 16 | int out1 = 34444445; 17 | int8 out2 = 87; 18 | uint24 out3 = 76545; 19 | out4 = "Copy kon lashi"; 20 | out5 = "Bia inja dahan service"; 21 | 22 | // Serializing 23 | uint offset = 200; 24 | 25 | intToBytes(offset, out2, buffer); 26 | offset -= sizeOfInt(8); 27 | 28 | uintToBytes(offset, out3, buffer); 29 | offset -= sizeOfUint(24); 30 | 31 | stringToBytes(offset, bytes(out5), buffer); 32 | offset -= sizeOfString(out5); 33 | 34 | stringToBytes(offset, bytes(out4), buffer); 35 | offset -= sizeOfString(out4); 36 | 37 | intToBytes(offset, out1, buffer); 38 | offset -= sizeOfInt(256); 39 | 40 | // Deserializing 41 | offset = 200; 42 | 43 | n2 = bytesToInt8(offset, buffer); 44 | offset -= sizeOfInt(8); 45 | 46 | n3 = bytesToUint24(offset, buffer); 47 | offset -= sizeOfUint(24); 48 | 49 | bytesToString(offset, buffer, bytes(n5)); 50 | offset -= sizeOfString(out5); 51 | 52 | bytesToString(offset, buffer, bytes(n4)); 53 | offset -= sizeOfString(out4); 54 | 55 | n1 = bytesToInt256(offset, buffer); 56 | 57 | } 58 | 59 | function testSample2() public pure returns(int8 n1, int24 n2, uint32 n3, int128 n4, address n5, address n6) { 60 | 61 | bytes memory buffer = new bytes(64); 62 | int8 out1 = -12; 63 | int24 out2 = 838860; 64 | uint32 out3 = 123; 65 | int128 out4 = -44444444444; 66 | address out5 = 0x90c2EA76336410195498E76E28c80922Bb495B53; 67 | address out6 = 0x313b1C7b270492214e011B7D36fde0Eb072A2798; 68 | 69 | // Serializing 70 | uint offset = 64; 71 | 72 | intToBytes(offset, out1, buffer); 73 | offset -= sizeOfInt(8); 74 | 75 | intToBytes(offset, out2, buffer); 76 | offset -= sizeOfUint(24); 77 | 78 | uintToBytes(offset, out3, buffer); 79 | offset -= sizeOfInt(32); 80 | 81 | intToBytes(offset, out4, buffer); 82 | offset -= sizeOfUint(128); 83 | 84 | addressToBytes(offset, out5, buffer); 85 | offset -= sizeOfAddress(); 86 | 87 | addressToBytes(offset, out6, buffer); 88 | 89 | // Deserializing 90 | offset = 64; 91 | 92 | n1 = bytesToInt8(offset, buffer); 93 | offset -= sizeOfInt(8); 94 | 95 | n2 = bytesToInt24(offset, buffer); 96 | offset -= sizeOfUint(24); 97 | 98 | n3 = bytesToUint8(offset, buffer); 99 | offset -= sizeOfInt(32); 100 | 101 | n4 = bytesToInt128(offset, buffer); 102 | offset -= sizeOfUint(128); 103 | 104 | n5 = bytesToAddress(offset, buffer); 105 | offset -= sizeOfAddress(); 106 | 107 | n6 = bytesToAddress(offset, buffer); 108 | 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /tests/StringsReturn.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: Apache-2.0 2 | pragma solidity ^0.8.0; 3 | 4 | import "../src/Seriality.sol"; 5 | 6 | contract StringsReturn is Seriality { 7 | 8 | function stringCaller() public pure returns( string memory out1, 9 | string memory out2, 10 | string memory out3, 11 | string memory out4, 12 | string memory out5) 13 | { 14 | 15 | bytes memory buffer = new bytes(320); 16 | uint offset = stringCallee(buffer); 17 | 18 | //deserializing 19 | out1 = new string (getStringSize(offset, buffer)); 20 | bytesToString(offset, buffer, bytes(out1)); 21 | offset -= sizeOfString(out1); 22 | 23 | out2 = new string (getStringSize(offset, buffer)); 24 | bytesToString(offset, buffer, bytes(out2)); 25 | offset -= sizeOfString(out2); 26 | 27 | out3 = new string (getStringSize(offset, buffer)); 28 | bytesToString(offset, buffer, bytes(out3)); 29 | offset -= sizeOfString(out3); 30 | 31 | out4 = new string (getStringSize(offset, buffer)); 32 | bytesToString(offset, buffer, bytes(out4)); 33 | offset -= sizeOfString(out4); 34 | 35 | out5 = new string (getStringSize(offset, buffer)); 36 | bytesToString(offset, buffer, bytes(out5)); 37 | 38 | } 39 | 40 | function stringCallee(bytes memory buffer) public pure returns (uint buffer_size) { 41 | 42 | string memory out1 = new string(32); 43 | string memory out2 = new string(32); 44 | string memory out3 = new string(32); 45 | string memory out4 = new string(32); 46 | string memory out5 = new string(32); 47 | 48 | out1 = "Come on baby lets dance!"; 49 | out2 = "May I buy you a drink?"; 50 | out3 = "I am an itinerant programmer"; 51 | out4 = "Inam javab lashi!"; 52 | out5 = "Bia inja dahan service"; 53 | 54 | // Serializing 55 | buffer_size = sizeOfString(out5) + 56 | sizeOfString(out4) + 57 | sizeOfString(out3) + 58 | sizeOfString(out2) + 59 | sizeOfString(out1); 60 | 61 | uint offset = buffer_size; 62 | 63 | stringToBytes(offset, bytes(out1), buffer); 64 | offset -= sizeOfString(out1); 65 | 66 | stringToBytes(offset, bytes(out2), buffer); 67 | offset -= sizeOfString(out2); 68 | 69 | stringToBytes(offset, bytes(out3), buffer); 70 | offset -= sizeOfString(out3); 71 | 72 | stringToBytes(offset, bytes(out4), buffer); 73 | offset -= sizeOfString(out4); 74 | 75 | stringToBytes(offset, bytes(out5), buffer); 76 | 77 | return buffer_size; 78 | 79 | } 80 | 81 | 82 | } 83 | --------------------------------------------------------------------------------