Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPT-818] Added filecoin #156

Merged
merged 5 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 131 additions & 65 deletions packages/core/src/IPFSMetadataProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,85 +3,151 @@ import url from 'url';
import RegistryNetworks from 'singularitynet-platform-contracts/networks/Registry.json';
import RegistryAbi from 'singularitynet-platform-contracts/abi/Registry.json';
import { get } from 'axios';
import {
LIGHTHOUSE_ENDPOINT,
STORAGE_TYPE_FILECOIN,
STORAGE_TYPE_IPFS,
STORAGE_URL_FILECOIN_PREFIX,
STORAGE_URL_IPFS_PREFIX,
} from './constants/StorageConstants';

import logger from './utils/logger';

export default class IPFSMetadataProvider {
constructor(web3, networkId, ipfsEndpoint) {
this._web3 = web3;
this._networkId = networkId;
this._ipfsEndpoint = ipfsEndpoint;
const registryAddress = RegistryNetworks[this._networkId].address;
this._registryContract = new this._web3.eth.Contract(RegistryAbi, registryAddress);
}
constructor(web3, networkId, ipfsEndpoint) {
this._web3 = web3;
this._networkId = networkId;
this._ipfsEndpoint = ipfsEndpoint;
const registryAddress = RegistryNetworks[this._networkId].address;
this._registryContract = new this._web3.eth.Contract(
RegistryAbi,
registryAddress
);
this._lighthouseEndpoint = LIGHTHOUSE_ENDPOINT;
this._storageTypeIpfs = STORAGE_TYPE_IPFS;
this._storageTypeFilecoin = STORAGE_TYPE_FILECOIN;
this._storageUrlIpfsPrefix = STORAGE_URL_IPFS_PREFIX;
this._storageUrlFilecoinPrefix = STORAGE_URL_FILECOIN_PREFIX;
}

/**
* @param {string} orgId
* @param {string} serviceId
* @returns {Promise.<ServiceMetadata>}
*/
async metadata(orgId, serviceId) {
logger.debug(
`Fetching service metadata [org: ${orgId} | service: ${serviceId}]`
);
let orgIdBytes = this._web3.utils.fromAscii(orgId);
orgIdBytes = orgIdBytes.padEnd(66, '0'); // 66 = '0x' + 64 hex characters

/**
* @param {string} orgId
* @param {string} serviceId
* @returns {Promise.<ServiceMetadata>}
*/
async metadata(orgId, serviceId) {
logger.debug(`Fetching service metadata [org: ${orgId} | service: ${serviceId}]`);
let orgIdBytes = this._web3.utils.fromAscii(orgId);
orgIdBytes = orgIdBytes.padEnd(66, '0'); // 66 = '0x' + 64 hex characters
let serviceIdBytes = this._web3.utils.fromAscii(serviceId);
serviceIdBytes = serviceIdBytes.padEnd(66, '0'); // 66 = '0x' + 64 hex characters

let serviceIdBytes = this._web3.utils.fromAscii(serviceId);
serviceIdBytes = serviceIdBytes.padEnd(66, '0'); // 66 = '0x' + 64 hex characters
const orgMetadata = await this._fetchOrgMetadata(orgIdBytes);
const serviceMetadata = await this._fetchServiceMetadata(
orgIdBytes,
serviceIdBytes
);
return Promise.resolve(
this._enhanceServiceGroupDetails(serviceMetadata, orgMetadata)
);
}

const orgMetadata = await this._fetchOrgMetadata(orgIdBytes);
const serviceMetadata = await this._fetchServiceMetadata(orgIdBytes, serviceIdBytes);
return Promise.resolve(this._enhanceServiceGroupDetails(serviceMetadata, orgMetadata));
}
async _fetchOrgMetadata(orgIdBytes) {
logger.debug('Fetching org metadata URI from registry contract');
const { orgMetadataURI } = await this._registryContract.methods
.getOrganizationById(orgIdBytes)
.call();

async _fetchOrgMetadata(orgIdBytes) {
logger.debug('Fetching org metadata URI from registry contract');
const { orgMetadataURI } = await this._registryContract.methods.getOrganizationById(orgIdBytes).call();
return this._fetchMetadataFromIpfs(orgMetadataURI);
}

return this._fetchMetadataFromIpfs(orgMetadataURI);
}
async _fetchServiceMetadata(orgIdBytes, serviceIdBytes) {
logger.debug('Fetching service metadata URI from registry contract');
const { metadataURI: serviceMetadataURI } =
await this._registryContract.methods
.getServiceRegistrationById(orgIdBytes, serviceIdBytes)
.call();
return this._fetchMetadataFromIpfs(serviceMetadataURI);
}

async _fetchServiceMetadata(orgIdBytes, serviceIdBytes) {
logger.debug('Fetching service metadata URI from registry contract');
const { metadataURI: serviceMetadataURI } = await this._registryContract
.methods
.getServiceRegistrationById(orgIdBytes, serviceIdBytes)
.call();
return this._fetchMetadataFromIpfs(serviceMetadataURI);
}
async _fetchMetadataFromIpfs(metadataURI) {
let storageInfo = this._getStorageInfoFromURI(metadataURI);
let storageTypeCID = storageInfo.uri;
storageTypeCID = storageTypeCID.replace(/\0/g, '');
debug(
`Fetching metadata from ${storageInfo.type} [CID: ${storageTypeCID}]`
);
try {
let fetchUrl;
if (storageInfo.type === this._storageTypeIpfs) {
fetchUrl = `${this._ipfsEndpoint}/api/v0/cat?arg=${storageTypeCID}`;
} else {
fetchUrl = `${this._lighthouseEndpoint}/${storageTypeCID}`;
}
const response = await fetch(fetchUrl);
if (!response.ok) {
throw response.error;
}
return response.json();
} catch (error) {
debug(`Error fetching metadata from IPFS[CID: ${storageTypeCID}]`);
throw error;
}
}

async _fetchMetadataFromIpfs(metadataURI) {
let ipfsCID = `${this._web3.utils.hexToUtf8(metadataURI).substring(7)}`;
ipfsCID = ipfsCID.replace(/\0/g, '');
logger.debug(`Fetching metadata from IPFS[CID: ${ipfsCID}]`);
try {
const fetchUrl = `${this._ipfsEndpoint}/api/v0/cat?arg=${ipfsCID}`;
const response = await get(fetchUrl);
return response.data;
} catch(error) {
logger.debug(`Error fetching metadata from IPFS[CID: ${ipfsCID}]`);
throw error;
_getStorageInfoFromURI(metadataURI) {
const decodedUri = this._web3.utils.hexToUtf8(metadataURI);
if (decodedUri.startsWith(STORAGE_URL_IPFS_PREFIX)) {
return {
type: this._storageTypeIpfs,
uri: decodedUri.replace(this._storageUrlIpfsPrefix, ''),
};
} else if (decodedUri.startsWith(STORAGE_URL_FILECOIN_PREFIX)) {
return {
type: this._storageTypeFilecoin,
uri: decodedUri.replace(this._storageUrlFilecoinPrefix, ''),
};
} else {
throw new Error(
`We support only ${this._storageTypeIpfs} and ${this._storageTypeFilecoin} uri in Registry`
);
}
}
}

_enhanceServiceGroupDetails(serviceMetadata, orgMetadata) {
const { groups: orgGroups } = orgMetadata;
const { groups: serviceGroups } = serviceMetadata;
_enhanceServiceGroupDetails(serviceMetadata, orgMetadata) {
const { groups: orgGroups } = orgMetadata;
const { groups: serviceGroups } = serviceMetadata;

const groups = map(serviceGroups, (group) => {
const { group_name: serviceGroupName } = group;
const orgGroup = find(orgGroups, ({ group_name: orgGroupName }) => orgGroupName === serviceGroupName);
return {
...group,
payment: orgGroup.payment,
};
});
const groups = map(serviceGroups, (group) => {
const { group_name: serviceGroupName } = group;
const orgGroup = find(
orgGroups,
({ group_name: orgGroupName }) =>
orgGroupName === serviceGroupName
);
return {
...group,
payment: orgGroup.payment,
};
});

return { ...serviceMetadata, groups };
}
return { ...serviceMetadata, groups };
}

_constructIpfsClient() {
const { protocol = 'http', hostname: host, port = 5001 } = url.parse(this._ipfsEndpoint);
const ipfsHostOrMultiaddr = { protocol: protocol.replace(':', ''), host, port };
return IPFSClient(ipfsHostOrMultiaddr);
}
_constructIpfsClient() {
const {
protocol = 'http',
hostname: host,
port = 5001,
} = url.parse(this._ipfsEndpoint);
const ipfsHostOrMultiaddr = {
protocol: protocol.replace(':', ''),
host,
port,
};
return IPFSClient(ipfsHostOrMultiaddr);
}
}
5 changes: 5 additions & 0 deletions packages/core/src/constants/StorageConstants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const LIGHTHOUSE_ENDPOINT = "https://gateway.lighthouse.storage/ipfs";
export const STORAGE_TYPE_IPFS = "ipfs";
export const STORAGE_TYPE_FILECOIN = "filecoin";
export const STORAGE_URL_IPFS_PREFIX = "ipfs://";
export const STORAGE_URL_FILECOIN_PREFIX = "filecoin://";
4 changes: 2 additions & 2 deletions packages/web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "snet-sdk-web",
"version": "4.0.4",
"version": "4.1.0",
"description": "SingularityNET SDK for Web",
"main": "./dist/index.js",
"files": [
Expand Down