在创建账号的时候有一步是 sign_transaction, 具体创建账号的方法请查看
创建账号
REQUEST:
---------------------
POST /v1/wallet/sign_transaction HTTP/1.0
Host: localhost
content-length: 688
Accept: */*
Connection: close
[{"expiration":"2018-06-29T08:02:46","ref_block_num":46,"ref_block_prefix":2928461137,"max_net_usage_words":0,"max_cpu_usage_ms":0,"delay_sec":0,"context_free_actions":[],"actions":[{"account":"eosio","name":"newaccount","authorization":[{"actor":"eosio","permission":"active"}],"data":"0000000000ea305500000000000040c801000000010002f17657f3303512f7b0ea2f2a682aa8b1b693817d98d959800fdcf480881aef0a0100000001000000010002f17657f3303512f7b0ea2f2a682aa8b1b693817d98d959800fdcf480881aef0a01000000"}],"transaction_extensions":[],"signatures":[],"context_free_data":[]},["EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"],"cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f"]
---------------------
RESPONSE:
---------------------
{
"expiration": "2018-06-29T08:02:46",
"ref_block_num": 46,
"ref_block_prefix": 2928461137,
"max_net_usage_words": 0,
"max_cpu_usage_ms": 0,
"delay_sec": 0,
"context_free_actions": [],
"actions": [{
"account": "eosio",
"name": "newaccount",
"authorization": [{
"actor": "eosio",
"permission": "active"
}
],
"data": "0000000000ea305500000000000040c801000000010002f17657f3303512f7b0ea2f2a682aa8b1b693817d98d959800fdcf480881aef0a0100000001000000010002f17657f3303512f7b0ea2f2a682aa8b1b693817d98d959800fdcf480881aef0a01000000"
}
],
"transaction_extensions": [],
"signatures": [
"SIG_K1_KenQx2BuGv5tYU31Q97oQLgowyw9woDhRdxuUcRYrk34qyoBnEMjUcECL14juchJATCzM5rj14LP5ATViy5mDyZVpyGh8N"
],
"context_free_data": []
}
定义签名方法,pk为公钥
wallet_manager.cpp
chain::signed_transaction
wallet_manager::sign_transaction(const chain::signed_transaction& txn, const flat_set<public_key_type>& keys, const chain::chain_id_type& id) {
check_timeout();
chain::signed_transaction stxn(txn);
for (const auto& pk : keys) {
bool found = false;
for (const auto& i : wallets) {
if (!i.second->is_locked()) {
optional<signature_type> sig = i.second->try_sign_digest(stxn.sig_digest(id, stxn.context_free_data), pk);
if (sig) {
stxn.signatures.push_back(*sig);
found = true;
break; // inner for
}
}
}
if (!found) {
EOS_THROW(chain::wallet_missing_pub_key_exception, "Public key not found in unlocked wallets ${k}", ("k", pk));
}
}
return stxn;
}
sig_digest,从这个函数来看,签名包括signed_transaction
digest_type transaction::sig_digest( const chain_id_type& chain_id, const vector<bytes>& cfd )const {
digest_type::encoder enc;
fc::raw::pack( enc, chain_id );链ID
fc::raw::pack( enc, *this );包括整个事务对象
if( cfd.size() ) {
fc::raw::pack( enc, digest_type::hash(cfd) );
} else {
fc::raw::pack( enc, digest_type() );
}
return enc.result();
}
通过公钥找到私钥,然后签名数据。
wallet.cpp
optional<signature_type> try_sign_digest( const digest_type digest, const public_key_type public_key ) {
auto it = _keys.find(public_key);
if( it == _keys.end() )
return optional<signature_type>{};
return it->second.sign(digest);
}
私钥定义
class private_key
{
public:
using storage_type = static_variant<ecc::private_key_shim, r1::private_key_shim>;
private_key() = default;
private_key( private_key&& ) = default;
private_key( const private_key& ) = default;
private_key& operator= (const private_key& ) = default;
public_key get_public_key() const;
signature sign( const sha256& digest, bool require_canonical = true ) const;