Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate to ts #1

Merged
merged 46 commits into from
Mar 11, 2022
Merged

feat: migrate to ts #1

merged 46 commits into from
Mar 11, 2022

Conversation

dmbsk
Copy link

@dmbsk dmbsk commented Dec 21, 2021

Changes

  • migrate to typescript
  • setup karma to run test in browser
  • setup eslint
  • setup rollup
  • remove committed libs and replace them with npm packages (when possible)
    • segwit_addr add minimal required amount of typing
    • cnBase58 removed (only used for monero)
  • remove monero validator
  • remove stx

Docs

Lib requires fallbacks/polyfills/shimming to be used in the browser because the most popular cryptographic libs are written to be used in node (not browser)

webpack example

 resolve: {
     resolve: {
          fallback: {
            crypto: require.resolve('crypto-browserify'),
            stream: 'stream-browserify',
            buffer: require.resolve('buffer/'),
          },
          extensions: ['.ts', '.js'],
        },
        plugins: [
          new webpack.ProvidePlugin({
            Buffer: ['buffer', 'Buffer'],
          }),
        ],
  }

⚠️ Currently there is a problem with running test in browser context on macOS; read more here

@dmbsk dmbsk added the enhancement New feature or request label Dec 21, 2021
@dmbsk dmbsk self-assigned this Dec 21, 2021
@dmbsk dmbsk changed the title feat: Migrate to ts feat: migrate to ts Dec 21, 2021
@@ -0,0 +1,251 @@
{
"cSpell.words": [
"bitcoincash"
Copy link

Choose a reason for hiding this comment

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

missing comma , at the end of line

Copy link
Author

Choose a reason for hiding this comment

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

This is for the VSC extension so linting rules doesn't apply here

@@ -0,0 +1,251 @@
{
"cSpell.words": [
Copy link

Choose a reason for hiding this comment

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

Also I would suggest to sort these words alphabetically ;D

Copy link
Author

Choose a reason for hiding this comment

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

Sorted

const regexp = new RegExp("^(ak_)([" + ALLOWED_CHARS + "]+)$"); // Begins with ak_ followed by

const aeValidator: TChecksumValidator = {
isValidAddress(address) {
Copy link

Choose a reason for hiding this comment

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

It is on purpose to remove old args? I see that they weren't used but maybe users that use this package will pass all 3 args.

Copy link

Choose a reason for hiding this comment

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

The same also applies to methods for rest of crypto.

Copy link
Author

@dmbsk dmbsk Jan 24, 2022

Choose a reason for hiding this comment

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

These are additional args - unused


verifyChecksum: (address) => {
const decodedBuffer = base58.decode(address);
return decodedBuffer.length === 32 + 4; // add 4 because base is adding 4 characters, why is base 58 adding them?
Copy link

Choose a reason for hiding this comment

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

I would suggest to leave a TODO comment ;D

Copy link

Choose a reason for hiding this comment

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

I was wodneriing if we should leave original code here but it lgtm.

Copy link
Author

Choose a reason for hiding this comment

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

I changed it because npm package base58 decode function return type is different so that was cleanest&easiest refactor

* @param {String} address The target address
* @param {Object} currency A currency from the ./currencies.js array
* @param {String} networkType Network Type. Could be 'prod', 'both' and 'testnet'
* @param {Array} addressFormats Array of formats. Options are: 'legacy', 'cashaddr', 'bitpay', 'slpaddr', 'all'
Copy link

Choose a reason for hiding this comment

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

You have declared thisjust as string:

 export type TAddress = string;

Can another crypto addresess have different address format?

Copy link

Choose a reason for hiding this comment

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

Btw. I see you have removed this comment for src/bitcoincash_validator.ts, but left simialr comment in src/wallet_address_validator.ts.

return b58;
})();

export default cnBase58;
Copy link

Choose a reason for hiding this comment

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

why cnBase58 not just base58 like before?

Copy link
Author

Choose a reason for hiding this comment

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

cnBase58 is not Base58. Maybe types names are wrong 🤔

}
}

module.exports = methods
Copy link

Choose a reason for hiding this comment

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

Do we have ts version for sha3? I don't see.

src/types/validator.types.ts Show resolved Hide resolved
tsconfig.json Show resolved Hide resolved
- simple-import-sort
- eslint-plugin-prettier

rules:
Copy link

Choose a reason for hiding this comment

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

As I fan of single quotes I would recommend you to use single quotes for imports, strings etc :D

Copy link

Choose a reason for hiding this comment

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

Also ramp-instant uses single quotes mostly

@dmbsk dmbsk marked this pull request as ready for review February 3, 2022 17:05
.eslintrc.yml Outdated
ignorePatterns:
- '*.d.ts'
- 'dist'
- "rollup.config.ts"
Copy link

Choose a reason for hiding this comment

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

Suggested change
- "rollup.config.ts"
- 'rollup.config.ts'

@@ -1,256 +1,11 @@
{
"name": "@swyftx/api-crypto-address-validator",
"name": "@ramp/api-crypto-address-validator",
Copy link

Choose a reason for hiding this comment

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

@macbem shouldn't it be @ramp-network/api-crypto-address-validator or just @ramp-network/crypto-address-validator?

Copy link
Member

Choose a reason for hiding this comment

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

@ramp-network/crypto-address-validator

package.json Outdated
"module": "dist/ramp-crypto-address-validator.es5.js",
"typings": "dist/types/wallet_address_validator.d.ts",
"version": "1.0.0",
"author": "RampNetwork",
Copy link

Choose a reason for hiding this comment

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

Suggested change
"author": "RampNetwork",
"author": "Ramp Network <contact@ramp.network>",

Copy link
Author

Choose a reason for hiding this comment

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

Changed, I miss out this one 😨

}
};

// TODO refactor
Copy link

Choose a reason for hiding this comment

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

Is it done/resolved?

Copy link
Author

Choose a reason for hiding this comment

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

Same reason as here

return BCH.isTestnetAddress(address);
}

// TODO: check if this is okay? Defaults to true if not prod or testnet? Probably 'both'?
Copy link

Choose a reason for hiding this comment

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

so is it okay?

Copy link
Author

Choose a reason for hiding this comment

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

That's not my comment, was already in the lib

return hex;
};

// TODO refactor
Copy link

Choose a reason for hiding this comment

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

is it refactored?

Copy link
Author

Choose a reason for hiding this comment

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

I didn't want to make refactor in this PR, because finding a potential error will be much harder

src/lumen_validator.ts Show resolved Hide resolved
Copy link

@akwodkiewicz akwodkiewicz left a comment

Choose a reason for hiding this comment

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

This is part of the output of yarn test (when run on macOS):

$ rollup -c rollup.config.ts

src/wallet_address_validator.ts → dist/ramp-crypto-address-validator.umd.js, dist/ramp-crypto-address-validator.es5.js...
(!) Missing shims for Node.js built-ins
Creating a browser bundle that depends on "buffer", "stream" and "string_decoder". You might need to include https://github.com/snowpackjs/rollup-plugin-polyfill-node
(!) Plugin node-resolve: preferring built-in module 'buffer' over local alternative at '/Users/andrzejwodkiewicz/Ramp/crypto-address-validator/node_modules/buffer/index.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
(!) Plugin node-resolve: preferring built-in module 'string_decoder' over local alternative at '/Users/andrzejwodkiewicz/Ramp/crypto-address-validator/node_modules/string_decoder/lib/string_decoder.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
(!) Missing global variable names
Use output.globals to specify browser global variable names corresponding to external modules
buffer (guessing 'require$$0')
readable-stream (guessing 'require$$0$1')
stream (guessing 'require$$0$2')
string_decoder (guessing 'require$$2')
created dist/ramp-crypto-address-validator.umd.js, dist/ramp-crypto-address-validator.es5.js in 3.1s

This is my first time with rollup (and karma as well), so I have some questions regarding the warnings:

Creating a browser bundle that depends on "buffer", "stream" and "string_decoder". You might need to include https://github.com/snowpackjs/rollup-plugin-polyfill-node

should we include this module?

(!) Plugin node-resolve: preferring built-in module (...) pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning

Do we want to use built-ins or no? Please set the config appropriately.

(!) Missing global variable names

Is this something that we should fix?

@dmbsk
Copy link
Author

dmbsk commented Mar 7, 2022

Creating a browser bundle that depends on "buffer", "stream" and "string_decoder". You might need to include https://github.com/snowpackjs/rollup-plugin-polyfill-node

should we include this module?

Imho it is better to add them while using because other libs/deps can yous global polyfills also

(!) Plugin node-resolve: preferring built-in module (...) pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning

Do we want to use built-ins or no? Please set the config appropriately.

preferBuiltins: true added

(!) Missing global variable names

Is this something that we should fix?

In our case this is because we don't include polyfills inside package. So imho no.

@dmbsk dmbsk requested a review from akwodkiewicz March 7, 2022 19:54
Copy link

@akwodkiewicz akwodkiewicz left a comment

Choose a reason for hiding this comment

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

LGTM, we'll fix potential errors if we find them when using the lib.

@dmbsk dmbsk requested review from macbem and Kraudia March 10, 2022 19:38
@dmbsk
Copy link
Author

dmbsk commented Mar 10, 2022

I recently checked if we can somehow move from karma to anything else but did not found anything.
Cypress was a good guess but cypress by default add a lot of polyfills to make testing easier 😞 (there is no way to disable auto polyfilling)

@dmbsk
Copy link
Author

dmbsk commented Mar 10, 2022

@macbem @jakubsta rereview/approve please

@dmbsk dmbsk merged commit 764a913 into dev Mar 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
5 participants