Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Boldizsar Mezei committed Sep 28, 2023
1 parent 7662efd commit be3c6d0
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/functions/src/runtime/firebase/stamp/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WEN_FUNC } from '@build-5/interfaces';
import { stampCreateControl } from '../../../controls/stamp/stamp.crete';
import { stampCreateControl } from '../../../controls/stamp/stamp.create';
import { onRequest } from '../common';
import { stampSchema } from './StampRequestSchema';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const createStampAndStampOrder = async (
build5Url = await migrateUriToSotrage(COL.STAMP, owner, stampUid, uriToUrl(uri), bucket, true);
}

const { ipfsMedia, bytes } = await downloadMediaAndPackCar(stampUid, build5Url, {});
const { ipfsMedia, bytes, hash } = await downloadMediaAndPackCar(stampUid, build5Url, {});

const space = await getSpace(project, owner, aliasId);
const stamp: Stamp = {
Expand All @@ -102,6 +102,8 @@ export const createStampAndStampOrder = async (
createdBy: owner,

build5Url,
originUri: uri,
checksum: hash,
bytes,
costPerMb: STAMP_COST_PER_MB,

Expand Down Expand Up @@ -214,7 +216,10 @@ const getNftOutputAmount = async (stamp: Stamp, aliasId: string, wallet: SmrWall
};

export const stampToNftMetadata = (stamp: Stamp) => ({
originUri: stamp.originUri,
uri: 'ipfs://' + stamp.ipfsMedia,
build5Url: stamp.build5Url,
checksum: stamp.checksum,
issuerName: KEY_NAME_TANGLE,
build5Id: stamp.uid,
});
Expand Down
11 changes: 6 additions & 5 deletions packages/functions/src/utils/car.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,23 @@ export const downloadMediaAndPackCar = async <M>(uid: string, mediaUrl: string,
const workdir = `${os.tmpdir()}/${randomUUID()}`;
fs.mkdirSync(workdir);

const bytes = await downloadFile(mediaUrl, workdir, uid);
const { size: bytes, hash } = await downloadFile(mediaUrl, workdir, uid);

const metadataFileName = `metadata.json`;
fs.writeFileSync(workdir + '/' + metadataFileName, JSON.stringify(metadata));
if (!isEmpty(metadata)) {
fs.writeFileSync(workdir + '/' + metadataFileName, JSON.stringify(metadata));
}

const { car, cid } = await packCar(workdir);
const cidMap = getNameToCidMap(car);

fs.rmSync(workdir, { recursive: true, force: true });

return {
car,
ipfsMedia: cidMap[uid],
ipfsMetadata: cidMap[metadataFileName],
ipfsMetadata: cidMap[metadataFileName] || '',
ipfsRoot: cid,
bytes,
hash,
};
};

Expand Down
21 changes: 12 additions & 9 deletions packages/functions/src/utils/media.utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { IBucket } from '@build-5/database';
import {
Bucket,
Expand All @@ -9,7 +10,7 @@ import {
WenError,
} from '@build-5/interfaces';
import axios from 'axios';
import { randomUUID } from 'crypto';
import crypto, { randomUUID } from 'crypto';
import * as functions from 'firebase-functions/v2';
import fs from 'fs';
import mime from 'mime-types';
Expand Down Expand Up @@ -37,7 +38,6 @@ export const migrateUriToSotrage = async (
const build5Url =
bucket.getName() === Bucket.DEV ? response : `https://${bucket.getName()}/${destination}`;
return build5Url;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
functions.logger.error(col, uid, error);
throw error.code && error.key ? error : WenError.ipfs_retrieve;
Expand All @@ -50,7 +50,6 @@ const downloadMedia = async (workdir: string, url: string, allowAnyType: boolean
let error = WenError.ipfs_retrieve;
for (let i = 0; i < 5; ++i) {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const head: any = await axios.head(url);
if (head.status !== 200) {
error = WenError.ipfs_retrieve;
Expand All @@ -65,9 +64,8 @@ const downloadMedia = async (workdir: string, url: string, allowAnyType: boolean
const extension = <string>mime.extension(contentType);
const fileName = generateRandomFileName() + '.' + extension;

const bytes = await downloadFile(url, workdir, fileName);
return { fileName, contentType, bytes };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { size: bytes, hash } = await downloadFile(url, workdir, fileName);
return { fileName, contentType, bytes, hash };
} catch (err: any) {
error = err.code === WenError.max_size.code ? err : WenError.ipfs_retrieve;
continue;
Expand Down Expand Up @@ -102,10 +100,14 @@ export const downloadFile = async (url: string, workDir: string, fileName: strin
const stream = fs.createWriteStream(path.join(workDir, fileName));
response.data.pipe(stream);

return new Promise<number>((resolve, reject) => {
return new Promise<{
size: number;
hash: string;
}>((resolve, reject) => {
let size = 0;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const chunks: any[] = [];
response.data.on('data', (chunk: any) => {
chunks.push(chunk);
size += chunk.length;
});

Expand All @@ -114,7 +116,8 @@ export const downloadFile = async (url: string, workDir: string, fileName: strin
reject(WenError.max_size);
return;
}
resolve(size);
const content = Buffer.concat(chunks).toString('utf8');
resolve({ size, hash: crypto.createHash('sha1').update(content).digest('hex') });
});
response.data.on('error', reject);
});
Expand Down
5 changes: 5 additions & 0 deletions packages/functions/test-tangle/stamp-tangle/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
Transaction,
TransactionType,
} from '@build-5/interfaces';
import crypto from 'crypto';
import fs from 'fs';
import { SmrWallet } from '../../src/services/wallet/SmrWalletService';
import { AddressDetails } from '../../src/services/wallet/wallet';
import { getRandomEthAddress } from '../../src/utils/wallet.utils';
Expand All @@ -22,6 +24,7 @@ export class Helper {
public wallet: SmrWallet = {} as any;
public address: AddressDetails = {} as any;
public dowloadUrl = '';
public checksum = '';

public request: StampTangleRequest = {} as any;
public beforeAll = async () => {
Expand All @@ -40,6 +43,8 @@ export class Helper {
uri: this.dowloadUrl,
network: Network.RMS,
};
const content = fs.readFileSync('./test/puppy.jpeg');
this.checksum = crypto.createHash('sha1').update(content).digest('hex');

this.address = await this.wallet.getNewIotaAddressDetails();
await requestFundsFromFaucet(Network.RMS, this.address.bech32, 5 * MIN_IOTA_AMOUNT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ describe('Stamp tangle test', () => {
expect(metadata.uri).toBe('ipfs://' + stamp!.ipfsMedia);
expect(metadata.issuerName).toBe(KEY_NAME_TANGLE);
expect(metadata.build5Id).toBe(stamp!.uid);
expect(metadata.originUri).toBe(helper.dowloadUrl);
expect(metadata.build5Url).toBe(helper.dowloadUrl);
expect(metadata.checksum).toBe(helper.checksum);

Check failure on line 75 in packages/functions/test-tangle/stamp-tangle/stamp-tangle_1.spec.ts

View workflow job for this annotation

GitHub Actions / chunk_20

Stamp tangle test › Should create and mint stamp

expect(received).toBe(expected) // Object.is equality Expected: "b3050cb2e186f3d5f5e875a5fee64020174fc65e" Received: "5860d3549477a09b3dedccb386efc21974bbd009" at Object.<anonymous> (test-tangle/stamp-tangle/stamp-tangle_1.spec.ts:75:31)

const billPayment = await build5Db()
.collection(COL.TRANSACTION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('Stamp tangle test', () => {
let stamp = await stampDocRef.get<Stamp>();
expect(stamp?.mediaStatus).toBe(MediaStatus.PENDING_UPLOAD);
expect(stamp?.ipfsMedia).toBeDefined();
expect(stamp?.originUri).toBe(CUSTOM_MEDIA);

const expiresAfter30Days = dayjs(stamp?.expiresAt.toDate()).isAfter(dayjs().add(30, 'd'));
expect(expiresAfter30Days).toBe(true);
Expand Down
1 change: 1 addition & 0 deletions packages/functions/test/controls/stamp.control.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe('Stamp control', () => {
const stamp = await stampDocRef.get<Stamp>();
expect(stamp?.space).toBe(order.space);
expect(stamp?.build5Url).toBe(dowloadUrl);
expect(stamp?.originUri).toBe(dowloadUrl);
expect(stamp?.costPerMb).toBe(STAMP_COST_PER_MB);
expect(stamp?.network).toBe(Network.RMS);
expect(stamp?.ipfsMedia).toBeDefined();
Expand Down
2 changes: 2 additions & 0 deletions packages/interfaces/src/models/stamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Network } from './transaction';
export interface Stamp extends BaseRecord {
space: string;
build5Url: string;
originUri: string;
checksum: string;

bytes: number;
costPerMb: number;
Expand Down

0 comments on commit be3c6d0

Please sign in to comment.