@cosmjs/stargate
Advanced tools
Comparing version 0.24.0-alpha.14 to 0.24.0-alpha.15
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AminoTypes = void 0; | ||
const defaultTypes = { | ||
"/cosmos.bank.v1beta1.MsgSend": "cosmos-sdk/MsgSend", | ||
"/cosmos.bank.v1beta1.MsgMultiSend": "cosmos-sdk/MsgMultiSend", | ||
"/cosmos.crisis.v1beta1.MsgVerifyInvariant": "cosmos-sdk/MsgVerifyInvariant", | ||
"/cosmos.distribution.v1beta1.MsgSetWithdrawAddress": "cosmos-sdk/MsgSetWithdrawAddress", | ||
"/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward": "cosmos-sdk/MsgWithdrawDelegatorReward", | ||
"/cosmos.distribution.v1beta1.MsgWithdrawValidatorComission": "cosmos-sdk/MsgWithdrawValidatorComission", | ||
"/cosmos.distribution.v1beta1.MsgFundCommunityPool": "cosmos-sdk/MsgFundCommunityPool", | ||
"/cosmos.evidence.v1beta1.MsgSubmitEvidence": "cosmos-sdk/MsgSubmitEvidence", | ||
"/cosmos.gov.v1beta1.MsgSubmitProposal": "cosmos-sdk/MsgSubmitProposal", | ||
"/cosmos.gov.v1beta1.MsgVote": "cosmos-sdk/MsgVote", | ||
"/cosmos.gov.v1beta1.MsgDeposit": "cosmos-sdk/MsgDeposit", | ||
"/cosmos.slashing.v1beta1.MsgUnjail": "cosmos-sdk/MsgUnjail", | ||
"/cosmos.staking.v1beta1.MsgCreateValidator": "cosmos-sdk/MsgCreateValidator", | ||
"/cosmos.staking.v1beta1.MsgEditValidator": "cosmos-sdk/MsgEditValidator", | ||
"/cosmos.staking.v1beta1.MsgDelegate": "cosmos-sdk/MsgDelegate", | ||
"/cosmos.staking.v1beta1.MsgBeginRedelegate": "cosmos-sdk/MsgBeginRedelegate", | ||
"/cosmos.staking.v1beta1.MsgUndelegate": "cosmos-sdk/MsgUndelegate", | ||
"/cosmos.vesting.v1beta1.MsgCreateVestingAccount": "cosmos-sdk/MsgCreateVestingAccount", | ||
}; | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
const encoding_1 = require("@cosmjs/encoding"); | ||
const launchpad_1 = require("@cosmjs/launchpad"); | ||
const utils_1 = require("@cosmjs/utils"); | ||
function checkAmount(amount) { | ||
utils_1.assert(amount, "missing amount"); | ||
return amount.map((a) => { | ||
utils_1.assert(a.amount, "missing amount"); | ||
utils_1.assert(a.denom, "missing denom"); | ||
return { | ||
amount: a.amount, | ||
denom: a.denom, | ||
}; | ||
}); | ||
} | ||
function createDefaultTypes(prefix) { | ||
return { | ||
"/cosmos.bank.v1beta1.MsgSend": { | ||
aminoType: "cosmos-sdk/MsgSend", | ||
toAmino: ({ fromAddress, toAddress, amount }) => { | ||
utils_1.assert(fromAddress, "missing fromAddress"); | ||
utils_1.assert(toAddress, "missing toAddress"); | ||
return { | ||
from_address: fromAddress, | ||
to_address: toAddress, | ||
amount: checkAmount(amount), | ||
}; | ||
}, | ||
fromAmino: ({ from_address, to_address, amount }) => ({ | ||
fromAddress: from_address, | ||
toAddress: to_address, | ||
amount: [...amount], | ||
}), | ||
}, | ||
"/cosmos.bank.v1beta1.MsgMultiSend": { | ||
aminoType: "cosmos-sdk/MsgMultiSend", | ||
toAmino: ({ inputs, outputs }) => { | ||
utils_1.assert(inputs, "missing inputs"); | ||
utils_1.assert(outputs, "missing outputs"); | ||
return { | ||
inputs: inputs.map((input) => { | ||
utils_1.assert(input.address, "missing input.address"); | ||
return { | ||
address: input.address, | ||
coins: checkAmount(input.coins), | ||
}; | ||
}), | ||
outputs: outputs.map((output) => { | ||
utils_1.assert(output.address, "missing output.address"); | ||
return { | ||
address: output.address, | ||
coins: checkAmount(output.coins), | ||
}; | ||
}), | ||
}; | ||
}, | ||
fromAmino: ({ inputs, outputs }) => ({ | ||
inputs: inputs.map((input) => ({ | ||
address: input.address, | ||
coins: [...input.coins], | ||
})), | ||
outputs: outputs.map((output) => ({ | ||
address: output.address, | ||
coins: [...output.coins], | ||
})), | ||
}), | ||
}, | ||
"/cosmos.staking.v1beta1.MsgBeginRedelegate": { | ||
aminoType: "cosmos-sdk/MsgBeginRedelegate", | ||
toAmino: ({ delegatorAddress, validatorSrcAddress, validatorDstAddress, amount, }) => { | ||
utils_1.assert(delegatorAddress, "missing delegatorAddress"); | ||
utils_1.assert(validatorSrcAddress, "missing validatorSrcAddress"); | ||
utils_1.assert(validatorDstAddress, "missing validatorDstAddress"); | ||
utils_1.assert(amount, "missing amount"); | ||
utils_1.assert(amount.amount, "missing amount.amount"); | ||
utils_1.assert(amount.denom, "missing amount.denom"); | ||
return { | ||
delegator_address: delegatorAddress, | ||
validator_src_address: validatorSrcAddress, | ||
validator_dst_address: validatorDstAddress, | ||
amount: { | ||
amount: amount.amount, | ||
denom: amount.denom, | ||
}, | ||
}; | ||
}, | ||
fromAmino: ({ delegator_address, validator_src_address, validator_dst_address, amount, }) => ({ | ||
delegatorAddress: delegator_address, | ||
validatorSrcAddress: validator_src_address, | ||
validatorDstAddress: validator_dst_address, | ||
amount: amount, | ||
}), | ||
}, | ||
"/cosmos.staking.v1beta1.MsgCreateValidator": { | ||
aminoType: "cosmos-sdk/MsgCreateValidator", | ||
toAmino: ({ description, commission, minSelfDelegation, delegatorAddress, validatorAddress, pubkey, value, }) => { | ||
utils_1.assert(description, "missing description"); | ||
utils_1.assert(description.moniker, "missing description.moniker"); | ||
utils_1.assert(description.identity, "missing description.identity"); | ||
utils_1.assert(description.website, "missing description.website"); | ||
utils_1.assert(description.securityContact, "missing description.securityContact"); | ||
utils_1.assert(description.details, "missing description.details"); | ||
utils_1.assert(commission, "missing commission"); | ||
utils_1.assert(commission.rate, "missing commission.rate"); | ||
utils_1.assert(commission.maxRate, "missing commission.maxRate"); | ||
utils_1.assert(commission.maxChangeRate, "missing commission.maxChangeRate"); | ||
utils_1.assert(minSelfDelegation, "missing minSelfDelegation"); | ||
utils_1.assert(delegatorAddress, "missing delegatorAddress"); | ||
utils_1.assert(validatorAddress, "missing validatorAddress"); | ||
utils_1.assert(pubkey, "missing pubkey"); | ||
utils_1.assert(pubkey.value, "missing pubkey.value"); | ||
utils_1.assert(value, "missing value"); | ||
utils_1.assert(value.amount, "missing value.amount"); | ||
utils_1.assert(value.denom, "missing value.denom"); | ||
return { | ||
description: { | ||
moniker: description.moniker, | ||
identity: description.identity, | ||
website: description.website, | ||
security_contact: description.securityContact, | ||
details: description.details, | ||
}, | ||
commission: { | ||
rate: commission.rate, | ||
max_rate: commission.maxRate, | ||
max_change_rate: commission.maxChangeRate, | ||
}, | ||
min_self_delegation: minSelfDelegation, | ||
delegator_address: delegatorAddress, | ||
validator_address: validatorAddress, | ||
pubkey: launchpad_1.encodeBech32Pubkey({ | ||
type: "tendermint/PubKeySecp256k1", | ||
value: encoding_1.toBase64(pubkey.value), | ||
}, prefix), | ||
value: { | ||
amount: value.amount, | ||
denom: value.denom, | ||
}, | ||
}; | ||
}, | ||
fromAmino: ({ description, commission, min_self_delegation, delegator_address, validator_address, pubkey, value, }) => { | ||
const decodedPubkey = launchpad_1.decodeBech32Pubkey(pubkey); | ||
if (decodedPubkey.type !== "tendermint/PubKeySecp256k1") { | ||
throw new Error("Only Secp256k1 public keys are supported"); | ||
} | ||
return { | ||
description: { | ||
moniker: description.moniker, | ||
identity: description.identity, | ||
website: description.website, | ||
securityContact: description.security_contact, | ||
details: description.details, | ||
}, | ||
commission: { | ||
rate: commission.rate, | ||
maxRate: commission.max_rate, | ||
maxChangeRate: commission.max_change_rate, | ||
}, | ||
minSelfDelegation: min_self_delegation, | ||
delegatorAddress: delegator_address, | ||
validatorAddress: validator_address, | ||
pubkey: { | ||
type_url: "/cosmos.crypto.secp256k1.PubKey", | ||
value: encoding_1.fromBase64(decodedPubkey.value), | ||
}, | ||
value: value, | ||
}; | ||
}, | ||
}, | ||
"/cosmos.staking.v1beta1.MsgDelegate": { | ||
aminoType: "cosmos-sdk/MsgDelegate", | ||
toAmino: ({ delegatorAddress, validatorAddress, amount }) => { | ||
utils_1.assert(delegatorAddress, "missing delegatorAddress"); | ||
utils_1.assert(validatorAddress, "missing validatorAddress"); | ||
utils_1.assert(amount, "missing amount"); | ||
utils_1.assert(amount.amount, "missing amount.amount"); | ||
utils_1.assert(amount.denom, "missing amount.denom"); | ||
return { | ||
delegator_address: delegatorAddress, | ||
validator_address: validatorAddress, | ||
amount: { | ||
amount: amount.amount, | ||
denom: amount.denom, | ||
}, | ||
}; | ||
}, | ||
fromAmino: ({ delegator_address, validator_address, amount }) => ({ | ||
delegatorAddress: delegator_address, | ||
validatorAddress: validator_address, | ||
amount: amount, | ||
}), | ||
}, | ||
"/cosmos.staking.v1beta1.MsgEditValidator": { | ||
aminoType: "cosmos-sdk/MsgEditValidator", | ||
toAmino: ({ description, commissionRate, minSelfDelegation, validatorAddress, }) => { | ||
utils_1.assert(description, "missing description"); | ||
utils_1.assert(description.moniker, "missing description.moniker"); | ||
utils_1.assert(description.identity, "missing description.identity"); | ||
utils_1.assert(description.website, "missing description.website"); | ||
utils_1.assert(description.securityContact, "missing description.securityContact"); | ||
utils_1.assert(description.details, "missing description.details"); | ||
utils_1.assert(commissionRate, "missing commissionRate"); | ||
utils_1.assert(minSelfDelegation, "missing minSelfDelegation"); | ||
utils_1.assert(validatorAddress, "missing validatorAddress"); | ||
return { | ||
description: { | ||
moniker: description.moniker, | ||
identity: description.identity, | ||
website: description.website, | ||
security_contact: description.securityContact, | ||
details: description.details, | ||
}, | ||
commission_rate: commissionRate, | ||
min_self_delegation: minSelfDelegation, | ||
validator_address: validatorAddress, | ||
}; | ||
}, | ||
fromAmino: ({ description, commission_rate, min_self_delegation, validator_address, }) => ({ | ||
description: { | ||
moniker: description.moniker, | ||
identity: description.identity, | ||
website: description.website, | ||
securityContact: description.security_contact, | ||
details: description.details, | ||
}, | ||
commissionRate: commission_rate, | ||
minSelfDelegation: min_self_delegation, | ||
validatorAddress: validator_address, | ||
}), | ||
}, | ||
"/cosmos.staking.v1beta1.MsgUndelegate": { | ||
aminoType: "cosmos-sdk/MsgUndelegate", | ||
toAmino: ({ delegatorAddress, validatorAddress, amount }) => { | ||
utils_1.assert(delegatorAddress, "missing delegatorAddress"); | ||
utils_1.assert(validatorAddress, "missing validatorAddress"); | ||
utils_1.assert(amount, "missing amount"); | ||
utils_1.assert(amount.amount, "missing amount.amount"); | ||
utils_1.assert(amount.denom, "missing amount.denom"); | ||
return { | ||
delegator_address: delegatorAddress, | ||
validator_address: validatorAddress, | ||
amount: { | ||
amount: amount.amount, | ||
denom: amount.denom, | ||
}, | ||
}; | ||
}, | ||
fromAmino: ({ delegator_address, validator_address, amount, }) => ({ | ||
delegatorAddress: delegator_address, | ||
validatorAddress: validator_address, | ||
amount: amount, | ||
}), | ||
}, | ||
}; | ||
} | ||
/** | ||
@@ -29,10 +264,12 @@ * A map from Stargate message types as used in the messages's `Any` type | ||
class AminoTypes { | ||
constructor(additions = {}) { | ||
constructor({ additions = {}, prefix = "cosmos" } = {}) { | ||
const additionalAminoTypes = Object.values(additions); | ||
const filteredDefaultTypes = Object.entries(defaultTypes).reduce((acc, [key, value]) => (additionalAminoTypes.includes(value) ? acc : Object.assign(Object.assign({}, acc), { [key]: value })), {}); | ||
const filteredDefaultTypes = Object.entries(createDefaultTypes(prefix)).reduce((acc, [key, value]) => additionalAminoTypes.find(({ aminoType }) => value.aminoType === aminoType) | ||
? acc | ||
: Object.assign(Object.assign({}, acc), { [key]: value }), {}); | ||
this.register = Object.assign(Object.assign({}, filteredDefaultTypes), additions); | ||
} | ||
toAmino(typeUrl) { | ||
const type = this.register[typeUrl]; | ||
if (!type) { | ||
toAmino({ typeUrl, value }) { | ||
const converter = this.register[typeUrl]; | ||
if (!converter) { | ||
throw new Error("Type URL does not exist in the Amino message type register. " + | ||
@@ -42,8 +279,10 @@ "If you need support for this message type, you can pass in additional entries to the AminoTypes constructor. " + | ||
} | ||
return type; | ||
return { | ||
type: converter.aminoType, | ||
value: converter.toAmino(value), | ||
}; | ||
} | ||
fromAmino(type) { | ||
var _a; | ||
const [typeUrl] = (_a = Object.entries(this.register).find(([_typeUrl, value]) => value === type)) !== null && _a !== void 0 ? _a : []; | ||
if (!typeUrl) { | ||
fromAmino({ type, value }) { | ||
const result = Object.entries(this.register).find(([_typeUrl, { aminoType }]) => aminoType === type); | ||
if (!result) { | ||
throw new Error("Type does not exist in the Amino message type register. " + | ||
@@ -53,3 +292,7 @@ "If you need support for this message type, you can pass in additional entries to the AminoTypes constructor. " + | ||
} | ||
return typeUrl; | ||
const [typeUrl, converter] = result; | ||
return { | ||
typeUrl: typeUrl, | ||
value: converter.fromAmino(value), | ||
}; | ||
} | ||
@@ -56,0 +299,0 @@ } |
@@ -13,2 +13,4 @@ "use strict"; | ||
const stargateclient_1 = require("./stargateclient"); | ||
const { MsgMultiSend } = codec_1.cosmos.bank.v1beta1; | ||
const { MsgBeginRedelegate, MsgCreateValidator, MsgDelegate, MsgEditValidator, MsgUndelegate, } = codec_1.cosmos.staking.v1beta1; | ||
const { TxRaw } = codec_1.cosmos.tx.v1beta1; | ||
@@ -20,9 +22,16 @@ const defaultGasPrice = launchpad_1.GasPrice.fromString("0.025ucosm"); | ||
super(tmClient); | ||
this.aminoTypes = new aminotypes_1.AminoTypes(); | ||
const { registry = new proto_signing_1.Registry(), gasPrice = defaultGasPrice, gasLimits = defaultGasLimits } = options; | ||
const { registry = new proto_signing_1.Registry([ | ||
["/cosmos.bank.v1beta1.MsgMultiSend", MsgMultiSend], | ||
["/cosmos.staking.v1beta1.MsgBeginRedelegate", MsgBeginRedelegate], | ||
["/cosmos.staking.v1beta1.MsgCreateValidator", MsgCreateValidator], | ||
["/cosmos.staking.v1beta1.MsgDelegate", MsgDelegate], | ||
["/cosmos.staking.v1beta1.MsgEditValidator", MsgEditValidator], | ||
["/cosmos.staking.v1beta1.MsgUndelegate", MsgUndelegate], | ||
]), aminoTypes = new aminotypes_1.AminoTypes(), gasPrice = defaultGasPrice, gasLimits = defaultGasLimits, } = options; | ||
this.fees = launchpad_1.buildFeeTable(gasPrice, defaultGasLimits, gasLimits); | ||
this.registry = registry; | ||
this.aminoTypes = aminoTypes; | ||
this.signer = signer; | ||
} | ||
static async connectWithWallet(endpoint, signer, options = {}) { | ||
static async connectWithSigner(endpoint, signer, options = {}) { | ||
const tmClient = await tendermint_rpc_1.Client.connect(endpoint, tendermint_rpc_1.adaptor34); | ||
@@ -81,13 +90,7 @@ return new SigningStargateClient(tmClient, signer, options); | ||
const signMode = codec_1.cosmos.tx.signing.v1beta1.SignMode.SIGN_MODE_LEGACY_AMINO_JSON; | ||
const msgs = messages.map((msg) => ({ | ||
type: this.aminoTypes.toAmino(msg.typeUrl), | ||
value: msg.value, | ||
})); | ||
const msgs = messages.map((msg) => this.aminoTypes.toAmino(msg)); | ||
const signDoc = launchpad_1.makeSignDoc(msgs, fee, chainId, memo, accountNumber, sequence); | ||
const { signature, signed } = await this.signer.signAmino(address, signDoc); | ||
const signedTxBody = { | ||
messages: signed.msgs.map((msg) => ({ | ||
typeUrl: this.aminoTypes.fromAmino(msg.type), | ||
value: msg.value, | ||
})), | ||
messages: signed.msgs.map((msg) => this.aminoTypes.fromAmino(msg)), | ||
memo: signed.memo, | ||
@@ -94,0 +97,0 @@ }; |
{ | ||
"name": "@cosmjs/stargate", | ||
"version": "0.24.0-alpha.14", | ||
"version": "0.24.0-alpha.15", | ||
"description": "Utilities for Cosmos SDK 0.40", | ||
@@ -52,5 +52,5 @@ "contributors": [ | ||
"@cosmjs/encoding": "^0.24.0-alpha.14", | ||
"@cosmjs/launchpad": "^0.24.0-alpha.14", | ||
"@cosmjs/launchpad": "^0.24.0-alpha.15", | ||
"@cosmjs/math": "^0.24.0-alpha.14", | ||
"@cosmjs/proto-signing": "^0.24.0-alpha.14", | ||
"@cosmjs/proto-signing": "^0.24.0-alpha.15", | ||
"@cosmjs/stream": "^0.24.0-alpha.14", | ||
@@ -62,3 +62,3 @@ "@cosmjs/tendermint-rpc": "^0.24.0-alpha.14", | ||
}, | ||
"gitHead": "55d66be270471b04e1e8587d06ab7da2ac30bbc0" | ||
"gitHead": "fb124ed6116a041897be36519cc5e812c9dd768c" | ||
} |
@@ -0,1 +1,12 @@ | ||
import { Msg } from "@cosmjs/launchpad"; | ||
import { EncodeObject } from "@cosmjs/proto-signing"; | ||
export interface AminoConverter { | ||
readonly aminoType: string; | ||
readonly toAmino: (value: any) => any; | ||
readonly fromAmino: (value: any) => any; | ||
} | ||
interface AminoTypesOptions { | ||
readonly additions?: Record<string, AminoConverter>; | ||
readonly prefix?: string; | ||
} | ||
/** | ||
@@ -7,5 +18,6 @@ * A map from Stargate message types as used in the messages's `Any` type | ||
private readonly register; | ||
constructor(additions?: Record<string, string>); | ||
toAmino(typeUrl: string): string; | ||
fromAmino(type: string): string; | ||
constructor({ additions, prefix }?: AminoTypesOptions); | ||
toAmino({ typeUrl, value }: EncodeObject): Msg; | ||
fromAmino({ type, value }: Msg): EncodeObject; | ||
} | ||
export {}; |
import { Coin, CosmosFeeTable, GasLimits, GasPrice, StdFee } from "@cosmjs/launchpad"; | ||
import { EncodeObject, OfflineSigner, Registry } from "@cosmjs/proto-signing"; | ||
import { AminoTypes } from "./aminotypes"; | ||
import { BroadcastTxResponse, StargateClient } from "./stargateclient"; | ||
@@ -11,2 +12,3 @@ /** Use for testing only */ | ||
readonly registry?: Registry; | ||
readonly aminoTypes?: AminoTypes; | ||
readonly gasPrice?: GasPrice; | ||
@@ -20,3 +22,3 @@ readonly gasLimits?: GasLimits<CosmosFeeTable>; | ||
private readonly aminoTypes; | ||
static connectWithWallet( | ||
static connectWithSigner( | ||
endpoint: string, | ||
@@ -23,0 +25,0 @@ signer: OfflineSigner, |
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
1357087
30515