Author

Topic: Attempting to create a 2 of 3 multi-sig address in C++ (Read 1873 times)

sdp
sr. member
Activity: 470
Merit: 281
It turns out there is no SHA256 on the initial chunk of script bytes at all.  Thanks for all who replied.  For the rest of you, thanks for nothing.   Cheesy

Code:
#include
#include
#include
using namespace bc;
payment_address create_multi_signature_address(const data_chunk& calculated_script) {
    // generate address from script.
short_hash sh2 = generate_ripemd_hash(calculated_script);

data_chunk raw_address;
raw_address.push_back((unsigned char)5);
for (unsigned char c : sh2)
raw_address.push_back(c);

hash_digest check_sum_digest = generate_sha256_hash(raw_address);

for (int i = sha256_digest_size-1; i >= sha256_digest_size-4; --i) {
raw_address.push_back(check_sum_digest[i]);
}

return payment_address(encode_base58(raw_address));
}


Compile with "-lwallet -lboost_system -lcrypto -lbitcoin".
sdp
sr. member
Activity: 470
Merit: 281
I am attempting to create a 2 of 3 multi-sig address in C++.  The example I am using is from http://www.soroushjp.com/2014/12/20/bitcoin-multisig-the-hard-way-understanding-raw-multisignature-bitcoin-transactions/.  I generate the script okay with the keys provided.  But when I generate the address, it is wrong.

Briefly in psuedocode I am trying to do this:

We define intermediate with:
intermediate = 5 concat (  ripemd( sha256( script_bytes ) ) )

Then the address is encodeBase58( intermediate concat sha256( sha256( intermediate ) )[0..3] )


Is this psuedocode wrong?  Here is the C++ code:
Code:
hash_digest lh0 = generate_sha256_hash(calculated_script);

data_chunk d1;
for (int i = 0; i < lh0.size(); ++i)
d1.push_back(lh0[i]);

short_hash sh2 = generate_ripemd_hash(d1);
data_chunk d3;
data_chunk& addy= d3;
d3.push_back((unsigned char)5);
for (int i = 0; i < sh2.size(); ++i)
d3.push_back(sh2[i]);

hash_digest lh4 = generate_sha256_hash(d3);
data_chunk d5;
for (int i = 0; i < lh4.size(); ++i)
d5.push_back(lh4[i]);

hash_digest lh6 = generate_sha256_hash(d5);
for (int i = 0; i < 4; ++i)
d3.push_back(lh6[i]);

calculated_address = encode_base58(addy);


 Huh  I am looking at this page and the code I have here, they look the same.  The addresses do not match in the end though.
Jump to: