Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

Cosmos JS #1751

Merged
merged 66 commits into from Feb 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
9ff6edc
increase gas amplifier
alexalouit Jan 31, 2022
bb1888b
fix payload construction
alexalouit Jan 31, 2022
92f4d59
More accurate gas amplifier
alexalouit Jan 31, 2022
f29a4ae
increase gas amplifier
alexalouit Jan 31, 2022
957a48f
use same node for calculation and broadcast
alexalouit Jan 31, 2022
b120a76
fix amount payload
alexalouit Jan 31, 2022
dd45d6d
Merge remote-tracking branch 'upstream/cosmos-js' into cosmos-js
alexalouit Jan 31, 2022
aaa9494
fix fees/gas calculation
alexalouit Jan 31, 2022
67a4414
fix signature
alexalouit Feb 1, 2022
510c1e9
Merge remote-tracking branch 'upstream/cosmos-js' into cosmos-js
alexalouit Feb 1, 2022
0594ce0
Merge remote-tracking branch 'upstream/cosmos-js' into cosmos-js
alexalouit Feb 3, 2022
75bd779
fix fees regression
alexalouit Feb 3, 2022
fff173f
Merge remote-tracking branch 'upstream/cosmos-js' into cosmos-js
alexalouit Feb 3, 2022
014b551
More accurate pubkey selection
alexalouit Feb 3, 2022
bed90f0
don't use extra.tx_bytes
alexalouit Feb 3, 2022
4faf89d
fix pubkey selection
alexalouit Feb 3, 2022
a49d4ff
simplify hex serialization
alexalouit Feb 4, 2022
e8ad8f3
update transaction: more strict types
alexalouit Feb 10, 2022
a03fd8f
many things
alexalouit Feb 10, 2022
27947b3
accuracy
alexalouit Feb 10, 2022
1ea1afe
remove useless isPreValidation
alexalouit Feb 10, 2022
f941d07
fix strange edge effect of ledger live desktop
alexalouit Feb 10, 2022
cda9bc1
Update xpub during sync
alexalouit Feb 14, 2022
d65266d
temporary enable log for bot
alexalouit Feb 14, 2022
1c62e0c
Merge remote-tracking branch 'upstream/cosmos-js' into cosmos-js
alexalouit Feb 14, 2022
8df4111
LL-9159 cosmos node
alexalouit Feb 16, 2022
804fccd
Update js-signOperation.ts
alexalouit Feb 16, 2022
35ecf59
fix signature
alexalouit Feb 16, 2022
f45276e
Merge remote-tracking branch 'upstream/cosmos-js' into cosmos-js
alexalouit Feb 17, 2022
99dc488
fix redelegate payload
alexalouit Feb 17, 2022
1800f02
fix payload send transaction when sendmax
alexalouit Feb 17, 2022
3aca833
fix optimistic operation type
alexalouit Feb 18, 2022
e22a144
Merge remote-tracking branch 'upstream/cosmos-js' into cosmos-js
alexalouit Feb 18, 2022
e7fd38b
fix typo
alexalouit Feb 18, 2022
c7a0e69
bugfix
alexalouit Feb 18, 2022
b64368e
fix regression
alexalouit Feb 18, 2022
523f73f
Merge remote-tracking branch 'upstream/cosmos-js' into cosmos-js
alexalouit Feb 18, 2022
992193f
update optimistic operation
alexalouit Feb 18, 2022
e81b5d3
Merge remote-tracking branch 'upstream/cosmos-js' into cosmos-js
alexalouit Feb 18, 2022
71ea6f8
fix prettier
alexalouit Feb 18, 2022
e0f4fce
more deterministic transaction parsing
alexalouit Feb 18, 2022
96591a7
Merge remote-tracking branch 'upstream/cosmos-js' into cosmos-js
alexalouit Feb 18, 2022
0d409ca
clarify code readable
alexalouit Feb 18, 2022
2823fcd
adjust sender and recipient
alexalouit Feb 18, 2022
fd3f0aa
fix fees when is ibc transaction
alexalouit Feb 18, 2022
6685baa
fix redelegations data mapping
alexalouit Feb 19, 2022
892148b
fix mixed styles
alexalouit Feb 21, 2022
7446337
fix array cast type
alexalouit Feb 21, 2022
d0819d0
Merge remote-tracking branch 'upstream/cosmos-js' into cosmos-js
alexalouit Feb 21, 2022
890c0d4
fix more determinist operation data
alexalouit Feb 21, 2022
289ef6d
fix could not find optimisticOperation in redelegate transaction
alexalouit Feb 22, 2022
6121e72
Merge remote-tracking branch 'upstream/cosmos-js' into cosmos-js
alexalouit Feb 22, 2022
c113968
restore getStargateRewardsState methode
alexalouit Feb 22, 2022
6c72c62
Merge remote-tracking branch 'upstream/cosmos-js' into cosmos-js
alexalouit Feb 22, 2022
c3e6853
amount of the operation more close
alexalouit Feb 25, 2022
6631073
return transaction in prepareTransaction
alexalouit Feb 25, 2022
a4b0c61
keep immutable paradigm for prepareTransaction
alexalouit Feb 25, 2022
b5e6e7f
Merge remote-tracking branch 'upstream/cosmos-js' into cosmos-js
alexalouit Feb 25, 2022
8780fb4
fix amount update in prepareTransaction
alexalouit Feb 25, 2022
e6637dd
fix amount control to be more specific
alexalouit Feb 25, 2022
c0b2f6b
Merge remote-tracking branch 'upstream/cosmos-js' into cosmos-js
alexalouit Feb 25, 2022
774230a
fix BigNumber compare
alexalouit Feb 25, 2022
8820ebf
Merge remote-tracking branch 'upstream/cosmos-js' into cosmos-js
alexalouit Feb 25, 2022
8ee49ac
fix spendableBalance
alexalouit Feb 25, 2022
33cefe8
separate postBuildTransaction logic + fix use useAllAmount
alexalouit Feb 25, 2022
7e2f220
Merge remote-tracking branch 'upstream/cosmos-js' into cosmos-js
alexalouit Feb 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion src/families/cosmos/js-buildTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import { Account } from "../../types";
import { Transaction } from "./types";
import {
makeAuthInfoBytes,
Registry,
TxBodyEncodeObject,
} from "@cosmjs/proto-signing";
import {
MsgDelegate,
MsgUndelegate,
MsgBeginRedelegate,
} from "cosmjs-types/cosmos/staking/v1beta1/tx";
import { MsgWithdrawDelegatorReward } from "cosmjs-types/cosmos/distribution/v1beta1/tx";
import { SignMode } from "cosmjs-types/cosmos/tx/signing/v1beta1/signing";
import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";
import { getAccount } from "./api/Cosmos";
import BigNumber from "bignumber.js";

const buildTransaction = async (
export const buildTransaction = async (
account: Account,
transaction: Transaction
): Promise<any> => {
Expand Down Expand Up @@ -163,4 +178,56 @@ const buildTransaction = async (
return msg;
};

export const postBuildTransaction = async (
account: Account,
transaction: Transaction,
pubkey: any,
unsignedPayload: any,
signature: Uint8Array
): Promise<any> => {
const txBodyFields: TxBodyEncodeObject = {
typeUrl: "/cosmos.tx.v1beta1.TxBody",
value: {
messages: unsignedPayload,
memo: transaction.memo || "",
},
};

const registry = new Registry([
["/cosmos.staking.v1beta1.MsgDelegate", MsgDelegate],
["/cosmos.staking.v1beta1.MsgUndelegate", MsgUndelegate],
["/cosmos.staking.v1beta1.MsgBeginRedelegate", MsgBeginRedelegate],
[
"/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
MsgWithdrawDelegatorReward,
],
]);

const { sequence } = await getAccount(account.freshAddress);

const txBodyBytes = registry.encode(txBodyFields);

const authInfoBytes = makeAuthInfoBytes(
[{ pubkey, sequence }],
[
{
amount: transaction.fees?.toString() || new BigNumber(2500).toString(),
denom: account.currency.units[1].code,
},
],
transaction.gas?.toNumber() || new BigNumber(250000).toNumber(),
SignMode.SIGN_MODE_LEGACY_AMINO_JSON
);

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

const tx_bytes = Array.from(Uint8Array.from(TxRaw.encode(txRaw).finish()));

return tx_bytes;
};

export default buildTransaction;
84 changes: 22 additions & 62 deletions src/families/cosmos/js-prepareTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
import { Account } from "../../types";
import { Transaction } from "./types";
import BigNumber from "bignumber.js";
import { getAccount, simulate } from "./api/Cosmos";
import {
encodePubkey,
makeAuthInfoBytes,
Registry,
TxBodyEncodeObject,
} from "@cosmjs/proto-signing";
import { SignMode } from "cosmjs-types/cosmos/tx/signing/v1beta1/signing";
import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";
import {
MsgDelegate,
MsgUndelegate,
MsgBeginRedelegate,
} from "cosmjs-types/cosmos/staking/v1beta1/tx";
import { MsgWithdrawDelegatorReward } from "cosmjs-types/cosmos/distribution/v1beta1/tx";
import { simulate } from "./api/Cosmos";
import { encodePubkey } from "@cosmjs/proto-signing";
import { getEnv } from "../../env";
import buildTransaction from "./js-buildTransaction";
import { buildTransaction, postBuildTransaction } from "./js-buildTransaction";
import { getMaxEstimatedBalance } from "./logic";

const prepareTransaction = async (
account: Account,
transaction: Transaction
transaction: Transaction,
calculateFees?: boolean
): Promise<Transaction> => {
let memo = transaction.memo;
let fees = transaction.fees;
Expand All @@ -32,12 +20,19 @@ const prepareTransaction = async (
let gasQty = new BigNumber(250000);
const gasPrice = new BigNumber(getEnv("COSMOS_GAS_PRICE"));

if (transaction.useAllAmount) {
if (transaction.useAllAmount && !calculateFees) {
const tempTransaction = await prepareTransaction(
account,
{
...transaction,
amount: account.spendableBalance.minus(new BigNumber(2500)),
},
true
);

amount = getMaxEstimatedBalance(
account,
account.balance
.dividedBy(new BigNumber(getEnv("COSMOS_GAS_AMPLIFIER")))
.integerValue()
tempTransaction.fees || new BigNumber(0)
);
}

Expand All @@ -52,54 +47,19 @@ const prepareTransaction = async (

// be sure payload is complete
if (unsignedPayload) {
const txBodyFields: TxBodyEncodeObject = {
typeUrl: "/cosmos.tx.v1beta1.TxBody",
value: {
messages: unsignedPayload,
memo: transaction.memo || memo || "",
},
};

const registry = new Registry([
["/cosmos.staking.v1beta1.MsgDelegate", MsgDelegate],
["/cosmos.staking.v1beta1.MsgUndelegate", MsgUndelegate],
["/cosmos.staking.v1beta1.MsgBeginRedelegate", MsgBeginRedelegate],
[
"/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
MsgWithdrawDelegatorReward,
],
]);

const { sequence } = await getAccount(account.freshAddress);

const pubkey = encodePubkey({
type: "tendermint/PubKeySecp256k1",
value: Buffer.from(account.seedIdentifier, "hex").toString("base64"),
});

const txBodyBytes = registry.encode(txBodyFields);

const authInfoBytes = makeAuthInfoBytes(
[{ pubkey, sequence }],
[
{
amount:
transaction.fees?.toString() || new BigNumber(2500).toString(),
denom: account.currency.units[1].code,
},
],
transaction.gas?.toNumber() || new BigNumber(250000).toNumber(),
SignMode.SIGN_MODE_LEGACY_AMINO_JSON
const tx_bytes = await postBuildTransaction(
account,
{ ...transaction, memo, fees, gas, amount },
pubkey,
unsignedPayload,
new Uint8Array(Buffer.from(account.seedIdentifier, "hex"))
);

const txRaw = TxRaw.fromPartial({
bodyBytes: txBodyBytes,
authInfoBytes,
signatures: [new Uint8Array(Buffer.from(account.seedIdentifier, "hex"))],
});

const tx_bytes = Array.from(Uint8Array.from(TxRaw.encode(txRaw).finish()));

const gasUsed = await simulate(tx_bytes);

if (gasUsed.gt(0)) {
Expand Down
66 changes: 9 additions & 57 deletions src/families/cosmos/js-signOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,12 @@ import type { Transaction } from "./types";
import { getAccount, getChainId } from "./api/Cosmos";
import { Observable } from "rxjs";
import { withDevice } from "../../hw/deviceAccess";
import {
encodePubkey,
makeAuthInfoBytes,
Registry,
TxBodyEncodeObject,
} from "@cosmjs/proto-signing";
import { SignMode } from "cosmjs-types/cosmos/tx/signing/v1beta1/signing";
import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";
import { encodePubkey } from "@cosmjs/proto-signing";
import { encodeOperationId } from "../../operation";
import { LedgerSigner } from "@cosmjs/ledger-amino";
import { AminoTypes } from "@cosmjs/stargate";
import { stringToPath } from "@cosmjs/crypto";
import buildTransaction from "./js-buildTransaction";
import {
MsgDelegate,
MsgUndelegate,
MsgBeginRedelegate,
} from "cosmjs-types/cosmos/staking/v1beta1/tx";
import { MsgWithdrawDelegatorReward } from "cosmjs-types/cosmos/distribution/v1beta1/tx";
import { buildTransaction, postBuildTransaction } from "./js-buildTransaction";
import BigNumber from "bignumber.js";

const aminoTypes = new AminoTypes({ prefix: "cosmos" });
Expand All @@ -51,16 +38,6 @@ const signOperation = ({

const chainId = await getChainId();

const registry = new Registry([
["/cosmos.staking.v1beta1.MsgDelegate", MsgDelegate],
["/cosmos.staking.v1beta1.MsgUndelegate", MsgUndelegate],
["/cosmos.staking.v1beta1.MsgBeginRedelegate", MsgBeginRedelegate],
[
"/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
MsgWithdrawDelegatorReward,
],
]);

const hdPaths: any = stringToPath("m/" + account.freshAddressPath);

const ledgerSigner = new LedgerSigner(transport, {
Expand Down Expand Up @@ -109,40 +86,15 @@ const signOperation = ({
memo: transaction.memo || "",
});

const txBodyFields: TxBodyEncodeObject = {
typeUrl: "/cosmos.tx.v1beta1.TxBody",
value: {
messages: msgs.map((msg) => aminoTypes.fromAmino(msg)),
memo: transaction.memo || "",
},
};

const txBodyBytes = registry.encode(txBodyFields);

const authInfoBytes = makeAuthInfoBytes(
[{ pubkey, sequence }],
[
{
amount:
transaction.fees?.toString() || new BigNumber(2500).toString(),
denom: account.currency.units[1].code,
},
],
transaction.gas?.toNumber() || new BigNumber(250000).toNumber(),
SignMode.SIGN_MODE_LEGACY_AMINO_JSON
const tx_bytes = await postBuildTransaction(
account,
transaction,
pubkey,
unsignedPayload,
new Uint8Array(Buffer.from(signed.signature.signature, "base64"))
);

const txRaw = TxRaw.fromPartial({
bodyBytes: txBodyBytes,
authInfoBytes,
signatures: [
new Uint8Array(Buffer.from(signed.signature.signature, "base64")),
],
});

const signature = Buffer.from(TxRaw.encode(txRaw).finish()).toString(
"hex"
);
const signature = Buffer.from(tx_bytes).toString("hex");

if (cancelled) {
return;
Expand Down