Skip to content

Commit

Permalink
itest: update SealBatch RPC call to accept a signed PSBT
Browse files Browse the repository at this point in the history
This commit updates the SealBatch RPC endpoint call to pass a
complete signed PSBT directly, instead of extracting the witness
and passing it as a separate argument.
  • Loading branch information
ffranr committed Jan 13, 2025
1 parent b64d01a commit ad2028f
Showing 1 changed file with 6 additions and 87 deletions.
93 changes: 6 additions & 87 deletions itest/mint_fund_seal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import (
"bytes"
"context"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"log"
"math"
"testing"

Expand Down Expand Up @@ -571,25 +567,16 @@ func testMintExternalGroupKeyChantools(t *harnessTest) {
var genesisAssetID asset.ID
copy(genesisAssetID[:], genesisAssetIDBytes)

// Extract witness from signed PSBT.
witnessStack, err := DeserializeWitnessStack(
signedPsbt.Inputs[0].FinalScriptWitness,
)
if err != nil {
log.Fatalf("Failed to deserialize witness stack: %v", err)
}

groupWitness := taprpc.GroupWitness{
GenesisId: genesisAssetID[:],
Witness: witnessStack,
}

ctxt, cancel = context.WithTimeout(ctxb, defaultWaitTimeout)
defer cancel()

// Encode the signed PSBT as a string.
signedPsbtStr, err := signedPsbt.B64Encode()
require.NoError(t.t, err)

sealReq := mintrpc.SealBatchRequest{
GroupWitnesses: []*taprpc.GroupWitness{
&groupWitness,
SignedGroupVirtualPsbts: []string{
signedPsbtStr,
},
}
sealResp, err := t.tapd.SealBatch(ctxt, &sealReq)
Expand Down Expand Up @@ -619,74 +606,6 @@ func testMintExternalGroupKeyChantools(t *harnessTest) {
t.Logf("Batch assets: %v", batchAssets)
}

// DeserializeWitnessStack deserializes a serialized witness stack into a
// [][]byte.
func DeserializeWitnessStack(serialized []byte) ([][]byte, error) {
var stack [][]byte
reader := bytes.NewReader(serialized)

// Read the number of witness elements (compact size integer)
count, err := readCompactSize(reader)
if err != nil {
return nil, fmt.Errorf("failed to read witness count: %w", err)
}

// Read each witness element
for i := uint64(0); i < count; i++ {
elementSize, err := readCompactSize(reader)
if err != nil {
return nil, fmt.Errorf("failed to read witness "+
"element size: %w", err)
}

// Read the witness element data
element := make([]byte, elementSize)
if _, err := reader.Read(element); err != nil {
return nil, fmt.Errorf("failed to read witness "+
"element data: %w", err)
}
stack = append(stack, element)
}

return stack, nil
}

// readCompactSize reads a compact size integer from the given reader.
func readCompactSize(r *bytes.Reader) (uint64, error) {
if r.Len() == 0 {
return 0, errors.New("unexpected EOF")
}

prefix, _ := r.ReadByte()
switch {
case prefix < 253:
return uint64(prefix), nil
case prefix == 253:
var value uint16
err := binary.Read(r, binary.LittleEndian, &value)
if err != nil {
return 0, err
}
return uint64(value), nil
case prefix == 254:
var value uint32
err := binary.Read(r, binary.LittleEndian, &value)
if err != nil {
return 0, err
}
return uint64(value), nil
case prefix == 255:
var value uint64
err := binary.Read(r, binary.LittleEndian, &value)
if err != nil {
return 0, err
}
return value, nil
default:
return 0, errors.New("invalid compact size prefix")
}
}

// Derive a random key on an LND node, with a key family not matching the
// Taproot Assets key family.
func deriveRandomKey(t *testing.T, ctxt context.Context,
Expand Down

0 comments on commit ad2028f

Please sign in to comment.