-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter.ts
57 lines (50 loc) · 1.74 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
import type { Address, Expression, ExpressionAdapter, PublicSharing, HolochainLanguageDelegate, LanguageContext, AgentService } from "@perspect3vism/ad4m";
import { name } from "./index";
class GenericExpressionPutAdapter implements PublicSharing {
#agent: AgentService;
#genericExpressionDNA: HolochainLanguageDelegate;
constructor(context: LanguageContext) {
this.#agent = context.agent;
this.#genericExpressionDNA = context.Holochain as HolochainLanguageDelegate;
}
async createPublic(data: object): Promise<Address> {
const orderedData = Object.keys(data)
.sort()
.reduce((obj, key) => {
obj[key] = data[key];
return obj;
}, {});
const expression = this.#agent.createSignedExpression(orderedData);
const expressionPostData = {
author: expression.author,
timestamp: expression.timestamp,
data: JSON.stringify(expression.data),
proof: expression.proof,
};
const res = await this.#genericExpressionDNA.call(
name,
"generic_expression",
"create_expression",
expressionPostData
);
return res.toString("hex");
}
}
export default class GenericExpressionAdapter implements ExpressionAdapter {
#genericExpressionDNA: HolochainLanguageDelegate;
putAdapter: PublicSharing;
constructor(context: LanguageContext) {
this.#genericExpressionDNA = context.Holochain as HolochainLanguageDelegate;
this.putAdapter = new GenericExpressionPutAdapter(context);
}
async get(address: Address): Promise<Expression> {
const hash = Buffer.from(address, "hex");
const expression = await this.#genericExpressionDNA.call(
name,
"generic_expression",
"get_expression_by_address",
hash
);
return expression
}
}