-
Notifications
You must be signed in to change notification settings - Fork 1
/
adapter.ts
78 lines (66 loc) · 2.32 KB
/
adapter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import type { Address, Expression, ExpressionAdapter, PublicSharing, IPFSNode, LanguageContext, AgentService, HolochainLanguageDelegate } from "@perspect3vism/ad4m";
//import { DNA_NICK } from "./dna";
const _appendBuffer = (buffer1, buffer2) => {
const tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(new Uint8Array(buffer1), 0);
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
return tmp.buffer;
};
const uint8ArrayConcat = (chunks) => {
return chunks.reduce(_appendBuffer);
};
class SharedPerspectivePutAdapter implements PublicSharing {
#agent: AgentService;
//#hcDna: HolochainLanguageDelegate;
#IPFS: IPFSNode
constructor(context: LanguageContext) {
this.#agent = context.agent;
//this.#hcDna = context.Holochain as HolochainLanguageDelegate;
this.#IPFS = context.IPFS;
}
async createPublic(neighbourhood: object): Promise<Address> {
// const expression = this.#agent.createSignedExpression(neighbourhood);
// let resp = await this.#hcDna.call(
// DNA_NICK,
// "neighbourhood_store",
// "index_neighbourhood",
// expression
// );
// return resp.toString("hex");
const agent = this.#agent;
const expression = agent.createSignedExpression(neighbourhood);
const content = JSON.stringify(expression);
const result = await this.#IPFS.add({ content });
// @ts-ignore
return result.cid.toString() as Address;
}
}
export default class Adapter implements ExpressionAdapter {
//#hcDna: HolochainLanguageDelegate;
#IPFS: IPFSNode
putAdapter: PublicSharing;
constructor(context: LanguageContext) {
//this.#hcDna = context.Holochain as HolochainLanguageDelegate;
this.#IPFS = context.IPFS;
this.putAdapter = new SharedPerspectivePutAdapter(context);
}
async get(address: Address): Promise<Expression> {
const cid = address.toString();
const chunks = [];
// @ts-ignore
for await (const chunk of this.#IPFS.cat(cid)) {
chunks.push(chunk);
}
const fileString = uint8ArrayConcat(chunks).toString();
const fileJson = JSON.parse(fileString);
return fileJson;
// const hash = Buffer.from(address, "hex");
// const res = await this.#hcDna.call(
// DNA_NICK,
// "neighbourhood_store",
// "get_neighbourhood",
// hash
// );
// return res;
}
}