Skip to content

jeankassio/ElectrumPHP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

ElectrumPHP

ElectrumPHP is a PHP library designed to interact with the Electrum wallet through RPC calls. With this library, you can manage wallets, generate addresses, check balances and transaction history, and make Bitcoin payments using the Electrum daemon.

Requirements

  • PHP 7.4 or higher
  • Curl enabled
  • Electrum installed and configured on your server

Installation

  composer require jeankassio/electrumphp

Usage

Instantiate the ElectrumPHP Class

require_once(dirname(__FILE__) . "/path/to/autoload.php");

use JeanKassio\ElectrumPHP;

$walletPath = "/root/wallet";
$walletPass = "0123456789";
$rpcUser = "user";
$rpcPass = "9876543210";
$rpcPort = 7777;
$rpcHost = "127.0.0.1";

$electrum = new ElectrumPHP($walletPath, $walletPass, $rpcUser, $rpcPass, $rpcPort, $rpcHost);

Methods

Start the Electrum Daemon (if you need)

$electrum->start();

Stop the Electrum Daemon (if you need)

$electrum->stop();

Check if Daemon is Running

if($electrum->isRunning()){
    echo "Electrum daemon is running.";
}

Create a New Wallet and receive Seed

$walletPath = "/root/wallet";
$seed = false;
$password = "0123456789";
$encrypt = true;
$segwit = true;
$language = "english";
$entropy = 256;
$response = $electrum->createWallet($walletPath, $password, $seed, $encrypt, $segwit, $language, $entropy);
echo "Seed: " . $response['seed'];

Create a New Wallet with your seed

$walletPath = "/root/wallet";
$seed = "excess title assist very badge rain pet purchase device narrow awesome recall";
$password = NULL;
$encrypt = false;
$segwit = false;
$response = $electrum->createWallet($walletPath, $password, $seed, $encrypt, $segwit);
echo "Seed: " . $response['seed'];

Create a New Address

$address = $electrum->createAddress();
echo "New address: " . $address;

Get Address Balance

$balance = $electrum->getAddressBalance($address);
echo "Confirmed: " . $balance['confirmed'];
echo "Unconfirmed: " . $balance['unconfirmed'];

Get Wallet Balance

$balance = $electrum->getWalletBalance();
echo "Confirmed: " . $balance['confirmed'];
echo "Unconfirmed: " . $balance['unconfirmed'];
echo "Unmatured: " . $balance['unmatured'];

Get Transaction Details

$txid = "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b";
$transaction = $electrum->getTransaction($txid);
echo "Transaction details: " . json_encode($transaction);

Pay to a Bitcoin Address

$address = "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa";
$amount = 0.001;
$fee = 0.00001;
$feerate = NULL;
$fromAddr = "bc1plgjpn3cr5khxfee0k40py8njx0qcejrqldldnedqrshvut64jlvs467hnr";
$fromCoins = NULL;
$change = "bc1pqys8mqkneumdyncyz42ljd5zl9gqw4pryd9xsj9gnx5cw6rlvn0sjhdlhy";
$nocheck = false;
$unsigned = false;
$replaceByFee = true;

$response = $electrum->pay($address, $amount, $fee, $feerate, $fromAddr, $fromCoins, $change, $nocheck, $unsigned, $replaceByFee);
echo "Payment response: " . json_encode($response);

Pay to Multiple Addresses

$addressesTo = [
  [
    "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa",
    0.002
  ],
  [
    "bc1pgmpdnkdyxs6qp2we865tz43umrssvsqweaxvz2cuq5gp9zz6e6tstuj3wa",
    0.0015
  ],
  [
    "bc1pce8yk5epjlqrpnnavelul54ed6663tjqv6taz3gq9cte979624uqjjrv55",
    5.9216
  ]
];
$fee = 0.0009;
$feerate = NULL;
$fromAddr = "bc1plgjpn3cr5khxfee0k40py8njx0qcejrqldldnedqrshvut64jlvs467hnr";
$fromCoins = NULL;
$change = "bc1pqys8mqkneumdyncyz42ljd5zl9gqw4pryd9xsj9gnx5cw6rlvn0sjhdlhy";
$nocheck = false;
$unsigned = false;
$replaceByFee = false;

$response = $electrum->payToMany($addressesTo, $fee, $feerate, $fromAddr, $fromCoins, $change, $nocheck, $unsigned, $replaceByFee);
echo "Payment response: " . json_encode($response);

Load a Wallet

$pathWallet = "path/to/wallet";
$password = "0123456789";
$electrum->loadWallet($pathWallet, $password);

Get Wallets Currently Open

$wallets = $electrum->getWalletsOpen();
echo "Open wallets: " . json_encode($wallets);

Notify of Address Changes (Webhooks) [Not recommended, could fail]

$address = "bc1pce8yk5epjlqrpnnavelul54ed6663tjqv6taz3gq9cte979624uqjjrv55";
$yourUrl = "https://your-webhook-url";
$response = $electrum->notify($address, $yourUrl);
echo "Notify response: " . json_encode($response);

Delete Address Notification

$address = "bc1pce8yk5epjlqrpnnavelul54ed6663tjqv6taz3gq9cte979624uqjjrv55";
$response = $electrum->deleteNotify($address);
echo "Notification deleted: " . json_encode($response);

Get Private Key of Address in wallet

$address = "bc1pce8yk5epjlqrpnnavelul54ed6663tjqv6taz3gq9cte979624uqjjrv55";
$privateKey = $electrum->getPrivateKeys($address);
echo "Private key: " . $privateKey;

Get Seed of the Wallet

$seed = $electrum->getSeed();
echo "Wallet seed: " . $seed;

If the method you need doesn't exist, make a custom call.

$method = "getaddressunspent";
$params = ["bc1pce8yk5epjlqrpnnavelul54ed6663tjqv6taz3gq9cte979624uqjjrv55"];
$response = $electrum->custom($method, $params);
echo "Response: " . $response;

Error Handling

Every RPC call that fails will throw an exception.

You can handle these exceptions using simple try-catch blocks:

try{
    $electrum->start();
}catch(Exception $e){
    echo "Error: " . $e->getMessage();
}

Contribution:

Contributions are welcome! If you find a bug or have suggestions for improvements.

Feel free to open an issue or submit a Pull Request.

License:

This project is licensed under the MIT License - see the LICENSE.md file for details.