Add signing and deposit checks for psbt
This commit is contained in:
parent
4ea8ff3b61
commit
d1c4cbaba8
2 changed files with 92 additions and 16 deletions
|
|
@ -142,8 +142,9 @@ private:
|
||||||
std::string sign_transaction_psbt(const sidechain_transaction_object &sto);
|
std::string sign_transaction_psbt(const sidechain_transaction_object &sto);
|
||||||
std::string sign_transaction_standalone(const sidechain_transaction_object &sto);
|
std::string sign_transaction_standalone(const sidechain_transaction_object &sto);
|
||||||
|
|
||||||
std::string send_transaction_raw(const sidechain_transaction_object &sto);
|
bool send_transaction_raw(const sidechain_transaction_object &sto, std::string &sidechain_transaction);
|
||||||
std::string send_transaction_psbt(const sidechain_transaction_object &sto);
|
bool send_transaction_psbt(const sidechain_transaction_object &sto, std::string &sidechain_transaction);
|
||||||
|
bool send_transaction_standalone(const sidechain_transaction_object &sto, std::string &sidechain_transaction);
|
||||||
|
|
||||||
void handle_event(const std::string &event_data);
|
void handle_event(const std::string &event_data);
|
||||||
std::vector<info_for_vin> extract_info_from_block(const std::string &_block);
|
std::vector<info_for_vin> extract_info_from_block(const std::string &_block);
|
||||||
|
|
|
||||||
|
|
@ -1412,7 +1412,7 @@ std::string sidechain_net_handler_bitcoin::create_deposit_transaction(const son_
|
||||||
|
|
||||||
outputs[pw_address] = transfer_amount;
|
outputs[pw_address] = transfer_amount;
|
||||||
|
|
||||||
return create_transaction(inputs, outputs);
|
return create_transaction_psbt(inputs, outputs);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string sidechain_net_handler_bitcoin::create_withdrawal_transaction(const son_wallet_withdraw_object &swwo) {
|
std::string sidechain_net_handler_bitcoin::create_withdrawal_transaction(const son_wallet_withdraw_object &swwo) {
|
||||||
|
|
@ -1465,8 +1465,8 @@ std::string sidechain_net_handler_bitcoin::create_withdrawal_transaction(const s
|
||||||
std::string sidechain_net_handler_bitcoin::create_multisig_address(const std::vector<std::pair<std::string, uint16_t>> &son_pubkeys) {
|
std::string sidechain_net_handler_bitcoin::create_multisig_address(const std::vector<std::pair<std::string, uint16_t>> &son_pubkeys) {
|
||||||
std::string new_addr = "";
|
std::string new_addr = "";
|
||||||
//new_addr = create_multisig_address_raw(son_pubkeys);
|
//new_addr = create_multisig_address_raw(son_pubkeys);
|
||||||
//new_addr = create_multisig_address_psbt(son_pubkeys);
|
new_addr = create_multisig_address_psbt(son_pubkeys);
|
||||||
new_addr = create_multisig_address_standalone(son_pubkeys);
|
//new_addr = create_multisig_address_standalone(son_pubkeys);
|
||||||
return new_addr;
|
return new_addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1484,15 +1484,25 @@ std::string sidechain_net_handler_bitcoin::create_transaction(const std::vector<
|
||||||
// Function to actually add signature should return transaction with added signature string, or empty string in case of failure
|
// Function to actually add signature should return transaction with added signature string, or empty string in case of failure
|
||||||
std::string sidechain_net_handler_bitcoin::sign_transaction(const sidechain_transaction_object &sto) {
|
std::string sidechain_net_handler_bitcoin::sign_transaction(const sidechain_transaction_object &sto) {
|
||||||
std::string new_tx = "";
|
std::string new_tx = "";
|
||||||
//new_tx = sign_transaction_raw(sto);
|
//new_tx = sign_transaction_raw(sto, complete);
|
||||||
new_tx = sign_transaction_psbt(sto);
|
if (sto.object_id.type() == 30) {
|
||||||
//new_tx = sign_transaction_standalone(sto);
|
new_tx = sign_transaction_psbt(sto, complete);
|
||||||
|
} else {
|
||||||
|
new_tx = sign_transaction_standalone(sto, complete);
|
||||||
|
}
|
||||||
|
//new_tx = sign_transaction_standalone(sto, complete);
|
||||||
return new_tx;
|
return new_tx;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string sidechain_net_handler_bitcoin::send_transaction(const sidechain_transaction_object &sto) {
|
bool sidechain_net_handler_bitcoin::send_transaction(const sidechain_transaction_object &sto, std::string &sidechain_transaction) {
|
||||||
//return send_transaction_raw(sto);
|
sidechain_transaction = "";
|
||||||
return send_transaction_psbt(sto);
|
//return send_transaction_raw(sto, sidechain_transaction);
|
||||||
|
//return send_transaction_psbt(sto, sidechain_transaction);
|
||||||
|
if (sto.object_id.type() == 30) {
|
||||||
|
return send_transaction_psbt(sto, sidechain_transaction);
|
||||||
|
} else {
|
||||||
|
return send_transaction_standalone(sto, sidechain_transaction);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string sidechain_net_handler_bitcoin::create_multisig_address_raw(const std::vector<std::pair<std::string, uint16_t>> &son_pubkeys) {
|
std::string sidechain_net_handler_bitcoin::create_multisig_address_raw(const std::vector<std::pair<std::string, uint16_t>> &son_pubkeys) {
|
||||||
|
|
@ -1523,6 +1533,56 @@ std::string sidechain_net_handler_bitcoin::create_multisig_address_psbt(const st
|
||||||
return bitcoin_client->addmultisigaddress(nrequired, pubkeys);
|
return bitcoin_client->addmultisigaddress(nrequired, pubkeys);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::vector<unsigned char>> read_byte_arrays_from_string(const std::string &string_buf)
|
||||||
|
{
|
||||||
|
std::stringstream ss(string_buf);
|
||||||
|
boost::property_tree::ptree json;
|
||||||
|
boost::property_tree::read_json(ss, json);
|
||||||
|
|
||||||
|
std::vector<bytes> data;
|
||||||
|
for(auto &v: json)
|
||||||
|
{
|
||||||
|
std::string hex = v.second.data();
|
||||||
|
bytes item;
|
||||||
|
item.resize(hex.size() / 2);
|
||||||
|
fc::from_hex(hex, (char*)&item[0], item.size());
|
||||||
|
data.push_back(item);
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string write_byte_arrays_to_string(const std::vector<std::vector<unsigned char>>& data)
|
||||||
|
{
|
||||||
|
std::string res = "[";
|
||||||
|
for (unsigned int idx = 0; idx < data.size(); ++idx) {
|
||||||
|
res += "\"" + fc::to_hex((char*)&data[idx][0], data[idx].size()) + "\"";
|
||||||
|
if (idx != data.size() - 1)
|
||||||
|
res += ",";
|
||||||
|
}
|
||||||
|
res += "]";
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
libbitcoin::chain::script get_multisig_witness_script(const std::vector<libbitcoin::wallet::ec_public> &son_pubkeys)
|
||||||
|
{
|
||||||
|
libbitcoin::point_list keys;
|
||||||
|
for (auto& key : son_pubkeys) {
|
||||||
|
keys.push_back(key.point());
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t nrequired = son_pubkeys.size() * 2 / 3 + 1;
|
||||||
|
libbitcoin::chain::script multisig = libbitcoin::chain::script::to_pay_multisig_pattern(nrequired, keys);
|
||||||
|
return multisig;
|
||||||
|
}
|
||||||
|
|
||||||
|
libbitcoin::chain::script getRedeemScript(const std::vector<libbitcoin::wallet::ec_public> &son_pubkeys) {
|
||||||
|
libbitcoin::chain::script multisig = get_multisig_witness_script(son_pubkeys);
|
||||||
|
libbitcoin::data_chunk multisig_hash = libbitcoin::to_chunk(libbitcoin::sha256_hash(multisig.to_data(0)));
|
||||||
|
libbitcoin::machine::operation::list redeemscript_ops {libbitcoin::machine::operation(libbitcoin::machine::opcode(0)), libbitcoin::machine::operation(multisig_hash)};
|
||||||
|
libbitcoin::chain::script redeem_script = libbitcoin::chain::script(redeemscript_ops);
|
||||||
|
return redeem_script;
|
||||||
|
}
|
||||||
|
|
||||||
libbitcoin::chain::script get_unlock_script(const std::vector<std::pair<std::string, uint16_t>> &son_pubkeys)
|
libbitcoin::chain::script get_unlock_script(const std::vector<std::pair<std::string, uint16_t>> &son_pubkeys)
|
||||||
{
|
{
|
||||||
using namespace libbitcoin;
|
using namespace libbitcoin;
|
||||||
|
|
@ -1916,6 +1976,8 @@ std::string sidechain_net_handler_bitcoin::sign_transaction_standalone(const sid
|
||||||
|
|
||||||
|
|
||||||
libbitcoin::data_chunk data;
|
libbitcoin::data_chunk data;
|
||||||
|
libbitcoin::ec_secret key;
|
||||||
|
libbitcoin::decode_base16(key, prvkey);
|
||||||
std::vector<uint64_t> in_amounts;
|
std::vector<uint64_t> in_amounts;
|
||||||
read_tx_data_from_string(sto.transaction, data, in_amounts);
|
read_tx_data_from_string(sto.transaction, data, in_amounts);
|
||||||
libbitcoin::chain::transaction tx;
|
libbitcoin::chain::transaction tx;
|
||||||
|
|
@ -1924,12 +1986,19 @@ std::string sidechain_net_handler_bitcoin::sign_transaction_standalone(const sid
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::pair<std::string, uint16_t>> son_pubkeys;
|
std::vector<ec_public> son_pubkeys;
|
||||||
libbitcoin::chain::script witness_script = get_unlock_script(son_pubkeys);
|
for (auto& son: sto.signers) {
|
||||||
|
std::string pub_key = son.sidechain_public_keys.at(sidechain_type::bitcoin);
|
||||||
|
son_pubkeys.push_back(ec_public(pub_key));
|
||||||
std::string tx_signature = "";
|
}
|
||||||
|
libbitcoin::chain::script witness_script = get_multisig_witness_script(son_pubkeys);
|
||||||
|
vector<endorsement> sigs;
|
||||||
|
sigs.resize(tx.inputs().size());
|
||||||
|
for (unsigned int itr = 0; itr < sigs.size(); itr++) {
|
||||||
|
libbitcoin::chain::script::create_endorsement(sigs.at(itr), key, witness_script, tx, itr, sighash_algorithm::all, script_version::zero, in_amounts[itr]);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string tx_signature = write_byte_arrays_to_string(sigs);
|
||||||
complete = true;
|
complete = true;
|
||||||
return tx_signature;
|
return tx_signature;
|
||||||
}
|
}
|
||||||
|
|
@ -1980,6 +2049,12 @@ std::string sidechain_net_handler_bitcoin::send_transaction_psbt(const sidechain
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool sidechain_net_handler_bitcoin::send_transaction_standalone(const sidechain_transaction_object &sto, std::string &sidechain_transaction) {
|
||||||
|
sidechain_transaction = "";
|
||||||
|
|
||||||
|
return bitcoin_client->sendrawtransaction(sto.transaction);
|
||||||
|
}
|
||||||
|
|
||||||
void sidechain_net_handler_bitcoin::handle_event(const std::string &event_data) {
|
void sidechain_net_handler_bitcoin::handle_event(const std::string &event_data) {
|
||||||
std::string block = bitcoin_client->getblock(event_data);
|
std::string block = bitcoin_client->getblock(event_data);
|
||||||
if (block != "") {
|
if (block != "") {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue