bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.69k stars 2.11k forks source link

Ability to validate an address #890

Closed jackmallers closed 7 years ago

jackmallers commented 7 years ago

Would be nice to have the ability to easily check if a Bitcoin address is valid :)

dcousens commented 7 years ago

(edited title to reflect email context)

jackmallers commented 7 years ago

Should the functionality include any address at all as well @dcousens? Just don't want to pull in separate tools to validate segwit vs non-segwit

dcousens commented 7 years ago

@jackmallers best practice is to:

function validate (address) {
  try {
    bitcoin.address.toOutputScript(address)
    return true
  } catch (e) {
    return false
  }
}

And if we added it as an API function or example, it would undoubtedly be the same.

It handles all current address types (P2PKH, P2SH, Bech32)

jackmallers commented 7 years ago

@dcousens agree, that looks great 👍

dcousens commented 7 years ago

@jackmallers that works ^, if that resolves your question

jackmallers commented 7 years ago

@dcousens so I created 3 addresses to test on testnet: p2pkh: mpR4D7zjxUD2EM5FzDLGasmLLUETkzPx3E p2wkh: tb1qhdgnhnjdagv0fv4z00yy6pqqfcdrfp3dvl7waa np2wkh: 2NFjYV3YZ2PYyNE3ewgoMisKnKyE7vRtCdB

p2pkh and np2wkh validates, but p2wkh does not. I will look at the source good in a bit to try and debug

dcousens commented 7 years ago

How did you create them? And what is NP2WKH?

dabura667 commented 7 years ago

@jackmallers worked for me...

> bitcoin.address.toOutputScript('tb1qhdgnhnjdagv0fv4z00yy6pqqfcdrfp3dvl7waa',bitcoin.networks.testnet)
//<Buffer 00 14 bb 51 3b ce 4d ea 18 f4 b2 a2 7b c8 4d 04 00 4e 1a 34 86 2d>
dabura667 commented 7 years ago

@dcousens P2SH-P2WPKH is what he's referring to.

Nested Pay 2 Witness Key Hash

jackmallers commented 7 years ago

@dcousens does bitcoin.address.toOutputScript('tb1qhdgnhnjdagv0fv4z00yy6pqqfcdrfp3dvl7waa', bitcoin.networks.testnet) work for you as well?

I must be doing something really dumb that I'm missing, I'm getting Error: Non-base58 character

dabura667 commented 7 years ago

@jackmallers did you install via npm?

latest npm version doesn't support bech32 yet.

npm install https://github.com/bitcoinjs/bitcoinjs-lib.git should work better.

dcousens commented 7 years ago

3.2.0 released in https://github.com/bitcoinjs/bitcoinjs-lib/pull/892, @jackmallers if you run npm install bitcoinjs-lib@latest, your issue should resolve.

jackmallers commented 7 years ago

sorry forgot to report back, works great @dcousens

thanks

0xashu commented 7 years ago

When I use bitcoin.address.toOutputScript to check a correct address,it's not work.

try {
    bitcoin.address.toOutputScript(‘1MPd34theheiGStaxaVBaLDFi75e8kkwvm’)
} catch (e) {
    console.log(e.message) // ' has no matching Script'
    return false
}

I found that this error occurred in this code when I tried debug

function decodeRaw (buffer) {
  var payload = buffer.slice(0, -4)
  var checksum = buffer.slice(-4)
  var newChecksum = sha256x2(payload)

  if (checksum[0] ^ newChecksum[0] |
      checksum[1] ^ newChecksum[1] |
      checksum[2] ^ newChecksum[2] |
      checksum[3] ^ newChecksum[3]) return

  return payload
}

Context: React Native 0.47 macOS Sierra

dcousens commented 7 years ago

@Aaaaaashu react-native is probably the issue, but I can't say for sure without further context/debugging information.

0xashu commented 7 years ago

@dcousens yep, when I use the Node environment, it's work.

coreyphillips commented 6 years ago

@Aaaaaashu, I ran into this as well. The issue is buffer.slice in RN. If you are not using buffer@5.0.5 or greater you will want to use a library similar to this in order to perform that action, "https://www.npmjs.com/package/arraybuffer.slice"

See: https://github.com/bitcoinjs/bitcoinjs-lib/issues/769

0xashu commented 6 years ago

@coreyphillips Thank you I solved this issue when I upgraded rn-nodeify and buffer.

maximilliangeorge commented 5 years ago

At time of writing the official releases (the npm repository) do not support regtest address validation. It took me a while to realise I had to update from the github repository directly, using a newer commit hash. Just thought I'd mention it if someone else has the same issue.