3
3
namespace GrapheneNodeClient \Tools \ChainOperations ;
4
4
5
5
use GrapheneNodeClient \Connectors \ConnectorInterface ;
6
+ use StephenHill \Base58 ;
7
+ use StephenHill \GMPService ;
6
8
use t3ran13 \ByteBuffer \ByteBuffer ;
7
9
8
10
class OperationSerializer
9
11
{
12
+ const TYPE_CHAIN_PROPERTIES = 'chain_properties ' ;
10
13
const TYPE_SET_EXTENSIONS = 'set_extensions ' ;
11
14
const TYPE_SET_BENEFICIARIES = 'set_beneficiaries ' ;
12
15
const TYPE_BENEFICIARY = 'set_beneficiary ' ;
13
16
const TYPE_SET_STRING = 'set_string ' ;
17
+ const TYPE_PUBLIC_KEY = 'public_key ' ;
14
18
const TYPE_STRING = 'string ' ;
15
19
const TYPE_INT16 = 'int16 ' ;
20
+ const TYPE_INT32 = 'int32 ' ;
16
21
const TYPE_ASSET = 'asset ' ;
17
22
const TYPE_BOOL = 'bool ' ;
18
23
const TYPE_INT8 = 'int8 ' ;
@@ -71,7 +76,7 @@ public static function serializeOperation($chainName, $operationName, $data, $by
71
76
$ byteBuffer ->writeInt8 ($ opId );
72
77
73
78
foreach (self ::getOpFieldsTypes ($ chainName , $ operationName ) as $ field => $ type ) {
74
- self ::serializeType ($ type , $ data [$ field ], $ byteBuffer );
79
+ self ::serializeType ($ type , $ data [$ field ], $ byteBuffer, $ chainName );
75
80
}
76
81
77
82
return $ byteBuffer ;
@@ -88,17 +93,26 @@ public static function serializeOperation($chainName, $operationName, $data, $by
88
93
public static function getOpFieldsTypes ($ chainName , $ operationName )
89
94
{
90
95
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 }' " );
101
114
}
115
+
102
116
self ::$ opFieldsMap [$ chainName ] = $ op ;
103
117
}
104
118
@@ -114,11 +128,12 @@ public static function getOpFieldsTypes($chainName, $operationName)
114
128
* @param string $type
115
129
* @param mixed $value
116
130
* @param ByteBuffer $byteBuffer
131
+ * @param string $chainName
117
132
*
118
133
* @return mixed
119
134
* @throws \Exception
120
135
*/
121
- public static function serializeType ($ type , $ value , $ byteBuffer )
136
+ public static function serializeType ($ type , $ value , $ byteBuffer, $ chainName )
122
137
{
123
138
if ($ type === self ::TYPE_STRING ) {
124
139
//Writes a UTF8 encoded string prefixed 32bit base 128 variable-length integer.
@@ -149,6 +164,8 @@ public static function serializeType($type, $value, $byteBuffer)
149
164
}
150
165
} elseif ($ type === self ::TYPE_INT16 ) {
151
166
$ byteBuffer ->writeInt16LE ($ value );
167
+ } elseif ($ type === self ::TYPE_INT32 ) {
168
+ $ byteBuffer ->writeInt32LE ($ value );
152
169
} elseif ($ type === self ::TYPE_ASSET ) {
153
170
list ($ amount , $ symbol ) = explode (' ' , $ value );
154
171
@@ -186,6 +203,17 @@ public static function serializeType($type, $value, $byteBuffer)
186
203
$ byteBuffer ->writeInt8 ($ value );
187
204
} elseif ($ type === self ::TYPE_BOOL ) {
188
205
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
+ }
189
217
}
190
218
191
219
return $ byteBuffer ;
0 commit comments