Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #21 from habrauser/new_methods
Browse files Browse the repository at this point in the history
Added new classes CreateWallet, ListWallets, LoadWallets
  • Loading branch information
Padrio authored Oct 15, 2020
2 parents 19ea81c + 528b612 commit 21a877b
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 13 deletions.
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,58 @@ try {
$response->getVersion();
```

## Create new wallet

Create default wallet:
```php
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password');
$wallet = new \Electrum\Request\Method\Wallet\CreateWallet($client);
$response = $wallet->execute();
```

This code is similar to the command:
```bash
$ electrum create
```

You can also create more wallets with custom names specifying flag of the new wallet.
```php
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password');
$wallet = new \Electrum\Request\Method\Wallet\CreateWallet($client);
$response = $wallet->execute(['wallet_path' => '~/.electrum/wallets/your_wallet']);
```

This code is similar to the command:
```bash
$ electrum create -w ~/.electrum/wallets/your_wallet
```

Response will be:
```php
[
'seed' => 'wallet seed',
'path' => 'path where wallet file is stored',
'msg' => 'Please keep your seed in a safe place; if you lose it, you will not be able to restore your wallet.',
];
```

## Load wallet

```php
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password');
$load_wallet = new \Electrum\Request\Method\Wallet\LoadWallet($client);
$load_wallet->execute(['wallet_path' => '~/.electrum/wallets/your_wallet']);
````

## List wallets

Get list of all loaded wallets:
```php
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password');
$list_wallets = new \Electrum\Request\Method\Wallet\ListWallets($client);
$list_wallets->execute();
```

## Get new address
```php
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password');
Expand All @@ -68,6 +120,15 @@ $response->getVersion();
echo $tx->getAddress();
```

## Create new address for wallet

```php
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password');
$newAddress = new \Electrum\Request\Method\Address\CreateNewAddress($client);
$newAddress->execute(['wallet' => '~/.electrum/wallets/your_wallet']);
```


## Make a new Payment
```php
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password');
Expand Down
32 changes: 32 additions & 0 deletions src/Request/Method/Address/CreateNewAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php


namespace Electrum\Request\Method\Address;


use Electrum\Request\AbstractMethod;
use Electrum\Request\MethodInterface;

/**
* Generating new address if you need more
* @original_author Pascal Krason <p.krason@padr.io>
*/
class CreateNewAddress extends AbstractMethod implements MethodInterface
{

/**
* @var string
*/
private $method = 'createnewaddress';

/**
* @param array $attributes
* @return object
* @throws \Electrum\Request\Exception\BadRequestException
* @throws \Electrum\Response\Exception\ElectrumResponseException
*/
public function execute(array $attributes = [])
{
return $this->getClient()->execute($this->method, $attributes);
}
}
8 changes: 4 additions & 4 deletions src/Request/Method/Payment/AddRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class AddRequest extends AbstractMethod implements MethodInterface
/**
* @var string
*/
private $method = 'addrequest';
private $method = 'add_request';

/**
* Bitcoin amount to request
* @var int
* @var float
*/
private $amount = 0;

Expand Down Expand Up @@ -64,15 +64,15 @@ public function setMethod($method)
}

/**
* @return int
* @return float
*/
public function getAmount()
{
return $this->amount;
}

/**
* @param int $amount
* @param float $amount
*
* @return AddRequest
*/
Expand Down
5 changes: 2 additions & 3 deletions src/Request/Method/Payment/Broadcast.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Electrum\Request\AbstractMethod;
use Electrum\Request\MethodInterface;
use Electrum\Response\Exception\ElectrumResponseException;
use Electrum\Response\Model\Payment\PaymentRequest;

/**
* Return a payment request.
Expand All @@ -26,8 +26,7 @@ class Broadcast extends AbstractMethod implements MethodInterface

/**
* @param array $optional
*
* @return PaymentRequestResponse|null
* @return mixed
* @throws \Electrum\Request\Exception\BadRequestException
* @throws \Electrum\Response\Exception\ElectrumResponseException
*/
Expand Down
9 changes: 3 additions & 6 deletions src/Request/Method/Payment/PayTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,16 @@ class PayTo extends AbstractMethod implements MethodInterface

/**
* @param array $optional
*
* @return PaymentRequestResponse|null
* @return mixed
* @throws ElectrumResponseException
* @throws \Electrum\Request\Exception\BadRequestException
* @throws \Electrum\Response\Exception\ElectrumResponseException
*/
public function execute(array $optional = [])
{
$data = $this->getClient()->execute($this->method, array_merge([
return $this->getClient()->execute($this->method, array_merge([
'destination' => $this->getDestination(),
'amount' => $this->getAmount()
], $optional));

return $data['hex'];
}

/**
Expand Down
30 changes: 30 additions & 0 deletions src/Request/Method/Wallet/CreateWallet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Electrum\Request\Method\Wallet;

use Electrum\Request\AbstractMethod;
use Electrum\Request\MethodInterface;

/**
* Return newly created wallet
* @original_author Pascal Krason <p.krason@padr.io>
*/
class CreateWallet extends AbstractMethod implements MethodInterface
{

/**
* @var string
*/
private $method = 'create';

/**
* @param array $attributes
* @return object
* @throws \Electrum\Request\Exception\BadRequestException
* @throws \Electrum\Response\Exception\ElectrumResponseException
*/
public function execute(array $attributes = [])
{
return $this->getClient()->execute($this->method, $attributes);
}
}
31 changes: 31 additions & 0 deletions src/Request/Method/Wallet/ListWallets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php


namespace Electrum\Request\Method\Wallet;


use Electrum\Request\AbstractMethod;
use Electrum\Request\MethodInterface;

/**
* Return all loaded wallets
* @original_author Pascal Krason <p.krason@padr.io>
*/
class ListWallets extends AbstractMethod implements MethodInterface
{

/**
* @var string
*/
private $method = 'list_wallets';

/**
* @return object
* @throws \Electrum\Request\Exception\BadRequestException
* @throws \Electrum\Response\Exception\ElectrumResponseException
*/
public function execute()
{
return $this->getClient()->execute($this->method);
}
}
32 changes: 32 additions & 0 deletions src/Request/Method/Wallet/LoadWallet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php


namespace Electrum\Request\Method\Wallet;


use Electrum\Request\AbstractMethod;
use Electrum\Request\MethodInterface;

/**
* Load wallet
* @original_author Pascal Krason <p.krason@padr.io>
*/
class LoadWallet extends AbstractMethod implements MethodInterface
{

/**
* @var string
*/
private $method = 'load_wallet';

/**
* @param array $attributes
* @return object
* @throws \Electrum\Request\Exception\BadRequestException
* @throws \Electrum\Response\Exception\ElectrumResponseException
*/
public function execute(array $attributes = [])
{
return $this->getClient()->execute($this->method, $attributes);
}
}

0 comments on commit 21a877b

Please sign in to comment.