2022-08-29 21:51:00 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"compress/zlib"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
2022-09-02 23:46:35 +02:00
|
|
|
"fmt"
|
2022-09-07 20:49:34 +02:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2022-08-29 21:51:00 +02:00
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
"github.com/lbryio/herald.go/db"
|
2022-09-01 00:23:47 +02:00
|
|
|
"github.com/lbryio/herald.go/internal"
|
2022-08-29 21:51:00 +02:00
|
|
|
"github.com/lbryio/lbcd/chaincfg"
|
2022-09-01 22:26:37 +02:00
|
|
|
"github.com/lbryio/lbcd/chaincfg/chainhash"
|
2022-08-29 21:51:00 +02:00
|
|
|
"github.com/lbryio/lbcd/txscript"
|
2022-09-01 00:23:47 +02:00
|
|
|
"github.com/lbryio/lbcd/wire"
|
2022-08-29 21:51:00 +02:00
|
|
|
"github.com/lbryio/lbcutil"
|
|
|
|
"golang.org/x/exp/constraints"
|
|
|
|
)
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
// TODO: import this from gorilla/rpc
|
|
|
|
// Codec creates a CodecRequest to process each request.
|
|
|
|
type Codec interface {
|
|
|
|
NewRequest(*http.Request) CodecRequest
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
|
|
|
|
// TODO: import this from gorilla/rpc
|
|
|
|
// CodecRequest decodes a request and encodes a response using a specific
|
|
|
|
// serialization scheme.
|
|
|
|
type CodecRequest interface {
|
|
|
|
// Reads request and returns the RPC method name.
|
|
|
|
Method() (string, error)
|
|
|
|
// Reads request filling the RPC method args.
|
|
|
|
ReadRequest(interface{}) error
|
|
|
|
// Writes response using the RPC method reply. The error parameter is
|
|
|
|
// the error returned by the method call, if any.
|
|
|
|
WriteResponse(http.ResponseWriter, interface{}, error) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type BlockchainCodec struct {
|
|
|
|
Codec
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BlockchainCodec) NewRequest(r *http.Request) CodecRequest {
|
|
|
|
return &BlockchainCodecRequest{c.Codec.NewRequest(r)}
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
|
|
|
|
type BlockchainCodecRequest struct {
|
|
|
|
CodecRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cr *BlockchainCodecRequest) Method() (string, error) {
|
|
|
|
rawMethod, err := cr.CodecRequest.Method()
|
|
|
|
if err != nil {
|
|
|
|
return rawMethod, err
|
|
|
|
}
|
|
|
|
parts := strings.Split(rawMethod, ".")
|
|
|
|
if len(parts) < 2 {
|
|
|
|
return rawMethod, fmt.Errorf("blockchain rpc: service/method ill-formed: %q", rawMethod)
|
|
|
|
}
|
|
|
|
service := strings.Join(parts[0:len(parts)-1], "_")
|
|
|
|
method := parts[len(parts)-1]
|
|
|
|
if len(method) < 1 {
|
|
|
|
return rawMethod, fmt.Errorf("blockchain rpc: method ill-formed: %q", method)
|
|
|
|
}
|
|
|
|
method = strings.ToUpper(string(method[0])) + string(method[1:])
|
|
|
|
return service + "." + method, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlockchainService methods handle "blockchain.block.*" RPCs
|
|
|
|
type BlockchainService struct {
|
|
|
|
DB *db.ReadOnlyDBColumnFamily
|
|
|
|
Chain *chaincfg.Params
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlockchainAddressService methods handle "blockchain.address.*" RPCs
|
|
|
|
type BlockchainAddressService struct {
|
|
|
|
BlockchainService
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlockchainScripthashService methods handle "blockchain.scripthash.*" RPCs
|
|
|
|
type BlockchainScripthashService struct {
|
|
|
|
BlockchainService
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const CHUNK_SIZE = 96
|
|
|
|
const MAX_CHUNK_SIZE = 40960
|
2022-09-01 00:23:47 +02:00
|
|
|
const HEADER_SIZE = wire.MaxBlockHeaderPayload
|
|
|
|
const HASHX_LEN = 11
|
2022-08-29 21:51:00 +02:00
|
|
|
|
|
|
|
func min[Ord constraints.Ordered](x, y Ord) Ord {
|
|
|
|
if x < y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type BlockGetChunkReq uint32
|
2022-08-29 21:51:00 +02:00
|
|
|
type blockGetChunkResp string
|
|
|
|
|
|
|
|
// 'blockchain.block.get_chunk'
|
2022-09-07 20:49:34 +02:00
|
|
|
func (s *BlockchainService) Get_chunk(r *http.Request, req *BlockGetChunkReq, resp **blockGetChunkResp) error {
|
2022-08-29 21:51:00 +02:00
|
|
|
index := uint32(*req)
|
|
|
|
db_headers, err := s.DB.GetHeaders(index*CHUNK_SIZE, CHUNK_SIZE)
|
|
|
|
if err != nil {
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
raw := make([]byte, 0, HEADER_SIZE*len(db_headers))
|
|
|
|
for _, h := range db_headers {
|
|
|
|
raw = append(raw, h[:]...)
|
|
|
|
}
|
|
|
|
headers := blockGetChunkResp(hex.EncodeToString(raw))
|
2022-09-07 20:49:34 +02:00
|
|
|
*resp = &headers
|
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type BlockGetHeaderReq uint32
|
|
|
|
type BlockGetHeaderResp struct {
|
2022-08-29 21:51:00 +02:00
|
|
|
Version uint32 `json:"version"`
|
|
|
|
PrevBlockHash string `json:"prev_block_hash"`
|
|
|
|
MerkleRoot string `json:"merkle_root"`
|
|
|
|
ClaimTrieRoot string `json:"claim_trie_root"`
|
|
|
|
Timestamp uint32 `json:"timestamp"`
|
|
|
|
Bits uint32 `json:"bits"`
|
|
|
|
Nonce uint32 `json:"nonce"`
|
|
|
|
BlockHeight uint32 `json:"block_height"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'blockchain.block.get_header'
|
2022-09-07 20:49:34 +02:00
|
|
|
func (s *BlockchainService) Get_header(r *http.Request, req *BlockGetHeaderReq, resp **BlockGetHeaderResp) error {
|
2022-08-29 21:51:00 +02:00
|
|
|
height := uint32(*req)
|
|
|
|
headers, err := s.DB.GetHeaders(height, 1)
|
|
|
|
if err != nil {
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
if len(headers) < 1 {
|
2022-09-07 20:49:34 +02:00
|
|
|
return errors.New("not found")
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
decode := func(header *[HEADER_SIZE]byte, height uint32) *BlockGetHeaderResp {
|
2022-09-01 22:26:37 +02:00
|
|
|
var h1, h2, h3 chainhash.Hash
|
|
|
|
h1.SetBytes(header[4:36])
|
|
|
|
h2.SetBytes(header[36:68])
|
|
|
|
h3.SetBytes(header[68:100])
|
2022-09-07 20:49:34 +02:00
|
|
|
return &BlockGetHeaderResp{
|
2022-08-29 21:51:00 +02:00
|
|
|
Version: binary.LittleEndian.Uint32(header[0:]),
|
2022-09-01 22:26:37 +02:00
|
|
|
PrevBlockHash: h1.String(),
|
|
|
|
MerkleRoot: h2.String(),
|
|
|
|
ClaimTrieRoot: h3.String(),
|
2022-08-29 21:51:00 +02:00
|
|
|
Timestamp: binary.LittleEndian.Uint32(header[100:]),
|
|
|
|
Bits: binary.LittleEndian.Uint32(header[104:]),
|
|
|
|
Nonce: binary.LittleEndian.Uint32(header[108:]),
|
|
|
|
BlockHeight: height,
|
|
|
|
}
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
*resp = decode(&headers[0], height)
|
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type BlockHeadersReq struct {
|
2022-08-29 21:51:00 +02:00
|
|
|
StartHeight uint32 `json:"start_height"`
|
|
|
|
Count uint32 `json:"count"`
|
|
|
|
CpHeight uint32 `json:"cp_height"`
|
|
|
|
B64 bool `json:"b64"`
|
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type BlockHeadersResp struct {
|
2022-08-29 21:51:00 +02:00
|
|
|
Base64 string `json:"base64,omitempty"`
|
|
|
|
Hex string `json:"hex,omitempty"`
|
|
|
|
Count uint32 `json:"count"`
|
|
|
|
Max uint32 `json:"max"`
|
|
|
|
Branch string `json:"branch,omitempty"`
|
|
|
|
Root string `json:"root,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'blockchain.block.headers'
|
2022-09-07 20:49:34 +02:00
|
|
|
func (s *BlockchainService) Headers(r *http.Request, req *BlockHeadersReq, resp **BlockHeadersResp) error {
|
2022-08-29 21:51:00 +02:00
|
|
|
count := min(req.Count, MAX_CHUNK_SIZE)
|
|
|
|
db_headers, err := s.DB.GetHeaders(req.StartHeight, count)
|
|
|
|
if err != nil {
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
count = uint32(len(db_headers))
|
|
|
|
raw := make([]byte, 0, HEADER_SIZE*count)
|
|
|
|
for _, h := range db_headers {
|
|
|
|
raw = append(raw, h[:]...)
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
result := &BlockHeadersResp{
|
2022-08-29 21:51:00 +02:00
|
|
|
Count: count,
|
|
|
|
Max: MAX_CHUNK_SIZE,
|
|
|
|
}
|
|
|
|
if req.B64 {
|
|
|
|
zipped := bytes.Buffer{}
|
|
|
|
w := zlib.NewWriter(&zipped)
|
|
|
|
w.Write(raw)
|
|
|
|
w.Close()
|
|
|
|
result.Base64 = base64.StdEncoding.EncodeToString(zipped.Bytes())
|
|
|
|
} else {
|
|
|
|
result.Hex = hex.EncodeToString(raw)
|
|
|
|
}
|
|
|
|
if count > 0 && req.CpHeight > 0 {
|
|
|
|
// TODO
|
|
|
|
//last_height := height + count - 1
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
*resp = result
|
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2022-09-02 23:46:35 +02:00
|
|
|
func decodeScriptHash(scripthash string) ([]byte, error) {
|
|
|
|
sh, err := hex.DecodeString(scripthash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(sh) != chainhash.HashSize {
|
|
|
|
return nil, fmt.Errorf("invalid scripthash: %v (length %v)", scripthash, len(sh))
|
|
|
|
}
|
2022-09-01 00:23:47 +02:00
|
|
|
internal.ReverseBytesInPlace(sh)
|
2022-09-02 23:46:35 +02:00
|
|
|
return sh, nil
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
|
2022-09-02 23:46:35 +02:00
|
|
|
func hashX(scripthash []byte) []byte {
|
|
|
|
return scripthash[:HASHX_LEN]
|
2022-09-01 00:23:47 +02:00
|
|
|
}
|
|
|
|
|
2022-08-29 21:51:00 +02:00
|
|
|
func hashXScript(script []byte, coin *chaincfg.Params) []byte {
|
|
|
|
if _, err := txscript.ExtractClaimScript(script); err == nil {
|
|
|
|
baseScript := txscript.StripClaimScriptPrefix(script)
|
|
|
|
if class, addrs, _, err := txscript.ExtractPkScriptAddrs(baseScript, coin); err == nil {
|
|
|
|
switch class {
|
|
|
|
case txscript.PubKeyHashTy, txscript.ScriptHashTy, txscript.PubKeyTy:
|
|
|
|
script, _ := txscript.PayToAddrScript(addrs[0])
|
|
|
|
return hashXScript(script, coin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sum := sha256.Sum256(script)
|
2022-09-01 00:23:47 +02:00
|
|
|
return sum[:HASHX_LEN]
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type AddressGetBalanceReq struct {
|
2022-09-01 00:23:47 +02:00
|
|
|
Address string `json:"address"`
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
type AddressGetBalanceResp struct {
|
2022-09-01 00:23:47 +02:00
|
|
|
Confirmed uint64 `json:"confirmed"`
|
|
|
|
Unconfirmed uint64 `json:"unconfirmed"`
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 'blockchain.address.get_balance'
|
2022-09-07 20:49:34 +02:00
|
|
|
func (s *BlockchainAddressService) Get_balance(r *http.Request, req *AddressGetBalanceReq, resp **AddressGetBalanceResp) error {
|
2022-09-01 00:23:47 +02:00
|
|
|
address, err := lbcutil.DecodeAddress(req.Address, s.Chain)
|
2022-08-29 21:51:00 +02:00
|
|
|
if err != nil {
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
script, err := txscript.PayToAddrScript(address)
|
|
|
|
if err != nil {
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-01 00:23:47 +02:00
|
|
|
hashX := hashXScript(script, s.Chain)
|
2022-08-29 21:51:00 +02:00
|
|
|
confirmed, unconfirmed, err := s.DB.GetBalance(hashX)
|
|
|
|
if err != nil {
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
*resp = &AddressGetBalanceResp{confirmed, unconfirmed}
|
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2022-09-02 23:46:35 +02:00
|
|
|
type scripthashGetBalanceReq struct {
|
|
|
|
ScriptHash string `json:"scripthash"`
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
type ScripthashGetBalanceResp struct {
|
2022-09-02 23:46:35 +02:00
|
|
|
Confirmed uint64 `json:"confirmed"`
|
|
|
|
Unconfirmed uint64 `json:"unconfirmed"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'blockchain.scripthash.get_balance'
|
2022-09-07 20:49:34 +02:00
|
|
|
func (s *BlockchainScripthashService) Get_balance(r *http.Request, req *scripthashGetBalanceReq, resp **ScripthashGetBalanceResp) error {
|
2022-09-02 23:46:35 +02:00
|
|
|
scripthash, err := decodeScriptHash(req.ScriptHash)
|
|
|
|
if err != nil {
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
|
|
|
hashX := hashX(scripthash)
|
|
|
|
confirmed, unconfirmed, err := s.DB.GetBalance(hashX)
|
|
|
|
if err != nil {
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
*resp = &ScripthashGetBalanceResp{confirmed, unconfirmed}
|
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type AddressGetHistoryReq struct {
|
2022-09-01 00:23:47 +02:00
|
|
|
Address string `json:"address"`
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
type TxInfo struct {
|
2022-09-01 00:23:47 +02:00
|
|
|
TxHash string `json:"tx_hash"`
|
|
|
|
Height uint32 `json:"height"`
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
type TxInfoFee struct {
|
|
|
|
TxInfo
|
2022-09-01 00:23:47 +02:00
|
|
|
Fee uint64 `json:"fee"`
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
type AddressGetHistoryResp struct {
|
2022-09-01 00:23:47 +02:00
|
|
|
Confirmed []TxInfo `json:"confirmed"`
|
|
|
|
Unconfirmed []TxInfoFee `json:"unconfirmed"`
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 'blockchain.address.get_history'
|
2022-09-07 20:49:34 +02:00
|
|
|
func (s *BlockchainAddressService) Get_history(r *http.Request, req *AddressGetHistoryReq, resp **AddressGetHistoryResp) error {
|
2022-09-01 00:23:47 +02:00
|
|
|
address, err := lbcutil.DecodeAddress(req.Address, s.Chain)
|
2022-08-29 21:51:00 +02:00
|
|
|
if err != nil {
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
script, err := txscript.PayToAddrScript(address)
|
|
|
|
if err != nil {
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-01 00:23:47 +02:00
|
|
|
hashX := hashXScript(script, s.Chain)
|
2022-08-29 21:51:00 +02:00
|
|
|
dbTXs, err := s.DB.GetHistory(hashX)
|
2022-09-02 23:46:35 +02:00
|
|
|
if err != nil {
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
2022-08-29 21:51:00 +02:00
|
|
|
confirmed := make([]TxInfo, 0, len(dbTXs))
|
|
|
|
for _, tx := range dbTXs {
|
|
|
|
confirmed = append(confirmed,
|
|
|
|
TxInfo{
|
2022-09-01 22:26:37 +02:00
|
|
|
TxHash: tx.TxHash.String(),
|
2022-08-29 21:51:00 +02:00
|
|
|
Height: tx.Height,
|
|
|
|
})
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
result := &AddressGetHistoryResp{
|
2022-08-29 21:51:00 +02:00
|
|
|
Confirmed: confirmed,
|
|
|
|
Unconfirmed: []TxInfoFee{}, // TODO
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
*resp = result
|
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type ScripthashGetHistoryReq struct {
|
2022-09-02 23:46:35 +02:00
|
|
|
ScriptHash string `json:"scripthash"`
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
type ScripthashGetHistoryResp struct {
|
2022-09-02 23:46:35 +02:00
|
|
|
Confirmed []TxInfo `json:"confirmed"`
|
|
|
|
Unconfirmed []TxInfoFee `json:"unconfirmed"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'blockchain.scripthash.get_history'
|
2022-09-07 20:49:34 +02:00
|
|
|
func (s *BlockchainScripthashService) Get_history(r *http.Request, req *ScripthashGetHistoryReq, resp **ScripthashGetHistoryResp) error {
|
2022-09-02 23:46:35 +02:00
|
|
|
scripthash, err := decodeScriptHash(req.ScriptHash)
|
|
|
|
if err != nil {
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
|
|
|
hashX := hashX(scripthash)
|
|
|
|
dbTXs, err := s.DB.GetHistory(hashX)
|
|
|
|
if err != nil {
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
|
|
|
confirmed := make([]TxInfo, 0, len(dbTXs))
|
|
|
|
for _, tx := range dbTXs {
|
|
|
|
confirmed = append(confirmed,
|
|
|
|
TxInfo{
|
|
|
|
TxHash: tx.TxHash.String(),
|
|
|
|
Height: tx.Height,
|
|
|
|
})
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
result := &ScripthashGetHistoryResp{
|
2022-09-02 23:46:35 +02:00
|
|
|
Confirmed: confirmed,
|
|
|
|
Unconfirmed: []TxInfoFee{}, // TODO
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
*resp = result
|
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type AddressGetMempoolReq struct {
|
2022-09-01 00:23:47 +02:00
|
|
|
Address string `json:"address"`
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
type AddressGetMempoolResp []TxInfoFee
|
2022-08-29 21:51:00 +02:00
|
|
|
|
|
|
|
// 'blockchain.address.get_mempool'
|
2022-09-07 20:49:34 +02:00
|
|
|
func (s *BlockchainAddressService) Get_mempool(r *http.Request, req *AddressGetMempoolReq, resp **AddressGetMempoolResp) error {
|
2022-09-01 00:23:47 +02:00
|
|
|
address, err := lbcutil.DecodeAddress(req.Address, s.Chain)
|
2022-08-29 21:51:00 +02:00
|
|
|
if err != nil {
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
script, err := txscript.PayToAddrScript(address)
|
|
|
|
if err != nil {
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-01 00:23:47 +02:00
|
|
|
hashX := hashXScript(script, s.Chain)
|
2022-08-29 21:51:00 +02:00
|
|
|
// TODO...
|
2022-09-01 00:23:47 +02:00
|
|
|
internal.ReverseBytesInPlace(hashX)
|
2022-08-29 21:51:00 +02:00
|
|
|
unconfirmed := make([]TxInfoFee, 0, 100)
|
2022-09-07 20:49:34 +02:00
|
|
|
result := AddressGetMempoolResp(unconfirmed)
|
|
|
|
*resp = &result
|
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type ScripthashGetMempoolReq struct {
|
2022-09-02 23:46:35 +02:00
|
|
|
ScriptHash string `json:"scripthash"`
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
type ScripthashGetMempoolResp []TxInfoFee
|
2022-09-02 23:46:35 +02:00
|
|
|
|
|
|
|
// 'blockchain.scripthash.get_mempool'
|
2022-09-07 20:49:34 +02:00
|
|
|
func (s *BlockchainScripthashService) Get_mempool(r *http.Request, req *ScripthashGetMempoolReq, resp **ScripthashGetMempoolResp) error {
|
2022-09-02 23:46:35 +02:00
|
|
|
scripthash, err := decodeScriptHash(req.ScriptHash)
|
|
|
|
if err != nil {
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
|
|
|
hashX := hashX(scripthash)
|
|
|
|
// TODO...
|
|
|
|
internal.ReverseBytesInPlace(hashX)
|
|
|
|
unconfirmed := make([]TxInfoFee, 0, 100)
|
2022-09-07 20:49:34 +02:00
|
|
|
result := ScripthashGetMempoolResp(unconfirmed)
|
|
|
|
*resp = &result
|
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type AddressListUnspentReq struct {
|
2022-09-01 00:23:47 +02:00
|
|
|
Address string `json:"address"`
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
type TXOInfo struct {
|
2022-09-01 00:23:47 +02:00
|
|
|
TxHash string `json:"tx_hash"`
|
|
|
|
TxPos uint16 `json:"tx_pos"`
|
|
|
|
Height uint32 `json:"height"`
|
|
|
|
Value uint64 `json:"value"`
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
type AddressListUnspentResp []TXOInfo
|
2022-08-29 21:51:00 +02:00
|
|
|
|
|
|
|
// 'blockchain.address.listunspent'
|
2022-09-07 20:49:34 +02:00
|
|
|
func (s *BlockchainAddressService) Listunspent(r *http.Request, req *AddressListUnspentReq, resp **AddressListUnspentResp) error {
|
2022-09-01 00:23:47 +02:00
|
|
|
address, err := lbcutil.DecodeAddress(req.Address, s.Chain)
|
2022-08-29 21:51:00 +02:00
|
|
|
if err != nil {
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
|
|
|
script, err := txscript.PayToAddrScript(address)
|
|
|
|
if err != nil {
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-01 00:23:47 +02:00
|
|
|
hashX := hashXScript(script, s.Chain)
|
2022-08-29 21:51:00 +02:00
|
|
|
dbTXOs, err := s.DB.GetUnspent(hashX)
|
|
|
|
unspent := make([]TXOInfo, 0, len(dbTXOs))
|
|
|
|
for _, txo := range dbTXOs {
|
|
|
|
unspent = append(unspent,
|
|
|
|
TXOInfo{
|
2022-09-01 22:26:37 +02:00
|
|
|
TxHash: txo.TxHash.String(),
|
2022-08-29 21:51:00 +02:00
|
|
|
TxPos: txo.TxPos,
|
|
|
|
Height: txo.Height,
|
|
|
|
Value: txo.Value,
|
|
|
|
})
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
result := AddressListUnspentResp(unspent)
|
|
|
|
*resp = &result
|
|
|
|
return err
|
2022-08-29 21:51:00 +02:00
|
|
|
}
|
2022-09-02 23:46:35 +02:00
|
|
|
|
2022-09-07 20:49:34 +02:00
|
|
|
type ScripthashListUnspentReq struct {
|
2022-09-02 23:46:35 +02:00
|
|
|
ScriptHash string `json:"scripthash"`
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
type ScripthashListUnspentResp []TXOInfo
|
2022-09-02 23:46:35 +02:00
|
|
|
|
|
|
|
// 'blockchain.scripthash.listunspent'
|
2022-09-07 20:49:34 +02:00
|
|
|
func (s *BlockchainScripthashService) Listunspent(r *http.Request, req *ScripthashListUnspentReq, resp **ScripthashListUnspentResp) error {
|
2022-09-02 23:46:35 +02:00
|
|
|
scripthash, err := decodeScriptHash(req.ScriptHash)
|
|
|
|
if err != nil {
|
2022-09-07 20:49:34 +02:00
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|
|
|
|
hashX := hashX(scripthash)
|
|
|
|
dbTXOs, err := s.DB.GetUnspent(hashX)
|
|
|
|
unspent := make([]TXOInfo, 0, len(dbTXOs))
|
|
|
|
for _, txo := range dbTXOs {
|
|
|
|
unspent = append(unspent,
|
|
|
|
TXOInfo{
|
|
|
|
TxHash: txo.TxHash.String(),
|
|
|
|
TxPos: txo.TxPos,
|
|
|
|
Height: txo.Height,
|
|
|
|
Value: txo.Value,
|
|
|
|
})
|
|
|
|
}
|
2022-09-07 20:49:34 +02:00
|
|
|
result := ScripthashListUnspentResp(unspent)
|
|
|
|
*resp = &result
|
|
|
|
return err
|
2022-09-02 23:46:35 +02:00
|
|
|
}
|