1: <?php declare( strict_types = 1 );
2:
3: namespace Waves\Model;
4:
5: use Exception;
6: use Waves\Common\ExceptionCode;
7: use Waves\Common\Base58String;
8:
9: class Id
10: {
11: const BYTE_LENGTH = 32;
12:
13: private Base58String $id;
14:
15: private function __construct(){}
16:
17: static function fromString( string $encoded ): Id
18: {
19: $id = new Id;
20: $id->id = Base58String::fromString( $encoded );
21: return $id;
22: }
23:
24: static function fromBytes( string $bytes ): Id
25: {
26: if( strlen( $bytes ) !== Id::BYTE_LENGTH )
27: throw new Exception( __FUNCTION__ . ' bad id length: ' . strlen( $bytes ), ExceptionCode::BAD_ASSET );
28: $id = new Id;
29: $id->id = Base58String::fromBytes( $bytes );
30: return $id;
31: }
32:
33: function bytes(): string
34: {
35: $bytes = $this->id->bytes();
36: if( strlen( $bytes ) !== Id::BYTE_LENGTH )
37: throw new Exception( __FUNCTION__ . ' bad id length: ' . strlen( $bytes ), ExceptionCode::BAD_ASSET );
38: return $bytes;
39: }
40:
41: function encoded(): string
42: {
43: return $this->id->encoded();
44: }
45:
46: function toString(): string
47: {
48: return $this->encoded();
49: }
50: }
51: