Files
ark/common/connectors.go
Pietralberto Mazza 7f937e8418 Vars and fields renaming (#387)
* Rename asp > server

* Rename pool > round

* Consolidate naming for pubkey/prvkey vars and types

* Fix

* Fix

* Fix wasm

* Rename congestionTree > vtxoTree

* Fix wasm

* Rename payment > request

* Rename congestionTree > vtxoTree after syncing with master

* Fix Send API in SDK

* Fix wasm

* Fix wasm

* Fixes

* Fixes after review

* Fix

* Fix naming

* Fix

* Fix e2e tests
2024-11-26 15:57:16 +01:00

56 lines
1.4 KiB
Go

package common
import (
"fmt"
"github.com/vulpemventures/go-elements/elementsutil"
"github.com/vulpemventures/go-elements/psetv2"
)
func ValidateConnectors(roundTx string, connectors []string) error {
ptx, err := psetv2.NewPsetFromBase64(roundTx)
if err != nil {
return fmt.Errorf("invalid round tx: %s", err)
}
utx, err := ptx.UnsignedTx()
if err != nil {
return fmt.Errorf("invalid round tx: %s", err)
}
prevConnectorTxid := utx.TxHash().String()
prevConnectorVout := uint32(1)
for i, tx := range connectors {
ctx, err := psetv2.NewPsetFromBase64(tx)
if err != nil {
return fmt.Errorf("invalid connector tx #%d: %s", i, err)
}
utx, err := ctx.UnsignedTx()
if err != nil {
return fmt.Errorf("invalid connector tx #%d: %s", i, err)
}
if ctx.Global.InputCount != 1 {
return fmt.Errorf(
"invalid connector tx #%d: got %d inputs, expected 1",
i, ctx.Global.InputCount,
)
}
inTxid := elementsutil.TxIDFromBytes(ctx.Inputs[0].PreviousTxid)
inVout := ctx.Inputs[0].PreviousTxIndex
if inTxid != prevConnectorTxid || inVout != prevConnectorVout {
return fmt.Errorf(
"invalid connector tx #%d: got prevout %s:%d, expected %s:%d",
i, inTxid, inVout, prevConnectorTxid, prevConnectorVout,
)
}
prevConnectorTxid = utx.TxHash().String()
// if the last connector is reached, reset to 1
if i == len(connectors)-1 {
prevConnectorVout = 1
} else {
prevConnectorVout = 0
}
}
return nil
}