Skip to content

Commit cf059f2

Browse files
committed
refactor(sdk-coin-near): break down token enablement validation into separate functions
- Split validateTokenEnablementTransaction into individual validation functions - Create validateSigner, validateReceiver, validatePublicKey, validateActions functions - Optimize address validation using filter() for O(n) complexity - Improve code readability and maintainability with single-responsibility functions - Maintain same validation logic while improving code structure TICKET: WP-5782
1 parent b81a0d0 commit cf059f2

File tree

6 files changed

+66
-26
lines changed

6 files changed

+66
-26
lines changed

modules/sdk-coin-atom/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"@bitgo/sdk-api": "^1.68.3",
5454
"@bitgo/sdk-test": "^9.0.9",
5555
"@types/lodash": "^4.14.183",
56-
"axios": "^1.11.0"
56+
"axios": "^1.12.2"
5757
},
5858
"gitHead": "18e460ddf02de2dbf13c2aa243478188fb539f0c"
5959
}

modules/sdk-coin-near/src/near.ts

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,26 +1085,66 @@ export class Near extends BaseCoin {
10851085
const freshTxData = freshTx.toJson();
10861086
const originalTxData = transaction.toJson();
10871087

1088-
// Verify key transaction fields match to prevent tampering
1089-
if (
1090-
freshTxData.signerId !== originalTxData.signerId ||
1091-
freshTxData.receiverId !== originalTxData.receiverId ||
1092-
freshTxData.publicKey !== originalTxData.publicKey ||
1093-
freshTxData.actions.length !== originalTxData.actions.length
1094-
) {
1095-
throw new Error('Transaction hex does not match provided transaction');
1088+
// Validate each aspect of the transaction separately
1089+
this.validateSigner(originalTxData, freshTxData);
1090+
this.validateReceiver(originalTxData, freshTxData);
1091+
this.validatePublicKey(originalTxData, freshTxData);
1092+
this.validateActions(originalTxData, freshTxData);
1093+
this.validateAddresses(txParams, explainedTx);
1094+
}
1095+
1096+
//Validates that the signer ID matches between original and fresh transaction
1097+
private validateSigner(originalTxData: any, freshTxData: any): void {
1098+
if (originalTxData.signerId !== freshTxData.signerId) {
1099+
throw new Error(
1100+
`Error on token enablements: signers are not the same, expected ${originalTxData.signerId} but got ${freshTxData.signerId}`
1101+
);
10961102
}
1103+
}
10971104

1098-
// Validate addresses match between parameters and explained transaction
1099-
if (txParams.recipients && explainedTx.outputs) {
1100-
const expectedAddresses = txParams.recipients.map((r) => r.address);
1101-
const explainedAddresses = explainedTx.outputs.map((o) => o.address);
1105+
//Validates that the receiver ID matches between original and fresh transaction
1106+
private validateReceiver(originalTxData: any, freshTxData: any): void {
1107+
if (originalTxData.receiverId !== freshTxData.receiverId) {
1108+
throw new Error(
1109+
`Error on token enablements: receivers are not the same, expected ${originalTxData.receiverId} but got ${freshTxData.receiverId}`
1110+
);
1111+
}
1112+
}
11021113

1103-
for (const addr of expectedAddresses) {
1104-
if (!explainedAddresses.includes(addr)) {
1105-
throw new Error(`Address mismatch: ${addr}`);
1106-
}
1107-
}
1114+
//Validates that the public key matches between original and fresh transaction
1115+
private validatePublicKey(originalTxData: any, freshTxData: any): void {
1116+
if (originalTxData.publicKey !== freshTxData.publicKey) {
1117+
throw new Error(
1118+
`Error on token enablements: public keys are not the same, expected ${originalTxData.publicKey} but got ${freshTxData.publicKey}`
1119+
);
1120+
}
1121+
}
1122+
1123+
//Validates that the actions length matches between original and fresh transaction
1124+
private validateActions(originalTxData: any, freshTxData: any): void {
1125+
if (originalTxData.actions.length !== freshTxData.actions.length) {
1126+
throw new Error(
1127+
`Error on token enablements: actions length mismatch, expected ${originalTxData.actions.length} but got ${freshTxData.actions.length}`
1128+
);
1129+
}
1130+
}
1131+
1132+
//Validates that addresses match between parameters and explained transaction
1133+
private validateAddresses(txParams: VerifyTransactionOptions['txParams'], explainedTx: TransactionExplanation): void {
1134+
if (!txParams.recipients || !explainedTx.outputs) {
1135+
return;
1136+
}
1137+
1138+
if (txParams.recipients.length !== explainedTx.outputs.length) {
1139+
throw new Error('Error on token enablements: output count does not match recipients count');
1140+
}
1141+
1142+
const mismatchedAddresses = txParams.recipients
1143+
.filter((recipient, index) => recipient.address !== explainedTx.outputs[index].address)
1144+
.map((recipient) => recipient.address);
1145+
1146+
if (mismatchedAddresses.length > 0) {
1147+
throw new Error(`Address mismatch: ${mismatchedAddresses.join(', ')}`);
11081148
}
11091149
}
11101150
}

modules/sdk-coin-sui/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"@bitgo/sdk-api": "^1.68.3",
5757
"@bitgo/sdk-test": "^9.0.9",
5858
"@types/lodash": "^4.14.183",
59-
"axios": "^1.11.0",
59+
"axios": "^1.12.2",
6060
"debug": "^4.3.4"
6161
},
6262
"gitHead": "18e460ddf02de2dbf13c2aa243478188fb539f0c"

modules/utxo-lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"devDependencies": {
6868
"@types/fs-extra": "^9.0.12",
6969
"@types/node": "^22.15.29",
70-
"axios": "^1.11.0",
70+
"axios": "^1.12.2",
7171
"debug": "^3.1.0",
7272
"fs-extra": "^9.1.0"
7373
},

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
"web3-utils": "4.2.1",
8888
"@polkadot/api": "14.1.1",
8989
"elliptic": "^6.6.1",
90-
"axios": "^1.11.0",
90+
"axios": "^1.12.2",
9191
"canvg": "4.0.3",
9292
"**/stellar-sdk/**/bignumber.js": "4.1.0",
9393
"**/stellar-base/**/bignumber.js": "4.1.0",
@@ -135,7 +135,7 @@
135135
"test:prepare-release": "mocha --require tsx ./scripts/tests/prepareRelease/prepare-release-main.test.ts"
136136
},
137137
"dependencies": {
138-
"axios": "^1.11.0",
138+
"axios": "^1.12.2",
139139
"terser": "^5.14.2",
140140
"tmp": "^0.2.3"
141141
},

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7482,10 +7482,10 @@ aws4@^1.8.0:
74827482
resolved "https://registry.npmjs.org/aws4/-/aws4-1.13.2.tgz"
74837483
integrity sha512-lHe62zvbTB5eEABUVi/AwVh0ZKY9rMMDhmm+eeyuuUQbQ3+J+fONVQOZyj+DdrvD4BY33uYniyRJ4UJIaSKAfw==
74847484

7485-
axios@0.25.0, axios@0.27.2, axios@1.7.4, axios@^0.21.2, axios@^0.26.1, axios@^1.0.0, axios@^1.11.0, axios@^1.3.1:
7486-
version "1.11.0"
7487-
resolved "https://registry.npmjs.org/axios/-/axios-1.11.0.tgz"
7488-
integrity sha512-1Lx3WLFQWm3ooKDYZD1eXmoGO9fxYQjrycfHFC8P0sCfQVXyROp0p9PFWBehewBOdCwHc+f/b8I0fMto5eSfwA==
7485+
axios@0.25.0, axios@0.27.2, axios@1.7.4, axios@^0.21.2, axios@^0.26.1, axios@^1.0.0, axios@^1.12.2, axios@^1.3.1:
7486+
version "1.12.2"
7487+
resolved "https://registry.npmjs.org/axios/-/axios-1.12.2.tgz#6c307390136cf7a2278d09cec63b136dfc6e6da7"
7488+
integrity sha512-vMJzPewAlRyOgxV2dU0Cuz2O8zzzx9VYtbJOaBgXFeLc4IV/Eg50n4LowmehOOR61S8ZMpc2K5Sa7g6A4jfkUw==
74897489
dependencies:
74907490
follow-redirects "^1.15.6"
74917491
form-data "^4.0.4"

0 commit comments

Comments
 (0)