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

refactored jellyfish-address to use a much lighter design #674

Merged
merged 7 commits into from
Sep 23, 2021

Conversation

fuxingloh
Copy link
Contributor

What kind of PR is this?:

/kind refactor

What this PR does / why we need it:

Refactoring of jellyfish-address code/logic to be lighter, currently, it's too complex. Dangerous technical debt.

Implemented an address parser with a simple interface and design, with just 2 main code branches and 4 sub-if branches. The previous implementation has been deprecated but not yet removed to make migration less forceful.

export interface DecodedAddress {
  type: AddressType
  address: string
  script: Script
  network: NetworkName
}

/**
 * Convert an address to a script, this operation requires the network to be known.
 * A script is network agnostic while the address is prefixed with the network's unique human readable part.
 *
 * @param {string} address to convert into Script
 * @param {NetworkName} network to prefix human readable part of the address
 * @return {DecodedAddress | undefined} DecodedAddress if is a recognizable, undefined if fail to parse
 */
export function fromAddress (address: string, network: NetworkName): DecodedAddress | undefined

Usage Example:

const decoded = fromAddress('df1qpe7q4vvtxpdunpazvmwqdh3xlnatfdt2xr8mpv', 'mainnet')

expect(decoded).toStrictEqual({
  type: AddressType.P2WPKH,
  address: 'df1qpe7q4vvtxpdunpazvmwqdh3xlnatfdt2xr8mpv',
  script: {
    stack: [
      OP_CODES.OP_0,
      OP_CODES.OP_PUSHDATA_HEX_LE('0e7c0ab18b305bc987a266dc06de26fcfab4b56a')
    ]
  },
  network: 'mainnet'
})

Which issue(s) does this PR fixes?:

Fixes #239

@codeclimate
Copy link

codeclimate bot commented Sep 22, 2021

Code Climate has analyzed commit 7c70b6a and detected 10 issues on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 2
Duplication 8

View more on Code Climate.

@github-actions
Copy link

github-actions bot commented Sep 22, 2021

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
packages/jellyfish/dist/index.umd.js 23.5 KB (0%) 470 ms (0%) 116 ms (+118.74% 🔺) 586 ms

@codecov
Copy link

codecov bot commented Sep 22, 2021

Codecov Report

Merging #674 (7c70b6a) into main (a09a251) will increase coverage by 0.02%.
The diff coverage is 97.75%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #674      +/-   ##
==========================================
+ Coverage   97.57%   97.59%   +0.02%     
==========================================
  Files         140      140              
  Lines        4166     4245      +79     
  Branches      519      543      +24     
==========================================
+ Hits         4065     4143      +78     
- Misses        101      102       +1     
Impacted Files Coverage Δ
packages/jellyfish-address/src/script/P2SH.ts 95.45% <87.50%> (-4.55%) ⬇️
...ckages/jellyfish-address/src/script/Base58Check.ts 95.00% <92.30%> (+5.00%) ⬆️
packages/jellyfish-address/src/address.ts 84.00% <100.00%> (ø)
packages/jellyfish-address/src/base58_address.ts 93.93% <100.00%> (ø)
packages/jellyfish-address/src/bech32_address.ts 100.00% <100.00%> (ø)
packages/jellyfish-address/src/index.ts 97.82% <100.00%> (ø)
packages/jellyfish-address/src/script/Bech32.ts 91.66% <100.00%> (+5.95%) ⬆️
packages/jellyfish-address/src/script/P2PKH.ts 100.00% <100.00%> (ø)
packages/jellyfish-address/src/script/P2WPKH.ts 100.00% <100.00%> (ø)
packages/jellyfish-address/src/script/P2WSH.ts 100.00% <100.00%> (ø)
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a09a251...7c70b6a. Read the comment docs.

@fuxingloh fuxingloh marked this pull request as ready for review September 22, 2021 09:23
jingyi2811
jingyi2811 previously approved these changes Sep 22, 2021
canonbrother
canonbrother previously approved these changes Sep 23, 2021
ivan-zynesis
ivan-zynesis previously approved these changes Sep 23, 2021
packages/jellyfish-address/src/script/P2SH.ts Outdated Show resolved Hide resolved
@netlify
Copy link

netlify bot commented Sep 23, 2021

✔️ Deploy Preview for jellyfish-defi ready!

🔨 Explore the source changes: 7c70b6a

🔍 Inspect the deploy log: https://app.netlify.com/sites/jellyfish-defi/deploys/614cb057dd26ec00070c54b9

😎 Browse the preview: https://deploy-preview-674--jellyfish-defi.netlify.app

@fuxingloh fuxingloh merged commit 6ec711c into main Sep 23, 2021
@fuxingloh fuxingloh deleted the fuxingloh/address-to-script branch September 23, 2021 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/refactor Non feature refactor change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

refactor jellyfish-address implementation to be lighter
5 participants