Skip to content

Commit 3d5568b

Browse files
committed
Merge pull request stellar#44 from stellar/decode-check-update
Add more checks to decodeCheck function
2 parents 6481755 + a1ed676 commit 3d5568b

7 files changed

+45
-9
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@
6262
},
6363
"dependencies": {
6464
"babel-runtime": "^4.7.16",
65+
"base32.js": "~0.1.0",
6566
"crc": "^3.3.0",
6667
"ed25519": "0.0.4",
6768
"js-xdr": "0.0.11",
6869
"karma": "^0.13.9",
6970
"lodash": "^3.6.0",
7071
"sha.js": "^2.3.6",
71-
"thirty-two": "^0.0.2",
7272
"tweetnacl": "^0.13.0"
7373
}
7474
}

src/account.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ export class Account {
1010
*/
1111
static isValidAddress(address) {
1212
try {
13-
decodeCheck("accountId", address);
13+
let decoded = decodeCheck("accountId", address);
14+
if (decoded.length !== 32) {
15+
return false;
16+
}
1417
} catch(err) {
1518
return false;
1619
}

src/keypair.js

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ export class Keypair {
3838

3939
static fromAddress(address) {
4040
let publicKey = strkey.decodeCheck("accountId", address);
41+
if (publicKey.length !== 32) {
42+
throw new Error('Invalid Stellar address');
43+
}
4144
return new this({publicKey});
4245
}
4346

src/strkey.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1-
import base32 from "thirty-two";
1+
import base32 from "base32.js";
22
import crc from "crc";
3-
import {isUndefined, isNull} from "lodash";
3+
import {contains, isUndefined, isNull, isString} from "lodash";
44

55
const versionBytes = {
66
accountId: 0x30,
77
seed: 0x90
88
};
99

1010
export function decodeCheck(versionByteName, encoded) {
11+
if (!isString(encoded)) {
12+
throw new TypeError('encoded argument must be of type String');
13+
}
14+
1115
let decoded = base32.decode(encoded);
1216
let versionByte = decoded[0];
1317
let payload = decoded.slice(0, -2);
1418
let data = payload.slice(1);
1519
let checksum = decoded.slice(-2);
1620

21+
if (encoded != base32.encode(decoded)) {
22+
throw new Error('invalid encoded string');
23+
}
24+
1725
let expectedVersion = versionBytes[versionByteName];
1826

1927
if (isUndefined(expectedVersion)) {
@@ -30,10 +38,6 @@ export function decodeCheck(versionByteName, encoded) {
3038
throw new Error(`invalid checksum`);
3139
}
3240

33-
if (versionByteName === 'accountId' && decoded.length !== 35) {
34-
throw new Error(`Decoded address length is invalid. Expected 35, got ${decoded.length}`);
35-
}
36-
3741
return new Buffer(data);
3842
}
3943

@@ -54,7 +58,7 @@ export function encodeCheck(versionByteName, data) {
5458
let checksum = calculateChecksum(payload);
5559
let unencoded = Buffer.concat([payload, checksum]);
5660

57-
return base32.encode(unencoded).toString();
61+
return base32.encode(unencoded);
5862
}
5963

6064
function calculateChecksum(payload) {

test/unit/account_test.js

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ describe('Account.isValidAddress', function() {
2323

2424
it("returns false for invalid address", function() {
2525
var addresses = [
26+
'GBPXX0A5N4JYPESHAADMQKBPWZWQDQ64ZV6ZL2S3LAGW4SY7NTCMWIVL',
27+
'GCFZB6L25D26RQFDWSSBDEYQ32JHLRMTT44ZYE3DZQUTYOL7WY43PLBG++',
28+
'GADE5QJ2TY7S5ZB65Q43DFGWYWCPHIYDJ2326KZGAGBN7AE5UY6JVDRRA',
29+
'GB6OWYST45X57HCJY5XWOHDEBULB6XUROWPIKW77L5DSNANBEQGUPADT2',
30+
'GB6OWYST45X57HCJY5XWOHDEBULB6XUROWPIKW77L5DSNANBEQGUPADT2T',
2631
'GDXIIZTKTLVYCBHURXL2UPMTYXOVNI7BRAEFQCP6EZCY4JLKY4VKFNLT',
2732
'gWRYUerEKuz53tstxEuR3NCkiQDcV4wzFHmvLnZmj7PUqxW2wt',
2833
'test',

test/unit/keypair_test.js

+6
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,13 @@ describe('Keypair.fromAddress', function() {
106106
expect(() => StellarBase.Keypair.fromAddress("hel0")).to.throw()
107107
expect(() => StellarBase.Keypair.fromAddress("masterpassphrasemasterpassphrase")).to.throw()
108108
expect(() => StellarBase.Keypair.fromAddress("sfyjodTxbwLtRToZvi6yQ1KnpZriwTJ7n6nrASFR6goRviCU3Ff")).to.throw()
109+
});
109110

111+
it("throws an error if the address isn't 32 bytes", function() {
112+
expect(() => StellarBase.Keypair.fromAddress("masterpassphrasemasterpassphrase")).to.throw()
113+
expect(() => StellarBase.Keypair.fromAddress("masterpassphrasemasterpassphrase")).to.throw()
114+
expect(() => StellarBase.Keypair.fromAddress(null)).to.throw()
115+
expect(() => StellarBase.Keypair.fromAddress()).to.throw()
110116
});
111117

112118
});

test/unit/strkey_test.js

+15
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ describe('StellarBase#decodeCheck', function() {
2121
expect(() => StellarBase.decodeCheck("accountId", "SBGWKM3CD4IL47QN6X54N6Y33T3JDNVI6AIJ6CD5IM47HG3IG4O36XCU")).to.throw(/invalid version/);
2222
});
2323

24+
it("throws an error when decoded data encodes to other string", function() {
25+
// accountId
26+
expect(() => StellarBase.decodeCheck("accountId", "GBPXX0A5N4JYPESHAADMQKBPWZWQDQ64ZV6ZL2S3LAGW4SY7NTCMWIVL")).to.throw(/invalid encoded string/);
27+
expect(() => StellarBase.decodeCheck("accountId", "GCFZB6L25D26RQFDWSSBDEYQ32JHLRMTT44ZYE3DZQUTYOL7WY43PLBG++")).to.throw(/invalid encoded string/);
28+
expect(() => StellarBase.decodeCheck("accountId", "GADE5QJ2TY7S5ZB65Q43DFGWYWCPHIYDJ2326KZGAGBN7AE5UY6JVDRRA")).to.throw(/invalid encoded string/);
29+
expect(() => StellarBase.decodeCheck("accountId", "GB6OWYST45X57HCJY5XWOHDEBULB6XUROWPIKW77L5DSNANBEQGUPADT2")).to.throw(/invalid encoded string/);
30+
expect(() => StellarBase.decodeCheck("accountId", "GB6OWYST45X57HCJY5XWOHDEBULB6XUROWPIKW77L5DSNANBEQGUPADT2T")).to.throw(/invalid encoded string/);
31+
// seed
32+
expect(() => StellarBase.decodeCheck("seed", "SB7OJNF5727F3RJUG5ASQJ3LUM44ELLNKW35ZZQDHMVUUQNGYW")).to.throw(/invalid encoded string/);
33+
expect(() => StellarBase.decodeCheck("seed", "SB7OJNF5727F3RJUG5ASQJ3LUM44ELLNKW35ZZQDHMVUUQNGYWMEGB2W2")).to.throw(/invalid encoded string/);
34+
expect(() => StellarBase.decodeCheck("seed", "SB7OJNF5727F3RJUG5ASQJ3LUM44ELLNKW35ZZQDHMVUUQNGYWMEGB2W2T")).to.throw(/invalid encoded string/);
35+
expect(() => StellarBase.decodeCheck("seed", "SCMB30FQCIQAWZ4WQTS6SVK37LGMAFJGXOZIHTH2PY6EXLP37G46H6DT")).to.throw(/invalid encoded string/);
36+
expect(() => StellarBase.decodeCheck("seed", "SAYC2LQ322EEHZYWNSKBEW6N66IRTDREEBUXXU5HPVZGMAXKLIZNM45H++")).to.throw(/invalid encoded string/);
37+
});
38+
2439
it("throws an error when the checksum is wrong", function() {
2540
expect(() => StellarBase.decodeCheck("accountId", "GBPXXOA5N4JYPESHAADMQKBPWZWQDQ64ZV6ZL2S3LAGW4SY7NTCMWIVT")).to.throw(/invalid checksum/);
2641
expect(() => StellarBase.decodeCheck("seed", "SBGWKM3CD4IL47QN6X54N6Y33T3JDNVI6AIJ6CD5IM47HG3IG4O36XCX")).to.throw(/invalid checksum/);

0 commit comments

Comments
 (0)