public function testResetRequestId()
 {
     $requestId = $this->apiContext->getrequestId();
     $newRequestId = $this->apiContext->resetRequestId();
     $this->assertNotNull($newRequestId);
     $this->assertNotEquals($newRequestId, $requestId);
 }
 /**
  * @param array $path
  * @param ApiContext $apiContext
  * @return string
  * @throws BlockCypherConfigurationException
  */
 private function getAbsoluteUrl($path, $apiContext)
 {
     $credential = $apiContext->getCredential();
     $config = $apiContext->getConfig();
     $endPoint = rtrim(trim($this->_getEndpoint($config)), '/');
     $path = rtrim(trim($path, '/'));
     $url = $endPoint . '/' . $path;
     $url = $this->addTokenToUrl($url, $credential->getAccessToken($config));
     return $url;
 }
 /**
  * Helper method for getting an APIContext for all calls (getting config from array)
  * @param string $token
  * @param string $version v1
  * @param string $coin btc|doge|ltc|uro|bcy
  * @param string $chain main|test3|test
  * @return ApiContext
  */
 private function getApiContextUsingConfigArray($token, $chain = 'main', $coin = 'btc', $version = 'v1')
 {
     $credentials = new SimpleTokenCredential($token);
     $config = array('mode' => 'sandbox', 'log.LogEnabled' => true, 'log.FileName' => $this->logDir . DIRECTORY_SEPARATOR . 'BlockCypher.log', 'log.LogLevel' => 'DEBUG', 'validation.level' => 'disabled');
     $apiContext = ApiContext::create($chain, $coin, $version, $credentials, $config);
     ApiContext::setDefault($apiContext);
     return $apiContext;
 }
 /**
  * @param array $handlers Array of handlers
  * @param string $path Resource path relative to base service endpoint
  * @param string $method HTTP method - one of GET, POST, PUT, DELETE, PATCH etc
  * @param string $data Request payload
  * @param array $headers HTTP headers
  * @return mixed
  * @throws \BlockCypher\Exception\BlockCypherConnectionException
  */
 public function execute($handlers = array(), $path, $method, $data = '', $headers = array())
 {
     $config = $this->apiContext->getConfig();
     $httpConfig = new BlockCypherHttpConfig(null, $method, $config);
     $headers = $headers ? $headers : array();
     $httpConfig->setHeaders($headers + array('Content-Type' => 'application/json'));
     /** @var \BlockCypher\Handler\IBlockCypherHandler $handler */
     foreach ($handlers as $handler) {
         if (!is_object($handler)) {
             $fullHandler = "\\" . (string) $handler;
             $handler = new $fullHandler($this->apiContext);
         }
         $handler->handle($httpConfig, $data, array('path' => $path, 'apiContext' => $this->apiContext));
     }
     $connection = new BlockCypherHttpConnection($httpConfig, $config);
     $response = $connection->execute($data);
     return $response;
 }
 public function setUp()
 {
     parent::SetUp();
     self::$walletName = 'alice';
     $className = $this->getClassName();
     $sampleName = substr($className, 0, -4);
     $this->url = self::baseUrl() . basename(__DIR__) . '/' . $sampleName . '.php';
     $apiContext = ApiContext::create('main', 'btc', 'v1', new SimpleTokenCredential('c0afcccdde5081d6429de37d16166ead'), array('log.LogEnabled' => true, 'log.FileName' => 'BlockCypher.log', 'log.LogLevel' => 'DEBUG'));
     $this->deleteWalletIfExists('alice', $apiContext);
 }
 /**
  * @return ApiContext
  */
 protected static function getApiContext()
 {
     // First try default ApiContext
     $apiContext = ApiContext::getDefault();
     if ($apiContext === null) {
         $apiContext = new ApiContext(self::$credential);
         return $apiContext;
     }
     return $apiContext;
 }
 /**
  * @return ApiContext
  * @throws BlockCypherMissingCredentialException
  */
 public function getApiContext()
 {
     // First try private property
     if ($this->apiContext !== null) {
         return $this->apiContext;
     }
     // Second try default ApiContext
     $apiContext = ApiContext::getDefault();
     if ($apiContext !== null) {
         return $apiContext;
     }
     // Third create a new ApiContext using local credentials
     if ($this->credential !== null) {
         $apiContext = new ApiContext($this->credential);
         return $apiContext;
     }
     throw new BlockCypherMissingCredentialException("Missing ApiContext or Credentials.");
 }
<?php

// Run on console:
// php -f .\sample\wallet-api\GenerateAddressInHDWalletEndpoint.php
require __DIR__ . '/../bootstrap.php';
use BlockCypher\Auth\SimpleTokenCredential;
use BlockCypher\Client\HDWalletClient;
use BlockCypher\Rest\ApiContext;
$apiContext = ApiContext::create('main', 'btc', 'v1', new SimpleTokenCredential('c0afcccdde5081d6429de37d16166ead'), array('mode' => 'sandbox', 'log.LogEnabled' => true, 'log.FileName' => 'BlockCypher.log', 'log.LogLevel' => 'DEBUG'));
$walletClient = new HDWalletClient($apiContext);
$hdWalletGenerateAddressResponse = $walletClient->generateAddress('bob');
ResultPrinter::printResult("Generate Address in a HDWallet", "HDWalletGenerateAddressResponse", $walletName, null, $hdWalletGenerateAddressResponse);
Exemple #9
0
 /**
  * Create new default ApiContext.
  *
  * @param string $chain
  * @param string $coin
  * @param string $version
  * @param TokenCredential $credential
  * @param array|null $config
  * @param string $blockCypherPartnerAttributionId
  * @return ApiContext
  * @throws BlockCypherConfigurationException
  */
 public static function create($chain = 'main', $coin = 'btc', $version = 'v1', $credential = null, $config = null, $blockCypherPartnerAttributionId = null)
 {
     // ### Api context
     // Use an ApiContext object to authenticate
     // API calls. The Token for the
     // SimpleTokenCredential class can be retrieved from
     // https://accounts.blockcypher.com/
     $apiContext = new ApiContext($credential, $chain, $coin, $version);
     if (is_array($config)) {
         $apiContext->setConfig($config);
     } else {
         // BC_CONFIG_PATH should be defined
         if (!defined("BC_CONFIG_PATH")) {
             throw new BlockCypherConfigurationException('BC_CONFIG_PATH should be defined with the path of sdk_config.ini file if no config array is given');
         }
     }
     // Partner Attribution Id. For the time being it is not used.
     if ($blockCypherPartnerAttributionId !== null) {
         $apiContext->addRequestHeader('BlockCypher-Partner-Attribution-Id', $blockCypherPartnerAttributionId);
     }
     return $apiContext;
 }