Skip to content

Commit 4ae10a9

Browse files
committed
address features
1 parent 2a5db54 commit 4ae10a9

File tree

6 files changed

+67
-26
lines changed

6 files changed

+67
-26
lines changed

keys/Address/address.go

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package Address
22

33
import (
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
2142
type 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
}
2748
type 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)
3556
func (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
6186
func 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
}

keys/PrivateKey/private_key.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
networks "github.com/ChayanDass/gobtc-lib/Network"
1111
addr "github.com/ChayanDass/gobtc-lib/keys/Address"
12-
base58 "github.com/ChayanDass/gobtc-lib/utils"
12+
base58 "github.com/ChayanDass/gobtc-lib/utils/base58"
1313
secp "github.com/decred/dcrd/dcrec/secp256k1/v4"
1414
"golang.org/x/crypto/ripemd160"
1515
)
@@ -81,7 +81,7 @@ func (p *PrivateKey) ToAddress(addrType ...string) *addr.Address {
8181
typ = addrType[0]
8282
}
8383

84-
address, err := addr.NewAddress(&addr.KeyOptions{
84+
address, err := addr.FromPublicKey(&addr.KeyOptions{
8585
Data: pubKey,
8686
Network: p.Network,
8787
Type: typ,

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ func main() {
1818
return
1919
}
2020
fmt.Println("Generated Private Key:", prvkey.ToString())
21-
fmt.Println("address", prvkey.ToAddress().ToString())
21+
addr := prvkey.ToAddress()
22+
fmt.Println(addr.HashHex())
2223

2324
// wif := prvkey.ToWIF()
2425
// fmt.Println("WIF:", wif)
File renamed without changes.

utils/base58.go renamed to utils/base58/base58.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// Copyright (c) 2013-2015 The btcsuite developers
2-
// Use of this source code is governed by an ISC
3-
// license that can be found in the LICENSE file.
4-
51
package base58
62

73
import (
File renamed without changes.

0 commit comments

Comments
 (0)