示例#1
0
 /**
  * Create
  *
  * This function creates a raw transaction from an array of inputs,
  * and an array of outputs. It takes essentially the same data is
  * bitcoind's createrawtransaction function.
  *
  * Inputs: Each input is a child array of [txid, vout, and optionally a sequence number.]
  * Outputs: Each output is a key in the array: address => $value.
  *
  * @param    array $inputs
  * @param    array $outputs
  * @param   string $magic_byte
  * @return    string/FALSE
  */
 public static function create($inputs, $outputs, $magic_byte = '00')
 {
     // Generate the p2sh byte from the regular byte.
     $regular_byte = $magic_byte;
     $p2sh_byte = str_pad(gmp_strval(gmp_add(gmp_init($magic_byte, 16), gmp_init('05', 16))), 2, '0', STR_PAD_LEFT);
     $tx_array = array('version' => '1');
     // Inputs is the set of [txid/vout/scriptPubKey]
     $tx_array['vin'] = array();
     foreach ($inputs as $input) {
         if (!isset($input['txid']) || strlen($input['txid']) !== 64 || !isset($input['vout']) || !is_numeric($input['vout'])) {
             return FALSE;
         }
         $tx_array['vin'][] = array('txid' => $input['txid'], 'vout' => $input['vout'], 'sequence' => isset($input['sequence']) ? $input['sequence'] : 4294967295, 'scriptSig' => array('hex' => ''));
     }
     // Outputs is the set of [address/amount]
     $tx_array['vout'] = array();
     foreach ($outputs as $address => $value) {
         if (BitcoinLib::validate_address($address, $regular_byte) == FALSE && BitcoinLib::validate_address($address, $p2sh_byte) == FALSE) {
             return FALSE;
         }
         $decode_address = BitcoinLib::base58_decode($address);
         $version = substr($decode_address, 0, 2);
         $hash = substr($decode_address, 2, 40);
         if ($version == $p2sh_byte) {
             // OP_HASH160 <scriptHash> OP_EQUAL
             $scriptPubKey = "a914{$hash}87";
         } else {
             // OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
             $scriptPubKey = "76a914{$hash}88ac";
         }
         $tx_array['vout'][] = array('value' => $value, 'scriptPubKey' => array('hex' => $scriptPubKey));
     }
     $tx_array['locktime'] = 0;
     return self::encode($tx_array);
 }
示例#2
0
 public static function import($ext_key)
 {
     $hex = BitcoinLib::base58_decode($ext_key);
     $key['magic_bytes'] = substr($hex, 0, 8);
     $magic_byte_info = self::describe_magic_bytes($key['magic_bytes']);
     // Die if key type isn't supported by this library.
     if ($magic_byte_info == FALSE) {
         return FALSE;
     }
     $key['type'] = $magic_byte_info['type'];
     $key['testnet'] = $magic_byte_info['testnet'];
     $key['network'] = $magic_byte_info['network'];
     $key['version'] = $magic_byte_info['version'];
     $key['depth'] = gmp_strval(gmp_init(substr($hex, 8, 2), 16), 10);
     $key['fingerprint'] = substr($hex, 10, 8);
     $key['i'] = substr($hex, 18, 8);
     $key['address_number'] = self::get_address_number($key['i']);
     $key['chain_code'] = substr($hex, 26, 64);
     if ($key['type'] == 'public') {
         $key_start_position = 90;
         $offset = 66;
     } else {
         $key_start_position = 92;
         $offset = 64;
     }
     $key['key'] = substr($hex, $key_start_position, $offset);
     // Validate obtained key:
     $validation = $key['type'] == 'public' ? BitcoinLib::validate_public_key($key['key']) : self::check_valid_hmac_key($key['key']);
     return $validation ? $key : FALSE;
 }