github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/lib/others/ltc/ltc.go (about)

     1  package ltc
     2  
     3  import (
     4  	"bytes"
     5  
     6  	"github.com/piotrnar/gocoin/lib/btc"
     7  	"github.com/piotrnar/gocoin/lib/others/utils"
     8  	"github.com/piotrnar/gocoin/lib/utxo"
     9  )
    10  
    11  const LTC_ADDR_VERSION = 48
    12  const LTC_ADDR_VERSION_SCRIPT = 50
    13  
    14  // LTC signing uses different seed string
    15  func HashFromMessage(msg []byte, out []byte) {
    16  	const MessageMagic = "Litecoin Signed Message:\n"
    17  	b := new(bytes.Buffer)
    18  	btc.WriteVlen(b, uint64(len(MessageMagic)))
    19  	b.Write([]byte(MessageMagic))
    20  	btc.WriteVlen(b, uint64(len(msg)))
    21  	b.Write(msg)
    22  	btc.ShaHash(b.Bytes(), out)
    23  }
    24  
    25  func AddrVerPubkey(testnet bool) byte {
    26  	if !testnet {
    27  		return LTC_ADDR_VERSION
    28  	}
    29  	return btc.AddrVerPubkey(testnet)
    30  }
    31  
    32  // At some point Litecoin started using addresses with M in front (version 50) - see github issue #41
    33  func AddrVerScript(testnet bool) byte {
    34  	if !testnet {
    35  		return LTC_ADDR_VERSION_SCRIPT
    36  	}
    37  	return btc.AddrVerScript(testnet)
    38  }
    39  
    40  func NewAddrFromPkScript(scr []byte, testnet bool) (ad *btc.BtcAddr) {
    41  	ad = btc.NewAddrFromPkScript(scr, testnet)
    42  	if ad != nil && ad.Version == btc.AddrVerPubkey(false) {
    43  		ad.Version = LTC_ADDR_VERSION
    44  	}
    45  	return
    46  }
    47  
    48  func GetUnspent(addr *btc.BtcAddr) (res utxo.AllUnspentTx) {
    49  	var er error
    50  
    51  	res, er = utils.GetUnspentFromBlockchair(addr, "litecoin")
    52  	if er == nil {
    53  		return
    54  	}
    55  	println("GetUnspentFromBlockchair:", er.Error())
    56  
    57  	res, er = utils.GetUnspentFromBlockcypher(addr, "ltc")
    58  	if er == nil {
    59  		return
    60  	}
    61  	println("GetUnspentFromBlockcypher:", er.Error())
    62  
    63  	return
    64  }
    65  
    66  func verify_txid(txid *btc.Uint256, rawtx []byte) bool {
    67  	tx, _ := btc.NewTx(rawtx)
    68  	if tx == nil {
    69  		return false
    70  	}
    71  	tx.SetHash(rawtx)
    72  	return txid.Equal(&tx.Hash)
    73  }
    74  
    75  // GetTxFromWeb downloads testnet's raw transaction from a web server.
    76  func GetTxFromWeb(txid *btc.Uint256) (raw []byte) {
    77  	raw = utils.GetTxFromBlockchair(txid, "litecoin")
    78  	if raw != nil && verify_txid(txid, raw) {
    79  		return
    80  	}
    81  	println("GetTxFromBlockchair failed", len(raw), txid.String())
    82  
    83  	raw = utils.GetTxFromBlockcypher(txid, "ltc")
    84  	if raw != nil && verify_txid(txid, raw) {
    85  		return
    86  	}
    87  	println("GetTxFromBlockcypher failed", len(raw), txid.String())
    88  
    89  	return
    90  }