Refactore address conversion functions to bitcoinwire.go
parent
2736d15d63
commit
54ddfa9e03
|
@ -5,6 +5,7 @@ import (
|
|||
"encoding/hex"
|
||||
|
||||
"github.com/btcsuite/btcd/blockchain"
|
||||
"github.com/btcsuite/btcutil"
|
||||
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/btcsuite/btcd/txscript"
|
||||
|
@ -27,16 +28,30 @@ type BitcoinBlockParser struct {
|
|||
Params *chaincfg.Params
|
||||
}
|
||||
|
||||
func (p *BitcoinBlockParser) parseOutputScript(b []byte) ([]string, error) {
|
||||
_, addresses, _, err := txscript.ExtractPkScriptAddrs(b, p.Params)
|
||||
// AddressToOutputScript converts bitcoin address to ScriptPubKey
|
||||
func AddressToOutputScript(address string) ([]byte, error) {
|
||||
da, err := btcutil.DecodeAddress(address, GetChainParams()[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
addrs := make([]string, len(addresses))
|
||||
for i, a := range addresses {
|
||||
addrs[i] = a.EncodeAddress()
|
||||
script, err := txscript.PayToAddrScript(da)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return addrs, nil
|
||||
return script, nil
|
||||
}
|
||||
|
||||
// OutputScriptToAddresses converts ScriptPubKey to bitcoin addresses
|
||||
func OutputScriptToAddresses(script []byte) ([]string, error) {
|
||||
_, addresses, _, err := txscript.ExtractPkScriptAddrs(script, GetChainParams()[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rv := make([]string, len(addresses))
|
||||
for i, a := range addresses {
|
||||
rv[i] = a.EncodeAddress()
|
||||
}
|
||||
return rv, nil
|
||||
}
|
||||
|
||||
func (p *BitcoinBlockParser) ParseBlock(b []byte) (*Block, error) {
|
||||
|
@ -71,7 +86,7 @@ func (p *BitcoinBlockParser) ParseBlock(b []byte) (*Block, error) {
|
|||
}
|
||||
vout := make([]Vout, len(t.TxOut))
|
||||
for i, out := range t.TxOut {
|
||||
// addrs, err := p.parseOutputScript(out.PkScript)
|
||||
// addrs, err := OutputScriptToAddresses(out.PkScript)
|
||||
// if err != nil {
|
||||
// addrs = []string{}
|
||||
// }
|
||||
|
|
|
@ -1,10 +1,5 @@
|
|||
package bchain
|
||||
|
||||
import (
|
||||
"github.com/btcsuite/btcd/txscript"
|
||||
"github.com/btcsuite/btcutil"
|
||||
)
|
||||
|
||||
type ScriptSig struct {
|
||||
// Asm string `json:"asm"`
|
||||
Hex string `json:"hex"`
|
||||
|
@ -31,32 +26,6 @@ type Vout struct {
|
|||
ScriptPubKey ScriptPubKey `json:"scriptPubKey"`
|
||||
}
|
||||
|
||||
// AddressToOutputScript converts bitcoin address to ScriptPubKey
|
||||
func AddressToOutputScript(address string) ([]byte, error) {
|
||||
da, err := btcutil.DecodeAddress(address, GetChainParams()[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
script, err := txscript.PayToAddrScript(da)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return script, nil
|
||||
}
|
||||
|
||||
// OutputScriptToAddresses converts ScriptPubKey to bitcoin addresses
|
||||
func OutputScriptToAddresses(script []byte) ([]string, error) {
|
||||
_, addresses, _, err := txscript.ExtractPkScriptAddrs(script, GetChainParams()[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rv := make([]string, len(addresses))
|
||||
for i, a := range addresses {
|
||||
rv[i] = a.EncodeAddress()
|
||||
}
|
||||
return rv, nil
|
||||
}
|
||||
|
||||
// Tx is blockchain transaction
|
||||
// unnecessary fields are commented out to avoid overhead
|
||||
type Tx struct {
|
||||
|
|
Loading…
Reference in New Issue