Skip to content

Commit a0e86d8

Browse files
committed
add new TYPE_CHAIN_PROPERTIES, TYPE_PUBLIC_KEY, TYPE_INT32 handlers
1 parent 1b43b50 commit a0e86d8

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

Tools/ChainOperations/OperationSerializer.php

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@
33
namespace GrapheneNodeClient\Tools\ChainOperations;
44

55
use GrapheneNodeClient\Connectors\ConnectorInterface;
6+
use StephenHill\Base58;
7+
use StephenHill\GMPService;
68
use t3ran13\ByteBuffer\ByteBuffer;
79

810
class OperationSerializer
911
{
12+
const TYPE_CHAIN_PROPERTIES = 'chain_properties';
1013
const TYPE_SET_EXTENSIONS = 'set_extensions';
1114
const TYPE_SET_BENEFICIARIES = 'set_beneficiaries';
1215
const TYPE_BENEFICIARY = 'set_beneficiary';
1316
const TYPE_SET_STRING = 'set_string';
17+
const TYPE_PUBLIC_KEY = 'public_key';
1418
const TYPE_STRING = 'string';
1519
const TYPE_INT16 = 'int16';
20+
const TYPE_INT32 = 'int32';
1621
const TYPE_ASSET = 'asset';
1722
const TYPE_BOOL = 'bool';
1823
const TYPE_INT8 = 'int8';
@@ -71,7 +76,7 @@ public static function serializeOperation($chainName, $operationName, $data, $by
7176
$byteBuffer->writeInt8($opId);
7277

7378
foreach (self::getOpFieldsTypes($chainName, $operationName) as $field => $type) {
74-
self::serializeType($type, $data[$field], $byteBuffer);
79+
self::serializeType($type, $data[$field], $byteBuffer, $chainName);
7580
}
7681

7782
return $byteBuffer;
@@ -88,17 +93,26 @@ public static function serializeOperation($chainName, $operationName, $data, $by
8893
public static function getOpFieldsTypes($chainName, $operationName)
8994
{
9095
if (!isset(self::$opFieldsMap[$chainName])) {
91-
if ($chainName === ConnectorInterface::PLATFORM_GOLOS) {
92-
$op = ChainOperationsGolos::FIELDS_TYPES;
93-
} elseif ($chainName === ConnectorInterface::PLATFORM_STEEMIT) {
94-
$op = ChainOperationsSteem::FIELDS_TYPES;
95-
} elseif ($chainName === ConnectorInterface::PLATFORM_VIZ) {
96-
$op = ChainOperationsViz::FIELDS_TYPES;
97-
} elseif ($chainName === ConnectorInterface::PLATFORM_WHALESHARES) {
98-
$op = ChainOperationsWhaleshares::FIELDS_TYPES;
99-
} else {
100-
throw new \Exception("There is no operations fields for '{$chainName}'");
96+
switch ($chainName) {
97+
case ConnectorInterface::PLATFORM_GOLOS:
98+
$op = ChainOperationsGolos::FIELDS_TYPES;
99+
break;
100+
case ConnectorInterface::PLATFORM_HIVE:
101+
$op = ChainOperationsHive::FIELDS_TYPES;
102+
break;
103+
case ConnectorInterface::PLATFORM_STEEMIT:
104+
$op = ChainOperationsSteem::FIELDS_TYPES;
105+
break;
106+
case ConnectorInterface::PLATFORM_VIZ:
107+
$op = ChainOperationsViz::FIELDS_TYPES;
108+
break;
109+
case ConnectorInterface::PLATFORM_WHALESHARES:
110+
$op = ChainOperationsWhaleshares::FIELDS_TYPES;
111+
break;
112+
default:
113+
throw new \Exception("There is no operations fields for '{$chainName}'");
101114
}
115+
102116
self::$opFieldsMap[$chainName] = $op;
103117
}
104118

@@ -114,11 +128,12 @@ public static function getOpFieldsTypes($chainName, $operationName)
114128
* @param string $type
115129
* @param mixed $value
116130
* @param ByteBuffer $byteBuffer
131+
* @param string $chainName
117132
*
118133
* @return mixed
119134
* @throws \Exception
120135
*/
121-
public static function serializeType($type, $value, $byteBuffer)
136+
public static function serializeType($type, $value, $byteBuffer, $chainName)
122137
{
123138
if ($type === self::TYPE_STRING) {
124139
//Writes a UTF8 encoded string prefixed 32bit base 128 variable-length integer.
@@ -149,6 +164,8 @@ public static function serializeType($type, $value, $byteBuffer)
149164
}
150165
} elseif ($type === self::TYPE_INT16) {
151166
$byteBuffer->writeInt16LE($value);
167+
} elseif ($type === self::TYPE_INT32) {
168+
$byteBuffer->writeInt32LE($value);
152169
} elseif ($type === self::TYPE_ASSET) {
153170
list($amount, $symbol) = explode(' ', $value);
154171

@@ -186,6 +203,17 @@ public static function serializeType($type, $value, $byteBuffer)
186203
$byteBuffer->writeInt8($value);
187204
} elseif ($type === self::TYPE_BOOL) {
188205
self::serializeType(self::TYPE_INT8, $value ? 1 : 0, $byteBuffer);
206+
} elseif ($type === self::TYPE_PUBLIC_KEY) {
207+
$clearPubKey = substr($value, 3);
208+
$base58 = new Base58(null, new GMPService()); //decode base 58 to str
209+
$stringPubKey = substr($base58->decode($clearPubKey), 0, 33);
210+
$byteBuffer->writeVStringLE($stringPubKey);
211+
} elseif ($type === self::TYPE_CHAIN_PROPERTIES) {
212+
if (count($value) > 0) {
213+
foreach (self::getOpFieldsTypes($chainName, self::TYPE_CHAIN_PROPERTIES) as $field => $type) {
214+
self::serializeType($type, $value[$field], $byteBuffer, $chainName);
215+
}
216+
}
189217
}
190218

191219
return $byteBuffer;

0 commit comments

Comments
 (0)