-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAbiBase.php
More file actions
140 lines (109 loc) · 4.39 KB
/
AbiBase.php
File metadata and controls
140 lines (109 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
declare(strict_types=1);
namespace ArkEcosystem\Crypto\Utils;
use ArkEcosystem\Crypto\Enums\ContractAbiType;
use kornrunner\Keccak;
abstract class AbiBase
{
protected array $abi;
protected array $functionSelectorMap = [];
protected array $errorSelectorMap = [];
public function __construct(ContractAbiType $type = ContractAbiType::CONSENSUS, ?string $path = null)
{
$abiFilePath = self::contractAbiPath($type, $path);
$decodedAbi = self::loadAbiJson($abiFilePath);
$this->abi = $decodedAbi['abi'];
foreach ($this->abi as $item) {
$signature = $this->getFunctionSignature($item);
$selector = substr($this->keccak256($signature), 2, 8);
if ($item['type'] === 'function') {
$this->functionSelectorMap[$selector] = $item;
} elseif ($item['type'] === 'error') {
$this->errorSelectorMap[$selector] = $item;
}
}
}
public static function methodIdentifiers(
ContractAbiType $type = ContractAbiType::CONSENSUS,
?string $path = null
): array {
$abiFilePath = self::contractAbiPath($type, $path);
$decodedAbi = self::loadAbiJson($abiFilePath);
if (! isset($decodedAbi['methodIdentifiers']) || ! is_array($decodedAbi['methodIdentifiers'])) {
throw new \RuntimeException("ABI JSON does not contain methodIdentifiers: {$abiFilePath}");
}
return $decodedAbi['methodIdentifiers'];
}
protected static function getArrayComponents(string $type): ?array
{
if (preg_match('/^(.*)\[(\d*)\]$/', $type, $matches)) {
$innerType = $matches[1];
$lengthStr = $matches[2];
$length = $lengthStr !== '' ? intval($lengthStr) : null;
return [$length, $innerType];
}
return null;
}
protected static function stripHexPrefix(string $hex): string
{
if (substr($hex, 0, 2) === '0x') {
return substr($hex, 2);
}
return $hex;
}
protected function isValidAddress($address): bool
{
return is_string($address)
&& str_starts_with($address, '0x')
&& strlen($address) === 42
&& ctype_xdigit(substr($address, 2));
}
protected function keccak256(string $input): string
{
return '0x'.Keccak::hash($input, 256);
}
protected function getFunctionSignature(array $abiItem): string
{
$name = $abiItem['name'];
$inputs = $abiItem['inputs'];
$types = array_map(function ($input) {
return $input['type'];
}, $inputs);
return $name.'('.implode(',', $types).')';
}
protected function toFunctionSelector(array $abiItem): string
{
$signature = $this->getFunctionSignature($abiItem);
$hash = $this->keccak256($signature);
$selector = '0x'.substr($hash, 2, 8);
return $selector;
}
protected static function contractAbiPath(ContractAbiType $type, ?string $path = null): string
{
return match ($type) {
ContractAbiType::CONSENSUS => __DIR__.'/Abi/json/Abi.Consensus.json',
ContractAbiType::MULTIPAYMENT => __DIR__.'/Abi/json/Abi.Multipayment.json',
ContractAbiType::USERNAMES => __DIR__.'/Abi/json/Abi.Usernames.json',
ContractAbiType::ERC20BATCH_TRANSFER => __DIR__.'/Abi/json/Abi.ERC20BatchTransfer.json',
ContractAbiType::TOKEN => __DIR__.'/Abi/json/Abi.Token.json',
ContractAbiType::CUSTOM => (function () use ($path): string {
if ($path === null || $path === '') {
throw new \InvalidArgumentException('A non-empty $path must be provided when using ContractAbiType::CUSTOM.');
}
return $path;
})(),
};
}
private static function loadAbiJson(string $path): array
{
$rawJson = file_get_contents($path);
if ($rawJson === false) {
throw new \RuntimeException("Unable to load ABI JSON: {$path}");
}
$decoded = json_decode($rawJson, true);
if (! is_array($decoded) || ! isset($decoded['abi']) || ! is_array($decoded['abi'])) {
throw new \RuntimeException("ABI JSON does not contain a valid abi array: {$path}");
}
return $decoded;
}
}