Skip to content

Commit

Permalink
fix: handle ledger amino messages (#215)
Browse files Browse the repository at this point in the history
* fix: handle ledger amino messages

* chore: add memo attribute to amino converter

* chore: add memo & remove unused parsing

* chore: branch cut release for sdk v1.13.1-beta.0 (#216)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* fix: update cosmjs for memo attribute & use cosmjs amino converters

* chore: branch cut release for sdk v1.13.1-beta.1 (#224)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* chore: bring back main version number & reorder changelog

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
odcey and github-actions[bot] authored Oct 25, 2023
1 parent e466ebe commit 2ef50f7
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 89 deletions.
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@

## [1.14.0](https://github.com/0xsquid/api-sdk/compare/v1.10.0...v1.14.0) (2023-09-26)

## [1.13.1-beta.1](https://github.com/0xsquid/api-sdk/compare/v1.14.1...v1.13.1-beta.1) (2023-10-25)


### Bug Fixes

* handle ledger amino messages ([89f8c32](https://github.com/0xsquid/api-sdk/commit/89f8c3282bcfd44d66d4c11ab703128ca65735ba))
* update cosmjs for memo attribute & use cosmjs amino converters ([7212228](https://github.com/0xsquid/api-sdk/commit/72122286ca3ce6ce1d34444094a553e86587c165))

## [1.13.1-beta.0](https://github.com/0xsquid/api-sdk/compare/v1.10.0...v1.13.1-beta.0) (2023-10-13)


### Bug Fixes

* handle ledger amino messages ([89f8c32](https://github.com/0xsquid/api-sdk/commit/89f8c3282bcfd44d66d4c11ab703128ca65735ba))

## [1.12.1](https://github.com/0xsquid/api-sdk/compare/v1.10.0...v1.12.1) (2023-09-06)

## [1.12.0](https://github.com/0xsquid/api-sdk/compare/v1.10.0...v1.12.0) (2023-08-29)
Expand Down Expand Up @@ -354,4 +369,4 @@
* remove extra data nested property for error handling refactor ([#40](https://github.com/0xsquid/api-sdk/issues/40)) ([b9a5069](https://github.com/0xsquid/api-sdk/commit/b9a5069f01abbd50d51a1061c6e53d711e5e4988))
* remove invalid setup secrets step ([#46](https://github.com/0xsquid/api-sdk/issues/46)) ([bf9664a](https://github.com/0xsquid/api-sdk/commit/bf9664a1afd1adf7586f4308d73d48c5f809b9ef))
* remove value from tx on send only ([2852e28](https://github.com/0xsquid/api-sdk/commit/2852e285620a247d92f3886782b83bf45883d109))
* rename enableForecall ([#39](https://github.com/0xsquid/api-sdk/issues/39)) ([cb515cd](https://github.com/0xsquid/api-sdk/commit/cb515cd892f675d2721444a3a24887926c62bc38))
* rename enableForecall ([#39](https://github.com/0xsquid/api-sdk/issues/39)) ([cb515cd](https://github.com/0xsquid/api-sdk/commit/cb515cd892f675d2721444a3a24887926c62bc38))
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
"dependencies": {
"axios": "^0.27.2",
"ethers": "^5.7.1",
"@cosmjs/encoding": "^0.31.0",
"@cosmjs/stargate": "^0.31.0",
"@cosmjs/encoding": "^0.31.3",
"@cosmjs/stargate": "^0.31.3",
"@cosmjs/cosmwasm-stargate": "^0.31.3",
"cosmjs-types": "^0.8.0"
},
"resolutions": {
Expand Down
58 changes: 48 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import {
SigningCosmWasmClient,
createWasmAminoConverters
} from "@cosmjs/cosmwasm-stargate";
import { toUtf8 } from "@cosmjs/encoding";
import {
calculateFee,
AminoTypes,
Coin,
GasPrice,
SigningStargateClient
SigningStargateClient,
calculateFee,
createIbcAminoConverters
} from "@cosmjs/stargate";
import axios, { AxiosInstance } from "axios";
import { MsgExecuteContract } from "cosmjs-types/cosmwasm/wasm/v1/tx";
import { BigNumber, ethers, UnsignedTransaction } from "ethers";

import { BigNumber, UnsignedTransaction, ethers } from "ethers";

import {
Allowance,
Expand All @@ -34,6 +40,8 @@ import {
} from "./types";

import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";
import { MsgExecuteContract } from "cosmjs-types/cosmwasm/wasm/v1/tx";
import Long from "long";
import { parseRouteResponse } from "./0xsquid/v1/route";
import { parseSdkInfoResponse } from "./0xsquid/v1/sdk-info";
import { parseStatusResponse } from "./0xsquid/v1/status";
Expand Down Expand Up @@ -221,7 +229,7 @@ export class Squid {
}

private async validateCosmosBalance(
signer: SigningStargateClient,
signer: SigningStargateClient | SigningCosmWasmClient,
signerAddress: string,
coin: Coin
): Promise<void> {
Expand Down Expand Up @@ -314,7 +322,9 @@ export class Squid {
// handle cosmos case
if (
signer instanceof SigningStargateClient ||
signer.constructor.name === "SigningStargateClient"
signer.constructor.name === "SigningStargateClient" ||
signer instanceof SigningCosmWasmClient ||
signer.constructor.name === "SigningCosmWasmClient"
) {
return await this.executeRouteCosmos(
signer as SigningStargateClient,
Expand Down Expand Up @@ -444,13 +454,12 @@ export class Squid {
}

private async executeRouteCosmos(
signer: SigningStargateClient,
signer: SigningStargateClient | SigningCosmWasmClient,
signerAddress: string,
route: RouteData
): Promise<TxRaw> {
const cosmosMsg: CosmosMsg = JSON.parse(route.transactionRequest!.data);
const msgs = [];

switch (cosmosMsg.msgTypeUrl) {
case IBC_TRANSFER_TYPE: {
msgs.push({
Expand Down Expand Up @@ -494,9 +503,24 @@ export class Squid {
const estimatedGas = await signer.simulate(signerAddress, msgs, "");
const gasMultiplier = Number(route.transactionRequest!.maxFeePerGas) || 1.3;

return signer.sign(
// This conversion is needed for Ledger, They only supports Amino messages
// TODO: At the moment there's a limit on Ledger Nano S models
// This limit prevents WASM_TYPE messages to be signed (because payload message is too big)
const aminoTypes = this.getAminoTypeConverters();
const formattedMsg = {
...msgs[0],
value: {
...msgs[0].value,
timeoutTimestamp: this.getTimeoutTimestamp()
}
};

const aminoMsg = aminoTypes.toAmino(formattedMsg);
const fromAminoMsg = aminoTypes.fromAmino(aminoMsg);

return (signer as SigningCosmWasmClient).sign(
signerAddress,
msgs,
[fromAminoMsg],
calculateFee(
Math.trunc(estimatedGas * gasMultiplier),
GasPrice.fromString(route.transactionRequest!.gasPrice)
Expand Down Expand Up @@ -731,6 +755,20 @@ export class Squid {
return response.data.price;
}

private getTimeoutTimestamp(): Long {
const PACKET_LIFETIME_NANOS = 3600 * 1_000_000_000; // 1 Hour

const currentTimeNanos = Math.floor(Date.now() * 1_000_000);
return Long.fromNumber(currentTimeNanos + PACKET_LIFETIME_NANOS);
}

private getAminoTypeConverters(): AminoTypes {
return new AminoTypes({
...createIbcAminoConverters(),
...createWasmAminoConverters()
});
}

public async getFromAmount({
fromToken,
toAmount,
Expand Down
174 changes: 98 additions & 76 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1003,121 +1003,138 @@
"@noble/hashes" "^1.0.0"
protobufjs "^6.8.8"

"@cosmjs/amino@^0.31.1":
version "0.31.1"
resolved "https://registry.yarnpkg.com/@cosmjs/amino/-/amino-0.31.1.tgz#e6b4adc3ebe19ddfd953c67ee04b1eae488238af"
integrity sha512-kkB9IAkNEUFtjp/uwHv95TgM8VGJ4VWfZwrTyLNqBDD1EpSX2dsNrmUe7k8OMPzKlZUFcKmD4iA0qGvIwzjbGA==
dependencies:
"@cosmjs/crypto" "^0.31.1"
"@cosmjs/encoding" "^0.31.1"
"@cosmjs/math" "^0.31.1"
"@cosmjs/utils" "^0.31.1"

"@cosmjs/crypto@^0.31.1":
version "0.31.1"
resolved "https://registry.yarnpkg.com/@cosmjs/crypto/-/crypto-0.31.1.tgz#ce4917df0f7b38f0909a32020907ccff04acefe6"
integrity sha512-4R/SqdzdVzd4E5dpyEh1IKm5GbTqwDogutyIyyb1bcOXiX/x3CrvPI9Tb4WSIMDLvlb5TVzu2YnUV51Q1+6mMA==
dependencies:
"@cosmjs/encoding" "^0.31.1"
"@cosmjs/math" "^0.31.1"
"@cosmjs/utils" "^0.31.1"
"@cosmjs/amino@^0.31.3":
version "0.31.3"
resolved "https://registry.yarnpkg.com/@cosmjs/amino/-/amino-0.31.3.tgz#0f4aa6bd68331c71bd51b187fa64f00eb075db0a"
integrity sha512-36emtUq895sPRX8PTSOnG+lhJDCVyIcE0Tr5ct59sUbgQiI14y43vj/4WAlJ/utSOxy+Zhj9wxcs4AZfu0BHsw==
dependencies:
"@cosmjs/crypto" "^0.31.3"
"@cosmjs/encoding" "^0.31.3"
"@cosmjs/math" "^0.31.3"
"@cosmjs/utils" "^0.31.3"

"@cosmjs/cosmwasm-stargate@^0.31.3":
version "0.31.3"
resolved "https://registry.yarnpkg.com/@cosmjs/cosmwasm-stargate/-/cosmwasm-stargate-0.31.3.tgz#13066822f111832d57c2c5acc9e697ed389713f8"
integrity sha512-Uv9TmCn3650gdFeZm7SEfUZF3uX3lfJfFhXOk6I2ZLr/FrKximnlb+vwAfZaZnWYvlA7qrKtHIjeRNHvT23zcw==
dependencies:
"@cosmjs/amino" "^0.31.3"
"@cosmjs/crypto" "^0.31.3"
"@cosmjs/encoding" "^0.31.3"
"@cosmjs/math" "^0.31.3"
"@cosmjs/proto-signing" "^0.31.3"
"@cosmjs/stargate" "^0.31.3"
"@cosmjs/tendermint-rpc" "^0.31.3"
"@cosmjs/utils" "^0.31.3"
cosmjs-types "^0.8.0"
long "^4.0.0"
pako "^2.0.2"

"@cosmjs/crypto@^0.31.3":
version "0.31.3"
resolved "https://registry.yarnpkg.com/@cosmjs/crypto/-/crypto-0.31.3.tgz#c752cb6d682fdc735dcb45a2519f89c56ba16c26"
integrity sha512-vRbvM9ZKR2017TO73dtJ50KxoGcFzKtKI7C8iO302BQ5p+DuB+AirUg1952UpSoLfv5ki9O416MFANNg8UN/EQ==
dependencies:
"@cosmjs/encoding" "^0.31.3"
"@cosmjs/math" "^0.31.3"
"@cosmjs/utils" "^0.31.3"
"@noble/hashes" "^1"
bn.js "^5.2.0"
elliptic "^6.5.4"
libsodium-wrappers-sumo "^0.7.11"

"@cosmjs/encoding@^0.31.0", "@cosmjs/encoding@^0.31.1":
version "0.31.1"
resolved "https://registry.yarnpkg.com/@cosmjs/encoding/-/encoding-0.31.1.tgz#0041b2650c443d883e22f27c7d3cd7b844c6d0ec"
integrity sha512-IuxP6ewwX6vg9sUJ8ocJD92pkerI4lyG8J5ynAM3NaX3q+n+uMoPRSQXNeL9bnlrv01FF1kIm8if/f5F7ZPtkA==
"@cosmjs/encoding@^0.31.3":
version "0.31.3"
resolved "https://registry.yarnpkg.com/@cosmjs/encoding/-/encoding-0.31.3.tgz#2519d9c9ae48368424971f253775c4580b54c5aa"
integrity sha512-6IRtG0fiVYwyP7n+8e54uTx2pLYijO48V3t9TLiROERm5aUAIzIlz6Wp0NYaI5he9nh1lcEGJ1lkquVKFw3sUg==
dependencies:
base64-js "^1.3.0"
bech32 "^1.1.4"
readonly-date "^1.0.0"

"@cosmjs/json-rpc@^0.31.1":
version "0.31.1"
resolved "https://registry.yarnpkg.com/@cosmjs/json-rpc/-/json-rpc-0.31.1.tgz#54d1064adb3ebd0412b04f87a3c2029384e4cb5e"
integrity sha512-gIkCj2mUDHAxvmJnHtybXtMLZDeXrkDZlujjzhvJlWsIuj1kpZbKtYqh+eNlfwhMkMMAlQa/y4422jDmizW+ng==
"@cosmjs/json-rpc@^0.31.3":
version "0.31.3"
resolved "https://registry.yarnpkg.com/@cosmjs/json-rpc/-/json-rpc-0.31.3.tgz#11e5cf0f6d9ab426dff470bb8d68d5d31cd6ab13"
integrity sha512-7LVYerXjnm69qqYR3uA6LGCrBW2EO5/F7lfJxAmY+iII2C7xO3a0vAjMSt5zBBh29PXrJVS6c2qRP22W1Le2Wg==
dependencies:
"@cosmjs/stream" "^0.31.1"
"@cosmjs/stream" "^0.31.3"
xstream "^11.14.0"

"@cosmjs/math@^0.31.1":
version "0.31.1"
resolved "https://registry.yarnpkg.com/@cosmjs/math/-/math-0.31.1.tgz#74c02cf237c2996b77661b636b014168b18d95e6"
integrity sha512-kiuHV6m6DSB8/4UV1qpFhlc4ul8SgLXTGRlYkYiIIP4l0YNeJ+OpPYaOlEgx4Unk2mW3/O2FWYj7Jc93+BWXng==
"@cosmjs/math@^0.31.3":
version "0.31.3"
resolved "https://registry.yarnpkg.com/@cosmjs/math/-/math-0.31.3.tgz#767f7263d12ba1b9ed2f01f68d857597839fd957"
integrity sha512-kZ2C6glA5HDb9hLz1WrftAjqdTBb3fWQsRR+Us2HsjAYdeE6M3VdXMsYCP5M3yiihal1WDwAY2U7HmfJw7Uh4A==
dependencies:
bn.js "^5.2.0"

"@cosmjs/proto-signing@^0.31.1":
version "0.31.1"
resolved "https://registry.yarnpkg.com/@cosmjs/proto-signing/-/proto-signing-0.31.1.tgz#3929d5bee3c88c42b3bc3c4b9db4ab3bddb684c4"
integrity sha512-hipbBVrssPu+jnmRzQRP5hhS/mbz2nU7RvxG/B1ZcdNhr1AtZC5DN09OTUoEpMSRgyQvScXmk/NTbyf+xmCgYg==
"@cosmjs/proto-signing@^0.31.3":
version "0.31.3"
resolved "https://registry.yarnpkg.com/@cosmjs/proto-signing/-/proto-signing-0.31.3.tgz#20440b7b96fb2cd924256a10e656fd8d4481cdcd"
integrity sha512-24+10/cGl6lLS4VCrGTCJeDRPQTn1K5JfknzXzDIHOx8THR31JxA7/HV5eWGHqWgAbudA7ccdSvEK08lEHHtLA==
dependencies:
"@cosmjs/amino" "^0.31.1"
"@cosmjs/crypto" "^0.31.1"
"@cosmjs/encoding" "^0.31.1"
"@cosmjs/math" "^0.31.1"
"@cosmjs/utils" "^0.31.1"
"@cosmjs/amino" "^0.31.3"
"@cosmjs/crypto" "^0.31.3"
"@cosmjs/encoding" "^0.31.3"
"@cosmjs/math" "^0.31.3"
"@cosmjs/utils" "^0.31.3"
cosmjs-types "^0.8.0"
long "^4.0.0"

"@cosmjs/socket@^0.31.1":
version "0.31.1"
resolved "https://registry.yarnpkg.com/@cosmjs/socket/-/socket-0.31.1.tgz#887d4e9b6aad4b3d264d64a47bdb40bcfa9802ff"
integrity sha512-XTtEr+x3WGbqkzoGX0sCkwVqf5n+bBqDwqNgb+DWaBABQxHVRuuainrTVp0Yc91D3Iy2twLQzeBA9OrRxDSerw==
"@cosmjs/socket@^0.31.3":
version "0.31.3"
resolved "https://registry.yarnpkg.com/@cosmjs/socket/-/socket-0.31.3.tgz#52086380f4de2fc3514b90b0484b4b1c4c50e39e"
integrity sha512-aqrDGGi7os/hsz5p++avI4L0ZushJ+ItnzbqA7C6hamFSCJwgOkXaOUs+K9hXZdX4rhY7rXO4PH9IH8q09JkTw==
dependencies:
"@cosmjs/stream" "^0.31.1"
"@cosmjs/stream" "^0.31.3"
isomorphic-ws "^4.0.1"
ws "^7"
xstream "^11.14.0"

"@cosmjs/stargate@^0.31.0":
version "0.31.1"
resolved "https://registry.yarnpkg.com/@cosmjs/stargate/-/stargate-0.31.1.tgz#7e2b0fd6f181250915b1d73ecf9dfbab6f3cdd0d"
integrity sha512-TqOJZYOH5W3sZIjR6949GfjhGXO3kSHQ3/KmE+SuKyMMmQ5fFZ45beawiRtVF0/CJg5RyPFyFGJKhb1Xxv3Lcg==
"@cosmjs/stargate@^0.31.3":
version "0.31.3"
resolved "https://registry.yarnpkg.com/@cosmjs/stargate/-/stargate-0.31.3.tgz#a2b38e398097a00f897dbd8f02d4d347d8fed818"
integrity sha512-53NxnzmB9FfXpG4KjOUAYAvWLYKdEmZKsutcat/u2BrDXNZ7BN8jim/ENcpwXfs9/Og0K24lEIdvA4gsq3JDQw==
dependencies:
"@confio/ics23" "^0.6.8"
"@cosmjs/amino" "^0.31.1"
"@cosmjs/encoding" "^0.31.1"
"@cosmjs/math" "^0.31.1"
"@cosmjs/proto-signing" "^0.31.1"
"@cosmjs/stream" "^0.31.1"
"@cosmjs/tendermint-rpc" "^0.31.1"
"@cosmjs/utils" "^0.31.1"
"@cosmjs/amino" "^0.31.3"
"@cosmjs/encoding" "^0.31.3"
"@cosmjs/math" "^0.31.3"
"@cosmjs/proto-signing" "^0.31.3"
"@cosmjs/stream" "^0.31.3"
"@cosmjs/tendermint-rpc" "^0.31.3"
"@cosmjs/utils" "^0.31.3"
cosmjs-types "^0.8.0"
long "^4.0.0"
protobufjs "~6.11.3"
xstream "^11.14.0"

"@cosmjs/stream@^0.31.1":
version "0.31.1"
resolved "https://registry.yarnpkg.com/@cosmjs/stream/-/stream-0.31.1.tgz#01bab56278bfe32cd601043949fcdc021a5f7ca7"
integrity sha512-xsIGD9bpBvYYZASajCyOevh1H5pDdbOWmvb4UwGZ78doGVz3IC3Kb9BZKJHIX2fjq9CMdGVJHmlM+Zp5aM8yZA==
"@cosmjs/stream@^0.31.3":
version "0.31.3"
resolved "https://registry.yarnpkg.com/@cosmjs/stream/-/stream-0.31.3.tgz#53428fd62487ec08fc3886a50a3feeb8b2af2e66"
integrity sha512-8keYyI7X0RjsLyVcZuBeNjSv5FA4IHwbFKx7H60NHFXszN8/MvXL6aZbNIvxtcIHHsW7K9QSQos26eoEWlAd+w==
dependencies:
xstream "^11.14.0"

"@cosmjs/tendermint-rpc@^0.31.1":
version "0.31.1"
resolved "https://registry.yarnpkg.com/@cosmjs/tendermint-rpc/-/tendermint-rpc-0.31.1.tgz#0699c6046fc8afd1eee3c648cfb3b896119ce52e"
integrity sha512-KX+wwi725sSePqIxfMPPOqg+xTETV8BHGOBhRhCZXEl5Fq48UlXXq3/yG1sn7K67ADC0kqHqcCF41Wn1GxNNPA==
dependencies:
"@cosmjs/crypto" "^0.31.1"
"@cosmjs/encoding" "^0.31.1"
"@cosmjs/json-rpc" "^0.31.1"
"@cosmjs/math" "^0.31.1"
"@cosmjs/socket" "^0.31.1"
"@cosmjs/stream" "^0.31.1"
"@cosmjs/utils" "^0.31.1"
"@cosmjs/tendermint-rpc@^0.31.3":
version "0.31.3"
resolved "https://registry.yarnpkg.com/@cosmjs/tendermint-rpc/-/tendermint-rpc-0.31.3.tgz#d1a2bc5b3c98743631c9b55888589d352403c9b3"
integrity sha512-s3TiWkPCW4QceTQjpYqn4xttUJH36mTPqplMl+qyocdqk5+X5mergzExU/pHZRWQ4pbby8bnR7kMvG4OC1aZ8g==
dependencies:
"@cosmjs/crypto" "^0.31.3"
"@cosmjs/encoding" "^0.31.3"
"@cosmjs/json-rpc" "^0.31.3"
"@cosmjs/math" "^0.31.3"
"@cosmjs/socket" "^0.31.3"
"@cosmjs/stream" "^0.31.3"
"@cosmjs/utils" "^0.31.3"
axios "^0.21.2"
readonly-date "^1.0.0"
xstream "^11.14.0"

"@cosmjs/utils@^0.31.1":
version "0.31.1"
resolved "https://registry.yarnpkg.com/@cosmjs/utils/-/utils-0.31.1.tgz#e6055cd7d722fa72df9cbd0d39cd1f7a9ac80483"
integrity sha512-n4Se1wu4GnKwztQHNFfJvUeWcpvx3o8cWhSbNs9JQShEuB3nv3R5lqFBtDCgHZF/emFQAP+ZjF8bTfCs9UBGhA==
"@cosmjs/utils@^0.31.3":
version "0.31.3"
resolved "https://registry.yarnpkg.com/@cosmjs/utils/-/utils-0.31.3.tgz#f97bbfda35ad69e80cd5c7fe0a270cbda16db1ed"
integrity sha512-VBhAgzrrYdIe0O5IbKRqwszbQa7ZyQLx9nEQuHQ3HUplQW7P44COG/ye2n6AzCudtqxmwdX7nyX8ta1J07GoqA==

"@cspotcode/source-map-support@^0.8.0":
version "0.8.1"
Expand Down Expand Up @@ -5859,6 +5876,11 @@ package-json@^8.1.0:
registry-url "^6.0.0"
semver "^7.3.7"

pako@^2.0.2:
version "2.1.0"
resolved "https://registry.yarnpkg.com/pako/-/pako-2.1.0.tgz#266cc37f98c7d883545d11335c00fbd4062c9a86"
integrity sha512-w+eufiZ1WuJYgPXbV/PO3NCMEc3xqylkKHzp8bxp1uW4qaSNQUkwmLLEc3kKsfz8lpV1F8Ht3U1Cm+9Srog2ug==

parent-module@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/parent-module/-/parent-module-1.0.1.tgz#691d2709e78c79fae3a156622452d00762caaaa2"
Expand Down

0 comments on commit 2ef50f7

Please sign in to comment.