Bitcoin Forum
November 09, 2024, 02:40:26 AM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Attempting to create a 2 of 3 multi-sig address in C++  (Read 1864 times)
sdp (OP)
Sr. Member
****
Offline Offline

Activity: 469
Merit: 281



View Profile WWW
March 10, 2015, 05:42:56 PM
 #1

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.

Coinsbank: Left money in their costodial wallet for my signature.  Then they kept the money.
sdp (OP)
Sr. Member
****
Offline Offline

Activity: 469
Merit: 281



View Profile WWW
March 11, 2015, 02:13:23 AM
 #2

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 <bitcoin/bitcoin.hpp>
#include <bitcoin/script.hpp>
#include <wallet/wallet.hpp>
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".

Coinsbank: Left money in their costodial wallet for my signature.  Then they kept the money.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!