Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keys: define proto type #5997

Closed
wants to merge 52 commits into from
Closed
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
caa7560
crypto/keys: move keybase and keyring to crypto/keyring/ dir
fedekunze Mar 24, 2020
0668da6
Update client/keys/root.go
fedekunze Mar 24, 2020
7b42d88
Update crypto/keyring/errors.go
fedekunze Mar 24, 2020
018b256
Update crypto/keyring/keybase.go
fedekunze Mar 24, 2020
3f4609e
Update crypto/keyring/options.go
fedekunze Mar 24, 2020
9e23f88
format
fedekunze Mar 24, 2020
30e0a38
Merge branch 'fedekunze/5819-move-keys' of https://github.com/cosmos/…
fedekunze Mar 24, 2020
49b6fd4
changelog
fedekunze Mar 24, 2020
715cd36
fix build
fedekunze Mar 24, 2020
c36c9f1
format
fedekunze Mar 24, 2020
2e10608
crypto/keys: move tendermint key types to the SDK
fedekunze Mar 24, 2020
1e96870
crypto/keys: flatten pkgs
fedekunze Mar 25, 2020
49034e9
crypto/keys: cleanup types
fedekunze Mar 25, 2020
5e4724d
revert dep change
fedekunze Mar 25, 2020
7849944
cleanup
fedekunze Mar 25, 2020
e040f03
update proto message for multisig
fedekunze Mar 25, 2020
47032f9
genprivkey functions
fedekunze Mar 25, 2020
411ae36
build errors
fedekunze Mar 25, 2020
0455a2f
add missing methods for secp256k1
fedekunze Mar 25, 2020
239abd5
more fixes
fedekunze Mar 25, 2020
4e7cad3
rebase to master
fedekunze Mar 25, 2020
2ce648f
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into fe…
fedekunze Mar 26, 2020
c7c9ab8
crypto/keys: add ByteArray util function for each key type
fedekunze Mar 26, 2020
371241f
crypto/keys: fixes from tests
fedekunze Mar 26, 2020
b7c6fcd
tests
fedekunze Mar 26, 2020
dd94902
cast types
tac0turtle Apr 15, 2020
9135567
merge master
fedekunze Apr 15, 2020
6ae127e
Merge branch 'fedekunze/5819-tm-keys' of github.com:cosmos/cosmos-sdk…
fedekunze Apr 15, 2020
92dc996
fix build
tac0turtle Apr 15, 2020
3586202
Merge branch 'marko/pubkeys' of https://github.com/cosmos/cosmos-sdk …
tac0turtle Apr 15, 2020
1960b0f
move PubKey and PrivKey to codec/std/
fedekunze Apr 15, 2020
980ae75
add to/from proto keys
tac0turtle Apr 16, 2020
9a3f012
remove bit array
tac0turtle Apr 16, 2020
fe64a81
bring back multisig
tac0turtle Apr 16, 2020
625ef37
Merge branch 'master' into marko/pubkeys
tac0turtle Apr 16, 2020
9f5b23d
add pubkey threshold to from to pubkeys
tac0turtle Apr 16, 2020
ddcbae3
add marshlers to compactbitarray
tac0turtle Apr 16, 2020
7015bdf
appease linter
tac0turtle Apr 16, 2020
e49b200
format according to guidelines
tac0turtle Apr 16, 2020
c5dd830
Apply suggestions from code review
tac0turtle Apr 16, 2020
3769492
Merge branch 'master' into marko/pubkeys
fedekunze Apr 16, 2020
c75f2b2
Merge branch 'master' into marko/pubkeys
fedekunze Apr 18, 2020
c47a74c
Merge branch 'master' into marko/pubkeys
tac0turtle Apr 20, 2020
34f5af2
migrate multisig to sdk
tac0turtle Apr 20, 2020
c2b3dc0
move bitarray to types
tac0turtle Apr 20, 2020
ea2afd8
move it up on dir
tac0turtle Apr 20, 2020
1f1d07d
Merge branch 'master' into marko/pubkeys
fedekunze Apr 20, 2020
e0b1ce1
fix pubkey tests
tac0turtle Apr 21, 2020
a15dfd7
Merge branch 'master' into marko/pubkeys
fedekunze Apr 21, 2020
855e976
Merge branch 'master' into marko/pubkeys
fedekunze Apr 21, 2020
94efb32
Merge branch 'master' into marko/pubkeys
Apr 21, 2020
6e1bdd6
Merge branch 'master' into marko/pubkeys
tac0turtle Apr 22, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 30 additions & 25 deletions codec/std/codec.proto
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ message Supply {
// Evidence defines the application-level allowed Evidence to be submitted via a
// MsgSubmitEvidence message.
message Evidence {
option (gogoproto.equal) = true;
option (cosmos_proto.interface_type) = "github.com/cosmos/cosmos-sdk/x/evidence/exported.Evidence";
option (gogoproto.equal) = true;
option (cosmos_proto.interface_type) =
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
"github.com/cosmos/cosmos-sdk/x/evidence/exported.Evidence";

// sum defines a set of all acceptable concrete Evidence implementations.
oneof sum {
Expand All @@ -62,8 +63,9 @@ message MsgSubmitEvidence {
option (gogoproto.equal) = true;
option (gogoproto.goproto_getters) = false;

cosmos_sdk.x.evidence.v1.MsgSubmitEvidenceBase base = 1 [(gogoproto.nullable) = false, (gogoproto.embed) = true];
Evidence evidence = 2;
cosmos_sdk.x.evidence.v1.MsgSubmitEvidenceBase base = 1
[(gogoproto.nullable) = false, (gogoproto.embed) = true];
Evidence evidence = 2;
}

// MsgSubmitProposal defines the application-level message type for handling
Expand All @@ -72,17 +74,19 @@ message MsgSubmitProposal {
option (gogoproto.equal) = true;
option (gogoproto.goproto_getters) = false;

cosmos_sdk.x.gov.v1.MsgSubmitProposalBase base = 1 [(gogoproto.nullable) = false, (gogoproto.embed) = true];
Content content = 2;
cosmos_sdk.x.gov.v1.MsgSubmitProposalBase base = 1
[(gogoproto.nullable) = false, (gogoproto.embed) = true];
Content content = 2;
}

// Proposal defines the application-level concrete proposal type used in governance
// proposals.
message Proposal {
option (gogoproto.equal) = true;

cosmos_sdk.x.gov.v1.ProposalBase base = 1 [(gogoproto.embed) = true, (gogoproto.nullable) = false];
Content content = 2 [(gogoproto.nullable) = false];
cosmos_sdk.x.gov.v1.ProposalBase base = 1
[(gogoproto.embed) = true, (gogoproto.nullable) = false];
Content content = 2 [(gogoproto.nullable) = false];
}

// Content defines the application-level allowed Content to be included in a
Expand Down Expand Up @@ -118,23 +122,24 @@ message Message {

// sum defines the set of all allowed valid messages defined in modules.
oneof sum {
cosmos_sdk.x.bank.v1.MsgSend msg_send = 1;
cosmos_sdk.x.bank.v1.MsgMultiSend msg_multi_send = 2;
cosmos_sdk.x.crisis.v1.MsgVerifyInvariant msg_verify_invariant = 3;
cosmos_sdk.x.distribution.v1.MsgSetWithdrawAddress msg_set_withdraw_address = 4;
cosmos_sdk.x.distribution.v1.MsgWithdrawDelegatorReward msg_withdraw_delegator_reward = 5;
cosmos_sdk.x.distribution.v1.MsgWithdrawValidatorCommission msg_withdraw_validator_commission = 6;
cosmos_sdk.x.distribution.v1.MsgFundCommunityPool msg_fund_community_pool = 7;
MsgSubmitEvidence msg_submit_evidence = 8;
MsgSubmitProposal msg_submit_proposal = 9;
cosmos_sdk.x.gov.v1.MsgVote msg_vote = 10;
cosmos_sdk.x.gov.v1.MsgDeposit msg_deposit = 11;
cosmos_sdk.x.slashing.v1.MsgUnjail msg_unjail = 12;
cosmos_sdk.x.staking.v1.MsgCreateValidator msg_create_validator = 13;
cosmos_sdk.x.staking.v1.MsgEditValidator msg_edit_validator = 14;
cosmos_sdk.x.staking.v1.MsgDelegate msg_delegate = 15;
cosmos_sdk.x.staking.v1.MsgBeginRedelegate msg_begin_redelegate = 16;
cosmos_sdk.x.staking.v1.MsgUndelegate msg_undelegate = 17;
cosmos_sdk.x.bank.v1.MsgSend msg_send = 1;
cosmos_sdk.x.bank.v1.MsgMultiSend msg_multi_send = 2;
cosmos_sdk.x.crisis.v1.MsgVerifyInvariant msg_verify_invariant = 3;
cosmos_sdk.x.distribution.v1.MsgSetWithdrawAddress msg_set_withdraw_address = 4;
cosmos_sdk.x.distribution.v1.MsgWithdrawDelegatorReward msg_withdraw_delegator_reward = 5;
cosmos_sdk.x.distribution.v1.MsgWithdrawValidatorCommission msg_withdraw_validator_commission =
6;
cosmos_sdk.x.distribution.v1.MsgFundCommunityPool msg_fund_community_pool = 7;
MsgSubmitEvidence msg_submit_evidence = 8;
MsgSubmitProposal msg_submit_proposal = 9;
cosmos_sdk.x.gov.v1.MsgVote msg_vote = 10;
cosmos_sdk.x.gov.v1.MsgDeposit msg_deposit = 11;
cosmos_sdk.x.slashing.v1.MsgUnjail msg_unjail = 12;
cosmos_sdk.x.staking.v1.MsgCreateValidator msg_create_validator = 13;
cosmos_sdk.x.staking.v1.MsgEditValidator msg_edit_validator = 14;
cosmos_sdk.x.staking.v1.MsgDelegate msg_delegate = 15;
cosmos_sdk.x.staking.v1.MsgBeginRedelegate msg_begin_redelegate = 16;
cosmos_sdk.x.staking.v1.MsgUndelegate msg_undelegate = 17;
}
}

Expand Down
223 changes: 223 additions & 0 deletions codec/std/compact_bit_array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
package std
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved

import (
"encoding/binary"
"fmt"
"regexp"
"strings"
)

// NewCompactBitArray returns a new compact bit array.
// It returns nil if the number of bits is zero.
func NewCompactBitArray(bits int) *CompactBitArray {
if bits <= 0 {
return nil
}
return &CompactBitArray{
ExtraBitsStored: []byte{byte(bits % 8)},
Elems: make([]byte, (bits+7)/8),
}
}

// Size returns the number of bits in the bitarray
func (bA *CompactBitArray) Size() int {
if bA == nil {
return 0
} else if bA.ExtraBitsStored[0] == byte(0) {
return len(bA.Elems) * 8
}
// num_bits = 8*num_full_bytes + overflow_in_last_byte
// num_full_bytes = (len(bA.Elems)-1)
return (len(bA.Elems)-1)*8 + int(bA.ExtraBitsStored[0])
}

// GetIndex returns the bit at index i within the bit array.
// The behavior is undefined if i >= bA.Size()
func (bA *CompactBitArray) GetIndex(i int) bool {
if bA == nil {
return false
}
if i >= bA.Size() {
return false
}
return bA.Elems[i>>3]&(uint8(1)<<uint8(7-(i%8))) > 0
}

// SetIndex sets the bit at index i within the bit array.
// The behavior is undefined if i >= bA.Size()
func (bA *CompactBitArray) SetIndex(i int, v bool) bool {
if bA == nil {
return false
}
if i >= bA.Size() {
return false
}
if v {
bA.Elems[i>>3] |= (uint8(1) << uint8(7-(i%8)))
} else {
bA.Elems[i>>3] &= ^(uint8(1) << uint8(7-(i%8)))
}
return true
}

// NumTrueBitsBefore returns the number of bits set to true before the
// given index. e.g. if bA = _XX__XX, NumOfTrueBitsBefore(4) = 2, since
// there are two bits set to true before index 4.
func (bA *CompactBitArray) NumTrueBitsBefore(index int) int {
numTrueValues := 0
for i := 0; i < index; i++ {
if bA.GetIndex(i) {
numTrueValues++
}
}
return numTrueValues
}

// Copy returns a copy of the provided bit array.
func (bA *CompactBitArray) Copy() *CompactBitArray {
if bA == nil {
return nil
}
c := make([]byte, len(bA.Elems))
copy(c, bA.Elems)
return &CompactBitArray{
ExtraBitsStored: bA.ExtraBitsStored,
Elems: c,
}
}

// String returns a string representation of CompactBitArray: BA{<bit-string>},
// where <bit-string> is a sequence of 'x' (1) and '_' (0).
// The <bit-string> includes spaces and newlines to help people.
// For a simple sequence of 'x' and '_' characters with no spaces or newlines,
// see the MarshalJSON() method.
// Example: "BA{_x_}" or "nil-BitArray" for nil.
func (bA *CompactBitArray) String() string {
return bA.StringIndented("")
}

// StringIndented returns the same thing as String(), but applies the indent
// at every 10th bit, and twice at every 50th bit.
func (bA *CompactBitArray) StringIndented(indent string) string {
if bA == nil {
return "nil-BitArray"
}
lines := []string{}
bits := ""
size := bA.Size()
for i := 0; i < size; i++ {
if bA.GetIndex(i) {
bits += "x"
} else {
bits += "_"
}
if i%100 == 99 {
lines = append(lines, bits)
bits = ""
}
if i%10 == 9 {
bits += indent
}
if i%50 == 49 {
bits += indent
}
}
if len(bits) > 0 {
lines = append(lines, bits)
}
return fmt.Sprintf("BA{%v:%v}", size, strings.Join(lines, indent))
}

// MarshalJSON implements json.Marshaler interface by marshaling bit array
// using a custom format: a string of '-' or 'x' where 'x' denotes the 1 bit.
func (bA *CompactBitArray) MarshalJSON() ([]byte, error) {
if bA == nil {
return []byte("null"), nil
}

bits := `"`
size := bA.Size()
for i := 0; i < size; i++ {
if bA.GetIndex(i) {
bits += `x`
} else {
bits += `_`
}
}
bits += `"`
return []byte(bits), nil
}

var bitArrayJSONRegexp = regexp.MustCompile(`\A"([_x]*)"\z`)

// UnmarshalJSON implements json.Unmarshaler interface by unmarshaling a custom
// JSON description.
func (bA *CompactBitArray) UnmarshalJSON(bz []byte) error {
b := string(bz)
if b == "null" {
// This is required e.g. for encoding/json when decoding
// into a pointer with pre-allocated BitArray.
bA.ExtraBitsStored = []byte{0}
bA.Elems = nil
return nil
}

// Validate 'b'.
match := bitArrayJSONRegexp.FindStringSubmatch(b)
if match == nil {
return fmt.Errorf("bitArray in JSON should be a string of format %q but got %s", bitArrayJSONRegexp.String(), b)
}
bits := match[1]

// Construct new CompactBitArray and copy over.
numBits := len(bits)
bA2 := NewCompactBitArray(numBits)
for i := 0; i < numBits; i++ {
if bits[i] == 'x' {
bA2.SetIndex(i, true)
}
}
*bA = *bA2
return nil
}

// // CompactMarshal is a space efficient encoding for CompactBitArray.
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
// // It is not amino compatible.
// func (bA *CompactBitArray) Marshal() []byte {
// size := bA.Size()
// if size <= 0 {
// return []byte("null")
// }
// bz := make([]byte, 0, size/8)
// // length prefix number of bits, not number of bytes. This difference
// // takes 3-4 bits in encoding, as opposed to instead encoding the number of
// // bytes (saving 3-4 bits) and including the offset as a full byte.
// bz = appendUvarint(bz, uint64(size))
// bz = append(bz, bA.Elems...)
// return bz
// }

// // CompactUnmarshal is a space efficient decoding for CompactBitArray.
// // It is not amino compatible.
// func (bA *CompactBitArray) Unmarshal(bz []byte) error {
// if len(bz) < 2 {
// return errors.New("compact bit array: invalid compact unmarshal size")
// } else if bytes.Equal(bz, []byte("null")) {
// return nil
// }
// size, n := binary.Uvarint(bz)
// bz = bz[n:]
// if len(bz) != int(size+7)/8 {
// return errors.New("compact bit array: invalid compact unmarshal size")
// }

// bA = NewCompactBitArray(int(size % 8))
// bA.Elems = bz
// return nil
// }

func appendUvarint(b []byte, x uint64) []byte {
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
var a [binary.MaxVarintLen64]byte
n := binary.PutUvarint(a[:], x)
return append(b, a[:n]...)
}
Loading