-
Notifications
You must be signed in to change notification settings - Fork 0
/
btc_address.go
33 lines (30 loc) · 998 Bytes
/
btc_address.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
package types
import (
"fmt"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
"github.com/ethereum/go-ethereum/common"
)
func ConvertBTCAddress(str string) (common.Address, error) {
from := make([]byte, 0)
addr, err := btcutil.DecodeAddress(str, &chaincfg.RegressionNetParams)
if err != nil {
return common.Address{}, fmt.Errorf("convert BTC address is error:%v", err)
} else {
if addr == nil {
return common.Address{}, fmt.Errorf("the address of converted is empty")
}
switch addr := addr.(type) {
case *btcutil.AddressPubKeyHash:
from = addr.ScriptAddress()
case *btcutil.AddressPubKey:
from = btcutil.Hash160(addr.ScriptAddress())
case *btcutil.AddressWitnessPubKeyHash:
from = addr.ScriptAddress()
default:
fmt.Printf("it's dangerous: %s\n", fmt.Errorf("%s is not support type of address, only support p2pkh p2pk p2wpk", addr.String()))
from = btcutil.Hash160(addr.ScriptAddress())
}
}
return common.BytesToAddress(from), nil
}