Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARCO-170: replace go-bt with go-sdk #561

Merged
merged 12 commits into from
Aug 22, 2024
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/bitcoin-sv/arc
go 1.22.5

require (
github.com/bitcoin-sv/go-sdk v1.0.0
github.com/cenkalti/backoff/v4 v4.3.0
github.com/getkin/kin-openapi v0.127.0
github.com/go-testfixtures/testfixtures/v3 v3.9.0
Expand All @@ -14,8 +15,6 @@ require (
github.com/labstack/echo/v4 v4.12.0
github.com/lib/pq v1.10.9
github.com/libsv/go-bc v0.1.29
github.com/libsv/go-bk v0.1.6
github.com/libsv/go-bt v1.0.8
github.com/libsv/go-bt/v2 v2.2.5
github.com/libsv/go-p2p v0.3.2
github.com/lmittmann/tint v1.0.5
Expand Down Expand Up @@ -93,6 +92,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/libsv/go-bk v0.1.6 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand Down
9 changes: 4 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7D
github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bitcoin-sv/go-sdk v1.0.0 h1:jAx0Ib5rtCC5eeY2h6JD/2ojSe6IYY50F4SWu78Yv34=
github.com/bitcoin-sv/go-sdk v1.0.0/go.mod h1:NOAkJLbjqKOLuxJmb9ABG86ExTZp4HS8+iygiDIUps4=
github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173 h1:2yTIV9u7H0BhRDGXH5xrAwAz7XibWJtX2dNezMeNsUo=
github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173/go.mod h1:BZ1UcC9+tmcDEcdVXgpt13hMczwJxWzpAn68wNs7zRA=
github.com/bitcoinsv/bsvutil v0.0.0-20181216182056-1d77cf353ea9 h1:hFI8rT84FCA0FFy3cFrkW5Nz4FyNKlIdCvEvvTNySKg=
Expand Down Expand Up @@ -193,8 +195,8 @@ github.com/libsv/go-bc v0.1.29 h1:w3ZnpZxLkTrjklwkr9x/y/xv0vJz5FVVZjd/gvtvGQs=
github.com/libsv/go-bc v0.1.29/go.mod h1:l6epTfcakN8YKId/hrpUzlu1QeT3ODF1MI3DeYhG1O8=
github.com/libsv/go-bk v0.1.6 h1:c9CiT5+64HRDbzxPl1v/oiFmbvWZTuUYqywCf+MBs/c=
github.com/libsv/go-bk v0.1.6/go.mod h1:khJboDoH18FPUaZlzRFKzlVN84d4YfdmlDtdX4LAjQA=
github.com/libsv/go-bt v1.0.8 h1:nWLLcnUm0dxNO3exqrL5jvAcTGkl0dsnBuQqB6+M6vQ=
github.com/libsv/go-bt v1.0.8/go.mod h1:yO023bNYLh5DwcOYl+ZqLAeTemoy6K+2UbQlIBMv+EQ=
github.com/libsv/go-bt v1.0.4 h1:2Css5lfomk/J97tM5Gk56Lp+tTK6xWYnmHNc/fGO6lE=
github.com/libsv/go-bt v1.0.4/go.mod h1:AfXoLFYEbY/TvCq/84xTce2xGjPUuC5imokHmcykF2k=
github.com/libsv/go-bt/v2 v2.2.5 h1:VoggBLMRW9NYoFujqe5bSYKqnw5y+fYfufgERSoubog=
github.com/libsv/go-bt/v2 v2.2.5/go.mod h1:cV45+jDlPOLfhJLfpLmpQoWzrIvVth9Ao2ZO1f6CcqU=
github.com/libsv/go-p2p v0.3.2 h1:O32CzkqM+jhSuleRHJln6JjL2pKH8aaRTx8lAfhIiic=
Expand Down Expand Up @@ -378,7 +380,6 @@ go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN8
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201208171446-5f87f3452ae9/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
Expand All @@ -405,7 +406,6 @@ golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -416,7 +416,6 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk=
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
Expand Down
38 changes: 23 additions & 15 deletions internal/beef/beef.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package beef

import (
"bytes"
"errors"
"fmt"

"github.com/libsv/go-bc"
"github.com/libsv/go-bt/v2"
"github.com/bitcoin-sv/go-sdk/transaction"
)

const (
Expand All @@ -25,8 +25,8 @@ const (
)

type TxData struct {
Transaction *bt.Tx
BumpIndex *bt.VarInt
Transaction *transaction.Transaction
BumpIndex *transaction.VarInt
txID string
}

Expand All @@ -43,7 +43,7 @@ func (td *TxData) GetTxID() string {
}

type BEEF struct {
BUMPs []*bc.BUMP
BUMPs []*transaction.MerklePath
Transactions []*TxData
}

Expand Down Expand Up @@ -83,31 +83,39 @@ func DecodeBEEF(beefHex []byte) (*BEEF, []byte, error) {
return decodedBeef, remainingBytes, nil
}

func (d *BEEF) GetLatestTx() *bt.Tx {
func (d *BEEF) GetLatestTx() *transaction.Transaction {
return d.Transactions[len(d.Transactions)-1].Transaction // get the last transaction as the processed transaction - it should be the last one because of khan's ordering
}

func decodeBUMPs(beefBytes []byte) ([]*bc.BUMP, []byte, error) {
func decodeBUMPs(beefBytes []byte) ([]*transaction.MerklePath, []byte, error) {
if len(beefBytes) == 0 {
return nil, nil, errors.New("cannot decode BUMP - no bytes provided")
}

nBump, bytesUsed := bt.NewVarIntFromBytes(beefBytes)
nBump, bytesUsed := transaction.NewVarIntFromBytes(beefBytes)

if nBump == 0 {
return nil, nil, errors.New("invalid BEEF - lack of BUMPs")
}

beefBytes = beefBytes[bytesUsed:]

bumps := make([]*bc.BUMP, 0, uint64(nBump))
bumps := make([]*transaction.MerklePath, 0, uint64(nBump))
for i := uint64(0); i < uint64(nBump); i++ {
bump, bytesUsed, err := bc.NewBUMPFromStream(beefBytes)
fmt.Println(len(beefBytes))
bump, err := transaction.NewMerklePathFromBinary(beefBytes)
if err != nil {
return nil, nil, err
}

beefBytes = beefBytes[bytesUsed:]
// calculate the number of bytes used to encode the bump
bumpBytes := bump.Bytes()
usedBytes := beefBytes[:len(bumpBytes)]
if !bytes.Equal(bumpBytes, usedBytes) {
return nil, nil, errors.New("beef bytes not equal")
}

beefBytes = beefBytes[len(bumpBytes):]

bumps = append(bumps, bump)
}
Expand All @@ -120,7 +128,7 @@ func decodeTransactionsWithPathIndexes(beefBytes []byte) ([]*TxData, []byte, err
return nil, nil, errors.New("invalid BEEF - no transaction")
}

nTransactions, bytesUsed := bt.NewVarIntFromBytes(beefBytes)
nTransactions, bytesUsed := transaction.NewVarIntFromBytes(beefBytes)

if nTransactions < 2 {
return nil, nil, errors.New("invalid BEEF- not enough transactions provided to decode BEEF")
Expand All @@ -131,7 +139,7 @@ func decodeTransactionsWithPathIndexes(beefBytes []byte) ([]*TxData, []byte, err
transactions := make([]*TxData, 0, int(nTransactions))

for i := 0; i < int(nTransactions); i++ {
tx, bytesUsed, err := bt.NewTxFromStream(beefBytes)
tx, bytesUsed, err := transaction.NewTransactionFromStream(beefBytes)
if err != nil {
return nil, nil, err
}
Expand All @@ -141,15 +149,15 @@ func decodeTransactionsWithPathIndexes(beefBytes []byte) ([]*TxData, []byte, err
return nil, nil, errors.New("invalid BEEF - no HasBUMP flag")
}

var pathIndex *bt.VarInt
var pathIndex *transaction.VarInt

switch beefBytes[0] {
case hasBump:
beefBytes = beefBytes[1:]
if len(beefBytes) == 0 {
return nil, nil, errors.New("invalid BEEF - HasBUMP flag set, but no BUMP index")
}
value, bytesUsed := bt.NewVarIntFromBytes(beefBytes)
value, bytesUsed := transaction.NewVarIntFromBytes(beefBytes)
pathIndex = &value
beefBytes = beefBytes[bytesUsed:]
case hasNoBump:
Expand Down
Loading
Loading