-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathputAdapter.ts
40 lines (33 loc) · 1.22 KB
/
putAdapter.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
import type { Address, AgentService, PublicSharing, HolochainLanguageDelegate, IPFSNode, LanguageContext, LanguageLanguageInput} from "@perspect3vism/ad4m";
import { DNA_NICK } from "./dna";
export class IpfsPutAdapter implements PublicSharing {
#agent: AgentService;
#IPFS: IPFSNode;
#holochain: HolochainLanguageDelegate;
constructor(context: LanguageContext) {
this.#agent = context.agent;
this.#IPFS = context.IPFS;
this.#holochain = context.Holochain as HolochainLanguageDelegate;
}
async createPublic(language: LanguageLanguageInput): Promise<Address> {
const ipfsAddress = await this.#IPFS.add({
content: language.bundle.toString(),
});
// @ts-ignore
const hash = ipfsAddress.cid.toString();
if(hash != language.meta.address)
throw new Error(`Language Persistence: Can't store language. Address stated in meta differs from actual file\nWanted: ${language.meta.address}\nGot: ${hash}`)
const agent = this.#agent;
const expression = agent.createSignedExpression(language.meta);
await this.#holochain.call(
DNA_NICK,
"anchored-expression",
"store_expression",
{
key: hash,
expression,
}
);
return hash as Address;
}
}