Skip to content

Commit 026f105

Browse files
Add toString method to SignedMessage
1 parent 4052eed commit 026f105

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

lib/Protocol/Message.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,40 @@ public function signatureValidForPublicKey($publicKey)
7171
Util::rawBinary($publicKey, 32)
7272
);
7373
}
74+
75+
/**
76+
* @param bool $pretty
77+
* @return string
78+
* @throws SodiumException
79+
*/
80+
public function toString($pretty = false)
81+
{
82+
$code = $pretty ? JSON_PRETTY_PRINT : 0;
83+
if ($this->signature) {
84+
$signature = sodium_bin2base64(
85+
Util::rawBinary($this->getSignature(), 64),
86+
SODIUM_BASE64_VARIANT_URLSAFE
87+
);
88+
} else {
89+
$signature = '';
90+
}
91+
return (string) json_encode(array(
92+
'signature' => $signature,
93+
'message' => $this->getContents(),
94+
), $code);
95+
}
96+
97+
/**
98+
* @return string
99+
*/
100+
public function __toString()
101+
{
102+
try {
103+
return $this->toString();
104+
} catch (\Error $ex) {
105+
return '';
106+
} catch (\Exception $ex) {
107+
return '';
108+
}
109+
}
74110
}

0 commit comments

Comments
 (0)