Question

i'm trying to validate a dogecoin address using node.js javascript.

But i'm still trying to understand, the algorithm.

my current node.js version is v0.10.22

from what i was able to understand (correct me if i'm wrong) its a dogecoin address is no different from a bitcoin address.

So i try to do the following

  1. Base58 decode the address
  2. calculate sha256 of the returned result of a sha256 of the decoded address (two times sha256)
  3. do a comparison of the first 4 bytes of sha256 result with the last 4 bytes of the decoded address (i think i'm making a mistake here) ..

when i attempted to write this to javascript the base58 decoded result is correct, when i try to create a buffer from the decoded value , it throws error pointing that the value is not correct

var decodedHex = new Buffer(decodedBase58Result,'hex')

the above throws an error, if i do decodedBase58Result.toString() which will stringify the number the script continues but doesn't return expected result.

So, is there a solution to this? without depending on third-party api calls


Update: i researched a lot and i found a client-side validator, with a small piece of code i made an npm package

which validates the address without relying on any third party api/service calls.

Was it helpful?

Solution

decodedBase58Result for DBKh7QAP9gkXncVK32jtfae4QXChPwsyKH would be 1e43d1c5e88853622efe39e7a838cebf01cf3029589a614274.

crypto.createHash('sha256').update(crypto.createHash('sha256').update((new Buffer('1e43d1c5e88853622efe39e7a838cebf01cf3029589a614274', 'hex')).slice(0,21)).digest('buffer')).digest('buffer')
<SlowBuffer 9a 61 42 74 97 0b 64 ae 2e 7f d6 f8 b1 81 90 12 f3 5e 3a 0b a1 f4 f2 c2 47 fe 20 7e 4f a1 27 25>

Compare last 4 bytes from the address - ...9a614274 - with the resulting hash - 9a614274..., if you get a match, address is valid.

So no, it's the same as bitcoin, code samples are here.

5.868133917508844e+59 is an address decoded as an integer. The issue with it is that javascript uses 52 bits to represent integers, but you have 200 bit address. So the decoder you're using won't work here.

OTHER TIPS

I wrote a module (coinstring) to do this. It's part of the CryptoCoinJS project which has a lot of components that you can use. It's very simple to use:

npm install --save coinstring@0.2.0

Example:

var coinstring = require('coinstring');

//0x1E => for public Dogecoin addresses
var isValid = coinstring.validate(0x1E, 'DBKh7QAP9gkXncVK32jtfae4QXChPwsyKH');
console.log(isValid); // => true

This works in the browser as well. Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top