|
| 1 | +import { BaseUtils, InvalidAddressError, UtilsError } from '@bitgo/sdk-core'; |
| 2 | +import * as querystring from 'querystring'; |
| 3 | +import * as rippleKeypairs from 'ripple-keypairs'; |
| 4 | +import * as url from 'url'; |
| 5 | +import * as xrpl from 'xrpl'; |
| 6 | +import { Address } from './iface'; |
| 7 | +import { KeyPair as XrpKeyPair } from './keyPair'; |
| 8 | + |
| 9 | +class Utils implements BaseUtils { |
| 10 | + isValidAddress(address: string): boolean { |
| 11 | + try { |
| 12 | + const addressDetails = this.getAddressDetails(address); |
| 13 | + return address === this.normalizeAddress(addressDetails); |
| 14 | + } catch (e) { |
| 15 | + return false; |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + isValidTransactionId(txId: string): boolean { |
| 20 | + return this.isValidHex(txId); |
| 21 | + } |
| 22 | + |
| 23 | + isValidPublicKey(key: string): boolean { |
| 24 | + try { |
| 25 | + new XrpKeyPair({ pub: key }); |
| 26 | + return true; |
| 27 | + } catch { |
| 28 | + return false; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + isValidPrivateKey(key: string): boolean { |
| 33 | + try { |
| 34 | + new XrpKeyPair({ prv: key }); |
| 35 | + return true; |
| 36 | + } catch { |
| 37 | + return false; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + isValidSignature(signature: string): boolean { |
| 42 | + return this.isValidHex(signature); |
| 43 | + } |
| 44 | + |
| 45 | + isValidBlockId(hash: string): boolean { |
| 46 | + return this.isValidHex(hash); |
| 47 | + } |
| 48 | + |
| 49 | + isValidHex(hex: string): boolean { |
| 50 | + return /^([a-fA-F0-9])+$/.test(hex); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Parse an address string into address and destination tag |
| 55 | + */ |
| 56 | + public getAddressDetails(address: string): Address { |
| 57 | + const destinationDetails = url.parse(address); |
| 58 | + const destinationAddress = destinationDetails.pathname; |
| 59 | + if (!destinationAddress || !xrpl.isValidClassicAddress(destinationAddress)) { |
| 60 | + throw new InvalidAddressError(`destination address "${destinationAddress}" is not valid`); |
| 61 | + } |
| 62 | + // there are no other properties like destination tags |
| 63 | + if (destinationDetails.pathname === address) { |
| 64 | + return { |
| 65 | + address: address, |
| 66 | + destinationTag: undefined, |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + if (!destinationDetails.query) { |
| 71 | + throw new InvalidAddressError('no query params present'); |
| 72 | + } |
| 73 | + |
| 74 | + const queryDetails = querystring.parse(destinationDetails.query); |
| 75 | + if (!queryDetails.dt) { |
| 76 | + // if there are more properties, the query details need to contain the destination tag property. |
| 77 | + throw new InvalidAddressError('destination tag missing'); |
| 78 | + } |
| 79 | + |
| 80 | + if (Array.isArray(queryDetails.dt)) { |
| 81 | + // if queryDetails.dt is an array, that means dt was given multiple times, which is not valid |
| 82 | + throw new InvalidAddressError( |
| 83 | + `destination tag can appear at most once, but ${queryDetails.dt.length} destination tags were found` |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + const parsedTag = parseInt(queryDetails.dt, 10); |
| 88 | + if (!Number.isSafeInteger(parsedTag)) { |
| 89 | + throw new InvalidAddressError('invalid destination tag'); |
| 90 | + } |
| 91 | + |
| 92 | + if (parsedTag > 0xffffffff || parsedTag < 0) { |
| 93 | + throw new InvalidAddressError('destination tag out of range'); |
| 94 | + } |
| 95 | + |
| 96 | + return { |
| 97 | + address: destinationAddress, |
| 98 | + destinationTag: parsedTag, |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Construct a full, normalized address from an address and destination tag |
| 104 | + */ |
| 105 | + public normalizeAddress({ address, destinationTag }: Address): string { |
| 106 | + if (typeof address !== 'string') { |
| 107 | + throw new InvalidAddressError('invalid address, expected string'); |
| 108 | + } |
| 109 | + if (typeof destinationTag === 'undefined' || destinationTag === null) { |
| 110 | + return address; |
| 111 | + } |
| 112 | + if (!Number.isInteger(destinationTag)) { |
| 113 | + throw new InvalidAddressError('invalid destination tag, expected integer'); |
| 114 | + } |
| 115 | + if (destinationTag > 0xffffffff || destinationTag < 0) { |
| 116 | + throw new InvalidAddressError('destination tag out of range'); |
| 117 | + } |
| 118 | + return `${address}?dt=${destinationTag}`; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @param message hex encoded string |
| 123 | + * @param privateKey |
| 124 | + * return hex encoded signature string, throws if any of the inputs are invalid |
| 125 | + */ |
| 126 | + public signString(message: string, privateKey: string): string { |
| 127 | + if (!this.isValidHex(message)) { |
| 128 | + throw new UtilsError('message must be a hex encoded string'); |
| 129 | + } |
| 130 | + if (!this.isValidPrivateKey(privateKey)) { |
| 131 | + throw new UtilsError('invalid private key'); |
| 132 | + } |
| 133 | + return rippleKeypairs.sign(message, privateKey); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @param message hex encoded string |
| 138 | + * @param signature hex encooded signature string |
| 139 | + * @param publicKey |
| 140 | + * return boolean, throws if any of the inputs are invalid |
| 141 | + */ |
| 142 | + public verifySignature(message: string, signature: string, publicKey: string): boolean { |
| 143 | + if (!this.isValidHex(message)) { |
| 144 | + throw new UtilsError('message must be a hex encoded string'); |
| 145 | + } |
| 146 | + if (!this.isValidSignature(signature)) { |
| 147 | + throw new UtilsError('invalid signature'); |
| 148 | + } |
| 149 | + if (!this.isValidPublicKey(publicKey)) { |
| 150 | + throw new UtilsError('invalid public key'); |
| 151 | + } |
| 152 | + try { |
| 153 | + return rippleKeypairs.verify(message, signature, publicKey); |
| 154 | + } catch (e) { |
| 155 | + return false; |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +const utils = new Utils(); |
| 161 | + |
| 162 | +export default utils; |
0 commit comments