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

Bug 340: Content hash is out of spec in announcements #343

Merged
merged 10 commits into from
Aug 7, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ export class DsnpAnnouncementProcessor {
attachment: attachments,
});
const noteString = JSON.stringify(note);
const [cid, hash] = await this.pinBufferToIPFS(Buffer.from(noteString));
const toUint8Array = new TextEncoder();
const encoded = toUint8Array.encode(noteString);

const [cid, hash] = await this.pinBufferToIPFS(Buffer.from(encoded));
const ipfsUrl = this.formIpfsUrl(cid);
return [cid, ipfsUrl, hash];
}
Expand Down Expand Up @@ -397,8 +400,11 @@ export class DsnpAnnouncementProcessor {
icon: attachments,
tag: this.prepareTags(content.profile.tag),
};
const toUint8Array = new TextEncoder();
const profileString = JSON.stringify(profileActivity);
const [cid, hash] = await this.pinBufferToIPFS(Buffer.from(profileString));
const profileEncoded = toUint8Array.encode(profileString);

const [cid, hash] = await this.pinBufferToIPFS(Buffer.from(profileEncoded));
saraswatpuneet marked this conversation as resolved.
Show resolved Hide resolved
return createProfile(dsnpUserId, this.formIpfsUrl(cid), hash);
}

Expand Down
20 changes: 5 additions & 15 deletions services/content-publishing/libs/common/src/utils/ipfs.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ export class IpfsService {

const ipfsAuthUser = this.configService.ipfsBasicAuthUser;
const ipfsAuthSecret = this.configService.ipfsBasicAuthSecret;
const ipfsAuth =
ipfsAuthUser && ipfsAuthSecret
? `Basic ${Buffer.from(`${ipfsAuthUser}:${ipfsAuthSecret}`).toString('base64')}`
: '';
const ipfsAuth = ipfsAuthUser && ipfsAuthSecret ? `Basic ${Buffer.from(`${ipfsAuthUser}:${ipfsAuthSecret}`).toString('base64')}` : '';

const headers = {
'Content-Type': `multipart/form-data; boundary=${form.getBoundary()}`,
Expand Down Expand Up @@ -93,10 +90,7 @@ export class IpfsService {
const ipfsGet = `${this.configService.ipfsEndpoint}/api/v0/cat?arg=${cid}`;
const ipfsAuthUser = this.configService.ipfsBasicAuthUser;
const ipfsAuthSecret = this.configService.ipfsBasicAuthSecret;
const ipfsAuth =
ipfsAuthUser && ipfsAuthSecret
? `Basic ${Buffer.from(`${ipfsAuthUser}:${ipfsAuthSecret}`).toString('base64')}`
: '';
const ipfsAuth = ipfsAuthUser && ipfsAuthSecret ? `Basic ${Buffer.from(`${ipfsAuthUser}:${ipfsAuthSecret}`).toString('base64')}` : '';

const headers = {
Accept: '*/*',
Expand All @@ -116,10 +110,7 @@ export class IpfsService {
const ipfsGet = `${this.configService.ipfsEndpoint}/api/v0/pin/ls?type=all&quiet=true&arg=${v0Cid}`;
const ipfsAuthUser = this.configService.ipfsBasicAuthUser;
const ipfsAuthSecret = this.configService.ipfsBasicAuthSecret;
const ipfsAuth =
ipfsAuthUser && ipfsAuthSecret
? `Basic ${Buffer.from(`${ipfsAuthUser}:${ipfsAuthSecret}`).toString('base64')}`
: '';
const ipfsAuth = ipfsAuthUser && ipfsAuthSecret ? `Basic ${Buffer.from(`${ipfsAuthUser}:${ipfsAuthSecret}`).toString('base64')}` : '';

const headers = {
Accept: '*/*',
Expand All @@ -138,9 +129,8 @@ export class IpfsService {

private async ipfsHashBuffer(fileBuffer: Buffer): Promise<string> {
this.logger.debug(`Hashing file buffer with length: ${fileBuffer.length}`);
const hashed = await hasher.digest(fileBuffer);
const hash = create(hasher.code, hashed.bytes);
saraswatpuneet marked this conversation as resolved.
Show resolved Hide resolved
return base58btc.encode(hash.bytes);
const hash = hasher.digest(fileBuffer);
return base58btc.encode((await hash).bytes);
saraswatpuneet marked this conversation as resolved.
Show resolved Hide resolved
saraswatpuneet marked this conversation as resolved.
Show resolved Hide resolved
}

public ipfsUrl(cid: string): string {
Expand Down
4 changes: 2 additions & 2 deletions services/content-watcher/libs/common/src/ipfs/ipfs.dsnp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class IPFSContentProcessor extends BaseConsumer {
if (isBroadcast(mapRecord)) {
announcementResponse.announcement = {
fromId: mapRecord.fromId,
contentHash: bases.base16.encode(mapRecord.contentHash as never),
saraswatpuneet marked this conversation as resolved.
Show resolved Hide resolved
contentHash: mapRecord.contentHash,
url: mapRecord.url,
announcementType: mapRecord.announcementType,
};
Expand All @@ -108,7 +108,7 @@ export class IPFSContentProcessor extends BaseConsumer {
announcementResponse.announcement = {
fromId: mapRecord.fromId,
targetAnnouncementType: mapRecord.targetAnnouncementType,
targetContentHash: bases.base58btc.encode(mapRecord.targetContentHash as any),
targetContentHash: mapRecord.targetContentHash,
announcementType: mapRecord.announcementType,
};
queue = this.tombstoneQueue;
Expand Down
6 changes: 3 additions & 3 deletions services/content-watcher/libs/common/src/utils/ipfs.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ export class IpfsService {

private async ipfsHashBuffer(fileBuffer: Buffer): Promise<string> {
this.logger.debug(`Hashing file buffer with length: ${fileBuffer.length}`);
const hashed = await hasher.digest(fileBuffer);
const hash = create(hasher.code, hashed.bytes);
return base58btc.encode(hash.bytes);
const hash = hasher.digest(fileBuffer);
return base58btc.encode((await hash).bytes);
}


public ipfsUrl(cid: string): string {
if (this.configService.ipfsGatewayUrl.includes('[CID]')) {
return this.configService.ipfsGatewayUrl.replace('[CID]', cid);
Expand Down
Loading