Skip to content

Commit

Permalink
Experiment with symbols for delegation tokens (#558)
Browse files Browse the repository at this point in the history
* Experiment with symbols for delegation tokens

This PR experiments with symbols for delegation tokens. Two possible approaches are illustrated:

1. The extension's asset registry can be augmented with the validator-specific token info, as I did for the two main testnet validators.
2. The extension could auto-create a symbol for the delegation token, but we must be very very careful about this, because the `symbol` field is shown to users and it is potentially attacker-controlled. For instance, if we just read the label off the chain, someone could spoof another validator's identity without consequence.

However, this seems like a good place to start.

* change from "Delegated UM" to "Delegation"

This is better because it doesn't mislead the user into thinking that the number of tokens for the asset is the value of UM they represent.

* Update regex parsing + unbonding case

---------

Co-authored-by: Gabe Rodriguez <[email protected]>
  • Loading branch information
hdevalence and grod220 authored Feb 22, 2024
1 parent 8c2150b commit 209f264
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 40 deletions.
35 changes: 25 additions & 10 deletions packages/constants/assets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,34 @@ import LocalAssetRegistry from './local-asset-registry.json';
import { JsonValue } from '@bufbuild/protobuf';

export interface AssetPattens {
lpNftPattern: RegExp;
delegationTokenPattern: RegExp;
proposalNftPattern: RegExp;
unbondingTokenPattern: RegExp;
votingReceiptPattern: RegExp;
lpNft: RegExp;
delegationToken: RegExp;
proposalNft: RegExp;
unbondingToken: RegExp;
votingReceipt: RegExp;
}

export interface DelegationCaptureGroups {
id: string;
bech32IdentityKey: string;
}

export interface UnbondingCaptureGroups {
epoch: string;
id: string;
}

// Source of truth for regex patterns: https://github.com/penumbra-zone/penumbra/blob/main/crates/core/asset/src/asset/registry.rs
export const assetPatterns: AssetPattens = {
lpNftPattern: new RegExp('^lpnft_'),
delegationTokenPattern: new RegExp('^delegation_'),
proposalNftPattern: new RegExp('^proposal_'),
unbondingTokenPattern: new RegExp('^unbonding_'),
votingReceiptPattern: new RegExp('^voted_on_'),
lpNft: new RegExp(/^lpnft_/),
delegationToken: new RegExp(
/.*delegation_(?<bech32IdentityKey>penumbravalid1(?<id>[a-zA-HJ-NP-Z0-9]+))$/,
),
proposalNft: new RegExp(/^proposal_/),
unbondingToken: new RegExp(
/.*unbonding_epoch_(?<epoch>[0-9]+)_penumbravalid1(?<id>[a-zA-HJ-NP-Z0-9]+)$/,
),
votingReceipt: new RegExp(/^voted_on_/),
};

export const localAssets: Metadata[] = LocalAssetRegistry.map(a =>
Expand Down
52 changes: 52 additions & 0 deletions packages/constants/assets/local-asset-registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,57 @@
"svg": "https://raw.githubusercontent.com/giuspen/cherrytree/356649a8f84cd5068d676185937d61fc8e0450d1/icons/ct_pizza.svg"
}
]
},
{
"denomUnits": [
{
"denom": "delegation_penumbravalid18nkv0r3sfp2seleq6du5kt3mhfce3k6cqm77kj2e7mhakmyw9v9qx42a20",
"exponent": 6
},
{
"denom": "mdelegation_penumbravalid18nkv0r3sfp2seleq6du5kt3mhfce3k6cqm77kj2e7mhakmyw9v9qx42a20",
"exponent": 3
},
{
"denom": "udelegation_penumbravalid18nkv0r3sfp2seleq6du5kt3mhfce3k6cqm77kj2e7mhakmyw9v9qx42a20"
}
],
"base": "udelegation_penumbravalid18nkv0r3sfp2seleq6du5kt3mhfce3k6cqm77kj2e7mhakmyw9v9qx42a20",
"display": "delegation_penumbravalid18nkv0r3sfp2seleq6du5kt3mhfce3k6cqm77kj2e7mhakmyw9v9qx42a20",
"penumbraAssetId": {
"inner": "CwpUYIdQ9H5Dnf3oQ1l7ISeVMVahWbVNNvMA0dBSdwI="
},
"symbol": "Delegation (Penumbra Labs CI 1)",
"images": [
{
"png": "https://raw.githubusercontent.com/penumbra-zone/web/main/apps/webapp/public/favicon.png"
}
]
},
{
"denomUnits": [
{
"denom": "delegation_penumbravalid1qfxldejdhanmu302kcn5fm98q5d7d2upfhzqhaz95hyjdn82pqysqfq050",
"exponent": 6
},
{
"denom": "mdelegation_penumbravalid1qfxldejdhanmu302kcn5fm98q5d7d2upfhzqhaz95hyjdn82pqysqfq050",
"exponent": 3
},
{
"denom": "udelegation_penumbravalid1qfxldejdhanmu302kcn5fm98q5d7d2upfhzqhaz95hyjdn82pqysqfq050"
}
],
"base": "udelegation_penumbravalid1qfxldejdhanmu302kcn5fm98q5d7d2upfhzqhaz95hyjdn82pqysqfq050",
"display": "delegation_penumbravalid1qfxldejdhanmu302kcn5fm98q5d7d2upfhzqhaz95hyjdn82pqysqfq050",
"penumbraAssetId": {
"inner": "qUn70lKZ3qQlCT5gj5sakux4daiTPKj0AN6ZuuFldQU="
},
"symbol": "Delegation (Penumbra Labs CI 2)",
"images": [
{
"png": "https://raw.githubusercontent.com/penumbra-zone/web/main/apps/webapp/public/favicon.png"
}
]
}
]
1 change: 1 addition & 0 deletions packages/query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"dependencies": {
"@penumbra-zone/crypto-web": "workspace:*",
"@penumbra-zone/constants": "workspace:*",
"@penumbra-zone/wasm": "workspace:*",
"bech32": "^2.0.0",
"exponential-backoff": "^3.1.1"
Expand Down
30 changes: 10 additions & 20 deletions packages/query/src/block-processor.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { RootQuerier } from './root-querier';

import { bech32 } from 'bech32';

import { sha256Hash } from '@penumbra-zone/crypto-web';
import {
BlockProcessorInterface,
IndexedDbInterface,
ViewServerInterface,
} from '@penumbra-zone/types';
import { computePositionId, decodeSctRoot, transactionInfo } from '@penumbra-zone/wasm';

import { Metadata } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb';
import {
PositionState,
PositionState_PositionStateEnum,
Expand All @@ -28,6 +24,7 @@ import {
TransactionInfo,
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/view/v1/view_pb';
import { backOff } from 'exponential-backoff';
import { customizeSymbol } from './customize-symbol';

interface QueryClientProps {
fullViewingKey: string;
Expand Down Expand Up @@ -246,23 +243,16 @@ export class BlockProcessor implements BlockProcessorInterface {
for (const n of newNotes) {
const assetId = n.note?.value?.assetId;
if (!assetId) continue;
if (await this.indexedDb.getAssetsMetadata(assetId)) continue;

let metadata: Metadata | undefined;
metadata = await this.querier.shieldedPool.assetMetadata(assetId);

if (!metadata) {
const UNNAMED_ASSET_PREFIX = 'passet';
const denom = bech32.encode(UNNAMED_ASSET_PREFIX, bech32.toWords(assetId.inner));
metadata = new Metadata({
base: denom,
denomUnits: [{ aliases: [], denom, exponent: 0 }],
display: denom,
penumbraAssetId: assetId,
});
}

await this.indexedDb.saveAssetsMetadata(metadata);
const metadataInDb = await this.indexedDb.getAssetsMetadata(assetId);
if (metadataInDb) continue;

const metadataFromNode = await this.querier.shieldedPool.assetMetadata(assetId);

if (metadataFromNode) {
customizeSymbol(metadataFromNode);
await this.indexedDb.saveAssetsMetadata(metadataFromNode);
}
}
}

Expand Down
32 changes: 32 additions & 0 deletions packages/query/src/customize-symbol.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, test } from 'vitest';
import { Metadata } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb';
import { customizeSymbol } from './customize-symbol';

describe('Customizing metadata', () => {
test('should work for delegation token', () => {
const metadata = new Metadata({
display:
'delegation_penumbravalid1fjuj67ayaqueqxg03d65ps5aah6m39u39qeacu3zv2cw3dzxssyq3yrcez',
});
customizeSymbol(metadata);
expect(metadata.symbol).toBe('Delegated UM (fjuj67ayaqueqxg03d65ps5aa...)');
});

test('should work for unbonding token', () => {
const metadata = new Metadata({
display:
'uunbonding_epoch_29_penumbravalid1fjuj67ayaqueqxg03d65ps5aah6m39u39qeacu3zv2cw3dzxssyq3yrcez',
});
customizeSymbol(metadata);
expect(metadata.symbol).toBe('Unbonding UM, epoch 29 (fjuj67ayaqueqxg03d65ps5aa...)');
});

test('should do nothing if no matches', () => {
const metadata = new Metadata({
display: 'test_usd',
symbol: 'usdc',
});
customizeSymbol(metadata);
expect(metadata.symbol).toBe('usdc');
});
});
27 changes: 27 additions & 0 deletions packages/query/src/customize-symbol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Metadata } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb';
import {
assetPatterns,
DelegationCaptureGroups,
UnbondingCaptureGroups,
} from '@penumbra-zone/constants';

const DELEGATION_SYMBOL_LENGTH = 50 - 'delegation_penumbravalid1'.length;
const UNBONDING_SYMBOL_LENGTH = 41 - 'unbonding_epoch_'.length;

// If the metadata is for a delegation or unbonding tokens, customize its symbol.
// We can't trust the validator's self-described name, so use their validator ID (in metadata.display).
export const customizeSymbol = (metadata: Metadata) => {
const delegationMatch = assetPatterns.delegationToken.exec(metadata.display);
if (delegationMatch) {
const { id } = delegationMatch.groups as unknown as DelegationCaptureGroups;
const shortenedId = id.slice(0, DELEGATION_SYMBOL_LENGTH);
metadata.symbol = `Delegated UM (${shortenedId}...)`;
}

const unbondingMatch = assetPatterns.unbondingToken.exec(metadata.display);
if (unbondingMatch) {
const { id, epoch } = unbondingMatch.groups as unknown as UnbondingCaptureGroups;
const shortenedId = id.slice(0, UNBONDING_SYMBOL_LENGTH);
metadata.symbol = `Unbonding UM, epoch ${epoch} (${shortenedId}...)`;
}
};
10 changes: 5 additions & 5 deletions packages/router/src/grpc/view-protocol-server/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ export const assets: Impl['assets'] = async function* (req, ctx) {
}[] = [
{
includeReq: includeLpNfts,
pattern: assetPatterns.lpNftPattern,
pattern: assetPatterns.lpNft,
},
{
includeReq: includeDelegationTokens,
pattern: assetPatterns.delegationTokenPattern,
pattern: assetPatterns.delegationToken,
},
{
includeReq: includeProposalNfts,
pattern: assetPatterns.proposalNftPattern,
pattern: assetPatterns.proposalNft,
},
{
includeReq: includeUnbondingTokens,
pattern: assetPatterns.unbondingTokenPattern,
pattern: assetPatterns.unbondingToken,
},
{
includeReq: includeVotingReceiptTokens,
pattern: assetPatterns.votingReceiptPattern,
pattern: assetPatterns.votingReceipt,
},
...includeSpecificDenominations.map(d => ({
includeReq: true,
Expand Down
10 changes: 5 additions & 5 deletions packages/storage/src/indexed-db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
AddressIndex,
IdentityKey,
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/keys/v1/keys_pb';
import { assetPatterns, localAssets } from '@penumbra-zone/constants';
import { assetPatterns, DelegationCaptureGroups, localAssets } from '@penumbra-zone/constants';
import {
Position,
PositionId,
Expand Down Expand Up @@ -319,7 +319,7 @@ export class IndexedDb implements IndexedDbInterface {
for await (const assetCursor of this.db.transaction('ASSETS').store) {
const denomMetadata = Metadata.fromJson(assetCursor.value);
if (
assetPatterns.delegationTokenPattern.test(denomMetadata.display) &&
assetPatterns.delegationToken.test(denomMetadata.display) &&
denomMetadata.penumbraAssetId
) {
delegationAssets.set(uint8ArrayToHex(denomMetadata.penumbraAssetId.inner), denomMetadata);
Expand Down Expand Up @@ -354,10 +354,10 @@ export class IndexedDb implements IndexedDbInterface {
// delegation asset denom consists of prefix 'delegation_' and validator identity key in bech32m encoding
// For example, in denom 'delegation_penumbravalid12s9lanucncnyasrsqgy6z532q7nwsw3aqzzeqqas55kkpyf6lhsqs2w0zar'
// 'penumbravalid12s9lanucncnyasrsqgy6z532q7nwsw3aqzzeqas55kkpyf6lhsqs2w0zar' is validator identity key.
const bech32IdentityKey = asset?.display.replace(assetPatterns.delegationTokenPattern, '');
const regexResult = assetPatterns.delegationToken.exec(asset?.display ?? '');
if (!regexResult) throw new Error('expected delegation token identity key not present');

if (!bech32IdentityKey)
throw new Error('expected delegation token identity key not present');
const { bech32IdentityKey } = regexResult.groups as unknown as DelegationCaptureGroups;

notesForVoting.push(
new NotesForVotingResponse({
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 209f264

Please sign in to comment.