Hedera Rust SDK unable to encoded transaction

33 views Asked by At

Hy everyone, I am trying to create freeze TransferTrasnaction using Rust SDK. I now it's still in development process but I was wondering if there is a way. What I want to do is create transaction on one client that will pay fees, freeze transaction convert it to bytes and base64 encode it. Then encoded transaction will be sent to to be signed by user that should be sending hbar. I did it in javascript and it is working but when i try to do it using Rust SDK i am always getting KEY_PREFIX_MISMATCH. What am I doing wrong?

use crate::{contract::HederaServiceContract, data::TransactionData};
use async_trait::async_trait;
use base64::{engine::general_purpose, Engine as _};
use error::Error;
use hedera::{AccountId, Client, PrivateKey, TransferTransaction};
use std::str::FromStr;

pub struct HederaService {
    client: Client,
}

impl HederaService {
    pub fn new(
        is_dev: String,
        account_id: String,
        private_key: String,
    ) -> Result<HederaService, Error> {
        let account_id = AccountId::from_str(&account_id)?;
        let private_key = PrivateKey::from_str_der(&private_key)?;

        let client = if is_dev == "true" {
            Client::for_testnet()
        } else {
            Client::for_mainnet()
        };

        client.set_operator(account_id, private_key);

        Ok(HederaService { client })
    }
}

#[async_trait]
impl HederaServiceContract for HederaService {
    async fn make_unsigned_transaction(&self, data: TransactionData) -> Result<String, Error> {
        let mut transaction = TransferTransaction::new();
        transaction
            .node_account_ids([(AccountId::from(3))])
            .hbar_transfer(data.sender_account_id, -data.amount)
            .hbar_transfer(data.reciever_account_id, data.amount)
            .transaction_memo(data.memo)
            .freeze_with(&self.client)?;

        transaction.sign_with_operator(&self.client)?;

        let tx_bytes = transaction.to_bytes()?;

        let base64_tx = general_purpose::STANDARD.encode(tx_bytes);

        Ok(base64_tx)
    }
}

example in js:

const JohnClient = Client.forTestnet();
JohnClient.setOperator(JohnAccountId, JohnPrivateKey);

const transaction = new TransferTransaction()
  .addHbarTransfer(AliceAccountId, new Hbar(-2)) /
  .addHbarTransfer(BobAccountId, new Hbar(2)) 
  .setTransactionMemo("TEST") 
  .freezeWith(JohnClient)

const frozenTransaction = await transaction.sign(JohnPrivateKey)


const txBytes = frozenTransaction.toBytes();


const base64Tx = Buffer.from(txBytes).toString('base64');
console.log(`Bas`e64 encoded tx: ${base64Tx}`)
return base64Tx

As I said same code is working in javascript but I am doing something wrong in here. Double checked keys and everything seems fine.

0

There are 0 answers