Skip to content

Commit a1ed676

Browse files
committed
Add address length check to Keypair
1 parent 021f98d commit a1ed676

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

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

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
});

0 commit comments

Comments
 (0)