Skip to content
This repository has been archived by the owner on Jul 8, 2024. It is now read-only.

Add typescript types #58

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Generated typescript files
src/wallet_address_validator.[dj]*
src/currencies.[dj]*

# Logs
logs
*.log
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,10 @@
"url": "https://github.com/Swyftx/crypto-address-validator.git"
},
"main": "src/wallet_address_validator",
"types": "src/wallet_address_validator.d.ts",
"scripts": {
"bundle": "browserify src/wallet_address_validator.js --standalone WAValidator --outfile dist/wallet-address-validator.js",
"build-ts": "tsc --declaration src/wallet_address_validator.ts src/currencies.ts",
"minify": "uglifyjs -c -m -o dist/wallet-address-validator.min.js -- dist/wallet-address-validator.js",
"test:node": "mocha test",
"test:browser": "echo Temp bypass until jenkins setup",
Expand All @@ -292,6 +294,7 @@
"rfc4648": "^1.3.0"
},
"devDependencies": {
"@types/node": "^14.0.27",
"browserify": "^15.1.0",
"chai": "^4.1.2",
"eslint-config-standard": "^13.0.1",
Expand All @@ -303,6 +306,7 @@
"node-gzip": "^1.1.2",
"prettysize": "^2.0.0",
"standard": "^12.0.1",
"typescript": "^3.9.7",
"uglify-es": "^3.3.10"
},
"standard": {
Expand Down
17 changes: 16 additions & 1 deletion src/currencies.js → src/currencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,23 @@ const ALGOValidator = require('./algo_validator')
const BCHValidator = require('./bitcoincash_validator')
const SYSValidator = require('./sys_validator')

export type Currencies = Array<{
name: string,
symbol: string,
validator: {
// `currency` must be `any` to avoid recureferences
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently the typescript seems to have no problem with recursive types. Or is it not so? Perhaps it is worth updating the request?

isValidAddress:(address:string, currency:any, networkType:string, addressFormats:string[])=>boolean,
verifyChecksum:(address:string)=>boolean
},
addressTypes?: { prod: string[], testnet?: string[] },
iAddressTypes?:{ prod: string[], testnet?: string[] },
expectedLength?: number,
hashFunction?: string,
regex?: RegExp,
}>

// defines P2PKH and P2SH address types for standard (prod) and testnet networks
const CURRENCIES = [{
const CURRENCIES:Currencies = [{
name: 'Algorand',
symbol: 'algo',
validator: ALGOValidator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var currencies = require('./currencies')
import {Currencies} from './currencies'

var DEFAULT_CURRENCY_NAME = 'bitcoin'

Expand All @@ -13,7 +14,7 @@ module.exports = {
* @param {Array} addressFormats Array of formats. For example ['legacy', 'slp ', 'cash']
* @returns {Error|Boolean}
*/
validate: function (address, currencyNameOrSymbol, networkType, addressFormats) {
validate: function (address:string, currencyNameOrSymbol:string, networkType?:string, addressFormats?:string[]):boolean {
var currency = currencies.getByNameOrSymbol(currencyNameOrSymbol || DEFAULT_CURRENCY_NAME)

if (currency && currency.validator) {
Expand All @@ -28,5 +29,5 @@ module.exports = {
throw new Error('Missing validator for currency: ' + currencyNameOrSymbol)
},

CURRENCIES: currencies.CURRENCIES
CURRENCIES: currencies.CURRENCIES as Currencies
}