diff --git a/core/btc/account.go b/core/btc/account.go index 35ce64e..712c098 100644 --- a/core/btc/account.go +++ b/core/btc/account.go @@ -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 @@ -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 } @@ -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. diff --git a/core/btc/account_test.go b/core/btc/account_test.go index 52a1b16..048baca 100644 --- a/core/btc/account_test.go +++ b/core/btc/account_test.go @@ -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) { diff --git a/core/btc/address_util.go b/core/btc/address_util.go index 155e929..51ea09f 100644 --- a/core/btc/address_util.go +++ b/core/btc/address_util.go @@ -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" @@ -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 @@ -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 @@ -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 {