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

jellyfish-address: from script to address #670

Merged
merged 10 commits into from
Sep 22, 2021
Merged

Conversation

fuxingloh
Copy link
Contributor

@fuxingloh fuxingloh commented Sep 21, 2021

What kind of PR is this?:

/kind feature

What this PR does / why we need it:

Currently, on jellyfish-address only Address to Script parsing was implemented.
This PR implements a from Script to Address for all address types.

Included in this PR, we are avoiding default, named, export const exports. Any of those implementations makes it incompatible with webpack tree-shaking to reduce bundle size. @canonbrother has also mentioned this months ago in another PR.

Use PascalCase and period, not underscores, or dashes in filenames. in this PR the new files are respecting the new rule.

BS58Check and Bech32 code has been re-implemented on jellyfish-address as they should not be part of crypto operations hence slated for deprecation.

The PR exposes two new top-level from Script-to-Address utility. This allows the automatic parsing of Script into their know address format. There are a significant amount of code branches for this module as it requires checking of Script Schema exact OP_CODE positions.

Support addresses format:

  • P2PKH - legacy via PublicKeyHashPrefix
  • P2SH - P2SH-SEGWIT via ScriptHashPrefix
  • P2WPKH - SegWit PubKeyHash via Bech32 HRP
  • P2WSH - SegWit ScriptHash via Bech32 HRP
/**
 * Convert a script to address, 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 {Script} script to convert into address
 * @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 fromScript (script: Script, network: NetworkName): DecodedAddress | undefined
/**
 * Convert a script to address, 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} hex of the script to convert into address, without VarUInt length
 * @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 fromScriptHex (hex: string, network: NetworkName): DecodedAddress | undefined

Usage Example

// e.g. P2SH-SEGWIT

const p2wpkh: Script = {
  stack: [
    OP_CODES.OP_0,
    OP_CODES.OP_PUSHDATA(HASH160(pubKey, 'little')
  ]
}

const script: Script = {
  stack: [
    OP_CODES.OP_HASH160,
    OP_CODES.OP_PUSHDATA(HASH160(fromOPCodes(p2wpkh.stack)), 'little'),
    OP_CODES.OP_EQUAL
  ]
}

expect(fromScript(script, 'regtest')).toStrictEqual({
  type: 'p2sh',
  address: address,
  script: script,
  network: 'regtest'
})

@codeclimate
Copy link

codeclimate bot commented Sep 21, 2021

Code Climate has analyzed commit 0eb7ede and detected 8 issues on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 2
Duplication 6

View more on Code Climate.

@github-actions
Copy link

github-actions bot commented Sep 21, 2021

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
packages/jellyfish/dist/index.umd.js 23.4 KB (0%) 468 ms (0%) 164 ms (+8.28% 🔺) 632 ms

@codecov
Copy link

codecov bot commented Sep 21, 2021

Codecov Report

Merging #670 (0eb7ede) into main (47c3dab) will increase coverage by 0.01%.
The diff coverage is 98.05%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #670      +/-   ##
==========================================
+ Coverage   97.56%   97.57%   +0.01%     
==========================================
  Files         133      140       +7     
  Lines        4063     4166     +103     
  Branches      493      519      +26     
==========================================
+ Hits         3964     4065     +101     
- Misses         99      101       +2     
Impacted Files Coverage Δ
packages/jellyfish-address/src/script/Bech32.ts 85.71% <85.71%> (ø)
...ckages/jellyfish-address/src/script/Base58Check.ts 90.00% <90.00%> (ø)
packages/jellyfish-address/src/index.ts 97.82% <100.00%> (+0.04%) ⬆️
packages/jellyfish-address/src/script/P2PKH.ts 100.00% <100.00%> (ø)
packages/jellyfish-address/src/script/P2SH.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%> (ø)
packages/jellyfish-address/src/script/index.ts 100.00% <100.00%> (ø)
packages/jellyfish-transaction/src/index.ts 100.00% <100.00%> (ø)
... and 6 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 47c3dab...0eb7ede. Read the comment docs.

@fuxingloh fuxingloh merged commit a09a251 into main Sep 22, 2021
@fuxingloh fuxingloh deleted the fuxingloh/address-to branch September 22, 2021 03:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature New feature request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants