@cosmjs/launchpad
Advanced tools
Comparing version 0.24.0-alpha.13 to 0.24.0-alpha.14
@@ -15,3 +15,7 @@ "use strict"; | ||
const sortedKeys = Object.keys(obj).sort(); | ||
const result = sortedKeys.reduce((accumulator, key) => (Object.assign(Object.assign({}, accumulator), { [key]: sortedObject(obj[key]) })), {}); | ||
const result = {}; | ||
// NOTE: Use forEach instead of reduce for performance with large objects eg Wasm code | ||
sortedKeys.forEach((key) => { | ||
result[key] = sortedObject(obj[key]); | ||
}); | ||
return result; | ||
@@ -32,3 +36,3 @@ } | ||
msgs: msgs, | ||
memo: memo, | ||
memo: memo || "", | ||
}; | ||
@@ -35,0 +39,0 @@ } |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SigningCosmosClient = void 0; | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
const fast_deep_equal_1 = __importDefault(require("fast-deep-equal")); | ||
const cosmosclient_1 = require("./cosmosclient"); | ||
@@ -54,2 +59,10 @@ const encoding_1 = require("./encoding"); | ||
async signAndBroadcast(msgs, fee, memo = "") { | ||
const signedTx = await this.sign(msgs, fee, memo); | ||
return this.broadcastTx(signedTx); | ||
} | ||
/** | ||
* Gets account number and sequence from the API, creates a sign doc, | ||
* creates a single signature and assembles the signed transaction. | ||
*/ | ||
async sign(msgs, fee, memo = "") { | ||
const { accountNumber, sequence } = await this.getSequence(); | ||
@@ -59,7 +72,21 @@ const chainId = await this.getChainId(); | ||
const { signed, signature } = await this.signer.signAmino(this.senderAddress, signDoc); | ||
const signedTx = tx_1.makeStdTx(signed, signature); | ||
return this.broadcastTx(signedTx); | ||
return tx_1.makeStdTx(signed, signature); | ||
} | ||
/** | ||
* Gets account number and sequence from the API, creates a sign doc, | ||
* creates a single signature and appends it to the existing signatures. | ||
*/ | ||
async appendSignature(signedTx) { | ||
const { msg: msgs, fee, memo } = signedTx; | ||
const { accountNumber, sequence } = await this.getSequence(); | ||
const chainId = await this.getChainId(); | ||
const signDoc = encoding_1.makeSignDoc(msgs, fee, chainId, memo, accountNumber, sequence); | ||
const { signed, signature: additionalSignature } = await this.signer.signAmino(this.senderAddress, signDoc); | ||
if (!fast_deep_equal_1.default(signDoc, signed)) { | ||
throw new Error("The signed document differs from the one of the original transaction. This is not allowed since the resulting transaction will be invalid."); | ||
} | ||
return tx_1.makeStdTx(signed, [...signedTx.signatures, additionalSignature]); | ||
} | ||
} | ||
exports.SigningCosmosClient = SigningCosmosClient; | ||
//# sourceMappingURL=signingcosmosclient.js.map |
{ | ||
"name": "@cosmjs/launchpad", | ||
"version": "0.24.0-alpha.13", | ||
"version": "0.24.0-alpha.14", | ||
"description": "A client library for the Cosmos SDK 0.37 (cosmoshub-3), 0.38 and 0.39 (Launchpad)", | ||
@@ -46,7 +46,8 @@ "contributors": [ | ||
"dependencies": { | ||
"@cosmjs/crypto": "^0.24.0-alpha.13", | ||
"@cosmjs/encoding": "^0.24.0-alpha.13", | ||
"@cosmjs/math": "^0.24.0-alpha.13", | ||
"@cosmjs/utils": "^0.24.0-alpha.13", | ||
"axios": "^0.19.0" | ||
"@cosmjs/crypto": "^0.24.0-alpha.14", | ||
"@cosmjs/encoding": "^0.24.0-alpha.14", | ||
"@cosmjs/math": "^0.24.0-alpha.14", | ||
"@cosmjs/utils": "^0.24.0-alpha.14", | ||
"axios": "^0.21.1", | ||
"fast-deep-equal": "^3.1.3" | ||
}, | ||
@@ -56,3 +57,3 @@ "devDependencies": { | ||
}, | ||
"gitHead": "a7674d01688462a7a58ff2c5b066348600854f11" | ||
"gitHead": "55d66be270471b04e1e8587d06ab7da2ac30bbc0" | ||
} |
219
README.md
@@ -10,2 +10,221 @@ # @cosmjs/launchpad | ||
## Basic usage | ||
The basic usage of the package `@cosmjs/launchpad` contains the following: | ||
1. [Create a wallet](#create-a-wallet) | ||
2. [Sign and broadcast transactions](#sign-and-broadcast-transactions) | ||
### Create a wallet | ||
For the sake of simplicity we use an in-memory wallet. This is not the safest | ||
way to store production keys. The following demo code is intended for developers | ||
using testnet credentials only. Integrating it into end user facing products | ||
requires serious security review. | ||
If you do not yet have a mnemonic, generate a new wallet with a random mnemonic: | ||
```ts | ||
import { Secp256k1HdWallet } from "@cosmjs/launchpad"; | ||
// … | ||
const wallet = await Secp256k1HdWallet.generate(); | ||
console.log("Mnemonic:", wallet.mnemonic); | ||
const [{ address }] = await wallet.getAccounts(); | ||
console.log("Address:", address); | ||
``` | ||
Or import an existing one: | ||
```ts | ||
import { Secp256k1HdWallet } from "@cosmjs/launchpad"; | ||
// … | ||
const wallet = await Secp256k1HdWallet.fromMnemonic( | ||
// your mnemonic here 👇 | ||
"enlist hip relief stomach skate base shallow young switch frequent cry park", | ||
); | ||
const [{ address }] = await wallet.getAccounts(); | ||
console.log("Address:", address); | ||
``` | ||
### Sign and broadcast transactions | ||
A wallet holds private keys and can use them for signing transactions. To do so | ||
we stick the wallet into a client: | ||
```ts | ||
import { | ||
Secp256k1HdWallet, | ||
SigningCosmosClient, | ||
coins, | ||
} from "@cosmjs/launchpad"; | ||
// … | ||
const wallet = await Secp256k1HdWallet.generate(); | ||
const [{ address }] = await wallet.getAccounts(); | ||
console.log("Address:", address); | ||
// Ensure the address has some tokens to spend | ||
const lcdApi = "https://…"; | ||
const client = new SigningCosmosClient(lcdApi, address, wallet); | ||
// check our balance | ||
const account = await client.getAccount(); | ||
console.log("Account:", account); | ||
// Send tokens | ||
const recipient = "cosmos1b2340gb2…"; | ||
await client.sendTokens(recipient, coins(123, "uatom")); | ||
``` | ||
or use custom message types: | ||
```ts | ||
import { | ||
Secp256k1HdWallet, | ||
SigningCosmosClient, | ||
coins, | ||
coin, | ||
MsgDelegate, | ||
} from "@cosmjs/launchpad"; | ||
// … | ||
const wallet = await Secp256k1HdWallet.generate(); | ||
const [{ address }] = await wallet.getAccounts(); | ||
console.log("Address:", address); | ||
// Ensure the address has some tokens to spend | ||
const lcdApi = "https://…"; | ||
const client = new SigningCosmosClient(lcdApi, address, wallet); | ||
// … | ||
const msg: MsgDelegate = { | ||
type: "cosmos-sdk/MsgDelegate", | ||
value: { | ||
delegator_address: address, | ||
validator_address: "cosmosvaloper1yfkkk04ve8a0sugj4fe6q6zxuvmvza8r3arurr", | ||
amount: coin(300000, "ustake"), | ||
}, | ||
}; | ||
const fee = { | ||
amount: coins(2000, "ucosm"), | ||
gas: "180000", // 180k | ||
}; | ||
await client.signAndBroadcast([msg], fee); | ||
``` | ||
## Advanced usage | ||
Here you will learn a few things that are slightly more advanced: | ||
1. [Sign only](#sign-only) | ||
2. [Aggregate signatures](#aggregate-signatures) from multiple signers | ||
### Sign only | ||
You can sign a transaction without broadcasting it: | ||
```ts | ||
import { | ||
Secp256k1HdWallet, | ||
SigningCosmosClient, | ||
coins, | ||
coin, | ||
MsgDelegate, | ||
} from "@cosmjs/launchpad"; | ||
// … | ||
const wallet = await Secp256k1HdWallet.generate(); | ||
const [{ address }] = await wallet.getAccounts(); | ||
console.log("Address:", address); | ||
const lcdApi = "https://…"; // Signing is offline, but from this endpoint we get the account number and sequence | ||
const client = new SigningCosmosClient(lcdApi, address, wallet); | ||
// … | ||
const msg: MsgDelegate = { | ||
type: "cosmos-sdk/MsgDelegate", | ||
value: { | ||
delegator_address: address, | ||
validator_address: "cosmosvaloper1yfkkk04ve8a0sugj4fe6q6zxuvmvza8r3arurr", | ||
amount: coin(300000, "ustake"), | ||
}, | ||
}; | ||
const fee = { | ||
amount: coins(2000, "ucosm"), | ||
gas: "180000", // 180k | ||
}; | ||
let signed = await client.sign([msg], fee); | ||
console.log("Signed transaction:", signed); | ||
// We can broadcast it manually later on | ||
const result = await client.broadcastTx(signed); | ||
console.log("Broadcasting result:", result); | ||
``` | ||
### Aggregate signatures | ||
In this example we use `wallet0`/`client0` and `wallet1`/`client1` which can | ||
live on separate systems: | ||
```ts | ||
import { | ||
Secp256k1HdWallet, | ||
SigningCosmosClient, | ||
coins, | ||
coin, | ||
MsgDelegate, | ||
} from "@cosmjs/launchpad"; | ||
const wallet0 = await Secp256k1HdWallet.fromMnemonic(mnemonic0); | ||
const [{ address: address0 }] = await wallet.getAccounts(); | ||
const client0 = new SigningCosmosClient("https://…", address0, wallet0); | ||
const wallet1 = await Secp256k1HdWallet.fromMnemonic(mnemonic1); | ||
const [{ address: address1 }] = await wallet.getAccounts(); | ||
const client1 = new SigningCosmosClient("https://…", address1, wallet1); | ||
const msg1: MsgSend = { | ||
type: "cosmos-sdk/MsgSend", | ||
value: { | ||
from_address: address0, | ||
to_address: "cosmos1b2340gb2…", | ||
amount: coins(1234567, "ucosm"), | ||
}, | ||
}; | ||
const msg2: MsgSend = { | ||
type: "cosmos-sdk/MsgSend", | ||
value: { | ||
from_address: address1, | ||
to_address: "cosmos1b2340gb2…", | ||
amount: coins(1234567, "ucosm"), | ||
}, | ||
}; | ||
const fee = { | ||
amount: coins(2000, "ucosm"), | ||
gas: "160000", // 2*80k | ||
}; | ||
const memo = "This must be authorized by the two of us"; | ||
const signed = await client0.sign([msg1, msg2], fee, memo); | ||
const cosigned = await client1.appendSignature(signed); | ||
const result = await client1.broadcastTx(cosigned); | ||
console.log("Broadcasting result:", result); | ||
``` | ||
## Cosmos SDK module support | ||
@@ -12,0 +231,0 @@ |
@@ -22,3 +22,3 @@ import { Msg } from "./msgs"; | ||
chainId: string, | ||
memo: string, | ||
memo: string | undefined, | ||
accountNumber: number | string, | ||
@@ -25,0 +25,0 @@ sequence: number | string, |
@@ -7,2 +7,3 @@ import { Coin } from "./coins"; | ||
import { OfflineSigner } from "./signer"; | ||
import { StdTx } from "./tx"; | ||
import { StdFee } from "./types"; | ||
@@ -56,2 +57,12 @@ /** | ||
signAndBroadcast(msgs: readonly Msg[], fee: StdFee, memo?: string): Promise<BroadcastTxResult>; | ||
/** | ||
* Gets account number and sequence from the API, creates a sign doc, | ||
* creates a single signature and assembles the signed transaction. | ||
*/ | ||
sign(msgs: readonly Msg[], fee: StdFee, memo?: string): Promise<StdTx>; | ||
/** | ||
* Gets account number and sequence from the API, creates a sign doc, | ||
* creates a single signature and appends it to the existing signatures. | ||
*/ | ||
appendSignature(signedTx: StdTx): Promise<StdTx>; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
218330
3539
524
6
+ Addedfast-deep-equal@^3.1.3
+ Addedaxios@0.21.4(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfollow-redirects@1.15.9(transitive)
- Removedaxios@0.19.2(transitive)
- Removedfollow-redirects@1.5.10(transitive)
Updatedaxios@^0.21.1