1: <?php declare( strict_types = 1 );
2:
3: namespace Waves\Transactions;
4:
5: use Exception;
6: use Waves\Account\PrivateKey;
7: use Waves\Common\Base58String;
8: use Waves\Account\PublicKey;
9: use Waves\Common\ExceptionCode;
10: use Waves\Common\Json;
11: use Waves\Model\Alias;
12: use Waves\Model\ChainId;
13:
14: use Waves\Transactions\CreateAliasTransaction as CurrentTransaction;
15:
16: class CreateAliasTransaction extends Transaction
17: {
18: const TYPE = 10;
19: const LATEST_VERSION = 3;
20: const MIN_FEE = 100_000;
21:
22: private Alias $alias;
23:
24: static function build( PublicKey $sender, Alias $alias ): CurrentTransaction
25: {
26: $tx = new CurrentTransaction;
27: $tx->setBase( $sender, CurrentTransaction::TYPE, CurrentTransaction::LATEST_VERSION, CurrentTransaction::MIN_FEE );
28:
29: // ALIAS TRANSACTION
30: {
31: $tx->setAlias( $alias );
32: }
33:
34: return $tx;
35: }
36:
37: function getUnsigned(): CurrentTransaction
38: {
39: // VERSION
40: if( $this->version() !== CurrentTransaction::LATEST_VERSION )
41: throw new Exception( __FUNCTION__ . ' unexpected version = ' . $this->version(), ExceptionCode::UNEXPECTED );
42:
43: // BASE
44: $pb_Transaction = $this->getProtobufTransactionBase();
45:
46: // ALIAS TRANSACTION
47: {
48: $pb_TransactionData = new \Waves\Protobuf\CreateAliasTransactionData;
49: // ID
50: {
51: $pb_TransactionData->setAlias( $this->alias()->name() );
52: }
53: }
54:
55: // ALIAS TRANSACTION
56: $this->setBodyBytes( $pb_Transaction->setCreateAlias( $pb_TransactionData )->serializeToString() );
57: return $this;
58: }
59:
60: function alias(): Alias
61: {
62: if( !isset( $this->alias ) )
63: $this->alias = Alias::fromString( $this->json->get( 'alias' )->asString() );
64: return $this->alias;
65: }
66:
67: function setAlias( Alias $alias ): CurrentTransaction
68: {
69: $this->alias = $alias;
70: $this->json->put( 'alias', $alias->name() );
71: return $this;
72: }
73:
74: // COMMON
75:
76: function __construct( Json $json = null )
77: {
78: parent::__construct( $json );
79: }
80:
81: function addProof( PrivateKey $privateKey, int $index = null ): CurrentTransaction
82: {
83: $proof = (new \deemru\WavesKit)->sign( $this->bodyBytes(), $privateKey->bytes() );
84: if( $proof === false )
85: throw new Exception( __FUNCTION__ . ' unexpected sign() error', ExceptionCode::UNEXPECTED );
86: $proof = Base58String::fromBytes( $proof )->encoded();
87:
88: $proofs = $this->proofs();
89: if( !isset( $index ) )
90: $proofs[] = $proof;
91: else
92: $proofs[$index] = $proof;
93: return $this->setProofs( $proofs );
94: }
95:
96: /**
97: * @return CurrentTransaction
98: */
99: function setType( int $type )
100: {
101: parent::setType( $type );
102: return $this;
103: }
104:
105: /**
106: * @return CurrentTransaction
107: */
108: function setSender( PublicKey $sender )
109: {
110: parent::setSender( $sender );
111: return $this;
112: }
113:
114: /**
115: * @return CurrentTransaction
116: */
117: function setVersion( int $version )
118: {
119: parent::setVersion( $version );
120: return $this;
121: }
122:
123: /**
124: * @return CurrentTransaction
125: */
126: function setFee( Amount $fee )
127: {
128: parent::setFee( $fee );
129: return $this;
130: }
131:
132: /**
133: * @return CurrentTransaction
134: */
135: function setChainId( ChainId $chainId = null )
136: {
137: parent::setChainId( $chainId );
138: return $this;
139: }
140:
141: /**
142: * @return CurrentTransaction
143: */
144: function setTimestamp( int $timestamp = null )
145: {
146: parent::setTimestamp( $timestamp );
147: return $this;
148: }
149:
150: /**
151: * @param array<int, string> $proofs
152: * @return CurrentTransaction
153: */
154: function setProofs( array $proofs = null )
155: {
156: parent::setProofs( $proofs );
157: return $this;
158: }
159:
160: function bodyBytes(): string
161: {
162: if( !isset( $this->bodyBytes ) )
163: $this->getUnsigned();
164: return parent::bodyBytes();
165: }
166: }
167: