Bit-Wasp /
bitcoin-php
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | namespace BitWasp\Bitcoin\Mnemonic\Electrum; |
||||
| 6 | |||||
| 7 | use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface; |
||||
| 8 | use BitWasp\Bitcoin\Mnemonic\MnemonicInterface; |
||||
| 9 | use BitWasp\Buffertools\Buffer; |
||||
| 10 | use BitWasp\Buffertools\BufferInterface; |
||||
| 11 | |||||
| 12 | class ElectrumMnemonic implements MnemonicInterface |
||||
| 13 | { |
||||
| 14 | /** |
||||
| 15 | * @var EcAdapterInterface |
||||
| 16 | */ |
||||
| 17 | private $ecAdapter; |
||||
| 18 | |||||
| 19 | /** |
||||
| 20 | * @var ElectrumWordListInterface |
||||
| 21 | */ |
||||
| 22 | private $wordList; |
||||
| 23 | |||||
| 24 | /** |
||||
| 25 | * @param EcAdapterInterface $ecAdapter |
||||
| 26 | * @param ElectrumWordListInterface $wordList |
||||
| 27 | */ |
||||
| 28 | 5 | public function __construct(EcAdapterInterface $ecAdapter, ElectrumWordListInterface $wordList) |
|||
| 29 | { |
||||
| 30 | 5 | $this->ecAdapter = $ecAdapter; |
|||
| 31 | 5 | $this->wordList = $wordList; |
|||
| 32 | 5 | } |
|||
| 33 | |||||
| 34 | /** |
||||
| 35 | * @param BufferInterface $entropy |
||||
| 36 | * @return string[] |
||||
| 37 | * @throws \Exception |
||||
| 38 | */ |
||||
| 39 | 1 | public function entropyToWords(BufferInterface $entropy): array |
|||
| 40 | { |
||||
| 41 | 1 | $math = $this->ecAdapter->getMath(); |
|||
| 42 | 1 | $n = gmp_init(count($this->wordList), 10); |
|||
| 43 | 1 | $wordArray = []; |
|||
| 44 | |||||
| 45 | 1 | $chunks = $entropy->getSize() / 4; |
|||
| 46 | 1 | for ($i = 0; $i < $chunks; $i++) { |
|||
| 47 | 1 | $x = $entropy->slice(4*$i, 4)->getGmp(); |
|||
| 48 | 1 | $index1 = $math->mod($x, $n); |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 49 | 1 | $index2 = $math->mod($math->add($math->div($x, $n), $index1), $n); |
|||
|
0 ignored issues
–
show
It seems like
$n can also be of type resource; however, parameter $divisor of Mdanter\Ecc\Math\GmpMath::div() does only seem to accept GMP, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 50 | 1 | $index3 = $math->mod($math->add($math->div($math->div($x, $n), $n), $index2), $n); |
|||
| 51 | |||||
| 52 | 1 | $wordArray[] = $this->wordList->getWord((int) gmp_strval($index1, 10)); |
|||
| 53 | 1 | $wordArray[] = $this->wordList->getWord((int) gmp_strval($index2, 10)); |
|||
| 54 | 1 | $wordArray[] = $this->wordList->getWord((int) gmp_strval($index3, 10)); |
|||
| 55 | } |
||||
| 56 | |||||
| 57 | 1 | return $wordArray; |
|||
| 58 | } |
||||
| 59 | |||||
| 60 | /** |
||||
| 61 | * @param BufferInterface $entropy |
||||
| 62 | * @return string |
||||
| 63 | */ |
||||
| 64 | 1 | public function entropyToMnemonic(BufferInterface $entropy): string |
|||
| 65 | { |
||||
| 66 | 1 | return implode(' ', $this->entropyToWords($entropy)); |
|||
| 67 | } |
||||
| 68 | |||||
| 69 | /** |
||||
| 70 | * @param string $mnemonic |
||||
| 71 | * @return BufferInterface |
||||
| 72 | */ |
||||
| 73 | 4 | public function mnemonicToEntropy(string $mnemonic): BufferInterface |
|||
| 74 | { |
||||
| 75 | 4 | $math = $this->ecAdapter->getMath(); |
|||
| 76 | 4 | $wordList = $this->wordList; |
|||
| 77 | 4 | $words = explode(' ', $mnemonic); |
|||
| 78 | 4 | $n = gmp_init(count($wordList), 10); |
|||
| 79 | 4 | $thirdWordCount = count($words) / 3; |
|||
| 80 | 4 | $out = ''; |
|||
| 81 | |||||
| 82 | 4 | for ($i = 0; $i < $thirdWordCount; $i++) { |
|||
| 83 | 4 | list ($index1, $index2, $index3) = array_map(function ($v) use ($wordList) { |
|||
| 84 | 4 | return gmp_init($wordList->getIndex($v), 10); |
|||
| 85 | 4 | }, array_slice($words, 3 * $i, 3)); |
|||
| 86 | |||||
| 87 | $x = $math->add( |
||||
| 88 | $index1, |
||||
| 89 | $math->add( |
||||
| 90 | $math->mul( |
||||
| 91 | $n, |
||||
|
0 ignored issues
–
show
It seems like
$n can also be of type resource; however, parameter $multiplier of Mdanter\Ecc\Math\GmpMath::mul() does only seem to accept GMP, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 92 | $math->mod($math->sub($index2, $index1), $n) |
||||
|
0 ignored issues
–
show
It seems like
$n can also be of type resource; however, parameter $modulus of Mdanter\Ecc\Math\GmpMath::mod() does only seem to accept GMP, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 93 | ), |
||||
| 94 | $math->mul( |
||||
| 95 | $n, |
||||
| 96 | $math->mul( |
||||
| 97 | $n, |
||||
| 98 | $math->mod($math->sub($index3, $index2), $n) |
||||
| 99 | ) |
||||
| 100 | ) |
||||
| 101 | ) |
||||
| 102 | ); |
||||
| 103 | |||||
| 104 | $out .= str_pad(gmp_strval($x, 16), 8, '0', STR_PAD_LEFT); |
||||
| 105 | } |
||||
| 106 | |||||
| 107 | return Buffer::hex($out); |
||||
| 108 | } |
||||
| 109 | } |
||||
| 110 |