diff --git a/btc/rpcclient.go b/btc/rpcclient.go index 5129b64..eed423d 100644 --- a/btc/rpcclient.go +++ b/btc/rpcclient.go @@ -13,7 +13,7 @@ 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 } @@ -21,7 +21,7 @@ func NewBtcClient(URL string, user string, pass string, chainId int) (*BtcClient connCfg := &rpcclient.ConnConfig{ User: user, Pass: pass, - Params: chainCfg.Name, + Params: chainParams.Name, } u, err := url.Parse(URL) diff --git a/btc/util.go b/btc/util.go index 4040b3c..98ca2cd 100644 --- a/btc/util.go +++ b/btc/util.go @@ -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) { diff --git a/eth/transact.go b/eth/transact.go index cff42b6..01be6ab 100644 --- a/eth/transact.go +++ b/eth/transact.go @@ -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 } diff --git a/qa/btc/transact_test.go b/qa/btc/transact_test.go index 402d257..0a4b049 100644 --- a/qa/btc/transact_test.go +++ b/qa/btc/transact_test.go @@ -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} { @@ -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) } diff --git a/qa/eth/transact_test.go b/qa/eth/transact_test.go index d32f51e..9e34ca2 100644 --- a/qa/eth/transact_test.go +++ b/qa/eth/transact_test.go @@ -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) diff --git a/wallet/wallet_btc.go b/wallet/wallet_btc.go index 3eef689..2da5755 100644 --- a/wallet/wallet_btc.go +++ b/wallet/wallet_btc.go @@ -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() @@ -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 } diff --git a/wallet/wallet_eth.go b/wallet/wallet_eth.go index a546984..f3b2a01 100644 --- a/wallet/wallet_eth.go +++ b/wallet/wallet_eth.go @@ -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 } @@ -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 } @@ -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() } @@ -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 { diff --git a/wallet/wallet_trx.go b/wallet/wallet_trx.go index 4bf915b..8f44c42 100644 --- a/wallet/wallet_trx.go +++ b/wallet/wallet_trx.go @@ -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 } @@ -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 }