Skip to content

Commit 49f2909

Browse files
committed
Address checksum and validation plus tests
1 parent 9083186 commit 49f2909

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

Diff for: src/__tests__/util.spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,22 @@ describe('utils', () => {
189189
expect(res).toBeTruthy();
190190
});
191191
});
192+
193+
it('should return a valid checksummed address', () => {
194+
const checksummed = "0x7bB3b0E8A59f3f61d9BFf038f4AEB42Cae2eccE8";
195+
const actual = util.toChecksumAddress(checksummed.toLowerCase());
196+
expect(actual).toEqual(checksummed);
197+
});
198+
199+
it('should return true when a valid checksummed address is tested', () => {
200+
const checksummed = "0x7bB3b0E8A59f3f61d9BFf038f4AEB42Cae2eccE8";
201+
const actual = util.isValidChecksumAddress(checksummed);
202+
expect(actual).toBeTruthy();
203+
});
204+
205+
it('should return false when an invalid checksummed address is tested', () => {
206+
const bad = "0x7bB3b0E8A59f3f61d9BFf038f4AEB42Cae2eccE8".toLowerCase();
207+
const actual = util.isValidChecksumAddress( bad );
208+
expect(actual).toBeFalsy();
209+
});
192210
});

Diff for: src/util.ts

+39-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
import randomBytes from 'randombytes';
1111
import elliptic from 'elliptic';
1212
import hashjs from 'hash.js';
13-
import {isWebUri} from 'valid-url';
13+
import { isWebUri } from 'valid-url';
1414

1515
import * as schnorr from './schnorr';
16-
import {TxDetails} from './types';
16+
import { TxDetails } from './types';
1717

1818
const NUM_BYTES = 32;
1919
//const HEX_PREFIX = '0x';
@@ -108,7 +108,7 @@ export const getAddressFromPublicKey = (pubKey: string) => {
108108
*/
109109
export const verifyPrivateKey = (privateKey: string): boolean => {
110110
const keyPair = secp256k1.keyFromPrivate(privateKey, 'hex');
111-
const {result} = keyPair.validate();
111+
const { result } = keyPair.validate();
112112
return result;
113113
};
114114

@@ -195,7 +195,7 @@ interface ValidatorDictionary {
195195
// make sure each of the keys in requiredArgs is present in args
196196
// and each of it's validator functions return true
197197
export const validateArgs = (
198-
args: {[key: string]: any},
198+
args: { [key: string]: any },
199199
requiredArgs: ValidatorDictionary,
200200
optionalArgs?: ValidatorDictionary,
201201
) => {
@@ -277,3 +277,38 @@ export const intToByteArray = (val: number, paddedSize: number) => {
277277

278278
return arr;
279279
};
280+
281+
/**
282+
* toChecksumAddress
283+
*
284+
* takes hex-encoded string and returns the corresponding address
285+
*
286+
* @param {string} address
287+
* @returns {string}
288+
*/
289+
export const toChecksumAddress = (address: string): string => {
290+
291+
address = address.toLowerCase().replace('0x', '');
292+
const hash = hashjs.sha256().update(address, 'hex').digest('hex');
293+
let ret = '0x';
294+
for (let i = 0; i < address.length; i++) {
295+
if (parseInt(hash[i], 16) >= 8) {
296+
ret += address[i].toUpperCase();
297+
} else {
298+
ret += address[i];
299+
}
300+
}
301+
return ret;
302+
};
303+
304+
/**
305+
* isValidChecksumAddress
306+
*
307+
* takes hex-encoded string and returns boolean if address is checksumed
308+
*
309+
* @param {string} address
310+
* @returns {boolean}
311+
*/
312+
export const isValidChecksumAddress = (address: string): boolean => {
313+
return (isAddress(address.replace('0x', '')) && toChecksumAddress(address) === address);
314+
};

Diff for: src/zilliqa.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ export default class Zilliqa {
3535
isAddress: util.isAddress,
3636
isPubKey: util.isPubKey,
3737
intToByteArray: util.intToByteArray,
38-
compressPublicKey: util.compressPublicKey
38+
compressPublicKey: util.compressPublicKey,
39+
isPrivateKey: util.isPrivateKey,
40+
toChecksumAddress: util.toChecksumAddress,
41+
isValidChecksumAddress: util.isValidChecksumAddress,
3942

4043
};
4144

0 commit comments

Comments
 (0)