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\Base64String;
10: use Waves\Common\ExceptionCode;
11: use Waves\Common\Json;
12: use Waves\Model\AssetId;
13: use Waves\Model\ChainId;
14:
15: use Waves\Transactions\SetAssetScriptTransaction as CurrentTransaction;
16:
17: class SetAssetScriptTransaction extends Transaction
18: {
19: const TYPE = 15;
20: const LATEST_VERSION = 2;
21: const MIN_FEE = 100_000_000;
22:
23: private AssetId $assetId;
24: private Base64String $script;
25:
26: static function build( PublicKey $sender, AssetId $assetId, Base64String $script ): CurrentTransaction
27: {
28: $tx = new CurrentTransaction;
29: $tx->setBase( $sender, CurrentTransaction::TYPE, CurrentTransaction::LATEST_VERSION, CurrentTransaction::MIN_FEE );
30:
31: // SET_ASSET_SCRIPT TRANSACTION
32: {
33: $tx->setAssetId( $assetId );
34: $tx->setScript( $script );
35: }
36:
37: return $tx;
38: }
39:
40: function getUnsigned(): CurrentTransaction
41: {
42: // VERSION
43: if( $this->version() !== CurrentTransaction::LATEST_VERSION )
44: throw new Exception( __FUNCTION__ . ' unexpected version = ' . $this->version(), ExceptionCode::UNEXPECTED );
45:
46: // BASE
47: $pb_Transaction = $this->getProtobufTransactionBase();
48:
49: // SET_ASSET_SCRIPT TRANSACTION
50: {
51: $pb_TransactionData = new \Waves\Protobuf\SetAssetScriptTransactionData;
52: // ASSET
53: {
54: $pb_TransactionData->setAssetId( $this->assetId()->bytes() );
55: }
56: // SCRIPT
57: {
58: $pb_TransactionData->setScript( $this->script()->bytes() );
59: }
60: }
61:
62: // SET_ASSET_SCRIPT TRANSACTION
63: $this->setBodyBytes( $pb_Transaction->setSetAssetScript( $pb_TransactionData )->serializeToString() );
64: return $this;
65: }
66:
67: function assetId(): AssetId
68: {
69: if( !isset( $this->assetId ) )
70: $this->assetId = $this->json->get( 'assetId' )->asAssetId();
71: return $this->assetId;
72: }
73:
74: function setAssetId( AssetId $assetId ): CurrentTransaction
75: {
76: $this->assetId = $assetId;
77: $this->json->put( 'assetId', $assetId->toJsonValue() );
78: return $this;
79: }
80:
81: function script(): Base64String
82: {
83: if( !isset( $this->script ) )
84: $this->script = $this->json->exists( 'script' ) ? $this->json->get( 'script' )->asBase64String() : Base64String::emptyString();
85: return $this->script;
86: }
87:
88: function setScript( Base64String $script = null ): CurrentTransaction
89: {
90: $script = $script ?? Base64String::emptyString();
91: $this->script = $script;
92: $this->json->put( 'script', $script->toJsonValue() );
93: return $this;
94: }
95:
96: // COMMON
97:
98: function __construct( Json $json = null )
99: {
100: parent::__construct( $json );
101: }
102:
103: function addProof( PrivateKey $privateKey, int $index = null ): CurrentTransaction
104: {
105: $proof = (new \deemru\WavesKit)->sign( $this->bodyBytes(), $privateKey->bytes() );
106: if( $proof === false )
107: throw new Exception( __FUNCTION__ . ' unexpected sign() error', ExceptionCode::UNEXPECTED );
108: $proof = Base58String::fromBytes( $proof )->encoded();
109:
110: $proofs = $this->proofs();
111: if( !isset( $index ) )
112: $proofs[] = $proof;
113: else
114: $proofs[$index] = $proof;
115: return $this->setProofs( $proofs );
116: }
117:
118: /**
119: * @return CurrentTransaction
120: */
121: function setType( int $type )
122: {
123: parent::setType( $type );
124: return $this;
125: }
126:
127: /**
128: * @return CurrentTransaction
129: */
130: function setSender( PublicKey $sender )
131: {
132: parent::setSender( $sender );
133: return $this;
134: }
135:
136: /**
137: * @return CurrentTransaction
138: */
139: function setVersion( int $version )
140: {
141: parent::setVersion( $version );
142: return $this;
143: }
144:
145: /**
146: * @return CurrentTransaction
147: */
148: function setFee( Amount $fee )
149: {
150: parent::setFee( $fee );
151: return $this;
152: }
153:
154: /**
155: * @return CurrentTransaction
156: */
157: function setChainId( ChainId $chainId = null )
158: {
159: parent::setChainId( $chainId );
160: return $this;
161: }
162:
163: /**
164: * @return CurrentTransaction
165: */
166: function setTimestamp( int $timestamp = null )
167: {
168: parent::setTimestamp( $timestamp );
169: return $this;
170: }
171:
172: /**
173: * @param array<int, string> $proofs
174: * @return CurrentTransaction
175: */
176: function setProofs( array $proofs = null )
177: {
178: parent::setProofs( $proofs );
179: return $this;
180: }
181:
182: function bodyBytes(): string
183: {
184: if( !isset( $this->bodyBytes ) )
185: $this->getUnsigned();
186: return parent::bodyBytes();
187: }
188: }
189: