transaction.cpp 3.7 KB
Newer Older
N
Nathan Hourt 已提交
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
/*
 * Copyright (c) 2017, Respective Authors.
 *
 * The MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include <eos/chain/exceptions.hpp>
#include <fc/io/raw.hpp>
#include <fc/bitutil.hpp>
#include <fc/smart_ref_impl.hpp>
#include <algorithm>

namespace eos { namespace chain {

N
Nathan Hourt 已提交
32
digest_type SignedTransaction::digest()const {
N
Nathan Hourt 已提交
33
   digest_type::encoder enc;
N
Nathan Hourt 已提交
34
   fc::raw::pack( enc, static_cast<const types::Transaction&>(*this) );
N
Nathan Hourt 已提交
35 36 37
   return enc.result();
}

N
Nathan Hourt 已提交
38
digest_type SignedTransaction::sig_digest( const chain_id_type& chain_id )const {
N
Nathan Hourt 已提交
39 40
   digest_type::encoder enc;
   fc::raw::pack( enc, chain_id );
N
Nathan Hourt 已提交
41
   fc::raw::pack( enc, static_cast<const types::Transaction&>(*this) );
N
Nathan Hourt 已提交
42 43 44
   return enc.result();
}

N
Nathan Hourt 已提交
45
eos::chain::transaction_id_type SignedTransaction::id() const {
N
Nathan Hourt 已提交
46 47 48 49 50 51
   auto h = digest();
   transaction_id_type result;
   memcpy(result._hash, h._hash, std::min(sizeof(result), sizeof(h)));
   return result;
}

N
Nathan Hourt 已提交
52
const signature_type& eos::chain::SignedTransaction::sign(const private_key_type& key, const chain_id_type& chain_id) {
N
Nathan Hourt 已提交
53 54 55 56 57
   digest_type h = sig_digest( chain_id );
   signatures.push_back(key.sign_compact(h));
   return signatures.back();
}

N
Nathan Hourt 已提交
58
signature_type eos::chain::SignedTransaction::sign(const private_key_type& key, const chain_id_type& chain_id)const {
N
Nathan Hourt 已提交
59 60
   digest_type::encoder enc;
   fc::raw::pack( enc, chain_id );
N
Nathan Hourt 已提交
61
   fc::raw::pack( enc, static_cast<const types::Transaction&>(*this) );
N
Nathan Hourt 已提交
62 63 64
   return key.sign_compact(enc.result());
}

N
Nathan Hourt 已提交
65
void SignedTransaction::set_reference_block(const block_id_type& reference_block) {
N
Nathan Hourt 已提交
66 67
   refBlockNum = fc::endian_reverse_u32(reference_block._hash[0]);
   refBlockPrefix = reference_block._hash[1];
N
Nathan Hourt 已提交
68 69
}

N
Nathan Hourt 已提交
70
bool SignedTransaction::verify_reference_block(const block_id_type& reference_block) const {
N
Fix #55  
Nathan Hourt 已提交
71
   return refBlockNum == (decltype(refBlockNum))fc::endian_reverse_u32(reference_block._hash[0]) &&
N
Nathan Hourt 已提交
72
         refBlockPrefix == (decltype(refBlockPrefix))reference_block._hash[1];
N
Nathan Hourt 已提交
73 74
}

N
Nathan Hourt 已提交
75
flat_set<public_key_type> SignedTransaction::get_signature_keys( const chain_id_type& chain_id )const
N
Nathan Hourt 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88
{ try {
   auto d = sig_digest( chain_id );
   flat_set<public_key_type> result;
   for( const auto&  sig : signatures )
   {
      EOS_ASSERT(
         result.insert( fc::ecc::public_key(sig,d) ).second,
         tx_duplicate_sig,
         "Duplicate Signature detected" );
   }
   return result;
} FC_CAPTURE_AND_RETHROW() }

N
Nathan Hourt 已提交
89
eos::chain::digest_type SignedTransaction::merkle_digest() const {
N
Nathan Hourt 已提交
90
   digest_type::encoder enc;
N
Nathan Hourt 已提交
91
   fc::raw::pack(enc, static_cast<const types::Transaction&>(*this));
N
Nathan Hourt 已提交
92 93 94 95
   return enc.result();
}

digest_type generated_transaction::merkle_digest() const {
N
Nathan Hourt 已提交
96 97 98 99 100 101
   digest_type::encoder enc;
   fc::raw::pack(enc, *this);
   return enc.result();
}

} } // eos::chain