-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (76 loc) · 2.51 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"encoding/hex"
"fmt"
"log"
"go-btc/helper"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/btcutil/hdkeychain"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/tyler-smith/go-bip39"
)
func main() {
mnemonic, err := helper.GetMnemonicFromENV()
if err != nil {
log.Fatalf("获取助记词失败: %v", err)
}
// 生成比特币地址
wif, publicKey, p2shAddress, err := generateBIP49Address(mnemonic)
if err != nil {
log.Fatalf("生成BIP-49地址失败: %v", err)
}
// 输出结果
printResults(mnemonic, wif, publicKey, p2shAddress)
}
func generateBIP49Address(mnemonic string) (*btcutil.WIF, *btcec.PublicKey, *btcutil.AddressScriptHash, error) {
seed := bip39.NewSeed(mnemonic, "")
masterKey, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams)
if err != nil {
return nil, nil, nil, fmt.Errorf("创建主私钥失败: %w", err)
}
path := []uint32{
49 + hdkeychain.HardenedKeyStart,
0 + hdkeychain.HardenedKeyStart,
0 + hdkeychain.HardenedKeyStart,
0,
0,
}
key := masterKey
for _, childNum := range path {
key, err = key.Derive(childNum)
if err != nil {
return nil, nil, nil, fmt.Errorf("派生密钥失败: %w", err)
}
}
privateKey, err := key.ECPrivKey()
if err != nil {
return nil, nil, nil, fmt.Errorf("获取私钥失败: %w", err)
}
wif, err := btcutil.NewWIF(privateKey, &chaincfg.MainNetParams, true)
if err != nil {
return nil, nil, nil, fmt.Errorf("创建WIF失败: %w", err)
}
publicKey := privateKey.PubKey()
pubKeyHash := btcutil.Hash160(publicKey.SerializeCompressed())
redeemScript, err := txscript.NewScriptBuilder().
AddOp(txscript.OP_0).
AddData(pubKeyHash).
Script()
if err != nil {
return nil, nil, nil, fmt.Errorf("创建赎回脚本失败: %w", err)
}
p2shAddress, err := btcutil.NewAddressScriptHash(redeemScript, &chaincfg.MainNetParams)
if err != nil {
return nil, nil, nil, fmt.Errorf("创建P2SH地址失败: %w", err)
}
return wif, publicKey, p2shAddress, nil
}
func printResults(mnemonic string, wif *btcutil.WIF, publicKey *btcec.PublicKey, p2shAddress *btcutil.AddressScriptHash) {
fmt.Println("助记词:", mnemonic)
fmt.Printf("私钥 (WIF): %s\n", wif.String())
fmt.Printf("公钥 (压缩格式): %s\n", hex.EncodeToString(publicKey.SerializeCompressed()))
fmt.Printf("公钥 (非压缩格式): %s\n", hex.EncodeToString(publicKey.SerializeUncompressed()))
fmt.Println("P2SH 地址:", p2shAddress.EncodeAddress())
}