Skip to content

Commit 16a528a

Browse files
committed
add to type casting docs
1 parent 463b415 commit 16a528a

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

website/docs/language/types.md

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,16 @@ require(hello + "world" == "Hello " + world);
153153
```
154154

155155
## Type Casting
156-
Type casting can be done both explicitly and implicitly as illustrated below. `pubkey`, `sig` and `datasig` can be implicitly cast to `bytes`, meaning they can be used anywhere where you would normally use a `bytes` type. Explicit type casting can be done with a broader range of types, but is still limited. The syntax of this explicit type casting is illustrated below. Note that you can also cast to bounded `bytes` types.
156+
Type casting can be done both explicitly and implicitly depending on the type. `pubkey`, `sig` and `datasig` can be implicitly cast to `bytes`, meaning they can be used anywhere where you would normally use a `bytes` type. Explicit type casting can be done with a broader range of types, but is still limited. The syntax of this explicit type casting is illustrated below:
157+
158+
#### Example
159+
```solidity
160+
pubkey pk = pubkey(0x0000);
161+
bytes editedPk = bytes(pk) + 0x1234;
162+
bytes4 zeroBytes = bytes4(0); // 0x00000000
163+
```
164+
165+
### Casting Table
157166

158167
See the following table for information on which types can be cast to other which other types.
159168

@@ -167,44 +176,46 @@ See the following table for information on which types can be cast to other whic
167176
| sig | bytes | bytes |
168177
| datasig | bytes | bytes |
169178

170-
#### Example
171-
```solidity
172-
pubkey pk = pubkey(0x0000);
173-
bytes editedPk = bytes(pk) + 0x1234;
174-
bytes4 integer = bytes4(25);
175-
```
176-
177179
### Int to Byte Casting
178180

179-
When casting integer types to bytes of a certain size, the integer value is padded with zeros, e.g. `bytes4(0) == 0x00000000`. It is also possible to pad with a variable number of zeros by passing in a `size` parameter, which indicates the size of the output, e.g. `bytes(0, 4 - 2) == 0x0000`. The size casting can be a very important feature when keeping local state in an nftCommitment or in the simulated state.
181+
When casting integer types to bytes of a certain size, the integer value is padded with zeros, e.g. `bytes4(0) == 0x00000000`. It is also possible to pad with a variable number of zeros by passing in a `size` parameter, which indicates the size of the output, e.g. `bytes(0, 4 - 2) == 0x0000`.
180182

181183
:::tip
182184
Using `bytes20 placeholderPkh= bytes20(0)` will generate a 20 byte zero-array programmatically, whereas
183185
`bytes20 placeholderPkh= 0x0000000000000000000000000000000000000000` will actually take 20 bytes of space in your contract.
184186
:::
185187

186-
:::note
188+
Casting an integer to a fixed-size byte-length can be a very important when storing local state in an nftCommitment. Developers need to consider what the preferable fixed-size length is for each indivual case depending on the interger range. Below we add a table with information on the maximum integer size for satoshis and fungible CashTokens:
189+
190+
| Integer Type | Maximum amount | Max Byte Size |
191+
| -------------- | ---------------------- | ---------------------------------- |
192+
| Satoshis | 2.1 quadrillion | 6 bytes (5 bytes for 1 quadrillion)|
193+
| CashTokens | 9223 quadrillion | 8 bytes for max supply token |
194+
195+
:::info
187196
VM numbers follow Script Number format (A.K.A. CSCriptNum), to convert VM number to bytes or the reverse, it's recommended to use helper functions for these conversions from libraries like Libauth.
188197
:::
189198

190-
### Semantic Byte Casting
199+
### Bytes to BytesX Casting
191200

192-
When casting unbounded `bytes` types to bounded `bytes` types (such as `bytes20` or `bytes32`), this is a purely semantic cast. The bytes are not padded with zeros, and no checks are performed to ensure the cast bytes are of the correct length. This can be helpful in certain cases, such as `LockingBytecode`, which expects a specific length input.
201+
If you do need to pad bytes to a specific length, you can convert the bytes to `int` first, and then cast to the bounded `bytes` type. This will pad the bytes with zeros to the specified length, like specified in the *Int to Byte Casting* section above.
193202

194203
#### Example
195204
```solidity
196-
bytes pkh = nftCommitment.split(20)[0]; // (type = bytes, content = 20 bytes)
197-
bytes20 bytes20Pkh = bytes20(pkh); // (type = bytes20, content = 20 bytes)
198-
bytes25 lockingBytecode = new LockingBytecodeP2PKH(bytes20Pkh);
205+
bytes data = nftCommitment.split(10)[0]; // (type = bytes, content = 10 bytes)
206+
bytes20 paddedData = bytes20(int(data)); // (type = bytes20, content = 20 bytes)
207+
require(storedContractState == paddedData);
199208
```
200209

201-
If you do need to pad bytes to a specific length, you can convert the bytes to `int` first, and then cast to the bounded `bytes` type. This will pad the bytes with zeros to the specified length, like specified in the *Int to Byte Casting* section above.
210+
### Semantic Byte Casting
211+
212+
When casting unbounded `bytes` types to bounded `bytes` types (such as `bytes20` or `bytes32`), this is a purely semantic cast. The bytes are not padded with zeros, and no checks are performed to ensure the cast bytes are of the correct length. This can be helpful in certain cases, such as `LockingBytecode`, which expects a specific length input.
202213

203214
#### Example
204215
```solidity
205-
bytes data = nftCommitment.split(10)[0]; // (type = bytes, content = 10 bytes)
206-
bytes20 paddedData = bytes20(int(data)); // (type = bytes20, content = 20 bytes)
207-
require(storedContractState == paddedData);
216+
bytes pkh = nftCommitment.split(20)[0]; // (type = bytes, content = 20 bytes)
217+
bytes20 bytes20Pkh = bytes20(pkh); // (type = bytes20, content = 20 bytes)
218+
bytes25 lockingBytecode = new LockingBytecodeP2PKH(bytes20Pkh);
208219
```
209220

210221
## Operators

0 commit comments

Comments
 (0)