Skip to content

Commit

Permalink
Prunes the proof for pubkeys stored in the canopy.
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Jul 16, 2024
1 parent 4863ca1 commit 9140e90
Show file tree
Hide file tree
Showing 2 changed files with 547 additions and 5 deletions.
19 changes: 16 additions & 3 deletions clients/js/src/getAssetWithProof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
DasApiInterface,
GetAssetProofRpcResponse,
} from '@metaplex-foundation/digital-asset-standard-api';
import { MetadataArgs, TokenProgramVersion, TokenStandard } from './generated';
import { fetchMerkleTree, MetadataArgs, TokenProgramVersion, TokenStandard } from './generated';

export type AssetWithProof = {
leafOwner: PublicKey;
Expand All @@ -29,15 +29,28 @@ export type AssetWithProof = {
rpcAssetProof: GetAssetProofRpcResponse;
};

type GetAssetWithProofOptions = {
/* Define the options properties here */
truncateCanopy?: boolean;
};

export const getAssetWithProof = async (
context: Pick<Context, 'rpc'> & { rpc: DasApiInterface },
assetId: PublicKey
assetId: PublicKey,
options?: GetAssetWithProofOptions,
): Promise<AssetWithProof> => {
const [rpcAsset, rpcAssetProof] = await Promise.all([
context.rpc.getAsset(assetId),
context.rpc.getAssetProof(assetId),
]);

let { proof } = rpcAssetProof;
if (options?.truncateCanopy) {
const merkleTreeAccount = await fetchMerkleTree(context, rpcAssetProof.tree_id);
const canopyDepth = Math.log2(merkleTreeAccount.canopy.length + 2) - 1;
proof = rpcAssetProof.proof.slice(0, (canopyDepth === 0) ? undefined : -canopyDepth);
}

const collectionString = (rpcAsset.grouping ?? []).find(
(group) => group.group_key === 'collection'
)?.group_value;
Expand Down Expand Up @@ -70,7 +83,7 @@ export const getAssetWithProof = async (
creatorHash: publicKeyBytes(rpcAsset.compression.creator_hash),
nonce: rpcAsset.compression.leaf_id,
index: rpcAssetProof.node_index - 2 ** rpcAssetProof.proof.length,
proof: rpcAssetProof.proof,
proof,
metadata,
rpcAsset,
rpcAssetProof,
Expand Down
Loading

0 comments on commit 9140e90

Please sign in to comment.