0.27.1 • Published 2 years ago

@cosmjs/launchpad v0.27.1

Weekly downloads
1,639
License
Apache-2.0
Repository
github
Last release
2 years ago

@cosmjs/launchpad

npm version

A client library for the Cosmos SDK 0.37 (cosmoshub-3), 0.38 and 0.39 (Launchpad). See the article Launchpad — A pre-stargate stable version of the Cosmos SDK to learn more about launchpad.

Cosmos SDK module support

This client library supports connecting to standard Cosmos SDK modules as well as custom modules. The modularity has two sides that are handled separately:

  • Queries are for reading data from the chain;
  • Messages are for writing data to chain as part of the transaction signing.

Query support

@cosmjs/launchpad now has a flexible LcdClient, which can be extended with all the standard Cosmos SDK modules in a type-safe way. With

import { LcdClient } from "@cosmjs/launchpad";

const client = new LcdClient(apiUrl);
const response = await client.nodeInfo();

you only get access to blocks, transaction lists and node info. In order to sign transactions, you need to setup the auth extension with:

import { LcdClient, setupAuthExtension } from "@cosmjs/launchpad";

const client = LcdClient.withExtensions({ apiUrl }, setupAuthExtension);
const { account_number, sequence } = (
  await client.auth.account(myAddress)
).result.value;

A full client can use all of the following extensions:

import {
  LcdClient,
  setupAuthExtension,
  setupBankExtension,
  setupDistributionExtension,
  setupGovExtension,
  setupMintExtension,
  setupSlashingExtension,
  setupStakingExtension,
  setupSupplyExtension,
} from "@cosmjs/launchpad";

const client = LcdClient.withExtensions(
  { apiUrl },
  setupAuthExtension,
  setupBankExtension,
  setupDistributionExtension,
  setupGovExtension,
  setupMintExtension,
  setupSlashingExtension,
  setupStakingExtension,
  setupSupplyExtension,
);

// Example queries
const balances = await client.bank.balances(myAddress);
const distParameters = await client.distribution.parameters();
const proposals = await client.gov.proposals();
const inflation = await client.mint.inflation();
const signingInfos = await client.slashing.signingInfos();
const redelegations = await client.staking.redelegations();
const supply = await client.supply.totalAll();

Messages suppport

Every Amino-JSON compatible message can be used for the Msg interface like this:

const voteMsg: Msg = {
  type: "cosmos-sdk/MsgVote",
  value: {
    proposal_id: proposalId,
    voter: faucet.address,
    option: "Yes",
  },
};

This is most flexible since you are not restricted to known messages, but gives you very little type safety. For improved type safety, we added TypeScript definitions for all common Cosmos SDK messages:

  • MsgSend
  • MsgMultiSend
  • MsgVerifyInvariant
  • MsgSetWithdrawAddress
  • MsgWithdrawDelegatorReward
  • MsgWithdrawValidatorCommission
  • MsgFundCommunityPool
  • MsgSubmitEvidence
  • MsgSubmitProposal
  • MsgVote
  • MsgDeposit
  • MsgUnjail
  • MsgCreateValidator
  • MsgEditValidator
  • MsgDelegate
  • MsgBeginRedelegate
  • MsgUndelegate

Those can be signed and broadcast the manual way or by using a higher level SigningCosmosClient:

import {
  coins,
  MsgSubmitProposal,
  OfflineSigner,
  PostTxResult,
  SigningCosmosClient,
  StdFee,
} from "@cosmjs/launchpad";

async function publishProposal(
  apiUrl: string,
  signer: OfflineSigner,
): Promise<PostTxResult> {
  const [{ address: authorAddress }] = await signer.getAccounts();

  const memo = "My first proposal on chain";
  const msg: MsgSubmitProposal = {
    type: "cosmos-sdk/MsgSubmitProposal",
    value: {
      content: {
        type: "cosmos-sdk/TextProposal",
        value: {
          description:
            "This proposal proposes to test whether this proposal passes",
          title: "Test Proposal",
        },
      },
      proposer: authorAddress,
      initial_deposit: coins(25000000, "ustake"),
    },
  };
  const fee: StdFee = {
    amount: coins(5000000, "ucosm"),
    gas: "89000000",
  };

  const client = new SigningCosmosClient(apiUrl, authorAddress, signer);
  return client.signAndPost([msg], fee, memo);
}

Custom modules

Both query and message support is implemented in a decentralized fashion, which allows applications to add their extensions without changing CosmJS. As an example we show how to build a client for a blockchain with a wasm module:

import { MsgExecuteContract, setupWasmExtension } from "@cosmjs/cosmwasm";
import {
  assertIsPostTxSuccess,
  LcdClient,
  makeSignDoc,
  setupAuthExtension,
  StdFee,
  StdTx,
} from "@cosmjs/launchpad";

const client = LcdClient.withExtensions(
  { apiUrl },
  setupAuthExtension,
  // 👇 this extension can come from a chain-specific package or the application itself
  setupWasmExtension,
);

// 👇 this message type can come from a chain-specific package or the application itself
const msg: MsgExecuteContract = {
  type: "wasm/MsgExecuteContract",
  value: {
    sender: myAddress,
    contract: contractAddress,
    msg: wasmMsg,
    sent_funds: [],
  },
};

const fee: StdFee = {
  amount: coins(5000000, "ucosm"),
  gas: "89000000",
};
const memo = "Time for action";
const { account_number, sequence } = (
  await client.auth.account(myAddress)
).result.value;
const signDoc = makeSignDoc([msg], fee, apiUrl, memo, account_number, sequence);
const { signed, signature } = await signer.sign(myAddress, signDoc);
const signedTx = makeStdTx(signed, signature);
const result = await client.postTx(signedTx);
assertIsPostTxSuccess(result);

Secure key storage

Secp256k1Wallet supports securely encrypted serialization/deserialization using Argon2 for key derivation and XChaCha20Poly1305 for authenticated encryption. It can be used as easily as:

// generate an 18 word mnemonic
const wallet = await Secp256k1Wallet.generate(18);
const serialized = await original.serialize("my password");

// serialized is encrypted and can now be stored in an application-specific way

const restored = await Secp256k1Wallet.deserialize(serialized, "my password");

If you want to use really strong KDF parameters in a user interface, you should offload the KDF execution to a separate thread in order to avoid freezing the UI. This can be done in the advanced mode:

Session 1 (main thread)

const wallet = await Secp256k1Wallet.generate(18);

Session 1 (WebWorker)

This operation can now run a couple of seconds without freezing the UI.

import { executeKdf } from "@cosmjs/launchpad";

// pass password to the worker

const strongKdfParams: KdfConfiguration = {
  algorithm: "argon2id",
  params: {
    outputLength: 32,
    opsLimit: 5000,
    memLimitKib: 15 * 1024,
  },
};
const encryptionKey = await executeKdf(password, strongKdfParams);

// pass encryptionKey to the main thread

Session 1 (main thread)

const serialized = await wallet.serializeWithEncryptionKey(
  encryptionKey,
  anyKdfParams,
);

Session 2 (WebWorker)

import { executeKdf, extractKdfConfiguration } from "@cosmjs/launchpad";

// pass serialized and password to the worker

const kdfConfiguration = extractKdfConfiguration(serialized);
const encryptionKey = await executeKdf(password, kdfConfiguration);

// pass encryptionKey to the main thread

Session 2 (main thead)

const restored = await Secp256k1Wallet.deserializeWithEncryptionKey(
  serialized,
  encryptionKey,
);

// use restored for signing

License

This package is part of the cosmjs repository, licensed under the Apache License 2.0 (see NOTICE and LICENSE).

@strangelovelabs/polaris-ignite@bitsongjs/btsgconfio-testsix-data-chain-sdk@infinitebrahmanuniverse/nolb-_cospolaris-ignite@everything-registry/sub-chunk-200@injectivelabs/wallet-ts@injectivelabs/cosmos-ts@jackallabs/banshee@jackallabs/jackal.js@jackallabs/jackal.js-protos@jackallabs/jackal.nodejs@jackallabs/jackal.nodejs-protos@jeffthenaef/ts-client@leonmw/akashjs@lavanet/lava-sdk@miker808/mantajs@gen-io/wallet@glitterprotocol/glitter-wallet@forrestg/web3-wallets@hanchon/evmos-keplr-message-verifier@grateful-hug/react@mantrachain/sdk@mamoru-ai/validation-chain-ts-client@neutron-org/client-ts@neutron-org/contracts2ts@nikaru-dev/provider-walletconnect-2@nikaru-dev/signer-cosmos@nephos/crescent-v4-client-ts@nephos/gaia-v8-client-ts@nephos/osmosis-v13-client-ts@khoren.markosyan/vue@johnrjj/fairysdk-test@josefleventon/graz-interchain@fetchai/wallet-types@furyahub-app/apps@furyahub-app/apps-wallet@furyahub-app/furyahub@furyahub/apps@furyahub/apps-wallet@furyahubapp/apps@furyahubapp/apps-wallet@ignt/spn-client-ts@ignt/tendermint-spn-client-ts@ignt/client@osmosis-labs/storesanconjsanconjs-datacontractalice-chain-client-tsalice-checkers-client-ts@0xtfm/widget-ibcautosmosis@akashnetwork/akashjsairhub-client-tsairblogs-client-ts@vagachain/sdk@viaprotocol/web3-walletsarchive-client-ts@wormhole-foundation/wormchain-sdk@thomasralee/wallet-ts@cosmicdapp/logic@cheqd/wallet@mrtruongleo/dym-ts-sdk@mrtruongleo/inj-ts-sdk@rarimo/client@saonetwork/saochain-ts-client@psiko-money/psiko.js@pioneer-platform/osmosis-tx-builder@pioneer-platform/cosmos-tx-builder@playwo/akashjs@stargazefi/proto-signing@sputniknetwork/starname-cosmjs@starport/vuex@stafihub/apps-wallet@soarchain/cosmjs-client@stream-wallet/wallet-types@sudophunk/roto-marketplace@rango-dev/provider-walletconnect-2@rango-dev/signer-cosmos@rangodev/wallets-shared@rango-test/wallets-shared@routerprotocol/router-chain-sdk-ts@sifchain/sdk@soupy-finance/noodle-ts-client@soja-sports/furyahubapp@spidex/client@openocean.finance/wallet@nuahorg/aga@nuahorg/client-ts@owallet/types@firmachain/firma-js@scriptione/cosmos@sixnetwork/six-data-chain-sdk@sixnetwork/walletconnector@bluzelle/sdkclanjsclaptrap-ui@chainapsis/cosmodal@zalastax/nolb-_cos
0.27.1

2 years ago

0.27.0

2 years ago

0.26.6

2 years ago

0.27.0-rc2

2 years ago

0.27.0-rc1

2 years ago

0.26.5

2 years ago

0.26.4

3 years ago

0.26.3

3 years ago

0.26.2

3 years ago

0.26.1

3 years ago

0.26.0

3 years ago

0.26.0-rc1

3 years ago

0.26.0-alpha2

3 years ago

0.26.0-alpha1

3 years ago

0.25.6

3 years ago

0.25.5

3 years ago

0.25.4

3 years ago

0.25.3

3 years ago

0.25.2

3 years ago

0.25.1

3 years ago

0.25.0

3 years ago

0.25.0-alpha.3

3 years ago

0.25.0-alpha.2

3 years ago

0.25.0-alpha.1

3 years ago

0.24.1

3 years ago

0.25.0-alpha.0

3 years ago

0.24.0

3 years ago

0.24.0-alpha.26

3 years ago

0.24.0-alpha.25

3 years ago

0.24.0-alpha.24

3 years ago

0.24.0-alpha.23

3 years ago

0.24.0-alpha.22

3 years ago

0.24.0-alpha.20

3 years ago

0.24.0-alpha.21

3 years ago

0.24.0-alpha.19

3 years ago

0.24.0-alpha.18

3 years ago

0.24.0-alpha.17

3 years ago

0.24.0-alpha.16

3 years ago

0.24.0-alpha.15

3 years ago

0.24.0-alpha.14

3 years ago

0.23.2

3 years ago

0.24.0-alpha.12

3 years ago

0.24.0-alpha.13

3 years ago

0.24.0-alpha.11

3 years ago

0.24.0-alpha.10

3 years ago

0.24.0-alpha.9

3 years ago

0.24.0-alpha.8

3 years ago

0.24.0-alpha.7

3 years ago

0.24.0-alpha.6

3 years ago

0.24.0-alpha.5

3 years ago

0.24.0-alpha.4

4 years ago

0.24.0-alpha.3

4 years ago

0.24.0-alpha.2

4 years ago

0.24.0-alpha.0

4 years ago

0.23.1

4 years ago

0.23.0

4 years ago

0.23.0-alpha.1

4 years ago

0.23.0-alpha.0

4 years ago

0.22.3

4 years ago

0.22.2

4 years ago

0.22.1

4 years ago

0.22.0

4 years ago

0.22.0-alpha.1

4 years ago

0.22.0-alpha.0

4 years ago