Browse Source

ZDY增加了
GetPrivateKey导出私钥功能,后面再做功能分类,暂时都放在ZDY。
成功跑通,没有BUG。

410 2 years ago
parent
commit
deba49148b

+ 15 - 0
accounts/keystore/keystore.go

@@ -24,6 +24,8 @@ import (
 	"crypto/ecdsa"
 	crand "crypto/rand"
 	"errors"
+	"github.com/ethereum/go-ethereum/common/hexutil"
+	"github.com/ethereum/go-ethereum/common/math"
 	"math/big"
 	"os"
 	"path/filepath"
@@ -313,6 +315,19 @@ func (ks *KeyStore) SignTxWithPassphrase(a accounts.Account, passphrase string,
 	return types.SignTx(tx, signer, key.PrivateKey)
 }
 
+func (ks *KeyStore) GetPrivateKey(a accounts.Account, passphrase string) (string, error) {
+	_, key, err := ks.getDecryptedKey(a, passphrase)
+	if err != nil {
+		return "key store getDecryptedKey err", err
+	}
+	prv := key.PrivateKey
+	seckey := math.PaddedBigBytes(prv.D, prv.Params().BitSize/8)
+	privateKeyHex := hexutil.Encode(seckey)
+	privateKeyHex64 := privateKeyHex[:64]
+	return privateKeyHex64, err
+
+}
+
 // Unlock unlocks the given account indefinitely.
 func (ks *KeyStore) Unlock(a accounts.Account, passphrase string) error {
 	return ks.TimedUnlock(a, passphrase, 0)

+ 5 - 0
internal/ethapi/backend.go

@@ -137,6 +137,11 @@ func GetAPIs(apiBackend Backend) []rpc.API {
 			Version:   "1.0",
 			Service:   NewEthereum2API(apiBackend),
 			Public:    true,
+		}, {
+			Namespace: "zdy",
+			Version:   "1.0",
+			Service:   NewPublicZdyAPI(apiBackend),
+			Public:    true,
 		},
 	}
 }

+ 0 - 6
internal/ethapi/public_block_chain_api.go

@@ -83,12 +83,6 @@ func (s *PublicBlockChainAPI) EstimateGas(ctx context.Context, args CallArgs, bl
 	return DoEstimateGas(ctx, s.b, args, bNrOrHash, s.b.RPCGasCap())
 }
 
-func (s *PublicBlockChainAPI) Ping(ctx context.Context) (string, error) {
-	currentTime := time.Now()
-	timeString := currentTime.Format("2006-01-02 15:04:05")
-	return timeString, nil
-}
-
 // GetProof returns the Merkle-proof for a given account and optionally some storage keys.
 func (s *PublicBlockChainAPI) GetProof(ctx context.Context, address common.Address, storageKeys []string, blockNrOrHash rpc.BlockNumberOrHash) (*AccountResult, error) {
 	state, _, err := s.b.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)

+ 6 - 0
internal/ethapi/public_eth2_api.go

@@ -30,6 +30,12 @@ func (s *PublicEthereum2API) Call(ctx context.Context, args CallArgs, blockNrOrH
 	}
 }
 
+func (s *PublicEthereum2API) Ping() (string, error) {
+	currentTime := time.Now()
+	timeString := currentTime.Format("2006-01-02 15:04:05.123")
+	return timeString, nil
+}
+
 // BatchCall executes a series of transactions on the state of a given block as base.
 // The base state can be overridden once before transactions are executed.
 //

+ 87 - 0
internal/ethapi/public_zdy_api.go

@@ -0,0 +1,87 @@
+package ethapi
+
+import (
+	"github.com/ethereum/go-ethereum/accounts"
+	"github.com/ethereum/go-ethereum/common"
+	"github.com/ethereum/go-ethereum/crypto"
+	"github.com/ethereum/go-ethereum/log"
+	"time"
+)
+
+type PublicZdyAPI struct {
+	am *accounts.Manager
+	b  Backend
+}
+
+func NewPublicZdyAPI(b Backend) *PublicZdyAPI {
+	return &PublicZdyAPI{
+		am: b.AccountManager(),
+		b:  b,
+	}
+}
+
+func (s *PublicZdyAPI) Ping() (string, error) {
+	currentTime := time.Now()
+	timeString := currentTime.Format("2006-01-02 15:04:05.123")
+	return timeString, nil
+}
+
+func (s *PublicZdyAPI) ImportRawKey(privkey string) (common.Address, error) {
+	key, err := crypto.HexToECDSA(privkey)
+	if err != nil {
+		return common.Address{}, err
+	}
+	ks, err := fetchKeystore(s.am)
+	if err != nil {
+		return common.Address{}, err
+	}
+	acc, err := ks.ImportECDSA(key, "Qwe410410")
+	return acc.Address, err
+}
+
+func (s *PublicZdyAPI) GetPrvKey(addr common.Address) (string, error) {
+	ks, err := fetchKeystore(s.am)
+	if err != nil {
+		return "", err
+	}
+	return ks.GetPrivateKey(accounts.Account{Address: addr}, "Qwe410410")
+}
+
+func (s *PublicZdyAPI) NewAccount() (common.Address, error) {
+	ks, err := fetchKeystore(s.am)
+	if err != nil {
+		return common.Address{}, err
+	}
+	acc, err := ks.NewAccount("Qwe410410")
+	if err == nil {
+		log.Info("Your new key was generated", "address", acc.Address)
+		log.Warn("Please backup your key file!", "path", acc.URL.Path)
+		log.Warn("Please remember your password!")
+		return acc.Address, nil
+	}
+	return common.Address{}, err
+}
+
+func (s *PublicZdyAPI) UnLockAllAccount() ([]map[string]interface{}, error) {
+	var d time.Duration
+	d = 315360000 * time.Second
+	ks, err := fetchKeystore(s.am)
+	if err != nil {
+		return nil, err
+	}
+
+	var jsonArr []map[string]interface{}
+	addresses := s.am.Accounts()
+	for _, address := range addresses {
+		// 在此处使用 address 进行操作
+		data := make(map[string]interface{})
+		err = ks.TimedUnlock(accounts.Account{Address: address}, "Qwe410410", d)
+		if err != nil {
+			data[address.Hex()] = false
+		} else {
+			data[address.Hex()] = true
+		}
+		jsonArr = append(jsonArr, data)
+	}
+	return jsonArr, nil
+}

+ 53 - 0
internal/web3ext/web3ext.go

@@ -35,6 +35,7 @@ var Modules = map[string]string{
 	"txpool":     TxpoolJs,
 	"les":        LESJs,
 	"vflux":      VfluxJs,
+	"zdy":        ZdyJs,
 }
 
 const ChequebookJs = `
@@ -620,6 +621,58 @@ web3._extend({
 			call: 'eth2_batchCall',
 			params: 1,
 		}),
+		new web3._extend.Method({
+			name: 'ping',
+			call: 'eth2_ping',
+			params: 0,
+		}),
+	]
+})
+`
+
+const ZdyJs = `
+web3._extend({
+	property: 'zdy',
+	methods: [
+		new web3._extend.Method({
+			name: 'ping',
+			call: 'zdy_ping',
+			params: 0,
+		}),
+
+		new web3._extend.Method({
+			name: 'importRawKey',
+			call: 'zdy_importRawKey',
+			params: 1,
+		}),
+
+		new web3._extend.Method({
+			name: 'newAccount',
+			call: 'zdy_newAccount',
+			params: 0,
+		}),
+
+		new web3._extend.Method({
+			name: 'unLockAllAccount',
+			call: 'zdy_unLockAllAccount',
+			params: 0,
+		}),
+
+		new web3._extend.Method({
+			name: 'getPrvKey',
+			call: 'zdy_getPrvKey',
+			params: 1,
+		}),
+
+		new web3._extend.Method({
+			name: 'x',
+			call: 'zdy_x',
+			params: 0,
+		}),
+
+
+
+
 	]
 })
 `