@agentbadge/hedera-core 0.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/catalog.d.ts +13 -0
- package/dist/catalog.js +231 -0
- package/dist/chain-adapter.d.ts +62 -0
- package/dist/chain-adapter.js +1 -0
- package/dist/datahub.client.d.ts +58 -0
- package/dist/datahub.client.js +356 -0
- package/dist/did.d.ts +7 -0
- package/dist/did.js +33 -0
- package/dist/hedera-chain-adapter-instance.d.ts +2 -0
- package/dist/hedera-chain-adapter-instance.js +10 -0
- package/dist/hedera-chain-adapter.d.ts +18 -0
- package/dist/hedera-chain-adapter.js +121 -0
- package/dist/hedera.service.d.ts +52 -0
- package/dist/hedera.service.js +415 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +261 -0
- package/dist/mirror.service.d.ts +34 -0
- package/dist/mirror.service.js +187 -0
- package/dist/mock-hedera.service.d.ts +69 -0
- package/dist/mock-hedera.service.js +195 -0
- package/dist/mock-mirror.service.d.ts +22 -0
- package/dist/mock-mirror.service.js +72 -0
- package/dist/mock-settle.d.ts +1 -0
- package/dist/mock-settle.js +6 -0
- package/dist/services-index.d.ts +84 -0
- package/dist/services-index.js +133 -0
- package/dist/signing.d.ts +5 -0
- package/dist/signing.js +17 -0
- package/dist/types.d.ts +215 -0
- package/dist/types.js +56 -0
- package/package.json +44 -0
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
2
|
+
const DEFAULT_GMS_URL = "http://localhost:8080";
|
|
3
|
+
export class DataHubClient {
|
|
4
|
+
baseUrl;
|
|
5
|
+
staticToken;
|
|
6
|
+
actorId;
|
|
7
|
+
timeoutMs;
|
|
8
|
+
mockMode;
|
|
9
|
+
cachedToken = null;
|
|
10
|
+
constructor() {
|
|
11
|
+
this.baseUrl = (process.env.DATAHUB_GMS_URL ?? DEFAULT_GMS_URL).replace(/\/$/, "");
|
|
12
|
+
this.staticToken = process.env.DATAHUB_GMS_TOKEN ?? process.env.DATAHUB_TOKEN;
|
|
13
|
+
this.actorId = process.env.DATAHUB_GMS_ACTOR_ID ?? "datahub";
|
|
14
|
+
this.timeoutMs = parseTimeout();
|
|
15
|
+
this.mockMode = !process.env.DATAHUB_ENABLED || process.env.DATAHUB_ENABLED === "false";
|
|
16
|
+
}
|
|
17
|
+
async generateToken() {
|
|
18
|
+
const data = await this.rawGraphQL(TOKEN_MUTATION, { type: "PERSONAL", actorId: this.actorId }, false);
|
|
19
|
+
const tokenData = data.datahubAccessToken;
|
|
20
|
+
this.cachedToken = {
|
|
21
|
+
accessToken: tokenData.accessToken,
|
|
22
|
+
expiresAt: new Date(tokenData.expiresAt),
|
|
23
|
+
};
|
|
24
|
+
return tokenData.accessToken;
|
|
25
|
+
}
|
|
26
|
+
async ensureToken() {
|
|
27
|
+
if (this.staticToken)
|
|
28
|
+
return this.staticToken;
|
|
29
|
+
if (this.cachedToken && !isTokenExpiringSoon(this.cachedToken.expiresAt)) {
|
|
30
|
+
return this.cachedToken.accessToken;
|
|
31
|
+
}
|
|
32
|
+
return this.generateToken();
|
|
33
|
+
}
|
|
34
|
+
async rawGraphQL(query, variables, withAuth) {
|
|
35
|
+
const controller = new AbortController();
|
|
36
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
37
|
+
try {
|
|
38
|
+
const headers = {
|
|
39
|
+
"Content-Type": "application/json",
|
|
40
|
+
};
|
|
41
|
+
if (withAuth && this.cachedToken) {
|
|
42
|
+
headers["Authorization"] = `Bearer ${this.cachedToken.accessToken}`;
|
|
43
|
+
}
|
|
44
|
+
else if (withAuth && this.staticToken) {
|
|
45
|
+
headers["Authorization"] = `Bearer ${this.staticToken}`;
|
|
46
|
+
}
|
|
47
|
+
const res = await fetch(`${this.baseUrl}/api/graphql`, {
|
|
48
|
+
method: "POST",
|
|
49
|
+
headers,
|
|
50
|
+
body: JSON.stringify({ query, variables }),
|
|
51
|
+
signal: controller.signal,
|
|
52
|
+
});
|
|
53
|
+
if (!res.ok) {
|
|
54
|
+
const body = await res.text().catch(() => "");
|
|
55
|
+
throw new Error(`DataHub HTTP ${res.status}: ${body || res.statusText}`);
|
|
56
|
+
}
|
|
57
|
+
const json = (await res.json());
|
|
58
|
+
if (json.errors && json.errors.length > 0) {
|
|
59
|
+
throw new Error(json.errors.map((e) => e.message).join("; "));
|
|
60
|
+
}
|
|
61
|
+
if (!json.data) {
|
|
62
|
+
throw new Error("DataHub returned no data");
|
|
63
|
+
}
|
|
64
|
+
return json.data;
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
if (err instanceof DOMException && err.name === "AbortError") {
|
|
68
|
+
throw new Error(`DataHub timeout after ${this.timeoutMs}ms`);
|
|
69
|
+
}
|
|
70
|
+
throw err;
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
clearTimeout(timer);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async graphql(query, variables) {
|
|
77
|
+
const token = await this.ensureToken();
|
|
78
|
+
try {
|
|
79
|
+
return await this.doGraphQL(query, variables, token);
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
if (err instanceof Error && err.message.startsWith("DataHub HTTP 401")) {
|
|
83
|
+
this.cachedToken = null;
|
|
84
|
+
const newToken = await this.ensureToken();
|
|
85
|
+
return this.doGraphQL(query, variables, newToken);
|
|
86
|
+
}
|
|
87
|
+
throw err;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
async doGraphQL(query, variables, token) {
|
|
91
|
+
const controller = new AbortController();
|
|
92
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
93
|
+
try {
|
|
94
|
+
const res = await fetch(`${this.baseUrl}/api/graphql`, {
|
|
95
|
+
method: "POST",
|
|
96
|
+
headers: {
|
|
97
|
+
"Content-Type": "application/json",
|
|
98
|
+
"Authorization": `Bearer ${token}`,
|
|
99
|
+
},
|
|
100
|
+
body: JSON.stringify({ query, variables }),
|
|
101
|
+
signal: controller.signal,
|
|
102
|
+
});
|
|
103
|
+
if (!res.ok) {
|
|
104
|
+
const body = await res.text().catch(() => "");
|
|
105
|
+
throw new Error(`DataHub HTTP ${res.status}: ${body || res.statusText}`);
|
|
106
|
+
}
|
|
107
|
+
const json = (await res.json());
|
|
108
|
+
if (json.errors && json.errors.length > 0) {
|
|
109
|
+
throw new Error(json.errors.map((e) => e.message).join("; "));
|
|
110
|
+
}
|
|
111
|
+
if (!json.data) {
|
|
112
|
+
throw new Error("DataHub returned no data");
|
|
113
|
+
}
|
|
114
|
+
return json.data;
|
|
115
|
+
}
|
|
116
|
+
catch (err) {
|
|
117
|
+
if (err instanceof DOMException && err.name === "AbortError") {
|
|
118
|
+
throw new Error(`DataHub timeout after ${this.timeoutMs}ms`);
|
|
119
|
+
}
|
|
120
|
+
throw err;
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
clearTimeout(timer);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
async search(query, type, limit) {
|
|
127
|
+
if (this.mockMode)
|
|
128
|
+
return { entities: [], total: 0 };
|
|
129
|
+
const data = await this.graphql(SEARCH_QUERY, { query, type, limit });
|
|
130
|
+
return {
|
|
131
|
+
total: data.search.total,
|
|
132
|
+
entities: data.search.searchResults.map((r) => r.entity),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
async getEntity(urn) {
|
|
136
|
+
if (this.mockMode)
|
|
137
|
+
return null;
|
|
138
|
+
const data = await this.graphql(GET_ENTITY_QUERY, { urn });
|
|
139
|
+
return data.entity;
|
|
140
|
+
}
|
|
141
|
+
async listSchemaFields(datasetUrn) {
|
|
142
|
+
if (this.mockMode)
|
|
143
|
+
return [];
|
|
144
|
+
const data = await this.graphql(DATASET_SCHEMA_QUERY, { urn: datasetUrn });
|
|
145
|
+
if (!data.dataset?.schemaMetadata)
|
|
146
|
+
return [];
|
|
147
|
+
return data.dataset.schemaMetadata.fields.map((f) => ({
|
|
148
|
+
fieldPath: f.fieldPath,
|
|
149
|
+
type: f.type?.type ?? "UNKNOWN",
|
|
150
|
+
}));
|
|
151
|
+
}
|
|
152
|
+
async getLineage(datasetUrn) {
|
|
153
|
+
if (this.mockMode)
|
|
154
|
+
return { upstreams: [], downstreams: [] };
|
|
155
|
+
const data = await this.graphql(LINEAGE_QUERY, { urn: datasetUrn });
|
|
156
|
+
return {
|
|
157
|
+
upstreams: data.lineage.upstreams.map((u) => u.entity),
|
|
158
|
+
downstreams: data.lineage.downstreams.map((d) => d.entity),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
async getDatasetAssertions(datasetUrn) {
|
|
162
|
+
if (this.mockMode)
|
|
163
|
+
return [];
|
|
164
|
+
const data = await this.graphql(DATASET_ASSERTIONS_QUERY, { urn: datasetUrn });
|
|
165
|
+
if (!data.dataset?.assertions)
|
|
166
|
+
return [];
|
|
167
|
+
return data.dataset.assertions.assertions.map((a) => a.entity);
|
|
168
|
+
}
|
|
169
|
+
async addTerms(termUrns, resourceUrn, subResourceType, subResource) {
|
|
170
|
+
if (this.mockMode)
|
|
171
|
+
return {};
|
|
172
|
+
const data = await this.graphql(ADD_TERMS_MUTATION, {
|
|
173
|
+
termUrns,
|
|
174
|
+
resourceUrn,
|
|
175
|
+
subResourceType: subResourceType ?? null,
|
|
176
|
+
subResource: subResource ?? null,
|
|
177
|
+
});
|
|
178
|
+
return data;
|
|
179
|
+
}
|
|
180
|
+
async createGlossaryTerm(name, description, parentNodeUrn) {
|
|
181
|
+
if (this.mockMode)
|
|
182
|
+
return null;
|
|
183
|
+
const data = await this.graphql(CREATE_GLOSSARY_TERM_MUTATION, { name, description, parentNodeUrn });
|
|
184
|
+
return data.createGlossaryTerm?.urn ?? null;
|
|
185
|
+
}
|
|
186
|
+
async upsertDatasetSchemaAssertionMonitor(entityUrn, fields, compatibility, description) {
|
|
187
|
+
if (this.mockMode)
|
|
188
|
+
return null;
|
|
189
|
+
const data = await this.graphql(UPSERT_SCHEMA_ASSERTION_MUTATION, {
|
|
190
|
+
entityUrn,
|
|
191
|
+
fields,
|
|
192
|
+
compatibility,
|
|
193
|
+
description,
|
|
194
|
+
});
|
|
195
|
+
return data.upsertDatasetSchemaAssertionMonitor?.urn ?? null;
|
|
196
|
+
}
|
|
197
|
+
async upsertDatasetFreshnessAssertionMonitor(entityUrn, schedule, description) {
|
|
198
|
+
if (this.mockMode)
|
|
199
|
+
return null;
|
|
200
|
+
const data = await this.graphql(UPSERT_FRESHNESS_ASSERTION_MUTATION, {
|
|
201
|
+
entityUrn,
|
|
202
|
+
schedule,
|
|
203
|
+
description,
|
|
204
|
+
});
|
|
205
|
+
return data.upsertDatasetFreshnessAssertionMonitor?.urn ?? null;
|
|
206
|
+
}
|
|
207
|
+
async updateLineage(edgesToAdd, edgesToRemove) {
|
|
208
|
+
if (this.mockMode)
|
|
209
|
+
return {};
|
|
210
|
+
const data = await this.graphql(UPDATE_LINEAGE_MUTATION, {
|
|
211
|
+
edgesToAdd,
|
|
212
|
+
edgesToRemove,
|
|
213
|
+
});
|
|
214
|
+
return data;
|
|
215
|
+
}
|
|
216
|
+
async getAssertionResults(assertionUrn) {
|
|
217
|
+
if (this.mockMode)
|
|
218
|
+
return [];
|
|
219
|
+
const data = await this.graphql(ASSERTION_RESULTS_QUERY, { urn: assertionUrn });
|
|
220
|
+
if (!data.assertion?.runEvents)
|
|
221
|
+
return [];
|
|
222
|
+
return data.assertion.runEvents.map((e) => ({
|
|
223
|
+
status: e.status,
|
|
224
|
+
timestamp: e.timestamp,
|
|
225
|
+
}));
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
function parseTimeout() {
|
|
229
|
+
const raw = process.env.DATAHUB_TIMEOUT_MS;
|
|
230
|
+
if (!raw)
|
|
231
|
+
return DEFAULT_TIMEOUT_MS;
|
|
232
|
+
const parsed = parseInt(raw, 10);
|
|
233
|
+
return Number.isFinite(parsed) && parsed > 0 ? parsed : DEFAULT_TIMEOUT_MS;
|
|
234
|
+
}
|
|
235
|
+
const TOKEN_REFRESH_BUFFER_MS = 5 * 60 * 1000;
|
|
236
|
+
function isTokenExpiringSoon(expiresAt) {
|
|
237
|
+
return Date.now() + TOKEN_REFRESH_BUFFER_MS >= expiresAt.getTime();
|
|
238
|
+
}
|
|
239
|
+
const SEARCH_QUERY = `
|
|
240
|
+
query Search($query: String!, $type: EntityType!, $limit: Int!) {
|
|
241
|
+
search(query: $query, type: $type, limit: $limit) {
|
|
242
|
+
total
|
|
243
|
+
searchResults {
|
|
244
|
+
entity {
|
|
245
|
+
urn
|
|
246
|
+
type
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
`;
|
|
252
|
+
const GET_ENTITY_QUERY = `
|
|
253
|
+
query GetEntity($urn: String!) {
|
|
254
|
+
entity(urn: $urn) {
|
|
255
|
+
urn
|
|
256
|
+
type
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
`;
|
|
260
|
+
const DATASET_SCHEMA_QUERY = `
|
|
261
|
+
query GetDatasetSchema($urn: String!) {
|
|
262
|
+
dataset(urn: $urn) {
|
|
263
|
+
urn
|
|
264
|
+
schemaMetadata {
|
|
265
|
+
fields {
|
|
266
|
+
fieldPath
|
|
267
|
+
type {
|
|
268
|
+
type
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
`;
|
|
275
|
+
const LINEAGE_QUERY = `
|
|
276
|
+
query GetLineage($urn: String!) {
|
|
277
|
+
lineage(urn: $urn) {
|
|
278
|
+
upstreams {
|
|
279
|
+
entity {
|
|
280
|
+
urn
|
|
281
|
+
type
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
downstreams {
|
|
285
|
+
entity {
|
|
286
|
+
urn
|
|
287
|
+
type
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
`;
|
|
293
|
+
const DATASET_ASSERTIONS_QUERY = `
|
|
294
|
+
query GetDatasetAssertions($urn: String!) {
|
|
295
|
+
dataset(urn: $urn) {
|
|
296
|
+
assertions {
|
|
297
|
+
total
|
|
298
|
+
assertions {
|
|
299
|
+
entity {
|
|
300
|
+
urn
|
|
301
|
+
type
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
`;
|
|
308
|
+
const ADD_TERMS_MUTATION = `
|
|
309
|
+
mutation AddTerms($termUrns: [String!]!, $resourceUrn: String!, $subResourceType: String, $subResource: String) {
|
|
310
|
+
addTerms(termUrns: $termUrns, resourceUrn: $resourceUrn, subResourceType: $subResourceType, subResource: $subResource)
|
|
311
|
+
}
|
|
312
|
+
`;
|
|
313
|
+
const CREATE_GLOSSARY_TERM_MUTATION = `
|
|
314
|
+
mutation CreateGlossaryTerm($name: String!, $description: String!, $parentNodeUrn: String!) {
|
|
315
|
+
createGlossaryTerm(input: { name: $name, description: $description, parentNodeUrn: $parentNodeUrn }) {
|
|
316
|
+
urn
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
`;
|
|
320
|
+
const UPSERT_SCHEMA_ASSERTION_MUTATION = `
|
|
321
|
+
mutation UpsertSchemaAssertion($entityUrn: String!, $fields: [String!]!, $compatibility: String!, $description: String!) {
|
|
322
|
+
upsertDatasetSchemaAssertionMonitor(entityUrn: $entityUrn, fields: $fields, compatibility: $compatibility, description: $description) {
|
|
323
|
+
urn
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
`;
|
|
327
|
+
const UPSERT_FRESHNESS_ASSERTION_MUTATION = `
|
|
328
|
+
mutation UpsertFreshnessAssertion($entityUrn: String!, $schedule: String!, $description: String!) {
|
|
329
|
+
upsertDatasetFreshnessAssertionMonitor(entityUrn: $entityUrn, schedule: $schedule, description: $description) {
|
|
330
|
+
urn
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
`;
|
|
334
|
+
const UPDATE_LINEAGE_MUTATION = `
|
|
335
|
+
mutation UpdateLineage($edgesToAdd: [LineageEdgeInput!]!, $edgesToRemove: [LineageEdgeInput!]!) {
|
|
336
|
+
updateLineage(edgesToAdd: $edgesToAdd, edgesToRemove: $edgesToRemove)
|
|
337
|
+
}
|
|
338
|
+
`;
|
|
339
|
+
const ASSERTION_RESULTS_QUERY = `
|
|
340
|
+
query GetAssertionResults($urn: String!) {
|
|
341
|
+
assertion(urn: $urn) {
|
|
342
|
+
runEvents {
|
|
343
|
+
status
|
|
344
|
+
timestamp
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
`;
|
|
349
|
+
const TOKEN_MUTATION = `
|
|
350
|
+
mutation GenerateToken($type: AccessTokenTokenType!, $actorId: String!) {
|
|
351
|
+
datahubAccessToken(input: { type: $type, actorId: $actorId }) {
|
|
352
|
+
accessToken
|
|
353
|
+
expiresAt
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
`;
|
package/dist/did.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare function isValidA2ADid(did: string): boolean;
|
|
2
|
+
export declare function extractTokenAndSerial(did: string): {
|
|
3
|
+
tokenId: string;
|
|
4
|
+
serial: number;
|
|
5
|
+
} | null;
|
|
6
|
+
export declare function didToAccountId(did: string): Promise<string | null>;
|
|
7
|
+
export declare function getMessageDirection(from: string, to: string, didA: string, didB: string): "A→B" | "B→A";
|
package/dist/did.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const DID_REGEX = /^did:hcs:\d+\.\d+\.\d+:\d+$/;
|
|
2
|
+
const DID_EXTRACT_REGEX = /^did:hcs:(.+):(\d+)$/;
|
|
3
|
+
export function isValidA2ADid(did) {
|
|
4
|
+
return DID_REGEX.test(did);
|
|
5
|
+
}
|
|
6
|
+
export function extractTokenAndSerial(did) {
|
|
7
|
+
const match = DID_EXTRACT_REGEX.exec(did);
|
|
8
|
+
if (!match)
|
|
9
|
+
return null;
|
|
10
|
+
return { tokenId: match[1], serial: parseInt(match[2], 10) };
|
|
11
|
+
}
|
|
12
|
+
export async function didToAccountId(did) {
|
|
13
|
+
const parsed = extractTokenAndSerial(did);
|
|
14
|
+
if (!parsed)
|
|
15
|
+
return null;
|
|
16
|
+
try {
|
|
17
|
+
const { getNftInfo } = await import("./services-index");
|
|
18
|
+
const nft = await getNftInfo(parsed.tokenId, parsed.serial);
|
|
19
|
+
if (!nft || nft.deleted)
|
|
20
|
+
return null;
|
|
21
|
+
return nft.account_id;
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export function getMessageDirection(from, to, didA, didB) {
|
|
28
|
+
if (from === didA && to === didB)
|
|
29
|
+
return "A→B";
|
|
30
|
+
if (from === didB && to === didA)
|
|
31
|
+
return "B→A";
|
|
32
|
+
throw new Error("Invalid message direction");
|
|
33
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { HederaChainAdapter } from "./hedera-chain-adapter";
|
|
2
|
+
let _instance = null;
|
|
3
|
+
export const hederaChainAdapter = new Proxy({}, {
|
|
4
|
+
get(_target, prop) {
|
|
5
|
+
if (!_instance) {
|
|
6
|
+
_instance = new HederaChainAdapter();
|
|
7
|
+
}
|
|
8
|
+
return Reflect.get(_instance, prop);
|
|
9
|
+
},
|
|
10
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ChainAdapter, ChainMetadata, ExplorerLinks, EscrowHoldParams, EscrowHoldResult, TransferTokenParams, EventQuery, ChainEvent } from "./chain-adapter";
|
|
2
|
+
import type { MintResult, NftInfo } from "./types";
|
|
3
|
+
export declare class HederaChainAdapter implements ChainAdapter {
|
|
4
|
+
readonly chain: ChainMetadata;
|
|
5
|
+
readonly explorer: ExplorerLinks;
|
|
6
|
+
mintPassport(tokenAddress: string, metadataUri: string): Promise<MintResult>;
|
|
7
|
+
revokePassport(tokenAddress: string, tokenId: number): Promise<void>;
|
|
8
|
+
getPassportInfo(tokenAddress: string, tokenId: number): Promise<NftInfo | null>;
|
|
9
|
+
buildDid(tokenAddress: string, tokenId: number): string;
|
|
10
|
+
resolveDid(did: string): Promise<string | null>;
|
|
11
|
+
verifyOwnershipSignature(did: string, _signature: string, _message: string): Promise<boolean>;
|
|
12
|
+
createEscrowHold(params: EscrowHoldParams): Promise<EscrowHoldResult>;
|
|
13
|
+
releaseEscrow(escrowId: string): Promise<void>;
|
|
14
|
+
reclaimEscrow(escrowId: string): Promise<void>;
|
|
15
|
+
getBalance(address: string): Promise<number>;
|
|
16
|
+
transferToken(params: TransferTokenParams): Promise<string>;
|
|
17
|
+
getEvents(query: EventQuery): Promise<ChainEvent[]>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { mintPassportNFT, burnPassportNFT, getNftInfo, getTopicMessages, transferHbar, transferHbarWithKey, createScheduledTransfer, signScheduledTransaction, deleteScheduledTransaction, } from "./services-index";
|
|
2
|
+
import { extractTokenAndSerial } from "./did";
|
|
3
|
+
const HEDERA_CHAIN_METADATA = {
|
|
4
|
+
id: 295,
|
|
5
|
+
name: "Hedera Testnet",
|
|
6
|
+
currency: "HBAR",
|
|
7
|
+
symbol: "HBAR",
|
|
8
|
+
decimals: 8,
|
|
9
|
+
};
|
|
10
|
+
function getMirrorBase() {
|
|
11
|
+
const network = process.env.HEDERA_NETWORK ?? "testnet";
|
|
12
|
+
return `https://${network}.mirrornode.hedera.com`;
|
|
13
|
+
}
|
|
14
|
+
function hashscanBase() {
|
|
15
|
+
const network = process.env.HEDERA_NETWORK ?? "testnet";
|
|
16
|
+
return `https://hashscan.io/${network}`;
|
|
17
|
+
}
|
|
18
|
+
export class HederaChainAdapter {
|
|
19
|
+
chain = HEDERA_CHAIN_METADATA;
|
|
20
|
+
explorer = {
|
|
21
|
+
tx(txHash) {
|
|
22
|
+
return `${hashscanBase()}/transaction/${txHash}`;
|
|
23
|
+
},
|
|
24
|
+
nft(tokenAddress, tokenId) {
|
|
25
|
+
return `${hashscanBase()}/token/${tokenAddress}/${tokenId}`;
|
|
26
|
+
},
|
|
27
|
+
account(address) {
|
|
28
|
+
return `${hashscanBase()}/account/${address}`;
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
async mintPassport(tokenAddress, metadataUri) {
|
|
32
|
+
return mintPassportNFT(tokenAddress, metadataUri);
|
|
33
|
+
}
|
|
34
|
+
async revokePassport(tokenAddress, tokenId) {
|
|
35
|
+
return burnPassportNFT(tokenAddress, tokenId);
|
|
36
|
+
}
|
|
37
|
+
async getPassportInfo(tokenAddress, tokenId) {
|
|
38
|
+
return getNftInfo(tokenAddress, tokenId);
|
|
39
|
+
}
|
|
40
|
+
buildDid(tokenAddress, tokenId) {
|
|
41
|
+
return `did:hcs:${tokenAddress}:${tokenId}`;
|
|
42
|
+
}
|
|
43
|
+
async resolveDid(did) {
|
|
44
|
+
const parsed = extractTokenAndSerial(did);
|
|
45
|
+
if (!parsed)
|
|
46
|
+
return null;
|
|
47
|
+
try {
|
|
48
|
+
const nft = await getNftInfo(parsed.tokenId, parsed.serial);
|
|
49
|
+
if (!nft || nft.deleted)
|
|
50
|
+
return null;
|
|
51
|
+
return nft.account_id;
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
async verifyOwnershipSignature(did, _signature, _message) {
|
|
58
|
+
const accountId = await this.resolveDid(did);
|
|
59
|
+
return accountId !== null;
|
|
60
|
+
}
|
|
61
|
+
async createEscrowHold(params) {
|
|
62
|
+
const amountHbar = Number(params.amount) / 1e8;
|
|
63
|
+
const result = await createScheduledTransfer(params.from, params.to, amountHbar, {
|
|
64
|
+
expirationSeconds: params.deadline
|
|
65
|
+
? Math.max(0, params.deadline - Math.floor(Date.now() / 1000))
|
|
66
|
+
: undefined,
|
|
67
|
+
memo: params.memo,
|
|
68
|
+
});
|
|
69
|
+
return {
|
|
70
|
+
escrowId: result.scheduleId,
|
|
71
|
+
txHash: result.scheduleTxId,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
async releaseEscrow(escrowId) {
|
|
75
|
+
const operatorKey = process.env.HEDERA_OPERATOR_KEY;
|
|
76
|
+
if (operatorKey) {
|
|
77
|
+
await signScheduledTransaction(escrowId, operatorKey);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
await signScheduledTransaction(escrowId, "mock-key");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
async reclaimEscrow(escrowId) {
|
|
84
|
+
await deleteScheduledTransaction(escrowId);
|
|
85
|
+
}
|
|
86
|
+
async getBalance(address) {
|
|
87
|
+
try {
|
|
88
|
+
const url = `${getMirrorBase()}/api/v1/accounts/${address}/balance`;
|
|
89
|
+
const resp = await fetch(url);
|
|
90
|
+
if (!resp.ok)
|
|
91
|
+
return 0;
|
|
92
|
+
const data = (await resp.json());
|
|
93
|
+
return data.balance ?? 0;
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
return 0;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
async transferToken(params) {
|
|
100
|
+
if (params.tokenAddress === "native") {
|
|
101
|
+
const amountHbar = Number(params.amount) / 1e8;
|
|
102
|
+
if (params.privateKey) {
|
|
103
|
+
return transferHbarWithKey(params.from ?? "", params.privateKey, params.to, amountHbar);
|
|
104
|
+
}
|
|
105
|
+
return transferHbar(params.from ?? "", params.to, amountHbar);
|
|
106
|
+
}
|
|
107
|
+
throw new Error(`HTS token transfer not yet supported: ${params.tokenAddress}`);
|
|
108
|
+
}
|
|
109
|
+
async getEvents(query) {
|
|
110
|
+
const messages = await getTopicMessages(query.contractAddress, {
|
|
111
|
+
limit: query.limit,
|
|
112
|
+
});
|
|
113
|
+
return messages.map((msg, index) => ({
|
|
114
|
+
blockNumber: msg.sequence_number,
|
|
115
|
+
txHash: msg.transaction_id ?? "",
|
|
116
|
+
eventName: "HCSMessage",
|
|
117
|
+
args: { message: msg.message },
|
|
118
|
+
logIndex: index,
|
|
119
|
+
}));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { PrivateKey } from "@hashgraph/sdk";
|
|
2
|
+
import type { A2AMessage, AuditMessage, DirectoryMessage, HcsMessageReceipt, HFSUploadResult, MintResult, TaskMessage } from "./types";
|
|
3
|
+
export type { DirectoryMessage };
|
|
4
|
+
export declare function normalizePrivateKey(keyStr: string): PrivateKey;
|
|
5
|
+
export declare function mintPassportNFT(tokenId: string, ipfsUri: string): Promise<MintResult>;
|
|
6
|
+
export declare function burnPassportNFT(tokenId: string, serial: number): Promise<void>;
|
|
7
|
+
export declare function transferNFTToAgent(tokenId: string, serial: number, fromAccountId: string, toAccountId: string): Promise<void>;
|
|
8
|
+
export declare function grantKyc(tokenId: string, accountId: string): Promise<void>;
|
|
9
|
+
export declare function submitAuditMessage(message: AuditMessage): Promise<string>;
|
|
10
|
+
export declare function submitDirectoryMessage(message: DirectoryMessage): Promise<string>;
|
|
11
|
+
export declare function submitA2AMessage(message: A2AMessage): Promise<HcsMessageReceipt>;
|
|
12
|
+
export declare function submitTaskMessage(message: TaskMessage): Promise<HcsMessageReceipt>;
|
|
13
|
+
export declare function prepareTopicMessageTransaction(agentAccountId: string, message: object, topicIdOverride?: string): Promise<{
|
|
14
|
+
txBytes: string;
|
|
15
|
+
txId: string;
|
|
16
|
+
}>;
|
|
17
|
+
export declare function prepareA2ATopicMessage(agentAccountId: string, message: A2AMessage): Promise<{
|
|
18
|
+
txBytes: string;
|
|
19
|
+
txId: string;
|
|
20
|
+
}>;
|
|
21
|
+
export declare function submitSignedTopicMessage(txBytesBase64: string, publicKeyDer: string, signatureBytes: Uint8Array[]): Promise<string>;
|
|
22
|
+
export declare function wipeNFT(tokenId: string, accountId: string, serial: number): Promise<void>;
|
|
23
|
+
export declare function updateNftMetadata(tokenId: string, serial: number, newUri: string): Promise<void>;
|
|
24
|
+
export declare function transferHbar(fromAccountId: string, toAccountId: string, amountHbar: number): Promise<string>;
|
|
25
|
+
export declare function transferHbarWithKey(fromAccountId: string, fromPrivateKey: string, toAccountId: string, amountHbar: number): Promise<string>;
|
|
26
|
+
export declare function prepareTransferTransaction(fromAccountId: string, toAccountId: string, amountHbar: number): Promise<{
|
|
27
|
+
txBytes: string;
|
|
28
|
+
txId: string;
|
|
29
|
+
}>;
|
|
30
|
+
export declare function transferHbarWithSignature(txBytesBase64: string, publicKeyStr: string, signatureBytes: Uint8Array | Uint8Array[]): Promise<string>;
|
|
31
|
+
export declare function createScheduledTransfer(fromAccountId: string, toAccountId: string, amountHbar: number, options?: {
|
|
32
|
+
adminKey?: boolean;
|
|
33
|
+
expirationSeconds?: number;
|
|
34
|
+
memo?: string;
|
|
35
|
+
}): Promise<{
|
|
36
|
+
scheduleId: string;
|
|
37
|
+
scheduleTxId: string;
|
|
38
|
+
}>;
|
|
39
|
+
export declare function signScheduledTransaction(scheduleId: string, signerPrivateKey: string): Promise<{
|
|
40
|
+
txId: string;
|
|
41
|
+
executed: boolean;
|
|
42
|
+
}>;
|
|
43
|
+
export declare function signScheduledTransactionWithSignature(scheduleId: string, txBytesBase64: string, publicKeyStr: string, signatureBytes: Uint8Array | Uint8Array[]): Promise<{
|
|
44
|
+
txId: string;
|
|
45
|
+
executed: boolean;
|
|
46
|
+
}>;
|
|
47
|
+
export declare function deleteScheduledTransaction(scheduleId: string): Promise<{
|
|
48
|
+
scheduleId: string;
|
|
49
|
+
deleted: boolean;
|
|
50
|
+
}>;
|
|
51
|
+
export declare function uploadFileToHFS(contents: Buffer, fileMemo?: string): Promise<HFSUploadResult>;
|
|
52
|
+
export declare function downloadFileFromHFS(fileId: string): Promise<Buffer>;
|