Skip to content

Commit

Permalink
support STObject and STArray
Browse files Browse the repository at this point in the history
  • Loading branch information
lessmore92 committed Jan 25, 2021
1 parent 419293c commit 53488ea
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions src/TxSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace Lessmore92\RippleBinaryCodec;

define('ARRAY_END_MARKER', 0xf1);
define('OBJECT_END_MARKER_BYTE', 0xe1);

use BN\BN;
use Exception;
use Lessmore92\Buffer\Buffer;
Expand Down Expand Up @@ -65,6 +68,12 @@ public function SerializeTx($tx, $options)
}
$bytes[] = $value;

if ($item->type->name == 'STObject')
{
$bytes[] = Buffer::int(OBJECT_END_MARKER_BYTE)
->getHex()
;
}
}

return Buffer::hex(join($bytes));
Expand Down Expand Up @@ -92,6 +101,14 @@ public function encodeValue(Field $item)
{
return $this->encodeAccountID($item->value);
}
else if ($item->type->name == 'STArray')
{
return $this->encodeSTArray($item->value);
}
else if ($item->type->name == 'STObject')
{
return $this->encodeSTObject($item->value);
}
else
{
throw new Exception(sprintf('field %s not supported field', $item->name));
Expand All @@ -115,9 +132,9 @@ public function encodeAmount($value)

$intBuf = [
sprintf('%08X', $number->shrn(32)
->toString()),
->toString()),
sprintf('%08X', $number->iand($mask)
->toString()),
->toString()),
];

$amount = Buffer::hex(join($intBuf));
Expand Down Expand Up @@ -149,7 +166,7 @@ public function encodeAccountID($value)
public function encodeVariableLength(int $length)
{
$lenBytes = Buffer::hex('000000')
->getDecimal()
->getDecimal()
;

if ($length <= 192)
Expand All @@ -174,4 +191,22 @@ public function encodeVariableLength(int $length)
}
throw new Exception("Overflow error");
}

public function encodeSTArray($array)
{
$bytes = [];
foreach ($array as $_obj)
{
$bytes = array_merge($bytes, $this->SerializeTx($_obj, [])
->getDecimal());
}

$bytes[] = ARRAY_END_MARKER;
return Buffer::hex(Utils::decimalArrayToHexStr($bytes));
}

public function encodeSTObject($obj)
{
return $this->SerializeTx($obj, []);
}
}

0 comments on commit 53488ea

Please sign in to comment.