Skip to content

Commit

Permalink
Eth signing on P-chain
Browse files Browse the repository at this point in the history
  • Loading branch information
mboben committed Nov 11, 2024
1 parent c759e41 commit 5d30b66
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 26 deletions.
1 change: 0 additions & 1 deletion genesis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ func (c Config) Unparse() (UnparsedConfig, error) {
}

func (c *Config) InitialSupply() (uint64, error) {
// SGB-MERGE
// For songbird, coston and local networks, the initial supply is 1
if c.NetworkID == constants.SongbirdID || c.NetworkID == constants.CostonID || c.NetworkID == constants.LocalID {
return 1, nil
Expand Down
2 changes: 0 additions & 2 deletions snow/validators/custom.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package validators

// SGB-MERGE

import (
"errors"
"fmt"
Expand Down
28 changes: 15 additions & 13 deletions version/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,26 @@ var (

ApricotPhase6Times = map[uint32]time.Time{
constants.MainnetID: time.Date(2022, time.September, 6, 20, 0, 0, 0, time.UTC),
constants.FlareID: time.Date(2024, time.June, 1, 0, 0, 0, 0, time.UTC),
constants.CostwoID: time.Date(2024, time.June, 1, 0, 0, 0, 0, time.UTC),
constants.StagingID: time.Date(2024, time.June, 1, 0, 0, 0, 0, time.UTC),
constants.LocalFlareID: time.Date(2024, time.June, 1, 0, 0, 0, 0, time.UTC),
constants.CostonID: time.Date(2024, time.June, 1, 0, 0, 0, 0, time.UTC),
constants.SongbirdID: time.Date(2024, time.June, 1, 0, 0, 0, 0, time.UTC),
constants.FlareID: time.Date(2024, time.December, 17, 13, 0, 0, 0, time.UTC),
constants.CostwoID: time.Date(2024, time.November, 26, 13, 0, 0, 0, time.UTC),
constants.StagingID: time.Date(2024, time.November, 5, 13, 0, 0, 0, time.UTC),
constants.LocalFlareID: time.Date(2024, time.November, 5, 13, 0, 0, 0, time.UTC),
constants.CostonID: time.Date(2025, time.January, 7, 13, 0, 0, 0, time.UTC),
constants.SongbirdID: time.Date(2025, time.January, 28, 13, 0, 0, 0, time.UTC),
constants.LocalID: time.Date(2024, time.November, 5, 13, 0, 0, 0, time.UTC),
}
ApricotPhase6DefaultTime = time.Date(2020, time.December, 5, 5, 0, 0, 0, time.UTC)

// FIXME: update this before release
BanffTimes = map[uint32]time.Time{
constants.MainnetID: time.Date(2022, time.October, 18, 16, 0, 0, 0, time.UTC),
constants.FlareID: time.Date(10000, time.December, 1, 0, 0, 0, 0, time.UTC),
constants.CostwoID: time.Date(10000, time.December, 1, 0, 0, 0, 0, time.UTC),
constants.StagingID: time.Date(10000, time.December, 1, 0, 0, 0, 0, time.UTC),
constants.LocalFlareID: time.Date(10000, time.December, 1, 0, 0, 0, 0, time.UTC),
constants.CostonID: time.Date(10000, time.December, 1, 0, 0, 0, 0, time.UTC),
constants.SongbirdID: time.Date(10000, time.December, 1, 0, 0, 0, 0, time.UTC),
constants.LocalID: time.Date(10000, time.December, 1, 0, 0, 0, 0, time.UTC),
constants.FlareID: time.Date(2024, time.December, 17, 15, 0, 0, 0, time.UTC),
constants.CostwoID: time.Date(2024, time.November, 26, 15, 0, 0, 0, time.UTC),
constants.StagingID: time.Date(2024, time.November, 5, 15, 0, 0, 0, time.UTC),
constants.LocalFlareID: time.Date(2024, time.May, 29, 9, 15, 0, 0, time.UTC),
constants.CostonID: time.Date(2025, time.January, 7, 15, 0, 0, 0, time.UTC),
constants.SongbirdID: time.Date(2025, time.January, 28, 15, 0, 0, 0, time.UTC),
constants.LocalID: time.Date(2024, time.November, 5, 15, 0, 0, 0, time.UTC),
}
BanffDefaultTime = time.Date(2020, time.December, 5, 5, 0, 0, 0, time.UTC)

Expand All @@ -137,6 +138,7 @@ var (
constants.LocalFlareID: time.Date(10000, time.December, 1, 0, 0, 0, 0, time.UTC),
constants.CostonID: time.Date(10000, time.December, 1, 0, 0, 0, 0, time.UTC),
constants.SongbirdID: time.Date(10000, time.December, 1, 0, 0, 0, 0, time.UTC),
constants.LocalID: time.Date(10000, time.December, 1, 0, 0, 0, 0, time.UTC),
}
XChainMigrationDefaultTime = time.Date(2020, time.December, 5, 5, 0, 0, 0, time.UTC)
)
Expand Down
1 change: 1 addition & 0 deletions vms/avm/txs/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ type fxVM struct {
func (vm *fxVM) Clock() *mockable.Clock { return vm.clock }
func (vm *fxVM) CodecRegistry() codec.Registry { return vm.codecRegistry }
func (vm *fxVM) Logger() logging.Logger { return vm.log }
func (vm *fxVM) EthVerificationEnabled() bool { return false }
1 change: 1 addition & 0 deletions vms/platformvm/blocks/builder/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ type fxVMInt struct {
func (fvi *fxVMInt) CodecRegistry() codec.Registry { return fvi.registry }
func (fvi *fxVMInt) Clock() *mockable.Clock { return fvi.clk }
func (fvi *fxVMInt) Logger() logging.Logger { return fvi.log }
func (fvi *fxVMInt) EthVerificationEnabled() bool { return false }

func defaultFx(clk *mockable.Clock, log logging.Logger, isBootstrapped bool) fx.Fx {
fxVMInt := &fxVMInt{
Expand Down
1 change: 1 addition & 0 deletions vms/platformvm/blocks/executor/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ type fxVMInt struct {
func (fvi *fxVMInt) CodecRegistry() codec.Registry { return fvi.registry }
func (fvi *fxVMInt) Clock() *mockable.Clock { return fvi.clk }
func (fvi *fxVMInt) Logger() logging.Logger { return fvi.log }
func (fvi *fxVMInt) EthVerificationEnabled() bool { return false }

func defaultFx(clk *mockable.Clock, log logging.Logger, isBootstrapped bool) fx.Fx {
fxVMInt := &fxVMInt{
Expand Down
1 change: 0 additions & 1 deletion vms/platformvm/state/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ type Diff interface {
}

type diff struct {
// SGB-MERGE
networkID uint32

parentID ids.ID
Expand Down
1 change: 0 additions & 1 deletion vms/platformvm/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ type Chain interface {
UTXOGetter
UTXODeleter

// SGB-MERGE
GetNetworkID() uint32

GetTimestamp() time.Time
Expand Down
1 change: 1 addition & 0 deletions vms/platformvm/txs/executor/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ type fxVMInt struct {
func (fvi *fxVMInt) CodecRegistry() codec.Registry { return fvi.registry }
func (fvi *fxVMInt) Clock() *mockable.Clock { return fvi.clk }
func (fvi *fxVMInt) Logger() logging.Logger { return fvi.log }
func (fvi *fxVMInt) EthVerificationEnabled() bool { return false }

func defaultFx(clk *mockable.Clock, log logging.Logger, isBootstrapped bool) fx.Fx {
fxVMInt := &fxVMInt{
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/txs/executor/inflation_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func getSongbirdInflationSettings(currentTimestamp time.Time, config *config.Con
MaxStakeDuration: 365 * 24 * time.Hour,
MinFutureStartTimeOffset: MaxFutureStartTime,
MaxValidatorWeightFactor: 15,
MinStakeStartTime: time.Date(2024, time.September, 3, 0, 0, 0, 0, time.UTC),
MinStakeStartTime: time.Date(2024, time.November, 19, 12, 0, 0, 0, time.UTC),
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions vms/platformvm/txs/executor/proposal_tx_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ var (
errAdvanceTimeTxIssuedAfterBanff = errors.New("AdvanceTimeTx issued after Banff")
)

var (
songbirdLatestStakingTime = time.Date(2024, time.December, 31, 0, 0, 0, 0, time.UTC)
)

type ProposalTxExecutor struct {
// inputs, to be filled before visitor methods are called
*Backend
Expand Down Expand Up @@ -572,6 +576,10 @@ func GetNextStakerChangeTime(state state.Chain) (time.Time, error) {
case hasPendingStaker:
return pendingStakerIterator.Value().NextTime, nil
default:
// Due to no initial stakers in genesis for Songbird networks
if state.GetNetworkID() == constants.SongbirdID || state.GetNetworkID() == constants.CostonID || state.GetNetworkID() == constants.LocalID {
return songbirdLatestStakingTime, nil
}
return time.Time{}, database.ErrNotFound
}
}
Expand Down
7 changes: 6 additions & 1 deletion vms/platformvm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ func (vm *VM) updateValidators() error {
return err
}

// SGB-MERGE: This are new lines of code
// Songbird (coston, local) has a fixed set of validators in genesis
for _, v := range validators.DefaultValidatorList() {
err := primaryValidators.AddWeight(v.ID(), v.Weight())
if err != nil {
Expand Down Expand Up @@ -632,3 +632,8 @@ func (vm *VM) getPercentConnected(subnetID ids.ID) (float64, error) {
}
return float64(connectedStake) / float64(vdrSetWeight), nil
}

func (vm *VM) EthVerificationEnabled() bool {
time := vm.state.GetTimestamp()
return !time.Before(vm.Config.BanffTime)
}
42 changes: 36 additions & 6 deletions vms/secp256k1fx/fx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
package secp256k1fx

import (
"encoding/hex"
"errors"
"fmt"

"github.com/ava-labs/avalanchego/cache"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/crypto"
"github.com/ava-labs/avalanchego/utils/hashing"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/vms/components/verify"
"github.com/ava-labs/coreth/accounts"
)

const (
Expand Down Expand Up @@ -184,23 +187,42 @@ func (fx *Fx) VerifyCredentials(utx UnsignedTx, in *Input, cred *Credential, out
}

txHash := hashing.ComputeHash256(utx.Bytes())
txHashStr := hex.EncodeToString(txHash)
txHashEth := accounts.TextHash([]byte(txHashStr))
isEthVerificationEnabled := fx.VM.EthVerificationEnabled()
for i, index := range in.SigIndices {
// Make sure the input references an address that exists
if index >= uint32(len(out.Addrs)) {
return errInputOutputIndexOutOfBounds
}
// Make sure each signature in the signature list is from an owner of
// the output being consumed
sig := cred.Sigs[i]
pk, err := fx.SECPFactory.RecoverHashPublicKey(txHash, sig[:])
sig := cred.Sigs[i][:]
expectedAddress := out.Addrs[index]

// Try to recover the address from the signature of the transaction hash without a prefix
// (Standard Avalanche approach, but unsupported/deprecated by most signing tools)
recoveredAddress, err := fx.recoverAddress(txHash, sig)
if err != nil {
return err
}
if expectedAddress := out.Addrs[index]; expectedAddress != pk.Address() {
return fmt.Errorf("expected signature from %s but got from %s",
expectedAddress,
pk.Address())
if recoveredAddress == expectedAddress {
continue
}

// Try to recover the address from the signature of the prefixed message hash
// (with the standard Ethereum prefix, see accounts.TextHash)
if isEthVerificationEnabled {
recoveredAddress, err = fx.recoverAddress(txHashEth, sig)
if err != nil {
return err
}
if recoveredAddress == expectedAddress {
continue
}
}

return fmt.Errorf("expected signature from %s", expectedAddress)
}

return nil
Expand All @@ -221,3 +243,11 @@ func (fx *Fx) CreateOutput(amount uint64, ownerIntf interface{}) (interface{}, e
OutputOwners: *owner,
}, nil
}

func (fx *Fx) recoverAddress(txHash []byte, sig []byte) (ids.ShortID, error) {
pk, err := fx.SECPFactory.RecoverHashPublicKey(txHash, sig)
if err != nil {
return ids.ShortEmpty, err
}
return pk.Address(), nil
}
2 changes: 2 additions & 0 deletions vms/secp256k1fx/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type VM interface {
CodecRegistry() codec.Registry
Clock() *mockable.Clock
Logger() logging.Logger
EthVerificationEnabled() bool
}

var _ VM = &TestVM{}
Expand All @@ -28,3 +29,4 @@ type TestVM struct {
func (vm *TestVM) Clock() *mockable.Clock { return &vm.CLK }
func (vm *TestVM) CodecRegistry() codec.Registry { return vm.Codec }
func (vm *TestVM) Logger() logging.Logger { return vm.Log }
func (vm *TestVM) EthVerificationEnabled() bool { return false }

0 comments on commit 5d30b66

Please sign in to comment.