基本的には、JavaScriptを完全にPHPに変換することができます。 ここで私は変換し、PHPのethereumアドレスを検証するためのコードをテストすることができました。
/**
* Checks if the given string is an address
*
* @method isAddress
* @param {String} $address the given HEX adress
* @return {Boolean}
*/
function isAddress($address) {
if (!preg_match('/^(0x)?[0-9a-f]{40}$/i',$address)) {
// check if it has the basic requirements of an address
return false;
} elseif (!preg_match('/^(0x)?[0-9a-f]{40}$/',$address) || preg_match('/^(0x)?[0-9A-F]{40}$/',$address)) {
// If it's all small caps or all all caps, return true
return true;
} else {
// Otherwise check each case
return isChecksumAddress($address);
}
}
/**
* Checks if the given string is a checksummed address
*
* @method isChecksumAddress
* @param {String} $address the given HEX adress
* @return {Boolean}
*/
function isChecksumAddress($address) {
// Check each case
$address = str_replace('0x','',$address);
$addressHash = hash('sha3',strtolower($address));
$addressArray=str_split($address);
$addressHashArray=str_split($addressHash);
for($i = 0; $i < 40; $i++) {
// the nth letter should be uppercase if the nth digit of casemap is 1
if ((intval($addressHashArray[$i], 16) > 7 && strtoupper($addressArray[$i]) !== $addressArray[$i]) || (intval($addressHashArray[$i], 16) <= 7 && strtolower($addressArray[$i]) !== $addressArray[$i])) {
return false;
}
}
return true;
}
一方、イーサリアムアドレスの有効性をチェックするための非常に単純な正規表現を探して誰かのために(使用する例えばHTMLフィールドのパターン属性としてある)、この正規表現は十分です。
^(0x)?[0-9a-fA-F]{40}$
また、@RiggsFolly私は、変換を求めていないよ一般的なアルゴリズムを読んで(それは一般的にチェックされているか)とPHP –
でそれを書き、私は学ぶことを愛し、私はとても素敵な場所だと思います。私はあなたが知っている質問にも答えますか? – btc4cash
PHPでethereum/regex検証に関する建設的な答えを提供できますか? – btc4cash