1: <?php declare( strict_types = 1 );
2:
3: namespace Waves\Account;
4:
5: use Exception;
6: use Waves\Common\ExceptionCode;
7: use Waves\Common\Base58String;
8: use Waves\Model\ChainId;
9: use Waves\Model\WavesConfig;
10:
11: class Address
12: {
13: const BYTE_LENGTH = 26;
14: const STRING_LENGTH = 35;
15:
16: private Base58String $address;
17:
18: private function __construct(){}
19:
20: static function fromString( string $encoded ): Address
21: {
22: $address = new Address;
23: $address->address = Base58String::fromString( $encoded );
24: return $address;
25: }
26:
27: static function fromBytes( string $bytes ): Address
28: {
29: if( strlen( $bytes ) !== Address::BYTE_LENGTH )
30: throw new Exception( __FUNCTION__ . ' bad address length: ' . strlen( $bytes ), ExceptionCode::BAD_ADDRESS );
31: $address = new Address;
32: $address->address = Base58String::fromBytes( $bytes );
33: return $address;
34: }
35:
36: static function fromPublicKey( PublicKey $publicKey, ChainId $chainId = null ): Address
37: {
38: $address = new Address;
39: $wk = new \deemru\WavesKit( ( isset( $chainId ) ? $chainId : WavesConfig::chainId() )->asString() );
40: $wk->setPublicKey( $publicKey->bytes(), true );
41: $address->address = Base58String::fromBytes( $wk->getAddress( true ) );
42: return $address;
43: }
44:
45: function chainId(): ChainId
46: {
47: return ChainId::fromString( $this->bytes()[1] );
48: }
49:
50: function bytes(): string
51: {
52: $bytes = $this->address->bytes();
53: if( strlen( $bytes ) !== Address::BYTE_LENGTH )
54: throw new Exception( __FUNCTION__ . ' bad address length: ' . strlen( $bytes ), ExceptionCode::BAD_ADDRESS );
55: return $bytes;
56: }
57:
58: function encoded(): string
59: {
60: return $this->address->encoded();
61: }
62:
63: function toString(): string
64: {
65: return $this->encoded();
66: }
67:
68: function publicKeyHash(): string
69: {
70: return substr( $this->bytes(), 2, 20 );
71: }
72: }
73: