PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/BlockIo/examples/dtrust.php

https://gitlab.com/nahoor/public
PHP | 127 lines | 57 code | 43 blank | 27 comment | 5 complexity | e7bc6ec41378ff6e544288e09a570e6a MD5 | raw file
  1. /* This example script does the following:
  2. Demonstration script for:
  3. 1. Creating a 3 of 4 MultiSig address (actually 4 of 5 due to Block.io's key)
  4. 2. Deposit coins into the new MultiSig address
  5. 3. Withdraw coins from MultiSig address back into the sending address in (2)
  6. IMPORTANT! Specify your own API Key and Secret PIN in this code. Keep your Secret PIN safe at all times.
  7. Contact support@block.io for any help with this.
  8. */
  9. <?php
  10. require_once '/path/to/block_io.php';
  11. /* Replace the $apiKey with the API Key from your Block.io Wallet. A different API key exists for Dogecoin, Dogecoin Testnet, Litecoin, Litecoin Testnet, etc. */
  12. $apiKey = 'DogecoinTestnetAPIKey';
  13. $pin = 'SecretPin';
  14. $version = 2; // the API version
  15. $block_io = new BlockIo($apiKey, $pin, $version);
  16. // create 4 keys for a 3 of 4 MultiSig address (1 key is Block.io's, added automatically by Block.io)
  17. $passphrases = [ strToHex('alpha1alpha2alpha3alpha4'), strToHex('alpha2alpha3alpha4alpha1'), strToHex('alpha3alpha4alpha1alpha2'), strToHex('alpha4alpha1alpha2alpha3') ];
  18. $keys = [ $block_io->initKey()->fromPassphrase($passphrases[0]), $block_io->initKey()->fromPassphrase($passphrases[1]), $block_io->initKey()->fromPassphrase($passphrases[2]), $block_io->initKey()->fromPassphrase($passphrases[3]) ];
  19. // create an address with label 'dTrust1' that requires 3 of 4 signatures from the above keys
  20. $pubKeyStr = $keys[0]->getPublicKey() . "," . $keys[1]->getPublicKey() . "," . $keys[2]->getPublicKey() . "," . $keys[3]->getPublicKey();
  21. $dTrustAddress = "";
  22. echo "*** Creating Address with 4 Signers, and 3 Required Signatures: " . "\n";
  23. try {
  24. $response = $block_io->get_new_dtrust_address(array('label' => 'dTrust1', 'public_keys' => $pubKeyStr, 'required_signatures' => 3 ));
  25. $dTrustAddress = $response->data->address;
  26. } catch (Exception $e) {
  27. // print the exception below for debugging
  28. // echo "Exception: " . $e->getMessage() . "\n";
  29. // the label must exist, let's get its address then
  30. $dTrustAddress = $block_io->get_dtrust_address_by_label(array('label' => 'dTrust1'))->data->address;
  31. }
  32. echo "*** Address with Label=dTrust1: " . $dTrustAddress . "\n";
  33. // let's deposit some testnet coins into this address
  34. try {
  35. $response = $block_io->withdraw(array('amounts' => '5.1', 'to_addresses' => $dTrustAddress));
  36. echo "*** Deposit Proof (Tx ID): " . $response->data->txid . "\n";
  37. } catch (Exception $e) {
  38. echo "Exception: " . $e->getMessage() . "\n";
  39. }
  40. // let's get our dtrust address' balance
  41. $response = $block_io->get_dtrust_address_balance(array('label' => 'dTrust1'));
  42. echo "*** dTrust1 Available Balance: " . $response->data->available_balance . " " . $response->data->network . "\n\n";
  43. echo "*** Beginning Withdrawal from dTrust1 to Testnet Default Address: " . "\n";
  44. // let's withdraw coins from dTrust1 and send to the non-dTrust address labeled 'default'
  45. $destAddress = $block_io->get_address_by_label(array('label' => 'default'))->data->address;
  46. echo "**** Destination Address: " . $destAddress . "\n";
  47. $response = $block_io->withdraw_from_dtrust_address(array('from_labels' => 'dTrust1', 'to_addresses' => $destAddress, 'amounts' => '2.0'));
  48. echo "*** Inputs to sign? " . count($response->data->inputs) . "\n";
  49. $refid = $response->data->reference_id;
  50. $counter = 0;
  51. // let's sign all the inputs we can, one key at a time
  52. foreach ($keys as &$key) {
  53. foreach ($response->data->inputs as &$input) {
  54. // iterate over all the inputs
  55. $dataToSign = $input->data_to_sign; // the script sig we need to sign
  56. foreach ($input->signers as &$signer) {
  57. // iterate over all the signers for this input
  58. // find the key that can sign for the signer_public_key
  59. if ($key->getPublicKey() == $signer->signer_public_key)
  60. { // we found the key, let's sign the data
  61. $signer->signed_data = $key->signHash($dataToSign);
  62. echo "* Data Signed By " . $key->getPublicKey() . "\n";
  63. }
  64. }
  65. }
  66. // all the data's signed for this public key, let's give it to Block.io
  67. $json_string = json_encode($response->data);
  68. $r1 = $block_io->sign_transaction(array('signature_data' => $json_string));
  69. echo "* Send signatures for " . $key->getPublicKey() . "? " . $r1->status . "\n";
  70. $counter += 1; // we've signed using an additional key, let's note that down
  71. // let's just use 3 signatures, since we created an address that required 3 of 4 signatures
  72. if ($counter == 3) { break; }
  73. }
  74. try {
  75. // now that everyone's signed the transaction, let's finalize (broadcast) it
  76. $response = $block_io->finalize_transaction(array('reference_id' => $refid));
  77. echo "*** dTrust Withdrawal Proof (Tx ID): " . $response->data->txid . "\n";
  78. } catch (Exception $e) {
  79. echo "Exception: " . $e->getMessage() . "\n";
  80. }
  81. ?>