Skip to content

Commit 6f908eb

Browse files
committed
crypto: add in secp256k1 support (#5500)
Secp256k1 was removed in the protobuf migration, this pr adds it back in order to provide this functionality for users (band) Closes: #5495
1 parent b3238cd commit 6f908eb

16 files changed

+711
-100
lines changed

.markdownlintignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
docs/node_modules
22
CHANGELOG.md
33
docs/architecture/*
4+
crypto/secp256k1/**
45
scripts/*
56
.github

CHANGELOG.md

-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ Friendly reminder, we have a [bug bounty program](https://hackerone.com/tendermi
5151
- [evidence] [\#5319](https://github.com/tendermint/tendermint/issues/5319) Remove Amnesia & potentialAmnesia evidence types and removed POLC. (@marbar3778)
5252
- [evidence] [\#5361](https://github.com/tendermint/tendermint/pull/5361) Add LightClientAttackEvidence and change evidence interface (@cmwaters)
5353
- [params] [\#5319](https://github.com/tendermint/tendermint/issues/5319) Remove `ProofofTrialPeriod` from evidence params (@marbar3778)
54-
- [crypto/secp256k1] [\#5280](https://github.com/tendermint/tendermint/issues/5280) `secp256k1` has been removed from the Tendermint repo. (@marbar3778)
5554
- [light] [\#5347](https://github.com/tendermint/tendermint/issues/5347) `NewClient`, `NewHTTPClient`, `VerifyHeader` and `VerifyLightBlockAtHeight` now accept `context.Context` as 1st param (@melekes)
5655
- [state] [\#5348](https://github.com/tendermint/tendermint/issues/5348) Define an Interface for the state store. (@marbar3778)
5756

UPGRADING.md

+51-54
Original file line numberDiff line numberDiff line change
@@ -10,63 +10,63 @@ the encoding format (see "Protocol Buffers," below) and the block header (see "B
1010

1111
### ABCI Changes
1212

13-
* New ABCI methods (`ListSnapshots`, `LoadSnapshotChunk`, `OfferSnapshot`, and `ApplySnapshotChunk`)
14-
were added to support the new State Sync feature.
15-
Previously, syncing a new node to a preexisting network could take days; but with State Sync,
16-
new nodes are able to join a network in a matter of seconds.
17-
Read [the spec](https://docs.tendermint.com/master/spec/abci/apps.html#state-sync)
18-
if you want to learn more about State Sync, or if you'd like your application to use it.
19-
(If you don't want to support State Sync in your application, you can just implement these new
20-
ABCI methods as no-ops, leaving them empty.)
21-
22-
* `KV.Pair` has been replaced with `abci.EventAttribute`. The `EventAttribute.Index` field
13+
* New ABCI methods (`ListSnapshots`, `LoadSnapshotChunk`, `OfferSnapshot`, and `ApplySnapshotChunk`)
14+
were added to support the new State Sync feature.
15+
Previously, syncing a new node to a preexisting network could take days; but with State Sync,
16+
new nodes are able to join a network in a matter of seconds.
17+
Read [the spec](https://docs.tendermint.com/master/spec/abci/apps.html#state-sync)
18+
if you want to learn more about State Sync, or if you'd like your application to use it.
19+
(If you don't want to support State Sync in your application, you can just implement these new
20+
ABCI methods as no-ops, leaving them empty.)
21+
22+
* `KV.Pair` has been replaced with `abci.EventAttribute`. The `EventAttribute.Index` field
2323
allows ABCI applications to dictate which events should be indexed.
2424

25-
* The blockchain can now start from an arbitrary initial height,
25+
* The blockchain can now start from an arbitrary initial height,
2626
provided to the application via `RequestInitChain.InitialHeight`.
2727

28-
* ABCI evidence type is now an enum with two recognized types of evidence:
29-
`DUPLICATE_VOTE` and `LIGHT_CLIENT_ATTACK`.
30-
Applications should be able to handle these evidence types
28+
* ABCI evidence type is now an enum with two recognized types of evidence:
29+
`DUPLICATE_VOTE` and `LIGHT_CLIENT_ATTACK`.
30+
Applications should be able to handle these evidence types
3131
(i.e., through slashing or other accountability measures).
3232

33-
* The [`PublicKey` type](https://github.com/tendermint/tendermint/blob/master/proto/tendermint/crypto/keys.proto#L13-L15)
34-
(used in ABCI as part of `ValidatorUpdate`) now uses a `oneof` protobuf type.
35-
Note that since Tendermint only supports ed25519 validator keys, there's only one
33+
* The [`PublicKey` type](https://github.com/tendermint/tendermint/blob/master/proto/tendermint/crypto/keys.proto#L13-L15)
34+
(used in ABCI as part of `ValidatorUpdate`) now uses a `oneof` protobuf type.
35+
Note that since Tendermint only supports ed25519 validator keys, there's only one
3636
option in the `oneof`. For more, see "Protocol Buffers," below.
3737

38-
* The field `Proof`, on the ABCI type `ResponseQuery`, is now named `ProofOps`.
39-
For more, see "Crypto," below.
38+
* The field `Proof`, on the ABCI type `ResponseQuery`, is now named `ProofOps`.
39+
For more, see "Crypto," below.
4040

4141
### P2P Protocol
4242

4343
The default codec is now proto3, not amino. The schema files can be found in the `/proto`
44-
directory. For more, see "Protobuf," below.
44+
directory. For more, see "Protobuf," below.
4545

4646
### Blockchain Protocol
4747

48-
* `Header#LastResultsHash` previously was the root hash of a Merkle tree built from `ResponseDeliverTx(Code, Data)` responses.
48+
* `Header#LastResultsHash` previously was the root hash of a Merkle tree built from `ResponseDeliverTx(Code, Data)` responses.
4949
As of 0.34,`Header#LastResultsHash` is now the root hash of a Merkle tree built from:
5050
* `BeginBlock#Events`
5151
* Root hash of a Merkle tree built from `ResponseDeliverTx(Code, Data,
5252
GasWanted, GasUsed, Events)` responses
5353
* `BeginBlock#Events`
5454

5555
* Merkle hashes of empty trees previously returned nothing, but now return the hash of an empty input,
56-
to conform with [RFC-6962](https://tools.ietf.org/html/rfc6962).
56+
to conform with [RFC-6962](https://tools.ietf.org/html/rfc6962).
5757
This mainly affects `Header#DataHash`, `Header#LastResultsHash`, and
5858
`Header#EvidenceHash`, which are often empty. Non-empty hashes can also be affected, e.g. if their
5959
inputs depend on other (empty) Merkle hashes, giving different results.
6060

6161
### Transaction Indexing
6262

63-
Tendermint now relies on the application to tell it which transactions to index. This means that
64-
in the `config.toml`, generated by Tendermint, there is no longer a way to specify which
63+
Tendermint now relies on the application to tell it which transactions to index. This means that
64+
in the `config.toml`, generated by Tendermint, there is no longer a way to specify which
6565
transactions to index. `tx.height` & `tx.hash` will always be indexed when using the `kv` indexer.
6666

67-
Applications must now choose to either a) enable indexing for all transactions, or
67+
Applications must now choose to either a) enable indexing for all transactions, or
6868
b) allow node operators to decide which transactions to index.
69-
Applications can notify Tendermint to index a specific transaction by setting
69+
Applications can notify Tendermint to index a specific transaction by setting
7070
`Index: bool` to `true` in the Event Attribute:
7171

7272
```go
@@ -82,19 +82,19 @@ Applications can notify Tendermint to index a specific transaction by setting
8282

8383
### Protocol Buffers
8484

85-
Tendermint 0.34 replaces Amino with Protocol Buffers for encoding.
86-
This migration is extensive and results in a number of changes, however,
85+
Tendermint 0.34 replaces Amino with Protocol Buffers for encoding.
86+
This migration is extensive and results in a number of changes, however,
8787
Tendermint only uses the types generated from Protocol Buffers for disk and
88-
wire serialization.
88+
wire serialization.
8989
**This means that these changes should not affect you as a Tendermint user.**
9090

9191
However, Tendermint users and contributors may note the following changes:
9292

93-
* Directory layout changes: All proto files have been moved under one directory, `/proto`.
94-
This is in line with the recommended file layout by [Buf](https://buf.build).
93+
* Directory layout changes: All proto files have been moved under one directory, `/proto`.
94+
This is in line with the recommended file layout by [Buf](https://buf.build).
9595
For more, see the [Buf documentation](https://buf.build/docs/lint-checkers#file_layout).
96-
* ABCI Changes: As noted in the "ABCI Changes" section above, the `PublicKey` type now uses
97-
a `oneof` type.
96+
* ABCI Changes: As noted in the "ABCI Changes" section above, the `PublicKey` type now uses
97+
a `oneof` type.
9898

9999
For more on the Protobuf changes, please see our [blog post on this migration](https://medium.com/tendermint/tendermint-0-34-protocol-buffers-and-you-8c40558939ae).
100100

@@ -114,30 +114,27 @@ Tendermint 0.34 includes new and updated consensus parameters.
114114

115115
#### Keys
116116

117-
* Keys no longer include a type prefix. For example, ed25519 pubkeys have been renamed from
118-
`PubKeyEd25519` to `PubKey`. This reduces stutter (e.g., `ed25519.PubKey`).
117+
* Keys no longer include a type prefix. For example, ed25519 pubkeys have been renamed from
118+
`PubKeyEd25519` to `PubKey`. This reduces stutter (e.g., `ed25519.PubKey`).
119119
* Keys are now byte slices (`[]byte`) instead of byte arrays (`[<size>]byte`).
120-
* The multisig functionality that was previously in Tendermint now has
121-
a new home within the Cosmos SDK:
120+
* The multisig functionality that was previously in Tendermint now has
121+
a new home within the Cosmos SDK:
122122
[`cosmos/cosmos-sdk/types/multisig`](https://github.com/cosmos/cosmos-sdk/blob/master/crypto/types/multisig/multisignature.go).
123-
* Similarly, secp256k1 has been removed from the Tendermint repo.
124-
There is still [a secp256k1 implementation in the Cosmos SDK](https://github.com/cosmos/cosmos-sdk/tree/443e0c1f89bd3730a731aea30453bd732f7efa35/crypto/keys/secp256k1),
125-
and we recommend you use that package for all your secp256k1 needs.
126123

127124
#### `merkle` Package
128125

129126
* `SimpleHashFromMap()` and `SimpleProofsFromMap()` were removed.
130-
* The prefix `Simple` has been removed. (For example, `SimpleProof` is now called `Proof`.)
131-
* All protobuf messages have been moved to the `/proto` directory.
132-
* The protobuf message `Proof` that contained multiple ProofOp's has been renamed to `ProofOps`.
133-
As noted above, this affects the ABCI type `ResponseQuery`:
127+
* The prefix `Simple` has been removed. (For example, `SimpleProof` is now called `Proof`.)
128+
* All protobuf messages have been moved to the `/proto` directory.
129+
* The protobuf message `Proof` that contained multiple ProofOp's has been renamed to `ProofOps`.
130+
As noted above, this affects the ABCI type `ResponseQuery`:
134131
The field that was named Proof is now named `ProofOps`.
135132
* `HashFromByteSlices` and `ProofsFromByteSlices` now return a hash for empty inputs, to conform with
136133
[RFC-6962](https://tools.ietf.org/html/rfc6962).
137134

138135
### `libs` Package
139136

140-
The `bech32` package has moved to the Cosmos SDK:
137+
The `bech32` package has moved to the Cosmos SDK:
141138
[`cosmos/cosmos-sdk/types/bech32`](https://github.com/cosmos/cosmos-sdk/tree/4173ea5ebad906dd9b45325bed69b9c655504867/types/bech32).
142139

143140
### CLI
@@ -147,37 +144,37 @@ See [the docs](https://docs.tendermint.com/master/tendermint-core/light-client-p
147144

148145
### Light Client
149146

150-
We have a new, rewritten light client! You can
147+
We have a new, rewritten light client! You can
151148
[read more](https://medium.com/tendermint/everything-you-need-to-know-about-the-tendermint-light-client-f80d03856f98)
152-
about the justifications and details behind this change.
149+
about the justifications and details behind this change.
153150

154151
Other user-relevant changes include:
155152

156153
* The old `lite` package was removed; the new light client uses the `light` package.
157-
* The `Verifier` was broken up into two pieces:
158-
* Core verification logic (pure `VerifyX` functions)
154+
* The `Verifier` was broken up into two pieces:
155+
* Core verification logic (pure `VerifyX` functions)
159156
* `Client` object, which represents the complete light client
160-
* The RPC client can be found in the `/rpc` directory.
157+
* The RPC client can be found in the `/rpc` directory.
161158
* The HTTP(S) proxy is located in the `/proxy` directory.
162159

163160
### `state` Package
164161

165162
* A new field `State.InitialHeight` has been added to record the initial chain height, which must be `1`
166163
(not `0`) if starting from height `1`. This can be configured via the genesis field `initial_height`.
167-
* The `state` package now has a `Store` interface. All functions in
168-
[state/store.go](https://github.com/tendermint/tendermint/blob/56911ee35298191c95ef1c7d3d5ec508237aaff4/state/store.go#L42-L42)
164+
* The `state` package now has a `Store` interface. All functions in
165+
[state/store.go](https://github.com/tendermint/tendermint/blob/56911ee35298191c95ef1c7d3d5ec508237aaff4/state/store.go#L42-L42)
169166
are now part of the interface. The interface returns errors on all methods and can be used by calling `state.NewStore(dbm.DB)`.
170167

171168
### `privval` Package
172169

173170
All requests are now accompanied by the chain ID from the network.
174-
This is a optional field and can be ignored by key management systems.
171+
This is a optional field and can be ignored by key management systems.
175172
It is recommended to check the chain ID if using the same key management system for multiple chains.
176173

177174
### RPC
178175

179176
`/unsafe_start_cpu_profiler`, `/unsafe_stop_cpu_profiler` and
180-
`/unsafe_write_heap_profile` were removed.
177+
`/unsafe_write_heap_profile` were removed.
181178
For profiling, please use the pprof server, which can
182179
be enabled through `--rpc.pprof_laddr=X` flag or `pprof_laddr=X` config setting
183180
in the rpc section.

abci/example/kvstore/persistent_kvstore.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,11 @@ func (app *PersistentKVStoreApplication) execValidatorTx(tx []byte) types.Respon
243243

244244
// add, update, or remove a validator
245245
func (app *PersistentKVStoreApplication) updateValidator(v types.ValidatorUpdate) types.ResponseDeliverTx {
246-
key := []byte("val:" + string(v.PubKey.GetEd25519()))
247246
pubkey, err := cryptoenc.PubKeyFromProto(v.PubKey)
248247
if err != nil {
249248
panic(fmt.Errorf("can't decode public key: %w", err))
250249
}
250+
key := []byte("val:" + string(pubkey.Bytes()))
251251

252252
if v.Power == 0 {
253253
// remove validator

crypto/ed25519/ed25519.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const (
3131
// private key representations used by RFC 8032.
3232
SeedSize = 32
3333

34-
keyType = "ed25519"
34+
KeyType = "ed25519"
3535
)
3636

3737
func init() {
@@ -93,7 +93,7 @@ func (privKey PrivKey) Equals(other crypto.PrivKey) bool {
9393
}
9494

9595
func (privKey PrivKey) Type() string {
96-
return keyType
96+
return KeyType
9797
}
9898

9999
// GenPrivKey generates a new ed25519 private key.
@@ -159,7 +159,7 @@ func (pubKey PubKey) String() string {
159159
}
160160

161161
func (pubKey PubKey) Type() string {
162-
return keyType
162+
return KeyType
163163
}
164164

165165
func (pubKey PubKey) Equals(other crypto.PubKey) bool {

crypto/encoding/codec.go

+16
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import (
55

66
"github.com/tendermint/tendermint/crypto"
77
"github.com/tendermint/tendermint/crypto/ed25519"
8+
"github.com/tendermint/tendermint/crypto/secp256k1"
89
"github.com/tendermint/tendermint/libs/json"
910
pc "github.com/tendermint/tendermint/proto/tendermint/crypto"
1011
)
1112

1213
func init() {
1314
json.RegisterType((*pc.PublicKey)(nil), "tendermint.crypto.PublicKey")
1415
json.RegisterType((*pc.PublicKey_Ed25519)(nil), "tendermint.crypto.PublicKey_Ed25519")
16+
json.RegisterType((*pc.PublicKey_Secp256K1)(nil), "tendermint.crypto.PublicKey_Secp256K1")
1517
}
1618

1719
// PubKeyToProto takes crypto.PubKey and transforms it to a protobuf Pubkey
@@ -24,6 +26,12 @@ func PubKeyToProto(k crypto.PubKey) (pc.PublicKey, error) {
2426
Ed25519: k,
2527
},
2628
}
29+
case secp256k1.PubKey:
30+
kp = pc.PublicKey{
31+
Sum: &pc.PublicKey_Secp256K1{
32+
Secp256K1: k,
33+
},
34+
}
2735
default:
2836
return kp, fmt.Errorf("toproto: key type %v is not supported", k)
2937
}
@@ -41,6 +49,14 @@ func PubKeyFromProto(k pc.PublicKey) (crypto.PubKey, error) {
4149
pk := make(ed25519.PubKey, ed25519.PubKeySize)
4250
copy(pk, k.Ed25519)
4351
return pk, nil
52+
case *pc.PublicKey_Secp256K1:
53+
if len(k.Secp256K1) != secp256k1.PubKeySize {
54+
return nil, fmt.Errorf("invalid size for PubKeyEd25519. Got %d, expected %d",
55+
len(k.Secp256K1), secp256k1.PubKeySize)
56+
}
57+
pk := make(secp256k1.PubKey, secp256k1.PubKeySize)
58+
copy(pk, k.Secp256K1)
59+
return pk, nil
4460
default:
4561
return nil, fmt.Errorf("fromproto: key type %v is not supported", k)
4662
}

0 commit comments

Comments
 (0)