mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-11-21 10:24:15 +01:00
solana nft improvements and other fixes (#3581)
* feat: improvements covering solana nfts(send, ownership badge, persistent imports etc), closing emptied token accounts and other fixes from patch * feat: more improvements for nfts including security hardening * fix: display error in full
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
import "package:cake_wallet/entities/solana_nft_asset_model.dart";
|
||||
import "package:cake_wallet/entities/wallet_nft_response.dart";
|
||||
import "package:cw_core/imported_nft.dart";
|
||||
|
||||
class SavedNFTs {
|
||||
Future<Map<String, SolanaNFTAssetModel>> solanaNFTs(String walletName) async {
|
||||
final rows = await ImportedNFT.getAllForWallet(walletName, ImportedNFT.solanaChain);
|
||||
|
||||
return {
|
||||
for (final row in rows)
|
||||
row.identifier: SolanaNFTAssetModel(
|
||||
address: row.identifier,
|
||||
mint: row.identifier,
|
||||
name: row.name,
|
||||
symbol: row.symbol,
|
||||
description: row.description,
|
||||
imageOriginalUrl: row.imageUrl,
|
||||
isOwned: row.isOwned,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
Future<void> addSolana(String walletName, SolanaNFTAssetModel asset) async {
|
||||
final mint = asset.mint;
|
||||
|
||||
if (mint == null || mint.isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
await _rowForSolana(walletName, mint, asset).save();
|
||||
}
|
||||
|
||||
Future<void> refreshSolana(String walletName, List<SolanaNFTAssetModel> assets) async {
|
||||
for (final asset in assets) {
|
||||
final mint = asset.mint;
|
||||
|
||||
if (mint == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
await ImportedNFT.updateMetadata(_rowForSolana(walletName, mint, asset));
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<NFTAssetModel>> evmNFTs(String walletName, String chain) async {
|
||||
final rows = await ImportedNFT.getAllForWallet(walletName, chain);
|
||||
|
||||
return rows
|
||||
.map(
|
||||
(row) => NFTAssetModel(
|
||||
tokenAddress: _tokenAddressOf(row.identifier),
|
||||
tokenId: _tokenIdOf(row.identifier),
|
||||
name: row.name,
|
||||
symbol: row.symbol,
|
||||
normalizedMetadata: NormalizedMetadata(
|
||||
name: row.name,
|
||||
description: row.description,
|
||||
image: row.imageUrl,
|
||||
),
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
}
|
||||
|
||||
Future<void> addEvm(String walletName, String chain, NFTAssetModel asset) async {
|
||||
final identifier = evmIdentifier(asset);
|
||||
|
||||
if (identifier == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
await ImportedNFT(
|
||||
walletName: walletName,
|
||||
chain: chain,
|
||||
identifier: identifier,
|
||||
name: asset.name,
|
||||
symbol: asset.symbol,
|
||||
description: asset.normalizedMetadata?.description,
|
||||
imageUrl: asset.normalizedMetadata?.imageUrl,
|
||||
).save();
|
||||
}
|
||||
|
||||
Future<void> removeSolana(String walletName, String mint) =>
|
||||
ImportedNFT.deleteOne(walletName, ImportedNFT.solanaChain, mint);
|
||||
|
||||
static String? evmIdentifier(NFTAssetModel asset) {
|
||||
final address = asset.tokenAddress;
|
||||
final tokenId = asset.tokenId;
|
||||
|
||||
if (address == null || address.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return "$address:${tokenId ?? ""}";
|
||||
}
|
||||
|
||||
ImportedNFT _rowForSolana(String walletName, String mint, SolanaNFTAssetModel asset) =>
|
||||
ImportedNFT(
|
||||
walletName: walletName,
|
||||
chain: ImportedNFT.solanaChain,
|
||||
identifier: mint,
|
||||
name: asset.name,
|
||||
symbol: asset.symbol,
|
||||
description: asset.description,
|
||||
imageUrl: asset.imageOriginalUrl,
|
||||
isOwned: asset.isOwned,
|
||||
);
|
||||
|
||||
String _tokenAddressOf(String identifier) => identifier.split(":").first;
|
||||
|
||||
String _tokenIdOf(String identifier) {
|
||||
final parts = identifier.split(":");
|
||||
|
||||
return parts.length > 1 ? parts.sublist(1).join(":") : "";
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,6 @@
|
||||
import "package:cw_core/utils/ipfs_url.dart";
|
||||
import "package:cw_core/utils/nft_text.dart";
|
||||
|
||||
class SolanaNFTAssetModel {
|
||||
String? address;
|
||||
String? mint;
|
||||
@@ -12,46 +15,50 @@ class SolanaNFTAssetModel {
|
||||
Metaplex? metaplex;
|
||||
Collection? collection;
|
||||
Contract? contract;
|
||||
bool? isOwned;
|
||||
|
||||
SolanaNFTAssetModel({
|
||||
this.address,
|
||||
this.mint,
|
||||
this.standard,
|
||||
this.name,
|
||||
this.symbol,
|
||||
this.description,
|
||||
this.imageOriginalUrl,
|
||||
String? name,
|
||||
String? symbol,
|
||||
String? description,
|
||||
String? imageOriginalUrl,
|
||||
this.externalUrl,
|
||||
this.metadataOriginalUrl,
|
||||
this.totalSupply,
|
||||
this.metaplex,
|
||||
this.collection,
|
||||
this.contract,
|
||||
});
|
||||
this.isOwned,
|
||||
}) : name = sanitizeNFTText(name),
|
||||
symbol = sanitizeNFTText(symbol),
|
||||
description = sanitizeNFTText(description),
|
||||
imageOriginalUrl = tryNormalizeIpfsUrl(imageOriginalUrl);
|
||||
|
||||
factory SolanaNFTAssetModel.fromJson(Map<String, dynamic> json) {
|
||||
return SolanaNFTAssetModel(
|
||||
address: json['address'] as String?,
|
||||
mint: json['mint'] as String?,
|
||||
standard: json['standard'] as String?,
|
||||
name: json['name'] as String?,
|
||||
symbol: json['symbol'] as String?,
|
||||
description: json['description'] as String?,
|
||||
imageOriginalUrl: json['imageOriginalUrl'] as String?,
|
||||
externalUrl: json['externalUrl'] as String?,
|
||||
metadataOriginalUrl: json['metadataOriginalUrl'] as String?,
|
||||
totalSupply: json['totalSupply'] as String?,
|
||||
metaplex: json['metaplex'] != null
|
||||
? Metaplex.fromJson(json['metaplex'] as Map<String, dynamic>)
|
||||
: null,
|
||||
collection: json['collection'] != null
|
||||
? Collection.fromJson(json['collection'] as Map<String, dynamic>)
|
||||
: null,
|
||||
contract: json['contract'] != null
|
||||
? Contract.fromJson(json['contract'] as Map<String, dynamic>)
|
||||
: null,
|
||||
);
|
||||
}
|
||||
factory SolanaNFTAssetModel.fromJson(Map<String, dynamic> json) => SolanaNFTAssetModel(
|
||||
address: json["address"] as String?,
|
||||
mint: json["mint"] as String?,
|
||||
standard: json["standard"] as String?,
|
||||
name: json["name"] as String?,
|
||||
symbol: json["symbol"] as String?,
|
||||
description: json["description"] as String?,
|
||||
imageOriginalUrl: json["imageOriginalUrl"] as String?,
|
||||
externalUrl: json["externalUrl"] as String?,
|
||||
metadataOriginalUrl: json["metadataOriginalUrl"] as String?,
|
||||
totalSupply: json["totalSupply"] as String?,
|
||||
metaplex: json["metaplex"] != null
|
||||
? Metaplex.fromJson(json["metaplex"] as Map<String, dynamic>)
|
||||
: null,
|
||||
collection: json["collection"] != null
|
||||
? Collection.fromJson(json["collection"] as Map<String, dynamic>)
|
||||
: null,
|
||||
contract: json["contract"] != null
|
||||
? Contract.fromJson(json["contract"] as Map<String, dynamic>)
|
||||
: null,
|
||||
isOwned: json["isOwned"] as bool?,
|
||||
);
|
||||
}
|
||||
|
||||
class Metaplex {
|
||||
@@ -100,17 +107,15 @@ class Collection {
|
||||
this.metaplexMint,
|
||||
this.sellerFeeBasisPoints});
|
||||
|
||||
factory Collection.fromJson(Map<String, dynamic> json) {
|
||||
return Collection(
|
||||
collectionAddress: json['collectionAddress'] as String?,
|
||||
name: json['name'] as String?,
|
||||
description: json['description'] as String?,
|
||||
imageOriginalUrl: json['imageOriginalUrl'] as String?,
|
||||
externalUrl: json['externalUrl'] as String?,
|
||||
metaplexMint: json['metaplexMint'] as String?,
|
||||
sellerFeeBasisPoints: json['sellerFeeBasisPoints'] as int?,
|
||||
);
|
||||
}
|
||||
factory Collection.fromJson(Map<String, dynamic> json) => Collection(
|
||||
collectionAddress: json["collectionAddress"] as String?,
|
||||
name: sanitizeNFTText(json["name"] as String?),
|
||||
description: sanitizeNFTText(json["description"] as String?),
|
||||
imageOriginalUrl: tryNormalizeIpfsUrl(json["imageOriginalUrl"] as String?),
|
||||
externalUrl: json["externalUrl"] as String?,
|
||||
metaplexMint: json["metaplexMint"] as String?,
|
||||
sellerFeeBasisPoints: json["sellerFeeBasisPoints"] as int?,
|
||||
);
|
||||
}
|
||||
|
||||
class Contract {
|
||||
@@ -123,8 +128,8 @@ class Contract {
|
||||
factory Contract.fromJson(Map<String, dynamic> json) {
|
||||
return Contract(
|
||||
type: json['type'] as String?,
|
||||
name: json['name'] as String?,
|
||||
symbol: json['symbol'] as String?,
|
||||
name: sanitizeNFTText(json["name"] as String?),
|
||||
symbol: sanitizeNFTText(json["symbol"] as String?),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import "package:cw_core/utils/ipfs_url.dart";
|
||||
import "package:cw_core/utils/nft_text.dart";
|
||||
|
||||
class WalletNFTsResponseModel {
|
||||
final int? page;
|
||||
final int? pageSize;
|
||||
@@ -40,8 +43,8 @@ class NFTAssetModel {
|
||||
tokenAddress: json['token_address'] as String?,
|
||||
tokenId: json['token_id'] as String?,
|
||||
contractType: json['contract_type'] as String?,
|
||||
name: json['name'] as String?,
|
||||
symbol: json['symbol'] as String?,
|
||||
name: sanitizeNFTText(json["name"] as String?),
|
||||
symbol: sanitizeNFTText(json["symbol"] as String?),
|
||||
normalizedMetadata: json['normalized_metadata'] != null
|
||||
? new NormalizedMetadata.fromJson(json['normalized_metadata'] as Map<String, dynamic>)
|
||||
: null,
|
||||
@@ -61,33 +64,11 @@ class NormalizedMetadata {
|
||||
|
||||
factory NormalizedMetadata.fromJson(Map<String, dynamic> json) {
|
||||
return NormalizedMetadata(
|
||||
name: json['name'] as String?,
|
||||
description: json['description'] as String?,
|
||||
image: json['image'] as String?,
|
||||
name: sanitizeNFTText(json["name"] as String?),
|
||||
description: sanitizeNFTText(json["description"] as String?),
|
||||
image: json["image"] as String?,
|
||||
);
|
||||
}
|
||||
|
||||
String? get imageUrl {
|
||||
if (image == null) return image;
|
||||
|
||||
if (image!.contains('ipfs.io')) return image;
|
||||
|
||||
if (!image!.contains('ipfs')) return image;
|
||||
|
||||
// IPFS public gateway provided by Cloudflare is https://cloudflare-ipfs.com/ipfs/
|
||||
//
|
||||
// Here is an example of an ipfs image link:
|
||||
//
|
||||
// [ipfs://bafkreia2i2ctfexpovgzfff66wqhbmwwpvqjvozan7ioifzcnq76jharwu]
|
||||
|
||||
//https://ipfs.io/ipfs/QmTRcRXo6cXByjHYHTVxGpag6vpocrG3rxjPC9PxKAArR9/1620.png
|
||||
|
||||
const String ipfsPublicGateway = 'https://cloudflare-ipfs.com/ipfs/';
|
||||
|
||||
final ipfsPath = image?.split('//')[1];
|
||||
|
||||
final imageLink = '$ipfsPublicGateway$ipfsPath';
|
||||
|
||||
return imageLink;
|
||||
}
|
||||
String? get imageUrl => tryNormalizeIpfsUrl(image);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user