1: <?php declare( strict_types = 1 );
2:
3: namespace Waves\Util;
4:
5: use Exception;
6: use Waves\Common\ExceptionCode;
7: use Waves\Model\Id;
8:
9: class Functions
10: {
11: /**
12: * Decodes binary data from base58 string
13: *
14: * @param string $string
15: * @return string
16: */
17: static function base58Decode( string $string ): string
18: {
19: $decoded = \deemru\ABCode::base58()->decode( $string );
20: if( $decoded === false )
21: throw new Exception( __FUNCTION__ . ' failed to decode string: ' . $string, ExceptionCode::BASE58_DECODE );
22: return $decoded;
23: }
24:
25: /**
26: * Encodes binary data to base58 string
27: *
28: * @param string $bytes
29: * @return string
30: */
31: static function base58Encode( string $bytes ): string
32: {
33: $encoded = \deemru\ABCode::base58()->encode( $bytes );
34: if( $encoded === false )
35: // Unreachable for binary encodings
36: throw new Exception( __FUNCTION__ . ' failed to encode bytes: ' . bin2hex( $bytes ), ExceptionCode::BASE58_ENCODE ); // @codeCoverageIgnore
37: return $encoded;
38: }
39:
40: static function calculateTransactionId( string $bodyBytes ): Id
41: {
42: return Id::fromBytes( (new \deemru\WavesKit)->blake2b256( $bodyBytes ) );
43: }
44: }
45: