Completed
Branch master (7ca1f6)
by
unknown
02:09
created

BlocktrailSDK::sendTransaction()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.0852

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 5
nop 4
dl 0
loc 30
ccs 13
cts 15
cp 0.8667
crap 6.0852
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Blocktrail\SDK;
4
5
use BitWasp\Bitcoin\Address\AddressFactory;
6
use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress;
7
use BitWasp\Bitcoin\Bitcoin;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\EcSerializer;
9
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
10
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Signature\CompactSignatureSerializerInterface;
11
use BitWasp\Bitcoin\Crypto\Random\Random;
12
use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKey;
13
use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKeyFactory;
14
use BitWasp\Bitcoin\MessageSigner\MessageSigner;
15
use BitWasp\Bitcoin\MessageSigner\SignedMessage;
16
use BitWasp\Bitcoin\Mnemonic\Bip39\Bip39SeedGenerator;
17
use BitWasp\Bitcoin\Mnemonic\MnemonicFactory;
18
use BitWasp\Bitcoin\Network\NetworkFactory;
19
use BitWasp\Bitcoin\Transaction\TransactionFactory;
20
use BitWasp\Buffertools\Buffer;
21
use BitWasp\Buffertools\BufferInterface;
22
use Blocktrail\CryptoJSAES\CryptoJSAES;
23
use Blocktrail\SDK\Bitcoin\BIP32Key;
24
use Blocktrail\SDK\Connection\RestClient;
25
use Blocktrail\SDK\Exceptions\BlocktrailSDKException;
26
use Blocktrail\SDK\V3Crypt\Encryption;
27
use Blocktrail\SDK\V3Crypt\EncryptionMnemonic;
28
use Blocktrail\SDK\V3Crypt\KeyDerivation;
29
30
/**
31
 * Class BlocktrailSDK
32
 */
33
class BlocktrailSDK implements BlocktrailSDKInterface {
34
    /**
35
     * @var Connection\RestClient
36
     */
37
    protected $client;
38
39
    /**
40
     * @var string          currently only supporting; bitcoin
41
     */
42
    protected $network;
43
44
    /**
45
     * @var bool
46
     */
47
    protected $testnet;
48
49
    /**
50
     * @param   string      $apiKey         the API_KEY to use for authentication
51
     * @param   string      $apiSecret      the API_SECRET to use for authentication
52
     * @param   string      $network        the cryptocurrency 'network' to consume, eg BTC, LTC, etc
53
     * @param   bool        $testnet        testnet yes/no
54
     * @param   string      $apiVersion     the version of the API to consume
55
     * @param   null        $apiEndpoint    overwrite the endpoint used
56
     *                                       this will cause the $network, $testnet and $apiVersion to be ignored!
57
     */
58 81
    public function __construct($apiKey, $apiSecret, $network = 'BTC', $testnet = false, $apiVersion = 'v1', $apiEndpoint = null) {
59
60 81
        list ($apiNetwork, $testnet) = Util::parseApiNetwork($network, $testnet);
61
62 81
        if (is_null($apiEndpoint)) {
63 81
            $apiEndpoint = getenv('BLOCKTRAIL_SDK_API_ENDPOINT') ?: "https://api.blocktrail.com";
64 81
            $apiEndpoint = "{$apiEndpoint}/{$apiVersion}/{$apiNetwork}/";
65
        }
66
67
        // normalize network and set bitcoinlib to the right magic-bytes
68 81
        list($this->network, $this->testnet) = $this->normalizeNetwork($network, $testnet);
69 81
        $this->setBitcoinLibMagicBytes($this->network, $this->testnet);
70
71 81
        $this->client = new RestClient($apiEndpoint, $apiVersion, $apiKey, $apiSecret);
72 81
    }
73
74
    /**
75
     * normalize network string
76
     *
77
     * @param $network
78
     * @param $testnet
79
     * @return array
80
     * @throws \Exception
81
     */
82 81
    protected function normalizeNetwork($network, $testnet) {
83 81
        return Util::normalizeNetwork($network, $testnet);
84
    }
85
86
    /**
87
     * set BitcoinLib to the correct magic-byte defaults for the selected network
88
     *
89
     * @param $network
90
     * @param $testnet
91
     */
92 81
    protected function setBitcoinLibMagicBytes($network, $testnet) {
93 81
        assert($network == "bitcoin" || $network == "bitcoincash");
94 81
        Bitcoin::setNetwork($testnet ? NetworkFactory::bitcoinTestnet() : NetworkFactory::bitcoin());
95 81
    }
96
97
    /**
98
     * enable CURL debugging output
99
     *
100
     * @param   bool        $debug
101
     *
102
     * @codeCoverageIgnore
103
     */
104
    public function setCurlDebugging($debug = true) {
105
        $this->client->setCurlDebugging($debug);
106
    }
107
108
    /**
109
     * enable verbose errors
110
     *
111
     * @param   bool        $verboseErrors
112
     *
113
     * @codeCoverageIgnore
114
     */
115
    public function setVerboseErrors($verboseErrors = true) {
116
        $this->client->setVerboseErrors($verboseErrors);
117
    }
118
    
119
    /**
120
     * set cURL default option on Guzzle client
121
     * @param string    $key
122
     * @param bool      $value
123
     *
124
     * @codeCoverageIgnore
125
     */
126
    public function setCurlDefaultOption($key, $value) {
127
        $this->client->setCurlDefaultOption($key, $value);
128
    }
129
130
    /**
131
     * @return  RestClient
132
     */
133 2
    public function getRestClient() {
134 2
        return $this->client;
135
    }
136
137
    /**
138
     * get a single address
139
     * @param  string $address address hash
140
     * @return array           associative array containing the response
141
     */
142 1
    public function address($address) {
143 1
        $response = $this->client->get("address/{$address}");
144 1
        return self::jsonDecode($response->body(), true);
145
    }
146
147
    /**
148
     * get all transactions for an address (paginated)
149
     * @param  string  $address address hash
150
     * @param  integer $page    pagination: page number
151
     * @param  integer $limit   pagination: records per page (max 500)
152
     * @param  string  $sortDir pagination: sort direction (asc|desc)
153
     * @return array            associative array containing the response
154
     */
155 1
    public function addressTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') {
156
        $queryString = [
157 1
            'page' => $page,
158 1
            'limit' => $limit,
159 1
            'sort_dir' => $sortDir
160
        ];
161 1
        $response = $this->client->get("address/{$address}/transactions", $queryString);
162 1
        return self::jsonDecode($response->body(), true);
163
    }
164
165
    /**
166
     * get all unconfirmed transactions for an address (paginated)
167
     * @param  string  $address address hash
168
     * @param  integer $page    pagination: page number
169
     * @param  integer $limit   pagination: records per page (max 500)
170
     * @param  string  $sortDir pagination: sort direction (asc|desc)
171
     * @return array            associative array containing the response
172
     */
173 1
    public function addressUnconfirmedTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') {
174
        $queryString = [
175 1
            'page' => $page,
176 1
            'limit' => $limit,
177 1
            'sort_dir' => $sortDir
178
        ];
179 1
        $response = $this->client->get("address/{$address}/unconfirmed-transactions", $queryString);
180 1
        return self::jsonDecode($response->body(), true);
181
    }
182
183
    /**
184
     * get all unspent outputs for an address (paginated)
185
     * @param  string  $address address hash
186
     * @param  integer $page    pagination: page number
187
     * @param  integer $limit   pagination: records per page (max 500)
188
     * @param  string  $sortDir pagination: sort direction (asc|desc)
189
     * @return array            associative array containing the response
190
     */
191 1
    public function addressUnspentOutputs($address, $page = 1, $limit = 20, $sortDir = 'asc') {
192
        $queryString = [
193 1
            'page' => $page,
194 1
            'limit' => $limit,
195 1
            'sort_dir' => $sortDir
196
        ];
197 1
        $response = $this->client->get("address/{$address}/unspent-outputs", $queryString);
198 1
        return self::jsonDecode($response->body(), true);
199
    }
200
201
    /**
202
     * get all unspent outputs for a batch of addresses (paginated)
203
     *
204
     * @param  string[] $addresses
205
     * @param  integer  $page    pagination: page number
206
     * @param  integer  $limit   pagination: records per page (max 500)
207
     * @param  string   $sortDir pagination: sort direction (asc|desc)
208
     * @return array associative array containing the response
209
     * @throws \Exception
210
     */
211
    public function batchAddressUnspentOutputs($addresses, $page = 1, $limit = 20, $sortDir = 'asc') {
212
        $queryString = [
213
            'page' => $page,
214
            'limit' => $limit,
215
            'sort_dir' => $sortDir
216
        ];
217
        $response = $this->client->post("address/unspent-outputs", $queryString, ['addresses' => $addresses]);
218
        return self::jsonDecode($response->body(), true);
219
    }
220
221
    /**
222
     * verify ownership of an address
223
     * @param  string  $address     address hash
224
     * @param  string  $signature   a signed message (the address hash) using the private key of the address
225
     * @return array                associative array containing the response
226
     */
227 2
    public function verifyAddress($address, $signature) {
228 2
        $postData = ['signature' => $signature];
229
230 2
        $response = $this->client->post("address/{$address}/verify", null, $postData, RestClient::AUTH_HTTP_SIG);
231
232 2
        return self::jsonDecode($response->body(), true);
233
    }
234
235
    /**
236
     * get all blocks (paginated)
237
     * @param  integer $page    pagination: page number
238
     * @param  integer $limit   pagination: records per page
239
     * @param  string  $sortDir pagination: sort direction (asc|desc)
240
     * @return array            associative array containing the response
241
     */
242 1
    public function allBlocks($page = 1, $limit = 20, $sortDir = 'asc') {
243
        $queryString = [
244 1
            'page' => $page,
245 1
            'limit' => $limit,
246 1
            'sort_dir' => $sortDir
247
        ];
248 1
        $response = $this->client->get("all-blocks", $queryString);
249 1
        return self::jsonDecode($response->body(), true);
250
    }
251
252
    /**
253
     * get the latest block
254
     * @return array            associative array containing the response
255
     */
256 1
    public function blockLatest() {
257 1
        $response = $this->client->get("block/latest");
258 1
        return self::jsonDecode($response->body(), true);
259
    }
260
261
    /**
262
     * get an individual block
263
     * @param  string|integer $block    a block hash or a block height
264
     * @return array                    associative array containing the response
265
     */
266 1
    public function block($block) {
267 1
        $response = $this->client->get("block/{$block}");
268 1
        return self::jsonDecode($response->body(), true);
269
    }
270
271
    /**
272
     * get all transaction in a block (paginated)
273
     * @param  string|integer   $block   a block hash or a block height
274
     * @param  integer          $page    pagination: page number
275
     * @param  integer          $limit   pagination: records per page
276
     * @param  string           $sortDir pagination: sort direction (asc|desc)
277
     * @return array                     associative array containing the response
278
     */
279 1
    public function blockTransactions($block, $page = 1, $limit = 20, $sortDir = 'asc') {
280
        $queryString = [
281 1
            'page' => $page,
282 1
            'limit' => $limit,
283 1
            'sort_dir' => $sortDir
284
        ];
285 1
        $response = $this->client->get("block/{$block}/transactions", $queryString);
286 1
        return self::jsonDecode($response->body(), true);
287
    }
288
289
    /**
290
     * get a single transaction
291
     * @param  string $txhash transaction hash
292
     * @return array          associative array containing the response
293
     */
294 5
    public function transaction($txhash) {
295 5
        $response = $this->client->get("transaction/{$txhash}");
296 5
        return self::jsonDecode($response->body(), true);
297
    }
298
299
    /**
300
     * get a single transaction
301
     * @param  string[] $txhashes list of transaction hashes (up to 20)
302
     * @return array[]            array containing the response
303
     */
304
    public function transactions($txhashes) {
305
        $response = $this->client->get("transactions/" . implode(",", $txhashes));
306
        return self::jsonDecode($response->body(), true);
307
    }
308
    
309
    /**
310
     * get a paginated list of all webhooks associated with the api user
311
     * @param  integer          $page    pagination: page number
312
     * @param  integer          $limit   pagination: records per page
313
     * @return array                     associative array containing the response
314
     */
315 1
    public function allWebhooks($page = 1, $limit = 20) {
316
        $queryString = [
317 1
            'page' => $page,
318 1
            'limit' => $limit
319
        ];
320 1
        $response = $this->client->get("webhooks", $queryString);
321 1
        return self::jsonDecode($response->body(), true);
322
    }
323
324
    /**
325
     * get an existing webhook by it's identifier
326
     * @param string    $identifier     a unique identifier associated with the webhook
327
     * @return array                    associative array containing the response
328
     */
329 1
    public function getWebhook($identifier) {
330 1
        $response = $this->client->get("webhook/".$identifier);
331 1
        return self::jsonDecode($response->body(), true);
332
    }
333
334
    /**
335
     * create a new webhook
336
     * @param  string  $url        the url to receive the webhook events
337
     * @param  string  $identifier a unique identifier to associate with this webhook
338
     * @return array               associative array containing the response
339
     */
340 1
    public function setupWebhook($url, $identifier = null) {
341
        $postData = [
342 1
            'url'        => $url,
343 1
            'identifier' => $identifier
344
        ];
345 1
        $response = $this->client->post("webhook", null, $postData, RestClient::AUTH_HTTP_SIG);
346 1
        return self::jsonDecode($response->body(), true);
347
    }
348
349
    /**
350
     * update an existing webhook
351
     * @param  string  $identifier      the unique identifier of the webhook to update
352
     * @param  string  $newUrl          the new url to receive the webhook events
353
     * @param  string  $newIdentifier   a new unique identifier to associate with this webhook
354
     * @return array                    associative array containing the response
355
     */
356 1
    public function updateWebhook($identifier, $newUrl = null, $newIdentifier = null) {
357
        $putData = [
358 1
            'url'        => $newUrl,
359 1
            'identifier' => $newIdentifier
360
        ];
361 1
        $response = $this->client->put("webhook/{$identifier}", null, $putData, RestClient::AUTH_HTTP_SIG);
362 1
        return self::jsonDecode($response->body(), true);
363
    }
364
365
    /**
366
     * deletes an existing webhook and any event subscriptions associated with it
367
     * @param  string  $identifier      the unique identifier of the webhook to delete
368
     * @return boolean                  true on success
369
     */
370 1
    public function deleteWebhook($identifier) {
371 1
        $response = $this->client->delete("webhook/{$identifier}", null, null, RestClient::AUTH_HTTP_SIG);
372 1
        return self::jsonDecode($response->body(), true);
373
    }
374
375
    /**
376
     * get a paginated list of all the events a webhook is subscribed to
377
     * @param  string  $identifier  the unique identifier of the webhook
378
     * @param  integer $page        pagination: page number
379
     * @param  integer $limit       pagination: records per page
380
     * @return array                associative array containing the response
381
     */
382 2
    public function getWebhookEvents($identifier, $page = 1, $limit = 20) {
383
        $queryString = [
384 2
            'page' => $page,
385 2
            'limit' => $limit
386
        ];
387 2
        $response = $this->client->get("webhook/{$identifier}/events", $queryString);
388 2
        return self::jsonDecode($response->body(), true);
389
    }
390
    
391
    /**
392
     * subscribes a webhook to transaction events of one particular transaction
393
     * @param  string  $identifier      the unique identifier of the webhook to be triggered
394
     * @param  string  $transaction     the transaction hash
395
     * @param  integer $confirmations   the amount of confirmations to send.
396
     * @return array                    associative array containing the response
397
     */
398 1
    public function subscribeTransaction($identifier, $transaction, $confirmations = 6) {
399
        $postData = [
400 1
            'event_type'    => 'transaction',
401 1
            'transaction'   => $transaction,
402 1
            'confirmations' => $confirmations,
403
        ];
404 1
        $response = $this->client->post("webhook/{$identifier}/events", null, $postData, RestClient::AUTH_HTTP_SIG);
405 1
        return self::jsonDecode($response->body(), true);
406
    }
407
408
    /**
409
     * subscribes a webhook to transaction events on a particular address
410
     * @param  string  $identifier      the unique identifier of the webhook to be triggered
411
     * @param  string  $address         the address hash
412
     * @param  integer $confirmations   the amount of confirmations to send.
413
     * @return array                    associative array containing the response
414
     */
415 1
    public function subscribeAddressTransactions($identifier, $address, $confirmations = 6) {
416
        $postData = [
417 1
            'event_type'    => 'address-transactions',
418 1
            'address'       => $address,
419 1
            'confirmations' => $confirmations,
420
        ];
421 1
        $response = $this->client->post("webhook/{$identifier}/events", null, $postData, RestClient::AUTH_HTTP_SIG);
422 1
        return self::jsonDecode($response->body(), true);
423
    }
424
425
    /**
426
     * batch subscribes a webhook to multiple transaction events
427
     *
428
     * @param  string $identifier   the unique identifier of the webhook
429
     * @param  array  $batchData    A 2D array of event data:
430
     *                              [address => $address, confirmations => $confirmations]
431
     *                              where $address is the address to subscibe to
432
     *                              and optionally $confirmations is the amount of confirmations
433
     * @return boolean              true on success
434
     */
435 1
    public function batchSubscribeAddressTransactions($identifier, $batchData) {
436 1
        $postData = [];
437 1
        foreach ($batchData as $record) {
438 1
            $postData[] = [
439 1
                'event_type' => 'address-transactions',
440 1
                'address' => $record['address'],
441 1
                'confirmations' => isset($record['confirmations']) ? $record['confirmations'] : 6,
442
            ];
443
        }
444 1
        $response = $this->client->post("webhook/{$identifier}/events/batch", null, $postData, RestClient::AUTH_HTTP_SIG);
445 1
        return self::jsonDecode($response->body(), true);
446
    }
447
448
    /**
449
     * subscribes a webhook to a new block event
450
     * @param  string  $identifier  the unique identifier of the webhook to be triggered
451
     * @return array                associative array containing the response
452
     */
453 1
    public function subscribeNewBlocks($identifier) {
454
        $postData = [
455 1
            'event_type'    => 'block',
456
        ];
457 1
        $response = $this->client->post("webhook/{$identifier}/events", null, $postData, RestClient::AUTH_HTTP_SIG);
458 1
        return self::jsonDecode($response->body(), true);
459
    }
460
461
    /**
462
     * removes an transaction event subscription from a webhook
463
     * @param  string  $identifier      the unique identifier of the webhook associated with the event subscription
464
     * @param  string  $transaction     the transaction hash of the event subscription
465
     * @return boolean                  true on success
466
     */
467 1
    public function unsubscribeTransaction($identifier, $transaction) {
468 1
        $response = $this->client->delete("webhook/{$identifier}/transaction/{$transaction}", null, null, RestClient::AUTH_HTTP_SIG);
469 1
        return self::jsonDecode($response->body(), true);
470
    }
471
472
    /**
473
     * removes an address transaction event subscription from a webhook
474
     * @param  string  $identifier      the unique identifier of the webhook associated with the event subscription
475
     * @param  string  $address         the address hash of the event subscription
476
     * @return boolean                  true on success
477
     */
478 1
    public function unsubscribeAddressTransactions($identifier, $address) {
479 1
        $response = $this->client->delete("webhook/{$identifier}/address-transactions/{$address}", null, null, RestClient::AUTH_HTTP_SIG);
480 1
        return self::jsonDecode($response->body(), true);
481
    }
482
483
    /**
484
     * removes a block event subscription from a webhook
485
     * @param  string  $identifier      the unique identifier of the webhook associated with the event subscription
486
     * @return boolean                  true on success
487
     */
488 1
    public function unsubscribeNewBlocks($identifier) {
489 1
        $response = $this->client->delete("webhook/{$identifier}/block", null, null, RestClient::AUTH_HTTP_SIG);
490 1
        return self::jsonDecode($response->body(), true);
491
    }
492
493
    /**
494
     * create a new wallet
495
     *   - will generate a new primary seed (with password) and backup seed (without password)
496
     *   - send the primary seed (BIP39 'encrypted') and backup public key to the server
497
     *   - receive the blocktrail co-signing public key from the server
498
     *
499
     * Either takes one argument:
500
     * @param array $options
501
     *
502
     * Or takes three arguments (old, deprecated syntax):
503
     * (@nonPHP-doc) @param      $identifier
504
     * (@nonPHP-doc) @param      $password
505
     * (@nonPHP-doc) @param int  $keyIndex          override for the blocktrail cosigning key to use
0 ignored issues
show
Bug introduced by
There is no parameter named $keyIndex. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
506
     *
507
     * @return array[WalletInterface, array]      list($wallet, $backupInfo)
0 ignored issues
show
Documentation introduced by
The doc-type array[WalletInterface, could not be parsed: Expected "]" at position 2, but found "WalletInterface". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
508
     * @throws \Exception
509
     */
510 7
    public function createNewWallet($options) {
511 7
        if (!is_array($options)) {
512 1
            $args = func_get_args();
513
            $options = [
514 1
                "identifier" => $args[0],
515 1
                "password" => $args[1],
516 1
                "key_index" => isset($args[2]) ? $args[2] : null,
517
            ];
518
        }
519
520 7
        if (isset($options['password'])) {
521 1
            if (isset($options['passphrase'])) {
522
                throw new \InvalidArgumentException("Can only provide either passphrase or password");
523
            } else {
524 1
                $options['passphrase'] = $options['password'];
525
            }
526
        }
527
528 7
        if (!isset($options['passphrase'])) {
529 1
            $options['passphrase'] = null;
530
        }
531
532 7
        if (!isset($options['key_index'])) {
533
            $options['key_index'] = 0;
534
        }
535
536 7
        if (!isset($options['wallet_version'])) {
537 3
            $options['wallet_version'] = Wallet::WALLET_VERSION_V3;
538
        }
539
540 7
        switch ($options['wallet_version']) {
541 7
            case Wallet::WALLET_VERSION_V1:
542 1
                return $this->createNewWalletV1($options);
543
544 6
            case Wallet::WALLET_VERSION_V2:
545 2
                return $this->createNewWalletV2($options);
546
547 4
            case Wallet::WALLET_VERSION_V3:
548 4
                return $this->createNewWalletV3($options);
549
550
            default:
551
                throw new \InvalidArgumentException("Invalid wallet version");
552
        }
553
    }
554
555 1
    protected function createNewWalletV1($options) {
556 1
        $walletPath = WalletPath::create($options['key_index']);
557
558 1
        $storePrimaryMnemonic = isset($options['store_primary_mnemonic']) ? $options['store_primary_mnemonic'] : null;
559
560 1
        if (isset($options['primary_mnemonic']) && isset($options['primary_private_key'])) {
561
            throw new \InvalidArgumentException("Can't specify Primary Mnemonic and Primary PrivateKey");
562
        }
563
564 1
        $primaryMnemonic = null;
565 1
        $primaryPrivateKey = null;
566 1
        if (!isset($options['primary_mnemonic']) && !isset($options['primary_private_key'])) {
567 1
            if (!$options['passphrase']) {
568
                throw new \InvalidArgumentException("Can't generate Primary Mnemonic without a passphrase");
569
            } else {
570
                // create new primary seed
571
                /** @var HierarchicalKey $primaryPrivateKey */
572 1
                list($primaryMnemonic, , $primaryPrivateKey) = $this->newPrimarySeed($options['passphrase']);
573 1
                if ($storePrimaryMnemonic !== false) {
574 1
                    $storePrimaryMnemonic = true;
575
                }
576
            }
577
        } elseif (isset($options['primary_mnemonic'])) {
578
            $primaryMnemonic = $options['primary_mnemonic'];
579
        } elseif (isset($options['primary_private_key'])) {
580
            $primaryPrivateKey = $options['primary_private_key'];
581
        }
582
583 1
        if ($storePrimaryMnemonic && $primaryMnemonic && !$options['passphrase']) {
584
            throw new \InvalidArgumentException("Can't store Primary Mnemonic on server without a passphrase");
585
        }
586
587 1
        if ($primaryPrivateKey) {
588 1
            if (is_string($primaryPrivateKey)) {
589 1
                $primaryPrivateKey = [$primaryPrivateKey, "m"];
590
            }
591
        } else {
592
            $primaryPrivateKey = HierarchicalKeyFactory::fromEntropy((new Bip39SeedGenerator())->getSeed($primaryMnemonic, $options['passphrase']));
593
        }
594
595 1
        if (!$storePrimaryMnemonic) {
596
            $primaryMnemonic = false;
597
        }
598
599
        // create primary public key from the created private key
600 1
        $path = $walletPath->keyIndexPath()->publicPath();
601 1
        $primaryPublicKey = BIP32Key::create($primaryPrivateKey, "m")->buildKey($path);
602
603 1
        if (isset($options['backup_mnemonic']) && $options['backup_public_key']) {
604
            throw new \InvalidArgumentException("Can't specify Backup Mnemonic and Backup PublicKey");
605
        }
606
607 1
        $backupMnemonic = null;
608 1
        $backupPublicKey = null;
609 1
        if (!isset($options['backup_mnemonic']) && !isset($options['backup_public_key'])) {
610
            /** @var HierarchicalKey $backupPrivateKey */
611 1
            list($backupMnemonic, , ) = $this->newBackupSeed();
612
        } else if (isset($options['backup_mnemonic'])) {
613
            $backupMnemonic = $options['backup_mnemonic'];
614
        } elseif (isset($options['backup_public_key'])) {
615
            $backupPublicKey = $options['backup_public_key'];
616
        }
617
618 1
        if ($backupPublicKey) {
619
            if (is_string($backupPublicKey)) {
620
                $backupPublicKey = [$backupPublicKey, "m"];
621
            }
622
        } else {
623 1
            $backupPrivateKey = HierarchicalKeyFactory::fromEntropy((new Bip39SeedGenerator())->getSeed($backupMnemonic, ""));
624 1
            $backupPublicKey = BIP32Key::create($backupPrivateKey->toPublic(), "M");
625
        }
626
627
        // create a checksum of our private key which we'll later use to verify we used the right password
628 1
        $checksum = $primaryPrivateKey->getPublicKey()->getAddress()->getAddress();
629
630
        // send the public keys to the server to store them
631
        //  and the mnemonic, which is safe because it's useless without the password
632 1
        $data = $this->storeNewWalletV1($options['identifier'], $primaryPublicKey->tuple(), $backupPublicKey->tuple(), $primaryMnemonic, $checksum, $options['key_index']);
633
634
        // received the blocktrail public keys
635
        $blocktrailPublicKeys = Util::arrayMapWithIndex(function ($keyIndex, $pubKeyTuple) {
636 1
            return [$keyIndex, BIP32Key::create(HierarchicalKeyFactory::fromExtended($pubKeyTuple[0]), $pubKeyTuple[1])];
637 1
        }, $data['blocktrail_public_keys']);
638
639 1
        $wallet = new WalletV1(
640 1
            $this,
641 1
            $options['identifier'],
642 1
            $primaryMnemonic,
643 1
            [$options['key_index'] => $primaryPublicKey],
644 1
            $backupPublicKey,
645 1
            $blocktrailPublicKeys,
646 1
            $options['key_index'],
647 1
            $this->network,
648 1
            $this->testnet,
649 1
            array_key_exists('segwit', $data) ? $data['segwit'] : false,
650 1
            $checksum
651
        );
652
653 1
        $wallet->unlock($options);
654
655
        // return wallet and backup mnemonic
656
        return [
657 1
            $wallet,
658
            [
659 1
                'primary_mnemonic' => $primaryMnemonic,
660 1
                'backup_mnemonic' => $backupMnemonic,
661 1
                'blocktrail_public_keys' => $blocktrailPublicKeys,
662
            ],
663
        ];
664
    }
665
666 5
    public static function randomBits($bits) {
667 5
        return self::randomBytes($bits / 8);
668
    }
669
670 5
    public static function randomBytes($bytes) {
671 5
        return (new Random())->bytes($bytes)->getBinary();
672
    }
673
674 2
    protected function createNewWalletV2($options) {
675 2
        $walletPath = WalletPath::create($options['key_index']);
676
677 2
        if (isset($options['store_primary_mnemonic'])) {
678
            $options['store_data_on_server'] = $options['store_primary_mnemonic'];
679
        }
680
681 2
        if (!isset($options['store_data_on_server'])) {
682 2
            if (isset($options['primary_private_key'])) {
683 1
                $options['store_data_on_server'] = false;
684
            } else {
685 1
                $options['store_data_on_server'] = true;
686
            }
687
        }
688
689 2
        $storeDataOnServer = $options['store_data_on_server'];
690
691 2
        $secret = null;
692 2
        $encryptedSecret = null;
693 2
        $primarySeed = null;
694 2
        $encryptedPrimarySeed = null;
695 2
        $recoverySecret = null;
696 2
        $recoveryEncryptedSecret = null;
697 2
        $backupSeed = null;
698
699 2
        if (!isset($options['primary_private_key'])) {
700 1
            $primarySeed = isset($options['primary_seed']) ? $options['primary_seed'] : self::randomBits(256);
701
        }
702
703 2
        if ($storeDataOnServer) {
704 1
            if (!isset($options['secret'])) {
705 1
                if (!$options['passphrase']) {
706
                    throw new \InvalidArgumentException("Can't encrypt data without a passphrase");
707
                }
708
709 1
                $secret = bin2hex(self::randomBits(256)); // string because we use it as passphrase
710 1
                $encryptedSecret = CryptoJSAES::encrypt($secret, $options['passphrase']);
711
            } else {
712
                $secret = $options['secret'];
713
            }
714
715 1
            $encryptedPrimarySeed = CryptoJSAES::encrypt(base64_encode($primarySeed), $secret);
716 1
            $recoverySecret = bin2hex(self::randomBits(256));
717
718 1
            $recoveryEncryptedSecret = CryptoJSAES::encrypt($secret, $recoverySecret);
719
        }
720
721 2
        if (!isset($options['backup_public_key'])) {
722 1
            $backupSeed = isset($options['backup_seed']) ? $options['backup_seed'] : self::randomBits(256);
723
        }
724
725 2
        if (isset($options['primary_private_key'])) {
726 1
            $options['primary_private_key'] = BlocktrailSDK::normalizeBIP32Key($options['primary_private_key']);
727
        } else {
728 1
            $options['primary_private_key'] = BIP32Key::create(HierarchicalKeyFactory::fromEntropy(new Buffer($primarySeed)), "m");
729
        }
730
731
        // create primary public key from the created private key
732 2
        $options['primary_public_key'] = $options['primary_private_key']->buildKey($walletPath->keyIndexPath()->publicPath());
733
734 2
        if (!isset($options['backup_public_key'])) {
735 1
            $options['backup_public_key'] = BIP32Key::create(HierarchicalKeyFactory::fromEntropy(new Buffer($backupSeed)), "m")->buildKey("M");
736
        }
737
738
        // create a checksum of our private key which we'll later use to verify we used the right password
739 2
        $checksum = $options['primary_private_key']->publicKey()->getAddress()->getAddress();
740
741
        // send the public keys and encrypted data to server
742 2
        $data = $this->storeNewWalletV2(
743 2
            $options['identifier'],
744 2
            $options['primary_public_key']->tuple(),
745 2
            $options['backup_public_key']->tuple(),
0 ignored issues
show
Documentation introduced by
$options['backup_public_key']->tuple() is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
746 2
            $storeDataOnServer ? $encryptedPrimarySeed : false,
747 2
            $storeDataOnServer ? $encryptedSecret : false,
748 2
            $storeDataOnServer ? $recoverySecret : false,
749 2
            $checksum,
750 2
            $options['key_index']
751
        );
752
753
        // received the blocktrail public keys
754
        $blocktrailPublicKeys = Util::arrayMapWithIndex(function ($keyIndex, $pubKeyTuple) {
755 2
            return [$keyIndex, BIP32Key::create(HierarchicalKeyFactory::fromExtended($pubKeyTuple[0]), $pubKeyTuple[1])];
756 2
        }, $data['blocktrail_public_keys']);
757
758 2
        $wallet = new WalletV2(
759 2
            $this,
760 2
            $options['identifier'],
761 2
            $encryptedPrimarySeed,
762 2
            $encryptedSecret,
763 2
            [$options['key_index'] => $options['primary_public_key']],
764 2
            $options['backup_public_key'],
765 2
            $blocktrailPublicKeys,
766 2
            $options['key_index'],
767 2
            $this->network,
768 2
            $this->testnet,
769 2
            array_key_exists('segwit', $data) ? $data['segwit'] : false,
770 2
            $checksum
771
        );
772
773 2
        $wallet->unlock([
774 2
            'passphrase' => isset($options['passphrase']) ? $options['passphrase'] : null,
775 2
            'primary_private_key' => $options['primary_private_key'],
776 2
            'primary_seed' => $primarySeed,
777 2
            'secret' => $secret,
778
        ]);
779
780
        // return wallet and mnemonics for backup sheet
781
        return [
782 2
            $wallet,
783
            [
784 2
                'encrypted_primary_seed' => $encryptedPrimarySeed ? MnemonicFactory::bip39()->entropyToMnemonic(new Buffer(base64_decode($encryptedPrimarySeed))) : null,
785 2
                'backup_seed' => $backupSeed ? MnemonicFactory::bip39()->entropyToMnemonic(new Buffer($backupSeed)) : null,
786 2
                'recovery_encrypted_secret' => $recoveryEncryptedSecret ? MnemonicFactory::bip39()->entropyToMnemonic(new Buffer(base64_decode($recoveryEncryptedSecret))) : null,
787 2
                'encrypted_secret' => $encryptedSecret ? MnemonicFactory::bip39()->entropyToMnemonic(new Buffer(base64_decode($encryptedSecret))) : null,
788
                'blocktrail_public_keys' => Util::arrayMapWithIndex(function ($keyIndex, BIP32Key $pubKey) {
789 2
                    return [$keyIndex, $pubKey->tuple()];
790 2
                }, $blocktrailPublicKeys),
791
            ],
792
        ];
793
    }
794
795 4
    protected function createNewWalletV3($options) {
796 4
        $walletPath = WalletPath::create($options['key_index']);
797
798 4
        if (isset($options['store_primary_mnemonic'])) {
799
            $options['store_data_on_server'] = $options['store_primary_mnemonic'];
800
        }
801
802 4
        if (!isset($options['store_data_on_server'])) {
803 4
            if (isset($options['primary_private_key'])) {
804
                $options['store_data_on_server'] = false;
805
            } else {
806 4
                $options['store_data_on_server'] = true;
807
            }
808
        }
809
810 4
        $storeDataOnServer = $options['store_data_on_server'];
811
812 4
        $secret = null;
813 4
        $encryptedSecret = null;
814 4
        $primarySeed = null;
815 4
        $encryptedPrimarySeed = null;
816 4
        $recoverySecret = null;
817 4
        $recoveryEncryptedSecret = null;
818 4
        $backupSeed = null;
819
820 4
        if (!isset($options['primary_private_key'])) {
821 4
            if (isset($options['primary_seed'])) {
822
                if (!$options['primary_seed'] instanceof BufferInterface) {
823
                    throw new \InvalidArgumentException('Primary Seed should be passed as a Buffer');
824
                }
825
                $primarySeed = $options['primary_seed'];
826
            } else {
827 4
                $primarySeed = new Buffer(self::randomBits(256));
828
            }
829
        }
830
831 4
        if ($storeDataOnServer) {
832 4
            if (!isset($options['secret'])) {
833 4
                if (!$options['passphrase']) {
834
                    throw new \InvalidArgumentException("Can't encrypt data without a passphrase");
835
                }
836
837 4
                $secret = new Buffer(self::randomBits(256));
838 4
                $encryptedSecret = Encryption::encrypt($secret, new Buffer($options['passphrase']), KeyDerivation::DEFAULT_ITERATIONS);
839
            } else {
840
                if (!$options['secret'] instanceof Buffer) {
841
                    throw new \RuntimeException('Secret must be provided as a Buffer');
842
                }
843
844
                $secret = $options['secret'];
845
            }
846
847 4
            $encryptedPrimarySeed = Encryption::encrypt($primarySeed, $secret, KeyDerivation::SUBKEY_ITERATIONS);
848 4
            $recoverySecret = new Buffer(self::randomBits(256));
849
850 4
            $recoveryEncryptedSecret = Encryption::encrypt($secret, $recoverySecret, KeyDerivation::DEFAULT_ITERATIONS);
851
        }
852
853 4
        if (!isset($options['backup_public_key'])) {
854 4
            if (isset($options['backup_seed'])) {
855
                if (!$options['backup_seed'] instanceof Buffer) {
856
                    throw new \RuntimeException('Backup seed must be an instance of Buffer');
857
                }
858
                $backupSeed = $options['backup_seed'];
859
            } else {
860 4
                $backupSeed = new Buffer(self::randomBits(256));
861
            }
862
        }
863
864 4
        if (isset($options['primary_private_key'])) {
865
            $options['primary_private_key'] = BlocktrailSDK::normalizeBIP32Key($options['primary_private_key']);
866
        } else {
867 4
            $options['primary_private_key'] = BIP32Key::create(HierarchicalKeyFactory::fromEntropy($primarySeed), "m");
868
        }
869
870
        // create primary public key from the created private key
871 4
        $options['primary_public_key'] = $options['primary_private_key']->buildKey($walletPath->keyIndexPath()->publicPath());
872
873 4
        if (!isset($options['backup_public_key'])) {
874 4
            $options['backup_public_key'] = BIP32Key::create(HierarchicalKeyFactory::fromEntropy($backupSeed), "m")->buildKey("M");
875
        }
876
877
        // create a checksum of our private key which we'll later use to verify we used the right password
878 4
        $checksum = $options['primary_private_key']->publicKey()->getAddress()->getAddress();
879
880
        // send the public keys and encrypted data to server
881 4
        $data = $this->storeNewWalletV3(
882 4
            $options['identifier'],
883 4
            $options['primary_public_key']->tuple(),
884 4
            $options['backup_public_key']->tuple(),
0 ignored issues
show
Documentation introduced by
$options['backup_public_key']->tuple() is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
885 4
            $storeDataOnServer ? base64_encode($encryptedPrimarySeed->getBinary()) : false,
886 4
            $storeDataOnServer ? base64_encode($encryptedSecret->getBinary()) : false,
887 4
            $storeDataOnServer ? $recoverySecret->getHex() : false,
888 4
            $checksum,
889 4
            $options['key_index']
890
        );
891
892
        // received the blocktrail public keys
893
        $blocktrailPublicKeys = Util::arrayMapWithIndex(function ($keyIndex, $pubKeyTuple) {
894 4
            return [$keyIndex, BIP32Key::create(HierarchicalKeyFactory::fromExtended($pubKeyTuple[0]), $pubKeyTuple[1])];
895 4
        }, $data['blocktrail_public_keys']);
896
897 4
        $wallet = new WalletV3(
898 4
            $this,
899 4
            $options['identifier'],
900 4
            $encryptedPrimarySeed,
0 ignored issues
show
Bug introduced by
It seems like $encryptedPrimarySeed defined by null on line 815 can be null; however, Blocktrail\SDK\WalletV3::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
901 4
            $encryptedSecret,
0 ignored issues
show
Bug introduced by
It seems like $encryptedSecret defined by null on line 813 can be null; however, Blocktrail\SDK\WalletV3::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
902 4
            [$options['key_index'] => $options['primary_public_key']],
903 4
            $options['backup_public_key'],
904 4
            $blocktrailPublicKeys,
905 4
            $options['key_index'],
906 4
            $this->network,
907 4
            $this->testnet,
908 4
            array_key_exists('segwit', $data) ? $data['segwit'] : false,
909 4
            $checksum
910
        );
911
912 4
        $wallet->unlock([
913 4
            'passphrase' => isset($options['passphrase']) ? $options['passphrase'] : null,
914 4
            'primary_private_key' => $options['primary_private_key'],
915 4
            'primary_seed' => $primarySeed,
916 4
            'secret' => $secret,
917
        ]);
918
919
        // return wallet and mnemonics for backup sheet
920
        return [
921 4
            $wallet,
922
            [
923 4
                'encrypted_primary_seed'    => $encryptedPrimarySeed ? EncryptionMnemonic::encode($encryptedPrimarySeed) : null,
924 4
                'backup_seed'               => $backupSeed ? MnemonicFactory::bip39()->entropyToMnemonic($backupSeed) : null,
925 4
                'recovery_encrypted_secret' => $recoveryEncryptedSecret ? EncryptionMnemonic::encode($recoveryEncryptedSecret) : null,
926 4
                'encrypted_secret'          => $encryptedSecret ? EncryptionMnemonic::encode($encryptedSecret) : null,
927
                'blocktrail_public_keys'    => Util::arrayMapWithIndex(function ($keyIndex, BIP32Key $pubKey) {
928 4
                    return [$keyIndex, $pubKey->tuple()];
929 4
                }, $blocktrailPublicKeys),
930
            ]
931
        ];
932
    }
933
934
    /**
935
     * @param array $bip32Key
936
     * @throws BlocktrailSDKException
937
     */
938 10
    private function verifyPublicBIP32Key(array $bip32Key) {
939 10
        $hk = HierarchicalKeyFactory::fromExtended($bip32Key[0]);
940 10
        if ($hk->isPrivate()) {
941
            throw new BlocktrailSDKException('Private key was included in request, abort');
942
        }
943
944 10
        if (substr($bip32Key[1], 0, 1) === "m") {
945
            throw new BlocktrailSDKException("Private path was included in the request, abort");
946
        }
947 10
    }
948
949
    /**
950
     * @param array $walletData
951
     * @throws BlocktrailSDKException
952
     */
953 10
    private function verifyPublicOnly(array $walletData) {
954 10
        $this->verifyPublicBIP32Key($walletData['primary_public_key']);
955 10
        $this->verifyPublicBIP32Key($walletData['backup_public_key']);
956 10
    }
957
958
    /**
959
     * create wallet using the API
960
     *
961
     * @param string    $identifier             the wallet identifier to create
962
     * @param array     $primaryPublicKey       BIP32 extended public key - [key, path]
963
     * @param string    $backupPublicKey        plain public key
964
     * @param string    $primaryMnemonic        mnemonic to store
965
     * @param string    $checksum               checksum to store
966
     * @param int       $keyIndex               account that we expect to use
967
     * @return mixed
968
     */
969 1
    public function storeNewWalletV1($identifier, $primaryPublicKey, $backupPublicKey, $primaryMnemonic, $checksum, $keyIndex) {
970
        $data = [
971 1
            'identifier' => $identifier,
972 1
            'primary_public_key' => $primaryPublicKey,
973 1
            'backup_public_key' => $backupPublicKey,
974 1
            'primary_mnemonic' => $primaryMnemonic,
975 1
            'checksum' => $checksum,
976 1
            'key_index' => $keyIndex
977
        ];
978 1
        $this->verifyPublicOnly($data);
979 1
        $response = $this->client->post("wallet", null, $data, RestClient::AUTH_HTTP_SIG);
980 1
        return self::jsonDecode($response->body(), true);
981
    }
982
983
    /**
984
     * create wallet using the API
985
     *
986
     * @param string $identifier       the wallet identifier to create
987
     * @param array  $primaryPublicKey BIP32 extended public key - [key, path]
988
     * @param string $backupPublicKey  plain public key
989
     * @param        $encryptedPrimarySeed
990
     * @param        $encryptedSecret
991
     * @param        $recoverySecret
992
     * @param string $checksum         checksum to store
993
     * @param int    $keyIndex         account that we expect to use
994
     * @return mixed
995
     * @throws \Exception
996
     */
997 5
    public function storeNewWalletV2($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex) {
998
        $data = [
999 5
            'identifier' => $identifier,
1000
            'wallet_version' => Wallet::WALLET_VERSION_V2,
1001 5
            'primary_public_key' => $primaryPublicKey,
1002 5
            'backup_public_key' => $backupPublicKey,
1003 5
            'encrypted_primary_seed' => $encryptedPrimarySeed,
1004 5
            'encrypted_secret' => $encryptedSecret,
1005 5
            'recovery_secret' => $recoverySecret,
1006 5
            'checksum' => $checksum,
1007 5
            'key_index' => $keyIndex
1008
        ];
1009 5
        $this->verifyPublicOnly($data);
1010 5
        $response = $this->client->post("wallet", null, $data, RestClient::AUTH_HTTP_SIG);
1011 5
        return self::jsonDecode($response->body(), true);
1012
    }
1013
1014
    /**
1015
     * create wallet using the API
1016
     *
1017
     * @param string $identifier       the wallet identifier to create
1018
     * @param array  $primaryPublicKey BIP32 extended public key - [key, path]
1019
     * @param string $backupPublicKey  plain public key
1020
     * @param        $encryptedPrimarySeed
1021
     * @param        $encryptedSecret
1022
     * @param        $recoverySecret
1023
     * @param string $checksum         checksum to store
1024
     * @param int    $keyIndex         account that we expect to use
1025
     * @return mixed
1026
     * @throws \Exception
1027
     */
1028 4
    public function storeNewWalletV3($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex) {
1029
1030
        $data = [
1031 4
            'identifier' => $identifier,
1032
            'wallet_version' => Wallet::WALLET_VERSION_V3,
1033 4
            'primary_public_key' => $primaryPublicKey,
1034 4
            'backup_public_key' => $backupPublicKey,
1035 4
            'encrypted_primary_seed' => $encryptedPrimarySeed,
1036 4
            'encrypted_secret' => $encryptedSecret,
1037 4
            'recovery_secret' => $recoverySecret,
1038 4
            'checksum' => $checksum,
1039 4
            'key_index' => $keyIndex
1040
        ];
1041
1042 4
        $this->verifyPublicOnly($data);
1043 4
        $response = $this->client->post("wallet", null, $data, RestClient::AUTH_HTTP_SIG);
1044 4
        return self::jsonDecode($response->body(), true);
1045
    }
1046
1047
    /**
1048
     * upgrade wallet to use a new account number
1049
     *  the account number specifies which blocktrail cosigning key is used
1050
     *
1051
     * @param string    $identifier             the wallet identifier to be upgraded
1052
     * @param int       $keyIndex               the new account to use
1053
     * @param array     $primaryPublicKey       BIP32 extended public key - [key, path]
1054
     * @return mixed
1055
     */
1056 5
    public function upgradeKeyIndex($identifier, $keyIndex, $primaryPublicKey) {
1057
        $data = [
1058 5
            'key_index' => $keyIndex,
1059 5
            'primary_public_key' => $primaryPublicKey
1060
        ];
1061
1062 5
        $response = $this->client->post("wallet/{$identifier}/upgrade", null, $data, RestClient::AUTH_HTTP_SIG);
1063 5
        return self::jsonDecode($response->body(), true);
1064
    }
1065
1066
    /**
1067
     * initialize a previously created wallet
1068
     *
1069
     * Takes an options object, or accepts identifier/password for backwards compatiblity.
1070
     *
1071
     * Some of the options:
1072
     *  - "readonly/readOnly/read-only" can be to a boolean value,
1073
     *    so the wallet is loaded in read-only mode (no private key)
1074
     *  - "check_backup_key" can be set to your own backup key:
1075
     *    Format: ["M', "xpub..."]
1076
     *    Setting this will allow the SDK to check the server hasn't
1077
     *    a different key (one it happens to control)
1078
1079
     * Either takes one argument:
1080
     * @param array $options
1081
     *
1082
     * Or takes two arguments (old, deprecated syntax):
1083
     * (@nonPHP-doc) @param string    $identifier             the wallet identifier to be initialized
0 ignored issues
show
Bug introduced by
There is no parameter named $identifier. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1084
     * (@nonPHP-doc) @param string    $password               the password to decrypt the mnemonic with
0 ignored issues
show
Bug introduced by
There is no parameter named $password. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1085
     *
1086
     * @return WalletInterface
1087
     * @throws \Exception
1088
     */
1089 16
    public function initWallet($options) {
1090 16
        if (!is_array($options)) {
1091 1
            $args = func_get_args();
1092
            $options = [
1093 1
                "identifier" => $args[0],
1094 1
                "password" => $args[1],
1095
            ];
1096
        }
1097
1098 16
        $identifier = $options['identifier'];
1099 16
        $readonly = isset($options['readonly']) ? $options['readonly'] :
1100 16
                    (isset($options['readOnly']) ? $options['readOnly'] :
1101 16
                        (isset($options['read-only']) ? $options['read-only'] :
1102 16
                            false));
1103
1104
        // get the wallet data from the server
1105 16
        $data = $this->getWallet($identifier);
1106 16
        if (!$data) {
1107
            throw new \Exception("Failed to get wallet");
1108
        }
1109
1110 16
        if (array_key_exists('check_backup_key', $options)) {
1111 1
            if (!is_string($options['check_backup_key'])) {
1112 1
                throw new \RuntimeException("check_backup_key should be a string (the xpub)");
1113
            }
1114 1
            if ($options['check_backup_key'] !== $data['backup_public_key'][0]) {
1115 1
                throw new \RuntimeException("Backup key returned from server didn't match our own");
1116
            }
1117
        }
1118
1119 16
        switch ($data['wallet_version']) {
1120 16
            case Wallet::WALLET_VERSION_V1:
1121 10
                $wallet = new WalletV1(
1122 10
                    $this,
1123 10
                    $identifier,
1124 10
                    isset($options['primary_mnemonic']) ? $options['primary_mnemonic'] : $data['primary_mnemonic'],
1125 10
                    $data['primary_public_keys'],
1126 10
                    $data['backup_public_key'],
1127 10
                    $data['blocktrail_public_keys'],
1128 10
                    isset($options['key_index']) ? $options['key_index'] : $data['key_index'],
1129 10
                    $this->network,
1130 10
                    $this->testnet,
1131 10
                    array_key_exists('segwit', $data) ? $data['segwit'] : false,
1132 10
                    $data['checksum']
1133
                );
1134 10
                break;
1135 6
            case Wallet::WALLET_VERSION_V2:
1136 2
                $wallet = new WalletV2(
1137 2
                    $this,
1138 2
                    $identifier,
1139 2
                    isset($options['encrypted_primary_seed']) ? $options['encrypted_primary_seed'] : $data['encrypted_primary_seed'],
1140 2
                    isset($options['encrypted_secret']) ? $options['encrypted_secret'] : $data['encrypted_secret'],
1141 2
                    $data['primary_public_keys'],
1142 2
                    $data['backup_public_key'],
1143 2
                    $data['blocktrail_public_keys'],
1144 2
                    isset($options['key_index']) ? $options['key_index'] : $data['key_index'],
1145 2
                    $this->network,
1146 2
                    $this->testnet,
1147 2
                    array_key_exists('segwit', $data) ? $data['segwit'] : false,
1148 2
                    $data['checksum']
1149
                );
1150 2
                break;
1151 4
            case Wallet::WALLET_VERSION_V3:
1152 4
                if (isset($options['encrypted_primary_seed'])) {
1153
                    if (!$options['encrypted_primary_seed'] instanceof Buffer) {
1154
                        throw new \InvalidArgumentException('Encrypted PrimarySeed must be provided as a Buffer');
1155
                    }
1156
                    $encryptedPrimarySeed = $data['encrypted_primary_seed'];
1157
                } else {
1158 4
                    $encryptedPrimarySeed = new Buffer(base64_decode($data['encrypted_primary_seed']));
1159
                }
1160
1161 4
                if (isset($options['encrypted_secret'])) {
1162
                    if (!$options['encrypted_secret'] instanceof Buffer) {
1163
                        throw new \InvalidArgumentException('Encrypted secret must be provided as a Buffer');
1164
                    }
1165
1166
                    $encryptedSecret = $data['encrypted_secret'];
1167
                } else {
1168 4
                    $encryptedSecret = new Buffer(base64_decode($data['encrypted_secret']));
1169
                }
1170
1171 4
                $wallet = new WalletV3(
1172 4
                    $this,
1173 4
                    $identifier,
1174 4
                    $encryptedPrimarySeed,
1175 4
                    $encryptedSecret,
1176 4
                    $data['primary_public_keys'],
1177 4
                    $data['backup_public_key'],
1178 4
                    $data['blocktrail_public_keys'],
1179 4
                    isset($options['key_index']) ? $options['key_index'] : $data['key_index'],
1180 4
                    $this->network,
1181 4
                    $this->testnet,
1182 4
                    array_key_exists('segwit', $data) ? $data['segwit'] : false,
1183 4
                    $data['checksum']
1184
                );
1185 4
                break;
1186
            default:
1187
                throw new \InvalidArgumentException("Invalid wallet version");
1188
        }
1189
1190 16
        if (!$readonly) {
1191 16
            $wallet->unlock($options);
1192
        }
1193
1194 16
        return $wallet;
1195
    }
1196
1197
    /**
1198
     * get the wallet data from the server
1199
     *
1200
     * @param string    $identifier             the identifier of the wallet
1201
     * @return mixed
1202
     */
1203 16
    public function getWallet($identifier) {
1204 16
        $response = $this->client->get("wallet/{$identifier}", null, RestClient::AUTH_HTTP_SIG);
1205 16
        return self::jsonDecode($response->body(), true);
1206
    }
1207
1208
    /**
1209
     * update the wallet data on the server
1210
     *
1211
     * @param string    $identifier
1212
     * @param $data
1213
     * @return mixed
1214
     */
1215 3
    public function updateWallet($identifier, $data) {
1216 3
        $response = $this->client->post("wallet/{$identifier}", null, $data, RestClient::AUTH_HTTP_SIG);
1217 3
        return self::jsonDecode($response->body(), true);
1218
    }
1219
1220
    /**
1221
     * delete a wallet from the server
1222
     *  the checksum address and a signature to verify you ownership of the key of that checksum address
1223
     *  is required to be able to delete a wallet
1224
     *
1225
     * @param string    $identifier             the identifier of the wallet
1226
     * @param string    $checksumAddress        the address for your master private key (and the checksum used when creating the wallet)
1227
     * @param string    $signature              a signature of the checksum address as message signed by the private key matching that address
1228
     * @param bool      $force                  ignore warnings (such as a non-zero balance)
1229
     * @return mixed
1230
     */
1231 10
    public function deleteWallet($identifier, $checksumAddress, $signature, $force = false) {
1232 10
        $response = $this->client->delete("wallet/{$identifier}", ['force' => $force], [
1233 10
            'checksum' => $checksumAddress,
1234 10
            'signature' => $signature
1235 10
        ], RestClient::AUTH_HTTP_SIG, 360);
1236 10
        return self::jsonDecode($response->body(), true);
1237
    }
1238
1239
    /**
1240
     * create new backup key;
1241
     *  1) a BIP39 mnemonic
1242
     *  2) a seed from that mnemonic with a blank password
1243
     *  3) a private key from that seed
1244
     *
1245
     * @return array [mnemonic, seed, key]
1246
     */
1247 1
    protected function newBackupSeed() {
1248 1
        list($backupMnemonic, $backupSeed, $backupPrivateKey) = $this->generateNewSeed("");
1249
1250 1
        return [$backupMnemonic, $backupSeed, $backupPrivateKey];
1251
    }
1252
1253
    /**
1254
     * create new primary key;
1255
     *  1) a BIP39 mnemonic
1256
     *  2) a seed from that mnemonic with the password
1257
     *  3) a private key from that seed
1258
     *
1259
     * @param string    $passphrase             the password to use in the BIP39 creation of the seed
1260
     * @return array [mnemonic, seed, key]
1261
     * @TODO: require a strong password?
1262
     */
1263 1
    protected function newPrimarySeed($passphrase) {
1264 1
        list($primaryMnemonic, $primarySeed, $primaryPrivateKey) = $this->generateNewSeed($passphrase);
1265
1266 1
        return [$primaryMnemonic, $primarySeed, $primaryPrivateKey];
1267
    }
1268
1269
    /**
1270
     * create a new key;
1271
     *  1) a BIP39 mnemonic
1272
     *  2) a seed from that mnemonic with the password
1273
     *  3) a private key from that seed
1274
     *
1275
     * @param string    $passphrase             the password to use in the BIP39 creation of the seed
1276
     * @param string    $forceEntropy           forced entropy instead of random entropy for testing purposes
1277
     * @return array
1278
     */
1279 1
    protected function generateNewSeed($passphrase = "", $forceEntropy = null) {
1280
        // generate master seed, retry if the generated private key isn't valid (FALSE is returned)
1281
        do {
1282 1
            $mnemonic = $this->generateNewMnemonic($forceEntropy);
1283
1284 1
            $seed = (new Bip39SeedGenerator)->getSeed($mnemonic, $passphrase);
1285
1286 1
            $key = null;
1287
            try {
1288 1
                $key = HierarchicalKeyFactory::fromEntropy($seed);
1289
            } catch (\Exception $e) {
1290
                // try again
1291
            }
1292 1
        } while (!$key);
1293
1294 1
        return [$mnemonic, $seed, $key];
1295
    }
1296
1297
    /**
1298
     * generate a new mnemonic from some random entropy (512 bit)
1299
     *
1300
     * @param string    $forceEntropy           forced entropy instead of random entropy for testing purposes
1301
     * @return string
1302
     * @throws \Exception
1303
     */
1304 1
    protected function generateNewMnemonic($forceEntropy = null) {
1305 1
        if ($forceEntropy === null) {
1306 1
            $random = new Random();
1307 1
            $entropy = $random->bytes(512 / 8);
1308
        } else {
1309
            $entropy = $forceEntropy;
1310
        }
1311
1312 1
        return MnemonicFactory::bip39()->entropyToMnemonic($entropy);
0 ignored issues
show
Bug introduced by
It seems like $entropy defined by $forceEntropy on line 1309 can also be of type string; however, BitWasp\Bitcoin\Mnemonic...ic::entropyToMnemonic() does only seem to accept object<BitWasp\Buffertools\BufferInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
1313
    }
1314
1315
    /**
1316
     * get the balance for the wallet
1317
     *
1318
     * @param string    $identifier             the identifier of the wallet
1319
     * @return array
1320
     */
1321 9
    public function getWalletBalance($identifier) {
1322 9
        $response = $this->client->get("wallet/{$identifier}/balance", null, RestClient::AUTH_HTTP_SIG);
1323 9
        return self::jsonDecode($response->body(), true);
1324
    }
1325
1326
    /**
1327
     * do HD wallet discovery for the wallet
1328
     *
1329
     * this can be REALLY slow, so we've set the timeout to 120s ...
1330
     *
1331
     * @param string    $identifier             the identifier of the wallet
1332
     * @param int       $gap                    the gap setting to use for discovery
1333
     * @return mixed
1334
     */
1335 2
    public function doWalletDiscovery($identifier, $gap = 200) {
1336 2
        $response = $this->client->get("wallet/{$identifier}/discovery", ['gap' => $gap], RestClient::AUTH_HTTP_SIG, 360.0);
1337 2
        return self::jsonDecode($response->body(), true);
1338
    }
1339
1340
    /**
1341
     * get a new derivation number for specified parent path
1342
     *  eg; m/44'/1'/0/0 results in m/44'/1'/0/0/0 and next time in m/44'/1'/0/0/1 and next time in m/44'/1'/0/0/2
1343
     *
1344
     * returns the path
1345
     *
1346
     * @param string    $identifier             the identifier of the wallet
1347
     * @param string    $path                   the parent path for which to get a new derivation
1348
     * @return string
1349
     */
1350 1
    public function getNewDerivation($identifier, $path) {
1351 1
        $result = $this->_getNewDerivation($identifier, $path);
1352 1
        return $result['path'];
1353
    }
1354
1355
    /**
1356
     * get a new derivation number for specified parent path
1357
     *  eg; m/44'/1'/0/0 results in m/44'/1'/0/0/0 and next time in m/44'/1'/0/0/1 and next time in m/44'/1'/0/0/2
1358
     *
1359
     * @param string    $identifier             the identifier of the wallet
1360
     * @param string    $path                   the parent path for which to get a new derivation
1361
     * @return mixed
1362
     */
1363 13
    public function _getNewDerivation($identifier, $path) {
1364 13
        $response = $this->client->post("wallet/{$identifier}/path", null, ['path' => $path], RestClient::AUTH_HTTP_SIG);
1365 13
        return self::jsonDecode($response->body(), true);
1366
    }
1367
1368
    /**
1369
     * get the path (and redeemScript) to specified address
1370
     *
1371
     * @param string $identifier
1372
     * @param string $address
1373
     * @return array
1374
     * @throws \Exception
1375
     */
1376
    public function getPathForAddress($identifier, $address) {
1377
        $response = $this->client->post("wallet/{$identifier}/path_for_address", null, ['address' => $address], RestClient::AUTH_HTTP_SIG);
1378
        return self::jsonDecode($response->body(), true)['path'];
1379
    }
1380
1381
    /**
1382
     * send the transaction using the API
1383
     *
1384
     * @param string         $identifier             the identifier of the wallet
1385
     * @param string|array   $rawTransaction         raw hex of the transaction (should be partially signed)
1386
     * @param array          $paths                  list of the paths that were used for the UTXO
1387
     * @param bool           $checkFee               let the server verify the fee after signing
1388
     * @return string                                the complete raw transaction
1389
     * @throws \Exception
1390
     */
1391 4
    public function sendTransaction($identifier, $rawTransaction, $paths, $checkFee = false) {
1392
        $data = [
1393 4
            'paths' => $paths
1394
        ];
1395
1396 4
        if (is_array($rawTransaction)) {
1397 4
            if (array_key_exists('base_transaction', $rawTransaction)
1398 4
            && array_key_exists('signed_transaction', $rawTransaction)) {
1399 4
                $data['base_transaction'] = $rawTransaction['base_transaction'];
1400 4
                $data['signed_transaction'] = $rawTransaction['signed_transaction'];
1401
            } else {
1402 4
                throw new \RuntimeException("Invalid value for transaction. For segwit transactions, pass ['base_transaction' => '...', 'signed_transaction' => '...']");
1403
            }
1404
        } else {
1405
            $data['raw_transaction'] = $rawTransaction;
1406
        }
1407
1408
        // dynamic TTL for when we're signing really big transactions
1409 4
        $ttl = max(5.0, count($paths) * 0.25) + 4.0;
1410
1411 4
        $response = $this->client->post("wallet/{$identifier}/send", ['check_fee' => (int)!!$checkFee], $data, RestClient::AUTH_HTTP_SIG, $ttl);
1412 4
        $signed = self::jsonDecode($response->body(), true);
1413
1414 4
        if (!$signed['complete'] || $signed['complete'] == 'false') {
1415
            throw new \Exception("Failed to completely sign transaction");
1416
        }
1417
1418
        // create TX hash from the raw signed hex
1419 4
        return TransactionFactory::fromHex($signed['hex'])->getTxId()->getHex();
1420
    }
1421
1422
    /**
1423
     * use the API to get the best inputs to use based on the outputs
1424
     *
1425
     * the return array has the following format:
1426
     * [
1427
     *  "utxos" => [
1428
     *      [
1429
     *          "hash" => "<txHash>",
1430
     *          "idx" => "<index of the output of that <txHash>",
1431
     *          "scriptpubkey_hex" => "<scriptPubKey-hex>",
1432
     *          "value" => 32746327,
1433
     *          "address" => "1address",
1434
     *          "path" => "m/44'/1'/0'/0/13",
1435
     *          "redeem_script" => "<redeemScript-hex>",
1436
     *      ],
1437
     *  ],
1438
     *  "fee"   => 10000,
1439
     *  "change"=> 1010109201,
1440
     * ]
1441
     *
1442
     * @param string   $identifier              the identifier of the wallet
1443
     * @param array    $outputs                 the outputs you want to create - array[address => satoshi-value]
1444
     * @param bool     $lockUTXO                when TRUE the UTXOs selected will be locked for a few seconds
1445
     *                                          so you have some time to spend them without race-conditions
1446
     * @param bool     $allowZeroConf
1447
     * @param string   $feeStrategy
1448
     * @param null|int $forceFee
1449
     * @return array
1450
     * @throws \Exception
1451
     */
1452 11
    public function coinSelection($identifier, $outputs, $lockUTXO = false, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null) {
1453
        $args = [
1454 11
            'lock' => (int)!!$lockUTXO,
1455 11
            'zeroconf' => (int)!!$allowZeroConf,
1456 11
            'fee_strategy' => $feeStrategy,
1457
        ];
1458
1459 11
        if ($forceFee !== null) {
1460 1
            $args['forcefee'] = (int)$forceFee;
1461
        }
1462
1463 11
        $response = $this->client->post(
1464 11
            "wallet/{$identifier}/coin-selection",
1465 11
            $args,
1466 11
            $outputs,
1467 11
            RestClient::AUTH_HTTP_SIG
1468
        );
1469
1470 5
        return self::jsonDecode($response->body(), true);
1471
    }
1472
1473
    /**
1474
     *
1475
     * @param string   $identifier the identifier of the wallet
1476
     * @param bool     $allowZeroConf
1477
     * @param string   $feeStrategy
1478
     * @param null|int $forceFee
1479
     * @param int      $outputCnt
1480
     * @return array
1481
     * @throws \Exception
1482
     */
1483
    public function walletMaxSpendable($identifier, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null, $outputCnt = 1) {
1484
        $args = [
1485
            'zeroconf' => (int)!!$allowZeroConf,
1486
            'fee_strategy' => $feeStrategy,
1487
            'outputs' => $outputCnt,
1488
        ];
1489
1490
        if ($forceFee !== null) {
1491
            $args['forcefee'] = (int)$forceFee;
1492
        }
1493
1494
        $response = $this->client->get(
1495
            "wallet/{$identifier}/max-spendable",
1496
            $args,
1497
            RestClient::AUTH_HTTP_SIG
1498
        );
1499
1500
        return self::jsonDecode($response->body(), true);
1501
    }
1502
1503
    /**
1504
     * @return array        ['optimal_fee' => 10000, 'low_priority_fee' => 5000]
1505
     */
1506 3
    public function feePerKB() {
1507 3
        $response = $this->client->get("fee-per-kb");
1508 3
        return self::jsonDecode($response->body(), true);
1509
    }
1510
1511
    /**
1512
     * get the current price index
1513
     *
1514
     * @return array        eg; ['USD' => 287.30]
1515
     */
1516 1
    public function price() {
1517 1
        $response = $this->client->get("price");
1518 1
        return self::jsonDecode($response->body(), true);
1519
    }
1520
1521
    /**
1522
     * setup webhook for wallet
1523
     *
1524
     * @param string    $identifier         the wallet identifier for which to create the webhook
1525
     * @param string    $webhookIdentifier  the webhook identifier to use
1526
     * @param string    $url                the url to receive the webhook events
1527
     * @return array
1528
     */
1529 1
    public function setupWalletWebhook($identifier, $webhookIdentifier, $url) {
1530 1
        $response = $this->client->post("wallet/{$identifier}/webhook", null, ['url' => $url, 'identifier' => $webhookIdentifier], RestClient::AUTH_HTTP_SIG);
1531 1
        return self::jsonDecode($response->body(), true);
1532
    }
1533
1534
    /**
1535
     * delete webhook for wallet
1536
     *
1537
     * @param string    $identifier         the wallet identifier for which to delete the webhook
1538
     * @param string    $webhookIdentifier  the webhook identifier to delete
1539
     * @return array
1540
     */
1541 1
    public function deleteWalletWebhook($identifier, $webhookIdentifier) {
1542 1
        $response = $this->client->delete("wallet/{$identifier}/webhook/{$webhookIdentifier}", null, null, RestClient::AUTH_HTTP_SIG);
1543 1
        return self::jsonDecode($response->body(), true);
1544
    }
1545
1546
    /**
1547
     * lock a specific unspent output
1548
     *
1549
     * @param     $identifier
1550
     * @param     $txHash
1551
     * @param     $txIdx
1552
     * @param int $ttl
1553
     * @return bool
1554
     */
1555
    public function lockWalletUTXO($identifier, $txHash, $txIdx, $ttl = 3) {
1556
        $response = $this->client->post("wallet/{$identifier}/lock-utxo", null, ['hash' => $txHash, 'idx' => $txIdx, 'ttl' => $ttl], RestClient::AUTH_HTTP_SIG);
1557
        return self::jsonDecode($response->body(), true)['locked'];
1558
    }
1559
1560
    /**
1561
     * unlock a specific unspent output
1562
     *
1563
     * @param     $identifier
1564
     * @param     $txHash
1565
     * @param     $txIdx
1566
     * @return bool
1567
     */
1568
    public function unlockWalletUTXO($identifier, $txHash, $txIdx) {
1569
        $response = $this->client->post("wallet/{$identifier}/unlock-utxo", null, ['hash' => $txHash, 'idx' => $txIdx], RestClient::AUTH_HTTP_SIG);
1570
        return self::jsonDecode($response->body(), true)['unlocked'];
1571
    }
1572
1573
    /**
1574
     * get all transactions for wallet (paginated)
1575
     *
1576
     * @param  string  $identifier  the wallet identifier for which to get transactions
1577
     * @param  integer $page        pagination: page number
1578
     * @param  integer $limit       pagination: records per page (max 500)
1579
     * @param  string  $sortDir     pagination: sort direction (asc|desc)
1580
     * @return array                associative array containing the response
1581
     */
1582 1
    public function walletTransactions($identifier, $page = 1, $limit = 20, $sortDir = 'asc') {
1583
        $queryString = [
1584 1
            'page' => $page,
1585 1
            'limit' => $limit,
1586 1
            'sort_dir' => $sortDir
1587
        ];
1588 1
        $response = $this->client->get("wallet/{$identifier}/transactions", $queryString, RestClient::AUTH_HTTP_SIG);
1589 1
        return self::jsonDecode($response->body(), true);
1590
    }
1591
1592
    /**
1593
     * get all addresses for wallet (paginated)
1594
     *
1595
     * @param  string  $identifier  the wallet identifier for which to get addresses
1596
     * @param  integer $page        pagination: page number
1597
     * @param  integer $limit       pagination: records per page (max 500)
1598
     * @param  string  $sortDir     pagination: sort direction (asc|desc)
1599
     * @return array                associative array containing the response
1600
     */
1601 1
    public function walletAddresses($identifier, $page = 1, $limit = 20, $sortDir = 'asc') {
1602
        $queryString = [
1603 1
            'page' => $page,
1604 1
            'limit' => $limit,
1605 1
            'sort_dir' => $sortDir
1606
        ];
1607 1
        $response = $this->client->get("wallet/{$identifier}/addresses", $queryString, RestClient::AUTH_HTTP_SIG);
1608 1
        return self::jsonDecode($response->body(), true);
1609
    }
1610
1611
    /**
1612
     * get all UTXOs for wallet (paginated)
1613
     *
1614
     * @param  string  $identifier  the wallet identifier for which to get addresses
1615
     * @param  integer $page        pagination: page number
1616
     * @param  integer $limit       pagination: records per page (max 500)
1617
     * @param  string  $sortDir     pagination: sort direction (asc|desc)
1618
     * @param  boolean $zeroconf    include zero confirmation transactions
1619
     * @return array                associative array containing the response
1620
     */
1621 1
    public function walletUTXOs($identifier, $page = 1, $limit = 20, $sortDir = 'asc', $zeroconf = true) {
1622
        $queryString = [
1623 1
            'page' => $page,
1624 1
            'limit' => $limit,
1625 1
            'sort_dir' => $sortDir,
1626 1
            'zeroconf' => (int)!!$zeroconf,
1627
        ];
1628 1
        $response = $this->client->get("wallet/{$identifier}/utxos", $queryString, RestClient::AUTH_HTTP_SIG);
1629 1
        return self::jsonDecode($response->body(), true);
1630
    }
1631
1632
    /**
1633
     * get a paginated list of all wallets associated with the api user
1634
     *
1635
     * @param  integer          $page    pagination: page number
1636
     * @param  integer          $limit   pagination: records per page
1637
     * @return array                     associative array containing the response
1638
     */
1639 2
    public function allWallets($page = 1, $limit = 20) {
1640
        $queryString = [
1641 2
            'page' => $page,
1642 2
            'limit' => $limit
1643
        ];
1644 2
        $response = $this->client->get("wallets", $queryString, RestClient::AUTH_HTTP_SIG);
1645 2
        return self::jsonDecode($response->body(), true);
1646
    }
1647
1648
    /**
1649
     * send raw transaction
1650
     *
1651
     * @param     $txHex
1652
     * @return bool
1653
     */
1654
    public function sendRawTransaction($txHex) {
1655
        $response = $this->client->post("send-raw-tx", null, ['hex' => $txHex], RestClient::AUTH_HTTP_SIG);
1656
        return self::jsonDecode($response->body(), true);
1657
    }
1658
1659
    /**
1660
     * testnet only ;-)
1661
     *
1662
     * @param     $address
1663
     * @param int $amount       defaults to 0.0001 BTC, max 0.001 BTC
1664
     * @return mixed
1665
     * @throws \Exception
1666
     */
1667
    public function faucetWithdrawal($address, $amount = 10000) {
1668
        $response = $this->client->post("faucet/withdrawl", null, [
1669
            'address' => $address,
1670
            'amount' => $amount,
1671
        ], RestClient::AUTH_HTTP_SIG);
1672
        return self::jsonDecode($response->body(), true);
1673
    }
1674
1675
    /**
1676
     * Exists for BC. Remove at major bump.
1677
     *
1678
     * @see faucetWithdrawal
1679
     * @deprecated
1680
     * @param     $address
1681
     * @param int $amount       defaults to 0.0001 BTC, max 0.001 BTC
1682
     * @return mixed
1683
     * @throws \Exception
1684
     */
1685
    public function faucetWithdrawl($address, $amount = 10000) {
1686
        return $this->faucetWithdrawal($address, $amount);
1687
    }
1688
1689
    /**
1690
     * verify a message signed bitcoin-core style
1691
     *
1692
     * @param  string           $message
1693
     * @param  string           $address
1694
     * @param  string           $signature
1695
     * @return boolean
1696
     */
1697 1
    public function verifyMessage($message, $address, $signature) {
1698
        // we could also use the API instead of the using BitcoinLib to verify
1699
        // $this->client->post("verify_message", null, ['message' => $message, 'address' => $address, 'signature' => $signature])['result'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1700
1701 1
        $adapter = Bitcoin::getEcAdapter();
1702 1
        $addr = AddressFactory::fromString($address);
1703 1
        if (!$addr instanceof PayToPubKeyHashAddress) {
1704
            throw new \RuntimeException('Can only verify a message with a pay-to-pubkey-hash address');
1705
        }
1706
1707
        /** @var CompactSignatureSerializerInterface $csSerializer */
1708 1
        $csSerializer = EcSerializer::getSerializer(CompactSignatureSerializerInterface::class, $adapter);
0 ignored issues
show
Documentation introduced by
$adapter is of type object<BitWasp\Bitcoin\C...ter\EcAdapterInterface>, but the function expects a boolean|object<BitWasp\B...\Crypto\EcAdapter\true>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1709 1
        $signedMessage = new SignedMessage($message, $csSerializer->parse(new Buffer(base64_decode($signature))));
1710
1711 1
        $signer = new MessageSigner($adapter);
1712 1
        return $signer->verify($signedMessage, $addr);
1713
    }
1714
1715
    /**
1716
     * convert a Satoshi value to a BTC value
1717
     *
1718
     * @param int       $satoshi
1719
     * @return float
1720
     */
1721
    public static function toBTC($satoshi) {
1722
        return bcdiv((int)(string)$satoshi, 100000000, 8);
1723
    }
1724
1725
    /**
1726
     * convert a Satoshi value to a BTC value and return it as a string
1727
1728
     * @param int       $satoshi
1729
     * @return string
1730
     */
1731
    public static function toBTCString($satoshi) {
1732
        return sprintf("%.8f", self::toBTC($satoshi));
1733
    }
1734
1735
    /**
1736
     * convert a BTC value to a Satoshi value
1737
     *
1738
     * @param float     $btc
1739
     * @return string
1740
     */
1741 12
    public static function toSatoshiString($btc) {
1742 12
        return bcmul(sprintf("%.8f", (float)$btc), 100000000, 0);
1743
    }
1744
1745
    /**
1746
     * convert a BTC value to a Satoshi value
1747
     *
1748
     * @param float     $btc
1749
     * @return string
1750
     */
1751 12
    public static function toSatoshi($btc) {
1752 12
        return (int)self::toSatoshiString($btc);
1753
    }
1754
1755
    /**
1756
     * json_decode helper that throws exceptions when it fails to decode
1757
     *
1758
     * @param      $json
1759
     * @param bool $assoc
1760
     * @return mixed
1761
     * @throws \Exception
1762
     */
1763 25
    protected static function jsonDecode($json, $assoc = false) {
1764 25
        if (!$json) {
1765
            throw new \Exception("Can't json_decode empty string [{$json}]");
1766
        }
1767
1768 25
        $data = json_decode($json, $assoc);
1769
1770 25
        if ($data === null) {
1771
            throw new \Exception("Failed to json_decode [{$json}]");
1772
        }
1773
1774 25
        return $data;
1775
    }
1776
1777
    /**
1778
     * sort public keys for multisig script
1779
     *
1780
     * @param PublicKeyInterface[] $pubKeys
1781
     * @return PublicKeyInterface[]
1782
     */
1783 14
    public static function sortMultisigKeys(array $pubKeys) {
1784 14
        $result = array_values($pubKeys);
1785
        usort($result, function (PublicKeyInterface $a, PublicKeyInterface $b) {
1786 14
            $av = $a->getHex();
1787 14
            $bv = $b->getHex();
1788 14
            return $av == $bv ? 0 : $av > $bv ? 1 : -1;
1789 14
        });
1790
1791 14
        return $result;
1792
    }
1793
1794
    /**
1795
     * read and decode the json payload from a webhook's POST request.
1796
     *
1797
     * @param bool $returnObject    flag to indicate if an object or associative array should be returned
1798
     * @return mixed|null
1799
     * @throws \Exception
1800
     */
1801
    public static function getWebhookPayload($returnObject = false) {
1802
        $data = file_get_contents("php://input");
1803
        if ($data) {
1804
            return self::jsonDecode($data, !$returnObject);
1805
        } else {
1806
            return null;
1807
        }
1808
    }
1809
1810
    public static function normalizeBIP32KeyArray($keys) {
1811 19
        return Util::arrayMapWithIndex(function ($idx, $key) {
1812 19
            return [$idx, self::normalizeBIP32Key($key)];
1813 19
        }, $keys);
1814
    }
1815
1816 19
    public static function normalizeBIP32Key($key) {
1817 19
        if ($key instanceof BIP32Key) {
1818 10
            return $key;
1819
        }
1820
1821 19
        if (is_array($key)) {
1822 19
            $path = $key[1];
1823 19
            $key = $key[0];
1824
1825 19
            if (!($key instanceof HierarchicalKey)) {
1826 19
                $key = HierarchicalKeyFactory::fromExtended($key);
1827
            }
1828
1829 19
            return BIP32Key::create($key, $path);
1830
        } else {
1831
            throw new \Exception("Bad Input");
1832
        }
1833
    }
1834
}
1835