diff --git a/src/token/TokenNftsUpdateTransaction.js b/src/token/TokenNftsUpdateTransaction.js index 28a334ed0..0827d3638 100644 --- a/src/token/TokenNftsUpdateTransaction.js +++ b/src/token/TokenNftsUpdateTransaction.js @@ -132,14 +132,6 @@ export default class TokenNftsUpdateTransaction extends Transaction { ); } - /** - * @description Extract the token id. Returns the token id. - * @returns {?TokenId} - */ - get tokenId() { - return this._tokenId; - } - /** * @description Assign the token id. * @param {TokenId | string} tokenId @@ -155,14 +147,6 @@ export default class TokenNftsUpdateTransaction extends Transaction { return this; } - /** - * @description Extract the list of serial numbers. Returns the list of serial numbers. - * @returns {?Long[]} - */ - get serialNumbers() { - return this._serialNumbers; - } - /** * @description Assign the list of serial numbers. * @param {Long[]} serialNumbers @@ -175,13 +159,6 @@ export default class TokenNftsUpdateTransaction extends Transaction { return this; } - /** - * @returns {?Uint8Array} - */ - get metadata() { - return this._metadata; - } - /** * @param {Uint8Array} metadata * @returns {this} @@ -232,9 +209,13 @@ export default class TokenNftsUpdateTransaction extends Transaction { token: this._tokenId != null ? this._tokenId._toProtobuf() : null, serialNumbers: this._serialNumbers != null ? this._serialNumbers : [], - metadata: { - value: this._metadata != null ? this._metadata : null, - }, + ...(this._metadata != null + ? { + metadata: { + value: this._metadata, + }, + } + : null), }; } diff --git a/test/integration/TokenCreateIntegrationTest.js b/test/integration/TokenCreateIntegrationTest.js index 695c87089..b4da60200 100644 --- a/test/integration/TokenCreateIntegrationTest.js +++ b/test/integration/TokenCreateIntegrationTest.js @@ -22,8 +22,6 @@ describe("TokenCreate", function () { const key2 = PrivateKey.generateED25519(); const key3 = PrivateKey.generateED25519(); const key4 = PrivateKey.generateED25519(); - const metadataKey = PrivateKey.generateED25519(); - const metadata = new Uint8Array(1); const response = await new TokenCreateTransaction() .setTokenName("ffff") @@ -37,8 +35,6 @@ describe("TokenCreate", function () { .setWipeKey(key3) .setSupplyKey(key4) .setFreezeDefault(false) - .setMetadataKey(metadataKey) - .setMetadata(metadata) .execute(env.client); const tokenId = (await response.getReceipt(env.client)).tokenId; @@ -70,8 +66,6 @@ describe("TokenCreate", function () { expect(info.autoRenewPeriod).to.be.not.null; expect(info.autoRenewPeriod.seconds.toInt()).to.be.eql(7776000); expect(info.expirationTime).to.be.not.null; - expect(info.metadataKey).to.eql(metadataKey.toString()); - expect(info.metadata).to.eql(metadata); }); it("should be executable with minimal properties set", async function () { diff --git a/test/integration/TokenNftsUpdateTransactionIntegrationTest.js b/test/integration/TokenNftsUpdateTransactionIntegrationTest.js index c05c78075..0c5767b15 100644 --- a/test/integration/TokenNftsUpdateTransactionIntegrationTest.js +++ b/test/integration/TokenNftsUpdateTransactionIntegrationTest.js @@ -1,16 +1,12 @@ import { TokenCreateTransaction, - TokenInfoQuery, TokenType, PrivateKey, - TransactionReceipt, - TransactionResponse, TokenMintTransaction, TokenNftsUpdateTransaction, - Status, TokenNftInfoQuery, NftId, - TokenNftInfo, + Status, } from "../../src/exports.js"; import IntegrationTestEnv from "./client/NodeIntegrationTestEnv.js"; @@ -24,9 +20,8 @@ describe("TokenNftsUpdateTransaction", function () { tokenName, tokenSymbol, supplyKey, - tokenNftsInfo, - nftInfo, - wrongMetadataKey; + wrongMetadataKey, + nftCount; before(async function () { const env = await IntegrationTestEnv.new(); @@ -40,110 +35,230 @@ describe("TokenNftsUpdateTransaction", function () { tokenName = "Test"; tokenSymbol = "T"; wrongMetadataKey = PrivateKey.generateECDSA(); + nftCount = 4; }); - it("should update the NFT metadata", async function () { + it("should update the metadata of entire NFT collection", async function () { this.timeout(120000); - try { - const createTokenTx = new TokenCreateTransaction() - .setTokenName(tokenName) - .setTokenSymbol(tokenSymbol) - .setMetadata(metadata) - .setAdminKey(operatorKey) - .setSupplyKey(supplyKey) - .setMetadataKey(metadataKey) - .setTreasuryAccountId(operatorId) - .setTokenType(TokenType.NonFungibleUnique); - expect(createTokenTx.tokenName).to.be.eql(tokenName); - expect(createTokenTx.tokenSymbol).to.be.eql(tokenSymbol); - expect(createTokenTx.metadata).to.be.eql(metadata); - expect(createTokenTx.metadataKey.toString()).to.be.eql( - metadataKey.toString(), - ); - expect(createTokenTx.treasuryAccountId.toString()).to.be.eql( - operatorId.toString(), - ); - expect(createTokenTx.tokenType).to.be.eql( - TokenType.NonFungibleUnique, - ); - expect(createTokenTx.adminKey.toString()).to.be.eql( - operatorKey.toString(), - ); - expect(createTokenTx.supplyKey.toString()).to.be.eql( - supplyKey.toString(), - ); - const createTokenTxResponse = await createTokenTx.execute(client); - expect(createTokenTxResponse).to.be.instanceof(TransactionResponse); - const createTokenTxReceipt = - await createTokenTxResponse.getReceipt(client); - expect(createTokenTxReceipt).to.be.instanceof(TransactionReceipt); - expect(createTokenTxReceipt.status).to.be.eql(Status.Success); - const tokenId = createTokenTxReceipt.tokenId; - expect(tokenId).to.not.be.null; + const createTokenTx = new TokenCreateTransaction() + .setTokenName(tokenName) + .setTokenSymbol(tokenSymbol) + .setAdminKey(operatorKey) + .setSupplyKey(supplyKey) + .setMetadataKey(metadataKey) + .setTreasuryAccountId(operatorId) + .setTokenType(TokenType.NonFungibleUnique); - const tokenInfo = await new TokenInfoQuery() - .setTokenId(tokenId) - .execute(client); - expect(tokenInfo.metadata).to.be.eql(metadata); + const createTokenTxResponse = await createTokenTx.execute(client); + const createTokenTxReceipt = + await createTokenTxResponse.getReceipt(client); + const tokenId = createTokenTxReceipt.tokenId; - const tokenMintTx = new TokenMintTransaction() - .setMetadata([metadata]) - .setTokenId(tokenId) - .freezeWith(client); + const tokenMintTx = new TokenMintTransaction() + .setMetadata(generateMetadataList(metadata, nftCount)) + .setTokenId(tokenId) + .freezeWith(client); - const tokenMintResponse = await ( - await tokenMintTx.sign(supplyKey) - ).execute(client); - expect(tokenMintResponse).to.be.instanceof(TransactionResponse); - const tokenMintReceipt = await tokenMintResponse.getReceipt(client); - expect(tokenMintReceipt).to.be.instanceof(TransactionReceipt); - expect(tokenMintReceipt.status).to.be.eql(Status.Success); + const tokenMintResponse = await ( + await tokenMintTx.sign(supplyKey) + ).execute(client); + const tokenMintReceipt = await tokenMintResponse.getReceipt(client); + const serials = tokenMintReceipt.serials; - const nftSerial = tokenMintReceipt.serials[0]; - const nftId = new NftId(tokenId, nftSerial); + const metadatas = await geNftsMetadata(client, tokenId, serials); + expect( + metadatas.every( + (mt) => mt === Buffer.from(metadata).toString("hex"), + ), + ).to.be.true; - tokenNftsInfo = await new TokenNftInfoQuery() - .setNftId(nftId) - .execute(client); - nftInfo = tokenNftsInfo[0]; - expect(nftInfo).to.be.instanceof(TokenNftInfo); - expect(nftInfo.metadata).to.be.eql(metadata); + await ( + await ( + await new TokenNftsUpdateTransaction() + .setTokenId(tokenId) + .setSerialNumbers(serials) + .setMetadata(newMetadata) + .freezeWith(client) + .sign(metadataKey) + ).execute(client) + ).getReceipt(client); - const tokenUpdateNftsTx = new TokenNftsUpdateTransaction() - .setTokenId(tokenId) - .setSerialNumbers([nftSerial]) - .setMetadata(newMetadata) - .freezeWith(client); + const newMetadatas = await geNftsMetadata(client, tokenId, serials); + expect( + newMetadatas.every( + (mt) => mt === Buffer.from(newMetadata).toString("hex"), + ), + ).to.be.true; + }); - const tokenUpdateNftsResponse = await ( - await tokenUpdateNftsTx.sign(metadataKey) - ).execute(client); - expect(tokenUpdateNftsResponse).to.be.instanceof( - TransactionResponse, - ); - const tokenUpdateNftsReceipt = - await tokenUpdateNftsResponse.getReceipt(client); - expect(tokenUpdateNftsReceipt).to.be.instanceof(TransactionReceipt); - expect(tokenUpdateNftsReceipt.status).to.be.eql(Status.Success); - - tokenNftsInfo = await new TokenNftInfoQuery() - .setNftId(new NftId(tokenId, nftSerial)) - .execute(client); - nftInfo = tokenNftsInfo[0]; - expect(nftInfo.metadata).to.be.eql(newMetadata); - } catch (error) { - console.warn(error); - } + it("should update the NFT's metadata", async function () { + this.timeout(120000); + + const createTokenTx = new TokenCreateTransaction() + .setTokenName(tokenName) + .setTokenSymbol(tokenSymbol) + .setAdminKey(operatorKey) + .setSupplyKey(supplyKey) + .setMetadataKey(metadataKey) + .setTreasuryAccountId(operatorId) + .setTokenType(TokenType.NonFungibleUnique); + + const createTokenTxResponse = await createTokenTx.execute(client); + const createTokenTxReceipt = + await createTokenTxResponse.getReceipt(client); + const tokenId = createTokenTxReceipt.tokenId; + + const tokenMintTx = new TokenMintTransaction() + .setMetadata(generateMetadataList(metadata, nftCount)) + .setTokenId(tokenId) + .freezeWith(client); + + const tokenMintResponse = await ( + await tokenMintTx.sign(supplyKey) + ).execute(client); + const tokenMintReceipt = await tokenMintResponse.getReceipt(client); + const serials = tokenMintReceipt.serials; + + const metadatas = await geNftsMetadata(client, tokenId, serials); + expect( + metadatas.every( + (mt) => mt === Buffer.from(metadata).toString("hex"), + ), + ).to.be.true; + + await ( + await ( + await new TokenNftsUpdateTransaction() + .setTokenId(tokenId) + .setSerialNumbers([serials[0], serials[1]]) + .setMetadata(newMetadata) + .freezeWith(client) + .sign(metadataKey) + ).execute(client) + ).getReceipt(client); + + const newMetadatas = await geNftsMetadata(client, tokenId, serials); + expect( + newMetadatas.map( + (mt) => mt === Buffer.from(newMetadata).toString("hex"), + ), + ).to.deep.eql([true, true, false, false]); }); - it("cannot update the NFT metadata if the metadataKey is missing", async function () { + it("should NOT update the NFT's metadata", async function () { + this.timeout(120000); + + const createTokenTx = new TokenCreateTransaction() + .setTokenName(tokenName) + .setTokenSymbol(tokenSymbol) + .setAdminKey(operatorKey) + .setSupplyKey(supplyKey) + .setMetadataKey(metadataKey) + .setTreasuryAccountId(operatorId) + .setTokenType(TokenType.NonFungibleUnique); + + const createTokenTxResponse = await createTokenTx.execute(client); + const createTokenTxReceipt = + await createTokenTxResponse.getReceipt(client); + const tokenId = createTokenTxReceipt.tokenId; + + const tokenMintTx = new TokenMintTransaction() + .setMetadata(generateMetadataList(metadata, nftCount)) + .setTokenId(tokenId) + .freezeWith(client); + + const tokenMintResponse = await ( + await tokenMintTx.sign(supplyKey) + ).execute(client); + const tokenMintReceipt = await tokenMintResponse.getReceipt(client); + const serials = tokenMintReceipt.serials; + + const metadatas = await geNftsMetadata(client, tokenId, serials); + expect( + metadatas.every( + (mt) => mt === Buffer.from(metadata).toString("hex"), + ), + ).to.be.true; + + await ( + await ( + await new TokenNftsUpdateTransaction() + .setTokenId(tokenId) + .setSerialNumbers(serials) + .freezeWith(client) + .sign(metadataKey) + ).execute(client) + ).getReceipt(client); + + const sameMetadatas = await geNftsMetadata(client, tokenId, serials); + expect( + sameMetadatas.every( + (mt) => mt === Buffer.from(metadata).toString("hex"), + ), + ).to.be.true; + }); + + it("should earse the metadata of entire NFT collection", async function () { + this.timeout(120000); + + const createTokenTx = new TokenCreateTransaction() + .setTokenName(tokenName) + .setTokenSymbol(tokenSymbol) + .setAdminKey(operatorKey) + .setSupplyKey(supplyKey) + .setMetadataKey(metadataKey) + .setTreasuryAccountId(operatorId) + .setTokenType(TokenType.NonFungibleUnique); + + const createTokenTxResponse = await createTokenTx.execute(client); + const createTokenTxReceipt = + await createTokenTxResponse.getReceipt(client); + const tokenId = createTokenTxReceipt.tokenId; + + const tokenMintTx = new TokenMintTransaction() + .setMetadata(generateMetadataList(metadata, nftCount)) + .setTokenId(tokenId) + .freezeWith(client); + + const tokenMintResponse = await ( + await tokenMintTx.sign(supplyKey) + ).execute(client); + const tokenMintReceipt = await tokenMintResponse.getReceipt(client); + const serials = tokenMintReceipt.serials; + + const metadatas = await geNftsMetadata(client, tokenId, serials); + expect( + metadatas.every( + (mt) => mt === Buffer.from(metadata).toString("hex"), + ), + ).to.be.true; + + await ( + await ( + await new TokenNftsUpdateTransaction() + .setTokenId(tokenId) + .setMetadata([]) + .setSerialNumbers(serials) + .freezeWith(client) + .sign(metadataKey) + ).execute(client) + ).getReceipt(client); + + const sameMetadatas = await geNftsMetadata(client, tokenId, serials); + expect( + sameMetadatas.every( + (mt) => mt === Buffer.from(new Uint8Array()).toString("hex"), + ), + ).to.be.true; + }); + + it("should NOT update the NFTs metadata if the metadataKey is NOT set", async function () { this.timeout(120000); try { const createTokenTx = new TokenCreateTransaction() .setTokenName(tokenName) .setTokenSymbol(tokenSymbol) - .setMetadata(metadata) .setAdminKey(operatorKey) .setSupplyKey(supplyKey) .setTreasuryAccountId(operatorId) @@ -155,7 +270,7 @@ describe("TokenNftsUpdateTransaction", function () { const tokenId = createTokenTxReceipt.tokenId; const tokenMintTx = new TokenMintTransaction() - .setMetadata([metadata]) + .setMetadata(generateMetadataList(metadata, nftCount)) .setTokenId(tokenId) .freezeWith(client); @@ -164,36 +279,30 @@ describe("TokenNftsUpdateTransaction", function () { ).execute(client); const tokenMintReceipt = await tokenMintResponse.getReceipt(client); - const nftSerial = tokenMintReceipt.serials[0]; - const nftId = new NftId(tokenId, nftSerial); - - tokenNftsInfo = await new TokenNftInfoQuery() - .setNftId(nftId) - .execute(client); - nftInfo = tokenNftsInfo[0]; + const serials = tokenMintReceipt.serials; const tokenUpdateNftsTx = new TokenNftsUpdateTransaction() .setTokenId(tokenId) - .setSerialNumbers([nftSerial]) + .setSerialNumbers(serials) .setMetadata(newMetadata) .freezeWith(client); - const tokenUpdateTxResponse = await ( - await tokenUpdateNftsTx.sign(metadataKey) - ).execute(client); - await tokenUpdateTxResponse.getReceipt(client); + await ( + await ( + await tokenUpdateNftsTx.sign(metadataKey) + ).execute(client) + ).getReceipt(client); } catch (error) { expect(error.status).to.be.eql(Status.TokenHasNoMetadataKey); } }); - it("cannot update the NFT metadata when the transaction is not signed with metadataKey", async function () { + it("should NOT update the NFTs metadata when the transaction is not signed with the metadataKey", async function () { this.timeout(120000); try { const createTokenTx = new TokenCreateTransaction() .setTokenName(tokenName) .setTokenSymbol(tokenSymbol) - .setMetadata(metadata) .setAdminKey(operatorKey) .setMetadataKey(metadataKey) .setSupplyKey(supplyKey) @@ -206,7 +315,7 @@ describe("TokenNftsUpdateTransaction", function () { const tokenId = createTokenTxReceipt.tokenId; const tokenMintTx = new TokenMintTransaction() - .setMetadata([metadata]) + .setMetadata(generateMetadataList(metadata, nftCount)) .setTokenId(tokenId) .freezeWith(client); @@ -215,11 +324,11 @@ describe("TokenNftsUpdateTransaction", function () { ).execute(client); const tokenMintReceipt = await tokenMintResponse.getReceipt(client); - const nftSerial = tokenMintReceipt.serials[0]; + const serials = tokenMintReceipt.serials; const tokenUpdateNftsTx = new TokenNftsUpdateTransaction() .setTokenId(tokenId) - .setSerialNumbers([nftSerial]) + .setSerialNumbers(serials) .setMetadata(newMetadata) .freezeWith(client); @@ -233,3 +342,26 @@ describe("TokenNftsUpdateTransaction", function () { } }); }); + +function generateMetadataList(metadata, count) { + const list = []; + + for (let index = 0; index < count; index++) { + list.push(metadata); + } + return list; +} + +async function geNftsMetadata(client, tokenId, serials) { + const metadatas = []; + + for (let index = 0; index < serials.length; index++) { + const nftId = new NftId(tokenId, serials[index]); + const nftInfo = await new TokenNftInfoQuery() + .setNftId(nftId) + .execute(client); + metadatas.push(nftInfo[0].metadata.toString("hex")); + } + + return metadatas; +} diff --git a/test/integration/TokenUpdateIntegrationTest.js b/test/integration/TokenUpdateIntegrationTest.js index 41a530a18..3fa65a41a 100644 --- a/test/integration/TokenUpdateIntegrationTest.js +++ b/test/integration/TokenUpdateIntegrationTest.js @@ -31,10 +31,6 @@ describe("TokenUpdate", function () { const key3 = PrivateKey.generateED25519(); const key4 = PrivateKey.generateED25519(); const key5 = PrivateKey.generateED25519(); - const metadataKey = PrivateKey.generateED25519(); - const newMetadataKey = PrivateKey.generateED25519(); - const metadata = new Uint8Array(1); - const newMetadata = new Uint8Array(2); const response = await new TokenCreateTransaction() .setTokenName("ffff") @@ -49,8 +45,6 @@ describe("TokenUpdate", function () { .setSupplyKey(key4) .setFreezeDefault(false) .setPauseKey(key5) - .setMetadataKey(metadataKey) - .setMetadata(metadata) .execute(env.client); const token = (await response.getReceipt(env.client)).tokenId; @@ -58,6 +52,11 @@ describe("TokenUpdate", function () { let info = await new TokenInfoQuery() .setTokenId(token) .execute(env.client); + + expect(info.tokenId.toString()).to.eql(token.toString()); + expect(info.name).to.eql("ffff"); + expect(info.symbol).to.eql("F"); + expect(info.decimals).to.eql(3); expect(info.totalSupply.toInt()).to.eql(1000000); expect(info.treasuryAccountId.toString()).to.be.equal( operatorId.toString(), @@ -78,16 +77,12 @@ describe("TokenUpdate", function () { expect(info.autoRenewPeriod).to.be.not.null; expect(info.autoRenewPeriod.seconds.toInt()).to.be.eql(7776000); expect(info.expirationTime).to.be.not.null; - expect(info.metadataKey.toString()).to.eql(metadataKey.toString()); - expect(info.metadata).to.eql(metadata); await ( await new TokenUpdateTransaction() .setTokenId(token) .setTokenName("aaaa") .setTokenSymbol("A") - .setMetadataKey(newMetadataKey) - .setMetadata(newMetadata) .execute(env.client) ).getReceipt(env.client); @@ -116,8 +111,6 @@ describe("TokenUpdate", function () { expect(info.autoRenewPeriod).to.be.not.null; expect(info.autoRenewPeriod.seconds.toInt()).to.be.eql(7776000); expect(info.expirationTime).to.be.not.null; - expect(info.metadataKey.toString()).to.eql(newMetadataKey.toString()); - expect(info.metadata).to.eql(newMetadata); }); it("should be able to update treasury", async function () { @@ -467,6 +460,238 @@ describe("TokenUpdate", function () { } }); + it("should update token metadata", async function () { + this.timeout(120000); + + const operatorId = env.operatorId; + const operatorKey = env.operatorKey; + const metadataKey = PrivateKey.generateECDSA(); + const supplyKey = PrivateKey.generateECDSA(); + const metadata = new Uint8Array([1]); + const newMetadata = new Uint8Array([1, 2]); + let tokeInfo; + + const tokenCreateTx = new TokenCreateTransaction() + .setTokenName("Test") + .setTokenSymbol("T") + .setSupplyKey(supplyKey) + .setTokenType(TokenType.NonFungibleUnique) + .setTreasuryAccountId(operatorId) + .setAdminKey(operatorKey) + .setMetadata(metadata) + .setMetadataKey(metadataKey); + + const tokenCreateTxresponse = await tokenCreateTx.execute(env.client); + const tokenCreateTxReceipt = await tokenCreateTxresponse.getReceipt( + env.client, + ); + const tokenId = tokenCreateTxReceipt.tokenId; + + tokeInfo = await new TokenInfoQuery() + .setTokenId(tokenId) + .execute(env.client); + expect(tokeInfo.metadata).to.eql(metadata); + + const tokenUpdateTx = new TokenUpdateTransaction() + .setTokenId(tokenId) + .setMetadata(newMetadata) + .freezeWith(env.client); + + await ( + await (await tokenUpdateTx.sign(metadataKey)).execute(env.client) + ).getReceipt(env.client); + + tokeInfo = await new TokenInfoQuery() + .setTokenId(tokenId) + .execute(env.client); + + expect(tokeInfo.metadata).to.eql(newMetadata); + }); + + it("should NOT update token metadata", async function () { + this.timeout(120000); + + const operatorId = env.operatorId; + const operatorKey = env.operatorKey; + const metadataKey = PrivateKey.generateECDSA(); + const supplyKey = PrivateKey.generateECDSA(); + const metadata = new Uint8Array([1]); + let tokeInfo; + + const tokenCreateTx = new TokenCreateTransaction() + .setTokenName("Test") + .setTokenSymbol("T") + .setSupplyKey(supplyKey) + .setTokenType(TokenType.NonFungibleUnique) + .setTreasuryAccountId(operatorId) + .setAdminKey(operatorKey) + .setMetadata(metadata) + .setMetadataKey(metadataKey); + + const tokenCreateTxresponse = await tokenCreateTx.execute(env.client); + const tokenCreateTxReceipt = await tokenCreateTxresponse.getReceipt( + env.client, + ); + const tokenId = tokenCreateTxReceipt.tokenId; + + tokeInfo = await new TokenInfoQuery() + .setTokenId(tokenId) + .execute(env.client); + expect(tokeInfo.metadata).to.eql(metadata); + + const tokenUpdateTx = new TokenUpdateTransaction() + .setTokenId(tokenId) + .freezeWith(env.client); + + await ( + await (await tokenUpdateTx.sign(metadataKey)).execute(env.client) + ).getReceipt(env.client); + + tokeInfo = await new TokenInfoQuery() + .setTokenId(tokenId) + .execute(env.client); + + expect(tokeInfo.metadata).to.eql(metadata); + }); + + it("should earse token metadata", async function () { + this.timeout(120000); + + const operatorId = env.operatorId; + const operatorKey = env.operatorKey; + const metadataKey = PrivateKey.generateECDSA(); + const supplyKey = PrivateKey.generateECDSA(); + const metadata = new Uint8Array([1]); + const newMetadata = new Uint8Array(); + let tokeInfo; + + const tokenCreateTx = new TokenCreateTransaction() + .setTokenName("Test") + .setTokenSymbol("T") + .setSupplyKey(supplyKey) + .setTokenType(TokenType.NonFungibleUnique) + .setTreasuryAccountId(operatorId) + .setAdminKey(operatorKey) + .setMetadata(metadata) + .setMetadataKey(metadataKey); + + const tokenCreateTxresponse = await tokenCreateTx.execute(env.client); + const tokenCreateTxReceipt = await tokenCreateTxresponse.getReceipt( + env.client, + ); + const tokenId = tokenCreateTxReceipt.tokenId; + + tokeInfo = await new TokenInfoQuery() + .setTokenId(tokenId) + .execute(env.client); + expect(tokeInfo.metadata).to.eql(metadata); + + const tokenUpdateTx = new TokenUpdateTransaction() + .setTokenId(tokenId) + .setMetadata(newMetadata) + .freezeWith(env.client); + + await ( + await (await tokenUpdateTx.sign(metadataKey)).execute(env.client) + ).getReceipt(env.client); + + tokeInfo = await new TokenInfoQuery() + .setTokenId(tokenId) + .execute(env.client); + + expect(tokeInfo.metadata).to.eql(newMetadata); + }); + + /** @summary Currently the transaction passing even when the transaction is signed with wrong metadata key */ + it("should NOT update token metadata if the transaction is not signed with metadata key", async function () { + this.timeout(120000); + + const operatorId = env.operatorId; + const operatorKey = env.operatorKey; + const metadataKey = PrivateKey.generateECDSA(); + const wrongMetadataKey = PrivateKey.generateECDSA(); + const supplyKey = PrivateKey.generateECDSA(); + const metadata = new Uint8Array([1]); + const newMetadata = new Uint8Array([1, 2]); + + try { + const tokenCreateTx = new TokenCreateTransaction() + .setTokenName("Test") + .setTokenSymbol("T") + .setSupplyKey(supplyKey) + .setTokenType(TokenType.NonFungibleUnique) + .setTreasuryAccountId(operatorId) + .setAdminKey(operatorKey) + .setMetadata(metadata) + .setMetadataKey(metadataKey); + + const tokenCreateTxresponse = await tokenCreateTx.execute( + env.client, + ); + const tokenCreateTxReceipt = await tokenCreateTxresponse.getReceipt( + env.client, + ); + const tokenId = tokenCreateTxReceipt.tokenId; + + const tokenUpdateTx = new TokenUpdateTransaction() + .setTokenId(tokenId) + .setMetadata(newMetadata) + .freezeWith(env.client); + + await ( + await ( + await tokenUpdateTx.sign(wrongMetadataKey) + ).execute(env.client) + ).getReceipt(env.client); + } catch (error) { + expect(error.status).to.be.eql(Status.InvalidSignature); + } + }); + + /** @summary Currently the transaction passing even when the metadata key is NOT set */ + it("should NOT update token metadata if the metadata key is NOT set", async function () { + this.timeout(120000); + + const operatorId = env.operatorId; + const operatorKey = env.operatorKey; + const metadataKey = PrivateKey.generateECDSA(); + const supplyKey = PrivateKey.generateECDSA(); + const metadata = new Uint8Array([1]); + const newMetadata = new Uint8Array([1, 2]); + + try { + const tokenCreateTx = new TokenCreateTransaction() + .setTokenName("Test") + .setTokenSymbol("T") + .setSupplyKey(supplyKey) + .setTokenType(TokenType.NonFungibleUnique) + .setTreasuryAccountId(operatorId) + .setAdminKey(operatorKey) + .setMetadata(metadata); + + const tokenCreateTxresponse = await tokenCreateTx.execute( + env.client, + ); + const tokenCreateTxReceipt = await tokenCreateTxresponse.getReceipt( + env.client, + ); + const tokenId = tokenCreateTxReceipt.tokenId; + + const tokenUpdateTx = new TokenUpdateTransaction() + .setTokenId(tokenId) + .setMetadata(newMetadata) + .freezeWith(env.client); + + await ( + await ( + await tokenUpdateTx.sign(metadataKey) + ).execute(env.client) + ).getReceipt(env.client); + } catch (error) { + expect(error.status).to.be.eql(Status.InvalidSignature); + } + }); + after(async function () { await env.close(); });