Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
romanzaycev committed Jul 5, 2022
0 parents commit 3355fa9
Show file tree
Hide file tree
Showing 25 changed files with 1,373 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/phpunit.xml export-ignore
/tests export-ignore
/build export-ignore
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.idea/
/vendor/
/build/
composer.lock
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Changelog

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Roman Zaycev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Olifanton PHP utils library
---

PHP port of [`tonweb-utils`](https://github.com/toncenter/tonweb/tree/master/src/utils) JS library

`⚠️ This project is under active development!`

## Install

```bash
composer require olifanton/utils
```

## Tests

```bash
composer run test
```

# License

MIT
51 changes: 51 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "olifanton/utils",
"version": "0.1.0",
"description": "Olifanton utils library",
"type": "library",
"license": "MIT",
"homepage": "https://github.com/olifanton/utils",
"keywords": [
"ton",
"blockchain",
"the open network",
"address",
"coins",
"olifanton"
],
"autoload": {
"psr-4": {
"Olifanton\\Utils\\": "src/Olifanton/Utils/"
}
},
"autoload-dev": {
"psr-4": {
"Olifanton\\Utils\\Tests\\": "tests/Olifanton/Utils/Tests/"
}
},
"authors": [
{
"name": "Roman Zaycev",
"email": "box@romanzaycev.ru",
"role": "Developer"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=8.1",
"ext-mbstring": "*",
"brick/math": "dev-master",
"ajf/typed-arrays": "dev-master"
},
"suggest": {
"ext-bcmath": "*",
"ext-openssl": "*",
"ext-sodium": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
},
"scripts": {
"test": "XDEBUG_MODE=coverage phpunit"
}
}
35 changes: 35 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
bootstrap="tests/bootstrap.php"
cacheResultFile="build/test-results"
executionOrder="depends,defects"
forceCoversAnnotation="false"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
convertDeprecationsToExceptions="true"
failOnRisky="true"
failOnWarning="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>

<coverage cacheDirectory="build"
processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
<report>
<clover outputFile="build/clover.xml" />
<html outputDirectory="build/html-coverage"
lowUpperBound="50"
highLowerBound="90"/>
<text outputFile="build/coverage.txt"
showUncoveredFiles="false"
showOnlySummary="true"/>
</report>
</coverage>
</phpunit>
223 changes: 223 additions & 0 deletions src/Olifanton/Utils/Address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
<?php declare(strict_types=1);

namespace Olifanton\Utils;

use ajf\TypedArrays\Uint8Array;
use InvalidArgumentException;
use JetBrains\PhpStorm\ArrayShape;

class Address implements \Stringable
{
private const BOUNCEABLE_TAG = 0x11;
private const NON_BOUNCEABLE_TAG = 0x51;
private const TEST_FLAG = 0x80;

private int $wc;

private Uint8Array $hashPart;

private bool $isTestOnly;

private bool $isUserFriendly;

private bool $isBounceable;

private bool $isUrlSafe;

public function __construct(string | Address $anyForm)
{
if ($anyForm instanceof Address) {
$this->wc = $anyForm->wc;
$this->hashPart = $anyForm->hashPart;
$this->isTestOnly = $anyForm->isTestOnly;
$this->isUserFriendly = $anyForm->isUserFriendly;
$this->isBounceable = $anyForm->isBounceable;
$this->isUrlSafe = $anyForm->isUrlSafe;
return;
}

if (strpos($anyForm, "-") > 0 || strpos($anyForm, "_") > 0) {
$this->isUrlSafe = true;
$anyForm = str_replace(["-", "_"], ["+", '/'], $anyForm);
} else {
$this->isUrlSafe = false;
}

if (str_contains($anyForm, ":")) {
$chunks = explode(":", $anyForm);

if (count($chunks) !== 2) {
throw new InvalidArgumentException("Invalid address: " . $anyForm);
}

$wc = (int)$chunks[0];

if ($wc !== 0 && $wc !== -1) {
throw new InvalidArgumentException('Invalid address wc: ' . $anyForm);
}

$hex = $chunks[1];

if (strlen($hex) !== 64) {
throw new InvalidArgumentException("Invalid address hex: " . $anyForm);
}

$this->isUserFriendly = false;
$this->wc = $wc;
$this->hashPart = Bytes::hexStringToBytes($hex);
$this->isTestOnly = false;
$this->isBounceable = false;
} else {
$parseResult = self::parseFriendlyAddress($anyForm);

$this->isUserFriendly = true;
$this->wc = $parseResult['workchain'];
$this->hashPart = $parseResult['hashPart'];
$this->isTestOnly = $parseResult['isTestOnly'];
$this->isBounceable = $parseResult['isBounceable'];
}
}

public function toString(?bool $isUserFriendly = null,
?bool $isUrlSafe = null,
?bool $isBounceable = null,
?bool $isTestOnly = null): string
{
$isUserFriendly = ($isUserFriendly === null) ? $this->isUserFriendly : $isUserFriendly;
$isUrlSafe = ($isUrlSafe === null) ? $this->isUrlSafe : $isUrlSafe;
$isBounceable = ($isBounceable === null) ? $this->isBounceable : $isBounceable;
$isTestOnly = ($isTestOnly === null) ? $this->isTestOnly : $isTestOnly;

if (!$isUserFriendly) {
return $this->wc . ":" . Bytes::bytesToHexString($this->hashPart);
}

$tag = $isBounceable ? self::BOUNCEABLE_TAG : self::NON_BOUNCEABLE_TAG;

if ($isTestOnly) {
$tag |= self::TEST_FLAG;
}

$addr = new Uint8Array(34);
$addr[0] = $tag;
$addr[1] = $this->wc;
$addr->set($this->hashPart, 2);

$addressWithChecksum = new Uint8Array(36);
$addressWithChecksum->set($addr);
$addressWithChecksum->set(Checksum::crc16($addr), 34);
$addressBase64 = base64_encode(Bytes::arrayToBytes($addressWithChecksum));

if ($isUrlSafe) {
$addressBase64 = str_replace(['+', '/'], ["-", '_'], $addressBase64);
}

return $addressBase64;
}

public function getWorkchain(): int
{
return $this->wc;
}

public function getHashPart(): Uint8Array
{
return Bytes::arraySlice($this->hashPart, 0, 32);
}

public function isTestOnly(): bool
{
return $this->isTestOnly;
}

public function isUserFriendly(): bool
{
return $this->isUserFriendly;
}

public function isBounceable(): bool
{
return $this->isBounceable;
}

public function isUrlSafe(): bool
{
return $this->isUrlSafe;
}

public function __toString(): string
{
return $this->toString();
}

public static function isValid(string | Address $address): bool
{
try {
new Address($address);

return true;
} catch (\Throwable $e) {
return false;
}
}

#[ArrayShape([
'isTestOnly' => "bool",
'isBounceable' => "bool",
'workchain' => "int",
'hashPart' => "mixed",
])]
private static function parseFriendlyAddress(string $addressString): array
{
if (strlen($addressString) !== 48) {
throw new InvalidArgumentException("User-friendly address should contain strictly 48 characters");
}

$data = Bytes::stringToBytes(base64_decode($addressString));

if ($data->length !== 36) {
throw new InvalidArgumentException("Unknown address type: byte length is not equal to 36");
}

$addr = Bytes::arraySlice($data, 0, 34);
$crc = Bytes::arraySlice($data, 34, 36);
$checkCrc = Checksum::crc16($addr);

if (!Bytes::compareBytes($crc, $checkCrc)) {
throw new InvalidArgumentException("Address CRC16-checksum error");
}

$tag = $addr[0];
$isTestOnly = false;

if ($tag & self::TEST_FLAG) {
$isTestOnly = true;
$tag ^= self::TEST_FLAG;
}

if (($tag !== self::BOUNCEABLE_TAG) && ($tag !== self::NON_BOUNCEABLE_TAG)) {
throw new InvalidArgumentException("Unknown address tag");
}

$isBounceable = $tag === self::BOUNCEABLE_TAG;

if ($addr[1] === 0xff) {
$workchain = -1;
} else {
$workchain = $addr[1];
}

if ($workchain !== 0 && $workchain !== -1) {
throw new InvalidArgumentException("Invalid address workchain: " . $workchain);
}

$hashPart = Bytes::arraySlice($addr, 2, 34);

return [
'isTestOnly' => $isTestOnly,
'isBounceable' => $isBounceable,
'workchain' => $workchain,
'hashPart' => $hashPart,
];
}
}
Loading

0 comments on commit 3355fa9

Please sign in to comment.