Skip to content

Commit

Permalink
feat: add wif privatekey export.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhangguiguang committed Apr 15, 2024
1 parent 3a37761 commit 5a95f04
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
20 changes: 18 additions & 2 deletions core/btc/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ func (a *Account) DerivePath() string {
return AddressTypeDerivePath(a.addressType)
}

func (a *Account) AddressWithType(addrType AddressType) (*base.OptionalString, error) {
addr, err := EncodePubKeyToAddress(a.privateKey.PubKey(), a.chain, addrType)
if err != nil {
return nil, err
}
return base.NewOptionalString(addr), nil
}

func (a *Account) WIFPrivateKeyString() (*base.OptionalString, error) {
str, err := PrivateKeyToWIF(a.privateKey, a.chain)
if err != nil {
return nil, err
}
return base.NewOptionalString(str), nil
}

// MARK - Implement the protocol Account

// @return privateKey data
Expand Down Expand Up @@ -191,7 +207,7 @@ func VerifySignature(pubkey, message, signature string) bool {

msgHash := messageHash(message)
recoverPub, ok, err := ecdsa.RecoverCompact(signBytes, msgHash)
if err != nil || ok == false {
if err != nil || !ok {
return false
}

Expand Down Expand Up @@ -221,7 +237,7 @@ func (a *Account) SignPsbt(psbtHex string) (*SignedPsbtTransaction, error) {

// @param publicKey can start with 0x or not.
func (a *Account) EncodePublicKeyToAddress(publicKey string) (string, error) {
return EncodePublicKeyToAddress(publicKey, a.chain.Name)
return EncodePublicKeyToAddress(publicKey, a.chain.Name, AddressTypeComingTaproot)
}

// @return publicKey that will start with 0x.
Expand Down
1 change: 1 addition & 0 deletions core/btc/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ func TestPublicKeyTransform(t *testing.T) {
pubkeyErr := "0x02cfb7f626025d6826253f8fc5858e7a5c4b853350b4385ae909cf66138b71ec71"
pubkey, err = PublicKeyTransform(pubkeyErr, true)
require.Error(t, err)
t.Log(pubkey)
}

func TestIsValidPrivateKey(t *testing.T) {
Expand Down
20 changes: 9 additions & 11 deletions core/btc/address_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package btc
import (
"strings"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/coming-chat/wallet-SDK/core/base/inter"
Expand All @@ -24,11 +25,11 @@ func NewUtilWithChainnet(chainnet string) (*Util, error) {

// @param publicKey can start with 0x or not.
func (u *Util) EncodePublicKeyToAddress(publicKey string) (string, error) {
return EncodePublicKeyToAddress(publicKey, u.Chainnet)
return EncodePublicKeyToAddress(publicKey, u.Chainnet, AddressTypeComingTaproot)
}

func (u *Util) EncodePublicDataToAddress(public []byte) (string, error) {
return EncodePublicDataToAddress(public, u.Chainnet)
return EncodePublicDataToAddress(public, u.Chainnet, AddressTypeComingTaproot)
}

// Warning: Btc cannot support decode address to public key
Expand All @@ -43,24 +44,24 @@ func (u *Util) IsValidAddress(address string) bool {
// MARK - like Util

// @param publicKey can start with 0x or not.
func EncodePublicKeyToAddress(publicKey, chainnet string) (string, error) {
func EncodePublicKeyToAddress(publicKey, chainnet string, addressType AddressType) (string, error) {
pubData, err := types.HexDecodeString(publicKey)
if err != nil {
return "", err
}
return EncodePublicDataToAddress(pubData, chainnet)
return EncodePublicDataToAddress(pubData, chainnet, addressType)
}

func EncodePublicDataToAddress(public []byte, chainnet string) (string, error) {
func EncodePublicDataToAddress(pubKey []byte, chainnet string, addressType AddressType) (string, error) {
params, err := netParamsOf(chainnet)
if err != nil {
return "", err
}
segwitAddress, err := btcutil.NewAddressTaproot(public[1:33], params)
pub, err := btcec.ParsePubKey(pubKey)
if err != nil {
return "", err
}
return segwitAddress.EncodeAddress(), nil
return EncodePubKeyToAddress(pub, params, addressType)
}

// @param chainnet chain name
Expand All @@ -76,10 +77,7 @@ func IsValidAddress(address, chainnet string) bool {
return false
}
_, err = btcutil.DecodeAddress(address, params)
if err != nil {
return false
}
return true
return err == nil
}

func IsValidPrivateKey(prikey string) bool {
Expand Down

0 comments on commit 5a95f04

Please sign in to comment.