Generate same CID from chiba-clonk chain

This commit is contained in:
2022-04-22 16:01:04 +05:30
committed by Ashwin Phatak
parent 28c8099b16
commit 349bc650d9
5 changed files with 43 additions and 164 deletions
+1 -1
View File
@@ -39,7 +39,7 @@ const utilTests = () => {
watcherId = result.data.id;
});
xtest('generate content id.', async () => {
test('Generate content id.', async () => {
const cid = await Util.getContentId(watcher.record);
expect(cid).toBe(watcherId)
});
+14 -5
View File
@@ -1,4 +1,7 @@
import dagCBOR from 'ipld-dag-cbor';
import * as Block from 'multiformats/block'
import { sha256 as hasher } from 'multiformats/hashes/sha2'
import * as dagCBOR from '@ipld/dag-cbor'
import * as dagJSON from '@ipld/dag-json'
/**
* Utils
@@ -82,10 +85,16 @@ export class Util {
/**
* Get record content ID.
*/
static async getContentId(record: any) {
const content = dagCBOR.util.serialize(record);
const cid = await dagCBOR.util.cid(content);
static async getContentId(record: any) {
const serialized = dagJSON.encode(record)
const recordData = dagJSON.decode(serialized)
return cid.toString();
const block = await Block.encode({
value: recordData,
codec: dagCBOR,
hasher
})
return block.cid.toString();
}
}