-
Notifications
You must be signed in to change notification settings - Fork 0
/
web3_util.go
61 lines (52 loc) · 1.79 KB
/
web3_util.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
package web3
import (
"encoding/hex"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
)
type Wallet struct {
privateKey string
publicKey string
address string
}
func CreateWalletP2TR(net *chaincfg.Params) *Wallet {
key, _ := btcec.NewPrivateKey()
pk := key.PubKey().SerializeCompressed()
wif, _ := btcutil.NewWIF(key, &chaincfg.TestNet3Params, true)
privateKey := wif.String()
address, _ := btcutil.NewAddressPubKeyHash(btcutil.Hash160(pk), net)
return &Wallet{privateKey: privateKey, address: address.EncodeAddress(), publicKey: hex.EncodeToString(pk)}
}
func ImportPrivateKeyHexToLegacy(hex []byte, net *chaincfg.Params) string {
key, _ := btcec.PrivKeyFromBytes(hex)
pk := key.PubKey().SerializeCompressed()
address, _ := btcutil.NewAddressPubKeyHash(btcutil.Hash160(pk), net)
return address.EncodeAddress()
}
func ImportPrivateKeyHexToP2TR(hex []byte, net *chaincfg.Params) string {
key, _ := btcec.PrivKeyFromBytes(hex)
tapKey := txscript.ComputeTaprootKeyNoScript(key.PubKey())
p2tr, _ := btcutil.NewAddressTaproot(schnorr.SerializePubKey(tapKey), net)
return p2tr.EncodeAddress()
}
func ImportPrivateKeyToLegacy(privateKey string, net *chaincfg.Params) string {
wif, err := btcutil.DecodeWIF(privateKey)
if err != nil {
panic("privateKey error")
}
privKey := wif.PrivKey.Serialize()
return ImportPrivateKeyHexToLegacy(privKey, net)
}
func ImportPrivateKeyToP2TR(privateKey string, net *chaincfg.Params) string {
wif, err := btcutil.DecodeWIF(privateKey)
if err != nil {
panic("privateKey error")
}
privKey := wif.PrivKey.Serialize()
return ImportPrivateKeyHexToP2TR(privKey, net)
}
func GetAddressTransactions(addr string, url string) {
}