Wallet::ReceivingAddress()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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