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