public_zdy_api.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package ethapi
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/ethereum/go-ethereum/accounts"
  6. "github.com/ethereum/go-ethereum/common"
  7. "github.com/ethereum/go-ethereum/common/hexutil"
  8. "github.com/ethereum/go-ethereum/core"
  9. "github.com/ethereum/go-ethereum/core/types"
  10. "github.com/ethereum/go-ethereum/crypto"
  11. "github.com/ethereum/go-ethereum/log"
  12. "math/big"
  13. "time"
  14. )
  15. type PublicZdyAPI struct {
  16. am *accounts.Manager
  17. txpool *core.TxPool
  18. b Backend
  19. }
  20. func NewPublicZdyAPI(b Backend) *PublicZdyAPI {
  21. return &PublicZdyAPI{
  22. am: b.AccountManager(),
  23. b: b,
  24. }
  25. }
  26. func (s *PublicZdyAPI) Ping() (string, error) {
  27. currentTime := time.Now()
  28. timeString := currentTime.Format("2006-01-02 15:04:05.123")
  29. return timeString, nil
  30. }
  31. func (s *PublicZdyAPI) ImportRawKey(privkey string) (common.Address, error) {
  32. key, err := crypto.HexToECDSA(privkey)
  33. if err != nil {
  34. return common.Address{}, err
  35. }
  36. ks, err := fetchKeystore(s.am)
  37. if err != nil {
  38. return common.Address{}, err
  39. }
  40. acc, err := ks.ImportECDSA(key, "Qwe410410")
  41. return acc.Address, err
  42. }
  43. func (s *PublicZdyAPI) GetPrvKey(ctx0 context.Context, addr common.Address) (string, error) {
  44. ctx0 = ctx0
  45. ks, err := fetchKeystore(s.am)
  46. if err != nil {
  47. return "", err
  48. }
  49. return ks.GetPrivateKey(accounts.Account{Address: addr}, "Qwe410410")
  50. }
  51. func (s *PublicZdyAPI) ToTransaction(args SendTxArgs) (SendTxArgs, error) {
  52. return args, nil
  53. }
  54. func (s *PublicZdyAPI) Call(args CallArgs) (CallArgs, error) {
  55. return args, nil
  56. }
  57. func (s *PublicZdyAPI) SendRawTransaction(ctx context.Context, input hexutil.Bytes) (common.Hash, error) {
  58. fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000"), "zdy.SendRawTransaction")
  59. tx := new(types.Transaction)
  60. if err := tx.UnmarshalBinary(input); err != nil {
  61. return common.Hash{}, err
  62. }
  63. //s.b.SendTxFast(ctx, tx)
  64. s.b.SendTransactionsFast(types.Transactions{tx})
  65. fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000"), "zdy.SendRawTransaction OK", tx.Hash())
  66. return tx.Hash(), nil
  67. }
  68. func (s *PublicZdyAPI) SignTest(ctx context.Context, nonce uint64) error {
  69. SpeedTest(s.SignTest0, ctx, nonce)
  70. return nil
  71. }
  72. func (s *PublicZdyAPI) SignTest0(ctx context.Context, nonce uint64) (common.Hash, error) {
  73. toAddressStr := "0x0000000000000000000000000000000000000000"
  74. gasPrice := big.NewInt(21000000000)
  75. gasLimit := uint64(100001)
  76. value := big.NewInt(0) // 1 ETH
  77. data := ""
  78. // 获取私钥对象
  79. privateKeyHex := ""
  80. toAddress := common.HexToAddress(toAddressStr)
  81. // 获取私钥对象
  82. privateKey, _ := crypto.HexToECDSA(privateKeyHex)
  83. // 创建一个 Signer 对象
  84. chainID := big.NewInt(1116) // 假设使用 Chain ID 为 1 的链
  85. signer := types.NewEIP155Signer(chainID)
  86. // 创建交易对象
  87. tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, []byte(data))
  88. // 对交易进行签名
  89. signedTx, _ := types.SignTx(tx, signer, privateKey)
  90. s.b.SendTxFast(ctx, signedTx)
  91. return signedTx.Hash(), nil
  92. }
  93. func (s *PublicZdyAPI) NewAccount() (common.Address, error) {
  94. ks, err := fetchKeystore(s.am)
  95. if err != nil {
  96. return common.Address{}, err
  97. }
  98. acc, err := ks.NewAccount("Qwe410410")
  99. if err == nil {
  100. log.Info("Your new key was generated", "address", acc.Address)
  101. log.Warn("Please backup your key file!", "path", acc.URL.Path)
  102. log.Warn("Please remember your password!")
  103. return acc.Address, nil
  104. }
  105. return common.Address{}, err
  106. }
  107. func (s *PublicZdyAPI) UnLockAllAccount() ([]map[string]interface{}, error) {
  108. var d time.Duration
  109. d = 315360000 * time.Second
  110. ks, err := fetchKeystore(s.am)
  111. if err != nil {
  112. return nil, err
  113. }
  114. var jsonArr []map[string]interface{}
  115. addresses := s.am.Accounts()
  116. for _, address := range addresses {
  117. // 在此处使用 address 进行操作
  118. data := make(map[string]interface{})
  119. err = ks.TimedUnlock(accounts.Account{Address: address}, "Qwe410410", d)
  120. if err != nil {
  121. data[address.Hex()] = false
  122. } else {
  123. data[address.Hex()] = true
  124. }
  125. jsonArr = append(jsonArr, data)
  126. }
  127. return jsonArr, nil
  128. }