From c0a11d44860e1e297d586e0eaf36b96ab7d956ed Mon Sep 17 00:00:00 2001 From: Hedi EDELBLOUTE Date: Fri, 15 Sep 2023 14:29:22 +0200 Subject: [PATCH] fix injective first tx --- .../src/families/cosmos/api/Cosmos.test.ts | 14 +++++++++++--- .../src/families/cosmos/api/Cosmos.ts | 8 ++++++-- .../src/families/cosmos/chain/Injective.ts | 1 + .../src/families/cosmos/chain/cosmosBase.ts | 1 + .../src/families/cosmos/js-prepareTransaction.ts | 5 ++++- .../src/families/cosmos/js-signOperation.ts | 4 +++- 6 files changed, 26 insertions(+), 7 deletions(-) diff --git a/libs/ledger-live-common/src/families/cosmos/api/Cosmos.test.ts b/libs/ledger-live-common/src/families/cosmos/api/Cosmos.test.ts index a84a081ca74f..86212348bdb0 100644 --- a/libs/ledger-live-common/src/families/cosmos/api/Cosmos.test.ts +++ b/libs/ledger-live-common/src/families/cosmos/api/Cosmos.test.ts @@ -33,7 +33,7 @@ describe("CosmosApi", () => { }, } as AxiosResponse); - const account = await cosmosApi.getAccount("addr"); + const account = await cosmosApi.getAccount("addr", "default"); expect(account.accountNumber).toEqual(2); expect(account.sequence).toEqual(42); expect(account.pubKey).toEqual("k2"); @@ -47,7 +47,7 @@ describe("CosmosApi", () => { }, } as AxiosResponse); - const account = await cosmosApi.getAccount("addr"); + const account = await cosmosApi.getAccount("addr", "default"); expect(account.accountNumber).toEqual(1); expect(account.sequence).toEqual(0); expect(account.pubKey).toEqual("k"); @@ -58,9 +58,17 @@ describe("CosmosApi", () => { mockedNetwork.mockImplementation(() => { throw new Error(); }); - const account = await cosmosApi.getAccount("addr"); + const account = await cosmosApi.getAccount("addr", "default"); expect(account.sequence).toEqual(0); }); + + it("should return default pubkeytype value if network fails", async () => { + mockedNetwork.mockImplementation(() => { + throw new Error(); + }); + const account = await cosmosApi.getAccount("addr", "default"); + expect(account.pubKeyType).toEqual("default"); + }); }); describe("simulate", () => { diff --git a/libs/ledger-live-common/src/families/cosmos/api/Cosmos.ts b/libs/ledger-live-common/src/families/cosmos/api/Cosmos.ts index 16df77870b76..6448d54fa6b7 100644 --- a/libs/ledger-live-common/src/families/cosmos/api/Cosmos.ts +++ b/libs/ledger-live-common/src/families/cosmos/api/Cosmos.ts @@ -5,6 +5,7 @@ import { Operation } from "@ledgerhq/types-live"; import BigNumber from "bignumber.js"; import { patchOperationWithHash } from "../../../operation"; import cryptoFactory from "../chain/chain"; +import cosmosBase from "../chain/cosmosBase"; import { CosmosDelegation, CosmosDelegationStatus, @@ -16,9 +17,11 @@ import { export class CosmosAPI { protected defaultEndpoint: string; private version: string; + private chainInstance: cosmosBase; constructor(currencyId: string) { const crypto = cryptoFactory(currencyId); + this.chainInstance = crypto; this.defaultEndpoint = crypto.lcd; this.version = crypto.version; } @@ -47,7 +50,7 @@ export class CosmosAPI { unbondings, withdrawAddress, ] = await Promise.all([ - this.getAccount(address), + this.getAccount(address, this.chainInstance.defaultPubKeyType), this.getAllBalances(address, currency), this.getHeight(), this.getTransactions(address, 100), @@ -74,11 +77,12 @@ export class CosmosAPI { getAccount = async ( address: string, + defaultPubKeyType: string, ): Promise<{ accountNumber: number; sequence: number; pubKeyType: string; pubKey: string }> => { const accountData = { accountNumber: 0, sequence: 0, - pubKeyType: "/cosmos.crypto.secp256k1.PubKey", + pubKeyType: defaultPubKeyType, pubKey: "", }; diff --git a/libs/ledger-live-common/src/families/cosmos/chain/Injective.ts b/libs/ledger-live-common/src/families/cosmos/chain/Injective.ts index c717e65f7ba6..1ef919df29fa 100644 --- a/libs/ledger-live-common/src/families/cosmos/chain/Injective.ts +++ b/libs/ledger-live-common/src/families/cosmos/chain/Injective.ts @@ -14,6 +14,7 @@ class Injective extends CosmosBase { this.unbondingPeriod = 21; this.prefix = "inj"; this.validatorPrefix = `${this.prefix}valoper`; + this.defaultPubKeyType = "/injective.crypto.v1beta1.ethsecp256k1.PubKey"; } } diff --git a/libs/ledger-live-common/src/families/cosmos/chain/cosmosBase.ts b/libs/ledger-live-common/src/families/cosmos/chain/cosmosBase.ts index f04ee27f3518..d538e860c87a 100644 --- a/libs/ledger-live-common/src/families/cosmos/chain/cosmosBase.ts +++ b/libs/ledger-live-common/src/families/cosmos/chain/cosmosBase.ts @@ -5,6 +5,7 @@ abstract class cosmosBase { abstract ledgerValidator?: string; abstract validatorPrefix: string; abstract prefix: string; + defaultPubKeyType = "/cosmos.crypto.secp256k1.PubKey"; defaultGas = 100000; minGasPrice = 0.0025; version = "v1beta1"; diff --git a/libs/ledger-live-common/src/families/cosmos/js-prepareTransaction.ts b/libs/ledger-live-common/src/families/cosmos/js-prepareTransaction.ts index 74c3ad3be805..83baeb1ae81f 100644 --- a/libs/ledger-live-common/src/families/cosmos/js-prepareTransaction.ts +++ b/libs/ledger-live-common/src/families/cosmos/js-prepareTransaction.ts @@ -56,7 +56,10 @@ export const getEstimatedFees = async ( const cosmosAPI = new CosmosAPI(account.currency.id); const { protoMsgs } = txToMessages(account, transaction); - const { sequence, pubKeyType, pubKey } = await cosmosAPI.getAccount(account.freshAddress); + const { sequence, pubKeyType, pubKey } = await cosmosAPI.getAccount( + account.freshAddress, + chainInstance.defaultPubKeyType, + ); const signature = new Uint8Array(Buffer.from(account.seedIdentifier, "hex")); const txBytes = buildTransaction({ diff --git a/libs/ledger-live-common/src/families/cosmos/js-signOperation.ts b/libs/ledger-live-common/src/families/cosmos/js-signOperation.ts index 96a030fadf6d..5770ae0507bc 100644 --- a/libs/ledger-live-common/src/families/cosmos/js-signOperation.ts +++ b/libs/ledger-live-common/src/families/cosmos/js-signOperation.ts @@ -20,10 +20,12 @@ const signOperation: SignOperationFnSignature = ({ account, deviceI async function main() { const cosmosAPI = new CosmosAPI(account.currency.id); + const chainInstance = cryptoFactory(account.currency.id); + const { accountNumber, sequence, pubKeyType } = await cosmosAPI.getAccount( account.freshAddress, + chainInstance.defaultPubKeyType, ); - const chainInstance = cryptoFactory(account.currency.id); o.next({ type: "device-signature-requested" }); const { aminoMsgs, protoMsgs } = txToMessages(account, transaction); if (transaction.fees == null || transaction.gas == null) {