Skip to content

Commit

Permalink
Move verified metadata fields to the verification sub-object
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-if committed Apr 8, 2024
1 parent 6465a2c commit a299b09
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 54 deletions.
7 changes: 0 additions & 7 deletions ironfish/src/assets/assetsVerificationApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ export class VerifiedAssets {
return verifiedAssets
}

isVerified(assetId: Buffer | string): boolean {
if (!(typeof assetId === 'string')) {
assetId = assetId.toString('hex')
}
return this.assets.has(assetId)
}

getAssetData(assetId: Buffer | string): VerifiedAssetMetadata | undefined {
if (!(typeof assetId === 'string')) {
assetId = assetId.toString('hex')
Expand Down
45 changes: 14 additions & 31 deletions ironfish/src/assets/assetsVerifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {

export type AssetVerification = {
status: 'verified' | 'unverified' | 'unknown'
}
} & Partial<AdditionalAssetData>

export class AssetsVerifier {
private readonly REFRESH_INTERVAL = 6 * 60 * 60 * 1000 // 6 hours
Expand Down Expand Up @@ -108,43 +108,26 @@ export class AssetsVerifier {
return this.cache.save()
}

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

verify(assetId: Buffer | string): AssetVerification {
if (!this.verifiedAssets) {
return { status: 'unknown' }
}

if (this.verifiedAssets.isVerified(assetId)) {
return { status: 'verified' }
const assetData = this.getAssetData(assetId)
if (assetData) {
return {
status: 'verified',
symbol: assetData.symbol,
decimals: assetData.decimals,
logoURI: assetData.logoURI,
website: assetData.website,
}
} else {
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),
...node.assetsVerifier.verifyWithMetadata(asset.id),
verification: node.assetsVerifier.verify(asset.id),
})
},
)
16 changes: 7 additions & 9 deletions ironfish/src/rpc/routes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ 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 @@ -98,16 +94,18 @@ export const RpcAssetSchema: yup.ObjectSchema<RpcAsset> = yup
nonce: yup.number().required(),
creator: yup.string().required(),
verification: yup
.object({ status: yup.string().oneOf(['verified', 'unverified', 'unknown']).defined() })
.object({
status: yup.string().oneOf(['verified', 'unverified', 'unknown']).defined(),
symbol: yup.string().optional(),
decimals: yup.number().optional(),
logoURI: yup.string().optional(),
website: yup.string().optional(),
})
.defined(),
status: yup.string().defined(),
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 @@ -109,7 +109,7 @@ routes.register<typeof BurnAssetRequestSchema, BurnAssetResponse>(
confirmations: request.data.confirmations,
}),
createdTransactionHash: asset.createdTransactionHash.toString('hex'),
...context.assetsVerifier.verifyWithMetadata(asset.id),
verification: context.assetsVerifier.verify(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,
...node.assetsVerifier.verifyWithMetadata(asset.id),
verification: node.assetsVerifier.verify(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 @@ -52,7 +52,7 @@ routes.register<typeof GetAssetsRequestSchema, GetAssetsResponse>(
}),
supply: asset.supply !== null ? CurrencyUtils.encode(asset.supply) : undefined,
createdTransactionHash: asset.createdTransactionHash.toString('hex'),
...node.assetsVerifier.verifyWithMetadata(asset.id),
verification: node.assetsVerifier.verify(asset.id),
})
}

Expand Down
2 changes: 1 addition & 1 deletion ironfish/src/rpc/routes/wallet/getBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ routes.register<typeof GetBalanceRequestSchema, GetBalanceResponse>(
request.end({
account: account.name,
assetId: assetId.toString('hex'),
assetVerification: node.assetsVerifier.verify(assetId),
assetVerification: { status: node.assetsVerifier.verify(assetId).status },
confirmed: balance.confirmed.toString(),
unconfirmed: balance.unconfirmed.toString(),
unconfirmedCount: balance.unconfirmedCount,
Expand Down
2 changes: 1 addition & 1 deletion ironfish/src/rpc/routes/wallet/getBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ routes.register<typeof GetBalancesRequestSchema, GetBalancesResponse>(
assetName: asset?.name.toString('hex') ?? '',
assetCreator: asset?.creator.toString('hex') ?? '',
assetOwner: asset?.owner.toString('hex') ?? '',
assetVerification: context.assetsVerifier.verify(balance.assetId),
assetVerification: { status: context.assetsVerifier.verify(balance.assetId).status },
blockHash: balance.blockHash?.toString('hex') ?? null,
confirmed: CurrencyUtils.encode(balance.confirmed),
sequence: balance.sequence,
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 @@ -147,7 +147,7 @@ routes.register<typeof MintAssetRequestSchema, MintAssetResponse>(
confirmations: request.data.confirmations,
}),
createdTransactionHash: asset.createdTransactionHash.toString('hex'),
...context.assetsVerifier.verifyWithMetadata(mint.asset.id()),
verification: context.assetsVerifier.verify(mint.asset.id()),
},
transaction: await serializeRpcWalletTransaction(
context.config,
Expand Down

0 comments on commit a299b09

Please sign in to comment.