@metamask/assets-controllers 75.2.0 → 77.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -1
- package/dist/NftController.cjs +19 -68
- package/dist/NftController.cjs.map +1 -1
- package/dist/NftController.d.cts +82 -59
- package/dist/NftController.d.cts.map +1 -1
- package/dist/NftController.d.mts +82 -59
- package/dist/NftController.d.mts.map +1 -1
- package/dist/NftController.mjs +19 -68
- package/dist/NftController.mjs.map +1 -1
- package/dist/utils/formatters.cjs +24 -0
- package/dist/utils/formatters.cjs.map +1 -1
- package/dist/utils/formatters.d.cts +7 -0
- package/dist/utils/formatters.d.cts.map +1 -1
- package/dist/utils/formatters.d.mts +7 -0
- package/dist/utils/formatters.d.mts.map +1 -1
- package/dist/utils/formatters.mjs +24 -0
- package/dist/utils/formatters.mjs.map +1 -1
- package/package.json +10 -10
package/dist/NftController.mjs
CHANGED
|
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _NftController_instances, _NftController_mutex, _NftController_selectedAccountId, _NftController_ipfsGateway,
|
|
12
|
+
var _NftController_instances, _NftController_mutex, _NftController_selectedAccountId, _NftController_ipfsGateway, _NftController_displayNftMedia, _NftController_useIpfsSubdomains, _NftController_isIpfsGatewayEnabled, _NftController_onNftAdded, _NftController_onPreferencesControllerStateChange, _NftController_onSelectedAccountChange, _NftController_updateNestedNftState, _NftController_getNftCollectionApi, _NftController_getNftInformationFromApi, _NftController_getNftInformationFromTokenURI, _NftController_getNftURIAndStandard, _NftController_getNftInformation, _NftController_getNftContractInformationFromContract, _NftController_getNftContractInformation, _NftController_addIndividualNft, _NftController_addNftContract, _NftController_removeAndIgnoreIndividualNft, _NftController_removeIndividualNft, _NftController_removeNftContract, _NftController_validateWatchNft, _NftController_getAddressOrSelectedAddress, _NftController_updateNftUpdateForAccount, _NftController_bulkSanitizeNftMetadata, _NftController_sanitizeNftMetadata;
|
|
13
13
|
function $importDefault(module) {
|
|
14
14
|
if (module?.__esModule) {
|
|
15
15
|
return module.default;
|
|
@@ -69,7 +69,7 @@ export class NftController extends BaseController {
|
|
|
69
69
|
*
|
|
70
70
|
* @param options - The controller options.
|
|
71
71
|
* @param options.ipfsGateway - The configured IPFS gateway.
|
|
72
|
-
* @param options.
|
|
72
|
+
* @param options.displayNftMedia - Controls whether the NFT API is used.
|
|
73
73
|
* @param options.useIpfsSubdomains - Controls whether IPFS subdomains are used.
|
|
74
74
|
* @param options.isIpfsGatewayEnabled - Controls whether IPFS is enabled or not.
|
|
75
75
|
* @param options.onNftAdded - Callback that is called when an NFT is added. Currently used pass data
|
|
@@ -77,7 +77,7 @@ export class NftController extends BaseController {
|
|
|
77
77
|
* @param options.messenger - The messenger.
|
|
78
78
|
* @param options.state - Initial state to set on this controller.
|
|
79
79
|
*/
|
|
80
|
-
constructor({ ipfsGateway = IPFS_DEFAULT_GATEWAY_URL,
|
|
80
|
+
constructor({ ipfsGateway = IPFS_DEFAULT_GATEWAY_URL, displayNftMedia = false, useIpfsSubdomains = true, isIpfsGatewayEnabled = true, onNftAdded, messenger, state = {}, }) {
|
|
81
81
|
super({
|
|
82
82
|
name: controllerName,
|
|
83
83
|
metadata: nftControllerMetadata,
|
|
@@ -91,28 +91,20 @@ export class NftController extends BaseController {
|
|
|
91
91
|
_NftController_mutex.set(this, new Mutex());
|
|
92
92
|
_NftController_selectedAccountId.set(this, void 0);
|
|
93
93
|
_NftController_ipfsGateway.set(this, void 0);
|
|
94
|
-
|
|
94
|
+
_NftController_displayNftMedia.set(this, void 0);
|
|
95
95
|
_NftController_useIpfsSubdomains.set(this, void 0);
|
|
96
96
|
_NftController_isIpfsGatewayEnabled.set(this, void 0);
|
|
97
97
|
_NftController_onNftAdded.set(this, void 0);
|
|
98
98
|
__classPrivateFieldSet(this, _NftController_selectedAccountId, this.messagingSystem.call('AccountsController:getSelectedAccount').id, "f");
|
|
99
99
|
__classPrivateFieldSet(this, _NftController_ipfsGateway, ipfsGateway, "f");
|
|
100
|
-
__classPrivateFieldSet(this,
|
|
100
|
+
__classPrivateFieldSet(this, _NftController_displayNftMedia, displayNftMedia, "f");
|
|
101
101
|
__classPrivateFieldSet(this, _NftController_useIpfsSubdomains, useIpfsSubdomains, "f");
|
|
102
102
|
__classPrivateFieldSet(this, _NftController_isIpfsGatewayEnabled, isIpfsGatewayEnabled, "f");
|
|
103
103
|
__classPrivateFieldSet(this, _NftController_onNftAdded, onNftAdded, "f");
|
|
104
|
-
this.messagingSystem.subscribe('PreferencesController:stateChange',
|
|
105
|
-
|
|
106
|
-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
107
|
-
__classPrivateFieldGet(this, _NftController_instances, "m", _NftController_onPreferencesControllerStateChange).bind(this));
|
|
108
|
-
this.messagingSystem.subscribe('AccountsController:selectedEvmAccountChange',
|
|
109
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
110
|
-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
111
|
-
__classPrivateFieldGet(this, _NftController_instances, "m", _NftController_onSelectedAccountChange).bind(this));
|
|
104
|
+
this.messagingSystem.subscribe('PreferencesController:stateChange', __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_onPreferencesControllerStateChange).bind(this));
|
|
105
|
+
this.messagingSystem.subscribe('AccountsController:selectedEvmAccountChange', __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_onSelectedAccountChange).bind(this));
|
|
112
106
|
}
|
|
113
107
|
getNftApi() {
|
|
114
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
115
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
116
108
|
return `${NFT_API_BASE_URL}/tokens`;
|
|
117
109
|
}
|
|
118
110
|
/**
|
|
@@ -166,14 +158,6 @@ export class NftController extends BaseController {
|
|
|
166
158
|
source: Source.Dapp,
|
|
167
159
|
});
|
|
168
160
|
}
|
|
169
|
-
/**
|
|
170
|
-
* Sets an OpenSea API key to retrieve NFT information.
|
|
171
|
-
*
|
|
172
|
-
* @param openSeaApiKey - OpenSea API key.
|
|
173
|
-
*/
|
|
174
|
-
setApiKey(openSeaApiKey) {
|
|
175
|
-
this.openSeaApiKey = openSeaApiKey;
|
|
176
|
-
}
|
|
177
161
|
/**
|
|
178
162
|
* Checks the ownership of a ERC-721 or ERC-1155 NFT for a given address.
|
|
179
163
|
*
|
|
@@ -188,7 +172,6 @@ export class NftController extends BaseController {
|
|
|
188
172
|
try {
|
|
189
173
|
const owner = await this.messagingSystem.call('AssetsContractController:getERC721OwnerOf', nftAddress, tokenId, networkClientId);
|
|
190
174
|
return ownerAddress.toLowerCase() === owner.toLowerCase();
|
|
191
|
-
// eslint-disable-next-line no-empty
|
|
192
175
|
}
|
|
193
176
|
catch {
|
|
194
177
|
// Ignore ERC-721 contract error
|
|
@@ -197,7 +180,6 @@ export class NftController extends BaseController {
|
|
|
197
180
|
try {
|
|
198
181
|
const balance = await this.messagingSystem.call('AssetsContractController:getERC1155BalanceOf', ownerAddress, nftAddress, tokenId, networkClientId);
|
|
199
182
|
return !balance.isZero();
|
|
200
|
-
// eslint-disable-next-line no-empty
|
|
201
183
|
}
|
|
202
184
|
catch {
|
|
203
185
|
// Ignore ERC-1155 contract error
|
|
@@ -629,26 +611,28 @@ export class NftController extends BaseController {
|
|
|
629
611
|
});
|
|
630
612
|
}
|
|
631
613
|
}
|
|
632
|
-
_NftController_mutex = new WeakMap(), _NftController_selectedAccountId = new WeakMap(), _NftController_ipfsGateway = new WeakMap(),
|
|
614
|
+
_NftController_mutex = new WeakMap(), _NftController_selectedAccountId = new WeakMap(), _NftController_ipfsGateway = new WeakMap(), _NftController_displayNftMedia = new WeakMap(), _NftController_useIpfsSubdomains = new WeakMap(), _NftController_isIpfsGatewayEnabled = new WeakMap(), _NftController_onNftAdded = new WeakMap(), _NftController_instances = new WeakSet(), _NftController_onPreferencesControllerStateChange =
|
|
633
615
|
/**
|
|
634
616
|
* Handles the state change of the preference controller.
|
|
635
617
|
*
|
|
636
618
|
* @param preferencesState - The new state of the preference controller.
|
|
637
619
|
* @param preferencesState.ipfsGateway - The configured IPFS gateway.
|
|
638
|
-
* @param preferencesState.openSeaEnabled - Controls whether the OpenSea API is used.
|
|
639
620
|
* @param preferencesState.isIpfsGatewayEnabled - Controls whether IPFS is enabled or not.
|
|
621
|
+
* @param preferencesState.displayNftMedia - Controls whether the NFT API is used (mobile).
|
|
622
|
+
* @param preferencesState.openSeaEnabled - Controls whether the NFT API is used (extension).
|
|
640
623
|
*/
|
|
641
|
-
async function _NftController_onPreferencesControllerStateChange({ ipfsGateway,
|
|
624
|
+
async function _NftController_onPreferencesControllerStateChange({ ipfsGateway, isIpfsGatewayEnabled, displayNftMedia, openSeaEnabled, }) {
|
|
642
625
|
const selectedAccount = this.messagingSystem.call('AccountsController:getSelectedAccount');
|
|
643
626
|
__classPrivateFieldSet(this, _NftController_selectedAccountId, selectedAccount.id, "f");
|
|
627
|
+
const newDisplayNftMedia = Boolean(displayNftMedia || openSeaEnabled);
|
|
644
628
|
// Get current state values
|
|
645
629
|
if (__classPrivateFieldGet(this, _NftController_ipfsGateway, "f") !== ipfsGateway ||
|
|
646
|
-
__classPrivateFieldGet(this,
|
|
630
|
+
__classPrivateFieldGet(this, _NftController_displayNftMedia, "f") !== newDisplayNftMedia ||
|
|
647
631
|
__classPrivateFieldGet(this, _NftController_isIpfsGatewayEnabled, "f") !== isIpfsGatewayEnabled) {
|
|
648
632
|
__classPrivateFieldSet(this, _NftController_ipfsGateway, ipfsGateway, "f");
|
|
649
|
-
__classPrivateFieldSet(this,
|
|
633
|
+
__classPrivateFieldSet(this, _NftController_displayNftMedia, newDisplayNftMedia, "f");
|
|
650
634
|
__classPrivateFieldSet(this, _NftController_isIpfsGatewayEnabled, isIpfsGatewayEnabled, "f");
|
|
651
|
-
const needsUpdateNftMetadata = (isIpfsGatewayEnabled && ipfsGateway !== '') ||
|
|
635
|
+
const needsUpdateNftMetadata = (isIpfsGatewayEnabled && ipfsGateway !== '') || newDisplayNftMedia;
|
|
652
636
|
if (needsUpdateNftMetadata && selectedAccount) {
|
|
653
637
|
await __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_updateNftUpdateForAccount).call(this, selectedAccount);
|
|
654
638
|
}
|
|
@@ -663,7 +647,7 @@ async function _NftController_onSelectedAccountChange(internalAccount) {
|
|
|
663
647
|
const oldSelectedAccountId = __classPrivateFieldGet(this, _NftController_selectedAccountId, "f");
|
|
664
648
|
__classPrivateFieldSet(this, _NftController_selectedAccountId, internalAccount.id, "f");
|
|
665
649
|
const needsUpdateNftMetadata = ((__classPrivateFieldGet(this, _NftController_isIpfsGatewayEnabled, "f") && __classPrivateFieldGet(this, _NftController_ipfsGateway, "f") !== '') ||
|
|
666
|
-
__classPrivateFieldGet(this,
|
|
650
|
+
__classPrivateFieldGet(this, _NftController_displayNftMedia, "f")) &&
|
|
667
651
|
oldSelectedAccountId !== internalAccount.id;
|
|
668
652
|
if (needsUpdateNftMetadata) {
|
|
669
653
|
await __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_updateNftUpdateForAccount).call(this, internalAccount);
|
|
@@ -688,7 +672,6 @@ async function _NftController_onSelectedAccountChange(internalAccount) {
|
|
|
688
672
|
});
|
|
689
673
|
}, _NftController_getNftCollectionApi = function _NftController_getNftCollectionApi() {
|
|
690
674
|
// False negative.
|
|
691
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
692
675
|
return `${NFT_API_BASE_URL}/collections`;
|
|
693
676
|
}, _NftController_getNftInformationFromApi =
|
|
694
677
|
/**
|
|
@@ -782,7 +765,7 @@ async function _NftController_getNftInformationFromTokenURI(contractAddress, tok
|
|
|
782
765
|
tokenURI: tokenURI ?? null,
|
|
783
766
|
};
|
|
784
767
|
}
|
|
785
|
-
const isDisplayNFTMediaToggleEnabled = __classPrivateFieldGet(this,
|
|
768
|
+
const isDisplayNFTMediaToggleEnabled = __classPrivateFieldGet(this, _NftController_displayNftMedia, "f");
|
|
786
769
|
if (!hasIpfsTokenURI && !isDisplayNFTMediaToggleEnabled) {
|
|
787
770
|
return {
|
|
788
771
|
image: null,
|
|
@@ -882,7 +865,7 @@ async function _NftController_getNftInformation(contractAddress, tokenId, networ
|
|
|
882
865
|
const { configuration: { chainId }, } = this.messagingSystem.call('NetworkController:getNetworkClientById', networkClientId);
|
|
883
866
|
const [blockchainMetadata, nftApiMetadata] = await Promise.all([
|
|
884
867
|
safelyExecute(() => __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getNftInformationFromTokenURI).call(this, contractAddress, tokenId, networkClientId)),
|
|
885
|
-
__classPrivateFieldGet(this,
|
|
868
|
+
__classPrivateFieldGet(this, _NftController_displayNftMedia, "f") && chainId === '0x1'
|
|
886
869
|
? safelyExecute(() => __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getNftInformationFromApi).call(this, contractAddress, tokenId))
|
|
887
870
|
: undefined,
|
|
888
871
|
]);
|
|
@@ -932,13 +915,9 @@ async function _NftController_getNftContractInformation(contractAddress, nftMeta
|
|
|
932
915
|
return {
|
|
933
916
|
address: contractAddress,
|
|
934
917
|
...blockchainContractData,
|
|
935
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
936
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
937
918
|
schema_name: nftMetadataFromApi?.standard ?? null,
|
|
938
919
|
collection: {
|
|
939
920
|
name: null,
|
|
940
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
941
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
942
921
|
image_url: nftMetadataFromApi?.collection?.image ??
|
|
943
922
|
nftMetadataFromApi?.collection?.imageUrl ??
|
|
944
923
|
null,
|
|
@@ -951,25 +930,13 @@ async function _NftController_getNftContractInformation(contractAddress, nftMeta
|
|
|
951
930
|
/* istanbul ignore next */
|
|
952
931
|
return {
|
|
953
932
|
address: contractAddress,
|
|
954
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
955
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
956
933
|
asset_contract_type: null,
|
|
957
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
958
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
959
934
|
created_date: null,
|
|
960
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
961
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
962
935
|
schema_name: null,
|
|
963
936
|
symbol: null,
|
|
964
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
965
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
966
937
|
total_supply: null,
|
|
967
938
|
description: null,
|
|
968
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
969
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
970
939
|
external_link: null,
|
|
971
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
972
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
973
940
|
collection: { name: null, image_url: null },
|
|
974
941
|
};
|
|
975
942
|
}, _NftController_addIndividualNft =
|
|
@@ -1065,22 +1032,7 @@ async function _NftController_addNftContract(networkClientId, { tokenAddress, us
|
|
|
1065
1032
|
// will be fixed once detection uses networkClientIds
|
|
1066
1033
|
// get name and symbol if ERC721 then put together the metadata
|
|
1067
1034
|
const contractInformation = await __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getNftContractInformation).call(this, checksumHexAddress, nftMetadata, networkClientId);
|
|
1068
|
-
const {
|
|
1069
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
1070
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
1071
|
-
asset_contract_type,
|
|
1072
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
1073
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
1074
|
-
created_date, symbol, description,
|
|
1075
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
1076
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
1077
|
-
external_link,
|
|
1078
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
1079
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
1080
|
-
schema_name,
|
|
1081
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
1082
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
1083
|
-
collection: { name, image_url, tokenCount }, } = contractInformation;
|
|
1035
|
+
const { asset_contract_type, created_date, symbol, description, external_link, schema_name, collection: { name, image_url, tokenCount }, } = contractInformation;
|
|
1084
1036
|
// If the nft is auto-detected we want some valid metadata to be present
|
|
1085
1037
|
if (source === Source.Detected &&
|
|
1086
1038
|
'address' in contractInformation &&
|
|
@@ -1156,7 +1108,6 @@ async function _NftController_addNftContract(networkClientId, { tokenAddress, us
|
|
|
1156
1108
|
}
|
|
1157
1109
|
if (type !== ERC721 && type !== ERC1155) {
|
|
1158
1110
|
throw rpcErrors.invalidParams(
|
|
1159
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
1160
1111
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
1161
1112
|
`Non NFT asset type ${type} not supported by watchNft`);
|
|
1162
1113
|
}
|