Skip to content

Add chain_anchor with block_timestamp to AssetLeaves RPC #1480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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: 4 additions & 0 deletions asset/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -1863,6 +1863,10 @@ type ChainAsset struct {
// tx.
AnchorBlockHeight uint32

// AnchorBlockTimestamp is the Unix timestamp of the block that mined
// the anchor tx.
AnchorBlockTimestamp int64

// AnchorOutpoint is the outpoint that commits to the asset.
AnchorOutpoint wire.OutPoint

Expand Down
1 change: 1 addition & 0 deletions proof/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ func (p *Proof) ToChainAsset() (asset.ChainAsset, error) {
AnchorBlockHash: p.BlockHeader.BlockHash(),
AnchorOutpoint: p.OutPoint(),
AnchorBlockHeight: p.BlockHeight,
AnchorBlockTimestamp: p.BlockHeader.Timestamp.Unix(),
AnchorInternalKey: p.InclusionProof.InternalKey,
AnchorMerkleRoot: merkleRoot[:],
AnchorTapscriptSibling: tsSibling,
Expand Down
22 changes: 20 additions & 2 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,13 @@ func (r *rpcServer) MarshalChainAsset(ctx context.Context, a asset.ChainAsset,
return nil, err
}

// Ensure the block timestamp is set if a block height is set.
if a.AnchorBlockTimestamp == 0 && a.AnchorBlockHeight > 0 {
a.AnchorBlockTimestamp = r.cfg.ChainBridge.GetBlockTimestamp(
ctx, a.AnchorBlockHeight,
)
}

return taprpc.MarshalChainAsset(
ctx, a, decDisplay, withWitness, keyRing,
)
Expand Down Expand Up @@ -5291,8 +5298,19 @@ func marshalAssetLeaf(ctx context.Context, keys taprpc.KeyLookup,
assetLeaf *universe.Leaf,
decDisplay fn.Option[uint32]) (*unirpc.AssetLeaf, error) {

rpcAsset, err := taprpc.MarshalAsset(
ctx, assetLeaf.Asset, false, true, keys, decDisplay,
// Decode the single proof to extract on-chain anchor info.
p, err := proof.Decode(assetLeaf.RawProof)
if err != nil {
return nil, err
}
chainAsset, err := p.ToChainAsset()
if err != nil {
return nil, err
}

// Marshal as a chain asset to include chain_anchor details.
rpcAsset, err := taprpc.MarshalChainAsset(
ctx, chainAsset, decDisplay, true, keys,
)
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions taprpc/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ func MarshalChainAsset(ctx context.Context, a asset.ChainAsset,
MerkleRoot: a.AnchorMerkleRoot,
TapscriptSibling: a.AnchorTapscriptSibling,
BlockHeight: a.AnchorBlockHeight,
BlockTimestamp: a.AnchorBlockTimestamp,
}

if a.AnchorLeaseOwner != [32]byte{} {
Expand Down
1,928 changes: 970 additions & 958 deletions taprpc/taprootassets.pb.go

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions taprpc/taprootassets.proto
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ message AnchorInfo {

// The height of the block which contains the anchor transaction.
uint32 block_height = 8;

// The UTC Unix timestamp of the block containing the anchor transaction.
int64 block_timestamp = 9;
}

message GenesisInfo {
Expand Down
5 changes: 5 additions & 0 deletions taprpc/taprootassets.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,11 @@
"type": "integer",
"format": "int64",
"description": "The height of the block which contains the anchor transaction."
},
"block_timestamp": {
"type": "string",
"format": "int64",
"description": "The UTC Unix timestamp of the block containing the anchor transaction."
}
}
},
Expand Down
5 changes: 5 additions & 0 deletions taprpc/universerpc/universe.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,11 @@
"type": "integer",
"format": "int64",
"description": "The height of the block which contains the anchor transaction."
},
"block_timestamp": {
"type": "string",
"format": "int64",
"description": "The UTC Unix timestamp of the block containing the anchor transaction."
}
}
},
Expand Down
Loading