1

I am trying to rewrite the JS class into pure php. I am struggling with converting the datatypes. In JS, $variable.slice() works just fine, however in php, it first must be converted to an array, Is there a php equivalent for JS "Buffer.from"? or Is it possible to create a 20-byte array in PHP like in JS?

Does js treat string as an array by default? In such case, I assume I'd need to convert the string params into arrays in php, but what sort of arrays? Byte? char?

Here's the original JS class: address.ts

Here is what I've tried, with equivalent js code in the comments. Can you check where I've messed things up? I am really open for suggestions.

<?php

include_once('./lib/Blake2b.php');


$literal_addr="vite_24d6313a1ee1bcd8e979565a39a9c6f4decc8a1be01a5debee";    #55 chars
$address_body="24d6313a1ee1bcd8e979565a39a9c6f4decc8a1b";                   #40 chars
$addr_cheksum="e01a5debee";                                                 #10 chars
echo "literal_addr ".$literal_addr."<br><br>";
echo "address_body ".$address_body."<br><br>";
echo "addr_cheksum ".$addr_cheksum."<br><br>";


$addrType = isValidAddress($literal_addr);
echo "addr type: ".$addrType."<br><br>";


#JS:
#const addrType = isValidAddress('vite_553462bca137bac29f440e9af4ab2e2c1bb82493e41d2bc8b2');  
#Expected result:
// addrType === 0 -> illegal address
// addrType === 1 -> account type
// addrType === 2 -> contract type

#JS:
#function isValidAddress(literal_addr: Hex): AddressType {
function isValidAddress($literal_addr) {
    if (!isValidHex($literal_addr)) {
        #JS:
        #return AddressType.Illegal;
        return 0;
    }
    return isValidCheckSum($literal_addr); #<-- here I should convert the address into an array?
}

#JS: 
#function isValidHex(hexAddr: Address): Boolean {
function isValidHex($hexAddr) {                                 
    $ADDR_PRE = 'vite_';
    $ADDR_SIZE = 20;
    $ADDR_CHECK_SUM_SIZE = 5;
    $ADDR_LEN = strlen($ADDR_PRE) + $ADDR_SIZE * 2 + $ADDR_CHECK_SUM_SIZE * 2; #55
    
    #JS: 
    #return hexAddr && hexAddr.length === ADDR_LEN && hexAddr.indexOf(ADDR_PRE) === 0;  
    return $hexAddr && strlen($hexAddr) == $ADDR_LEN && strpos($hexAddr,$ADDR_PRE) == 0; 
}

#JS: 
#function isValidCheckSum(hexAddr: Address): AddressType {
function isValidCheckSum($hexAddr){
    $ADDR_PRE = 'vite_';
    $ADDR_SIZE = 20;    
    $currentChecksum = array_slice($hexAddr,0,(strlen($ADDR_PRE) + $ADDR_SIZE * 2));        #const currentChecksum = hexAddr.slice(ADDR_PRE.length + ADDR_SIZE * 2);
    $_addr = array_slice($hexAddr, strlen($ADDR_PRE), strlen($ADDR_PRE) + $ADDR_SIZE * 2);  #const _addr = hexAddr.slice(ADDR_PRE.length, ADDR_PRE.length + ADDR_SIZE * 2);
    $addr =$_addr;      #const addr = Buffer.from(_addr, 'hex');

    $contractCheckSum = getAddrCheckSum($addr, true);   #const contractCheckSum = getAddrCheckSum(addr, true);
    if ($contractCheckSum == $currentChecksum) {    #if (contractCheckSum === currentChecksum) {
        return 2;   #    return AddressType.Contract;
    }   #}

    $checkSum = getAddrCheckSum($addr);     #const checkSum = getAddrCheckSum(addr);
    if ($currentChecksum == $checkSum) {        #if (currentChecksum === checkSum) {
        return 1;   #    return AddressType.Account;
    }   #}

    return 0;   #return AddressType.Illegal;
}



#Original JS function:
#function getAddrCheckSum(addr: Buffer, isContract? : boolean): Hex {
function getAddrCheckSum($addr, $isContract=false) {                            
    $ADDR_CHECK_SUM_SIZE = 5;               
                                                    #JS:
    $addrPre20 = array_slice($addr, 0, 20);         #const addrPre20 = addr.slice(0, 20);
    $blake2b = new Blake2b($ADDR_CHECK_SUM_SIZE);   
    $_checkSum = $blake2b->hash($addrPre20);        #const _checkSum = blake2b(addrPre20, null, ADDR_CHECK_SUM_SIZE);
    $checkSum = $_checkSum;                         #const checkSum = Buffer.from(_checkSum);
                    
    #return $checkSum;              
    if (!$isContract) {                                                 #if (!isContract) {
        return strval($checkSum);                                       #   return checkSum.toString('hex');
    }                                                                   #}
                
    $newCheckSum = [];                                                  #const newCheckSum = [];
    $newCheckSum = $checkSum ^ str_pad("", strlen($checkSum), "\xFF");  #checkSum.forEach(function (byte) {
                                                                        #   newCheckSum.push(byte ^ 0xFF);
                                                                        #});
                                                                        #
    return bin2hex($newCheckSum);                                       #return Buffer.from(newCheckSum).toString('hex');
}



?>
<br><br><br><br><br><br>

Here's my output:

literal_addr vite_24d6313a1ee1bcd8e979565a39a9c6f4decc8a1be01a5debee

address_body 24d6313a1ee1bcd8e979565a39a9c6f4decc8a1b

addr_cheksum e01a5debee


Warning: array_slice() expects parameter 1 to be array, string given in C:\xampp\htdocs\vitex\v03\vite2.php on line 54

Warning: array_slice() expects parameter 1 to be array, string given in C:\xampp\htdocs\vitex\v03\vite2.php on line 55

Warning: array_slice() expects parameter 1 to be array, null given in C:\xampp\htdocs\vitex\v03\vite2.php on line 78

Warning: array_slice() expects parameter 1 to be array, null given in C:\xampp\htdocs\vitex\v03\vite2.php on line 78
addr type: 0
skazichris
  • 95
  • 2
  • 12

0 Answers0