olifanton/interop instead.
PHP port of tonweb-boc JS library
composer require olifanton/bocInstall olifanton/boc package via Composer and include autoload script:
<?php declare(strict_types=1);
require __DIR__ . "/vendor/autoload.php";
use OlifantonBocBitString;
use OlifantonBocCell;
// Now you can use BoC classesOlifantonBocBitStringBitString 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): boolReturns a bit value at $n position.
/**
* @param int $n Position
*/
public function on(int $n): voidSets a bit value to 1 at position $n.
/**
* @param int $n Position
*/
public function off(int $n): voidSets a bit value to 0 at position $n.
/**
* @param int $n Position
*/
public function toggle(int $n): voidToggle (inverse) bit value at position $n.
Returns Generator of used bits.
Example:
<?php declare(strict_types=1);
use OlifantonBocBitString;
$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): voidWrites bit and increase BitString internal cursor.
/**
* @param array<int | bool> $ba Array of bits
*/
public function writeBitArray(array $ba): voidWrites array of bits.
Example:
<?php declare(strict_types=1);
use OlifantonBocBitString;
$bs = new BitString(4);
$bs->writeBitArray([1, false, 0, true]);
foreach ($bs->iterate() as $b) {
echo (int)$b;
}
// Prints "1001"/**
* @param int|BrickMathBigInteger $number Unsigned integer
* @param int $bitLength Integer size (8, 16, 32, ...)
*/
public function writeUint(int | BigInteger $number, int $bitLength): voidWrites $bitLength-bit unsigned integer.
/**
* @param int|BrickMathBigInteger $number Signed integer
* @param int $bitLength Integer size (8, 16, 32, ...)
*/
public function writeInt(int | BigInteger $number, int $bitLength): voidWrites $bitLength-bit signed integer.
Alias of writeUint() method with predefined $bitLength parameter value.
/**
* @param ajfTypedArraysUint8Array $ui8 Byte array
*/
public function writeBytes(Uint8Array $ui8): voidWrite array of unsigned 8-bit integers.
/**
* @param string $value
*/
public function writeString(string $value): voidWrites UTF-8 string.
/**
* @param int|BrickMathBigInteger $amount
*/
public function writeCoins(int | BigInteger $amount): void;Writes coins in nanotoncoins. 1 TON === 1000000000 (10^9) nanotoncoins.
/**
* @param OlifantonUtilsAddress|null $address TON Address
*/
public function writeAddress(?Address $address): voidWrites TON address. See Address implementation in olifanton/utils package.
/**
* @param OlifantonBocBitString $anotherBitString BitString instance
*/
public function writeBitString(BitString $anotherBitString): voidWrites 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.
OlifantonBocCellCell 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): arrayCreates 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): CellFetch one root Cell from byte array or hex string.
/**
* @param Cell $anotherCell Another cell
* @return Cell This Cell
*/
public function writeCell(Cell $anotherCell): selfWrites 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): Uint8ArrayCreates BoC Byte array.
OlifantonBocSliceSlice 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 ajfTypedArraysUint8Array $array
* @param int $length
* @param OlifantonBocSlice[] $refs
*/
public function __construct(Uint8Array $array, int $length, array $refs)Parameters:
$array — Uint8Array from BitString representation of Cell$length — BitString length$refs — Children Cells slicesReturns the unread bits according to the internal cursor.
/**
* @param int $n
*/
public function get(int $n): boolReturns a bit value at position $n.
Reads a bit and moves the cursor.
/**
* @param int $bitLength
*/
public function loadBits(int $bitLength): Uint8ArrayReads bit array.
/**
* @param int $bitLength
*/
public function loadUint(int $bitLength): BigIntegerReads unsigned integer.
/**
* @param int $bitLength
*/
public function loadInt(int $bitLength): BigIntegerReads signed integer.
/**
* @param int $bitLength
*/
public function loadVarUint(int $bitLength): BigIntegerReads TON amount in nanotoncoins.
Reads Address.
Reads Slice of children Cell.
composer run testMIT