Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
lizhenchun committed Nov 2, 2021
1 parent e5a8bf9 commit 91165c2
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 45 deletions.
4 changes: 2 additions & 2 deletions btc/rpcclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ type BtcClient struct {
}

func NewBtcClient(URL string, user string, pass string, chainId int) (*BtcClient, error) {
chainCfg, err := wallet.GetBtcChainParams(chainId)
chainParams, err := wallet.GetBtcChainParams(chainId)
if err != nil {
return nil, err
}

connCfg := &rpcclient.ConnConfig{
User: user,
Pass: pass,
Params: chainCfg.Name,
Params: chainParams.Name,
}

u, err := url.Parse(URL)
Expand Down
4 changes: 2 additions & 2 deletions btc/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/btcsuite/btcwallet/wallet/txsizes"
)

func DecodeAddress(addr string, chainCfg *chaincfg.Params) (btcutil.Address, error) {
return btcutil.DecodeAddress(addr, chainCfg)
func DecodeAddress(addr string, chainParams *chaincfg.Params) (btcutil.Address, error) {
return btcutil.DecodeAddress(addr, chainParams)
}

func HexToHash(s string) (*chainhash.Hash, error) {
Expand Down
2 changes: 1 addition & 1 deletion eth/transact.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (this *TransactBaseParam) GetGasPrice() *big.Int {

func SignTx(w *wallet.EthWallet, tx *types.Transaction) (*types.Transaction, error) {
signer := types.LatestSigner(w.ChainParams())
signedTx, err := types.SignTx(tx, signer, w.DeriveEthPrivateKey())
signedTx, err := types.SignTx(tx, signer, w.DeriveNativePrivateKey())
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions qa/btc/transact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ func TestTransaction(t *testing.T) {
w3, err := hdw.NewNativeSegWitWallet(0, 0, 1)
rq.Nil(err)

chainCfg, _ := wallet.GetBtcChainParams(btcChainId)
chainParams, _ := wallet.GetBtcChainParams(btcChainId)
a0 := w0.DeriveAddress()
a1 := w1.DeriveAddress()
a2 := w2.DeriveAddress()
a3 := w3.DeriveAddress()
fmt.Printf("a0: %s\na1: %s\na2: %s\na3: %s\n", a0, a1, a2, a3)
addrA0, _ := btc.DecodeAddress(a0, chainCfg)
addrA1, _ := btc.DecodeAddress(a1, chainCfg)
addrA2, _ := btc.DecodeAddress(a2, chainCfg)
addrA3, _ := btc.DecodeAddress(a3, chainCfg)
addrA0, _ := btc.DecodeAddress(a0, chainParams)
addrA1, _ := btc.DecodeAddress(a1, chainParams)
addrA2, _ := btc.DecodeAddress(a2, chainParams)
addrA3, _ := btc.DecodeAddress(a3, chainParams)

{
for _, ad := range []string{a0, a1, a2, a3} {
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestTransaction(t *testing.T) {
out3 := btc.BtcOutput{Address: addrA3, Amount: btc.BtcToSatoshi(transferAmount)}

tx, err = btc.NewBtcTransaction([]btc.BtcUnspent{unspent}, []btc.BtcOutput{out1, out2, out3},
addrA0, feePerKb, chainCfg)
addrA0, feePerKb, chainParams)
rq.Nil(err)
}

Expand Down
1 change: 0 additions & 1 deletion qa/eth/transact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func TestTransaction(t *testing.T) {
w1, err := hdw.NewWallet(wallet.SymbolEth, 0, 0, 1)
rq.Nil(err)

//chainCfg, _ := wallet.GetBtcChainParams(btcChainId)
a0 := w0.DeriveAddress()
a1 := w1.DeriveAddress()
fmt.Printf("a0: %s\na1: %s\n", a0, a1)
Expand Down
46 changes: 23 additions & 23 deletions wallet/wallet_btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,28 @@ func (w *BtcWallet) Symbol() string {
return w.symbol
}

func (w *BtcWallet) DeriveBtcAddress() btcutil.Address {
func (w *BtcWallet) DeriveAddress() string {
addr := w.DeriveNativeAddress()
if addr != nil {
return addr.EncodeAddress()
}
return ""
}

func (w *BtcWallet) DerivePublicKey() string {
return hex.EncodeToString(w.publicKey.SerializeCompressed())
}

func (w *BtcWallet) DerivePrivateKey() string {
wif, err := btcutil.NewWIF(w.privateKey, w.chainParams, true)
if err != nil {
log.Println("DerivePrivateKey error:", err)
return ""
}
return wif.String()
}

func (w *BtcWallet) DeriveNativeAddress() btcutil.Address {
switch w.segWitType {
case SegWitNone:
pk := w.publicKey.SerializeCompressed()
Expand Down Expand Up @@ -113,28 +134,7 @@ func (w *BtcWallet) DeriveBtcAddress() btcutil.Address {
return nil
}

func (w *BtcWallet) DeriveAddress() string {
addr := w.DeriveBtcAddress()
if addr != nil {
return addr.EncodeAddress()
}
return ""
}

func (w *BtcWallet) DerivePublicKey() string {
return hex.EncodeToString(w.publicKey.SerializeCompressed())
}

func (w *BtcWallet) DerivePrivateKey() string {
wif, err := btcutil.NewWIF(w.privateKey, w.chainParams, true)
if err != nil {
log.Println("DerivePrivateKey error:", err)
return ""
}
return wif.String()
}

func (w *BtcWallet) DeriveBtcPrivateKey() *btcec.PrivateKey {
func (w *BtcWallet) DeriveNativePrivateKey() *btcec.PrivateKey {
return w.privateKey
}

Expand Down
16 changes: 8 additions & 8 deletions wallet/wallet_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewEthWallet(privateKey string, chainId int) (*EthWallet, error) {
return nil, err
}

publicKey, err := derivePublicKey(privKey)
publicKey, err := DerivePublicKey(privKey)
if err != nil {
return nil, err
}
Expand All @@ -57,7 +57,7 @@ func NewEthWalletByPath(path string, seed []byte, chainId int) (*EthWallet, erro
}
privateKey := privKey.ToECDSA()

publicKey, err := derivePublicKey(privateKey)
publicKey, err := DerivePublicKey(privateKey)
if err != nil {
return nil, err
}
Expand All @@ -79,10 +79,6 @@ func (w *EthWallet) Symbol() string {
return w.symbol
}

func (w *EthWallet) DeriveEthAddress() common.Address {
return crypto.PubkeyToAddress(*w.publicKey)
}

func (w *EthWallet) DeriveAddress() string {
return crypto.PubkeyToAddress(*w.publicKey).Hex()
}
Expand All @@ -95,11 +91,15 @@ func (w *EthWallet) DerivePrivateKey() string {
return hex.EncodeToString(crypto.FromECDSA(w.privateKey))
}

func (w *EthWallet) DeriveEthPrivateKey() *ecdsa.PrivateKey {
func (w *EthWallet) DeriveNativeAddress() common.Address {
return crypto.PubkeyToAddress(*w.publicKey)
}

func (w *EthWallet) DeriveNativePrivateKey() *ecdsa.PrivateKey {
return w.privateKey
}

func derivePublicKey(privateKey *ecdsa.PrivateKey) (*ecdsa.PublicKey, error) {
func DerivePublicKey(privateKey *ecdsa.PrivateKey) (*ecdsa.PublicKey, error) {
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
Expand Down
4 changes: 2 additions & 2 deletions wallet/wallet_trx.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewTrxWallet(privateKey string) (*TrxWallet, error) {
return nil, err
}

publicKey, err := derivePublicKey(privKey)
publicKey, err := DerivePublicKey(privKey)
if err != nil {
return nil, err
}
Expand All @@ -42,7 +42,7 @@ func NewTrxWalletByPath(path string, seed []byte) (*TrxWallet, error) {
}
privateKey := privKey.ToECDSA()

publicKey, err := derivePublicKey(privateKey)
publicKey, err := DerivePublicKey(privateKey)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 91165c2

Please sign in to comment.