Convert from byte array to hexadecimal using bitcoinjs-min.js

I am trying to generate a public key from the following x and y of a Q object in a browser. The problem is to use this public key to verify the JWT. I need to get the hexadecimal key format. I use keypair from src = "bitcoinjs.min.js", which does not allow me to get the hexadecimal form of the public key. Is there a library or function to convert it to hexadecimal?

// Taking reference from http://procbits.com/2013/08/27/generating-a-bitcoin-address-with-javascript
var pubX = hdnode.keyPair.Q.x.toByteArrayUnsigned();
var pubY = hdnode.keyPair.Q.y.toByteArrayUnsigned();
var publicKeyBytes = pubX.concat(pubY);
publicKeyBytes.unshift(0x04);

meanwhile I tried

<script src="http://peterolson.github.com/BigInteger.js/BigInteger.min.js"></script>
var publicKeyInt = BigInt.fromByteArrayUnsigned(publicKeyBytes);

but it does not work Thanks in Advance

+4
source share
2 answers

: , .

function toHexString(bytes) {
  return bytes.map(function(byte) {
    return (byte & 0xFF).toString(16)
  }).join('')
}

@derekdreery :)

0

,

: -

function getHexArray(key) {
    function num2hex(num) {
        return num > 9 ? num + 55 : num + 48;
    }
    var hex_key = [];
    var lower, upper;
    for (var i = 0; i < key.length; i++) {
        lower = key[i] & 0x0f;
        upper = key[i] >> 4;
        return String.fromCharCode(num2hex(upper)) + 
            String.fromCharCode(num2hex(lower));
    }
    return hex_key;
}

, hex, , , ( )

2 char, .

+1

Source: https://habr.com/ru/post/1619897/


All Articles