lib/WalletManager.php
/*
     * Convert mnemonic words to a hexadecimal seed
     *
     * @param string $mnemonic
     * @return string
     */
Convert mnemonic words to a hexadecimal seed
@param string $mnemonic
@return string
Convert mnemonic words to a hexadecimal seed @param string $mnemonic @return string
public function mn_decode($mnemonic)
    {
        $words = json_decode(file_get_contents(__DIR__ . '/mnemonic.json'), true);
        $words = $words[$this->mnemonic_language];
        $wordsLen = count($words['words']);
        $mnemonicList = explode(' ', $mnemonic);
        if (count($mnemonicList) < 12) {
            throw new Exception('Error: Too few mnemonic words');
        }
        if ($words['prefix_len'] === 0 && count($mnemonicList) % 3 !== 0) {
            throw new Exception('Error: Too few mnemonic words');
        }
        if ($words['prefix_len'] > 0 && count($mnemonicList) % 3 === 2) {
            throw new Exception('Error: Too few mnemonic words');
        }
        if ($words['prefix_len'] > 0 && count($mnemonicList) % 3 === 0) {
            throw new Exception('Error: Last mnemonic word is missing');
        }
        $checksum_word = '';
        if ($words['prefix_len'] > 0) {
            $checksum_word = array_pop($mnemonicList);
        }
        $result = '';
        for ($i = 0; $i < count($mnemonicList); $i += 3) {
            $w1 = array_search($mnemonicList[$i], $words['words']);
            $w2 = array_search($mnemonicList[$i + 1], $words['words']);
            $w3 = array_search($mnemonicList[$i + 2], $words['words']);
            $x = $w1 + $wordsLen * ((($wordsLen - $w1) + $w2) % $wordsLen) + $wordsLen * $wordsLen * ((($wordsLen - $w2) + $w3) % $wordsLen);
            $result .= $this->mn_swap_endian_4byte(substr('0000000' . dechex($x), -8));
        }
        if (!empty($checksum_word)) {
            $checksum_index = $this->mn_get_checksum_index($mnemonicList, $words['prefix_len']);
            if (substr($mnemonicList[$checksum_index], 0, $words['prefix_len']) !== substr($checksum_word, 0, $words['prefix_len'])) {
                throw new Exception('Error: Private key cannot be verified');
            }
        }
        return $result;
    }
public function mn_decode ( $ mnemonic ) { $ words = json_decode ( file_get_contents ( __DIR__ . '/mnemonic.json' ) , true ) ; $ words = $ words [ $ this -> mnemonic_language ] ; $ wordsLen = count ( $ words [ 'words' ] ) ; $ mnemonicList = explode ( ' ' , $ mnemonic ) ; if ( count ( $ mnemonicList ) < 12 ) { throw new Exception ( 'Error: Too few mnemonic words' ) ; } if ( $ words [ 'prefix_len' ] === 0 && count ( $ mnemonicList ) % 3 !== 0 ) { throw new Exception ( 'Error: Too few mnemonic words' ) ; } if ( $ words [ 'prefix_len' ] > 0 && count ( $ mnemonicList ) % 3 === 2 ) { throw new Exception ( 'Error: Too few mnemonic words' ) ; } if ( $ words [ 'prefix_len' ] > 0 && count ( $ mnemonicList ) % 3 === 0 ) { throw new Exception ( 'Error: Last mnemonic word is missing' ) ; } $ checksum_word = '' ; if ( $ words [ 'prefix_len' ] > 0 ) { $ checksum_word = array_pop ( $ mnemonicList ) ; } $ result = '' ; for ( $ i = 0 ; $ i < count ( $ mnemonicList ) ; $ i += 3 ) { $ w1 = array_search ( $ mnemonicList [ $ i ] , $ words [ 'words' ] ) ; $ w2 = array_search ( $ mnemonicList [ $ i + 1 ] , $ words [ 'words' ] ) ; $ w3 = array_search ( $ mnemonicList [ $ i + 2 ] , $ words [ 'words' ] ) ; $ x = $ w1 + $ wordsLen * ( ( ( $ wordsLen - $ w1 ) + $ w2 ) % $ wordsLen ) + $ wordsLen * $ wordsLen * ( ( ( $ wordsLen - $ w2 ) + $ w3 ) % $ wordsLen ) ; $ result .= $ this -> mn_swap_endian_4byte ( substr ( '0000000' . dechex ( $ x ) , - 8 ) ) ; } if ( ! empty ( $ checksum_word ) ) { $ checksum_index = $ this -> mn_get_checksum_index ( $ mnemonicList , $ words [ 'prefix_len' ] ) ; if ( substr ( $ mnemonicList [ $ checksum_index ] , 0 , $ words [ 'prefix_len' ] ) !== substr ( $ checksum_word , 0 , $ words [ 'prefix_len' ] ) ) { throw new Exception ( 'Error: Private key cannot be verified' ) ; } } return $ result ; }
Convert mnemonic words to a hexadecimal seed
 @param string $mnemonic
 @return string
Convert mnemonic words to a hexadecimal seed @param string $mnemonic @return string
// Get the mnemonic word list for the given language. // Split the mnemonic string into an array. // Validate mnemonic word length. // Validate mnemonic word length number. // Get the checksum if required. // Build the seed from the mnemonic word list. // Verify the checksum.