Bitpay\PrivateKey::serializeSig PHP Method

serializeSig() public static method

ASN.1 DER encodes the signature based on the form: 0x30 + size(all) + 0x02 + size(r) + r + 0x02 + size(s) + s http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
public static serializeSig ( $r, $s ) : string
return string
    public static function serializeSig($r, $s)
    {
        $dec = '';
        $byte = '';
        $seq = '';
        $digits = array();
        $retval = array();
        for ($x = 0; $x < 256; $x++) {
            $digits[$x] = chr($x);
        }
        $dec = Util::decodeHex($r);
        while (Math::cmp($dec, '0') > 0) {
            $dv = Math::div($dec, '256');
            $rem = Math::mod($dec, '256');
            $dec = $dv;
            $byte = $byte . $digits[$rem];
        }
        $byte = strrev($byte);
        // msb check
        if (Math::cmp('0x' . bin2hex($byte[0]), '0x80') >= 0) {
            $byte = chr(0x0) . $byte;
        }
        $retval['bin_r'] = bin2hex($byte);
        $seq = chr(0x2) . chr(strlen($byte)) . $byte;
        $dec = Util::decodeHex($s);
        $byte = '';
        while (Math::cmp($dec, '0') > 0) {
            $dv = Math::div($dec, '256');
            $rem = Math::mod($dec, '256');
            $dec = $dv;
            $byte = $byte . $digits[$rem];
        }
        $byte = strrev($byte);
        // msb check
        if (Math::cmp('0x' . bin2hex($byte[0]), '0x80') >= 0) {
            $byte = chr(0x0) . $byte;
        }
        $retval['bin_s'] = bin2hex($byte);
        $seq = $seq . chr(0x2) . chr(strlen($byte)) . $byte;
        $seq = chr(0x30) . chr(strlen($seq)) . $seq;
        $retval['seq'] = bin2hex($seq);
        return $retval;
    }