Skip to content

Commit eb3ed93

Browse files
authored
Merge pull request #765 from lightninglabs/tap-channels-prep-1
[tap channels preparation 1/?]: Add QueryInternalKey and QueryScriptKey RPCs, pre-fill script key info in vPSBT
2 parents cd916e7 + 3e7d61d commit eb3ed93

File tree

22 files changed

+1304
-170
lines changed

22 files changed

+1304
-170
lines changed

address/address.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ var (
6060
// the local database.
6161
ErrScriptKeyNotFound = errors.New("script key not found")
6262

63+
// ErrInternalKeyNotFound is returned when an internal key is not found
64+
// in the local database.
65+
ErrInternalKeyNotFound = errors.New("internal key not found")
66+
6367
// ErrUnknownVersion is returned when encountering an address with an
6468
// unrecognised version number.
6569
ErrUnknownVersion = errors.New("address: unknown version number")

asset/asset.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,10 @@ func (w *Witness) Decode(r io.Reader) error {
426426
return stream.Decode(r)
427427
}
428428

429-
// DeepEqual returns true if this witness is equal with the given witness.
430-
func (w *Witness) DeepEqual(o *Witness) bool {
429+
// DeepEqual returns true if this witness is equal with the given witness. If
430+
// the skipTxWitness boolean is set, the TxWitness field of the Witness is not
431+
// compared.
432+
func (w *Witness) DeepEqual(skipTxWitness bool, o *Witness) bool {
431433
if w == nil || o == nil {
432434
return w == o
433435
}
@@ -436,11 +438,17 @@ func (w *Witness) DeepEqual(o *Witness) bool {
436438
return false
437439
}
438440

439-
if !reflect.DeepEqual(w.TxWitness, o.TxWitness) {
441+
if !w.SplitCommitment.DeepEqual(o.SplitCommitment) {
440442
return false
441443
}
442444

443-
return w.SplitCommitment.DeepEqual(o.SplitCommitment)
445+
// If we're not comparing the TxWitness, we're done. This might be
446+
// useful when comparing witnesses of segregated witness version assets.
447+
if skipTxWitness {
448+
return true
449+
}
450+
451+
return reflect.DeepEqual(w.TxWitness, o.TxWitness)
444452
}
445453

446454
// ScriptVersion denotes the asset script versioning scheme.
@@ -1398,6 +1406,20 @@ func (a *Asset) Copy() *Asset {
13981406

13991407
// DeepEqual returns true if this asset is equal with the given asset.
14001408
func (a *Asset) DeepEqual(o *Asset) bool {
1409+
return a.deepEqual(false, o)
1410+
}
1411+
1412+
// DeepEqualAllowSegWitIgnoreTxWitness returns true if this asset is equal with
1413+
// the given asset, ignoring the TxWitness field of the Witness if the asset
1414+
// version is v1.
1415+
func (a *Asset) DeepEqualAllowSegWitIgnoreTxWitness(o *Asset) bool {
1416+
return a.deepEqual(true, o)
1417+
}
1418+
1419+
// deepEqual returns true if this asset is equal with the given asset. The
1420+
// allowSegWitIgnoreTxWitness flag is used to determine whether the TxWitness
1421+
// field of the Witness should be ignored if the asset version is v1.
1422+
func (a *Asset) deepEqual(allowSegWitIgnoreTxWitness bool, o *Asset) bool {
14011423
if a.Version != o.Version {
14021424
return false
14031425
}
@@ -1437,7 +1459,9 @@ func (a *Asset) DeepEqual(o *Asset) bool {
14371459
}
14381460

14391461
for i := range a.PrevWitnesses {
1440-
if !a.PrevWitnesses[i].DeepEqual(&o.PrevWitnesses[i]) {
1462+
oPrevWitness := &o.PrevWitnesses[i]
1463+
skipTxWitness := a.Version == V1 && allowSegWitIgnoreTxWitness
1464+
if !a.PrevWitnesses[i].DeepEqual(skipTxWitness, oPrevWitness) {
14411465
return false
14421466
}
14431467
}

itest/assertions.go

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import (
2020
"github.com/lightninglabs/taproot-assets/fn"
2121
"github.com/lightninglabs/taproot-assets/proof"
2222
"github.com/lightninglabs/taproot-assets/taprpc"
23+
wrpc "github.com/lightninglabs/taproot-assets/taprpc/assetwalletrpc"
2324
"github.com/lightninglabs/taproot-assets/taprpc/mintrpc"
25+
"github.com/lightninglabs/taproot-assets/taprpc/tapdevrpc"
2426
unirpc "github.com/lightninglabs/taproot-assets/taprpc/universerpc"
2527
"github.com/lightninglabs/taproot-assets/universe"
2628
"github.com/lightningnetwork/lnd/lnrpc/chainrpc"
@@ -36,6 +38,16 @@ var (
3638
statusCompleted = taprpc.AddrEventStatus_ADDR_EVENT_STATUS_COMPLETED
3739
)
3840

41+
// tapClient is an interface that covers all currently available RPC interfaces
42+
// a client should implement.
43+
type tapClient interface {
44+
taprpc.TaprootAssetsClient
45+
wrpc.AssetWalletClient
46+
tapdevrpc.TapDevClient
47+
mintrpc.MintClient
48+
unirpc.UniverseClient
49+
}
50+
3951
// AssetCheck is a function type that checks an RPC asset's property.
4052
type AssetCheck func(a *taprpc.Asset) error
4153

@@ -593,7 +605,7 @@ func VerifyProofBlob(t *testing.T, tapClient taprpc.TaprootAssetsClient,
593605

594606
// AssertAddrCreated makes sure an address was created correctly for the given
595607
// asset.
596-
func AssertAddrCreated(t *testing.T, client taprpc.TaprootAssetsClient,
608+
func AssertAddrCreated(t *testing.T, client tapClient,
597609
expected *taprpc.Asset, actual *taprpc.Addr) {
598610

599611
// Was the address created correctly?
@@ -631,6 +643,43 @@ func AssertAddrCreated(t *testing.T, client taprpc.TaprootAssetsClient,
631643

632644
// Does the address in the list contain all information we expect?
633645
AssertAddr(t, expected, rpcAddr)
646+
647+
// We also make sure we can query the script and internal keys of the
648+
// address correctly.
649+
scriptKeyResp, err := client.QueryScriptKey(
650+
ctxt, &wrpc.QueryScriptKeyRequest{
651+
TweakedScriptKey: actual.ScriptKey,
652+
},
653+
)
654+
require.NoError(t, err)
655+
require.NotNil(t, scriptKeyResp.ScriptKey)
656+
require.NotNil(t, scriptKeyResp.ScriptKey.KeyDesc)
657+
require.NotNil(t, scriptKeyResp.ScriptKey.KeyDesc.KeyLoc)
658+
require.EqualValues(
659+
t, asset.TaprootAssetsKeyFamily,
660+
scriptKeyResp.ScriptKey.KeyDesc.KeyLoc.KeyFamily,
661+
)
662+
require.NotEqual(
663+
t, scriptKeyResp.ScriptKey.PubKey,
664+
scriptKeyResp.ScriptKey.KeyDesc.RawKeyBytes,
665+
)
666+
667+
internalKeyResp, err := client.QueryInternalKey(
668+
ctxt, &wrpc.QueryInternalKeyRequest{
669+
InternalKey: actual.InternalKey,
670+
},
671+
)
672+
require.NoError(t, err)
673+
require.NotNil(t, internalKeyResp.InternalKey)
674+
require.NotNil(t, internalKeyResp.InternalKey.KeyLoc)
675+
require.EqualValues(
676+
t, asset.TaprootAssetsKeyFamily,
677+
internalKeyResp.InternalKey.KeyLoc.KeyFamily,
678+
)
679+
require.Equal(
680+
t, actual.InternalKey,
681+
internalKeyResp.InternalKey.RawKeyBytes,
682+
)
634683
}
635684

636685
// AssertAddrEvent makes sure the given address was detected by the given
@@ -850,11 +899,8 @@ func AssertAddr(t *testing.T, expected *taprpc.Asset, actual *taprpc.Addr) {
850899
if expected.AssetGroup == nil {
851900
require.Nil(t, actual.GroupKey)
852901
} else {
853-
// TODO(guggero): Address 33-byte vs. 32-byte issue in encoded
854-
// address vs. database.
855902
require.Equal(
856-
t, expected.AssetGroup.TweakedGroupKey[1:],
857-
actual.GroupKey[1:],
903+
t, expected.AssetGroup.TweakedGroupKey, actual.GroupKey,
858904
)
859905
}
860906

@@ -863,7 +909,7 @@ func AssertAddr(t *testing.T, expected *taprpc.Asset, actual *taprpc.Addr) {
863909
require.NotEqual(t, expected.ScriptKey, actual.ScriptKey)
864910
}
865911

866-
// assertEqualAsset asserts that two taprpc.Asset objects are equal, ignoring
912+
// AssertAsset asserts that two taprpc.Asset objects are equal, ignoring
867913
// node-specific fields like if script keys are local, if the asset is spent,
868914
// or if the anchor information is populated.
869915
func AssertAsset(t *testing.T, expected, actual *taprpc.Asset) {

itest/loadtest/utils.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/lightninglabs/taproot-assets/taprpc"
1818
"github.com/lightninglabs/taproot-assets/taprpc/assetwalletrpc"
1919
"github.com/lightninglabs/taproot-assets/taprpc/mintrpc"
20+
"github.com/lightninglabs/taproot-assets/taprpc/tapdevrpc"
2021
"github.com/lightninglabs/taproot-assets/taprpc/universerpc"
2122
"github.com/lightningnetwork/lnd/macaroons"
2223
"github.com/stretchr/testify/require"
@@ -34,9 +35,10 @@ var (
3435
type rpcClient struct {
3536
cfg *TapConfig
3637
taprpc.TaprootAssetsClient
37-
universerpc.UniverseClient
38-
mintrpc.MintClient
3938
assetwalletrpc.AssetWalletClient
39+
tapdevrpc.TapDevClient
40+
mintrpc.MintClient
41+
universerpc.UniverseClient
4042
}
4143

4244
// assetIDWithBalance returns the asset ID of an asset that has at least the
@@ -166,16 +168,18 @@ func getTapClient(t *testing.T, ctx context.Context,
166168
require.NoError(t, err)
167169

168170
assetsClient := taprpc.NewTaprootAssetsClient(conn)
169-
universeClient := universerpc.NewUniverseClient(conn)
170-
mintMintClient := mintrpc.NewMintClient(conn)
171171
assetWalletClient := assetwalletrpc.NewAssetWalletClient(conn)
172+
devClient := tapdevrpc.NewTapDevClient(conn)
173+
mintMintClient := mintrpc.NewMintClient(conn)
174+
universeClient := universerpc.NewUniverseClient(conn)
172175

173176
client := &rpcClient{
174177
cfg: cfg,
175178
TaprootAssetsClient: assetsClient,
176-
UniverseClient: universeClient,
177-
MintClient: mintMintClient,
178179
AssetWalletClient: assetWalletClient,
180+
TapDevClient: devClient,
181+
MintClient: mintMintClient,
182+
UniverseClient: universeClient,
179183
}
180184

181185
t.Cleanup(func() {

perms/perms.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ var (
108108
Entity: "assets",
109109
Action: "write",
110110
}},
111+
"/assetwalletrpc.AssetWallet/QueryInternalKey": {{
112+
Entity: "assets",
113+
Action: "read",
114+
}},
115+
"/assetwalletrpc.AssetWallet/QueryScriptKey": {{
116+
Entity: "assets",
117+
Action: "read",
118+
}},
111119
"/assetwalletrpc.AssetWallet/ProveAssetOwnership": {{
112120
Entity: "assets",
113121
Action: "write",

proof/append.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,13 @@ func CreateTransitionProof(prevOut wire.OutPoint,
166166
}
167167

168168
// Make sure the committed asset matches the root asset exactly.
169-
if !committedRoot.DeepEqual(rootAsset) {
169+
// We allow the TxWitness to mismatch for assets with version 1
170+
// as they would not include the witness when the proof is
171+
// created.
172+
if !committedRoot.DeepEqualAllowSegWitIgnoreTxWitness(
173+
rootAsset,
174+
) {
175+
170176
return nil, fmt.Errorf("root asset mismatch")
171177
}
172178

0 commit comments

Comments
 (0)