Coder Social home page Coder Social logo

ealmansi / bchaddrjs Goto Github PK

View Code? Open in Web Editor NEW
64.0 64.0 47.0 1.47 MB

BchAddr.js: Bitcoin Cash general purpose address translation for Node.js and web browsers.

Home Page: https://emilio.almansi.me/bchaddrjs/module-bchaddr.html

License: MIT License

JavaScript 100.00%
address base58 bch bitcoin bitcoin-cash bitpay cash-address cryptocurrency encoding javascript library translation

bchaddrjs's People

Contributors

dependabot[bot] avatar dhruvdangi avatar ealmansi avatar joshmh avatar knoxcard avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

bchaddrjs's Issues

SLP address

Hello,

Does this library also support validation of SLP addresses?

Zcash address is wrongly recognized as P2PKH address

Zcash address is wrongly recognized as BCH's P2PKH address, as the title say.
Confirmed on 3 addresses but the result is same.

Expected

isBitpayAddress("t1LuPdPkGH5QoNSewQrr8EzNbM27ktPdgQX")
isMainnetAddress("t1LuPdPkGH5QoNSewQrr8EzNbM27ktPdgQX")
isP2PKHAddress("t1LuPdPkGH5QoNSewQrr8EzNbM27ktPdgQX")
toCashAddress("t1LuPdPkGH5QoNSewQrr8EzNbM27ktPdgQX")
// Everything listed above must throw error

Actual

isBitpayAddress("t1LuPdPkGH5QoNSewQrr8EzNbM27ktPdgQX")
isMainnetAddress("t1LuPdPkGH5QoNSewQrr8EzNbM27ktPdgQX")
isP2PKHAddress("t1LuPdPkGH5QoNSewQrr8EzNbM27ktPdgQX")
// Everything listed above return true
toCashAddress("t1LuPdPkGH5QoNSewQrr8EzNbM27ktPdgQX")
// toCashAddress throws error, that's OK

Is this library ready for use?

Hi, I'm about to publicly release a tool which uses this library for Bitcoin Cash address translation.

Is this tool considered to be ready for use? Any comments on tests, community review, general reliability or upcoming development?

Thanks!

TypeError on client side

I am using bchaddrjs on client side to convert address to LegacyAddress and CashAddress.

this is the environment and packages:

node: v18.16.0
browser: Version 115.0.5790.170 (Official Build) (x86_64)

{
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "serve": "vite preview",
    "type-check": "tsc --noEmit",
    "scripts:svgr:build": "sh ./scripts/svgr/run.sh && npm run prettier:fix && npm run lint:fix",
    "lint": "eslint ./src --ext .jsx,.js,.ts,.tsx --quiet --ignore-path ./.gitignore",
    "lint:fix": "eslint ./src --ext .jsx,.js,.ts,.tsx --quiet --fix --ignore-path ./.gitignore",
    "prettier": "prettier --check \"src/**/*.{ts,tsx}\"",
    "prettier:fix": "prettier --write \"src/**/*.{ts,tsx}\"",
    "prepare": "husky install",
    "test": "jest"
  },
  "dependencies": {
    "bchaddrjs": "^0.5.2",
    "bignumber.js": "^9.1.1",
    "date-fns": "^2.30.0",
    "jwt-decode": "^3.1.2",
    "react": "^18.2.0",
    "react-admin": "^4.11.1",
    "react-dom": "^18.2.0"
  },
  "devDependencies": {
    "@babel/core": "^7.22.5",
    "@babel/preset-env": "^7.22.5",
    "@svgr/cli": "^6.5.1",
    "@types/bchaddrjs": "^0.4.0",
    "@types/jest": "^29.5.2",
    "@types/lodash": "^4.14.195",
    "@types/node": "^18.16.1",
    "@types/react": "^18.0.22",
    "@types/react-dom": "^18.0.7",
    "@typescript-eslint/eslint-plugin": "^5.59.7",
    "@typescript-eslint/parser": "^5.59.7",
    "@vitejs/plugin-react": "^2.2.0",
    "babel-jest": "^29.5.0",
    "eslint": "^8.41.0",
    "eslint-config-prettier": "^8.8.0",
    "eslint-config-standard": "^17.0.0",
    "eslint-plugin-lodash": "^7.4.0",
    "eslint-plugin-react": "^7.32.2",
    "eslint-plugin-react-hooks": "^4.6.0",
    "eslint-plugin-simple-import-sort": "^10.0.0",
    "husky": "^8.0.0",
    "jest": "^29.5.0",
    "prettier": "^2.8.8",
    "ts-jest": "^29.1.0",
    "typescript": "^4.6.4",
    "vite": "^3.2.0"
  }
}

this is the code snippet:
in general, im using react admin to display a field on client side.
and this part of code is just getting the data from client side and use bchaddr to convert the address to desired format.

const BchAddressField = () => {
    const record = useRecordContext();
    try {
      const toLegacyAddress = bchaddr.toLegacyAddress(record.verifiableAddress);
      const toCashAddress = bchaddr.toCashAddress(record.verifiableAddress);
    } catch (e) {
      // TODO: fix typeError: Cannot read properties of undefined (reading 'call')
      console.log(e);
    }
  };

this the error:

TypeError: Cannot read properties of undefined (reading 'call')
    at Hash.CipherBase (index.js:7:13)
    at new Hash (browser.js:9:8)
    at createHash (browser.js:29:10)
    at sha256x2 (index.js:8:13)
    at Object.encode (base.js:9:20)
    at encodeAsLegacy (bchaddr.js:321:20)
    at Object.toLegacyAddress (bchaddr.js:110:10)
    at BchAddressField (DfcTokensTabs.tsx?t=1691657617814:84:39)
    at renderWithHooks (react-dom.development.js:16305:18)
    at mountIndeterminateComponent (react-dom.development.js:20074:13)

this is the warning coming from browser console:

browser-external:events:9 Module "events" has been externalized for browser compatibility. Cannot access "events.EventEmitter" in client code.
get @ browser-external:events:9
node_modules/readable-stream/lib/_stream_readable.js @ _stream_readable.js:33
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/readable-stream/readable-browser.js @ readable-browser.js:1
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/hash-base/index.js @ index.js:3
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/md5.js/index.js @ index.js:3
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/create-hash/browser.js @ browser.js:3
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/bs58check/index.js @ index.js:3
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/bchaddrjs/src/bchaddr.js @ bchaddr.js:9
__require @ chunk-J43GMYXM.js?v=09e9a855:11
(anonymous) @ bchaddr.js:461
browser-external:events:9 Module "events" has been externalized for browser compatibility. Cannot access "events.EventEmitter" in client code.
get @ browser-external:events:9
node_modules/readable-stream/lib/internal/streams/stream-browser.js @ stream-browser.js:1
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/readable-stream/lib/_stream_readable.js @ _stream_readable.js:40
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/readable-stream/readable-browser.js @ readable-browser.js:1
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/hash-base/index.js @ index.js:3
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/md5.js/index.js @ index.js:3
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/create-hash/browser.js @ browser.js:3
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/bs58check/index.js @ index.js:3
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/bchaddrjs/src/bchaddr.js @ bchaddr.js:9
__require @ chunk-J43GMYXM.js?v=09e9a855:11
(anonymous) @ bchaddr.js:461
browser-external:util:9 Module "util" has been externalized for browser compatibility. Cannot access "util.debuglog" in client code.
get @ browser-external:util:9
node_modules/readable-stream/lib/_stream_readable.js @ _stream_readable.js:55
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/readable-stream/readable-browser.js @ readable-browser.js:1
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/hash-base/index.js @ index.js:3
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/md5.js/index.js @ index.js:3
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/create-hash/browser.js @ browser.js:3
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/bs58check/index.js @ index.js:3
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/bchaddrjs/src/bchaddr.js @ bchaddr.js:9
__require @ chunk-J43GMYXM.js?v=09e9a855:11
(anonymous) @ bchaddr.js:461
browser-external:util:9 Module "util" has been externalized for browser compatibility. Cannot access "util.inspect" in client code.
get @ browser-external:util:9
node_modules/readable-stream/lib/internal/streams/buffer_list.js @ buffer_list.js:14
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/readable-stream/lib/_stream_readable.js @ _stream_readable.js:62
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/readable-stream/readable-browser.js @ readable-browser.js:1
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/hash-base/index.js @ index.js:3
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/md5.js/index.js @ index.js:3
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/create-hash/browser.js @ browser.js:3
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/bs58check/index.js @ index.js:3
__require @ chunk-J43GMYXM.js?v=09e9a855:11
node_modules/bchaddrjs/src/bchaddr.js @ bchaddr.js:9
__require @ chunk-J43GMYXM.js?v=09e9a855:11
(anonymous) @ bchaddr.js:461
browser-external:stream:9 Module "stream" has been externalized for browser compatibility. Cannot access "stream.Transform" in client code.

What is BitpayAddress?

I didn't find any resources about this type address.
I use bitpay and the address begin with '1' not 'C'
What kind of address is this?

Package not actually in bower

If you try to install this package via bower as indicated in the README, you get these results:

bower i --save bchaddrjs
bower                        ENOTFOUND Package bchaddrjs not found

You can also confirm this by searching the packages at https://bower.io/.

In the mean time, I have the package installed and running next to my other bower modules with:

bower i --save https://github.com/bitcoincashjs/bchaddrjs.git

decodeAddress (and its variations) should be public

Doing so will make many operations way easier, for example in order to get all the information for an address, one needs to run isValidAddress first (or wrap all operations in try-catch), and then call all detectAddress methods individually (which are already using decodeAddress as source);

Also, there may be cases where one wants to check the output of cashaddr decode directly (for example, accessing prefix property which do includes bchreg and detectNetwork treats it as testnet)

does ie support this

when i use this lib in ie11, it throws error:
object does't support property or method fill.
And I found it is this line :
const BASE_MAP = new Uint8Array(256); BASE_MAP.fill(255) ;

So i add polyfill for TypedArray.prototype.fill().
But then it throw: InvalidAddressError: Received an invalid Bitcoin Cash address as input.
This works fine in other browers such as chrome.

Error throw

Hi!
Thanks a lot for your library. It was implemented in time!

I've got one idea
Why do u throw error if f.e. BCH address was invalid?
Validation method is named as isCashAddress, i think it's better (in case of invalid address) return just false
What do u think?
Regards

BCH address is invalid

Was searching web for js lib to verify bch address, and found you lib.

Cant get my coinomi wallet address to validate, lib shows that it invalid:

qqvek95wvh6gxg5aeyv6u4huxtwxmpm575rsvn40yz

bchaddr.isMainnetAddress('qqvek95wvh6gxg5aeyv6u4huxtwxmpm575rsvn40yz')

And all other functions (cashAddress, etc)

What is wrong with that address?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.