Skip to content

Commit 26eb453

Browse files
Merge pull request #7154 from BitGo/WIN-7458
fix(sdk-coin-iota): update transaction and block hash validation
2 parents 49c2061 + fd8c350 commit 26eb453

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

modules/sdk-coin-iota/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@bitgo/sdk-core": "^36.10.1",
4444
"@bitgo/statics": "^58.2.0",
4545
"@iota/iota-sdk": "^1.6.0",
46+
"@iota/bcs": "^1.2.0",
4647
"bignumber.js": "^9.1.2"
4748
},
4849
"devDependencies": {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export const IOTA_ADDRESS_LENGTH = 64;
2-
export const IOTA_TRANSACTION_ID_LENGTH = 64;
3-
export const IOTA_BLOCK_ID_LENGTH = 64;
4-
export const IOTA_SIGNATURE_LENGTH = 128;
2+
export const IOTA_TRANSACTION_DIGEST_LENGTH = 32;
3+
export const IOTA_BLOCK_DIGEST_LENGTH = 32;
4+
export const IOTA_SIGNATURE_LENGTH = 64;
55
export const ADDRESS_BYTES_LENGTH = 32;
66
export const AMOUNT_BYTES_LENGTH = 8;
77
export const SECONDS_PER_WEEK = 7 * 24 * 60 * 60; // 1 week in seconds

modules/sdk-coin-iota/src/lib/utils.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { BaseUtils, isValidEd25519PublicKey, isValidEd25519SecretKey } from '@bitgo/sdk-core';
1+
import { BaseUtils, isValidEd25519PublicKey, isValidEd25519SecretKey, isBase58 } from '@bitgo/sdk-core';
22
import {
33
IOTA_ADDRESS_LENGTH,
4-
IOTA_BLOCK_ID_LENGTH,
4+
IOTA_BLOCK_DIGEST_LENGTH,
55
IOTA_SIGNATURE_LENGTH,
6-
IOTA_TRANSACTION_ID_LENGTH,
6+
IOTA_TRANSACTION_DIGEST_LENGTH,
77
} from './constants';
88
import { Ed25519PublicKey } from '@iota/iota-sdk/keypairs/ed25519';
9+
import { fromBase64 } from '@iota/bcs';
910

1011
export class Utils implements BaseUtils {
1112
/** @inheritdoc */
@@ -15,7 +16,7 @@ export class Utils implements BaseUtils {
1516

1617
/** @inheritdoc */
1718
isValidBlockId(hash: string): boolean {
18-
return this.isValidHex(hash, IOTA_BLOCK_ID_LENGTH);
19+
return isBase58(hash, IOTA_BLOCK_DIGEST_LENGTH);
1920
}
2021

2122
/** @inheritdoc */
@@ -30,12 +31,12 @@ export class Utils implements BaseUtils {
3031

3132
/** @inheritdoc */
3233
isValidSignature(signature: string): boolean {
33-
return this.isValidHex(signature, IOTA_SIGNATURE_LENGTH);
34+
return fromBase64(signature).length === IOTA_SIGNATURE_LENGTH;
3435
}
3536

3637
/** @inheritdoc */
3738
isValidTransactionId(txId: string): boolean {
38-
return this.isValidHex(txId, IOTA_TRANSACTION_ID_LENGTH);
39+
return isBase58(txId, IOTA_TRANSACTION_DIGEST_LENGTH);
3940
}
4041

4142
isValidHex(value: string, length: number): boolean {

modules/sdk-coin-iota/test/unit/utils.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import utils from '../../src/lib/utils';
44

55
describe('Iota util library', function () {
66
describe('isValidAddress', function () {
7-
it('should succeed to validate raw transactoin', function () {
7+
it('should succeed to validate addresses', function () {
88
for (const address of testData.addresses.validAddresses) {
99
should.equal(utils.isValidAddress(address), true);
1010
}
@@ -28,4 +28,28 @@ describe('Iota util library', function () {
2828
// without 0x prefix
2929
should.equal(true, utils.isValidPublicKey('b2051899478edeb36a79d1d16dfec56dc3a6ebd29fbbbb4a4ef2dfaf46043355'));
3030
});
31+
32+
it('is valid tx id', function () {
33+
should.equal(
34+
false,
35+
utils.isValidTransactionId('0xff86b121181a43d03df52e8930785af3dda944ec87654cdba3a378ff518cd75b')
36+
);
37+
should.equal(false, utils.isValidTransactionId('BftEk3BeKUWTj9uzVGntd4Ka16QZG8hUnr6KsAb7q7b53t'));
38+
should.equal(true, utils.isValidTransactionId('BftEk3BeKUWTj9uzVGntd4Ka16QZG8hUnr6KsAb7q7bt'));
39+
});
40+
41+
it('is valid block id', function () {
42+
should.equal(false, utils.isValidBlockId('0x9ac6a0c313c4a0563a169dad29f1d018647683be54a314ed229a2693293dfc98'));
43+
should.equal(false, utils.isValidBlockId('GZXZvvLS3ZnuE4E9CxQJJ2ij5xeNsvUXdAK56VrPCQKrPz'));
44+
should.equal(true, utils.isValidBlockId('GZXZvvLS3ZnuE4E9CxQJJ2ij5xeNsvUXdAKVrPCQKrPz'));
45+
});
46+
47+
it('is valid signature', function () {
48+
should.equal(false, utils.isValidSignature('0x9ac6a0c313c4a0563a169dad29f1d018647683be54a314ed229a2693293dfc98'));
49+
should.equal(false, utils.isValidSignature('goppBTDgLuBbcU5tP90n3igvZGHmcE23HCoxLfdJwOCcbyztVh9r0TPacJRXmjZ6'));
50+
should.equal(
51+
true,
52+
utils.isValidSignature('iXrcUjgQgpYUsa7O90KZicdTmIdJSjB99+tJW6l6wPCqI/lUTou6sQ2sLoZgC0n4qQKX+vFDz+lBIXl7J/ZgCg==')
53+
);
54+
});
3155
});

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2971,7 +2971,7 @@
29712971
resolved "https://registry.npmjs.org/@inquirer/type/-/type-3.0.8.tgz#efc293ba0ed91e90e6267f1aacc1c70d20b8b4e8"
29722972
integrity sha512-lg9Whz8onIHRthWaN1Q9EGLa/0LFJjyM8mEUbL1eTi6yMGvBf8gvyDLtxSXztQsxMvhxxNpJYrwa1YHdq+w4Jw==
29732973

2974-
"@iota/bcs@1.2.0":
2974+
"@iota/bcs@1.2.0", "@iota/bcs@^1.2.0":
29752975
version "1.2.0"
29762976
resolved "https://registry.npmjs.org/@iota/bcs/-/bcs-1.2.0.tgz"
29772977
integrity sha512-QdRSR0KpJ87tdjVNmM/j0+0DvE0aTxHIa02337iluaOsMqtJ8OdgUCfSyLduC/3qS+8tJE+UB1KOw55tF+sN2w==

0 commit comments

Comments
 (0)