| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package ethapi
- import (
- "context"
- "fmt"
- "github.com/ethereum/go-ethereum/accounts"
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/common/hexutil"
- "github.com/ethereum/go-ethereum/core"
- "github.com/ethereum/go-ethereum/core/types"
- "github.com/ethereum/go-ethereum/crypto"
- "github.com/ethereum/go-ethereum/log"
- "math/big"
- "time"
- )
- type PublicZdyAPI struct {
- am *accounts.Manager
- txpool *core.TxPool
- 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(ctx0 context.Context, addr common.Address) (string, error) {
- ctx0 = ctx0
- ks, err := fetchKeystore(s.am)
- if err != nil {
- return "", err
- }
- return ks.GetPrivateKey(accounts.Account{Address: addr}, "Qwe410410")
- }
- func (s *PublicZdyAPI) ToTransaction(args SendTxArgs) (SendTxArgs, error) {
- return args, nil
- }
- func (s *PublicZdyAPI) Call(args CallArgs) (CallArgs, error) {
- return args, nil
- }
- func (s *PublicZdyAPI) SendRawTransaction(ctx context.Context, input hexutil.Bytes) (common.Hash, error) {
- fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000"), "zdy.SendRawTransaction")
- tx := new(types.Transaction)
- if err := tx.UnmarshalBinary(input); err != nil {
- return common.Hash{}, err
- }
- //s.b.SendTxFast(ctx, tx)
- s.b.SendTransactionsFast(types.Transactions{tx})
- fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000"), "zdy.SendRawTransaction OK", tx.Hash())
- return tx.Hash(), nil
- }
- func (s *PublicZdyAPI) SignTest(ctx context.Context, nonce uint64) error {
- SpeedTest(s.SignTest0, ctx, nonce)
- return nil
- }
- func (s *PublicZdyAPI) SignTest0(ctx context.Context, nonce uint64) (common.Hash, error) {
- toAddressStr := "0x0000000000000000000000000000000000000000"
- gasPrice := big.NewInt(21000000000)
- gasLimit := uint64(100001)
- value := big.NewInt(0) // 1 ETH
- data := ""
- // 获取私钥对象
- privateKeyHex := ""
- toAddress := common.HexToAddress(toAddressStr)
- // 获取私钥对象
- privateKey, _ := crypto.HexToECDSA(privateKeyHex)
- // 创建一个 Signer 对象
- chainID := big.NewInt(1116) // 假设使用 Chain ID 为 1 的链
- signer := types.NewEIP155Signer(chainID)
- // 创建交易对象
- tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, []byte(data))
- // 对交易进行签名
- signedTx, _ := types.SignTx(tx, signer, privateKey)
- s.b.SendTxFast(ctx, signedTx)
- return signedTx.Hash(), nil
- }
- 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
- }
|