Skip to content

Commit

Permalink
refactor: use php-crypto for better payload handling (#1004)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbarnsley authored Nov 15, 2024
1 parent 7225f4d commit 265ea4e
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions app/ViewModels/Concerns/Transaction/HasPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace App\ViewModels\Concerns\Transaction;

use ArkEcosystem\Crypto\Utils\AbiDecoder;

trait HasPayload
{
public function hasPayload(): bool
Expand Down Expand Up @@ -46,45 +48,40 @@ public function utf8Payload(): ?string
return $utf8;
}

public function methodHash(): ?string
public function methodHash(?string $payload = null): ?string
{
$payload = $this->rawPayload();
if ($payload === null) {
return null;
$payload = $this->rawPayload();
if ($payload === null) {
return null;
}
}

return substr($payload, 0, 8);
}

public function formattedPayload(): ?string
{
$methodId = $this->methodHash();
$payload = $this->rawPayload();
if ($payload === null) {
return null;
}

$method = (new AbiDecoder())->decodeFunctionData($payload);

$methodId = $this->methodHash($payload);

$functionName = null;
if (app('translator')->has('contracts.'.$methodId)) {
$functionName = trans('contracts.'.$methodId);
} else {
$functionName = $method['functionName'];
}

return trim(view('components.transaction.code-block.formatted-contract', [
'function' => $functionName,
'methodId' => $methodId,
'arguments' => $this->payloadArguments(),
'arguments' => $method['args'],
])->render());
}

private function payloadArguments(): ?array
{
$payload = $this->rawPayload();
if ($payload === null) {
return [];
}

$argumentsPayload = substr($payload, 8);
$separatedArguments = trim(chunk_split($argumentsPayload, 64, ' '));
if (strlen($separatedArguments) === 0) {
return [];
}

return explode(' ', $separatedArguments);
}
}

0 comments on commit 265ea4e

Please sign in to comment.