Skip to content
This repository was archived by the owner on May 26, 2023. It is now read-only.

Typescript improvements and Address Checksumming #65

Merged
merged 5 commits into from
Sep 27, 2018

Conversation

mickys
Copy link
Contributor

@mickys mickys commented Sep 27, 2018

Description

1. Cleanup and Typescript improvements.

  • Cleaned up repository of .lock files
  • Added util, schnorr, znode, exports to index for easy import
  • Added prepare for /dist generation upon install so types can actually be used in TypeScript

1.1. New usage example no longer requires "Zilliqa" class instantiation to get to the util library

import { util } from 'zilliqa-js';

util.isAddress( "not_an_address" ); // false
util.isAddress( "7bb3b0e8a59f3f61d9bff038f4aeb42cae2ecce8" ); // true

2. Address Checksumming based on EIP-55

see: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md

2.1. Methods:

methods support both standard and 0x prepended addresses

  • toChecksumAddress(address: string): string
  • isValidChecksumAddress(address: string): boolean

2.2. Usage Example

import { util } from 'zilliqa-js';

const address = "0x7bb3b0e8a59f3f61d9bff038f4aeb42cae2ecce8";
const checksummed = util.toChecksumAddress( address );
// 0x7bB3b0E8A59f3f61d9BFf038f4AEB42Cae2eccE8

util.isValidChecksumAddress( checksummed ); // true
util.isValidChecksumAddress( address ); // false

methods support both standard and 0x prepended addresses as inputs

const lowercaseAddress = "7bb3b0e8a59f3f61d9bff038f4aeb42cae2ecce8";

util.toChecksumAddress( lowercaseAddress );
// 0x7bB3b0E8A59f3f61d9BFf038f4AEB42Cae2eccE8

util.toChecksumAddress( "0x" + lowercaseAddress );
// 0x7bB3b0E8A59f3f61d9BFf038f4AEB42Cae2eccE8

util.isValidChecksumAddress( "0x7bB3b0E8A59f3f61d9BFf038f4AEB42Cae2eccE8".replace('0x', '') );
// true
util.isValidChecksumAddress( "0x7bB3b0E8A59f3f61d9BFf038f4AEB42Cae2eccE8" );
// true

2.3. Tests

Added tests for these in tests/util.spec.ts

Review Suggestion

Checksumming uses: hashjs.sha256() since it was already imported in the util class

Status

Implementation

  • ready for review

Sorry, something went wrong.

package.json Outdated
@@ -13,7 +13,8 @@
"scripts": {
"build": "rimraf dist/* && tsc --emitDeclarationOnly && cross-env NODE_ENV=production webpack --config webpack.config.js",
"test": "jest -c jest.config.js",
"publish": "yarn build && yarn publish"
"publish": "yarn build && yarn publish",
"prepare": "npm run build"
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the purpose of prepare?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the package is used as a dependency in a project, we should be able to use the types provided in the dist folder.

"prepare" tells the npm installer to run a build once the package is installed, in order to generate the dist directory.

Copy link
Contributor

Choose a reason for hiding this comment

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

@mickys I think the typings are already included in the dist folder as part of the NPM distribution. It seems I forgot to add the types field. Can you add it in? If you do, I don't think prepare is necessary.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a "typings": "dist/index.d.ts" in the package.json file, which is synonymous to types. So if it was supposed to work it should have.
( unless yarn publish / npm.org does this, which i'm unable to test )

There are 3 ways these can be provided to the user as far as i'm aware

  1. Provide said typings in the "dist" folder or another one "lib" maybe.
    this does create the dependency of building before committing

  2. Create a typings module and PR it to @types ( https://github.com/DefinitelyTyped/DefinitelyTyped ) to be included when needed.
    Does take some time to get accepted thou and afaik these are meant for modules that don't do it themselves

  3. Use "prepare" to generate dist or lib folder.

I'm fine with whichever option you guys prefer, thou

Copy link
Contributor

Choose a reason for hiding this comment

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

@mickys you're right. It looks like there has been some mistake when the build was published. We'll rectify this issue. prepare should not be necessary.

@edisonljh which folder do we normally publish?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've removed the prepare instruction from the PR.
Feel free to merge then do the changes required.

@edison0xyz edison0xyz requested a review from iantanwx September 27, 2018 05:02
@iantanwx
Copy link
Contributor

Thanks @mickys. This looks good. Would you be able to add a few more test vectors for the checksum tests? You can do something like checksum.fixtures.ts.

Copy link
Contributor

@iantanwx iantanwx left a comment

Choose a reason for hiding this comment

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

More test vectors to be added.

@mickys
Copy link
Contributor Author

mickys commented Sep 27, 2018

Added more checksum test vectors, see checksum.fixtures.ts

@mickys
Copy link
Contributor Author

mickys commented Sep 27, 2018

I've removed the prepare instruction from the PR.
Feel free to merge then do the types required changes you're planning.

@edison0xyz
Copy link
Contributor

This looks good! Thanks, this is a good PR! 👍

@iantanwx
Copy link
Contributor

Thanks @mickys !

@mickys mickys mentioned this pull request Sep 27, 2018
@AmritKumar AmritKumar merged commit 43742aa into Zilliqa:master Sep 27, 2018
iantanwx added a commit that referenced this pull request Nov 15, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants