コード例 #1
0
<?php

use BitWasp\BitcoinLib\BitcoinLib;
use BitWasp\BitcoinLib\RawTransaction;
require_once __DIR__ . '/../vendor/autoload.php';
$m = 2;
$publicKeys = ['0379ddc228d8c44a85ae30c877a6b037ec3d627e0507f223a0412790a83a46cd5f', '024d1cf2ca917f4d679fc02df2a39c0a8110a1b6935b27ae6762a0ceeec7752801', '0258f70f6400aa6f60ff0d21c3aaf1ca236d177877d2b9ad9d2c55280e375ab2d2'];
// It's recommended that you sort the public keys before creating multisig
// Someday this might be standardized in BIP67; https://github.com/bitcoin/bips/pull/146
// Many other libraries already do this too!
RawTransaction::sort_multisig_keys($publicKeys);
// Create redeem script
$redeemScript = RawTransaction::create_multisig($m, $public_keys);
// Display data
echo "Public Keys: \n";
foreach ($publicKeys as $i => $publicKey) {
    echo "{$i} : {$publicKey} \n";
}
echo "\n";
echo "Redeem Script: \n";
echo "{$redeemScript['redeem_script']} \n\n";
echo "Address: \n";
echo "{$redeemScript['address']} \n\n";
コード例 #2
0
 public function testP2SHMultisig2()
 {
     $n = 3;
     $m = 2;
     $privKeys = ["a56a29f79648d95c5666989c9b2b8d40bfe29c4f65b6fbc3e28ed15f8bc46691", "df3fa8db488c6ab6eb31f6b8979dcffd9a7c334196db88b1705bf8bfada41bb2", "2c44e5a2b83abded4e02aae1c3c02a95bf68a4ca56b5473c7f55b8940a5dcfa6"];
     $pubKeys = array_map(function ($privKey) {
         return BitcoinLib::private_key_to_public_key($privKey, true);
     }, $privKeys);
     $pubKeys = RawTransaction::sort_multisig_keys($pubKeys);
     $multisig = RawTransaction::create_multisig($m, $pubKeys);
     $this->assertEquals("3BMH67dedFZTbbtMQ3e7nnKEzHfkwB6VpU", $multisig['address']);
 }
コード例 #3
0
 /**
  * Vendor Accept Order
  *
  * Pass info generated at either buyer_confirm or vendor_accept page
  * via $info, and then create the order/address details.
  *
  * $info = array('vendor_public_keys' => array,
  *                'order_type' => array,
  *                'order' => array
  *                'initiating_user' => array
  *                'update_fields' => array
  *
  * );
  * @param        array $info
  * @return    string/TRUE
  */
 public function vendor_accept_order($info)
 {
     $this->load->model('bitcoin_model');
     $this->load->model('bip32_model');
     $this->load->model('accounts_model');
     if ($info['initiating_user'] == 'buyer') {
         // Buyer public key is in $info.buyerpubkey array, also ID in update fields.
         $buyer_public_key = $info['buyer_public_key'];
         $this->update_order($info['order']['id'], $info['update_fields']);
         foreach ($info['update_fields'] as $key => $field) {
             $info['order'][$key] = $field;
         }
         $info['update_fields'] = array();
     } else {
         $buyer_public_key = $info['order']['public_keys']['buyer'];
     }
     // Add vendors public key no matter what we're doing!
     $vendor_public_key = $this->bip32_model->add_child_key(array('user_id' => $info['order']['vendor']['id'], 'user_role' => 'Vendor', 'order_id' => $info['order']['id'], 'order_hash' => '', 'parent_extended_public_key' => $info['vendor_public_key']['parent_extended_public_key'], 'provider' => $info['vendor_public_key']['provider'], 'extended_public_key' => $info['vendor_public_key']['extended_public_key'], 'public_key' => $info['vendor_public_key']['public_key'], 'key_index' => $info['vendor_public_key']['key_index']));
     // Get vendors public key, stored by that function.
     $admin_public_key = $this->bip32_model->get_next_admin_child();
     if ($admin_public_key == FALSE) {
         return 'An error occured, which prevented your order being created. Please notify an administrator.';
     } else {
         $admin_public_key = $this->bip32_model->add_child_key(array('user_id' => '0', 'user_role' => 'Admin', 'order_id' => $info['order']['id'], 'order_hash' => '', 'parent_extended_public_key' => $admin_public_key['parent_extended_public_key'], 'provider' => 'Manual', 'extended_public_key' => $admin_public_key['extended_public_key'], 'public_key' => $admin_public_key['public_key'], 'key_index' => $admin_public_key['key_index']));
         $public_keys = array($buyer_public_key['public_key'], $vendor_public_key['public_key'], $admin_public_key['public_key']);
         $sorted_keys = RawTransaction::sort_multisig_keys($public_keys);
         $multisig_details = RawTransaction::create_multisig('2', $sorted_keys);
         // If no errors, we're good to create the order!
         if ($multisig_details !== FALSE) {
             $this->bitcoin_model->log_key_usage('order', $this->bw_config->bip32_mpk, $admin_public_key['key_index'], $admin_public_key['public_key'], $info['order']['id']);
             $info['update_fields']['vendor_public_key'] = $vendor_public_key['id'];
             $info['update_fields']['admin_public_key'] = $admin_public_key['id'];
             $info['update_fields']['buyer_public_key'] = $buyer_public_key['id'];
             $info['update_fields']['address'] = $multisig_details['address'];
             $info['update_fields']['redeemScript'] = $multisig_details['redeemScript'];
             $info['update_fields']['selected_payment_type_time'] = time();
             $info['update_fields']['progress'] = 2;
             $info['update_fields']['time'] = time();
             if ($info['order_type'] == 'escrow') {
                 $info['update_fields']['vendor_selected_escrow'] = '1';
                 $info['update_fields']['extra_fees'] = ($info['order']['price'] + $info['order']['shipping_costs']) / 100 * $this->bw_config->escrow_rate;
             } else {
                 $info['update_fields']['vendor_selected_escrow'] = '0';
                 $info['update_fields']['vendor_selected_upfront'] = '1';
                 $info['update_fields']['extra_fees'] = ($info['order']['price'] + $info['order']['shipping_costs']) / 100 * $this->bw_config->upfront_rate;
             }
             if ($this->update_order($info['order']['id'], $info['update_fields']) == TRUE) {
                 $this->bitcoin_model->add_watch_address($multisig_details['address'], 'order');
                 $subject = 'Confirmed Order #' . $info['order']['id'];
                 $message = "Your order with {$info['order']['vendor']['user_name']} has been confirmed.\n" . ($info['order_type'] == 'escrow' ? "Escrow payment was chosen. Once you pay to the address, the vendor will ship the goods. You can raise a dispute if you have any issues." : "You must make payment up-front to complete this order. Once the full amount is sent to the address, you must sign a transaction paying the vendor.");
                 $this->order_model->send_order_message($info['order']['id'], $info['order']['buyer']['user_name'], $subject, $message);
                 $subject = 'New Order #' . $info['order']['id'];
                 $message = "A new order from {$info['order']['buyer']['user_name']} has been confirmed.\n" . ($info['order_type'] == 'escrow' ? "Escrow was chosen for this order. Once paid, you will be asked to sign the transaction to indicate the goods have been dispatched." : "Up-front payment was chosen for this order based on your settings for one of the items. The buyer will be asked to sign the transaction paying you immediately after payment, which you can sign and broadcast to mark the order as dispatched.");
                 $this->order_model->send_order_message($info['order']['id'], $info['order']['vendor']['user_name'], $subject, $message);
                 $msg = $info['initiating_user'] == 'buyer' ? 'This order has been automatically accepted, visit the orders page to see the payment address!' : 'You have accepted this order! Visit the orders page to see the bitcoin address!';
                 $this->current_user->set_return_message($msg, 'success');
                 return TRUE;
             } else {
                 return 'There was an error creating your order.';
             }
         } else {
             return 'Unable to create address.';
         }
     }
 }