|
10 | 10 | import randomBytes from 'randombytes';
|
11 | 11 | import elliptic from 'elliptic';
|
12 | 12 | import hashjs from 'hash.js';
|
13 |
| -import {isWebUri} from 'valid-url'; |
| 13 | +import { isWebUri } from 'valid-url'; |
14 | 14 |
|
15 | 15 | import * as schnorr from './schnorr';
|
16 |
| -import {TxDetails} from './types'; |
| 16 | +import { TxDetails } from './types'; |
17 | 17 |
|
18 | 18 | const NUM_BYTES = 32;
|
19 | 19 | //const HEX_PREFIX = '0x';
|
@@ -108,7 +108,7 @@ export const getAddressFromPublicKey = (pubKey: string) => {
|
108 | 108 | */
|
109 | 109 | export const verifyPrivateKey = (privateKey: string): boolean => {
|
110 | 110 | const keyPair = secp256k1.keyFromPrivate(privateKey, 'hex');
|
111 |
| - const {result} = keyPair.validate(); |
| 111 | + const { result } = keyPair.validate(); |
112 | 112 | return result;
|
113 | 113 | };
|
114 | 114 |
|
@@ -195,7 +195,7 @@ interface ValidatorDictionary {
|
195 | 195 | // make sure each of the keys in requiredArgs is present in args
|
196 | 196 | // and each of it's validator functions return true
|
197 | 197 | export const validateArgs = (
|
198 |
| - args: {[key: string]: any}, |
| 198 | + args: { [key: string]: any }, |
199 | 199 | requiredArgs: ValidatorDictionary,
|
200 | 200 | optionalArgs?: ValidatorDictionary,
|
201 | 201 | ) => {
|
@@ -277,3 +277,38 @@ export const intToByteArray = (val: number, paddedSize: number) => {
|
277 | 277 |
|
278 | 278 | return arr;
|
279 | 279 | };
|
| 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 | +}; |
0 commit comments