Completed
Pull Request — master (#118)
by thomas
02:37
created

WalletPath   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 107
ccs 0
cts 44
cp 0
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 4
A address() 0 3 1
A BIP32Path() 0 5 1
A path() 0 5 1
A backupPath() 0 5 1
A keyIndexPath() 0 5 1
A keyIndexBackupPath() 0 5 1
A create() 0 3 1
A fromBIP32Path() 0 7 2
A __toString() 0 3 1
1
<?php
2
3
namespace Blocktrail\SDK;
4
5
use Blocktrail\SDK\Bitcoin\BIP32Path;
6
7
/**
8
 * Class WalletPath
9
 *
10
 * Blocktrail Wallet custom BIP32 Path
11
 */
12
class WalletPath {
13
    protected $keyIndex;
14
    protected $chain;
15
    protected $address;
16
17
    public function __construct($keyIndex = 0, $chain = 0, $address = 0) {
18
        $this->keyIndex = str_replace("'", "", $keyIndex ?: 0);
19
        $this->chain = str_replace("'", "", $chain ?: 0);
20
        $this->address = str_replace("'", "", $address ?: 0);
21
    }
22
23
    /**
24
     * change the address and return new instance
25
     *
26
     * @param $address
27
     * @return WalletPath
28
     */
29
    public function address($address) {
30
        return new static($this->keyIndex, $this->chain, $address);
31
    }
32
33
    protected static function BIP32Path(array $path) {
34
        return BIP32Path::path(array_values(array_filter($path, function ($v) {
35
            return $v !== null;
36
        })));
37
    }
38
39
    /**
40
     * get the BIP32Path
41
     * m / key_index' / chain / address_index
42
     *
43
     * @return BIP32Path
44
     */
45
    public function path() {
46
        return self::BIP32Path([
47
            "m", "{$this->keyIndex}'", $this->chain, $this->address
48
        ]);
49
    }
50
51
    /**
52
     * get the BIP32Path for the backup key
53
     * m / key_index / chain / address_index
54
     *
55
     * @return BIP32Path
56
     */
57
    public function backupPath() {
58
        return self::BIP32Path([
59
            "m", $this->keyIndex, $this->chain, $this->address
60
        ]);
61
    }
62
63
    /**
64
     * get the BIP32Path for the key index
65
     * m / key_index'
66
     *
67
     * @return BIP32Path
68
     */
69
    public function keyIndexPath() {
70
        return self::BIP32Path([
71
            "m", "{$this->keyIndex}'"
72
        ]);
73
    }
74
75
    /**
76
     * get the BIP32Path for the key index for the backup key
77
     * m / key_index
78
     *
79
     * @return BIP32Path
80
     */
81
    public function keyIndexBackupPath() {
82
        return self::BIP32Path([
83
            "m", $this->keyIndex
84
        ]);
85
    }
86
87
    /**
88
     * static method to initialize class
89
     *
90
     * @param int  $keyIndex
91
     * @param int  $chain
92
     * @param int  $address
93
     * @return WalletPath
94
     */
95
    public static function create($keyIndex = 0, $chain = 0, $address = 0) {
96
        return new static($keyIndex, $chain, $address);
97
    }
98
99
    /**
100
     * @param BIP32Path $path
101
     * @return WalletPath
102
     * @throws \Exception
103
     */
104
    public static function fromBIP32Path(BIP32Path $path) {
105
        if (strtolower($path[0]) != "m") {
106
            throw new \Exception("Path should be absolute [{$path}]");
107
        }
108
109
        return static::create($path[1], $path[2], $path[3]);
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function __toString() {
116
        return (string)$this->path();
117
    }
118
}
119