InjectiveLabs / injective-ts

Collection of TypeScript packages that consume and interact with the Injective Chain
Apache License 2.0
130 stars 193 forks source link

pubKey does not match signer address #507

Closed muratcinar91 closed 2 hours ago

muratcinar91 commented 2 hours ago

I get the error ‘pubKey does not match signer address with signer index: 0: invalid pubkey’ during the broadcast phase when transferring injective tokens with the following code.

const { TxRestApi, MsgSend, BaseAccount, ChainRestAuthApi, createTransaction} = require('@injectivelabs/sdk-ts');
const { DirectSecp256k1HdWallet, encodePubkey, makeSignBytes} = require("@cosmjs/proto-signing");
const { stringToPath } = require("@cosmjs/crypto");
const { BigNumberInBase, getStdFee} = require('@injectivelabs/utils');
const Long = require('long')
const TxBody = require("base58");
const AuthInfo = require("base58");

const { CosmosTxV1Beta1Tx } = require("@injectivelabs/sdk-ts");
const TxRaw = CosmosTxV1Beta1Tx.TxRaw;

const restEndpoint = "https://sentry.lcd.injective.network";

const chainId = "injective-1"; 
const hdPath = stringToPath(injectiveAddressPath);

const createInjectiveWallet = async (mnemonic) => {
    const injectiveAddressPath = "m/44'/118'/0'/0/0";
    const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, {
        prefix: "inj",
        hdPaths: [stringToPath(injectiveAddressPath)],
    });

    const [account] = await wallet.getAccounts();
    return {
        address: account.address,
        pubKey: account.pubkey,
        wallet,
    };
};

const sendTokens = async (wallet, fromAddress, pubKey, toAddress, amount, denom, chainId, gasFee) => {

    const chainRestAuthApi = new ChainRestAuthApi(restEndpoint);
    const accountDetails = await chainRestAuthApi.fetchAccount(fromAddress);
    const baseAccount = BaseAccount.fromRestApi(accountDetails);

    const sequence = baseAccount.sequence;
    const accountNumber = baseAccount.accountNumber;

    const msg = MsgSend.fromJSON({
        amount: {
            amount: new BigNumberInBase(amount).toWei().toFixed(),
            denom,
        },
        srcInjectiveAddress: fromAddress,
        dstInjectiveAddress: toAddress
    });

    const { signDoc } = createTransaction({
        pubKey: Buffer.from(pubKey).toString('base64'),
        chainId: "injective-1",  
        fee: getStdFee({}),
        message: msg,
        sequence,
        accountNumber
        memo: 'this is test'
    });

    const signResponse = await wallet.signDirect(fromAddress, signDoc);

    const txRaw = TxRaw.fromPartial({
        bodyBytes: signResponse.signed.bodyBytes,
        authInfoBytes: signResponse.signed.authInfoBytes,
        signatures: [signResponse.signature.signature],
    });

    const txApi = new TxRestApi(restEndpoint);
    const result = await txApi.broadcastTx(txRaw);

    if (!result || result.tx_response.code !== 0) {
        throw new Error(`Transaction failed with code: ${result.tx_response.code}`);
    }
    console.log(`Transaction successful! TxHash: ${result.tx_response.txhash}`);
};

const transferInjectiveTokens = async () => {
        const mnemonic = "my mnemonic";
    const { address: fromAddress, pubKey, wallet } = await createInjectiveWallet(mnemonic);

    const toAddress = "inj1...."; 
    const amount = 0.1; 
    const denom = "inj"; 

    await sendTokens(wallet, fromAddress, pubKey, toAddress, amount, denom);
};

transferInjectiveTokens().catch((err) => {
    console.error(err);
});
bangjelkoski commented 2 hours ago

const injectiveAddressPath = "m/44'/118'/0'/0/0";

This is incorrect, Injective uses 60 for coinType (same as Ethereum)

muratcinar91 commented 1 hour ago

Hi @bangjelkoski, we have generated the address with 118 coinType and token transfer to this address has been made, can we recover these tokens?