From 026f105b2960131dae6d890a2d1af9f59a8c82aa Mon Sep 17 00:00:00 2001 From: Paragon Initiative Enterprises Date: Mon, 20 Dec 2021 17:04:17 -0500 Subject: [PATCH] Add toString method to SignedMessage --- lib/Protocol/Message.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/lib/Protocol/Message.php b/lib/Protocol/Message.php index b79a85e..04f10b8 100644 --- a/lib/Protocol/Message.php +++ b/lib/Protocol/Message.php @@ -71,4 +71,40 @@ public function signatureValidForPublicKey($publicKey) Util::rawBinary($publicKey, 32) ); } + + /** + * @param bool $pretty + * @return string + * @throws SodiumException + */ + public function toString($pretty = false) + { + $code = $pretty ? JSON_PRETTY_PRINT : 0; + if ($this->signature) { + $signature = sodium_bin2base64( + Util::rawBinary($this->getSignature(), 64), + SODIUM_BASE64_VARIANT_URLSAFE + ); + } else { + $signature = ''; + } + return (string) json_encode(array( + 'signature' => $signature, + 'message' => $this->getContents(), + ), $code); + } + + /** + * @return string + */ + public function __toString() + { + try { + return $this->toString(); + } catch (\Error $ex) { + return ''; + } catch (\Exception $ex) { + return ''; + } + } }