1: <?php declare( strict_types = 1 );
2:
3: namespace Waves\Account;
4:
5: use Waves\Common\Base58String;
6: use Waves\Model\ChainId;
7:
8: class PublicKey
9: {
10: const BYTES_LENGTH = 32;
11: const ETH_BYTES_LENGTH = 64;
12:
13: private Base58String $key;
14: private Address $address;
15:
16: private function __construct(){}
17:
18: static function fromBytes( string $key ): PublicKey
19: {
20: $publicKey = new PublicKey;
21: $publicKey->key = Base58String::fromBytes( $key );
22: return $publicKey;
23: }
24:
25: static function fromString( string $key ): PublicKey
26: {
27: $publicKey = new PublicKey;
28: $publicKey->key = Base58String::fromString( $key );
29: return $publicKey;
30: }
31:
32: static function fromPrivateKey( PrivateKey $key ): PublicKey
33: {
34: $publicKey = new PublicKey;
35: $wk = new \deemru\WavesKit;
36: $wk->setPrivateKey( $key->bytes(), true );
37: $publicKey->key = Base58String::fromBytes( $wk->getPublicKey( true ) );
38: return $publicKey;
39: }
40:
41: function address( ChainId $chainId = null ): Address
42: {
43: if( !isset( $this->address ) )
44: $this->address = Address::fromPublicKey( $this, $chainId );
45: return $this->address;
46: }
47:
48: function attachAddress( Address $address ): void
49: {
50: $this->address = $address;
51: }
52:
53: function bytes(): string
54: {
55: return $this->key->bytes();
56: }
57:
58: function toString(): string
59: {
60: return $this->key->toString();
61: }
62: }
63: