Passed
Push — main ( 4f667e...0f6bfa )
by Pouya
02:05
created

Wallet::CreateAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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
     * Create new Address
92
     * @param null|string $label
93
     * @return AccountResponse
94
     */
95
96
    public function CreateAddress($label = null){
0 ignored issues
show
Unused Code introduced by
The parameter $label is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

96
    public function CreateAddress(/** @scrutinizer ignore-unused */ $label = null){

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
97
        $response = $this->call('accounts/create');
98
        return new AccountResponse($response);
99
    }
100
101
    /**
102
     * Get Account Balance
103
     *
104
     * @param $password string Main Wallet Password
105
     * @return int in satoshi
106
     */
107
108
    public function Balance(){
109
        $response = $this->call('balance');
110
        return $response['balance'];
111
    }
112
113
    /**
114
     * Get Specific Address Balance
115
     *
116
     * @param $address string Can be Index of Address or Xpub
117
     * @return int in satoshi
118
     */
119
120
    public function AddressBallance($param){
121
        $response = $this->call('accounts/'.$param.'/balance');
122
        return $response['balance'];
123
    }
124
125
    /**
126
     * Get Active Wallets
127
     * @return array<AccountResponse>
128
     */
129
130
    public function ActiveAddresses(){
131
        $addresses = $this->call('accounts');
132
        $response = array();
133
        foreach ($addresses as $address){
134
            $response[] = new AccountResponse($address);
135
        }
136
        return $response;
137
    }
138
139
    /**
140
     * Get Xpub List
141
     *
142
     * @return array<string> xpub address
143
     */
144
145
    public function XpubList(){
146
        $response = $this->call('accounts/xpubs');
147
        return $response;
148
    }
149
150
    /**
151
     * Get Single Wallet Data
152
     *
153
     * @param $param string Can be Index of Account or Xpub Address
154
     * @return AccountResponse
155
     */
156
157
    public function SingleAddress($param){
158
        $response = $this->call('accounts/'.$param);
159
        return new AccountResponse($response);
160
    }
161
162
    /**
163
     * Get Receiving Address
164
     *
165
     * @param $param string Can be Index of Account or Xpub Address
166
     * @return string
167
     */
168
169
    public function ReceivingAddress($param){
170
        $response = $this->call('accounts/'.$param.'/receiveAddress');
171
        return $response['address'];
172
    }
173
174
    /**
175
     * Archive Wallet
176
     *
177
     * @param $param string Can be Index of Account or Xpub Address
178
     * @return AccountResponse
179
     */
180
181
    public function ArchiveAddress($param){
182
        $response = $this->call('accounts/'.$param.'/archive');
183
        return new AccountResponse($response);
184
    }
185
186
    /**
187
     * UnArchive Wallet
188
     *
189
     * @param $param string Can be Index of Account or Xpub Address
190
     * @return AccountResponse
191
     */
192
193
    public function UnArchiveAddress($param){
194
        $response = $this->call('accounts/'.$param.'/unarchive');
195
        return new AccountResponse($response);
196
    }
197
198
    /**
199
     * Send Bitcoin to Address
200
     *
201
     * @param string $to bitcoin address that you want to send payment to
202
     * @param integer $amount amount of payment you want to send in satoshi
203
     * @param integer|string|null $from xpub address or index of account that you want to send payment from
204
     * @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...
205
     * @return PaymentResponse
206
     * @throws ParameterError
207
     */
208
209
    public function SendPayment($to, $amount, $from=null, $fee=null, $fee_per_byte=null){
210
        if(!isset($amount))
211
            throw new ParameterError("Amount required.");
212
213
        $params = array(
214
            'to'=>$to,
215
            'amount'=>$amount
216
        );
217
        if(!is_null($from))
218
            $params['from'] = $from;
219
        if(!is_null($fee))
0 ignored issues
show
introduced by
The condition is_null($fee) is always true.
Loading history...
220
            $params['fee'] = $fee;
221
        if(!is_null($fee_per_byte))
222
            $params['fee_per_byte'] = $fee_per_byte;
223
        $response = $this->call('payment',$params);
224
        return new PaymentResponse($response);
225
    }
226
227
    /**
228
     * Send Bitcoin to multiple Addresses
229
     *
230
     * @param array<string,integer> $recipients recipients must be an array of address as key and satoshi as integer
231
     * @param integer|string|null $from xpub address or index of account that you want to send payment from
232
     * @param integer|null $fee must be in satoshi (better to set null or use fee_per_byte)
233
     * @param integer|null $fee_per_byte must be in satoshi
234
     */
235
236
    public function SendManyPayment($recipients, $from=null, $fee=null, $fee_per_byte = null){
237
        $params = array(
238
            'recipients'=>json_encode($recipients)
239
        );
240
        if(!is_null($from))
241
            $params['from'] = $from;
242
        if(!is_null($fee))
243
            $params['fee'] = $fee;
244
        if(!is_null($fee_per_byte))
245
            $params['fee_per_byte'] = $fee_per_byte;
246
        $response = $this->call('sendmany',$params);
247
        return new PaymentResponse($response);
248
    }
249
250
}
251