Structure related functions in separate source files#182
Conversation
holgerd77
left a comment
There was a problem hiding this comment.
Ok, some first review, I think this is already a very good start. Since e.g. #38 is open for more than 2 (! sigh) years I also think there is not THE absolute best structure and once we come up with something quite consistent this would already be some huge improvement.
Maybe already do some update on this PR with the change suggestions you agree with, then it becomes easier to evolve.
| export const isPrecompiled = function(address: Buffer | string): boolean { | ||
| const a = unpad(address) | ||
| return a.length === 1 && a[0] >= 1 && a[0] <= 8 | ||
| } |
There was a problem hiding this comment.
I think all this fits really well.
|
|
||
| function isValidSigRecovery(recovery: number): boolean { | ||
| return recovery === 0 || recovery === 1 | ||
| } |
There was a problem hiding this comment.
I would have a tendency to move to top-most functions to address.js and make for the others some dedicated signature.js file.
src/pubkey.ts
Outdated
| */ | ||
| export const isValidPrivate = function(privateKey: Buffer): boolean { | ||
| return secp256k1.privateKeyVerify(privateKey) | ||
| } |
There was a problem hiding this comment.
One side note: on refactoring we should go through all the file names, I think there are better versions for many of them (like this one, but also fromSigned, baToJSON, zeros) being more descriptive and generally better grasp what the function is doing. Think this would help a lot on code readability on the usage side where people (we 😄) are integrating this within their code.
9fe2d76 to
c1b9281
Compare
|
Applied your comments, the main differences (to my original post) are:
|
|
Ok, will leave this open until tomorrow so that others have a chance to comment, but I think we've already got some good ground here. |
holgerd77
left a comment
There was a problem hiding this comment.
One file naming thing to settle out.
| } | ||
|
|
||
| return secp256k1.publicKeyVerify(publicKey) | ||
| } |
There was a problem hiding this comment.
With these two I am unsure, if they rather belong in the signature area or here. Just did some context research, both are used in wallet, I also had some short look at this HackerNoon article on Ethereum signature.
Think we can leave here for now, since we are not doing a release yet we can still move if found to be more appropriate in signature.
| } | ||
|
|
||
| return ethjsUtil.isHexPrefixed(str) ? str : '0x' + str | ||
| } |
There was a problem hiding this comment.
Ok, this is not fitting at all. Should we call the whole file byte.js? Then this is generally more generic and would also fit other functionality in the future eventually.
|
Renamed buffer to bytes. I was thinking for future maybe it'd make sense to define custom types, and organize part of the code around those types. E.g. a type |
|
Hi Sina, I was out for a longer (baby-related 🍼 😄 ) walk and actually also thinking about how to better and more coherently impose hex value validation, your suggestion with custom types sounds great! I think I'll also leave function naming "as is" for this round if we might restructure in a bigger way mid-term, we should nevertheless do the API changes on this round. Not sure how to organize some transition process, that's probably something for a second round and mid-term? Or would it already make sense to introduce parts of the functionality already here in parallel or something? Anyhow. Will merge this PR for now. Can further brainstorm. 😄 |
holgerd77
left a comment
There was a problem hiding this comment.
Ah, small thing: can we do "byte.ts" instead of "bytes.ts"? In my ears bytes sound a bit strange and it fits more to the other namings (like account-related, hash-related, byte-related... functionality).
|
(or if you've got a strong opinion on this please let me know) |
Maybe we can introduce the types in addition to the existing API, test it in smaller libraries and only if it worked well do a full transition?
I don't have a strong opinion, and would happily change, but it does seem that in this case since we're always working with a collection of bytes and almost never with a single byte, |
|
Ok, let's keep, have merged. |
|
Makes sense (transition idea/suggestion). |
This PR follows up on the discussion in #170 (and supersedes #38) and puts conceptually related functions in separate files. I totally made up the structure. Feel free to propose different files, or suggest moving specific functions from one file to another. Here's a gist:
BN,rlpandsecp256k1).Buffer, byte array or string manipulationpubkeyI haven't modified the functions themselves, except for explicitly using
ethjsUtilincommoninstead ofexports.Update: I also removed the
safe-bufferdependency.