-
Notifications
You must be signed in to change notification settings - Fork 80
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
dkg: generate deposit data #491
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1006053
add basic structure for deposit data
xenowits ad505ee
add functions for hash tree root
xenowits 6bc4d12
Merge branch 'main' of https://github.com/ObolNetwork/charon into xen…
xenowits cc95ae5
remove prysm
xenowits 94697fe
Merge branch 'main' of https://github.com/ObolNetwork/charon into xen…
xenowits 1ff4ba7
add tree hashing and json serialization
xenowits 68edcad
fix deposit message code
xenowits 4325cc8
add final types
xenowits ba1c580
add nolint revive to depositdata
xenowits 368d967
address review comments
xenowits 2f030c5
reordered hash fields according to specs
xenowits f74eedd
fix golangci lint error
xenowits File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
// Copyright © 2022 Obol Labs Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify it | ||
// under the terms of the GNU General Public License as published by the Free | ||
// Software Foundation, either version 3 of the License, or (at your option) | ||
// any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, but WITHOUT | ||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
// more details. | ||
// | ||
// You should have received a copy of the GNU General Public License along with | ||
// this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package deposit | ||
|
||
import ( | ||
"encoding/hex" | ||
"encoding/json" | ||
|
||
eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0" | ||
ssz "github.com/ferranbt/fastssz" | ||
|
||
"github.com/obolnetwork/charon/app/errors" | ||
) | ||
|
||
const depositAmt = 32000000000 | ||
|
||
// depositData contains all the information required for activating validators on the Ethereum Network. | ||
// Ref: https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#depositdata | ||
type depositData struct { | ||
// PubKey is the group public key for a Distributed Validator. | ||
PubKey eth2p0.BLSPubKey | ||
|
||
// Amount is the amount of Eth needed to activate a validator. | ||
Amount eth2p0.Gwei | ||
|
||
// Eth1WithdrawalAddress is the Ethereum withdrawal address. | ||
Eth1WithdrawalAddress string | ||
|
||
// DepositMessageRoot is the hash tree root of DepositMessage. | ||
DepositMessageRoot eth2p0.Root | ||
|
||
// Signature is constructed from DepositMessageRoot combined with DOMAIN_DEPOSIT. | ||
Signature eth2p0.BLSSignature | ||
|
||
// ForkVersion identifies the network/chainID. | ||
ForkVersion eth2p0.Version | ||
} | ||
|
||
func (d depositData) HashTreeRoot() ([32]byte, error) { | ||
b, err := ssz.HashWithDefaultHasher(d) | ||
if err != nil { | ||
return [32]byte{}, errors.Wrap(err, "hash deposit data") | ||
} | ||
|
||
return b, nil | ||
} | ||
|
||
func (d depositData) HashTreeRootWith(hh *ssz.Hasher) error { | ||
idx := hh.Index() | ||
|
||
// Field 0 'PubKey` | ||
hh.PutBytes(d.PubKey[:]) | ||
|
||
// Field 1 'WithdrawalCredentials | ||
creds, err := withdrawalCredsFromAddr(d.Eth1WithdrawalAddress) | ||
if err != nil { | ||
return errors.Wrap(err, "withdrawal credentials") | ||
} | ||
hh.PutBytes(creds[:]) | ||
|
||
// Field 2 'Amount' | ||
hh.PutUint64(uint64(d.Amount)) | ||
|
||
// Field 4 'Signature' | ||
hh.PutBytes(d.Signature[:]) | ||
|
||
hh.Merkleize(idx) | ||
|
||
return nil | ||
} | ||
|
||
// MarshalDepositData returns the json serialised deposit data bytes to be written to disk. | ||
func MarshalDepositData(pubkey eth2p0.BLSPubKey, msgRoot eth2p0.Root, sig eth2p0.BLSSignature, withdrawalAddr, forkVersion string) ([]byte, error) { | ||
creds, err := withdrawalCredsFromAddr(withdrawalAddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var version eth2p0.Version | ||
forkVersionBytes, err := hex.DecodeString(forkVersion) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "decode fork version") | ||
} | ||
copy(version[:], forkVersionBytes) | ||
|
||
// construct DepositData and then calculate the hash. | ||
d := depositData{ | ||
PubKey: pubkey, | ||
Amount: depositAmt, | ||
Eth1WithdrawalAddress: withdrawalAddr, | ||
DepositMessageRoot: msgRoot, | ||
Signature: sig, | ||
ForkVersion: version, | ||
} | ||
hash, err := d.HashTreeRoot() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Marshal json version of deposit data. | ||
resp, err := json.Marshal(ddJSON{ | ||
PubKey: hex.EncodeToString(pubkey[:]), | ||
Amount: uint64(d.Amount), | ||
WithdrawalCredentials: hex.EncodeToString(creds[:]), | ||
DepositDataRoot: hex.EncodeToString(hash[:]), | ||
DepositMessageRoot: hex.EncodeToString(msgRoot[:]), | ||
Signature: hex.EncodeToString(sig[:]), | ||
ForkVersion: forkVersion, | ||
NetworkName: forkVersionToNetwork(forkVersion), | ||
}) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "marshal deposit data") | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
// forkVersionToNetwork returns the name of the ethereum network corresponding to a given fork version. | ||
func forkVersionToNetwork(forkVersion string) string { | ||
switch forkVersion { | ||
case "00000000": | ||
return "mainnet" | ||
case "00001020": | ||
return "prater" | ||
case "60000069": | ||
return "kintsugi" | ||
case "70000069": | ||
return "kiln" | ||
case "00000064": | ||
return "gnosis" | ||
default: | ||
return "mainnet" | ||
} | ||
} | ||
|
||
// ddJSON is the json formatter for depositData. | ||
type ddJSON struct { | ||
PubKey string `json:"pubkey"` | ||
Amount uint64 `json:"amount"` | ||
WithdrawalCredentials string `json:"withdrawal_credentials"` | ||
DepositDataRoot string `json:"deposit_data_root"` | ||
DepositMessageRoot string `json:"deposit_message_root"` | ||
Signature string `json:"signature"` | ||
ForkVersion string `json:"fork_version"` | ||
NetworkName string `json:"network_name"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright © 2022 Obol Labs Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify it | ||
// under the terms of the GNU General Public License as published by the Free | ||
// Software Foundation, either version 3 of the License, or (at your option) | ||
// any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, but WITHOUT | ||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
// more details. | ||
// | ||
// You should have received a copy of the GNU General Public License along with | ||
// this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package deposit | ||
|
||
import ( | ||
"encoding/hex" | ||
"strings" | ||
|
||
eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0" | ||
"github.com/ethereum/go-ethereum/common" | ||
ssz "github.com/ferranbt/fastssz" | ||
|
||
"github.com/obolnetwork/charon/app/errors" | ||
"github.com/obolnetwork/charon/app/z" | ||
) | ||
|
||
var ( | ||
eth1AddressWithdrawalPrefix = byte(0x01) | ||
elevenZeroes = []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} | ||
) | ||
|
||
// depositMessage contains all the basic information necessary to activate a validator. The fields are | ||
// hashed to get the DepositMessageRoot. This root is signed and then the signature is added to DepositData. | ||
// Ref: https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#depositmessage | ||
type depositMessage struct { | ||
pubKey eth2p0.BLSPubKey | ||
|
||
// WithdrawalCredentials is the 0x01 withdrawal credentials. See spec: | ||
// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/validator.md#withdrawal-credentials | ||
withdrawalCredentials [32]byte | ||
|
||
amount eth2p0.Gwei | ||
} | ||
|
||
func (d depositMessage) HashTreeRoot() ([32]byte, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. colocate type and its methods |
||
b, err := ssz.HashWithDefaultHasher(d) | ||
if err != nil { | ||
return [32]byte{}, errors.Wrap(err, "hash deposit message") | ||
} | ||
|
||
return b, nil | ||
} | ||
|
||
func (d depositMessage) HashTreeRootWith(hh *ssz.Hasher) error { | ||
idx := hh.Index() | ||
|
||
// Field 0 'pubKey` | ||
hh.PutBytes(d.pubKey[:]) | ||
|
||
// Field 1 'withdrawalCredentials' | ||
hh.PutBytes(d.withdrawalCredentials[:]) | ||
|
||
// Field 2 'amount' | ||
hh.PutUint64(uint64(d.amount)) | ||
|
||
hh.Merkleize(idx) | ||
|
||
return nil | ||
} | ||
|
||
// MessageRoot returns the hash tree root of the deposit message. | ||
func MessageRoot(pubkey eth2p0.BLSPubKey, withdrawalAddr string) (eth2p0.Root, error) { | ||
creds, err := withdrawalCredsFromAddr(withdrawalAddr) | ||
if err != nil { | ||
return eth2p0.Root{}, err | ||
} | ||
|
||
depositMessage := depositMessage{ | ||
pubKey: pubkey, | ||
amount: depositAmt, | ||
withdrawalCredentials: creds, | ||
} | ||
|
||
root, err := depositMessage.HashTreeRoot() | ||
if err != nil { | ||
return eth2p0.Root{}, err | ||
} | ||
|
||
return root, nil | ||
} | ||
|
||
// withdrawalCredsFromAddr returns the Withdrawal Credentials corresponding to a '0x01' Ethereum withdrawal address. | ||
func withdrawalCredsFromAddr(addr string) ([32]byte, error) { | ||
// Check for validity of address. | ||
if !common.IsHexAddress(addr) { | ||
return [32]byte{}, errors.New("invalid withdrawal address", z.Str("address", addr)) | ||
} | ||
|
||
var creds []byte | ||
|
||
// Append the single byte ETH1_ADDRESS_WITHDRAWAL_PREFIX as prefix. | ||
creds = append(creds, eth1AddressWithdrawalPrefix) | ||
|
||
// Append 11 bytes of 0. | ||
creds = append(creds, elevenZeroes...) | ||
|
||
addr = strings.TrimPrefix(addr, "0x") | ||
addrBytes, err := hex.DecodeString(addr) | ||
if err != nil { | ||
return [32]byte{}, errors.Wrap(err, "decode address") | ||
} | ||
|
||
// Finally, append 20 bytes of ethereum address. | ||
creds = append(creds, addrBytes...) | ||
|
||
var resp [32]byte | ||
copy(resp[:], creds) | ||
|
||
return resp, nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe rather remove msgRoot, since you can calculate it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm thinking along the lines of if we have a
sig
(that's calculated onmsgRoot
), we already have amsgRoot
. So, we could have that as an input parameter.