1 | <?php |
||
12 | class Bip39Mnemonic implements MnemonicInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var EcAdapterInterface |
||
16 | */ |
||
17 | private $ecAdapter; |
||
18 | |||
19 | /** |
||
20 | * @var Bip39WordListInterface |
||
21 | */ |
||
22 | private $wordList; |
||
23 | |||
24 | /** |
||
25 | * @param EcAdapterInterface $ecAdapter |
||
26 | * @param Bip39WordListInterface $wordList |
||
27 | */ |
||
28 | 12 | public function __construct(EcAdapterInterface $ecAdapter, Bip39WordListInterface $wordList) |
|
33 | |||
34 | /** |
||
35 | * Creates a new Bip39 mnemonic string. |
||
36 | * |
||
37 | * @param int $entropySize |
||
38 | * @return string |
||
39 | * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure |
||
40 | */ |
||
41 | public function create($entropySize = 512) |
||
48 | |||
49 | /** |
||
50 | * @param BufferInterface $entropy |
||
51 | * @param integer $CSlen |
||
52 | * @return string |
||
53 | */ |
||
54 | 98 | private function calculateChecksum(BufferInterface $entropy, $CSlen) |
|
55 | { |
||
56 | 98 | $entHash = Hash::sha256($entropy); |
|
57 | |||
58 | // Convert byte string to padded binary string of 0/1's. |
||
59 | 98 | $hashBits = str_pad(gmp_strval($entHash->getGmp(), 2), 256, '0', STR_PAD_LEFT); |
|
60 | |||
61 | // Take $CSlen bits for the checksum |
||
62 | 98 | $checksumBits = substr($hashBits, 0, $CSlen); |
|
63 | |||
64 | 98 | return $checksumBits; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param BufferInterface $entropy |
||
69 | * @return array |
||
70 | */ |
||
71 | 52 | public function entropyToWords(BufferInterface $entropy) |
|
72 | { |
||
73 | 52 | if ($entropy->getSize() === 0) { |
|
74 | throw new \InvalidArgumentException('Invalid entropy, empty'); |
||
75 | } |
||
76 | 52 | if ($entropy->getSize() > 1024) { |
|
77 | 2 | throw new \InvalidArgumentException('Invalid entropy, max 1024 bytes'); |
|
78 | } |
||
79 | 50 | if ($entropy->getSize() % 4 !== 0) { |
|
80 | 2 | throw new \InvalidArgumentException('Invalid entropy, must be multitude of 4 bytes'); |
|
81 | } |
||
82 | |||
83 | 48 | $math = $this->ecAdapter->getMath(); |
|
84 | |||
85 | 48 | $ENT = $entropy->getSize() * 8; |
|
86 | 48 | $CS = $ENT / 32; |
|
87 | |||
88 | 48 | $bits = gmp_strval($entropy->getGmp(), 2) . $this->calculateChecksum($entropy, $CS); |
|
89 | 48 | $bits = str_pad($bits, ($ENT + $CS), '0', STR_PAD_LEFT); |
|
90 | |||
91 | 48 | $result = []; |
|
92 | 48 | foreach (str_split($bits, 11) as $bit) { |
|
93 | 48 | $idx = $math->baseConvert($bit, 2, 10); |
|
94 | 48 | $result[] = $this->wordList->getWord($idx); |
|
95 | } |
||
96 | |||
97 | 48 | return $result; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param BufferInterface $entropy |
||
102 | * @return string |
||
103 | */ |
||
104 | 52 | public function entropyToMnemonic(BufferInterface $entropy) |
|
108 | |||
109 | /** |
||
110 | * @param string $mnemonic |
||
111 | * @return BufferInterface |
||
112 | */ |
||
113 | 54 | public function mnemonicToEntropy($mnemonic) |
|
156 | } |
||
157 |