Make input and output of decodePubkey non-optional

This commit is contained in:
Simon Warta 2022-12-08 12:55:11 +01:00
parent 6b05c122fd
commit 866a831dca
4 changed files with 13 additions and 10 deletions

View File

@ -16,10 +16,13 @@ and this project adheres to
`pbkdf2Sha512Crypto` was removed. Node.js has sufficient support for WebCrypto `pbkdf2Sha512Crypto` was removed. Node.js has sufficient support for WebCrypto
these days and we still have a pure-JS fallback implementation. This avoids these days and we still have a pure-JS fallback implementation. This avoids
unnecessary problems around importing Node.js modules. ([#1341]) unnecessary problems around importing Node.js modules. ([#1341])
- @cosmjs/proto-signing: Make input and output of `decodePubkey` non-optional
([#1289]).
[#1002]: https://github.com/cosmos/cosmjs/issues/1002 [#1002]: https://github.com/cosmos/cosmjs/issues/1002
[#1341]: https://github.com/cosmos/cosmjs/issues/1341 [#1341]: https://github.com/cosmos/cosmjs/issues/1341
[#1240]: https://github.com/cosmos/cosmjs/pull/1240 [#1240]: https://github.com/cosmos/cosmjs/pull/1240
[#1289]: https://github.com/cosmos/cosmjs/issues/1289
## [0.29.5] - 2022-12-07 ## [0.29.5] - 2022-12-07

View File

@ -57,8 +57,7 @@ export function encodePubkey(pubkey: Pubkey): Any {
* Decodes a single pubkey (i.e. not a multisig pubkey) from `Any` into * Decodes a single pubkey (i.e. not a multisig pubkey) from `Any` into
* `SinglePubkey`. * `SinglePubkey`.
* *
* In most cases you probably want to use `decodePubkey`, but `anyToSinglePubkey` * In most cases you probably want to use `decodePubkey`.
* might be preferred in CosmJS 0.29.x due to https://github.com/cosmos/cosmjs/issues/1289.
*/ */
export function anyToSinglePubkey(pubkey: Any): SinglePubkey { export function anyToSinglePubkey(pubkey: Any): SinglePubkey {
switch (pubkey.typeUrl) { switch (pubkey.typeUrl) {
@ -75,11 +74,12 @@ export function anyToSinglePubkey(pubkey: Any): SinglePubkey {
} }
} }
export function decodePubkey(pubkey?: Any | null): Pubkey | null { /**
if (!pubkey || !pubkey.value) { * Decodes a pubkey from a protobuf `Any` into `Pubkey`.
return null; * This supports single pubkeys such as Cosmos ed25519 and secp256k1 keys
} * as well as multisig threshold pubkeys.
*/
export function decodePubkey(pubkey: Any): Pubkey {
switch (pubkey.typeUrl) { switch (pubkey.typeUrl) {
case "/cosmos.crypto.secp256k1.PubKey": case "/cosmos.crypto.secp256k1.PubKey":
case "/cosmos.crypto.ed25519.PubKey": { case "/cosmos.crypto.ed25519.PubKey": {

View File

@ -26,7 +26,7 @@ function uint64FromProto(input: number | Long): Uint64 {
function accountFromBaseAccount(input: BaseAccount): Account { function accountFromBaseAccount(input: BaseAccount): Account {
const { address, pubKey, accountNumber, sequence } = input; const { address, pubKey, accountNumber, sequence } = input;
const pubkey = decodePubkey(pubKey); const pubkey = pubKey ? decodePubkey(pubKey) : null;
return { return {
address: address, address: address,
pubkey: pubkey, pubkey: pubkey,

View File

@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */ /* eslint-disable @typescript-eslint/naming-convention */
import { AminoMsg, Coin, Pubkey } from "@cosmjs/amino"; import { AminoMsg, Coin, Pubkey } from "@cosmjs/amino";
import { Decimal } from "@cosmjs/math"; import { Decimal } from "@cosmjs/math";
import { anyToSinglePubkey, encodePubkey } from "@cosmjs/proto-signing"; import { decodePubkey, encodePubkey } from "@cosmjs/proto-signing";
import { assertDefinedAndNotNull } from "@cosmjs/utils"; import { assertDefinedAndNotNull } from "@cosmjs/utils";
import { import {
MsgBeginRedelegate, MsgBeginRedelegate,
@ -206,7 +206,7 @@ export function createStakingAminoConverters(
min_self_delegation: minSelfDelegation, min_self_delegation: minSelfDelegation,
delegator_address: delegatorAddress, delegator_address: delegatorAddress,
validator_address: validatorAddress, validator_address: validatorAddress,
pubkey: anyToSinglePubkey(pubkey), pubkey: decodePubkey(pubkey),
value: value, value: value,
}; };
}, },