|
5 | 5 | TransactionId,
|
6 | 6 | AccountId,
|
7 | 7 | Timestamp,
|
| 8 | + TokenType, |
| 9 | + TokenSupplyType, |
8 | 10 | } from "../../src/index.js";
|
9 | 11 | import Long from "long";
|
10 | 12 | import { DEFAULT_AUTO_RENEW_PERIOD } from "../../src/transaction/Transaction.js";
|
@@ -140,4 +142,192 @@ describe("TokenCreateTransaction", function () {
|
140 | 142 | },
|
141 | 143 | });
|
142 | 144 | });
|
| 145 | + |
| 146 | + it("can be decoded from protobuf", function () { |
| 147 | + const key1 = PrivateKey.fromStringDer( |
| 148 | + "302e020100300506032b6570042204205fc37fbd55631722b7ab5ec8e31696f6d3f818a15c5258a1529de7d4a1def6e2", |
| 149 | + ); |
| 150 | + const key2 = PrivateKey.fromStringDer( |
| 151 | + "302e020100300506032b657004220420b5e15b70109fe6e11d1d6d06b20d27b494aa05a28a8bc84c627d9be66e179391", |
| 152 | + ); |
| 153 | + const key3 = PrivateKey.fromStringDer( |
| 154 | + "302e020100300506032b6570042204202b5dc9915d5b6829592f4562a3d099e7b1bdd48da347e351da8d31cd41653016", |
| 155 | + ); |
| 156 | + const key4 = PrivateKey.fromStringDer( |
| 157 | + "302e020100300506032b6570042204205a8571fe4badbc6bd76f03170f5ec4bdc69f2edb93f133477d0ef8f9e17a0f3a", |
| 158 | + ); |
| 159 | + const key5 = PrivateKey.fromStringDer( |
| 160 | + "302e020100300506032b65700422042081c36f46db2bc4e7d993a23718a158c9fffa96719d7e72b3823d8bc9b973d596", |
| 161 | + ); |
| 162 | + const key6 = PrivateKey.fromStringDer( |
| 163 | + "302e020100300506032b657004220420ff498f69b92ea43a0a8fd6e4a7036e5f8b5f23e527b0443bc309cc4ff5b75303", |
| 164 | + ); |
| 165 | + const key7 = PrivateKey.fromStringDer( |
| 166 | + "302e020100300506032b657004220420542b4d4a318a1ae5f91071f34c8d900b1150e83d15fe71d22b8581e1203f99ad", |
| 167 | + ); |
| 168 | + const key8 = PrivateKey.fromStringDer( |
| 169 | + "302e020100300506032b6570042204205447805ce906170817e2bd4e26f4ea1fd5bbc38a2532c7f66b7d7a24f60ee9d5", |
| 170 | + ); |
| 171 | + const metadata = new Uint8Array([1, 2, 3, 4, 5]); |
| 172 | + const autoRenewAccountId = new AccountId(10); |
| 173 | + const treasuryAccountId = new AccountId(11); |
| 174 | + |
| 175 | + const expirationTime = new Timestamp( |
| 176 | + Math.floor( |
| 177 | + Date.now() / 1000 + DEFAULT_AUTO_RENEW_PERIOD.toNumber(), |
| 178 | + ), |
| 179 | + 0, |
| 180 | + ); |
| 181 | + |
| 182 | + const original = new TokenCreateTransaction() |
| 183 | + .setMaxTransactionFee(new Hbar(30)) |
| 184 | + .setTransactionId( |
| 185 | + TransactionId.withValidStart( |
| 186 | + new AccountId(1), |
| 187 | + new Timestamp(2, 3), |
| 188 | + ), |
| 189 | + ) |
| 190 | + .setTokenName("name") |
| 191 | + .setTokenSymbol("symbol") |
| 192 | + .setTokenMemo("memo") |
| 193 | + .setDecimals(7) |
| 194 | + .setTreasuryAccountId(treasuryAccountId) |
| 195 | + .setAutoRenewAccountId(autoRenewAccountId) |
| 196 | + .setExpirationTime(expirationTime) |
| 197 | + .setAdminKey(key1) |
| 198 | + .setKycKey(key2) |
| 199 | + .setFreezeKey(key3) |
| 200 | + .setPauseKey(key4) |
| 201 | + .setWipeKey(key5) |
| 202 | + .setSupplyKey(key6) |
| 203 | + .setFeeScheduleKey(key7) |
| 204 | + .setMetadata(metadata) |
| 205 | + .setMetadataKey(key8) |
| 206 | + .setNodeAccountIds([new AccountId(4)]) |
| 207 | + .setTransactionMemo("random memo") |
| 208 | + .freeze(); |
| 209 | + |
| 210 | + const transactions = [original._makeTransactionBody()]; |
| 211 | + const protobufBody = transactions[0]; |
| 212 | + const signedTransactions = []; |
| 213 | + const transactionIds = [original.transactionId]; |
| 214 | + const nodeIds = [new AccountId(4)]; |
| 215 | + const bodies = [protobufBody]; |
| 216 | + |
| 217 | + const reconstructed = TokenCreateTransaction._fromProtobuf( |
| 218 | + transactions, |
| 219 | + signedTransactions, |
| 220 | + transactionIds, |
| 221 | + nodeIds, |
| 222 | + bodies, |
| 223 | + ); |
| 224 | + |
| 225 | + expect(reconstructed.tokenName).to.equal("name"); |
| 226 | + expect(reconstructed.tokenSymbol).to.equal("symbol"); |
| 227 | + expect(reconstructed.tokenMemo).to.equal("memo"); |
| 228 | + expect(reconstructed.decimals?.toNumber()).to.equal(7); |
| 229 | + expect(reconstructed.treasuryAccountId?.toString()).to.equal("0.0.11"); |
| 230 | + expect(reconstructed.autoRenewAccountId?.toString()).to.equal("0.0.10"); |
| 231 | + expect(reconstructed.expirationTime?.seconds.toString()).to.equal( |
| 232 | + expirationTime.seconds.toString(), |
| 233 | + ); |
| 234 | + expect(reconstructed.adminKey?.toString()).to.equal( |
| 235 | + key1.publicKey.toString(), |
| 236 | + ); |
| 237 | + expect(reconstructed.kycKey?.toString()).to.equal( |
| 238 | + key2.publicKey.toString(), |
| 239 | + ); |
| 240 | + expect(reconstructed.freezeKey?.toString()).to.equal( |
| 241 | + key3.publicKey.toString(), |
| 242 | + ); |
| 243 | + expect(reconstructed.pauseKey?.toString()).to.equal( |
| 244 | + key4.publicKey.toString(), |
| 245 | + ); |
| 246 | + expect(reconstructed.wipeKey?.toString()).to.equal( |
| 247 | + key5.publicKey.toString(), |
| 248 | + ); |
| 249 | + expect(reconstructed.supplyKey?.toString()).to.equal( |
| 250 | + key6.publicKey.toString(), |
| 251 | + ); |
| 252 | + expect(reconstructed.feeScheduleKey?.toString()).to.equal( |
| 253 | + key7.publicKey.toString(), |
| 254 | + ); |
| 255 | + expect(reconstructed.metadataKey?.toString()).to.equal( |
| 256 | + key8.publicKey.toString(), |
| 257 | + ); |
| 258 | + expect(reconstructed.metadata).to.deep.equal(metadata); |
| 259 | + }); |
| 260 | + |
| 261 | + it("can be decoded from protobuf with custom fees and additional properties", function () { |
| 262 | + const key1 = PrivateKey.fromStringDer( |
| 263 | + "302e020100300506032b6570042204205fc37fbd55631722b7ab5ec8e31696f6d3f818a15c5258a1529de7d4a1def6e2", |
| 264 | + ); |
| 265 | + const customFees = [ |
| 266 | + // We'll use a mock object for the custom fees since we're only testing serialization/deserialization |
| 267 | + { |
| 268 | + _toProtobuf: () => ({ |
| 269 | + fixedFee: { |
| 270 | + amount: Long.fromNumber(10), |
| 271 | + denominatingTokenId: { tokenNum: Long.fromNumber(123) }, |
| 272 | + }, |
| 273 | + }), |
| 274 | + }, |
| 275 | + ]; |
| 276 | + const metadata = new Uint8Array([1, 2, 3, 4, 5]); |
| 277 | + const treasuryAccountId = new AccountId(11); |
| 278 | + const initialSupply = Long.fromNumber(1000); |
| 279 | + const maxSupply = Long.fromNumber(10000); |
| 280 | + const freezeDefault = true; |
| 281 | + |
| 282 | + const original = new TokenCreateTransaction() |
| 283 | + .setTokenName("name") |
| 284 | + .setTokenSymbol("symbol") |
| 285 | + .setDecimals(7) |
| 286 | + .setInitialSupply(initialSupply) |
| 287 | + .setMaxSupply(maxSupply) |
| 288 | + .setTreasuryAccountId(treasuryAccountId) |
| 289 | + .setAdminKey(key1) |
| 290 | + .setFreezeDefault(freezeDefault) |
| 291 | + .setCustomFees(customFees) |
| 292 | + .setMetadata(metadata) |
| 293 | + .setTokenType(TokenType.NonFungibleUnique) |
| 294 | + .setSupplyType(TokenSupplyType.Finite) |
| 295 | + .setNodeAccountIds([new AccountId(4)]) |
| 296 | + .setTransactionId(TransactionId.generate(new AccountId(1))) |
| 297 | + .freeze(); |
| 298 | + |
| 299 | + const transactions = [original._makeTransactionBody()]; |
| 300 | + const protobufBody = transactions[0]; |
| 301 | + const signedTransactions = []; |
| 302 | + const transactionIds = [original.transactionId]; |
| 303 | + const nodeIds = [new AccountId(4)]; |
| 304 | + const bodies = [protobufBody]; |
| 305 | + |
| 306 | + const reconstructed = TokenCreateTransaction._fromProtobuf( |
| 307 | + transactions, |
| 308 | + signedTransactions, |
| 309 | + transactionIds, |
| 310 | + nodeIds, |
| 311 | + bodies, |
| 312 | + ); |
| 313 | + |
| 314 | + expect(reconstructed.tokenName).to.equal("name"); |
| 315 | + expect(reconstructed.tokenSymbol).to.equal("symbol"); |
| 316 | + expect(reconstructed.decimals?.toNumber()).to.equal(7); |
| 317 | + expect(reconstructed.initialSupply?.toNumber()).to.equal(1000); |
| 318 | + expect(reconstructed.maxSupply?.toNumber()).to.equal(10000); |
| 319 | + expect(reconstructed.treasuryAccountId?.toString()).to.equal("0.0.11"); |
| 320 | + expect(reconstructed.adminKey?.toString()).to.equal( |
| 321 | + key1.publicKey.toString(), |
| 322 | + ); |
| 323 | + expect(reconstructed.freezeDefault).to.equal(true); |
| 324 | + expect(reconstructed.metadata).to.deep.equal(metadata); |
| 325 | + expect(reconstructed.tokenType?.toString()).to.equal( |
| 326 | + TokenType.NonFungibleUnique.toString(), |
| 327 | + ); |
| 328 | + expect(reconstructed.supplyType?.toString()).to.equal( |
| 329 | + TokenSupplyType.Finite.toString(), |
| 330 | + ); |
| 331 | + expect(reconstructed.customFees.length).to.be.greaterThan(0); |
| 332 | + }); |
143 | 333 | });
|
0 commit comments