File Coverage

blib/lib/Steemit/OperationSerializer.pm
Criterion Covered Total %
statement 51 52 98.0
branch 5 8 62.5
condition 1 2 50.0
subroutine 9 9 100.0
pod 0 5 0.0
total 66 76 86.8


line stmt bran cond sub pod time code
1             package Steemit::OperationSerializer;
2 2     2   1587 use Modern::Perl;
  2         7437  
  2         12  
3 2     2   212 use Carp;
  2         4  
  2         79  
4 2     2   17 use Data::Dumper;
  2         3  
  2         1231  
5              
6             sub new {
7 2     2 0 320 my( $class, %params ) = @_;
8 2         4 my $self = {};
9 2         6 return bless $self, $class;
10              
11             }
12              
13              
14             sub serialize_operation {
15 4     4 0 305 my( $self, $operation_name, $operation_parameters ) = @_;
16             ##operation id
17              
18 4 50       26 if( my $serializer_method = $self->can("serialize_$operation_name") ){
19 4         9 return $serializer_method->($self,$operation_name,$operation_parameters);
20             }else{
21 0         0 die "operation $operation_name currently not support for serialisation";
22             }
23              
24             }
25              
26             #let vote = new Serializer(
27             # "vote", {
28             # voter: string,
29             # author: string,
30             # permlink: string,
31             # weight: int16
32             #}
33             #);
34              
35             sub serialize_vote {
36 2     2 0 4 my( $self, $operation_name, $operation_parameters ) = @_;
37              
38 2         3 my $serialized_operation = '';
39 2         4 my $operation_id = $self->_index_of_operation($operation_name);
40 2         7 $serialized_operation .= pack "C", $operation_id;
41              
42 2         5 $serialized_operation .= pack "C", length $operation_parameters->{voter};
43 2         6 $serialized_operation .= pack "A*", $operation_parameters->{voter};
44              
45 2         5 $serialized_operation .= pack "C", length $operation_parameters->{author};
46 2         4 $serialized_operation .= pack "A*", $operation_parameters->{author};
47              
48 2         4 $serialized_operation .= pack "C", length $operation_parameters->{permlink};
49 2         4 $serialized_operation .= pack "A*", $operation_parameters->{permlink};
50              
51 2         5 $serialized_operation .= pack "s", $operation_parameters->{weight};
52              
53              
54 2         11 return $serialized_operation;
55             }
56              
57              
58             #let comment = new Serializer(
59             # "comment", {
60             # parent_author: string,
61             # parent_permlink: string,
62             # author: string,
63             # permlink: string,
64             # title: string,
65             # body: string,
66             # json_metadata: string
67             #}
68             #);
69              
70             sub serialize_comment {
71 1     1 0 2 my( $self, $operation_name, $operation_parameters ) = @_;
72              
73 1         2 my $serialized_operation = '';
74 1         2 my $operation_id = $self->_index_of_operation($operation_name);
75 1         3 $serialized_operation .= pack "C", $operation_id;
76              
77 1         2 for my $field ( qw(parent_author parent_permlink author permlink title body json_metadata) ){
78 7 50       13 confess "$field missing in parameters".Dumper($operation_parameters) unless defined $operation_parameters->{$field};
79 7         11 $serialized_operation .= pack "C", length $operation_parameters->{$field};
80 7         12 $serialized_operation .= pack "A*", $operation_parameters->{$field};
81             }
82              
83 1         6 return $serialized_operation;
84             }
85              
86              
87             #let delete_comment = new Serializer(
88             # "delete_comment", {
89             # author: string,
90             # permlink: string
91             #}
92             #);
93              
94             sub serialize_delete_comment {
95 1     1 0 2 my( $self, $operation_name, $operation_parameters ) = @_;
96              
97 1         1 my $serialized_operation = '';
98 1         2 my $operation_id = $self->_index_of_operation($operation_name);
99 1         3 $serialized_operation .= pack "C", $operation_id;
100              
101 1         2 for my $field ( qw(author permlink) ){
102 2 50       6 $serialized_operation .= pack "C", length $operation_parameters->{$field} or confess "$field missing in parameters".Dumper($operation_parameters);;
103 2         5 $serialized_operation .= pack "A*", $operation_parameters->{$field};
104             }
105              
106 1         5 return $serialized_operation;
107             }
108              
109              
110              
111             sub _index_of_operation {
112 4     4   7 my ( $self, $operation ) = @_;
113              
114             #https://github.com/steemit/steem-js/blob/master/src/auth/serializer/src/operations.js#L767
115 4         21 my @operations = qw(
116             vote
117             comment
118             transfer
119             transfer_to_vesting
120             withdraw_vesting
121             limit_order_create
122             limit_order_cancel
123             feed_publish
124             convert
125             account_create
126             account_update
127             witness_update
128             account_witness_vote
129             account_witness_proxy
130             pow
131             custom
132             report_over_production
133             delete_comment
134             custom_json
135             comment_options
136             set_withdraw_vesting_route
137             limit_order_create2
138             challenge_authority
139             prove_authority
140             request_account_recovery
141             recover_account
142             change_recovery_account
143             escrow_transfer
144             escrow_dispute
145             escrow_release
146             pow2
147             escrow_approve
148             transfer_to_savings
149             transfer_from_savings
150             cancel_transfer_from_savings
151             custom_binary
152             decline_voting_rights
153             reset_account
154             set_reset_account
155             claim_reward_balance
156             delegate_vesting_shares
157             account_create_with_delegation
158             fill_convert_request
159             author_reward
160             curation_reward
161             comment_reward
162             liquidity_reward
163             interest
164             fill_vesting_withdraw
165             fill_order
166             shutdown_witness
167             fill_transfer_from_savings
168             hardfork
169             comment_payout_update
170             return_vesting_delegation
171             comment_benefactor_reward
172             );
173 4 100       13 unless( $self->{_op_index} ){
174 2         4 my $count = 0;
175 2         3 $self->{_op_index} = { map { $_ => $count++ } @operations };
  112         181  
176             }
177 4   50     19 return $self->{_op_index}{$operation} // die "$operation not defined";
178              
179             }
180              
181              
182              
183             1;
184             __END__