Main > Technical Support

How to encode a PTS address?

(1/3) > >>

toast:
http://docs.python.org/2/library/hashlib.html#hashlib.hash.update

In that case it looks like


--- Code: ---ripe160 = hashlib.new('ripemd160')
ripe160.update(h)
d = ripe160.digest()

--- End code ---

is just computing ripemd160(h), no?

5chdn:

--- Quote from: toast on January 03, 2014, 07:07:21 pm ---Ok yeah that script seems wrong. Let me try.


--- Code: ---asm_hash = "your_hash"
extended_hash = "34" + asm_hash
binary_extended_hash = extended_hash.decode("hex")
double_hash = hashlib.sha256(hashlib.sha256(binary_extended_hash))
checksum = double_hash[:3]    # first 4 bytes
binary_address = binary_extended_hash + checksum
pts_address = b58encode(binary_address)

--- End code ---

You might have to insert some prints to see make sure I'm converting between strings, hex, and binary correctly, and adjust accordingly. If you put a print after every line and show us the output we can try to help debug.

--- End quote ---

toast, i really appreciate your help, but the original script is exactly doing what i want. your modified script is encoding correctly the wrong hash.


--- Code: ---# this is the <sig pupkey> asm hash
script = "304402205e81e8ed0b1f7cf6d1d415961859d3b95f5e5c353af303b6cef1e3efa6c3349702202fa9fdd6914abd0e9606c78899e7f3010cafdad211645cf459ae18b3b827b2c101 0365e0beb9a0c1497f3667067aeb8f3ea9dc4c9d5696cee7f19eae49f9457a5cfb".split()

# this is the extracted pubkey
pub_key = script[1]

# this is the binary pubkey? (why is it called 'decode'?)
binary_pub_key = pub_key.decode("hex")

# thats a sha256 performed on the binary pub key
h = hashlib.sha256(binary_pub_key).digest()

# thats generating a new ripemd160 hash, isnt it?
ripe160 = hashlib.new('ripemd160')

# no idea whats happening here?
ripe160.update(h)

# no idea whats happening here?!?
d = ripe160.digest()

# the d is what I want to get, from here I know!
--- End code ---

toast:
Ok yeah that script seems wrong. Let me try.


--- Code: ---asm_hash = "your_hash"
extended_hash = "34" + asm_hash
binary_extended_hash = extended_hash.decode("hex")
double_hash = hashlib.sha256(hashlib.sha256(binary_extended_hash))
checksum = double_hash[:3]    # first 4 bytes
binary_address = binary_extended_hash + checksum
pts_address = b58encode(binary_address)

--- End code ---

You might have to insert some prints to see make sure I'm converting between strings, hex, and binary correctly, and adjust accordingly. If you put a print after every line and show us the output we can try to help debug.

5chdn:

--- Quote from: toast on January 03, 2014, 06:35:46 pm ---Le me know if this helps. I may be able to be more helpful if you go back a few steps and explain where you got your original "ASM hash".

--- End quote ---

This is the ASM hash in the code below:

--- Code: ---script[1] = 0365e0beb9a0c1497f3667067aeb8f3ea9dc4c9d5696cee7f19eae49f9457a5cfb
--- End code ---

And thats where I'm stuck.

--- Quote from: toast on January 03, 2014, 06:35:46 pm ---This is where the python code starts, assigning the string to the variable "script". I cut some unimportant stuff.


--- Code: ---script = "304402205e81e8ed0b1f7cf6d1d415961859d3b95f5e5c353af303b6cef1e3efa6c3349702202fa9fdd6914abd0e9606c78899e7f3010cafdad211645cf459ae18b3b827b2c101 0365e0beb9a0c1497f3667067aeb8f3ea9dc4c9d5696cee7f19eae49f9457a5cfb".split()
h = hashlib.sha256(script[1].decode("hex")).digest()
ripe160 =  hashlib.new('ripemd160')
ripe160.update(h)
d = ripe160.digest()
--- End code ---

This just takes the pubkey and assigns ripemd160(sha256(pubkey)) to the variable "d". The rest of the code is pretty easy to match up with the diagram I linked above.

--- End quote ---

What is h exactly? I wasnt able to reproduce this.

bytemaster:

--- Code: ---namespace bts
{
   pts_address::pts_address()
   {
      memset( addr.data, 0, sizeof(addr.data) );
   }

   pts_address::pts_address( const std::string& base58str )
   {
      std::vector<char> v = fc::from_base58( fc::string(base58str) );
      if( v.size() )
         memcpy( addr.data, v.data(), std::min<size_t>( v.size(), sizeof(addr) ) );

      if( !is_valid() )
      {
         FC_THROW_EXCEPTION( exception, "invalid pts_address ${a}", ("a", base58str) );
      }
   }

   pts_address::pts_address( const fc::ecc::public_key& pub )
   {
       auto dat      = pub.serialize_ecc_point();
       auto sha2     = fc::sha256::hash(dat.data, sizeof(dat) );
       auto rep      = fc::ripemd160::hash((char*)&sha2,sizeof(sha2));
       addr.data[0]  = 56;
       memcpy( addr.data+1, (char*)&rep, sizeof(rep) );
       auto check    = fc::sha256::hash( addr.data, sizeof(rep)+1 );
       check = fc::sha256::hash(check); // double
       memcpy( addr.data+1+sizeof(rep), (char*)&check, 4 );
   }

   /**
    *  Checks the address to verify it has a
    *  valid checksum and prefix.
    */
   bool pts_address::is_valid()const
   {
       if( addr.data[0]  != 56 ) return false;
       auto check    = fc::sha256::hash( addr.data, sizeof(fc::ripemd160)+1 );
       check = fc::sha256::hash(check); // double
       return memcmp( addr.data+1+sizeof(fc::ripemd160), (char*)&check, 4 ) == 0;
   }

   pts_address::operator std::string()const
   {
        return fc::to_base58( addr.data, sizeof(addr) );
   }
}
--- End code ---

bitshares/src/pts_address.cpp

Navigation

[0] Message Index

[#] Next page

Go to full version