public function signTransaction(TxModel $transaction, KeyPair $kp = null)
    {
        // always set optional `signer` in case we provide a KeyPair
        if (null !== $kp) {
            $transaction->setAttribute("signer", $kp->getPublicKey("hex"));
        }

        // now we can serialize and sign
        $serialized = $transaction->serialize();
        $serialHex = Buffer::fromUInt8($serialized)->getHex();
        $serialBin = hex2bin($serialHex);
        return null !== $kp ? $kp->sign($serialBin) : null;
    }