Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: _fromProtobuf functions where google primitive wrappers used #2657

Merged
18 changes: 11 additions & 7 deletions src/account/AccountUpdateTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ export default class AccountUpdateTransaction extends Transaction {
* @param {object} props
* @param {AccountId} [props.accountId]
* @param {Key} [props.key]
* @param {boolean} [props.receiverSignatureRequired]
* @param {?boolean} [props.receiverSignatureRequired]
* @param {AccountId} [props.proxyAccountId]
* @param {Duration | Long | number} [props.autoRenewPeriod]
* @param {Timestamp | Date} [props.expirationTime]
* @param {string} [props.accountMemo]
* @param {?string} [props.accountMemo]
* @param {Long | number} [props.maxAutomaticTokenAssociations]
* @param {Key} [props.aliasKey]
* @param {AccountId | string} [props.stakedAccountId]
* @param {Long | number} [props.stakedNodeId]
* @param {boolean} [props.declineStakingReward]
* @param {?boolean} [props.declineStakingReward]
*/
constructor(props = {}) {
super();
Expand Down Expand Up @@ -224,7 +224,10 @@ export default class AccountUpdateTransaction extends Transaction {
: undefined,
receiverSignatureRequired:
update.receiverSigRequiredWrapper != null
? update.receiverSigRequiredWrapper.value != null
? Object.hasOwn(
update.receiverSigRequiredWrapper,
"value",
)
? update.receiverSigRequiredWrapper.value
: undefined
: undefined,
Expand All @@ -248,13 +251,14 @@ export default class AccountUpdateTransaction extends Transaction {
: undefined,
accountMemo:
update.memo != null
? update.memo.value != null
? Object.hasOwn(update.memo, "value")
? update.memo.value
: undefined
: undefined,
maxAutomaticTokenAssociations:
update.maxAutomaticTokenAssociations != null &&
update.maxAutomaticTokenAssociations.value != null
update.maxAutomaticTokenAssociations.value != null &&
Object.hasOwn(update.maxAutomaticTokenAssociations, "value")
? Long.fromNumber(
update.maxAutomaticTokenAssociations.value,
)
Expand All @@ -269,7 +273,7 @@ export default class AccountUpdateTransaction extends Transaction {
: undefined,
declineStakingReward:
update.declineReward != null
? update.declineReward.value != null
? Object.hasOwn(update.declineReward, "value")
? update.declineReward.value
: undefined
: undefined,
Expand Down
8 changes: 5 additions & 3 deletions src/contract/ContractFunctionResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ export default class ContractFunctionResult {
: []
).map((contractId) => ContractId._fromProtobuf(contractId)),
evmAddress:
result.evmAddress != null && result.evmAddress.value != null
result.evmAddress != null &&
Object.hasOwn(result.evmAddress, "value") &&
result.evmAddress.value != null
? result.evmAddress.value
: null,
stateChanges: [],
Expand All @@ -213,8 +215,8 @@ export default class ContractFunctionResult {
),
signerNonce:
result.signerNonce != null
? result.signerNonce.value
? result.signerNonce.value
? Object.hasOwn(result.signerNonce, "value")
? result.signerNonce.value || null
: null
: null,
});
Expand Down
6 changes: 5 additions & 1 deletion src/contract/ContractUpdateTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,11 @@ export default class ContractUpdateTransaction extends Transaction {
}

let contractMemo = undefined;
if (update.memoWrapper != null && update.memoWrapper.value != null) {
if (
update.memoWrapper != null &&
Object.hasOwn(update.memoWrapper, "value") &&
update.memoWrapper.value != null
) {
contractMemo = update.memoWrapper.value;
}

Expand Down
4 changes: 2 additions & 2 deletions src/file/FileUpdateTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default class FileUpdateTransaction extends Transaction {
* @param {Key[] | KeyList} [props.keys]
* @param {Timestamp | Date} [props.expirationTime]
* @param {Uint8Array | string} [props.contents]
* @param {string} [props.fileMemo]
* @param {?string} [props.fileMemo]
*/
constructor(props = {}) {
super();
Expand Down Expand Up @@ -153,7 +153,7 @@ export default class FileUpdateTransaction extends Transaction {
contents: update.contents != null ? update.contents : undefined,
fileMemo:
update.memo != null
? update.memo.value != null
? Object.hasOwn(update.memo, "value")
? update.memo.value
: undefined
: undefined,
Expand Down
10 changes: 5 additions & 5 deletions src/node/NodeUpdateTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export default class NodeUpdateTransaction extends Transaction {
* @param {?string} [props.description]
* @param {Array<ServiceEndpoint>} [props.gossipEndpoints]
* @param {?Array<ServiceEndpoint>} [props.serviceEndpoints]
* @param {Uint8Array} [props.gossipCaCertificate]
* @param {Uint8Array} [props.grpcCertificateHash]
* @param {?Uint8Array} [props.gossipCaCertificate]
* @param {?Uint8Array} [props.grpcCertificateHash]
* @param {Key} [props.adminKey]
*/
constructor(props) {
Expand Down Expand Up @@ -164,7 +164,7 @@ export default class NodeUpdateTransaction extends Transaction {
: undefined,
description:
nodeUpdate.description != null
? nodeUpdate.description.value != null
? Object.hasOwn(nodeUpdate.description, "value")
? nodeUpdate.description.value
: undefined
: undefined,
Expand All @@ -182,13 +182,13 @@ export default class NodeUpdateTransaction extends Transaction {
: undefined,
gossipCaCertificate:
nodeUpdate.gossipCaCertificate != null
? nodeUpdate.gossipCaCertificate.value != null
? Object.hasOwn(nodeUpdate.gossipCaCertificate, "value")
? nodeUpdate.gossipCaCertificate.value
: undefined
: undefined,
grpcCertificateHash:
nodeUpdate.grpcCertificateHash != null
? nodeUpdate.grpcCertificateHash.value != null
? Object.hasOwn(nodeUpdate.grpcCertificateHash, "value")
? nodeUpdate.grpcCertificateHash.value
: undefined
: undefined,
Expand Down
8 changes: 4 additions & 4 deletions src/token/TokenTransfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ export default class TokenTransfer {
);
const expectedDecimals =
tokenTransfer.expectedDecimals != null
? /** @type {number | null } */ (
tokenTransfer.expectedDecimals.value
)
? Object.hasOwn(tokenTransfer.expectedDecimals, "value")
? tokenTransfer.expectedDecimals.value
: null
: null;

for (const transfer of tokenTransfer.transfers != null
Expand All @@ -114,7 +114,7 @@ export default class TokenTransfer {
transfer.accountID
),
),
expectedDecimals,
expectedDecimals: expectedDecimals || null,
amount:
transfer.amount != null
? transfer.amount
Expand Down
4 changes: 2 additions & 2 deletions src/token/TokenUpdateNftsTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class TokenUpdateNftsTransaction extends Transaction {
* @param {object} [props]
* @param {TokenId | string} [props.tokenId]
* @param {Long[]} [props.serialNumbers]
* @param {Uint8Array} [props.metadata]
* @param {?Uint8Array} [props.metadata]
*/
constructor(props = {}) {
super();
Expand Down Expand Up @@ -116,7 +116,7 @@ export default class TokenUpdateNftsTransaction extends Transaction {
: [],
metadata:
tokenUpdate.metadata != null
? tokenUpdate.metadata.value != null
? Object.hasOwn(tokenUpdate.metadata, "value")
? tokenUpdate.metadata.value
: undefined
: undefined,
Expand Down
22 changes: 13 additions & 9 deletions src/token/TokenUpdateTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export default class TokenUpdateTransaction extends Transaction {
/**
* @param {object} [props]
* @param {TokenId | string} [props.tokenId]
* @param {string} [props.tokenName]
* @param {string} [props.tokenSymbol]
* @param {?string} [props.tokenName]
* @param {?string} [props.tokenSymbol]
* @param {AccountId | string} [props.treasuryAccountId]
* @param {Key} [props.adminKey]
* @param {Key} [props.kycKey]
Expand All @@ -64,11 +64,11 @@ export default class TokenUpdateTransaction extends Transaction {
* @param {AccountId | string} [props.autoRenewAccountId]
* @param {Timestamp | Date} [props.expirationTime]
* @param {Duration | Long | number} [props.autoRenewPeriod]
* @param {string} [props.tokenMemo]
* @param {?string} [props.tokenMemo]
* @param {Key} [props.feeScheduleKey]
* @param {Key} [props.pauseKey]
* @param {Key} [props.metadataKey]
* @param {Uint8Array} [props.metadata]
* @param {?Uint8Array} [props.metadata]
* @param {TokenKeyValidation} [props.keyVerificationMode]
*/
constructor(props = {}) {
Expand Down Expand Up @@ -285,8 +285,12 @@ export default class TokenUpdateTransaction extends Transaction {
update.token != null
? TokenId._fromProtobuf(update.token)
: undefined,
tokenName: update.name != null ? update.name : undefined,
tokenSymbol: update.symbol != null ? update.symbol : undefined,
tokenName: Object.hasOwn(update, "name")
? update.name
: undefined,
tokenSymbol: Object.hasOwn(update, "symbol")
? update.symbol
: undefined,
treasuryAccountId:
update.treasury != null
? AccountId._fromProtobuf(update.treasury)
Expand Down Expand Up @@ -325,7 +329,7 @@ export default class TokenUpdateTransaction extends Transaction {
: undefined,
tokenMemo:
update.memo != null
? update.memo.value != null
? Object.hasOwn(update.memo, "value")
? update.memo.value
: undefined
: undefined,
Expand All @@ -343,7 +347,7 @@ export default class TokenUpdateTransaction extends Transaction {
: undefined,
metadata:
update.metadata != null
? update.metadata.value != null
? Object.hasOwn(update.metadata, "value")
? update.metadata.value
: undefined
: undefined,
Expand Down Expand Up @@ -763,7 +767,7 @@ export default class TokenUpdateTransaction extends Transaction {
_makeTransactionData() {
return {
token: this._tokenId != null ? this._tokenId._toProtobuf() : null,
name: this.tokenName,
name: this.tokenName != null ? this.tokenName : null,
symbol: this.tokenSymbol,
treasury:
this._treasuryAccountId != null
Expand Down
4 changes: 2 additions & 2 deletions src/topic/TopicUpdateTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default class TopicUpdateTransaction extends Transaction {
* @param {Key} [props.submitKey]
* @param {Duration | Long | number} [props.autoRenewPeriod]
* @param {AccountId | string} [props.autoRenewAccountId]
* @param {string} [props.topicMemo]
* @param {?string} [props.topicMemo]
* @param {Timestamp | Date} [props.expirationTime]
*/
constructor(props = {}) {
Expand Down Expand Up @@ -186,7 +186,7 @@ export default class TopicUpdateTransaction extends Transaction {
: undefined,
topicMemo:
update.memo != null
? update.memo.value != null
? Object.hasOwn(update.memo, "value")
? update.memo.value
: undefined
: undefined,
Expand Down
31 changes: 31 additions & 0 deletions test/unit/AccountUpdateTransaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { AccountUpdateTransaction } from "../../src/index.js";

describe("AccountUpdateTransaction", function () {
describe("deserialization of optional parameters", function () {
let tx, txBytes, tx2;

before(function () {
tx = new AccountUpdateTransaction();
txBytes = tx.toBytes();
tx2 = AccountUpdateTransaction.fromBytes(txBytes);
});

it("should deserialize with accountMemo being null", function () {
expect(tx.accountMemo).to.be.null;
expect(tx2.accountMemo).to.be.null;
});

it("should deserialize with declineReward, receiverSignatureRequired being null", function () {
expect(tx.declineStakingRewards).to.be.null;
expect(tx2.declineStakingRewards).to.be.null;

expect(tx.receiverSignatureRequired).to.be.null;
expect(tx2.receiverSignatureRequired).to.be.null;
});

it("should deserialize with maxAutomaticTokenAssociations being null", function () {
expect(tx.maxAutomaticTokenAssociations).to.be.null;
expect(tx2.maxAutomaticTokenAssociations).to.be.null;
});
});
});
13 changes: 13 additions & 0 deletions test/unit/ContractUpdateTransaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ContractUpdateTransaction } from "../../src/index.js";

describe("ContractUpdateTransaction", function () {
describe("deserialization of optional parameters", function () {
it("should deserialize with contractMemo being null", function () {
const tx = new ContractUpdateTransaction();
const tx2 = ContractUpdateTransaction.fromBytes(tx.toBytes());

expect(tx.contractMemo).to.be.null;
expect(tx2.contractMemo).to.be.null;
});
});
});
13 changes: 13 additions & 0 deletions test/unit/FileUpdateTransaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { FileUpdateTransaction } from "../../src/index.js";

describe("FileUpdateTransaction", function () {
describe("deserialization of optional parameters", function () {
it("should deserialize with fileMemo being null", function () {
const tx = new FileUpdateTransaction();
const tx2 = FileUpdateTransaction.fromBytes(tx.toBytes());

expect(tx.fileMemo).to.be.null;
expect(tx2.fileMemo).to.be.null;
});
});
});
20 changes: 20 additions & 0 deletions test/unit/NodeUpdateTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,24 @@ describe("NodeUpdateTransaction", function () {
expect(err).to.be.true;
});
});

describe("deserialization of optional parameters", function () {
it("should deserialize with gossipCaCertificate, grpcCertificateHash being null", function () {
const tx = new NodeUpdateTransaction();
const tx2 = NodeUpdateTransaction.fromBytes(tx.toBytes());

expect(tx.gossipCaCertificate).to.be.null;
expect(tx.certificateHash).to.be.null;
expect(tx2.gossipCaCertificate).to.be.null;
expect(tx2.certificateHash).to.be.null;
});

it("should deserialize with description being null", function () {
const tx = new NodeUpdateTransaction();
const tx2 = NodeUpdateTransaction.fromBytes(tx.toBytes());

expect(tx.description).to.be.null;
expect(tx2.description).to.be.null;
});
});
});
28 changes: 28 additions & 0 deletions test/unit/TokenTransfer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import TokenTransfer from "../../src/token/TokenTransfer.js";

describe("TokenTransfer", function () {
describe("_fromProtobuf with optional parameters", function () {
it("should deserialize with expectedDecimals being null", function () {
const transfer = new TokenTransfer({
tokenId: "0.0.123",
accountId: "0.0.456",
amount: 100,
expectedDecimals: null,
isApproved: true,
});

const transfersProtobuf = [
{
token: transfer.tokenId._toProtobuf(),
expectedDecimals: {},
transfers: [transfer._toProtobuf()],
},
];

const [transferFromProtobuf] =
TokenTransfer._fromProtobuf(transfersProtobuf);

expect(transferFromProtobuf.expectedDecimals).to.be.null;
});
});
});
8 changes: 8 additions & 0 deletions test/unit/TokenUpdateNftsTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ describe("TokenUpdateNftsTransaction", function () {
expect(transaction._serialNumbers).to.eql(serials);
expect(transaction._metadata).to.eql(metadata);
});

it("should _metadata equal to null", async function () {
const tx = new TokenUpdateNftsTransaction();
const tx2 = TokenUpdateNftsTransaction.fromBytes(tx.toBytes());

expect(tx._metadata).to.be.null;
expect(tx2._metadata).to.be.null;
});
});
Loading