Skip to content

Commit e7f0653

Browse files
authored
Merge pull request #51 from wemixarchive/dev
gwemix: merge 0.10.3 to master
2 parents 05eb4a4 + b9dc179 commit e7f0653

53 files changed

Lines changed: 2890 additions & 16675 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

FEEDELEGATION.md

Lines changed: 487 additions & 0 deletions
Large diffs are not rendered by default.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ BEGIN { print "package wemix\n"; } \
159159
n = "Registry"; \
160160
print "var " n "Abi = `{ \"contractName\": \"" n "\", \"abi\": " $$0 "}`"; \
161161
} \
162-
/^var Staking_contract/ { \
162+
/^var StakingImp_contract/ { \
163163
sub("^var[^(]*\\(","",$$0); sub("\\);$$","",$$0); \
164164
n = "Staking"; \
165165
print "var " n "Abi = `{ \"contractName\": \"" n "\", \"abi\": " $$0 "}`"; \

accounts/abi/bind/backends/simulated.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,9 @@ func (m callMsg) Value() *big.Int { return m.CallMsg.Value }
813813
func (m callMsg) Data() []byte { return m.CallMsg.Data }
814814
func (m callMsg) AccessList() types.AccessList { return m.CallMsg.AccessList }
815815

816+
// fee delegation
817+
func (m callMsg) FeePayer() *common.Address { return m.CallMsg.FeePayer }
818+
816819
// filterBackend implements filters.Backend to support filtering for logs without
817820
// taking bloom-bits acceleration structures into account.
818821
type filterBackend struct {

accounts/accounts.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,18 @@ type Wallet interface {
151151

152152
// SignTxWithPassphrase is identical to SignTx, but also takes a password
153153
SignTxWithPassphrase(account Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
154+
155+
// Get pk from sk based on ed25519
156+
EdPubKey(account Account) ([]byte, error)
157+
158+
// Get pk from sk based on ed25519 with account password
159+
EdPubKeyWithPassphrase(account Account, passphrase string) ([]byte, error)
160+
161+
// Get prove
162+
Prove(account Account, message []byte) ([]byte, error)
163+
164+
// Get prove with account password
165+
ProveWithPassphrase(account Account, passphrase string, message []byte) ([]byte, error)
154166
}
155167

156168
// Backend is a "wallet provider" that may contain a batch of accounts they can

accounts/external/backend.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,22 @@ func (api *ExternalSigner) SignDataWithPassphrase(account accounts.Account, pass
256256
return nil, fmt.Errorf("password-operations not supported on external signers")
257257
}
258258

259+
func (api *ExternalSigner) EdPubKey(account accounts.Account) ([]byte, error) {
260+
return nil, fmt.Errorf("vrf-operations not supported on external signers yet") // TODO (lukepark327)
261+
}
262+
263+
func (api *ExternalSigner) EdPubKeyWithPassphrase(account accounts.Account, passphrase string) ([]byte, error) {
264+
return nil, fmt.Errorf("vrf-operations not supported on external signers yet") // TODO (lukepark327)
265+
}
266+
267+
func (api *ExternalSigner) Prove(account accounts.Account, message []byte) ([]byte, error) {
268+
return nil, fmt.Errorf("vrf-operations not supported on external signers yet") // TODO (lukepark327)
269+
}
270+
271+
func (api *ExternalSigner) ProveWithPassphrase(account accounts.Account, passphrase string, message []byte) ([]byte, error) {
272+
return nil, fmt.Errorf("vrf-operations not supported on external signers yet") // TODO (lukepark327)
273+
}
274+
259275
func (api *ExternalSigner) listAccounts() ([]common.Address, error) {
260276
var res []common.Address
261277
if err := api.client.Call(&res, "account_list"); err != nil {

accounts/keystore/keystore.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package keystore
2222

2323
import (
2424
"crypto/ecdsa"
25+
"crypto/ed25519"
2526
crand "crypto/rand"
2627
"errors"
2728
"math/big"
@@ -36,13 +37,15 @@ import (
3637
"github.com/ethereum/go-ethereum/common"
3738
"github.com/ethereum/go-ethereum/core/types"
3839
"github.com/ethereum/go-ethereum/crypto"
40+
"github.com/ethereum/go-ethereum/crypto/vrf"
3941
"github.com/ethereum/go-ethereum/event"
4042
)
4143

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

4750
// ErrAccountAlreadyExists is returned if an account attempted to import is
4851
// already present in the keystore.
@@ -283,6 +286,11 @@ func (ks *KeyStore) SignTx(a accounts.Account, tx *types.Transaction, chainID *b
283286
if !found {
284287
return nil, ErrLocked
285288
}
289+
// fee delegation
290+
if tx.Type() == types.FeeDelegateDynamicFeeTxType {
291+
signer := types.NewFeeDelegateSigner(chainID)
292+
return types.SignTx(tx, signer, unlockedKey.PrivateKey)
293+
}
286294
// Depending on the presence of the chain ID, sign with 2718 or homestead
287295
signer := types.LatestSignerForChainID(chainID)
288296
return types.SignTx(tx, signer, unlockedKey.PrivateKey)
@@ -308,6 +316,12 @@ func (ks *KeyStore) SignTxWithPassphrase(a accounts.Account, passphrase string,
308316
return nil, err
309317
}
310318
defer zeroKey(key.PrivateKey)
319+
// fee delegation
320+
if tx.Type() == types.FeeDelegateDynamicFeeTxType {
321+
signer := types.NewFeeDelegateSigner(chainID)
322+
return types.SignTx(tx, signer, key.PrivateKey)
323+
}
324+
311325
// Depending on the presence of the chain ID, sign with or without replay protection.
312326
signer := types.LatestSignerForChainID(chainID)
313327
return types.SignTx(tx, signer, key.PrivateKey)
@@ -505,3 +519,83 @@ func zeroKey(k *ecdsa.PrivateKey) {
505519
b[i] = 0
506520
}
507521
}
522+
523+
func (ks *KeyStore) EdPubKey(a accounts.Account) ([]byte, error) {
524+
// Look up the key to sign with and abort if it cannot be found
525+
ks.mu.RLock()
526+
defer ks.mu.RUnlock()
527+
528+
unlockedKey, found := ks.unlocked[a.Address]
529+
if !found {
530+
return nil, ErrLocked
531+
}
532+
533+
return edPubKeyFromPrivateKey(unlockedKey.PrivateKey)
534+
}
535+
536+
func (ks *KeyStore) EdPubKeyWithPassphrase(a accounts.Account, passphrase string) ([]byte, error) {
537+
_, key, err := ks.getDecryptedKey(a, passphrase)
538+
if err != nil {
539+
return nil, err
540+
}
541+
defer zeroKey(key.PrivateKey)
542+
543+
return edPubKeyFromPrivateKey(key.PrivateKey)
544+
}
545+
546+
func edPubKeyFromPrivateKey(k *ecdsa.PrivateKey) ([]byte, error) {
547+
// get private key
548+
seed := k.D.Bytes()
549+
sk := ed25519.NewKeyFromSeed(seed)
550+
pk := sk.Public()
551+
552+
xx, ok := pk.(ed25519.PublicKey)
553+
if !ok {
554+
return nil, ErrVrf
555+
}
556+
return xx, nil
557+
}
558+
559+
func (ks *KeyStore) Prove(a accounts.Account, m []byte) ([]byte, error) {
560+
// Look up the key to sign with and abort if it cannot be found
561+
ks.mu.RLock()
562+
defer ks.mu.RUnlock()
563+
564+
unlockedKey, found := ks.unlocked[a.Address]
565+
if !found {
566+
return nil, ErrLocked
567+
}
568+
569+
pi, _, err := prove(unlockedKey.PrivateKey, m)
570+
if err != nil {
571+
return nil, err
572+
}
573+
return pi, err
574+
}
575+
576+
func (ks *KeyStore) ProveWithPassphrase(a accounts.Account, passphrase string, m []byte) ([]byte, error) {
577+
_, key, err := ks.getDecryptedKey(a, passphrase)
578+
if err != nil {
579+
return nil, err
580+
}
581+
defer zeroKey(key.PrivateKey)
582+
583+
pi, _, err := prove(key.PrivateKey, m)
584+
if err != nil {
585+
return nil, err
586+
}
587+
return pi, err
588+
}
589+
590+
func prove(k *ecdsa.PrivateKey, msg []byte) ([]byte, []byte, error) {
591+
// get private key
592+
seed := k.D.Bytes()
593+
sk := ed25519.NewKeyFromSeed(seed)
594+
pk := sk.Public()
595+
596+
xx, ok := pk.(ed25519.PublicKey)
597+
if !ok {
598+
return nil, nil, ErrVrf
599+
}
600+
return vrf.Prove(xx, sk, msg[:])
601+
}

accounts/keystore/wallet.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,39 @@ func (w *keystoreWallet) SignTxWithPassphrase(account accounts.Account, passphra
148148
// Account seems valid, request the keystore to sign
149149
return w.keystore.SignTxWithPassphrase(account, passphrase, tx, chainID)
150150
}
151+
152+
func (w *keystoreWallet) EdPubKey(account accounts.Account) ([]byte, error) {
153+
// Make sure the requested account is contained within
154+
if !w.Contains(account) {
155+
return nil, accounts.ErrUnknownAccount
156+
}
157+
// Account seems valid, request the keystore to get EdPubKey
158+
return w.keystore.EdPubKey(account)
159+
}
160+
161+
func (w *keystoreWallet) EdPubKeyWithPassphrase(account accounts.Account, passphrase string) ([]byte, error) {
162+
// Make sure the requested account is contained within
163+
if !w.Contains(account) {
164+
return nil, accounts.ErrUnknownAccount
165+
}
166+
// Account seems valid, request the keystore to get EdPubKey
167+
return w.keystore.EdPubKeyWithPassphrase(account, passphrase)
168+
}
169+
170+
func (w *keystoreWallet) Prove(account accounts.Account, message []byte) ([]byte, error) {
171+
// Make sure the requested account is contained within
172+
if !w.Contains(account) {
173+
return nil, accounts.ErrUnknownAccount
174+
}
175+
// Account seems valid, request the keystore to prove
176+
return w.keystore.Prove(account, message)
177+
}
178+
179+
func (w *keystoreWallet) ProveWithPassphrase(account accounts.Account, passphrase string, message []byte) ([]byte, error) {
180+
// Make sure the requested account is contained within
181+
if !w.Contains(account) {
182+
return nil, accounts.ErrUnknownAccount
183+
}
184+
// Account seems valid, request the keystore to prove
185+
return w.keystore.ProveWithPassphrase(account, passphrase, message)
186+
}

accounts/scwallet/wallet.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,18 @@ func (w *Wallet) signHash(account accounts.Account, hash []byte) ([]byte, error)
699699
// the needed details via SignTxWithPassphrase, or by other means (e.g. unlock
700700
// the account in a keystore).
701701
func (w *Wallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
702+
703+
// fee delegation
704+
if tx.Type() == types.FeeDelegateDynamicFeeTxType {
705+
signer := types.NewFeeDelegateSigner(chainID)
706+
hash := signer.Hash(tx)
707+
sig, err := w.signHash(account, hash[:])
708+
if err != nil {
709+
return nil, err
710+
}
711+
return tx.WithSignature(signer, sig)
712+
}
713+
702714
signer := types.LatestSignerForChainID(chainID)
703715
hash := signer.Hash(tx)
704716
sig, err := w.signHash(account, hash[:])
@@ -788,6 +800,22 @@ func (w *Wallet) findAccountPath(account accounts.Account) (accounts.DerivationP
788800
return accounts.ParseDerivationPath(parts[1])
789801
}
790802

803+
func (w *Wallet) EdPubKey(account accounts.Account) ([]byte, error) {
804+
return nil, accounts.ErrNotSupported // TODO
805+
}
806+
807+
func (w *Wallet) EdPubKeyWithPassphrase(account accounts.Account, passphrase string) ([]byte, error) {
808+
return nil, accounts.ErrNotSupported // TODO
809+
}
810+
811+
func (w *Wallet) Prove(account accounts.Account, message []byte) ([]byte, error) {
812+
return nil, accounts.ErrNotSupported // TODO
813+
}
814+
815+
func (w *Wallet) ProveWithPassphrase(account accounts.Account, passphrase string, message []byte) ([]byte, error) {
816+
return nil, accounts.ErrNotSupported // TODO
817+
}
818+
791819
// Session represents a secured communication session with the wallet.
792820
type Session struct {
793821
Wallet *Wallet // A handle to the wallet that opened the session

accounts/usbwallet/ledger.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,22 @@ func (w *ledgerDriver) SignTypedMessage(path accounts.DerivationPath, domainHash
190190
return w.ledgerSignTypedMessage(path, domainHash, messageHash)
191191
}
192192

193+
func (w *ledgerDriver) EdPubKey(a accounts.Account) ([]byte, error) {
194+
return nil, accounts.ErrNotSupported // TODO
195+
}
196+
197+
func (w *ledgerDriver) EdPubKeyWithPassphrase(a accounts.Account, passphrase string) ([]byte, error) {
198+
return nil, accounts.ErrNotSupported // TODO
199+
}
200+
201+
func (w *ledgerDriver) Prove(a accounts.Account, m []byte) ([]byte, error) {
202+
return nil, accounts.ErrNotSupported // TODO
203+
}
204+
205+
func (w *ledgerDriver) ProveWithPassphrase(a accounts.Account, passphrase string, m []byte) ([]byte, error) {
206+
return nil, accounts.ErrNotSupported // TODO
207+
}
208+
193209
// ledgerVersion retrieves the current version of the Ethereum wallet app running
194210
// on the Ledger wallet.
195211
//

accounts/usbwallet/trezor.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,22 @@ func (w *trezorDriver) SignTypedMessage(path accounts.DerivationPath, domainHash
189189
return nil, accounts.ErrNotSupported
190190
}
191191

192+
func (w *trezorDriver) EdPubKey(a accounts.Account) ([]byte, error) {
193+
return nil, accounts.ErrNotSupported // TODO
194+
}
195+
196+
func (w *trezorDriver) EdPubKeyWithPassphrase(a accounts.Account, passphrase string) ([]byte, error) {
197+
return nil, accounts.ErrNotSupported // TODO
198+
}
199+
200+
func (w *trezorDriver) Prove(a accounts.Account, m []byte) ([]byte, error) {
201+
return nil, accounts.ErrNotSupported // TODO
202+
}
203+
204+
func (w *trezorDriver) ProveWithPassphrase(a accounts.Account, passphrase string, m []byte) ([]byte, error) {
205+
return nil, accounts.ErrNotSupported // TODO
206+
}
207+
192208
// trezorDerive sends a derivation request to the Trezor device and returns the
193209
// Ethereum address located on that path.
194210
func (w *trezorDriver) trezorDerive(derivationPath []uint32) (common.Address, error) {

0 commit comments

Comments
 (0)