-
Notifications
You must be signed in to change notification settings - Fork 79
/
hd-wallet-derive.php
executable file
·83 lines (71 loc) · 2.46 KB
/
hd-wallet-derive.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env php
<?php
/**
* Entry point for hd-wallet-derive.
*
* Code in this file is related to interacting with the shell.
*/
// Let's be strict about things.
require_once __DIR__ . '/vendor/autoload.php';
\strictmode\initializer::init();
use App\Utils\MyLogger;
use App\WalletDerive;
use App\Utils\WalletDeriveReport;
use App\Utils\Util;
/**
* Our main function. It performs top-level exception handling.
*/
function main()
{
// why limit ourselves? ;-)
ini_set('memory_limit', -1 );
try
{
// CLI Parameters processing
$orig_params = Util::getCliParams();
list( $params, $success ) = Util::processCliParams();
if( $success != 0 )
{
return $success;
}
// Creates WalletDerive object
$walletDerive = new WalletDerive($params);
if($params['gen-key-all']) {
$result = $walletDerive->genRandomKeyForAllChains();
WalletDeriveReport::printResults($params, $result, true);
return 0;
}
if($params['gen-key']) {
$result = $walletDerive->genRandomKeyForNetwork($params['coin']);
WalletDeriveReport::printResults($params, $result);
return 0;
}
// Key derived from mnemonic if mnemonic is choosen
if( !@$params['key'] && @$params['mnemonic'] && !@$orig_params['path'] && !@$orig_params['preset']) {
$path = $walletDerive->getCoinBip44ExtKeyPathPurposeByKeyType($params['coin'], $params['key-type']);
if($path) {
$params['path'] = $path;
$walletDerive = new WalletDerive($params);
}
else {
throw new Exception(sprintf("Bip32 extended key path unknown because no Bip44 ID found for %s. You can override by setting --path explicitly.", $params['coin']));
}
}
$key = @$params['key'] ?: $walletDerive->mnemonicToKey($params['coin'], $params['mnemonic'], $params['key-type'], $params['mnemonic-pw']);
$addrs = $walletDerive->derive_keys($key);
// Prints result
echo "\n";
WalletDeriveReport::printResults($params, $addrs);
return 0;
}
catch(Exception $e)
{
MyLogger::getInstance()->log_exception( $e );
// print validation errors to stderr.
if( $e->getCode() == 2 ) {
fprintf( STDERR, $e->getMessage() . "\n\n" );
}
return $e->getCode() ?: 1;
}
}
exit(main());