Skip to content

Commit

Permalink
add DeriveBtcAddress DeriveEthAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
lizhenchun committed Jul 26, 2021
1 parent 5783e52 commit 7d87356
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
11 changes: 10 additions & 1 deletion btc/transact.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type BtcOutput struct {
type BtcTransaction struct {
txauthor.AuthoredTx
chainParams *chaincfg.Params
feePerKb int64
}

func NewBtcTransaction(unspents []BtcUnspent, outputs []BtcOutput,
Expand Down Expand Up @@ -73,7 +74,7 @@ func NewBtcTransaction(unspents []BtcUnspent, outputs []BtcOutput,
unsignedTx.RandomizeChangePosition()
}

return &BtcTransaction{*unsignedTx, chainCfg}, nil
return &BtcTransaction{*unsignedTx, chainCfg, feePerKb}, nil
}

func (t *BtcTransaction) Sign(wallet *wallet.BtcWallet) error {
Expand All @@ -98,6 +99,14 @@ func (t *BtcTransaction) GetFee() int64 {
return int64(fee)
}

func (t *BtcTransaction) GetFeePerKb() int64 {
return t.feePerKb
}

func (t *BtcTransaction) HasChange() bool {
return t.ChangeIndex >= 0
}

func (t *BtcTransaction) Serialize() (string, error) {
// Serialize the transaction and convert to hex string.
buf := bytes.NewBuffer(make([]byte, 0, t.Tx.SerializeSize()))
Expand Down
24 changes: 16 additions & 8 deletions wallet/wallet_btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,40 +75,48 @@ func (w *BtcWallet) Symbol() string {
return w.symbol
}

func (w *BtcWallet) DeriveAddress() string {
func (w *BtcWallet) DeriveBtcAddress() btcutil.Address {
switch w.segWitType {
case SegWitNone:
pk := w.publicKey.SerializeCompressed()
keyHash := btcutil.Hash160(pk)
p2pkhAddr, err := btcutil.NewAddressPubKeyHash(keyHash, w.chainCfg)
if err != nil {
log.Println("DeriveAddress error:", err)
return ""
return nil
}
return p2pkhAddr.String()
return p2pkhAddr
case SegWitScript:
pk := w.publicKey.SerializeCompressed()
keyHash := btcutil.Hash160(pk)
scriptSig, err := txscript.NewScriptBuilder().AddOp(txscript.OP_0).AddData(keyHash).Script()
if err != nil {
log.Println("DeriveAddress error:", err)
return ""
return nil
}
addr, err := btcutil.NewAddressScriptHash(scriptSig, w.chainCfg)
if err != nil {
log.Println("DeriveAddress error:", err)
return ""
return nil
}
return addr.String()
return addr
case SegWitNative:
pk := w.publicKey.SerializeCompressed()
keyHash := btcutil.Hash160(pk)
p2wpkh, err := btcutil.NewAddressWitnessPubKeyHash(keyHash, w.chainCfg)
if err != nil {
log.Println("DeriveAddress error:", err)
return ""
return nil
}
return p2wpkh.String()
return p2wpkh
}
return nil
}

func (w *BtcWallet) DeriveAddress() string {
addr := w.DeriveBtcAddress()
if addr != nil {
return addr.EncodeAddress()
}
return ""
}
Expand Down
5 changes: 5 additions & 0 deletions wallet/wallet_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil/hdkeychain"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
Expand Down Expand Up @@ -79,6 +80,10 @@ 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 Down

0 comments on commit 7d87356

Please sign in to comment.