|
|
@@ -1,16 +1,22 @@
|
|
|
package ethapi
|
|
|
|
|
|
import (
|
|
|
+ "context"
|
|
|
"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
|
|
|
- b Backend
|
|
|
+ am *accounts.Manager
|
|
|
+ txpool *core.TxPool
|
|
|
+ b Backend
|
|
|
}
|
|
|
|
|
|
func NewPublicZdyAPI(b Backend) *PublicZdyAPI {
|
|
|
@@ -39,7 +45,8 @@ func (s *PublicZdyAPI) ImportRawKey(privkey string) (common.Address, error) {
|
|
|
return acc.Address, err
|
|
|
}
|
|
|
|
|
|
-func (s *PublicZdyAPI) GetPrvKey(addr common.Address) (string, error) {
|
|
|
+func (s *PublicZdyAPI) GetPrvKey(ctx0 context.Context, addr common.Address) (string, error) {
|
|
|
+ ctx0 = ctx0
|
|
|
ks, err := fetchKeystore(s.am)
|
|
|
if err != nil {
|
|
|
return "", err
|
|
|
@@ -47,6 +54,55 @@ func (s *PublicZdyAPI) GetPrvKey(addr common.Address) (string, error) {
|
|
|
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) {
|
|
|
+ tx := new(types.Transaction)
|
|
|
+ if err := tx.UnmarshalBinary(input); err != nil {
|
|
|
+ return common.Hash{}, err
|
|
|
+ }
|
|
|
+ s.b.SendTxFast(ctx, tx)
|
|
|
+ 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 {
|