示例#1
0
 /**
  * Encode Signature
  *
  * This function accepts a signature object, and information about
  * the txout being spent, and the relevant key for signing, and
  * encodes the signature in DER format.
  *
  * @param    \Signature $signature
  * @return    string
  */
 public static function encode_signature(\Signature $signature)
 {
     // Pad r and s to 64 characters.
     $rh = str_pad(BitcoinLib::hex_encode($signature->getR()), 64, '0', STR_PAD_LEFT);
     $sh = str_pad(BitcoinLib::hex_encode($signature->getS()), 64, '0', STR_PAD_LEFT);
     // Check if the first byte of each has its highest bit set,
     $t1 = unpack("H*", pack('H*', substr($rh, 0, 2)) & pack('H*', '80'));
     $t2 = unpack("H*", pack('H*', substr($sh, 0, 2)) & pack('H*', '80'));
     // if so, the result != 00, and must be padded.
     $r = $t1[1] !== '00' ? '00' . $rh : $rh;
     $s = $t2[1] !== '00' ? '00' . $sh : $sh;
     // Create the signature.
     $der_sig = '30' . self::_dec_to_bytes(4 + (strlen($r) + strlen($s)) / 2, 1) . '02' . self::_dec_to_bytes(strlen($r) / 2, 1) . $r . '02' . self::_dec_to_bytes(strlen($s) / 2, 1) . $s . '01';
     return $der_sig;
 }
示例#2
0
 /**
  * Decode Mnemonic
  * 
  * This function decodes a string of 12 words to convert to the electrum
  * seed. This is an implementation of http://tools.ietf.org/html/rfc1751,
  * which is how electrum generates a 128-bit key from 12 words. 
  * 
  * @param	string	$words
  * @return	string
  */
 public static function decode_mnemonic($words)
 {
     $words = explode(" ", $words);
     $out = '';
     $n = 1626;
     for ($i = 0; $i < count($words) / 3; $i++) {
         $a = 3 * $i;
         list($word1, $word2, $word3) = array($words[$a], $words[$a + 1], $words[$a + 2]);
         $index_w1 = array_search($word1, self::$words);
         $index_w2 = array_search($word2, self::$words) % $n;
         $index_w3 = array_search($word3, self::$words) % $n;
         $x = $index_w1 + $n * \gmp_Utils::gmp_mod2($index_w2 - $index_w1, $n) + $n * $n * \gmp_Utils::gmp_mod2($index_w3 - $index_w2, $n);
         $out .= BitcoinLib::hex_encode($x);
     }
     return $out;
 }