-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtransaction.cpp
287 lines (242 loc) · 9.62 KB
/
transaction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <steemit/protocol/transaction.hpp>
#include <steemit/protocol/exceptions.hpp>
#include <fc/io/raw.hpp>
#include <fc/bitutil.hpp>
#include <fc/smart_ref_impl.hpp>
#include <algorithm>
namespace steemit { namespace protocol {
digest_type signed_transaction::merkle_digest()const
{
digest_type::encoder enc;
fc::raw::pack( enc, *this );
return enc.result();
}
digest_type transaction::digest()const
{
digest_type::encoder enc;
fc::raw::pack( enc, *this );
return enc.result();
}
digest_type transaction::sig_digest( const chain_id_type& chain_id )const
{
digest_type::encoder enc;
fc::raw::pack( enc, chain_id );
fc::raw::pack( enc, *this );
return enc.result();
}
void transaction::validate() const
{
FC_ASSERT( operations.size() > 0, "A transaction must have at least one operation", ("trx",*this) );
for( const auto& op : operations )
operation_validate(op);
}
steemit::protocol::transaction_id_type steemit::protocol::transaction::id() const
{
auto h = digest();
transaction_id_type result;
memcpy(result._hash, h._hash, std::min(sizeof(result), sizeof(h)));
return result;
}
const signature_type& steemit::protocol::signed_transaction::sign(const private_key_type& key, const chain_id_type& chain_id)
{
digest_type h = sig_digest( chain_id );
signatures.push_back(key.sign_compact(h));
return signatures.back();
}
signature_type steemit::protocol::signed_transaction::sign(const private_key_type& key, const chain_id_type& chain_id)const
{
digest_type::encoder enc;
fc::raw::pack( enc, chain_id );
fc::raw::pack( enc, *this );
return key.sign_compact(enc.result());
}
void transaction::set_expiration( fc::time_point_sec expiration_time )
{
expiration = expiration_time;
}
void transaction::set_reference_block( const block_id_type& reference_block )
{
ref_block_num = fc::endian_reverse_u32(reference_block._hash[0]);
ref_block_prefix = reference_block._hash[1];
}
void transaction::get_required_authorities( flat_set< account_name_type >& active,
flat_set< account_name_type >& owner,
flat_set< account_name_type >& posting,
vector< authority >& other )const
{
for( const auto& op : operations )
operation_get_required_authorities( op, active, owner, posting, other );
}
void verify_authority( const vector<operation>& ops, const flat_set<public_key_type>& sigs,
const authority_getter& get_active,
const authority_getter& get_owner,
const authority_getter& get_posting,
uint32_t max_recursion_depth,
bool allow_committe,
const flat_set< account_name_type >& active_aprovals,
const flat_set< account_name_type >& owner_approvals,
const flat_set< account_name_type >& posting_approvals
)
{ try {
flat_set< account_name_type > required_active;
flat_set< account_name_type > required_owner;
flat_set< account_name_type > required_posting;
vector< authority > other;
for( const auto& op : ops )
operation_get_required_authorities( op, required_active, required_owner, required_posting, other );
/**
* Transactions with operations required posting authority cannot be combined
* with transactions requiring active or owner authority. This is for ease of
* implementation. Future versions of authority verification may be able to
* check for the merged authority of active and posting.
*/
if( required_posting.size() ) {
FC_ASSERT( required_active.size() == 0 );
FC_ASSERT( required_owner.size() == 0 );
FC_ASSERT( other.size() == 0 );
flat_set< public_key_type > avail;
sign_state s(sigs,get_posting,avail);
s.max_recursion = max_recursion_depth;
for( auto& id : posting_approvals )
s.approved_by.insert( id );
for( auto id : required_posting )
{
STEEMIT_ASSERT( s.check_authority(id) ||
s.check_authority(get_active(id)) ||
s.check_authority(get_owner(id)),
tx_missing_posting_auth, "Missing Posting Authority ${id}",
("id",id)
("posting",get_posting(id))
("active",get_active(id))
("owner",get_owner(id)) );
}
STEEMIT_ASSERT(
!s.remove_unused_signatures(),
tx_irrelevant_sig,
"Unnecessary signature(s) detected"
);
return;
}
flat_set< public_key_type > avail;
sign_state s(sigs,get_active,avail);
s.max_recursion = max_recursion_depth;
for( auto& id : active_aprovals )
s.approved_by.insert( id );
for( auto& id : owner_approvals )
s.approved_by.insert( id );
for( const auto& auth : other )
{
STEEMIT_ASSERT( s.check_authority(auth), tx_missing_other_auth, "Missing Authority", ("auth",auth)("sigs",sigs) );
}
// fetch all of the top level authorities
for( auto id : required_active )
{
STEEMIT_ASSERT( s.check_authority(id) ||
s.check_authority(get_owner(id)),
tx_missing_active_auth, "Missing Active Authority ${id}", ("id",id)("auth",get_active(id))("owner",get_owner(id)) );
}
for( auto id : required_owner )
{
STEEMIT_ASSERT( owner_approvals.find(id) != owner_approvals.end() ||
s.check_authority(get_owner(id)),
tx_missing_owner_auth, "Missing Owner Authority ${id}", ("id",id)("auth",get_owner(id)) );
}
STEEMIT_ASSERT(
!s.remove_unused_signatures(),
tx_irrelevant_sig,
"Unnecessary signature(s) detected"
);
} FC_CAPTURE_AND_RETHROW( (ops)(sigs) ) }
flat_set<public_key_type> signed_transaction::get_signature_keys( const chain_id_type& chain_id )const
{ try {
auto d = sig_digest( chain_id );
flat_set<public_key_type> result;
for( const auto& sig : signatures )
{
STEEMIT_ASSERT(
result.insert( fc::ecc::public_key(sig,d) ).second,
tx_duplicate_sig,
"Duplicate Signature detected" );
}
return result;
} FC_CAPTURE_AND_RETHROW() }
set<public_key_type> signed_transaction::get_required_signatures(
const chain_id_type& chain_id,
const flat_set<public_key_type>& available_keys,
const authority_getter& get_active,
const authority_getter& get_owner,
const authority_getter& get_posting,
uint32_t max_recursion_depth )const
{
flat_set< account_name_type > required_active;
flat_set< account_name_type > required_owner;
flat_set< account_name_type > required_posting;
vector< authority > other;
get_required_authorities( required_active, required_owner, required_posting, other );
/** posting authority cannot be mixed with active authority in same transaction */
if( required_posting.size() ) {
sign_state s(get_signature_keys( chain_id ),get_posting,available_keys);
s.max_recursion = max_recursion_depth;
FC_ASSERT( !required_owner.size() );
FC_ASSERT( !required_active.size() );
for( auto& posting : required_posting )
s.check_authority( posting );
s.remove_unused_signatures();
set<public_key_type> result;
for( auto& provided_sig : s.provided_signatures )
if( available_keys.find( provided_sig.first ) != available_keys.end() )
result.insert( provided_sig.first );
return result;
}
sign_state s(get_signature_keys( chain_id ),get_active,available_keys);
s.max_recursion = max_recursion_depth;
for( const auto& auth : other )
s.check_authority( auth );
for( auto& owner : required_owner )
s.check_authority( get_owner( owner ) );
for( auto& active : required_active )
s.check_authority( active );
s.remove_unused_signatures();
set<public_key_type> result;
for( auto& provided_sig : s.provided_signatures )
if( available_keys.find( provided_sig.first ) != available_keys.end() )
result.insert( provided_sig.first );
return result;
}
set<public_key_type> signed_transaction::minimize_required_signatures(
const chain_id_type& chain_id,
const flat_set< public_key_type >& available_keys,
const authority_getter& get_active,
const authority_getter& get_owner,
const authority_getter& get_posting,
uint32_t max_recursion
) const
{
set< public_key_type > s = get_required_signatures( chain_id, available_keys, get_active, get_owner, get_posting, max_recursion );
flat_set< public_key_type > result( s.begin(), s.end() );
for( const public_key_type& k : s )
{
result.erase( k );
try
{
steemit::protocol::verify_authority( operations, result, get_active, get_owner, get_posting, max_recursion );
continue; // element stays erased if verify_authority is ok
}
catch( const tx_missing_owner_auth& e ) {}
catch( const tx_missing_active_auth& e ) {}
catch( const tx_missing_posting_auth& e ) {}
catch( const tx_missing_other_auth& e ) {}
result.insert( k );
}
return set<public_key_type>( result.begin(), result.end() );
}
void signed_transaction::verify_authority(
const chain_id_type& chain_id,
const authority_getter& get_active,
const authority_getter& get_owner,
const authority_getter& get_posting,
uint32_t max_recursion )const
{ try {
steemit::protocol::verify_authority( operations, get_signature_keys( chain_id ), get_active, get_owner, get_posting, max_recursion );
} FC_CAPTURE_AND_RETHROW( (*this) ) }
} } // steemit::protocol