@@ -22,6 +22,7 @@ package keystore
2222
2323import (
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
4244var (
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+ }
0 commit comments