mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-11 14:09:15 +00:00
Merge pull request #236 from CosmWasm/delegate
Add delegation message and demo
This commit is contained in:
commit
27644dfc96
38
packages/cli/examples/delegate.ts
Normal file
38
packages/cli/examples/delegate.ts
Normal file
@ -0,0 +1,38 @@
|
||||
const pen = await Secp256k1Pen.fromMnemonic("enlist hip relief stomach skate base shallow young switch frequent cry park");
|
||||
const senderAddress = pen.address("cosmos");
|
||||
|
||||
const client = new CosmosClient("http://localhost:1317");
|
||||
|
||||
const msg: MsgDelegate = {
|
||||
type: "cosmos-sdk/MsgDelegate",
|
||||
value: {
|
||||
delegator_address: senderAddress,
|
||||
// To get the proper validator address, start the demo chain (./scripts/wasmd/start.sh), then run:
|
||||
// curl http://localhost:1317/staking/validators | jq '.result[0].operator_address'
|
||||
validator_address: "cosmosvaloper1gjvanqxc774u6ed9thj4gpn9gj5zus5u32enqn",
|
||||
amount: coin(300000, "ustake"),
|
||||
}
|
||||
}
|
||||
const fee = {
|
||||
amount: coins(2000, "ucosm"),
|
||||
gas: "120000", // 120k
|
||||
};
|
||||
const memo = "Use your power wisely";
|
||||
|
||||
const chainId = await client.getChainId();
|
||||
console.log("Connected to chain:", chainId);
|
||||
|
||||
const { accountNumber, sequence } = await client.getNonce(senderAddress);
|
||||
console.log("Account/sequence:", accountNumber, sequence);
|
||||
|
||||
const signBytes = makeSignBytes([msg], fee, chainId, memo, accountNumber, sequence);
|
||||
const signature = await pen.sign(signBytes);
|
||||
const signedTx: StdTx = {
|
||||
msg: [msg],
|
||||
fee: fee,
|
||||
memo: memo,
|
||||
signatures: [signature],
|
||||
};
|
||||
|
||||
const result = await client.postTx(signedTx);
|
||||
console.log("Post result:", result);
|
@ -92,13 +92,16 @@ export function main(originalArgs: readonly string[]): void {
|
||||
"makeSignBytes",
|
||||
"IndexedTx",
|
||||
"Coin",
|
||||
"CosmosClient",
|
||||
"Msg",
|
||||
"MsgDelegate",
|
||||
"MsgSend",
|
||||
"Pen",
|
||||
"PubKey",
|
||||
"pubkeyToAddress",
|
||||
"RestClient",
|
||||
"Secp256k1Pen",
|
||||
"SigningCosmosClient",
|
||||
"StdFee",
|
||||
"StdTx",
|
||||
],
|
||||
|
@ -1,6 +1,15 @@
|
||||
import { Sha256 } from "@cosmjs/crypto";
|
||||
import { toBase64, toHex } from "@cosmjs/encoding";
|
||||
import { BroadcastMode, Coin, coins, makeSignBytes, MsgSend, StdFee, StdSignature } from "@cosmjs/sdk38";
|
||||
import {
|
||||
BroadcastMode,
|
||||
Coin,
|
||||
coins,
|
||||
makeSignBytes,
|
||||
MsgSend,
|
||||
StdFee,
|
||||
StdSignature,
|
||||
StdTx,
|
||||
} from "@cosmjs/sdk38";
|
||||
import pako from "pako";
|
||||
|
||||
import { isValidBuilder } from "./builder";
|
||||
@ -157,7 +166,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
|
||||
const chainId = await this.getChainId();
|
||||
const signBytes = makeSignBytes([storeCodeMsg], fee, chainId, memo, accountNumber, sequence);
|
||||
const signature = await this.signCallback(signBytes);
|
||||
const signedTx = {
|
||||
const signedTx: StdTx = {
|
||||
msg: [storeCodeMsg],
|
||||
fee: fee,
|
||||
memo: memo,
|
||||
@ -206,7 +215,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
|
||||
const signBytes = makeSignBytes([instantiateMsg], fee, chainId, memo, accountNumber, sequence);
|
||||
|
||||
const signature = await this.signCallback(signBytes);
|
||||
const signedTx = {
|
||||
const signedTx: StdTx = {
|
||||
msg: [instantiateMsg],
|
||||
fee: fee,
|
||||
memo: memo,
|
||||
@ -246,7 +255,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
|
||||
const chainId = await this.getChainId();
|
||||
const signBytes = makeSignBytes([executeMsg], fee, chainId, memo, accountNumber, sequence);
|
||||
const signature = await this.signCallback(signBytes);
|
||||
const signedTx = {
|
||||
const signedTx: StdTx = {
|
||||
msg: [executeMsg],
|
||||
fee: fee,
|
||||
memo: memo,
|
||||
@ -283,7 +292,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
|
||||
const chainId = await this.getChainId();
|
||||
const signBytes = makeSignBytes([sendMsg], fee, chainId, memo, accountNumber, sequence);
|
||||
const signature = await this.signCallback(signBytes);
|
||||
const signedTx = {
|
||||
const signedTx: StdTx = {
|
||||
msg: [sendMsg],
|
||||
fee: fee,
|
||||
memo: memo,
|
||||
|
@ -4,6 +4,7 @@ import { assert, sleep } from "@cosmjs/utils";
|
||||
import { Coin } from "./coins";
|
||||
import { CosmosClient, isPostTxFailure } from "./cosmosclient";
|
||||
import { makeSignBytes } from "./encoding";
|
||||
import { isMsgSend, MsgSend } from "./msgs";
|
||||
import { Secp256k1Pen } from "./pen";
|
||||
import { RestClient } from "./restclient";
|
||||
import { SigningCosmosClient } from "./signingcosmosclient";
|
||||
@ -15,7 +16,7 @@ import {
|
||||
wasmd,
|
||||
wasmdEnabled,
|
||||
} from "./testutils.spec";
|
||||
import { CosmosSdkTx, isMsgSend, MsgSend } from "./types";
|
||||
import { CosmosSdkTx } from "./types";
|
||||
|
||||
describe("CosmosClient.searchTx", () => {
|
||||
let sendSuccessful:
|
||||
|
@ -5,6 +5,7 @@ import { ReadonlyDate } from "readonly-date";
|
||||
import { CosmosClient, isPostTxFailure, PrivateCosmWasmClient } from "./cosmosclient";
|
||||
import { makeSignBytes } from "./encoding";
|
||||
import { findAttribute } from "./logs";
|
||||
import { MsgSend } from "./msgs";
|
||||
import { Secp256k1Pen } from "./pen";
|
||||
import cosmoshub from "./testdata/cosmoshub.json";
|
||||
import {
|
||||
@ -15,7 +16,7 @@ import {
|
||||
unused,
|
||||
wasmd,
|
||||
} from "./testutils.spec";
|
||||
import { MsgSend, StdFee } from "./types";
|
||||
import { StdFee } from "./types";
|
||||
|
||||
const blockTime = 1_000; // ms
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { toUtf8 } from "@cosmjs/encoding";
|
||||
|
||||
import { Msg, StdFee } from "./types";
|
||||
import { Msg } from "./msgs";
|
||||
import { StdFee } from "./types";
|
||||
|
||||
function sortJson(json: any): any {
|
||||
if (typeof json !== "object" || json === null) {
|
||||
@ -20,7 +21,12 @@ function sortJson(json: any): any {
|
||||
return result;
|
||||
}
|
||||
|
||||
interface SignJson {
|
||||
/**
|
||||
* The document to be signed
|
||||
*
|
||||
* @see https://docs.cosmos.network/master/modules/auth/03_types.html#stdsigndoc
|
||||
*/
|
||||
interface StdSignDoc {
|
||||
readonly account_number: string;
|
||||
readonly chain_id: string;
|
||||
readonly fee: StdFee;
|
||||
@ -37,7 +43,7 @@ export function makeSignBytes(
|
||||
accountNumber: number,
|
||||
sequence: number,
|
||||
): Uint8Array {
|
||||
const signJson: SignJson = {
|
||||
const signDoc: StdSignDoc = {
|
||||
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||
account_number: accountNumber.toString(),
|
||||
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||
@ -47,6 +53,6 @@ export function makeSignBytes(
|
||||
msgs: msgs,
|
||||
sequence: sequence.toString(),
|
||||
};
|
||||
const signMsg = sortJson(signJson);
|
||||
return toUtf8(JSON.stringify(signMsg));
|
||||
const sortedSignDoc = sortJson(signDoc);
|
||||
return toUtf8(JSON.stringify(sortedSignDoc));
|
||||
}
|
||||
|
@ -31,20 +31,10 @@ export {
|
||||
SearchTxsResponse,
|
||||
TxsResponse,
|
||||
} from "./restclient";
|
||||
export { isMsgDelegate, isMsgSend, Msg, MsgDelegate, MsgSend } from "./msgs";
|
||||
export { Pen, Secp256k1Pen, makeCosmoshubPath } from "./pen";
|
||||
export { decodeBech32Pubkey, encodeBech32Pubkey, encodeSecp256k1Pubkey } from "./pubkey";
|
||||
export { findSequenceForSignedTx } from "./sequence";
|
||||
export { encodeSecp256k1Signature, decodeSignature } from "./signature";
|
||||
export { FeeTable, SigningCallback, SigningCosmosClient } from "./signingcosmosclient";
|
||||
export {
|
||||
isMsgSend,
|
||||
isStdTx,
|
||||
pubkeyType,
|
||||
CosmosSdkTx,
|
||||
PubKey,
|
||||
Msg,
|
||||
MsgSend,
|
||||
StdFee,
|
||||
StdSignature,
|
||||
StdTx,
|
||||
} from "./types";
|
||||
export { isStdTx, pubkeyType, CosmosSdkTx, PubKey, StdFee, StdSignature, StdTx } from "./types";
|
||||
|
42
packages/sdk38/src/msgs.ts
Normal file
42
packages/sdk38/src/msgs.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { Coin } from "./coins";
|
||||
|
||||
export interface Msg {
|
||||
readonly type: string;
|
||||
readonly value: any;
|
||||
}
|
||||
|
||||
/** A Cosmos SDK token transfer message */
|
||||
export interface MsgSend extends Msg {
|
||||
readonly type: "cosmos-sdk/MsgSend";
|
||||
readonly value: {
|
||||
/** Bech32 account address */
|
||||
readonly from_address: string;
|
||||
/** Bech32 account address */
|
||||
readonly to_address: string;
|
||||
readonly amount: ReadonlyArray<Coin>;
|
||||
};
|
||||
}
|
||||
|
||||
export function isMsgSend(msg: Msg): msg is MsgSend {
|
||||
return (msg as MsgSend).type === "cosmos-sdk/MsgSend";
|
||||
}
|
||||
|
||||
/**
|
||||
* A Cosmos SDK MsgDelegate
|
||||
*
|
||||
* @see https://docs.cosmos.network/master/modules/staking/03_messages.html#msgdelegate
|
||||
*/
|
||||
export interface MsgDelegate extends Msg {
|
||||
readonly type: "cosmos-sdk/MsgDelegate";
|
||||
readonly value: {
|
||||
/** Bech32 encoded delegator address */
|
||||
readonly delegator_address: string;
|
||||
/** Bech32 encoded validator address */
|
||||
readonly validator_address: string;
|
||||
readonly amount: Coin;
|
||||
};
|
||||
}
|
||||
|
||||
export function isMsgDelegate(msg: Msg): msg is MsgDelegate {
|
||||
return (msg as MsgDelegate).type === "cosmos-sdk/MsgDelegate";
|
||||
}
|
@ -6,6 +6,7 @@ import { rawSecp256k1PubkeyToAddress } from "./address";
|
||||
import { isPostTxFailure } from "./cosmosclient";
|
||||
import { makeSignBytes } from "./encoding";
|
||||
import { parseLogs } from "./logs";
|
||||
import { Msg, MsgSend } from "./msgs";
|
||||
import { makeCosmoshubPath, Secp256k1Pen } from "./pen";
|
||||
import { encodeBech32Pubkey } from "./pubkey";
|
||||
import { RestClient, TxsResponse } from "./restclient";
|
||||
@ -25,7 +26,7 @@ import {
|
||||
wasmd,
|
||||
wasmdEnabled,
|
||||
} from "./testutils.spec";
|
||||
import { Msg, MsgSend, StdFee, StdSignature, StdTx } from "./types";
|
||||
import { StdFee, StdSignature, StdTx } from "./types";
|
||||
|
||||
const emptyAddress = "cosmos1ltkhnmdcqemmd2tkhnx7qx66tq7e0wykw2j85k";
|
||||
|
||||
|
@ -1,8 +1,9 @@
|
||||
import { Coin, coins } from "./coins";
|
||||
import { Account, CosmosClient, GetNonceResult, PostTxResult } from "./cosmosclient";
|
||||
import { makeSignBytes } from "./encoding";
|
||||
import { MsgSend } from "./msgs";
|
||||
import { BroadcastMode } from "./restclient";
|
||||
import { MsgSend, StdFee, StdSignature } from "./types";
|
||||
import { StdFee, StdSignature, StdTx } from "./types";
|
||||
|
||||
export interface SigningCallback {
|
||||
(signBytes: Uint8Array): Promise<StdSignature>;
|
||||
@ -95,7 +96,7 @@ export class SigningCosmosClient extends CosmosClient {
|
||||
const chainId = await this.getChainId();
|
||||
const signBytes = makeSignBytes([sendMsg], fee, chainId, memo, accountNumber, sequence);
|
||||
const signature = await this.signCallback(signBytes);
|
||||
const signedTx = {
|
||||
const signedTx: StdTx = {
|
||||
msg: [sendMsg],
|
||||
fee: fee,
|
||||
memo: memo,
|
||||
|
@ -1,10 +1,15 @@
|
||||
import { Coin } from "./coins";
|
||||
import { Msg } from "./msgs";
|
||||
|
||||
/** An Amino/Cosmos SDK StdTx */
|
||||
/**
|
||||
* A Cosmos SDK StdTx
|
||||
*
|
||||
* @see https://docs.cosmos.network/master/modules/auth/03_types.html#stdtx
|
||||
*/
|
||||
export interface StdTx {
|
||||
readonly msg: ReadonlyArray<Msg>;
|
||||
readonly msg: readonly Msg[];
|
||||
readonly fee: StdFee;
|
||||
readonly signatures: ReadonlyArray<StdSignature>;
|
||||
readonly signatures: readonly StdSignature[];
|
||||
readonly memo: string | undefined;
|
||||
}
|
||||
|
||||
@ -20,27 +25,6 @@ export interface CosmosSdkTx {
|
||||
readonly value: StdTx;
|
||||
}
|
||||
|
||||
export interface Msg {
|
||||
readonly type: string;
|
||||
readonly value: any;
|
||||
}
|
||||
|
||||
/** A Cosmos SDK token transfer message */
|
||||
export interface MsgSend extends Msg {
|
||||
readonly type: "cosmos-sdk/MsgSend";
|
||||
readonly value: {
|
||||
/** Bech32 account address */
|
||||
readonly from_address: string;
|
||||
/** Bech32 account address */
|
||||
readonly to_address: string;
|
||||
readonly amount: ReadonlyArray<Coin>;
|
||||
};
|
||||
}
|
||||
|
||||
export function isMsgSend(msg: Msg): msg is MsgSend {
|
||||
return (msg as MsgSend).type === "cosmos-sdk/MsgSend";
|
||||
}
|
||||
|
||||
export interface StdFee {
|
||||
readonly amount: ReadonlyArray<Coin>;
|
||||
readonly gas: string;
|
||||
|
3
packages/sdk38/types/encoding.d.ts
vendored
3
packages/sdk38/types/encoding.d.ts
vendored
@ -1,4 +1,5 @@
|
||||
import { Msg, StdFee } from "./types";
|
||||
import { Msg } from "./msgs";
|
||||
import { StdFee } from "./types";
|
||||
export declare function makeSignBytes(
|
||||
msgs: readonly Msg[],
|
||||
fee: StdFee,
|
||||
|
14
packages/sdk38/types/index.d.ts
vendored
14
packages/sdk38/types/index.d.ts
vendored
@ -29,20 +29,10 @@ export {
|
||||
SearchTxsResponse,
|
||||
TxsResponse,
|
||||
} from "./restclient";
|
||||
export { isMsgDelegate, isMsgSend, Msg, MsgDelegate, MsgSend } from "./msgs";
|
||||
export { Pen, Secp256k1Pen, makeCosmoshubPath } from "./pen";
|
||||
export { decodeBech32Pubkey, encodeBech32Pubkey, encodeSecp256k1Pubkey } from "./pubkey";
|
||||
export { findSequenceForSignedTx } from "./sequence";
|
||||
export { encodeSecp256k1Signature, decodeSignature } from "./signature";
|
||||
export { FeeTable, SigningCallback, SigningCosmosClient } from "./signingcosmosclient";
|
||||
export {
|
||||
isMsgSend,
|
||||
isStdTx,
|
||||
pubkeyType,
|
||||
CosmosSdkTx,
|
||||
PubKey,
|
||||
Msg,
|
||||
MsgSend,
|
||||
StdFee,
|
||||
StdSignature,
|
||||
StdTx,
|
||||
} from "./types";
|
||||
export { isStdTx, pubkeyType, CosmosSdkTx, PubKey, StdFee, StdSignature, StdTx } from "./types";
|
||||
|
33
packages/sdk38/types/msgs.d.ts
vendored
Normal file
33
packages/sdk38/types/msgs.d.ts
vendored
Normal file
@ -0,0 +1,33 @@
|
||||
import { Coin } from "./coins";
|
||||
export interface Msg {
|
||||
readonly type: string;
|
||||
readonly value: any;
|
||||
}
|
||||
/** A Cosmos SDK token transfer message */
|
||||
export interface MsgSend extends Msg {
|
||||
readonly type: "cosmos-sdk/MsgSend";
|
||||
readonly value: {
|
||||
/** Bech32 account address */
|
||||
readonly from_address: string;
|
||||
/** Bech32 account address */
|
||||
readonly to_address: string;
|
||||
readonly amount: ReadonlyArray<Coin>;
|
||||
};
|
||||
}
|
||||
export declare function isMsgSend(msg: Msg): msg is MsgSend;
|
||||
/**
|
||||
* A Cosmos SDK MsgDelegate
|
||||
*
|
||||
* @see https://docs.cosmos.network/master/modules/staking/03_messages.html#msgdelegate
|
||||
*/
|
||||
export interface MsgDelegate extends Msg {
|
||||
readonly type: "cosmos-sdk/MsgDelegate";
|
||||
readonly value: {
|
||||
/** Bech32 encoded delegator address */
|
||||
readonly delegator_address: string;
|
||||
/** Bech32 encoded validator address */
|
||||
readonly validator_address: string;
|
||||
readonly amount: Coin;
|
||||
};
|
||||
}
|
||||
export declare function isMsgDelegate(msg: Msg): msg is MsgDelegate;
|
27
packages/sdk38/types/types.d.ts
vendored
27
packages/sdk38/types/types.d.ts
vendored
@ -1,9 +1,14 @@
|
||||
import { Coin } from "./coins";
|
||||
/** An Amino/Cosmos SDK StdTx */
|
||||
import { Msg } from "./msgs";
|
||||
/**
|
||||
* A Cosmos SDK StdTx
|
||||
*
|
||||
* @see https://docs.cosmos.network/master/modules/auth/03_types.html#stdtx
|
||||
*/
|
||||
export interface StdTx {
|
||||
readonly msg: ReadonlyArray<Msg>;
|
||||
readonly msg: readonly Msg[];
|
||||
readonly fee: StdFee;
|
||||
readonly signatures: ReadonlyArray<StdSignature>;
|
||||
readonly signatures: readonly StdSignature[];
|
||||
readonly memo: string | undefined;
|
||||
}
|
||||
export declare function isStdTx(txValue: unknown): txValue is StdTx;
|
||||
@ -11,22 +16,6 @@ export interface CosmosSdkTx {
|
||||
readonly type: string;
|
||||
readonly value: StdTx;
|
||||
}
|
||||
export interface Msg {
|
||||
readonly type: string;
|
||||
readonly value: any;
|
||||
}
|
||||
/** A Cosmos SDK token transfer message */
|
||||
export interface MsgSend extends Msg {
|
||||
readonly type: "cosmos-sdk/MsgSend";
|
||||
readonly value: {
|
||||
/** Bech32 account address */
|
||||
readonly from_address: string;
|
||||
/** Bech32 account address */
|
||||
readonly to_address: string;
|
||||
readonly amount: ReadonlyArray<Coin>;
|
||||
};
|
||||
}
|
||||
export declare function isMsgSend(msg: Msg): msg is MsgSend;
|
||||
export interface StdFee {
|
||||
readonly amount: ReadonlyArray<Coin>;
|
||||
readonly gas: string;
|
||||
|
@ -17,7 +17,7 @@ const codeMeta = {
|
||||
};
|
||||
|
||||
// To get the proper validator address, run the demo chain (./scripts/wasmd/start.sh), then run:
|
||||
// curl http://localhost:1317/staking/validators | jq .result[0].operator_address
|
||||
// curl http://localhost:1317/staking/validators | jq '.result[0].operator_address'
|
||||
const bounty = {
|
||||
label: "Bounty",
|
||||
initMsg: {
|
||||
|
Loading…
x
Reference in New Issue
Block a user