Skip to content

Commit df3edc9

Browse files
authored
Merge pull request #1 from earth-association/feat-nfts
Add validators, image helper functions
2 parents b2b917c + f941340 commit df3edc9

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "@earthwallet/assets",
3-
"version": "0.3.1",
3+
"version": "0.3.5",
44
"description": "Secure assets fetching and transfer functions for Earth Wallet",
55
"main": "build/main/index.js",
66
"typings": "build/main/index.d.ts",
77
"module": "build/module/index.js",
8-
"repository": "https://github.com/earthgohan/assets",
8+
"repository": "https://github.com/earth-association/assets",
99
"license": "MIT",
1010
"keywords": [],
1111
"scripts": {

src/util/icp/index.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ test('get tokens for a EXT type canister for a user', async (t) => {
1515
info: {
1616
seller:
1717
'o7nwu-n6kuf-4afzp-ybcuf-346pr-odd54-damf5-v4pvc-4sexh-cabph-7qe',
18-
price: BigInt(18000000000),
18+
price: BigInt(18000000000).toString(),
1919
locked: [],
2020
},
2121
tokenIndex: 2112,
@@ -60,7 +60,7 @@ test('list not owned NFT of a canister should give UNAUTHORISED status', async (
6060
'owuqd-dyaaa-aaaah-qapxq-cai',
6161
walletObj.identity,
6262
'2112',
63-
4
63+
180
6464
);
6565

6666
t.is(status, 'UNAUTHORISED');

src/util/icp/index.ts

+31-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
12
import { Actor, HttpAgent } from '@dfinity/agent';
23
import { sha224 } from '@dfinity/rosetta-client/lib/hash';
34
import fetch from 'cross-fetch';
@@ -31,6 +32,14 @@ export const principal_id_to_address_buffer = (pid) => {
3132
]);
3233
};
3334

35+
export const getTokenImageExt = (canisterId: string, tokenid: string) =>
36+
`https://${canisterId}.raw.ic0.app/?tokenid=${tokenid}`;
37+
38+
export const getTokenThumbnailImageExt = (
39+
canisterId: string,
40+
tokenid: string
41+
) => `https://${canisterId}.raw.ic0.app/?type=thumbnail&tokenid=${tokenid}`;
42+
3443
export const getNFTsFromCanisterExt = async (
3544
canisterId: string,
3645
accountId: string
@@ -63,11 +72,11 @@ export const getNFTsFromCanisterExt = async (
6372

6473
return tokensOK.map((token) => {
6574
const tokenIndex = parseInt(token[0]);
66-
const info = { seller: '', price: BigInt(0), locked: [] };
75+
const info = { seller: '', price: BigInt(0).toString(), locked: [] };
6776
let forSale = false;
6877
if (token[1][0] !== undefined) {
6978
info.seller = token[1][0]?.seller.toText();
70-
info.price = BigInt(token[1][0]?.price);
79+
info.price = BigInt(token[1][0]?.price).toString();
7180
info.locked = token[1][0]?.locked;
7281
forSale = true;
7382
}
@@ -84,8 +93,8 @@ export const getNFTsFromCanisterExt = async (
8493
});
8594
};
8695

87-
export const principal_to_address = (princial) =>
88-
address_to_hex(principal_id_to_address_buffer(princial));
96+
export const principal_to_address = (principal) =>
97+
address_to_hex(principal_id_to_address_buffer(principal));
8998

9099
export const getTokenIdentifier = (
91100
canisterId: string,
@@ -106,8 +115,6 @@ export const transferNFTsExt = async (
106115
toAccountId: string,
107116
tokenIndex: string
108117
) => {
109-
//const fetchWallet = await createWallet(TEST_MNE_1, 'ICP');
110-
111118
const agent = await Promise.resolve(
112119
new HttpAgent({
113120
host: ICP_HOST,
@@ -156,14 +163,13 @@ export const transferNFTsExt = async (
156163
return status;
157164
};
158165

166+
// Price in xx ICP and 0 to unlist
159167
export const listNFTsExt = async (
160168
canisterId: string,
161169
identity: Identity,
162170
tokenIndex: string,
163171
price: number
164172
) => {
165-
//const fetchWallet = await createWallet(TEST_MNE_1, 'ICP');
166-
167173
const agent = await Promise.resolve(
168174
new HttpAgent({
169175
host: ICP_HOST,
@@ -186,7 +192,7 @@ export const listNFTsExt = async (
186192
try {
187193
status = await API.list({
188194
token,
189-
price: [Math.floor(price * 100000000)],
195+
price: price === 0 ? [] : [Math.floor(price * 100000000)],
190196
from_subaccount: [getSubAccountArray(0)],
191197
});
192198
} catch (error) {
@@ -201,3 +207,19 @@ export const listNFTsExt = async (
201207
}
202208
return status;
203209
};
210+
211+
export const isHex = (str) => {
212+
return Boolean(str.match(/^[0-9a-f]+$/i));
213+
};
214+
215+
export const validateAddress = (a) => {
216+
return isHex(a) && a.length === 64;
217+
};
218+
219+
export const validatePrincipal = (p) => {
220+
try {
221+
return p === Principal.fromText(p).toText();
222+
} catch (e) {
223+
return false;
224+
}
225+
};

src/util/icp/nfts/index.ts

+16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const list = [
33
name: 'Cronic Critters',
44
id: 'e3izy-jiaaa-aaaah-qacbq-cai',
55
standard: 'EXT',
6+
isLive: true,
67
description:
78
'Cronics is a Play-to-earn NFT game being developed by ToniqLabs for the Internet Computer. Cronics incorporates breeding mechanics, wearable NFTs and a p2e minigame ecosystem and more.',
89
icon: 'https://e3izy-jiaaa-aaaah-qacbq-cai.raw.ic0.app/?tokenid=hancg-5ykor-uwiaa-aaaaa-b4aaq-maqca-aabuk-a',
@@ -11,29 +12,36 @@ const list = [
1112
name: 'Starverse',
1213
id: 'nbg4r-saaaa-aaaah-qap7a-cai',
1314
standard: 'EXT',
15+
isLive: true,
1416
description:
1517
'Starverse is an NFT collection of rare and unique Stars, a collaboration between DSCVR and ToniqLabs. The Starverse symbolizes the unlimited potential of the Internet Computer with it’s infinite size and unstoppable nature.',
1618
icon: 'https://nbg4r-saaaa-aaaah-qap7a-cai.raw.ic0.app/?tokenid=wdyem-pikor-uwiaa-aaaaa-b4ad7-yaqca-aacsh-a',
1719
},
1820
{
1921
name: 'Wrapped ICPunks',
22+
wrapped: true,
2023
id: 'bxdf4-baaaa-aaaah-qaruq-cai',
2124
standard: 'EXT',
25+
isLive: true,
2226
description:
2327
'ICPunks wrapped under the EXT standard. 10,000 randomly generated, unique collectible clowns with proof of ownership stored on the Internet Computer blockchain. Created as a reference to a meme comparing the Internet Computer token (ICP) with the Insane Clown Posse.',
2428
icon: 'https://qcg3w-tyaaa-aaaah-qakea-cai.raw.ic0.app/Token/1',
29+
tokenImage: (tokenNumber) =>
30+
`https://qcg3w-tyaaa-aaaah-qakea-cai.raw.ic0.app/Token/${tokenNumber}`,
2531
},
2632
{
2733
name: 'ICP News',
2834
id: 'uzhxd-ziaaa-aaaah-qanaq-cai',
2935
standard: 'EXT',
36+
isLive: false,
3037
description:
3138
'An NFT collection set designed by the @icp_news artist with an Internet Computer theme.',
3239
icon: 'https://uzhxd-ziaaa-aaaah-qanaq-cai.raw.ic0.app/?tokenid=3qdzl-jikor-uwiaa-aaaaa-b4adi-eaqca-aaaad-q',
3340
},
3441
{
3542
name: 'Cronic Wearables',
3643
id: 'tde7l-3qaaa-aaaah-qansa-cai',
44+
isLive: true,
3745
standard: 'EXT',
3846
description:
3947
'Wearable NFTs, usable with the Cronics NFT collection. A Play-to-earn NFT game being developed by ToniqLabs for the Internet Computer. Cronics incorporates breeding mechanics, wearable NFTs, a p2e minigame ecosystem, and more.',
@@ -42,6 +50,7 @@ const list = [
4250
{
4351
name: 'ICMojis',
4452
id: 'gevsk-tqaaa-aaaah-qaoca-cai',
53+
isLive: true,
4554
standard: 'EXT',
4655
description:
4756
'A collection inspired in old school forum emotes, designed by the artist @Visions_GFX, also part of the interactive strategy game ICMoji Origins.',
@@ -51,6 +60,7 @@ const list = [
5160
name: 'ICPuzzle',
5261
id: 'owuqd-dyaaa-aaaah-qapxq-cai',
5362
standard: 'EXT',
63+
isLive: true,
5464
description:
5565
'The ICPuzzle NFT is an artistic NFT that is meant to invoke thought around individuality, community, and the beauty of the human condition. Each puzzle piece represents human individuality within humanity, a self-contained piece of a larger cohesive whole.',
5666
icon: 'https://owuqd-dyaaa-aaaah-qapxq-cai.raw.ic0.app/?tokenid=2e7o5-wykor-uwiaa-aaaaa-b4ad5-4aqca-aaagc-q',
@@ -59,20 +69,26 @@ const list = [
5969
name: 'IC Drip',
6070
id: '3db6u-aiaaa-aaaah-qbjbq-cai',
6171
standard: 'EXT',
72+
wrapped: true,
73+
isLive: true,
6274
description:
6375
'IC Drip are randomly generated NFTs with meta-commerce shopping carts for outfits and personas stored on chain on the Internet Computer.',
6476
icon: 'https://d3ttm-qaaaa-aaaai-qam4a-cai.raw.ic0.app/?tokenId=1',
77+
tokenImage: (tokenNumber) =>
78+
`https://d3ttm-qaaaa-aaaai-qam4a-cai.raw.ic0.app/?tokenId=${tokenNumber}`,
6579
},
6680
{
6781
name: 'Wing',
6882
id: '73xld-saaaa-aaaah-qbjya-cai',
6983
standard: 'EXT',
84+
isLive: true,
7085
description:
7186
'An NFT photographic series created by the photographer @olisav ',
7287
icon: 'https://73xld-saaaa-aaaah-qbjya-cai.raw.ic0.app/?tokenid=tpx6i-sykor-uwiaa-aaaaa-b4ako-aaqca-aaaaz-a',
7388
},
7489
{
7590
name: 'ICPunks',
91+
isLive: false,
7692
id: 'qcg3w-tyaaa-aaaah-qakea-cai',
7793
standard: 'ICPunks',
7894
description:

0 commit comments

Comments
 (0)