btcsuite / btcd

An alternative full node bitcoin implementation written in Go (golang)
https://github.com/btcsuite/btcd/blob/master/README.md
ISC License
6.27k stars 2.37k forks source link

I am unable to perform "go get github.com/btcsuite/btcd/btcec" because it was changed #1962

Closed visualbasic6 closed 1 year ago

visualbasic6 commented 1 year ago
root@1986:~/btc# go run btc.go
../go/pkg/mod/github.com/btcsuite/btcutil@v1.0.3-0.20201208143702-a53e38424cce/address.go:14:2: no required module provides package github.com/btcsuite/btcd/btcec; to add it:
        go get github.com/btcsuite/btcd/btcec

root@1986:~/btc# go get github.com/btcsuite/btcd/btcec
go: module github.com/btcsuite/btcd@upgrade found (v0.23.4), but does not contain package github.com/btcsuite/btcd/btcec
root@1986:~/btc# cat go.mod
module myproject

go 1.20

require github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce

require (
        github.com/aead/siphash v1.0.1 // indirect
        github.com/btcsuite/btcd v0.23.4 // indirect
        github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
        github.com/btcsuite/btcd/btcutil v1.1.3 // indirect
        github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 // indirect
        github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f // indirect
        github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd // indirect
        github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792 // indirect
        github.com/btcsuite/winsvc v1.0.0 // indirect
        github.com/davecgh/go-spew v1.1.1 // indirect
        github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect
        github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
        github.com/decred/dcrd/lru v1.1.1 // indirect
        github.com/golang/snappy v0.0.4 // indirect
        github.com/jessevdk/go-flags v1.5.0 // indirect
        github.com/jrick/logrotate v1.0.0 // indirect
        github.com/kkdai/bstream v1.0.0 // indirect
        github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
        golang.org/x/crypto v0.7.0 // indirect
        golang.org/x/sys v0.6.0 // indirect
)

root@1986:~/btc#

I am trying to create a light client that retrieves a block from a remote node over P2P.

package main

import (
    "bytes"
    "encoding/hex"
    "errors"
    "fmt"
    "io"
    "strconv"

    "github.com/btcsuite/btcd/btcec/v2"
    "github.com/btcsuite/btcutil"
)

const (
    protocolVersion = 70015
)

var (
    magic = [4]byte{0xf9, 0xbe, 0xb4, 0xd9}
)

// Reverse the byte order of a slice of bytes
func reverseBytes(data []byte) []byte {
    for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 {
        data[i], data[j] = data[j], data[i]
    }
    return data
}

// Convert a hexadecimal string to a slice of bytes
func decodeHex(hexString string) []byte {
    decoded, err := hex.DecodeString(hexString)
    if err != nil {
        panic(err)
    }
    return decoded
}

// Serialize a struct into a byte slice
func serialize(v interface{}) []byte {
    var buf bytes.Buffer
    err := binary.Write(&buf, binary.LittleEndian, v)
    if err != nil {
        panic(err)
    }
    return buf.Bytes()
}

// Deserialize a block header from its serialized bytes
func deserializeHeader(serialized []byte) (*btcutil.BlockHeader, int, error) {
    header := &btcutil.BlockHeader{}

    reader := bytes.NewReader(serialized)

    // Read the version field
    err := binary.Read(reader, binary.LittleEndian, &header.Version)
    if err != nil {
        return nil, 0, err
    }

    // Read the previous block hash field
    _, err = reader.Read(header.PrevBlockHash[:])
    if err != nil {
        return nil, 0, err
    }

    // Read the merkle root field
    _, err = reader.Read(header.MerkleRoot[:])
    if err != nil {
        return nil, 0, err
    }

    // Read the timestamp field
    err = binary.Read(reader, binary.LittleEndian, &header.Timestamp)
    if err != nil {
        return nil, 0, err
    }

    // Read the difficulty target field
    err = binary.Read(reader, binary.LittleEndian, &header.DifficultyTarget)
    if err != nil {
        return nil, 0, err
    }

    // Read the nonce field
    err = binary.Read(reader, binary.LittleEndian, &header.Nonce)
    if err != nil {
        return nil, 0, err
    }

    return header, 80, nil
}

// Deserialize a transaction from its serialized bytes
func deserializeTransaction(serialized []byte) (*btcutil.Tx, int, error) {
    transaction := &btcutil.Tx{}

    reader := bytes.NewReader(serialized)

    // Read the version field
    var version int32
    err := binary.Read(reader, binary.LittleEndian, &version)
    if err != nil {
        return nil, 0, err
    }
    transaction.SetVersion(version)

    // Read the inputs count field
    inputsCount, _, err := readVarInt(reader)
    if err != nil {
        return nil, 0, err
    }

    // Read the inputs
    inputs := make([]*btcutil.TxIn, inputsCount)
    for i := uint64(0); i < inputsCount; i++ {
        input, bytesRead, err := deserializeInput(reader)
        if err != nil {
            return nil, 0, err
        }
        inputs[i] = input
        serialized = serialized[bytesRead:]
    }

    // Read the outputs count field
    outputsCount, bytesRead, err := readVarint(reader)
    if err != nil {
        return nil, 0, err
    }

    // Read the outputs
    outputs := make([]*btcutil.TxOut, outputsCount)
    for i := uint64(0); i < outputsCount; i++ {
        output, bytesRead, err := deserializeOutput(reader)
        if err != nil {
            return nil, 0, err
        }
        outputs[i] = output
        serialized = serialized[bytesRead:]
    }

    // Read the locktime field
    var lockTime uint32
    err = binary.Read(reader, binary.LittleEndian, &lockTime)
    if err != nil {
        return nil, 0, err
    }

    return &btcutil.MsgTx{
        Version:  version,
        TxIn:     inputs,
        TxOut:    outputs,
        LockTime: lockTime,
    }, len(serialized), nil
}

But this error is persistent:

root@1986:~/btc# go run btc.go
../go/pkg/mod/github.com/btcsuite/btcutil@v1.0.3-0.20201208143702-a53e38424cce/address.go:14:2: no required module provides package github.com/btcsuite/btcd/btcec; to add it:
        go get github.com/btcsuite/btcd/btcec

Furthermore:

root@1986:~/btc# go mod tidy
go: finding module for package github.com/btcsuite/btcd/btcec
myproject imports
        github.com/btcsuite/btcutil imports
        github.com/btcsuite/btcd/btcec: module github.com/btcsuite/btcd@latest found (v0.23.4), but does not contain package github.com/btcsuite/btcd/btcec

wat do

guggero commented 1 year ago

You need to use github.com/btcsuite/btcd/btcutil instead of github.com/btcsuite/btcutil in your import. And use at least the versions of the packages used here: https://github.com/btcsuite/btcd/blob/master/go.mod