Skip to content

Commit

Permalink
Add Abi tests and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
maxvx committed Nov 15, 2020
1 parent 0b523e8 commit 60cadfe
Show file tree
Hide file tree
Showing 29 changed files with 529 additions and 237 deletions.
112 changes: 51 additions & 61 deletions src/Abi.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,8 @@
/**
* Provides message encoding and decoding according to the ABI specification
*/
class Abi
class Abi extends AbstractModule
{
private TonClient $tonClient;

/**
* @param TonClient $tonClient
*/
public function __construct(TonClient $tonClient)
{
$this->tonClient = $tonClient;
}

/**
* Encodes message body according to ABI function call
*
Expand Down Expand Up @@ -63,28 +53,22 @@ public function encodeMessageBody(
}

/**
* Attach signature to message body
* Decodes message body using provided body BOC and ABI
*
* @param AbiParams $abi Contract ABI
* @param string $publicKey Public key. Must be encoded with hex
* @param string $message Unsigned message BOC. Must be encoded with base64
* @param string $signature Signature. Must be encoded with hex
* @return ResultOfAttachSignatureToMessageBody
* @param AbiParams $abi Contract ABI used to decode
* @param string $body Message body BOC encoded in base64
* @param bool $isInternal True if the body belongs to the internal message
* @return DecodedMessageBody
*/
public function attachSignatureToMessageBody(
AbiParams $abi,
string $publicKey,
string $message,
string $signature
): ResultOfAttachSignatureToMessageBody {
return new ResultOfAttachSignatureToMessageBody(
public function decodeMessageBody(AbiParams $abi, string $body, bool $isInternal = false): DecodedMessageBody
{
return new DecodedMessageBody(
$this->tonClient->request(
'abi.attach_signature_to_message_body',
'abi.decode_message_body',
[
'abi' => $abi,
'public_key' => $publicKey,
'message' => $message,
'signature' => $signature,
'abi' => $abi,
'body' => $body,
'is_internal' => $isInternal,
]
)->wait()
);
Expand Down Expand Up @@ -124,6 +108,26 @@ public function encodeMessage(
);
}

/**
* Decodes message body using provided message BOC and ABI
*
* @param AbiParams $abi Contract ABI used to decode
* @param string $message Boc message
* @return DecodedMessageBody
*/
public function decodeMessage(AbiParams $abi, string $message): DecodedMessageBody
{
return new DecodedMessageBody(
$this->tonClient->request(
'abi.decode_message',
[
'abi' => $abi,
'message' => $message,
]
)->wait()
);
}

/**
* Combines hex-encoded signature with base64-encoded unsigned_message. Returns signed message encoded in base64
*
Expand Down Expand Up @@ -153,42 +157,28 @@ public function attachSignature(
}

/**
* Decodes message body using provided message BOC and ABI
*
* @param AbiParams $abi Contract ABI used to decode
* @param string $message Boc message
* @return DecodedMessageBody
*/
public function decodeMessage(AbiParams $abi, string $message): DecodedMessageBody
{
return new DecodedMessageBody(
$this->tonClient->request(
'abi.decode_message',
[
'abi' => $abi,
'message' => $message,
]
)->wait()
);
}

/**
* Decodes message body using provided body BOC and ABI
* Attach signature to message body
*
* @param AbiParams $abi Contract ABI used to decode
* @param string $body Message body BOC encoded in base64
* @param bool $isInternal True if the body belongs to the internal message
* @return DecodedMessageBody
* @param AbiParams $abi Contract ABI
* @param string $publicKey Public key. Must be encoded with hex
* @param string $message Unsigned message BOC. Must be encoded with base64
* @param string $signature Signature. Must be encoded with hex
* @return ResultOfAttachSignatureToMessageBody
*/
public function decodeMessageBody(AbiParams $abi, string $body, bool $isInternal = false): DecodedMessageBody
{
return new DecodedMessageBody(
public function attachSignatureToMessageBody(
AbiParams $abi,
string $publicKey,
string $message,
string $signature
): ResultOfAttachSignatureToMessageBody {
return new ResultOfAttachSignatureToMessageBody(
$this->tonClient->request(
'abi.decode_message_body',
'abi.attach_signature_to_message_body',
[
'abi' => $abi,
'body' => $body,
'is_internal' => $isInternal,
'abi' => $abi,
'public_key' => $publicKey,
'message' => $message,
'signature' => $signature,
]
)->wait()
);
Expand Down
26 changes: 26 additions & 0 deletions src/AbstractModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Extraton\TonClient;

abstract class AbstractModule implements Module
{
protected TonClient $tonClient;

/**
* @param TonClient $tonClient
*/
public function __construct(TonClient $tonClient)
{
$this->tonClient = $tonClient;
}

/**
* @return TonClient
*/
public function getTonClient(): TonClient
{
return $this->tonClient;
}
}
12 changes: 1 addition & 11 deletions src/Boc.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,8 @@
/**
* Boc module
*/
class Boc
class Boc extends AbstractModule
{
private TonClient $tonClient;

/**
* @param TonClient $tonClient
*/
public function __construct(TonClient $tonClient)
{
$this->tonClient = $tonClient;
}

/**
* Parses message boc
*
Expand Down
Loading

0 comments on commit 60cadfe

Please sign in to comment.