dokichan
dokichan

Reputation: 591

How to decode base58 solana transaction info?

I am using solana API and as response from getTransaction function I get some data. Generally speaking, I am interested in data filed. Documentation says:

data: - The program input data encoded in a base-58 string.

And here is the problem, I cannot convert this base-58 string, which looks like this 3Bxs43eF7ZuXE46B - to something more readable.

Converting by using some default decoders doesn't work. So, how can I do this?

Upvotes: 5

Views: 11764

Answers (3)

Radinski Kire
Radinski Kire

Reputation: 1

You need to use bs58.default.decode(PRIVATE_KEY) instead of bs58.decode(PRIVATE_KEY)

Upvotes: 0

mikemaccana
mikemaccana

Reputation: 123058

Solana generally uses the bs58 module, so to decode your string:

import bs58 from 'bs58';
const bytes = bs58.decode('3Bxs43eF7ZuXE46B')

Upvotes: 2

Ian Samz
Ian Samz

Reputation: 2109

I know this is a bit off topic, but this is how to save and read Data on solana using @solana/web3.js and decoding the data using bs58 and buffers

const web3 = require('@solana/web3.js');
var bs58 = require('bs58');
let keypair;
const memoProgramId = "MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr";
let connection;


const establishConnection = async () =>{
    let rpcUrl = web3.clusterApiUrl('devnet')
    connection = new web3.Connection(rpcUrl, 'confirmed');   
    console.log('Connection to cluster established:', rpcUrl);
}

const connectWallet = async () => {
    let secretKey = Uint8Array.from(enter_secret);
    keypair = web3.Keypair.fromSecretKey(secretKey);
    console.log('keypair created: ' + keypair.publicKey.toString());
}

saveData = async (data) => {
    let transferTransaction = new web3.Transaction();

    transferTransaction.add(new web3.TransactionInstruction({
        programId: memoProgramId,
        keys: [{
            pubkey: keypair.publicKey,
            isSigner: true,
            isWritable: false,
        }],
        data: Buffer.from(JSON.stringify(data))
    }))

    const transcationHash =  await web3.sendAndConfirmTransaction(
        connection, 
        transferTransaction, 
        [keypair]
    );

    return transcationHash;
}

readTransaction = async (signature) => {
    const transaction = await connection.getTransaction(signature);
    return transaction.transaction.message.instructions[0].data;
}

saveReadData = async () => {
    const signature = await saveData({
        amount: 1,
        isWon: true,
        ROI: 2,
    });

    console.log(signature);

    const b58Address = await readTransaction(signature);
    const dataAsUint8Arr = bs58.decode(b58Address);
    const jsonString = new Buffer.from(dataAsUint8Arr).toString('utf8');
    const data = JSON.parse(jsonString);

    console.log(data);
}

initConnection = async () => {
    await establishConnection();
    await connectWallet();
}

initTestReadSaveData = async () => {
    await initConnection();
    await saveReadData();
}

initTestReadSaveData();

Upvotes: 3

Related Questions