⚠️ ⚠️ ⚠️ This package is outdated and will not be updated! Use olifanton/interop
instead.
PHP port of tonweb-boc
JS library
composer require olifanton/boc
Install olifanton/boc
package via Composer and include autoload script:
<?php declare(strict_types=1);
require __DIR__ . "/vendor/autoload.php";
use Olifanton\Boc\BitString;
use Olifanton\Boc\Cell;
// Now you can use BoC classes
BitString
is a class that allows you to manipulate binary data. BitString
is at the heart of the PHP representation of TVM Cells. BitString
is memory optimized for storing binary data.
Internally, BitString uses implementation of Uint8Array
provided by ajf/typed-arrays
package and is used as the base type for transferring binary data between parts of the Olifanton libraries.
The BitString instance is created with a strictly fixed length. write%
(writeBit, writeUint, ...) methods move the internal cursor. If you try to write a value that exceeds the length of the free bits, BitStringException
exception will be thrown.
/**
* @param int $length
*/
public function __construct(int $length)
Parameters:
$length
— length of Uint8Array. Default value for TVM Cell: 1023 (Documentation)
Returns unused bits length of BitString.
Returns used bits length of BitString.
Returns used bytes length of BitString.
/**
* @param int $n Position
*/
public function get(int $n): bool
Returns a bit value at $n
position.
/**
* @param int $n Position
*/
public function on(int $n): void
Sets a bit value to 1 at position $n
.
/**
* @param int $n Position
*/
public function off(int $n): void
Sets a bit value to 0 at position $n
.
/**
* @param int $n Position
*/
public function toggle(int $n): void
Toggle (inverse) bit value at position $n
.
Returns Generator of used bits.
Example:
<?php declare(strict_types=1);
use Olifanton\Boc\BitString;
$bs = new BitString(4);
$bs->writeBit(1);
$bs->writeBit(0);
$bs->writeBit(1);
$bs->writeBit(1);
foreach ($bs->iterate() as $b) {
echo (int)$b;
}
// Prints "1011"
/**
* @param int|bool $b
*/
public function writeBit(int | bool $b): void
Writes bit and increase BitString internal cursor.
/**
* @param array<int | bool> $ba Array of bits
*/
public function writeBitArray(array $ba): void
Writes array of bits.
Example:
<?php declare(strict_types=1);
use Olifanton\Boc\BitString;
$bs = new BitString(4);
$bs->writeBitArray([1, false, 0, true]);
foreach ($bs->iterate() as $b) {
echo (int)$b;
}
// Prints "1001"
/**
* @param int|\Brick\Math\BigInteger $number Unsigned integer
* @param int $bitLength Integer size (8, 16, 32, ...)
*/
public function writeUint(int | BigInteger $number, int $bitLength): void
Writes $bitLength-bit unsigned integer.
/**
* @param int|\Brick\Math\BigInteger $number Signed integer
* @param int $bitLength Integer size (8, 16, 32, ...)
*/
public function writeInt(int | BigInteger $number, int $bitLength): void
Writes $bitLength-bit signed integer.
Alias of writeUint()
method with predefined $bitLength parameter value.
/**
* @param \ajf\TypedArrays\Uint8Array $ui8 Byte array
*/
public function writeBytes(Uint8Array $ui8): void
Write array of unsigned 8-bit integers.
/**
* @param string $value
*/
public function writeString(string $value): void
Writes UTF-8 string.
/**
* @param int|\Brick\Math\BigInteger $amount
*/
public function writeCoins(int | BigInteger $amount): void;
Writes coins in nanotoncoins. 1 TON === 1000000000 (10^9) nanotoncoins.
/**
* @param \Olifanton\Utils\Address|null $address TON Address
*/
public function writeAddress(?Address $address): void
Writes TON address. See Address implementation in olifanton/utils package.
/**
* @param \Olifanton\Boc\BitString $anotherBitString BitString instance
*/
public function writeBitString(BitString $anotherBitString): void
Writes another BitString to this BitString.
Clones this BitString and returns new BitString instance.
Returns hex string representation of BitString.
Returns immutable copy of internal Uint8Array.
Returns size of BitString in bits.
Cell
is a class that implements the concept of TVM Cells in PHP. To create new and process received messages from the blockchain, you will work with instances of the Cell class.
Without parameters.
/**
* @param string|Uint8Array $serializedBoc Serialized BoC
* @return Cell[]
*/
public static function fromBoc(string|Uint8Array $serializedBoc): array
Creates array of Cell's from byte array or hex string.
/**
* @param string|Uint8Array $serializedBoc Serialized BoC
* @param bool $isBase64 Base64-serialized flag, default false
*/
public static function oneFromBoc(string|Uint8Array $serializedBoc, bool $isBase64 = false): Cell
Fetch one root Cell from byte array or hex string.
/**
* @param Cell $anotherCell Another cell
* @return Cell This Cell
*/
public function writeCell(Cell $anotherCell): self
Writes another Cell to this cell and returns this cell. Mutable method.
Returns max depth of child cells.
Returns internal BitString instance for writing and reading.
Returns Array-like object of children cells.
Returns SHA-256 hash of this Cell.
Recursively prints cell's content like Fift.
/**
* @param bool $has_idx Default _true_
* @param bool $hash_crc32 Default _true_
* @param bool $has_cache_bits Default _false_
* @param int $flags Default _0_
*/
public function toBoc(bool $has_idx = true,
bool $hash_crc32 = true,
bool $has_cache_bits = false,
int $flags = 0): Uint8Array
Creates BoC Byte array.
Slice
is the type of cell slices. A cell can be transformed into a slice, and then the data bits and references to other cells from the cell can be obtained by loading them from the slice.
load%
(loadBit, loadUint, ...) methods move the Slice internal cursor. If you try to read a value that exceeds the length of the free bits, SliceException
exception will be thrown.
/**
* @param \ajf\TypedArrays\Uint8Array $array
* @param int $length
* @param \Olifanton\Boc\Slice[] $refs
*/
public function __construct(Uint8Array $array, int $length, array $refs)
Parameters:
$array
— Uint8Array from BitString representation of Cell$length
— BitString length$refs
— Children Cells slices
Returns the unread bits according to the internal cursor.
/**
* @param int $n
*/
public function get(int $n): bool
Returns a bit value at position $n
.
Reads a bit and moves the cursor.
/**
* @param int $bitLength
*/
public function loadBits(int $bitLength): Uint8Array
Reads bit array.
/**
* @param int $bitLength
*/
public function loadUint(int $bitLength): BigInteger
Reads unsigned integer.
/**
* @param int $bitLength
*/
public function loadInt(int $bitLength): BigInteger
Reads signed integer.
/**
* @param int $bitLength
*/
public function loadVarUint(int $bitLength): BigInteger
Reads TON amount in nanotoncoins.
Reads Address.
Reads Slice of children Cell.
composer run test
MIT