EOS区块链的钱包服务器Keosd,提供了sign_transaction开发接口,允许开发者进行交易签名。但是,如果我们希望自己管理密钥而不是交给keosd管理,应该如何在PHP代码中实现交易的离线签名功能?
答案是使用EosTool这个用于EOS应用开发的PHP包。EosTool的LocalSigner类可以轻松实现对EOS交易的离线签名。例如,下面的代码使用自己管理的私钥来签名一个代币转账交易:
代码语言:javascript复制use EosToolSignerLocalSigner;
$tx = [
'expiration'=>'2018-12-04T17:00:00',
'ref_block_num' => 2878,
'ref_block_prefix' => 29012031,
'max_net_usage_words' => 0,
'max_cpu_usage_ms' => 0,
'delay_sec' => 0,
'context_free_actions' => [],
'actions' => [[
'account' => 'eosio.token',
'name' => 'transfer',
'authorization' => [[
'actor' => 'eosio',
'permission' => 'active'
]],
'data' => '1122334455667788990011223344556677.....889900'
]],
'transaction_extensions' => []
];
$pubKeys = [...];
$chainId = '...';
$signer = new LocalSigner(['5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3']/*private keys*/);
$signatures = $signer->sign($tx,$pubKeys,$chainId);
实例化LocalSigner需要传入一组用于签名的私钥,然后调用其sign()
方法传入交易发起账户公钥数组 及区块链ID,返回结果就是交易的权威签名数组。