Skip to content

Instantly share code, notes, and snippets.

@walkeralencar
Last active January 16, 2016 19:05
Show Gist options
  • Save walkeralencar/112bbe929a6711c9b35d to your computer and use it in GitHub Desktop.
Save walkeralencar/112bbe929a6711c9b35d to your computer and use it in GitHub Desktop.
Decred Wallet Class - PHP
<?php
include_once('Wallet.php');
$wallet = Decred\Wallet::create(Decred\Wallet::SEED); // create with seed
// or
$wallet = Decred\Wallet::create(Decred\Wallet::NOSEED); // create without seed
<?php
/**
* Decred class to simplify the use of dcraddrgen
* @author Walker de Alencar <@walkeralencar>
* @license LGPL 3
*/
namespace Decred;
class Wallet
{
const SEED = 'seed',
NOSEED = 'noseed';
private $address = null;
/**
* @param $output
* @param string $type can use: 'seed'|'noseed'
* @return string
* @throws Exception
*/
protected function generate($output, $type = Decred\Wallet::SEED)
{
if (!in_array($type, ['seed', 'noseed'])) {
throw new \Exception('Invalid Type!');
}
if (!exec('dcraddrgen'.($type === 'seed' ? '' : ' -'.$type).' '.$output)) {
throw new \Exception('dcraddrgen Not Working!');
}
$address = file_get_contents($output);
unlink($output); // DELETE FILE FROM DISK
return $address;
}
/**
* @param $output
* @param string $type can use: 'seed'|'noseed'
* @return array
* @throws Exception
*/
public static function create($type)
{
$address = null;
try {
$address = (new self)->generate(microtime(true), $type);
} catch (\Exception $e) {
$result = [
'error' => true,
'message' => $e->getMessage()
];
} finally {
$result = [
'error' => false,
'message' => 'Wallet ' . $type . ' generated with sucess!',
'wallet' => $address
];
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment