Bitcoinjs-lib: Ability to validate an address

Created on 14 Sep 2017  ·  19Comments  ·  Source: bitcoinjs/bitcoinjs-lib

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

how to / question / docs

Most helpful comment

@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)

All 19 comments

(edited title to reflect email context)

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

@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)

@dcousens agree, that looks great 👍

@jackmallers that works ^, if that resolves your question

@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

How did you create them?
And what is NP2WKH?

@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>

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

Nested Pay 2 Witness Key Hash

@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

@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.

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.

sorry forgot to report back, works great @dcousens

thanks

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

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

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

@Aaaaaashu, I ran into this as well. The issue is buffer.slice in RN. If you are not using [email protected] 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

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

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  ·  3Comments

askucher picture askucher  ·  3Comments

prahaladbelavadi picture prahaladbelavadi  ·  3Comments

Beardcoding picture Beardcoding  ·  3Comments

namnv04 picture namnv04  ·  3Comments