Skip to content

Commit

Permalink
Merge pull request wemixarchive#51 from wemixarchive/dev
Browse files Browse the repository at this point in the history
gwemix: merge 0.10.3 to master
  • Loading branch information
wm-jsong1230 committed May 10, 2023
2 parents 05eb4a4 + b9dc179 commit e7f0653
Show file tree
Hide file tree
Showing 53 changed files with 2,890 additions and 16,675 deletions.
487 changes: 487 additions & 0 deletions FEEDELEGATION.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ BEGIN { print "package wemix\n"; } \
n = "Registry"; \
print "var " n "Abi = `{ \"contractName\": \"" n "\", \"abi\": " $$0 "}`"; \
} \
/^var Staking_contract/ { \
/^var StakingImp_contract/ { \
sub("^var[^(]*\\(","",$$0); sub("\\);$$","",$$0); \
n = "Staking"; \
print "var " n "Abi = `{ \"contractName\": \"" n "\", \"abi\": " $$0 "}`"; \
Expand Down
3 changes: 3 additions & 0 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,9 @@ func (m callMsg) Value() *big.Int { return m.CallMsg.Value }
func (m callMsg) Data() []byte { return m.CallMsg.Data }
func (m callMsg) AccessList() types.AccessList { return m.CallMsg.AccessList }

// fee delegation
func (m callMsg) FeePayer() *common.Address { return m.CallMsg.FeePayer }

// filterBackend implements filters.Backend to support filtering for logs without
// taking bloom-bits acceleration structures into account.
type filterBackend struct {
Expand Down
12 changes: 12 additions & 0 deletions accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,18 @@ type Wallet interface {

// SignTxWithPassphrase is identical to SignTx, but also takes a password
SignTxWithPassphrase(account Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)

// Get pk from sk based on ed25519
EdPubKey(account Account) ([]byte, error)

// Get pk from sk based on ed25519 with account password
EdPubKeyWithPassphrase(account Account, passphrase string) ([]byte, error)

// Get prove
Prove(account Account, message []byte) ([]byte, error)

// Get prove with account password
ProveWithPassphrase(account Account, passphrase string, message []byte) ([]byte, error)
}

// Backend is a "wallet provider" that may contain a batch of accounts they can
Expand Down
16 changes: 16 additions & 0 deletions accounts/external/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,22 @@ func (api *ExternalSigner) SignDataWithPassphrase(account accounts.Account, pass
return nil, fmt.Errorf("password-operations not supported on external signers")
}

func (api *ExternalSigner) EdPubKey(account accounts.Account) ([]byte, error) {
return nil, fmt.Errorf("vrf-operations not supported on external signers yet") // TODO (lukepark327)
}

func (api *ExternalSigner) EdPubKeyWithPassphrase(account accounts.Account, passphrase string) ([]byte, error) {
return nil, fmt.Errorf("vrf-operations not supported on external signers yet") // TODO (lukepark327)
}

func (api *ExternalSigner) Prove(account accounts.Account, message []byte) ([]byte, error) {
return nil, fmt.Errorf("vrf-operations not supported on external signers yet") // TODO (lukepark327)
}

func (api *ExternalSigner) ProveWithPassphrase(account accounts.Account, passphrase string, message []byte) ([]byte, error) {
return nil, fmt.Errorf("vrf-operations not supported on external signers yet") // TODO (lukepark327)
}

func (api *ExternalSigner) listAccounts() ([]common.Address, error) {
var res []common.Address
if err := api.client.Call(&res, "account_list"); err != nil {
Expand Down
94 changes: 94 additions & 0 deletions accounts/keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package keystore

import (
"crypto/ecdsa"
"crypto/ed25519"
crand "crypto/rand"
"errors"
"math/big"
Expand All @@ -36,13 +37,15 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/vrf"
"github.com/ethereum/go-ethereum/event"
)

var (
ErrLocked = accounts.NewAuthNeededError("password or unlock")
ErrNoMatch = errors.New("no key for given address or file")
ErrDecrypt = errors.New("could not decrypt key with given password")
ErrVrf = errors.New("errors on VRF functions") // TODO (lukepark327): more details

// ErrAccountAlreadyExists is returned if an account attempted to import is
// already present in the keystore.
Expand Down Expand Up @@ -283,6 +286,11 @@ func (ks *KeyStore) SignTx(a accounts.Account, tx *types.Transaction, chainID *b
if !found {
return nil, ErrLocked
}
// fee delegation
if tx.Type() == types.FeeDelegateDynamicFeeTxType {
signer := types.NewFeeDelegateSigner(chainID)
return types.SignTx(tx, signer, unlockedKey.PrivateKey)
}
// Depending on the presence of the chain ID, sign with 2718 or homestead
signer := types.LatestSignerForChainID(chainID)
return types.SignTx(tx, signer, unlockedKey.PrivateKey)
Expand All @@ -308,6 +316,12 @@ func (ks *KeyStore) SignTxWithPassphrase(a accounts.Account, passphrase string,
return nil, err
}
defer zeroKey(key.PrivateKey)
// fee delegation
if tx.Type() == types.FeeDelegateDynamicFeeTxType {
signer := types.NewFeeDelegateSigner(chainID)
return types.SignTx(tx, signer, key.PrivateKey)
}

// Depending on the presence of the chain ID, sign with or without replay protection.
signer := types.LatestSignerForChainID(chainID)
return types.SignTx(tx, signer, key.PrivateKey)
Expand Down Expand Up @@ -505,3 +519,83 @@ func zeroKey(k *ecdsa.PrivateKey) {
b[i] = 0
}
}

func (ks *KeyStore) EdPubKey(a accounts.Account) ([]byte, error) {
// Look up the key to sign with and abort if it cannot be found
ks.mu.RLock()
defer ks.mu.RUnlock()

unlockedKey, found := ks.unlocked[a.Address]
if !found {
return nil, ErrLocked
}

return edPubKeyFromPrivateKey(unlockedKey.PrivateKey)
}

func (ks *KeyStore) EdPubKeyWithPassphrase(a accounts.Account, passphrase string) ([]byte, error) {
_, key, err := ks.getDecryptedKey(a, passphrase)
if err != nil {
return nil, err
}
defer zeroKey(key.PrivateKey)

return edPubKeyFromPrivateKey(key.PrivateKey)
}

func edPubKeyFromPrivateKey(k *ecdsa.PrivateKey) ([]byte, error) {
// get private key
seed := k.D.Bytes()
sk := ed25519.NewKeyFromSeed(seed)
pk := sk.Public()

xx, ok := pk.(ed25519.PublicKey)
if !ok {
return nil, ErrVrf
}
return xx, nil
}

func (ks *KeyStore) Prove(a accounts.Account, m []byte) ([]byte, error) {
// Look up the key to sign with and abort if it cannot be found
ks.mu.RLock()
defer ks.mu.RUnlock()

unlockedKey, found := ks.unlocked[a.Address]
if !found {
return nil, ErrLocked
}

pi, _, err := prove(unlockedKey.PrivateKey, m)
if err != nil {
return nil, err
}
return pi, err
}

func (ks *KeyStore) ProveWithPassphrase(a accounts.Account, passphrase string, m []byte) ([]byte, error) {
_, key, err := ks.getDecryptedKey(a, passphrase)
if err != nil {
return nil, err
}
defer zeroKey(key.PrivateKey)

pi, _, err := prove(key.PrivateKey, m)
if err != nil {
return nil, err
}
return pi, err
}

func prove(k *ecdsa.PrivateKey, msg []byte) ([]byte, []byte, error) {
// get private key
seed := k.D.Bytes()
sk := ed25519.NewKeyFromSeed(seed)
pk := sk.Public()

xx, ok := pk.(ed25519.PublicKey)
if !ok {
return nil, nil, ErrVrf
}
return vrf.Prove(xx, sk, msg[:])
}
36 changes: 36 additions & 0 deletions accounts/keystore/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,39 @@ func (w *keystoreWallet) SignTxWithPassphrase(account accounts.Account, passphra
// Account seems valid, request the keystore to sign
return w.keystore.SignTxWithPassphrase(account, passphrase, tx, chainID)
}

func (w *keystoreWallet) EdPubKey(account accounts.Account) ([]byte, error) {
// Make sure the requested account is contained within
if !w.Contains(account) {
return nil, accounts.ErrUnknownAccount
}
// Account seems valid, request the keystore to get EdPubKey
return w.keystore.EdPubKey(account)
}

func (w *keystoreWallet) EdPubKeyWithPassphrase(account accounts.Account, passphrase string) ([]byte, error) {
// Make sure the requested account is contained within
if !w.Contains(account) {
return nil, accounts.ErrUnknownAccount
}
// Account seems valid, request the keystore to get EdPubKey
return w.keystore.EdPubKeyWithPassphrase(account, passphrase)
}

func (w *keystoreWallet) Prove(account accounts.Account, message []byte) ([]byte, error) {
// Make sure the requested account is contained within
if !w.Contains(account) {
return nil, accounts.ErrUnknownAccount
}
// Account seems valid, request the keystore to prove
return w.keystore.Prove(account, message)
}

func (w *keystoreWallet) ProveWithPassphrase(account accounts.Account, passphrase string, message []byte) ([]byte, error) {
// Make sure the requested account is contained within
if !w.Contains(account) {
return nil, accounts.ErrUnknownAccount
}
// Account seems valid, request the keystore to prove
return w.keystore.ProveWithPassphrase(account, passphrase, message)
}
28 changes: 28 additions & 0 deletions accounts/scwallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,18 @@ func (w *Wallet) signHash(account accounts.Account, hash []byte) ([]byte, error)
// the needed details via SignTxWithPassphrase, or by other means (e.g. unlock
// the account in a keystore).
func (w *Wallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {

// fee delegation
if tx.Type() == types.FeeDelegateDynamicFeeTxType {
signer := types.NewFeeDelegateSigner(chainID)
hash := signer.Hash(tx)
sig, err := w.signHash(account, hash[:])
if err != nil {
return nil, err
}
return tx.WithSignature(signer, sig)
}

signer := types.LatestSignerForChainID(chainID)
hash := signer.Hash(tx)
sig, err := w.signHash(account, hash[:])
Expand Down Expand Up @@ -788,6 +800,22 @@ func (w *Wallet) findAccountPath(account accounts.Account) (accounts.DerivationP
return accounts.ParseDerivationPath(parts[1])
}

func (w *Wallet) EdPubKey(account accounts.Account) ([]byte, error) {
return nil, accounts.ErrNotSupported // TODO
}

func (w *Wallet) EdPubKeyWithPassphrase(account accounts.Account, passphrase string) ([]byte, error) {
return nil, accounts.ErrNotSupported // TODO
}

func (w *Wallet) Prove(account accounts.Account, message []byte) ([]byte, error) {
return nil, accounts.ErrNotSupported // TODO
}

func (w *Wallet) ProveWithPassphrase(account accounts.Account, passphrase string, message []byte) ([]byte, error) {
return nil, accounts.ErrNotSupported // TODO
}

// Session represents a secured communication session with the wallet.
type Session struct {
Wallet *Wallet // A handle to the wallet that opened the session
Expand Down
16 changes: 16 additions & 0 deletions accounts/usbwallet/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,22 @@ func (w *ledgerDriver) SignTypedMessage(path accounts.DerivationPath, domainHash
return w.ledgerSignTypedMessage(path, domainHash, messageHash)
}

func (w *ledgerDriver) EdPubKey(a accounts.Account) ([]byte, error) {
return nil, accounts.ErrNotSupported // TODO
}

func (w *ledgerDriver) EdPubKeyWithPassphrase(a accounts.Account, passphrase string) ([]byte, error) {
return nil, accounts.ErrNotSupported // TODO
}

func (w *ledgerDriver) Prove(a accounts.Account, m []byte) ([]byte, error) {
return nil, accounts.ErrNotSupported // TODO
}

func (w *ledgerDriver) ProveWithPassphrase(a accounts.Account, passphrase string, m []byte) ([]byte, error) {
return nil, accounts.ErrNotSupported // TODO
}

// ledgerVersion retrieves the current version of the Ethereum wallet app running
// on the Ledger wallet.
//
Expand Down
16 changes: 16 additions & 0 deletions accounts/usbwallet/trezor.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,22 @@ func (w *trezorDriver) SignTypedMessage(path accounts.DerivationPath, domainHash
return nil, accounts.ErrNotSupported
}

func (w *trezorDriver) EdPubKey(a accounts.Account) ([]byte, error) {
return nil, accounts.ErrNotSupported // TODO
}

func (w *trezorDriver) EdPubKeyWithPassphrase(a accounts.Account, passphrase string) ([]byte, error) {
return nil, accounts.ErrNotSupported // TODO
}

func (w *trezorDriver) Prove(a accounts.Account, m []byte) ([]byte, error) {
return nil, accounts.ErrNotSupported // TODO
}

func (w *trezorDriver) ProveWithPassphrase(a accounts.Account, passphrase string, m []byte) ([]byte, error) {
return nil, accounts.ErrNotSupported // TODO
}

// trezorDerive sends a derivation request to the Trezor device and returns the
// Ethereum address located on that path.
func (w *trezorDriver) trezorDerive(derivationPath []uint32) (common.Address, error) {
Expand Down
24 changes: 24 additions & 0 deletions accounts/usbwallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ type driver interface {
SignTx(path accounts.DerivationPath, tx *types.Transaction, chainID *big.Int) (common.Address, *types.Transaction, error)

SignTypedMessage(path accounts.DerivationPath, messageHash []byte, domainHash []byte) ([]byte, error)

EdPubKey(account accounts.Account) ([]byte, error)

EdPubKeyWithPassphrase(account accounts.Account, passphrase string) ([]byte, error)

Prove(account accounts.Account, message []byte) ([]byte, error)

ProveWithPassphrase(account accounts.Account, passphrase string, message []byte) ([]byte, error)
}

// wallet represents the common functionality shared by all USB hardware
Expand Down Expand Up @@ -638,3 +646,19 @@ func (w *wallet) SignTextWithPassphrase(account accounts.Account, passphrase str
func (w *wallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
return w.SignTx(account, tx, chainID)
}

func (w *wallet) EdPubKey(account accounts.Account) ([]byte, error) {
return nil, accounts.ErrNotSupported // TODO
}

func (w *wallet) EdPubKeyWithPassphrase(account accounts.Account, passphrase string) ([]byte, error) {
return nil, accounts.ErrNotSupported // TODO
}

func (w *wallet) Prove(account accounts.Account, message []byte) ([]byte, error) {
return nil, accounts.ErrNotSupported // TODO
}

func (w *wallet) ProveWithPassphrase(account accounts.Account, passphrase string, message []byte) ([]byte, error) {
return nil, accounts.ErrNotSupported // TODO
}
2 changes: 2 additions & 0 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const (
HashLength = 32
// AddressLength is the expected length of the address
AddressLength = 20

EdPubKeyLength = 32
)

var (
Expand Down
12 changes: 8 additions & 4 deletions consensus/misc/eip1559.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,19 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
var baseFeeDelta *big.Int
if wemixminer.IsPoW() {
baseFeeDelta = x.Div(y, baseFeeChangeDenominator)
return math.BigMax(
x.Sub(parent.BaseFee, baseFeeDelta),
common.Big1,
)
} else {
baseFeeDelta = x.Div(y.Mul(y, baseFeeChangeRate), big.NewInt(100))
if baseFeeDelta.Cmp(common.Big0) == 0 && parent.BaseFee.Cmp(common.Big1) > 0 {
baseFeeDelta.SetUint64(1)
}
return math.BigMin(math.BigMax(
x.Sub(parent.BaseFee, baseFeeDelta),
common.Big1,
), maxBaseFee)
}
return math.BigMax(
x.Sub(parent.BaseFee, baseFeeDelta),
common.Big1,
)
}
}

0 comments on commit e7f0653

Please sign in to comment.