javascriptarraysbitcoinjs-lib

Convert UInt8Array to String


I'm working on an application involving cryptocurrency, and I'm having trouble with handling the conversion of some of the data involved.

I'm using bitcoinjs-lib to generate Bitcoin addresses. The addresses are created successfully, and my response object looks like the following:

address: "1Nnn9HpxgykWXxZX5rL3hIH7iikWkQaBSc"
balance: 0
currency: "BTC"
privateKey: Uint8Array(32) [86, 201, 0, 216, 118, 231, 201, 251, 161, 22, 223, 14, 234, 229, 168, 146, 41, 121, 182, 136, 176, 120, 185, 173, 181, 47, 228, 244, 107, 230, 29, 27]
publicKey: Uint8Array(33) [3, 233, 119, 81, 11, 119, 13, 133, 115, 183, 163, 90, 218, 2, 36, 41, 105, 158, 248, 131, 68, 234, 193, 110, 105, 72, 38, 110, 253, 192, 245, 108, 214]
wif: "Kz8QjBvSPjfRVxazJDwGEGwaoGTjRhFGe1MPsiPZRPpKEpidH7Qf"

I'm using an IndexedDB to store the created wallets. Since I'm generating different types of wallets, my database call looks like this:

{
 date: new Date(),
 coinType: crypto,
 isHDWallet: true,
 derivationPath: null,
 publicKey: bytesToString(Buffer.from(wallet.publicKey)) ?? null,
 privateKey: bytesToString(Buffer.from(wallet.privateKey)) ?? null,
 wif: wallet.wif ?? null,
 address: wallet.address ?? null,
 balance: wallet.balance ?? null,
 secret: wallet.secret ?? null,
 user_id: 1
}

My data is stored in my database just fine, except I can't correctly translate the UInt8Array into a string. I've tried nearly everything from this post, but have not had any success.

Here is the bytesToString function that I tried:

function bytesToString (bytes) {
        return String.fromCharCode.apply(null, bytes)
      }

I tried using Node's StringDecoder module with no success. I've also tried using Buffer.from(privateKey).toString('utf-8').

I've read that Bitcoin addresses use a base 58 encoding. I don't know if that's relevant or not.

I don't have any experience using buffers, or with any type of conversion like this. Any help would be greatly appreciated.


Solution

  • This is not UTF-8 encoded text, but just binary data. So you can forget about the linked Q/A, you are not in the same situation.

    Here it's all your choice as to how you'll encode it, some will prefer to convert it to an hex-dump

    const arr = new Uint8Array([86, 201, 0, 216, 118, 231, 201, 251, 161, 22, 223, 14, 234, 229, 168, 146, 41, 121, 182, 136, 176, 120, 185, 173, 181, 47, 228, 244, 107, 230, 29, 27]);
    const as_text = Array.from( arr )
      .map( (val) => val.toString( 16 ).padStart( 2,"0" ) )
      .join(" ");
    
    console.log( as_text );

    some will prefer to store it as base64:

    const arr = new Uint8Array([86, 201, 0, 216, 118, 231, 201, 251, 161, 22, 223, 14, 234, 229, 168, 146, 41, 121, 182, 136, 176, 120, 185, 173, 181, 47, 228, 244, 107, 230, 29, 27]);
    const bin = [];
    for (let i = 0; i < arr.length; i++) {
      bin.push( String.fromCharCode( arr[ i ] ) );
    }
    const as_text = btoa( bin.join( "" ) );
    console.log( as_text );

    some might prefer to store the binary data directly as TINYBLOB, though I'm really no expert in DB maintenance.