Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion arkrpc/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ Proto source: `arkrpc/ark.proto`, `arkrpc/indexer.proto`.
## Key Types

- `TreePath` / `TreePathNode` / `TxOut` — Structured proto messages for the
VTXO commitment tree path.
VTXO commitment tree path. `TreePath.batch_asset_root` carries the optional
32-byte Taproot Asset commitment sibling hash composed beside
`sweep_tapscript_root` in `batch_output` for asset trees.
- `TreePathFromTree` / `TreePathToTree` — Lossless conversion between
`tree.Tree` and `arkrpc.TreePath`. Uses deterministic pre-order flattening
with sorted child indices. Re-exported under the narrower
Expand Down
4 changes: 3 additions & 1 deletion arkrpc/CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ Proto source: `arkrpc/ark.proto`, `arkrpc/indexer.proto`.
## Key Types

- `TreePath` / `TreePathNode` / `TxOut` — Structured proto messages for the
VTXO commitment tree path.
VTXO commitment tree path. `TreePath.batch_asset_root` carries the optional
32-byte Taproot Asset commitment sibling hash composed beside
`sweep_tapscript_root` in `batch_output` for asset trees.
- `TreePathFromTree` / `TreePathToTree` — Lossless conversion between
`tree.Tree` and `arkrpc.TreePath`. Uses deterministic pre-order flattening
with sorted child indices. Re-exported under the narrower
Expand Down
20 changes: 16 additions & 4 deletions arkrpc/indexer.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions arkrpc/indexer.proto
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ message TreePath {
// sweep_tapscript_root is the tapscript root hash for tweaking branch
// outputs. Empty for connector trees.
bytes sweep_tapscript_root = 4;

// batch_asset_root is the optional 32-byte Taproot Asset commitment leaf
// hash composed beside sweep_tapscript_root in batch_output. Empty for
// Bitcoin-only trees.
bytes batch_asset_root = 5;
}

// AncestryPath describes one rooted VTXO tree fragment that contributes
Expand Down
17 changes: 17 additions & 0 deletions arkrpc/tree_path_convert.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package arkrpc

import (
"bytes"
"fmt"
"sort"

Expand Down Expand Up @@ -30,11 +31,19 @@ func TreePathFromTree(t *tree.Tree) (*TreePath, error) {
return nil, err
}

if len(t.BatchAssetRoot) != 0 &&
len(t.BatchAssetRoot) != chainhash.HashSize {
return nil, fmt.Errorf("batch asset root must be %d "+
"bytes, got %d", chainhash.HashSize,
len(t.BatchAssetRoot))
}

return &TreePath{
Nodes: nodes,
BatchOutpoint: outpointToProto(t.BatchOutpoint),
BatchOutput: txOutToProto(t.BatchOutput),
SweepTapscriptRoot: t.SweepTapscriptRoot,
BatchAssetRoot: bytes.Clone(t.BatchAssetRoot),
}, nil
}

Expand Down Expand Up @@ -196,11 +205,19 @@ func TreePathToTree(tp *TreePath,
return nil, fmt.Errorf("batch output: %w", err)
}

if len(tp.BatchAssetRoot) != 0 &&
len(tp.BatchAssetRoot) != chainhash.HashSize {
return nil, fmt.Errorf("batch asset root must be %d "+
"bytes, got %d", chainhash.HashSize,
len(tp.BatchAssetRoot))
}

return &tree.Tree{
Root: goNodes[0],
BatchOutpoint: batchOP,
BatchOutput: batchOut,
SweepTapscriptRoot: tp.SweepTapscriptRoot,
BatchAssetRoot: bytes.Clone(tp.BatchAssetRoot),
}, nil
}

Expand Down
51 changes: 51 additions & 0 deletions arkrpc/tree_path_convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,54 @@ func TestTreePathTxOutRoundTrip(t *testing.T) {
require.Equal(t, out.PkScript, got.PkScript)
})
}

// TestTreePathBatchAssetRootRoundTrip checks that an optional 32-byte
// BatchAssetRoot round-trips through proto conversion and that non-32-byte
// roots are rejected on both directions.
func TestTreePathBatchAssetRootRoundTrip(t *testing.T) {
t.Parallel()

rootHash := make([]byte, chainhash.HashSize)
for i := range rootHash {
rootHash[i] = byte(i + 1)
}

orig := &tree.Tree{
Root: &tree.Node{
Input: wire.OutPoint{
Hash: chainhash.Hash{
0x01,
},
},
},
BatchOutpoint: wire.OutPoint{
Hash: chainhash.Hash{
0x01,
},
},
BatchOutput: &wire.TxOut{
Value: 1_000,
PkScript: []byte{
0x51,
},
},
SweepTapscriptRoot: make([]byte, chainhash.HashSize),
BatchAssetRoot: rootHash,
}

pb, err := TreePathFromTree(orig)
require.NoError(t, err)
require.Equal(t, rootHash, pb.BatchAssetRoot)

got, err := TreePathToTree(pb)
require.NoError(t, err)
require.Equal(t, rootHash, got.BatchAssetRoot)

orig.BatchAssetRoot = []byte{0x01, 0x02}
_, err = TreePathFromTree(orig)
require.ErrorContains(t, err, "batch asset root must be 32")

pb.BatchAssetRoot = []byte{0x01, 0x02}
_, err = TreePathToTree(pb)
require.ErrorContains(t, err, "batch asset root must be 32")
}
7 changes: 7 additions & 0 deletions cmd/wavecli/waveclicommands/devrpc/registry_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions db/tree_codec_asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func newAssetCodecTree(t *testing.T) *tree.Tree {
},
},
SweepTapscriptRoot: assetCodecHash("sweep"),
BatchAssetRoot: assetCodecHash("batch-asset-root"),
AssetContext: assetCtx,
}
}
Expand All @@ -90,6 +91,7 @@ func TestTreeCodecAssetRoundTrip(t *testing.T) {

decoded, err := DeserializeTree(encoded)
require.NoError(t, err)
require.Equal(t, original.BatchAssetRoot, decoded.BatchAssetRoot)
require.NotNil(t, decoded.AssetContext)
require.Equal(
t, original.AssetContext.AssetRef(),
Expand Down
4 changes: 4 additions & 0 deletions indexer/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ proofs for proof-of-control.
`ListVTXOsByScripts`. `newPolicyScope` signs the script and canonical policy
under the explicit `policy_script_scope` type. It creates no registration.
Legacy `script_scope` proofs keep their original wire format.
`TaprootScriptScope.SignerScript` is a local-only script key lookup override
for composed asset outputs: the RPC request queries the composed `PkScript`,
while `SignerScript` resolves the local uncomposed receive key that signs
the `script_scope` proof.
- `BuildListVTXOsByScriptsTaprootRequest(ctx, scopes, afterCursor []byte, limit, statusFilter)` / `ListVTXOsByScriptsTaproot(ctx, scopes, afterCursor []byte, limit, statusFilter)` — Build and execute taproot-scope-proofed `ListVTXOsByScripts` queries. `afterCursor` is an opaque `[]byte` keyset cursor passed through unchanged. The proof covers each pkScript using owner-key signatures gated on script scope.
- `BuildGetOORSessionByTxidTaprootRequest` / `GetOORSessionByTxidTaproot` — Build and execute a taproot-proofed OOR session lookup by Ark txid.
- `BuildListOORRecipientEventsByScriptTaprootRequest` / `ListOORRecipientEventsByScriptTaproot` — Build and execute a taproot-proofed listing of OOR receive events for a given pkScript.
Expand Down
4 changes: 4 additions & 0 deletions indexer/CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ proofs for proof-of-control.
`ListVTXOsByScripts`. `newPolicyScope` signs the script and canonical policy
under the explicit `policy_script_scope` type. It creates no registration.
Legacy `script_scope` proofs keep their original wire format.
`TaprootScriptScope.SignerScript` is a local-only script key lookup override
for composed asset outputs: the RPC request queries the composed `PkScript`,
while `SignerScript` resolves the local uncomposed receive key that signs
the `script_scope` proof.
- `BuildListVTXOsByScriptsTaprootRequest(ctx, scopes, afterCursor []byte, limit, statusFilter)` / `ListVTXOsByScriptsTaproot(ctx, scopes, afterCursor []byte, limit, statusFilter)` — Build and execute taproot-scope-proofed `ListVTXOsByScripts` queries. `afterCursor` is an opaque `[]byte` keyset cursor passed through unchanged. The proof covers each pkScript using owner-key signatures gated on script scope.
- `BuildGetOORSessionByTxidTaprootRequest` / `GetOORSessionByTxidTaproot` — Build and execute a taproot-proofed OOR session lookup by Ark txid.
- `BuildListOORRecipientEventsByScriptTaprootRequest` / `ListOORRecipientEventsByScriptTaproot` — Build and execute a taproot-proofed listing of OOR receive events for a given pkScript.
Expand Down
30 changes: 26 additions & 4 deletions indexer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,14 @@ type TaprootScriptScope struct {
// a valid pay-to-taproot script (OP_1 <32-byte key>).
PkScript []byte

// SignerScript optionally identifies the local receive script used to
// look up the signing key for the scope proof when the queried
// PkScript differs from the registered receive script (for example,
// when a Taproot Asset root is composed into an Ark output script).
// When empty, PkScript is used for signer key lookup. This field is
// local-only and never transmitted on the wire.
SignerScript []byte

// PolicyTemplate optionally carries the canonical policy for a
// registration-free funding query. The proof commits to this exact
// policy and output; empty preserves the existing scoped proof.
Expand All @@ -550,16 +558,29 @@ type TaprootScriptScope struct {
func (c *Client) newTaprootScope(ctx context.Context, pkScript []byte,
purpose string) (*arkrpc.ScriptScope, error) {

return c.newTaprootScopeWithSigner(ctx, pkScript, nil, purpose)
}

// newTaprootScopeWithSigner builds a ScriptScope proto for pkScript while
// looking up the proof signing key using signerScript when non-empty.
func (c *Client) newTaprootScopeWithSigner(ctx context.Context, pkScript,
signerScript []byte, purpose string) (*arkrpc.ScriptScope, error) {

if err := validateTaprootPkScript(pkScript); err != nil {
return nil, err
}
if purpose == "" {
return nil, fmt.Errorf("missing purpose")
}

lookupScript := pkScript
if len(signerScript) > 0 {
lookupScript = signerScript
}

now := time.Now()
expiresAt := now.Add(offlineReceiveProofTTL)
signerPubKey, err := proofSignerPubKey(pkScript, c.signer)
signerPubKey, err := proofSignerPubKey(lookupScript, c.signer)
if err != nil {
return nil, err
}
Expand All @@ -586,7 +607,7 @@ func (c *Client) newTaprootScope(ctx context.Context, pkScript []byte,
}

sig64, err := schnorrSigOverMessage(
ctx, msgBytes, pkScript, c.signer,
ctx, msgBytes, lookupScript, c.signer,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -672,8 +693,9 @@ func (c *Client) buildTaprootScopes(ctx context.Context,
if len(scope.PolicyTemplate) != 0 {
ss, err = c.newPolicyScope(ctx, scope, purpose)
} else {
ss, err = c.newTaprootScope(
ctx, scope.PkScript, purpose,
ss, err = c.newTaprootScopeWithSigner(
ctx, scope.PkScript, scope.SignerScript,
purpose,
)
}
if err != nil {
Expand Down
86 changes: 86 additions & 0 deletions indexer/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,3 +596,89 @@ func TestPolicyScopeCommitsTemplate(t *testing.T) {
}, "list_vtxo_events")
require.Error(t, err)
}

// scriptAwareSigner records which lookup script was requested and only signs
// when that lookup script matches expectedScript.
type scriptAwareSigner struct {
expectedScript []byte
key *btcec.PrivateKey
seenPubScripts [][]byte
seenSigScripts [][]byte
}

func (s *scriptAwareSigner) ProofPubKey(pkScript []byte) (*btcec.PublicKey,
error) {

s.seenPubScripts = append(s.seenPubScripts, bytes.Clone(pkScript))
if !bytes.Equal(pkScript, s.expectedScript) {
return nil, context.DeadlineExceeded
}

return s.key.PubKey(), nil
}

func (s *scriptAwareSigner) SignSchnorr(pkScript []byte, hash [32]byte) ([]byte,
error) {

s.seenSigScripts = append(s.seenSigScripts, bytes.Clone(pkScript))
if !bytes.Equal(pkScript, s.expectedScript) {
return nil, context.DeadlineExceeded
}

sig, err := schnorr.Sign(s.key, hash[:])
if err != nil {
return nil, err
}

return sig.Serialize(), nil
}

// TestBuildListVTXOsByScriptsTaprootRequestSignerScript verifies that a
// TaprootScriptScope with SignerScript looks up and signs with the local
// SignerScript while keeping the queried PkScript on the wire scope.
func TestBuildListVTXOsByScriptsTaprootRequestSignerScript(t *testing.T) {
t.Parallel()

key, err := btcec.NewPrivateKey()
require.NoError(t, err)

uncomposed := make([]byte, 34)
uncomposed[0] = 0x51
uncomposed[1] = 0x20
uncomposed[2] = 0xaa

composed := make([]byte, 34)
composed[0] = 0x51
composed[1] = 0x20
composed[2] = 0xbb

signer := &scriptAwareSigner{
expectedScript: uncomposed,
key: key,
}
client := New(
&recordingRPCClient{}, signer, "test-server", "client:test",
fn.None[btclog.Logger](),
)

req, err := client.BuildListVTXOsByScriptsTaprootRequest(
t.Context(),
[]TaprootScriptScope{{
PkScript: composed,
SignerScript: uncomposed,
}},
nil, 10, nil,
)
require.NoError(t, err)
require.Len(t, req.Scripts, 1)
require.Equal(t, composed, req.Scripts[0].PkScript)
require.Equal(t, [][]byte{uncomposed}, signer.seenPubScripts)
require.Equal(t, [][]byte{uncomposed}, signer.seenSigScripts)

proof := req.Scripts[0].GetTaprootSchnorr()
require.NotNil(t, proof)
sig, err := schnorr.ParseSignature(proof.Sig64)
require.NoError(t, err)
digest := chainhash.TaggedHash(proofTag(), proof.Message)
require.True(t, sig.Verify(digest[:], key.PubKey()))
}
Loading
Loading