skip to Main Content

Just to note I do not know Javascript at all, I can script in PS/BATCH but that is my limit and even then it’s basic stuff.

Through google efforts I’ve ended up with what I want (decode Base58 string to HEX) but from two separate scripts, the first script outputs the results (byte array) to a file which I then manually copy/paste to the second script/section to convert it to HEX but if I could just pass those results as a variable to the next script/section and combine as a single script without outputting anything to file that would be great.

1st Script:

const bs58 = require('bs58');
const fs = require('fs');
b = bs58.decode('5P1mhvZrHosrH5svDZh78xH9JaSxzhnW9zMb9FRbFM2yH5yqq2udFN1FoaiiJTTUfEpeEP5hT71RX94w7RTkZ2ef');
j = new Uint8Array(b.buffer, b.byteOffset, b.byteLength / Uint8Array.BYTES_PER_ELEMENT);
//output to a file - i want this output to go to the 2nd function
fs.writeFileSync('mykey.json', `[${j}]`);

2nd Script:

const toHexString = (bytes) => {
  return Array.from(bytes, (byte) => {
    return ('0' + (byte & 0xff).toString(16)).slice(-2);
  }).join('');
};

//this array is what is output to the mykey.json file in the first script and i manually copy here
const byteArr = new Uint8Array([219,13,41,199,3,152,117,230,42,93,161,161,35,176,217,140,122,241,213,53,238,36,223,12,247,137,86,221,44,92,14,196,70,16,156,255,115,52,6,147,205,90,73,83,208,179,47,215,149,229,12,61,49,59,25,114,67,190,24,7,53,126,167,156]);

console.log(toHexString(byteArr));

Please note that the original string is a private key for a wallet, it is purely a test wallet that contains no funds and never will.

I tried commenting out the writeFileSync line and passing the ‘j’ in various ways to the next section as a variable for the array but I’m failing miserably.

2

Answers


  1. If you just need to pass j into your toHexString() function, you can combine the code into one file like below.

    I have removed the references to the filesystem, and the second Unit8Array cast, as you shouldn’t need to call that function again if j is already the output of that function.

    const bs58 = require('bs58');
    
    const toHexString = (bytes) => {
      return Array.from(bytes, (byte) => {
        return ('0' + (byte & 0xff).toString(16)).slice(-2);
      }).join('');
    };
    
    b = bs58.decode('5P1mhvZrHosrH5svDZh78xH9JaSxzhnW9zMb9FRbFM2yH5yqq2udFN1FoaiiJTTUfEpeEP5hT71RX94w7RTkZ2ef');
    j = new Uint8Array(b.buffer, b.byteOffset, b.byteLength / Uint8Array.BYTES_PER_ELEMENT);
    
    console.log(toHexString(j));
    Login or Signup to reply.
  2. const bs58 = require('bs58');
    
    // Decode Base58 string
    const base58String = '5P1mhvZrHosrH5svDZh78xH9JaSxzhnW9zMb9FRbFM2yH5yqq2udFN1FoaiiJTTUfEpeEP5hT71RX94w7RTkZ2ef';
    const decoded = bs58.decode(base58String);
    const byteArr = new Uint8Array(decoded.buffer, decoded.byteOffset, decoded.byteLength / Uint8Array.BYTES_PER_ELEMENT);
    
    // Function to convert byte array to hex string
    const toHexString = (bytes) => {
      return Array.from(bytes, (byte) => {
        return ('0' + (byte & 0xff).toString(16)).slice(-2);
      }).join('');
    };
    
    // Use the function to convert and log the hex string
    console.log(toHexString(byteArr));
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search