Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update bytes string dynamic #281

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 69 additions & 15 deletions src/Contracts/SolidityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function nestedTypes($name)
*/
public function nestedName($name)
{

if (!is_string($name)) {
throw new InvalidArgumentException('nestedName name must string.');
}
Expand Down Expand Up @@ -197,12 +198,27 @@ public function isDynamicType()
* @return string
*/
public function encode($value, $name)
{
{
if ($this->isDynamicArray($name)) {
$length = count($value);
$nestedName = $this->nestedName($name);
$result = [];
$result[] = IntegerFormatter::format($length);
//解决 encode

if ($this->isDynamicType($nestedName)){
$start = 0;
foreach ($value as $k => $val) {
if ($start == 0){
$l = $length * 32;
}else{
$v_1 = Utils::toHex($value[$k-1]);
$l = (floor((mb_strlen($v_1) + 63) / 64)+1) * 32;
}
$start += $l;
$result[] = IntegerFormatter::format($start);
}
}

foreach ($value as $val) {
$result[] = $this->encode($val, $nestedName);
Expand Down Expand Up @@ -232,18 +248,47 @@ public function encode($value, $name)
public function decode($value, $offset, $name)
{
if ($this->isDynamicArray($name)) {
$arrayOffset = (int) Utils::toBn('0x' . mb_substr($value, $offset * 2, 64))->toString();
$length = (int) Utils::toBn('0x' . mb_substr($value, $arrayOffset * 2, 64))->toString();
$arrayOffset = (int) Utils::toBn('0x' . mb_substr($value, $offset * 2, 64))->toString(); //32
$length = (int) Utils::toBn('0x' . mb_substr($value, $arrayOffset * 2, 64))->toString(); //数组的个数
$arrayStart = $arrayOffset + 32;

$nestedName = $this->nestedName($name);
$nestedStaticPartLength = $this->staticPartLength($nestedName);
$roundedNestedStaticPartLength = floor(($nestedStaticPartLength + 31) / 32) * 32;
$result = [];

if($nestedName=='bytes' || $nestedName=='string')
{
$mA = $arrayStart*2;
$mAA = ($mA + (64*1));
for($i=0;$i<$length;$i++)
{
$mAA = ($mA + (64*$i)); //目前的定位

$mB = (int) Utils::toBn('0x' . mb_substr($value, ($mA + (64*$i)), 64))->toString();


$mBB= $mA+($mB*2);
#clear $mBB.PHP_EOL;
$mC = (int) Utils::toBn('0x' . mb_substr($value, $mBB, 64))->toString();
$mCC = (floor($mC/32)+1)*64;
$mD = mb_substr($value, ($mBB+64), $mCC);
#echo $mD.PHP_EOL;
#echo mb_substr($value, $mBB, 64).PHP_EOL;

#echo "mb ={$mB} mBB={$mBB} mc={$mC} mcc={$mCC}".PHP_EOL;
$result[] = $this->decode($value, $mBB , $nestedName);
}

}else
{
$nestedStaticPartLength = $this->staticPartLength($nestedName);
$roundedNestedStaticPartLength = floor(($nestedStaticPartLength + 31) / 32) * 32;
$result = [];

for ($i=0; $i<$length * $roundedNestedStaticPartLength; $i+=$roundedNestedStaticPartLength) {
$result[] = $this->decode($value, $arrayStart + $i, $nestedName);
}
}


for ($i=0; $i<$length * $roundedNestedStaticPartLength; $i+=$roundedNestedStaticPartLength) {
$result[] = $this->decode($value, $arrayStart + $i, $nestedName);
}
return $result;
} elseif ($this->isStaticArray($name)) {
$length = $this->staticArrayLength($name);
Expand All @@ -259,15 +304,24 @@ public function decode($value, $offset, $name)
}
return $result;
} elseif ($this->isDynamicType()) {
$dynamicOffset = (int) Utils::toBn('0x' . mb_substr($value, $offset * 2, 64))->toString();
$length = (int) Utils::toBn('0x' . mb_substr($value, $dynamicOffset * 2, 64))->toString();
$roundedLength = floor(($length + 31) / 32);
$param = mb_substr($value, $dynamicOffset * 2, ( 1 + $roundedLength) * 64);

if($name=='bytes' || $name=='string')
{
$mC = (int) Utils::toBn('0x' . mb_substr($value, $offset, 64))->toString();
$mCC = (floor($mC/32)+1)*64;
$param = mb_substr($value, ($offset), ($mCC+64));
}else
{
$dynamicOffset = (int) Utils::toBn('0x' . mb_substr($value, $offset * 2, 64))->toString();
$length = (int) Utils::toBn('0x' . mb_substr($value, $dynamicOffset * 2, 64))->toString();
$roundedLength = floor(($length + 31) / 32);
$param = mb_substr($value, $dynamicOffset * 2, ( 1 + $roundedLength) * 64);
}
return $this->outputFormat($param, $name);
}
$length = $this->staticPartLength($name);
$param = mb_substr($value, $offset * 2, $length * 2);

return $this->outputFormat($param, $name);
}
}
}