Skip to content

Commit

Permalink
Add new verified asset metadata fields to RpcAsset
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-if committed Apr 8, 2024
1 parent 589b7fc commit 6465a2c
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 12 deletions.
20 changes: 14 additions & 6 deletions ironfish/src/assets/assetsVerificationApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import axios, { AxiosAdapter, AxiosError, AxiosRequestConfig, AxiosResponse } fr
import url, { URL } from 'url'
import { FileSystem } from '../fileSystems'

type AssetData = {
identifier: string
export type AdditionalAssetData = {
symbol: string
decimals?: number
logoURI?: string
website?: string
}

export type VerifiedAssetMetadata = { identifier: string } & AdditionalAssetData

type GetAssetDataResponse = {
version?: number
assets: AssetData[]
assets: VerifiedAssetMetadata[]
}

type GetVerifiedAssetsRequestHeaders = {
Expand All @@ -27,7 +28,7 @@ type GetVerifiedAssetsResponseHeaders = {
}

export class VerifiedAssets {
private readonly assets: Map<string, AssetData> = new Map()
private readonly assets: Map<string, VerifiedAssetMetadata> = new Map()
private lastModified?: string

export(): ExportedVerifiedAssets {
Expand All @@ -52,6 +53,13 @@ export class VerifiedAssets {
}
return this.assets.has(assetId)
}

getAssetData(assetId: Buffer | string): VerifiedAssetMetadata | undefined {
if (!(typeof assetId === 'string')) {
assetId = assetId.toString('hex')
}
return this.assets.get(assetId)
}
}

// `ExportedVerifiedAssets` may seem redundant, given that it duplicates the
Expand All @@ -63,7 +71,7 @@ export class VerifiedAssets {
// - The `assets` field from `VerifiedAssets` is a `Map`, which is not
// properly supported by the cache serializer.
export type ExportedVerifiedAssets = {
assets: AssetData[]
assets: VerifiedAssetMetadata[]
lastModified?: string
}

Expand Down Expand Up @@ -153,7 +161,7 @@ const axiosFileAdapter =
}))
}

const sanitizedAssetData = (assetData: AssetData): AssetData => {
const sanitizedAssetData = (assetData: VerifiedAssetMetadata): VerifiedAssetMetadata => {
return {
identifier: assetData.identifier,
symbol: assetData.symbol,
Expand Down
35 changes: 34 additions & 1 deletion ironfish/src/assets/assetsVerifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { createRootLogger, Logger } from '../logger'
import { ErrorUtils } from '../utils'
import { SetIntervalToken } from '../utils'
import { Retry } from '../utils'
import { AssetsVerificationApi, VerifiedAssets } from './assetsVerificationApi'
import {
AdditionalAssetData,
AssetsVerificationApi,
VerifiedAssetMetadata,
VerifiedAssets,
} from './assetsVerificationApi'

export type AssetVerification = {
status: 'verified' | 'unverified' | 'unknown'
Expand Down Expand Up @@ -114,4 +119,32 @@ export class AssetsVerifier {
return { status: 'unverified' }
}
}

getAssetData(assetId: Buffer | string): VerifiedAssetMetadata | undefined {
return this.verifiedAssets?.getAssetData(assetId)
}

verifyWithMetadata(
assetId: Buffer | string,
):
| { verification: AssetVerification }
| ({ verification: AssetVerification } & AdditionalAssetData) {
const verification = this.verify(assetId)
if (verification.status === 'verified') {
const assetData = this.getAssetData(assetId)
if (assetData) {
return {
verification,
symbol: assetData.symbol,
decimals: assetData.decimals,
logoURI: assetData.logoURI,
website: assetData.website,
}
}
}

return {
verification,
}
}
}
2 changes: 1 addition & 1 deletion ironfish/src/rpc/routes/chain/getAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ routes.register<typeof GetAssetRequestSchema, GetAssetResponse>(
owner: asset.owner.toString('hex'),
supply: CurrencyUtils.encode(asset.supply),
status: await getAssetStatus(node, asset),
verification: node.assetsVerifier.verify(asset.id),
...node.assetsVerifier.verifyWithMetadata(asset.id),
})
},
)
8 changes: 8 additions & 0 deletions ironfish/src/rpc/routes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ export type RpcAsset = {
metadata: string
createdTransactionHash: string
verification: AssetVerification
symbol?: string
decimals?: number
logoURI?: string
website?: string
supply?: string
/**
* @deprecated query for the transaction to find it's status
Expand All @@ -100,6 +104,10 @@ export const RpcAssetSchema: yup.ObjectSchema<RpcAsset> = yup
supply: yup.string().optional(),
owner: yup.string().defined(),
createdTransactionHash: yup.string().defined(),
symbol: yup.string().optional(),
decimals: yup.number().optional(),
logoURI: yup.string().optional(),
website: yup.string().optional(),
})
.defined()

Expand Down
2 changes: 1 addition & 1 deletion ironfish/src/rpc/routes/wallet/burnAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ routes.register<typeof BurnAssetRequestSchema, BurnAssetResponse>(
nonce: asset.nonce,
creator: asset.creator.toString('hex'),
owner: asset.owner.toString('hex'),
verification: context.assetsVerifier.verify(asset.id),
status: await context.wallet.getAssetStatus(account, asset, {
confirmations: request.data.confirmations,
}),
createdTransactionHash: asset.createdTransactionHash.toString('hex'),
...context.assetsVerifier.verifyWithMetadata(asset.id),
},
transaction: await serializeRpcWalletTransaction(
context.config,
Expand Down
2 changes: 1 addition & 1 deletion ironfish/src/rpc/routes/wallet/getAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ routes.register<typeof GetWalletAssetRequestSchema, GetWalletAssetResponse>(
confirmations: request.data.confirmations,
}),
supply: asset.supply ? CurrencyUtils.encode(asset.supply) : undefined,
verification: node.assetsVerifier.verify(asset.id),
...node.assetsVerifier.verifyWithMetadata(asset.id),
})
},
)
2 changes: 1 addition & 1 deletion ironfish/src/rpc/routes/wallet/getAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ routes.register<typeof GetAssetsRequestSchema, GetAssetsResponse>(
confirmations: request.data.confirmations,
}),
supply: asset.supply !== null ? CurrencyUtils.encode(asset.supply) : undefined,
verification: node.assetsVerifier.verify(asset.id),
createdTransactionHash: asset.createdTransactionHash.toString('hex'),
...node.assetsVerifier.verifyWithMetadata(asset.id),
})
}

Expand Down
2 changes: 1 addition & 1 deletion ironfish/src/rpc/routes/wallet/mintAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ routes.register<typeof MintAssetRequestSchema, MintAssetResponse>(
nonce: asset.nonce,
creator: asset.creator.toString('hex'),
owner: asset.owner.toString('hex'),
verification: context.assetsVerifier.verify(mint.asset.id()),
status: await context.wallet.getAssetStatus(account, asset, {
confirmations: request.data.confirmations,
}),
createdTransactionHash: asset.createdTransactionHash.toString('hex'),
...context.assetsVerifier.verifyWithMetadata(mint.asset.id()),
},
transaction: await serializeRpcWalletTransaction(
context.config,
Expand Down

0 comments on commit 6465a2c

Please sign in to comment.