Skip to content

Commit

Permalink
Keypair.isValidPublicKey close stellar#62
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekn committed Feb 12, 2016
1 parent 07f43fb commit af10f2a
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 15 deletions.
7 changes: 5 additions & 2 deletions src/account.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import BigNumber from 'bignumber.js';
import {isString} from 'lodash';
import {Keypair} from './keypair';
import {decodeCheck} from "./strkey";

export class Account {
Expand All @@ -15,8 +16,8 @@ export class Account {
* @param {string} sequence current sequence number of the account
*/
constructor(accountId, sequence) {
if (!Account.isValidAccountId(accountId)) {
throw new Error('address is invalid');
if (!Keypair.isValidPublicKey(accountId)) {
throw new Error('accountId is invalid');
}
if (!isString(sequence)) {
throw new Error('sequence must be of type string');
Expand All @@ -29,8 +30,10 @@ export class Account {
* Returns true if the given accountId is a valid Stellar account ID.
* @param {string} accountId account ID to check
* @returns {boolean}
* @deprecated Use {@link Keypair.isValidPublicKey}
*/
static isValidAccountId(accountId) {
console.warn('Account.isValidAccountId is deprecated. Use Keypair.isValidPublicKey.')
try {
let decoded = decodeCheck("accountId", accountId);
if (decoded.length !== 32) {
Expand Down
3 changes: 1 addition & 2 deletions src/asset.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {default as xdr} from "./generated/stellar-xdr_generated";
import {Account} from "./account";
import {Keypair} from "./keypair";
import {encodeCheck} from "./strkey";
import {clone, padEnd, trimEnd} from 'lodash';
Expand All @@ -23,7 +22,7 @@ export class Asset {
if (String(code).toLowerCase() !== "xlm" && !issuer) {
throw new Error("Issuer cannot be null");
}
if (issuer && !Account.isValidAccountId(issuer)) {
if (issuer && !Keypair.isValidPublicKey(issuer)) {
throw new Error("Issuer is invalid");
}

Expand Down
17 changes: 17 additions & 0 deletions src/keypair.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ export class Keypair {
return this.fromRawSeed(seed);
}

/**
* Returns true if the given Stellar public key is valid.
* @param {string} publicKey public key to check
* @returns {boolean}
*/
static isValidPublicKey(publicKey) {
try {
let decoded = strkey.decodeCheck("accountId", publicKey);
if (decoded.length !== 32) {
return false;
}
} catch (err) {
return false;
}
return true;
}

xdrAccountId() {
return new xdr.AccountId.keyTypeEd25519(this._publicKey);
}
Expand Down
17 changes: 8 additions & 9 deletions src/operation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {default as xdr} from "./generated/stellar-xdr_generated";
import {Account} from "./account";
import {Keypair} from "./keypair";
import {UnsignedHyper, Hyper} from "js-xdr";
import {hash} from "./hashing";
Expand Down Expand Up @@ -40,7 +39,7 @@ export class Operation {
* @returns {xdr.CreateAccountOp}
*/
static createAccount(opts) {
if (!Account.isValidAccountId(opts.destination)) {
if (!Keypair.isValidPublicKey(opts.destination)) {
throw new Error("destination is invalid");
}
if (!this.isValidAmount(opts.startingBalance)) {
Expand Down Expand Up @@ -68,7 +67,7 @@ export class Operation {
* @returns {xdr.PaymentOp}
*/
static payment(opts) {
if (!Account.isValidAccountId(opts.destination)) {
if (!Keypair.isValidPublicKey(opts.destination)) {
throw new Error("destination is invalid");
}
if (!opts.asset) {
Expand Down Expand Up @@ -112,7 +111,7 @@ export class Operation {
if (!this.isValidAmount(opts.sendMax)) {
throw new TypeError('sendMax argument must be of type String and represent a positive number');
}
if (!Account.isValidAccountId(opts.destination)) {
if (!Keypair.isValidPublicKey(opts.destination)) {
throw new Error("destination is invalid");
}
if (!opts.destAsset) {
Expand Down Expand Up @@ -191,7 +190,7 @@ export class Operation {
* @returns {xdr.AllowTrustOp}
*/
static allowTrust(opts) {
if (!Account.isValidAccountId(opts.trustor)) {
if (!Keypair.isValidPublicKey(opts.trustor)) {
throw new Error("trustor is invalid");
}
let attributes = {};
Expand Down Expand Up @@ -242,7 +241,7 @@ export class Operation {
let attributes = {};

if (opts.inflationDest) {
if (!Account.isValidAccountId(opts.inflationDest)) {
if (!Keypair.isValidPublicKey(opts.inflationDest)) {
throw new Error("inflationDest is invalid");
}
attributes.inflationDest = Keypair.fromAccountId(opts.inflationDest).xdrAccountId();
Expand Down Expand Up @@ -274,7 +273,7 @@ export class Operation {
opts.signer.pubKey = opts.signer.address;
}

if (!Account.isValidAccountId(opts.signer.pubKey)) {
if (!Keypair.isValidPublicKey(opts.signer.pubKey)) {
throw new Error("signer.pubKey is invalid");
}

Expand Down Expand Up @@ -384,7 +383,7 @@ export class Operation {
*/
static accountMerge(opts) {
let opAttributes = {};
if (!Account.isValidAccountId(opts.destination)) {
if (!Keypair.isValidPublicKey(opts.destination)) {
throw new Error("destination is invalid");
}
opAttributes.body = xdr.OperationBody.accountMerge(
Expand All @@ -410,7 +409,7 @@ export class Operation {

static setSourceAccount(opAttributes, opts) {
if (opts.source) {
if (!Account.isValidAccountId(opts.source)) {
if (!Keypair.isValidPublicKey(opts.source)) {
throw new Error("Source address is invalid");
}
opAttributes.sourceAccount = Keypair.fromAccountId(opts.source).xdrAccountId();
Expand Down
4 changes: 2 additions & 2 deletions test/unit/account_test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

describe('Account.constructor', function() {
it("fails to create Account object from an invalid address", function() {
expect(() => new StellarBase.Account('GBBB')).to.throw(/address is invalid/);
expect(() => new StellarBase.Account('GBBB')).to.throw(/accountId is invalid/);
});

it("fails to create Account object from an invalid sequence number", function() {
Expand Down Expand Up @@ -56,7 +56,7 @@ describe('Account.isValidAccountId', function() {

});

describe('Account.isValidAccountId', function() {
describe('Account.incrementSequenceNumber', function() {
it("correctly increments the sequence number", function() {
let account = new StellarBase.Account('GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB', '100');
account.incrementSequenceNumber();
Expand Down
41 changes: 41 additions & 0 deletions test/unit/keypair_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,44 @@ describe('Keypair.random', function() {
});

});

describe('Keypair.isValidPublicKey', function() {

it("returns true for valid public key", function() {
var keys = [
'GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB',
'GB7KKHHVYLDIZEKYJPAJUOTBE5E3NJAXPSDZK7O6O44WR3EBRO5HRPVT',
'GD6WVYRVID442Y4JVWFWKWCZKB45UGHJAABBJRS22TUSTWGJYXIUR7N2',
'GBCG42WTVWPO4Q6OZCYI3D6ZSTFSJIXIS6INCIUF23L6VN3ADE4337AP',
'GDFX463YPLCO2EY7NGFMI7SXWWDQAMASGYZXCG2LATOF3PP5NQIUKBPT',
'GBXEODUMM3SJ3QSX2VYUWFU3NRP7BQRC2ERWS7E2LZXDJXL2N66ZQ5PT',
'GAJHORKJKDDEPYCD6URDFODV7CVLJ5AAOJKR6PG2VQOLWFQOF3X7XLOG',
'GACXQEAXYBEZLBMQ2XETOBRO4P66FZAJENDHOQRYPUIXZIIXLKMZEXBJ',
'GDD3XRXU3G4DXHVRUDH7LJM4CD4PDZTVP4QHOO4Q6DELKXUATR657OZV',
'GDTYVCTAUQVPKEDZIBWEJGKBQHB4UGGXI2SXXUEW7LXMD4B7MK37CWLJ'
];

for (var i in keys) {
expect(StellarBase.Keypair.isValidPublicKey(keys[i])).to.be.true;
}
});

it("returns false for invalid public key", function() {
var keys = [
'GBPXX0A5N4JYPESHAADMQKBPWZWQDQ64ZV6ZL2S3LAGW4SY7NTCMWIVL',
'GCFZB6L25D26RQFDWSSBDEYQ32JHLRMTT44ZYE3DZQUTYOL7WY43PLBG++',
'GADE5QJ2TY7S5ZB65Q43DFGWYWCPHIYDJ2326KZGAGBN7AE5UY6JVDRRA',
'GB6OWYST45X57HCJY5XWOHDEBULB6XUROWPIKW77L5DSNANBEQGUPADT2',
'GB6OWYST45X57HCJY5XWOHDEBULB6XUROWPIKW77L5DSNANBEQGUPADT2T',
'GDXIIZTKTLVYCBHURXL2UPMTYXOVNI7BRAEFQCP6EZCY4JLKY4VKFNLT',
'gWRYUerEKuz53tstxEuR3NCkiQDcV4wzFHmvLnZmj7PUqxW2wt',
'test',
'g4VPBPrHZkfE8CsjuG2S4yBQNd455UWmk' // Old network key
];

for (var i in keys) {
expect(StellarBase.Keypair.isValidPublicKey(keys[i])).to.be.false;
}
});

});

0 comments on commit af10f2a

Please sign in to comment.