Skip to content

Commit e0631a9

Browse files
committed
rpcutils: add UnmarshalOutPoint function
1 parent d2ee0a9 commit e0631a9

File tree

3 files changed

+51
-51
lines changed

3 files changed

+51
-51
lines changed

itest/assets_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ func testAssetBalances(t *harnessTest) {
732732
},
733733
},
734734
expectError: true,
735-
expectedErrMsg: "invalid Txid",
735+
expectedErrMsg: "invalid outpoint txid",
736736
},
737737
{
738738
name: "Fail if AssetId is too short",

rpcserver.go

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,17 +1097,13 @@ func (r *rpcServer) ListAssets(ctx context.Context,
10971097
}
10981098

10991099
if req.AnchorOutpoint != nil {
1100-
txid, err := chainhash.NewHash(req.AnchorOutpoint.Txid)
1100+
op, err := rpcutils.UnmarshalOutPoint(req.AnchorOutpoint)
11011101
if err != nil {
1102-
return nil, fmt.Errorf("error parsing outpoint: %w",
1102+
return nil, fmt.Errorf("unmarshalling outpoint: %w",
11031103
err)
11041104
}
1105-
outPoint := &wire.OutPoint{
1106-
Hash: *txid,
1107-
Index: req.AnchorOutpoint.OutputIndex,
1108-
}
11091105

1110-
filters.AnchorPoint = outPoint
1106+
filters.AnchorPoint = &op
11111107
}
11121108

11131109
scriptKeyType, includeSpent, err := rpcutils.ParseScriptKeyTypeQuery(
@@ -2054,15 +2050,13 @@ func (r *rpcServer) ExportProof(ctx context.Context,
20542050
// error will be returned and the outpoint needs to be specified to
20552051
// disambiguate.
20562052
if req.Outpoint != nil {
2057-
txid, err := chainhash.NewHash(req.Outpoint.Txid)
2053+
op, err := rpcutils.UnmarshalOutPoint(req.Outpoint)
20582054
if err != nil {
2059-
return nil, fmt.Errorf("error parsing outpoint: %w",
2055+
return nil, fmt.Errorf("unmarshalling outpoint: %w",
20602056
err)
20612057
}
2062-
outPoint = &wire.OutPoint{
2063-
Hash: *txid,
2064-
Index: req.Outpoint.OutputIndex,
2065-
}
2058+
2059+
outPoint = &op
20662060
}
20672061

20682062
proofBlob, err := r.cfg.ProofArchive.FetchProof(ctx, proof.Locator{
@@ -2255,14 +2249,19 @@ func (r *rpcServer) FundVirtualPsbt(ctx context.Context,
22552249
)
22562250
for i, input := range raw.Inputs {
22572251
if input.Outpoint == nil {
2252+
// Return an error message which specifically
2253+
// refers to the index which has a corresponding
2254+
// nil outpoint.
22582255
return nil, fmt.Errorf("input at index %d has "+
22592256
"a nil Outpoint", i)
22602257
}
22612258

2262-
hash, err := chainhash.NewHash(input.Outpoint.Txid)
2259+
outPoint, err := rpcutils.UnmarshalOutPoint(
2260+
input.Outpoint,
2261+
)
22632262
if err != nil {
2264-
return nil, fmt.Errorf("input at index %d has "+
2265-
"invalid Txid: %w", i, err)
2263+
return nil, fmt.Errorf("unmarshalling "+
2264+
"outpoint: %w", err)
22662265
}
22672266

22682267
scriptKey, err := parseUserKey(input.ScriptKey)
@@ -2278,12 +2277,8 @@ func (r *rpcServer) FundVirtualPsbt(ctx context.Context,
22782277
}
22792278

22802279
// Decode the input into an asset.PrevID.
2281-
outpoint := wire.OutPoint{
2282-
Hash: *hash,
2283-
Index: input.Outpoint.OutputIndex,
2284-
}
22852280
prevID := asset.PrevID{
2286-
OutPoint: outpoint,
2281+
OutPoint: outPoint,
22872282
ID: asset.ID(input.Id),
22882283
ScriptKey: asset.ToSerialized(
22892284
scriptKey,
@@ -2970,15 +2965,13 @@ func (r *rpcServer) PublishAndLogTransfer(ctx context.Context,
29702965
FinalTx: finalTx,
29712966
}
29722967
for idx, lndOutpoint := range req.LndLockedUtxos {
2973-
hash, err := chainhash.NewHash(lndOutpoint.Txid)
2968+
op, err := rpcutils.UnmarshalOutPoint(lndOutpoint)
29742969
if err != nil {
2975-
return nil, fmt.Errorf("error parsing txid: %w", err)
2970+
return nil, fmt.Errorf("unmarshalling outpoint: %w",
2971+
err)
29762972
}
29772973

2978-
anchorTx.FundedPsbt.LockedUTXOs[idx] = wire.OutPoint{
2979-
Hash: *hash,
2980-
Index: lndOutpoint.OutputIndex,
2981-
}
2974+
anchorTx.FundedPsbt.LockedUTXOs[idx] = op
29822975
}
29832976

29842977
// We now have everything to ship the pre-anchored parcel using the
@@ -6289,15 +6282,13 @@ func (r *rpcServer) ProveAssetOwnership(ctx context.Context,
62896282
// error will be returned and the outpoint needs to be specified to
62906283
// disambiguate.
62916284
if req.Outpoint != nil {
6292-
txid, err := chainhash.NewHash(req.Outpoint.Txid)
6285+
op, err := rpcutils.UnmarshalOutPoint(req.Outpoint)
62936286
if err != nil {
6294-
return nil, fmt.Errorf("error parsing outpoint: %w",
6287+
return nil, fmt.Errorf("unmarshalling outpoint: %w",
62956288
err)
62966289
}
6297-
outPoint = &wire.OutPoint{
6298-
Hash: *txid,
6299-
Index: req.Outpoint.OutputIndex,
6300-
}
6290+
6291+
outPoint = &op
63016292
}
63026293

63036294
proofBlob, err := r.cfg.ProofArchive.FetchProof(ctx, proof.Locator{
@@ -6584,18 +6575,9 @@ func (r *rpcServer) RemoveUTXOLease(ctx context.Context,
65846575
req *wrpc.RemoveUTXOLeaseRequest) (*wrpc.RemoveUTXOLeaseResponse,
65856576
error) {
65866577

6587-
if req.Outpoint == nil {
6588-
return nil, fmt.Errorf("outpoint must be specified")
6589-
}
6590-
6591-
hash, err := chainhash.NewHash(req.Outpoint.Txid)
6578+
outPoint, err := rpcutils.UnmarshalOutPoint(req.Outpoint)
65926579
if err != nil {
6593-
return nil, fmt.Errorf("error parsing txid: %w", err)
6594-
}
6595-
6596-
outPoint := wire.OutPoint{
6597-
Hash: *hash,
6598-
Index: req.Outpoint.OutputIndex,
6580+
return nil, fmt.Errorf("unmarshaling outpoint: %w", err)
65996581
}
66006582

66016583
err = r.cfg.CoinSelect.ReleaseCoins(ctx, outPoint)
@@ -8753,15 +8735,13 @@ func (r *rpcServer) RegisterTransfer(ctx context.Context,
87538735
}
87548736
locator.ScriptKey = *scriptPubKey
87558737

8756-
hash, err := chainhash.NewHash(req.Outpoint.Txid)
8738+
op, err := rpcutils.UnmarshalOutPoint(req.Outpoint)
87578739
if err != nil {
8758-
return nil, err
8759-
}
8760-
locator.OutPoint = &wire.OutPoint{
8761-
Hash: *hash,
8762-
Index: req.Outpoint.OutputIndex,
8740+
return nil, fmt.Errorf("unmarshalling outpoint: %w", err)
87638741
}
87648742

8743+
locator.OutPoint = &op
8744+
87658745
// Before we query for the proof, we want to make sure the script key is
87668746
// already known to us. In an interactive transfer, we'd expect a script
87678747
// key to be derived on the recipient node, so it should already be

rpcutils/marshal.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/btcsuite/btcd/btcec/v2"
1111
"github.com/btcsuite/btcd/btcec/v2/schnorr"
1212
"github.com/btcsuite/btcd/btcutil/hdkeychain"
13+
"github.com/btcsuite/btcd/chaincfg/chainhash"
1314
"github.com/btcsuite/btcd/txscript"
1415
"github.com/btcsuite/btcd/wire"
1516
"github.com/lightninglabs/taproot-assets/asset"
@@ -257,6 +258,25 @@ func UnmarshalGenesisInfo(rpcGen *taprpc.GenesisInfo) (*asset.Genesis, error) {
257258
}, nil
258259
}
259260

261+
// UnmarshalOutPoint parses an outpoint from the RPC variant.
262+
func UnmarshalOutPoint(rpcOutpoint *taprpc.OutPoint) (wire.OutPoint, error) {
263+
var zero wire.OutPoint
264+
265+
if rpcOutpoint == nil {
266+
return zero, fmt.Errorf("unexpected nil RPC outpoint")
267+
}
268+
269+
txid, err := chainhash.NewHash(rpcOutpoint.Txid)
270+
if err != nil {
271+
return zero, fmt.Errorf("invalid outpoint txid: %w", err)
272+
}
273+
274+
return wire.OutPoint{
275+
Hash: *txid,
276+
Index: rpcOutpoint.OutputIndex,
277+
}, nil
278+
}
279+
260280
// UnmarshalTapscriptFullTree parses a Tapscript tree from the RPC variant.
261281
func UnmarshalTapscriptFullTree(tree *taprpc.TapscriptFullTree) (
262282
*asset.TapscriptTreeNodes, error) {

0 commit comments

Comments
 (0)