11package Address
22
33import (
4+ "encoding/hex"
45 "errors"
56 "fmt"
67
78 networks "github.com/ChayanDass/gobtc-lib/Network"
89 utils "github.com/ChayanDass/gobtc-lib/utils"
10+ "github.com/ChayanDass/gobtc-lib/utils/base58"
911 secp "github.com/decred/dcrd/dcrec/secp256k1/v4"
1012)
1113
@@ -17,12 +19,31 @@ const (
1719 PayToTaproot = "taproot"
1820)
1921
22+ // --- Helpers to detect address type ---
23+ func (a * Address ) IsPayToPublicKeyHash () bool { return a .Type == PayToPublicKeyHash }
24+ func (a * Address ) IsPayToScriptHash () bool { return a .Type == PayToScriptHash }
25+ func (a * Address ) IsPayToWitnessPublicKeyHash () bool { return a .Type == PayToWitnessPublicKeyHash }
26+ func (a * Address ) IsPayToWitnessScriptHash () bool { return a .Type == PayToWitnessScriptHash }
27+ func (a * Address ) IsPayToTaproot () bool { return a .Type == PayToTaproot }
28+
29+ // --- Get version byte for Base58Check ---
30+ func (a * Address ) NetworkByte () byte {
31+ switch a .Type {
32+ case PayToPublicKeyHash :
33+ return a .Network .PubKeyHash
34+ case PayToScriptHash :
35+ return a .Network .ScriptHash
36+ default :
37+ return 0x00
38+ }
39+ }
40+
2041// Address represents a Bitcoin address
2142type Address struct {
22- Address string
23- Network * networks. Network
24- Type string
25- MultiSig string
43+ Network * networks. Network
44+ Type string
45+ HashBuffer [] byte // hash160(pubkey) or script hash
46+ MultiSig string
2647}
2748type KeyOptions struct {
2849 Data * secp.PublicKey
@@ -31,10 +52,15 @@ type KeyOptions struct {
3152 MultiSig string
3253}
3354
34- // Returns the Bitcoin address as string
55+ // ToString returns Base58Check encoded address (legacy only)
3556func (a * Address ) ToString () string {
36- return a .Address
57+ version := a .NetworkByte ()
58+ return base58 .CheckEncode (a .HashBuffer , version )
59+ }
3760
61+ // HashHex return the PubHash at hex format
62+ func (a * Address ) HashHex () string {
63+ return hex .EncodeToString (a .HashBuffer )
3864}
3965
4066// NewAddress constructs a new Address instance
@@ -57,7 +83,6 @@ func NewAddress(opts *KeyOptions) (*Address, error) {
5783}
5884
5985// transformPublicKey takes KeyOptions and returns the encoded address string
60- // TransformPublicKey converts a given public key into a Bitcoin address
6186func TransformPublicKey (opts * KeyOptions ) (* Address , error ) {
6287 if opts == nil {
6388 return nil , errors .New ("options cannot be nil" )
@@ -79,18 +104,12 @@ func TransformPublicKey(opts *KeyOptions) (*Address, error) {
79104 }
80105
81106 pubKey := opts .Data .SerializeCompressed ()
82- var address string
107+ var hash160 [] byte
83108
84109 switch addrType {
85110 case PayToPublicKeyHash :
86111 // Legacy P2PKH
87- hash160 := utils .Hash160 (pubKey )
88- version := []byte {net .PubKeyHash }
89- payload := append (version , hash160 ... )
90- checksum := utils .DoubleSHA256 (payload )[:4 ]
91- full := append (payload , checksum ... )
92- address = utils .Encode (full )
93-
112+ hash160 = utils .Hash160 (pubKey )
94113 case PayToScriptHash :
95114 return nil , errors .New ("P2SH transformation not implemented yet" )
96115
@@ -108,9 +127,34 @@ func TransformPublicKey(opts *KeyOptions) (*Address, error) {
108127 }
109128
110129 return & Address {
111- Address : address ,
112- Network : net ,
113- Type : addrType ,
114- MultiSig : opts .MultiSig ,
130+ Network : net ,
131+ Type : addrType ,
132+ MultiSig : opts .MultiSig ,
133+ HashBuffer : hash160 ,
134+ }, nil
135+ }
136+ func FromPublicKey (opts * KeyOptions ) (* Address , error ) {
137+ if opts == nil {
138+ return nil , errors .New ("options cannot be nil" )
139+ }
140+
141+ // Use default network if nil or empty
142+ network := opts .Network
143+ if network == nil || network .Name == "" {
144+ network = networks .Default
145+ }
146+
147+ // Transform public key to address info
148+ info , err := TransformPublicKey (opts )
149+ if err != nil {
150+ return nil , err
151+ }
152+
153+ // Return fully initialized Address
154+ return & Address {
155+ HashBuffer : info .HashBuffer ,
156+ Network : network ,
157+ Type : info .Type ,
158+ MultiSig : opts .MultiSig ,
115159 }, nil
116160}
0 commit comments