Passed
Push — main ( cd1286...7e00d3 )
by Pouya
02:15
created

Wallet::ActiveAddresses()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
4
namespace Appino\Blockchain\Classes;
5
6
7
use Appino\Blockchain\Blockchain;
8
use Appino\Blockchain\Classes\Conversion\Conversion;
9
use Appino\Blockchain\Objects\AccountResponse;
10
use Appino\Blockchain\Objects\PaymentResponse;
11
use Blockchain\Exception\CredentialsError;
0 ignored issues
show
Bug introduced by
The type Blockchain\Exception\CredentialsError was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Blockchain\Exception\ParameterError;
0 ignored issues
show
Bug introduced by
The type Blockchain\Exception\ParameterError was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class Wallet{
15
16
    protected $blockchain;
17
18
    /**
19
     * @var string|null
20
     */
21
    private $identifier = null;
22
    /**
23
     * @var string|null
24
     */
25
    private $password = null;
26
27
    /**
28
     * Wallet constructor.
29
     * @param Blockchain $blockchain
30
     */
31
    public function __construct(Blockchain $blockchain){
32
        $this->blockchain = $blockchain;
33
    }
34
35
    /**
36
     * Generate Url
37
     *
38
     * @param $resource
39
     * @return string
40
     */
41
    private function URL($resource){
42
        return 'merchant/'.$this->identifier.'/'.$resource;
43
    }
44
45
    /**
46
     * Gets Access Credentials
47
     *
48
     * @param string $guid
49
     * @param string $password
50
     */
51
    public function credentials($guid, $password){
52
        $this->identifier = $guid;
53
        $this->password = $password;
54
    }
55
56
    /**
57
     * Check Access Credentials
58
     *
59
     * @throws CredentialsError
60
     */
61
    private function _checkCredentials() {
62
        if(is_null($this->identifier) || is_null($this->password)) {
63
            throw new CredentialsError('Please enter wallet credentials.');
64
        }
65
    }
66
67
    /**
68
     * Merge Params for Request
69
     *
70
     * @param array $extras
71
     * @return array
72
     */
73
    private function reqParams($extras=array()) {
74
        $ret = array('password'=>$this->password);
75
        return array_merge($ret, $extras);
76
    }
77
78
    /**
79
     * @param string $resource
80
     * @param array $params
81
     * @return array
82
     * @throws CredentialsError
83
     */
84
85
    private function call($resource, $params=array()) {
86
        $this->_checkCredentials();
87
        return $this->blockchain->Request('post', $this->URL($resource), $this->reqParams($params));
88
    }
89
90
    /**
91
     * Get Account Balance
92
     *
93
     * @param $password string Main Wallet Password
94
     * @return int in satoshi
95
     */
96
97
    public function Balance(){
98
        $response = $this->call('balance');
99
        return $response['balance'];
100
    }
101
102
    /**
103
     * Get Specific Address Balance
104
     *
105
     * @param $address string Can be Index of Address or Xpub
106
     * @return int in satoshi
107
     */
108
109
    public function AddressBallance($param){
110
        $response = $this->call('accounts/'.$param.'/balance');
111
        return $response['balance'];
112
    }
113
114
    /**
115
     * Get Active Wallets
116
     * @return array<AccountResponse>
117
     */
118
119
    public function ActiveAddresses(){
120
        $addresses = $this->call('accounts');
121
        $response = array();
122
        foreach ($addresses as $address){
123
            $response[] = new AccountResponse($address);
124
        }
125
        return $response;
126
    }
127
128
    /**
129
     * Get Xpub List
130
     *
131
     * @return array<string> xpub address
132
     */
133
134
    public function XpubList(){
135
        $response = $this->call('accounts/xpubs');
136
        return $response;
137
    }
138
139
    /**
140
     * Get Single Wallet Data
141
     *
142
     * @param $param string Can be Index of Account or Xpub Address
143
     * @return AccountResponse
144
     */
145
146
    public function SingleAddress($param){
147
        $response = $this->call('accounts/'.$param);
148
        return new AccountResponse($response);
149
    }
150
151
    /**
152
     * Get Receiving Address
153
     *
154
     * @param $param string Can be Index of Account or Xpub Address
155
     * @return string
156
     */
157
158
    public function ReceivingAddress($param){
159
        $response = $this->call('accounts/'.$param.'/receiveAddress');
160
        return $response['address'];
161
    }
162
163
    /**
164
     * Archive Wallet
165
     *
166
     * @param $param string Can be Index of Account or Xpub Address
167
     * @return AccountResponse
168
     */
169
170
    public function ArchiveAddress($param){
171
        $response = $this->call('accounts/'.$param.'/archive');
172
        return new AccountResponse($response);
173
    }
174
175
    /**
176
     * UnArchive Wallet
177
     *
178
     * @param $param string Can be Index of Account or Xpub Address
179
     * @return AccountResponse
180
     */
181
182
    public function UnArchiveAddress($param){
183
        $response = $this->call('accounts/'.$param.'/unarchive');
184
        return new AccountResponse($response);
185
    }
186
187
    /**
188
     * Send Bitcoin to Address
189
     *
190
     * @param string $to bitcoin address that you want to send payment to
191
     * @param integer $amount amount of payment you want to send in satoshi
192
     * @param integer|string|null $from xpub address or index of account that you want to send payment from
193
     * @param null $fee
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fee is correct as it would always require null to be passed?
Loading history...
194
     * @return PaymentResponse
195
     * @throws ParameterError
196
     */
197
198
    public function SendPayment($to, $amount, $from=null, $fee=null, $fee_per_byte=null){
199
        if(!isset($amount))
200
            throw new ParameterError("Amount required.");
201
202
        $params = array(
203
            'to'=>$to,
204
            'amount'=>$amount
205
        );
206
        if(!is_null($from))
207
            $params['from'] = $from;
208
        if(!is_null($fee))
0 ignored issues
show
introduced by
The condition is_null($fee) is always true.
Loading history...
209
            $params['fee'] = $fee;
210
        if(!is_null($fee_per_byte))
211
            $params['fee_per_byte'] = $fee_per_byte;
212
        $response = $this->call('payment',$params);
213
        return new PaymentResponse($response);
214
    }
215
216
    /**
217
     * Send Bitcoin to multiple Addresses
218
     *
219
     * @param array<string,integer> $recipients recipients must be an array of address as key and satoshi as integer
220
     * @param integer|string|null $from xpub address or index of account that you want to send payment from
221
     * @param integer|null $fee must be in satoshi (better to set null or use fee_per_byte)
222
     * @param integer|null $fee_per_byte must be in satoshi
223
     */
224
225
    public function SendManyPayment($recipients, $from=null, $fee=null, $fee_per_byte = null){
226
        $params = array(
227
            'recipients'=>json_encode($recipients)
228
        );
229
        if(!is_null($from))
230
            $params['from'] = $from;
231
        if(!is_null($fee))
232
            $params['fee'] = $fee;
233
        if(!is_null($fee_per_byte))
234
            $params['fee_per_byte'] = $fee_per_byte;
235
        $response = $this->call('sendmany',$params);
236
        return new PaymentResponse($response);
237
    }
238
239
}
240