@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,415 @@
|
|
|
1
|
+
import { Client, PrivateKey, PublicKey, AccountId, TokenId, TopicId, Transaction, TokenMintTransaction, TokenBurnTransaction, TokenWipeTransaction, TransferTransaction, TopicMessageSubmitTransaction, TransactionId, TokenUpdateNftsTransaction, TokenGrantKycTransaction, ScheduleCreateTransaction, ScheduleSignTransaction, ScheduleDeleteTransaction, ScheduleId, Status, Timestamp, Hbar, FileCreateTransaction, FileAppendTransaction, FileId, } from "@hashgraph/sdk";
|
|
2
|
+
import Long from "long";
|
|
3
|
+
export function normalizePrivateKey(keyStr) {
|
|
4
|
+
const trimmed = keyStr.trim();
|
|
5
|
+
if (trimmed.startsWith("0x") || trimmed.startsWith("0X")) {
|
|
6
|
+
const hex = trimmed.slice(2);
|
|
7
|
+
if (hex.length === 64) {
|
|
8
|
+
try {
|
|
9
|
+
return PrivateKey.fromStringECDSA(trimmed);
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return PrivateKey.fromStringED25519(trimmed);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return PrivateKey.fromStringED25519(trimmed);
|
|
16
|
+
}
|
|
17
|
+
if (trimmed.startsWith("30")) {
|
|
18
|
+
return PrivateKey.fromStringDer(trimmed);
|
|
19
|
+
}
|
|
20
|
+
if (/^[0-9a-fA-F]{64}$/.test(trimmed)) {
|
|
21
|
+
try {
|
|
22
|
+
return PrivateKey.fromStringECDSA(trimmed);
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return PrivateKey.fromStringED25519(trimmed);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return PrivateKey.fromString(trimmed);
|
|
29
|
+
}
|
|
30
|
+
let clientInstance = null;
|
|
31
|
+
function getClient() {
|
|
32
|
+
if (clientInstance)
|
|
33
|
+
return clientInstance;
|
|
34
|
+
const network = process.env.HEDERA_NETWORK ?? "testnet";
|
|
35
|
+
const operatorId = process.env.HEDERA_OPERATOR_ID;
|
|
36
|
+
const operatorKey = process.env.HEDERA_OPERATOR_KEY;
|
|
37
|
+
if (!operatorId || !operatorKey) {
|
|
38
|
+
throw new Error("HEDERA_OPERATOR_ID and HEDERA_OPERATOR_KEY must be set");
|
|
39
|
+
}
|
|
40
|
+
const client = network === "mainnet" ? Client.forMainnet() : Client.forTestnet();
|
|
41
|
+
client.setOperator(AccountId.fromString(operatorId), PrivateKey.fromStringED25519(operatorKey));
|
|
42
|
+
client.setDefaultMaxTransactionFee(new Hbar(50));
|
|
43
|
+
client.setDefaultMaxQueryPayment(new Hbar(1));
|
|
44
|
+
clientInstance = client;
|
|
45
|
+
return client;
|
|
46
|
+
}
|
|
47
|
+
function getSupplyKey() {
|
|
48
|
+
const key = process.env.HEDERA_OPERATOR_KEY;
|
|
49
|
+
if (!key)
|
|
50
|
+
throw new Error("HEDERA_OPERATOR_KEY must be set");
|
|
51
|
+
return PrivateKey.fromStringED25519(key);
|
|
52
|
+
}
|
|
53
|
+
export async function mintPassportNFT(tokenId, ipfsUri) {
|
|
54
|
+
const client = getClient();
|
|
55
|
+
const supplyKey = getSupplyKey();
|
|
56
|
+
const metadata = new TextEncoder().encode(ipfsUri);
|
|
57
|
+
const tx = await new TokenMintTransaction()
|
|
58
|
+
.setTokenId(TokenId.fromString(tokenId))
|
|
59
|
+
.addMetadata(metadata)
|
|
60
|
+
.freezeWith(client)
|
|
61
|
+
.sign(supplyKey);
|
|
62
|
+
const receipt = await (await tx.execute(client)).getReceipt(client);
|
|
63
|
+
return {
|
|
64
|
+
tokenId,
|
|
65
|
+
serial: receipt.serials[0].toNumber(),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
export async function burnPassportNFT(tokenId, serial) {
|
|
69
|
+
const client = getClient();
|
|
70
|
+
const supplyKey = getSupplyKey();
|
|
71
|
+
const tx = await new TokenBurnTransaction()
|
|
72
|
+
.setTokenId(TokenId.fromString(tokenId))
|
|
73
|
+
.setSerials([Long.fromNumber(serial)])
|
|
74
|
+
.freezeWith(client)
|
|
75
|
+
.sign(supplyKey);
|
|
76
|
+
await (await tx.execute(client)).getReceipt(client);
|
|
77
|
+
}
|
|
78
|
+
export async function transferNFTToAgent(tokenId, serial, fromAccountId, toAccountId) {
|
|
79
|
+
const client = getClient();
|
|
80
|
+
const treasuryKey = getSupplyKey();
|
|
81
|
+
const tx = await new TransferTransaction()
|
|
82
|
+
.addNftTransfer(TokenId.fromString(tokenId), serial, AccountId.fromString(fromAccountId), AccountId.fromString(toAccountId))
|
|
83
|
+
.freezeWith(client)
|
|
84
|
+
.sign(treasuryKey);
|
|
85
|
+
const result = await tx.execute(client);
|
|
86
|
+
await result.getReceipt(client);
|
|
87
|
+
}
|
|
88
|
+
export async function grantKyc(tokenId, accountId) {
|
|
89
|
+
const client = getClient();
|
|
90
|
+
const kycKey = getSupplyKey();
|
|
91
|
+
const tx = await new TokenGrantKycTransaction()
|
|
92
|
+
.setTokenId(TokenId.fromString(tokenId))
|
|
93
|
+
.setAccountId(AccountId.fromString(accountId))
|
|
94
|
+
.freezeWith(client)
|
|
95
|
+
.sign(kycKey);
|
|
96
|
+
const result = await tx.execute(client);
|
|
97
|
+
await result.getReceipt(client);
|
|
98
|
+
}
|
|
99
|
+
export async function submitAuditMessage(message) {
|
|
100
|
+
const client = getClient();
|
|
101
|
+
const topicId = process.env.AUDIT_TOPIC_ID;
|
|
102
|
+
if (!topicId)
|
|
103
|
+
throw new Error("AUDIT_TOPIC_ID must be set");
|
|
104
|
+
const messageStr = JSON.stringify(message);
|
|
105
|
+
const tx = new TopicMessageSubmitTransaction()
|
|
106
|
+
.setTopicId(TopicId.fromString(topicId))
|
|
107
|
+
.setMessage(messageStr);
|
|
108
|
+
if (messageStr.length > 1024) {
|
|
109
|
+
tx.setMaxChunks(10);
|
|
110
|
+
}
|
|
111
|
+
const result = await tx.execute(client);
|
|
112
|
+
await result.getReceipt(client);
|
|
113
|
+
return result.transactionId.toString();
|
|
114
|
+
}
|
|
115
|
+
export async function submitDirectoryMessage(message) {
|
|
116
|
+
const client = getClient();
|
|
117
|
+
const topicId = process.env.DIRECTORY_TOPIC_ID;
|
|
118
|
+
if (!topicId)
|
|
119
|
+
throw new Error("DIRECTORY_TOPIC_ID must be set");
|
|
120
|
+
const messageStr = JSON.stringify(message);
|
|
121
|
+
const tx = new TopicMessageSubmitTransaction()
|
|
122
|
+
.setTopicId(TopicId.fromString(topicId))
|
|
123
|
+
.setMessage(messageStr);
|
|
124
|
+
if (messageStr.length > 1024) {
|
|
125
|
+
tx.setMaxChunks(10);
|
|
126
|
+
}
|
|
127
|
+
const result = await tx.execute(client);
|
|
128
|
+
await result.getReceipt(client);
|
|
129
|
+
return result.transactionId.toString();
|
|
130
|
+
}
|
|
131
|
+
export async function submitA2AMessage(message) {
|
|
132
|
+
const client = getClient();
|
|
133
|
+
const topicId = process.env.A2A_TOPIC_ID;
|
|
134
|
+
if (!topicId)
|
|
135
|
+
throw new Error("A2A_TOPIC_ID must be set");
|
|
136
|
+
const messageStr = JSON.stringify(message);
|
|
137
|
+
const tx = new TopicMessageSubmitTransaction()
|
|
138
|
+
.setTopicId(TopicId.fromString(topicId))
|
|
139
|
+
.setMessage(messageStr);
|
|
140
|
+
if (messageStr.length > 1024) {
|
|
141
|
+
tx.setMaxChunks(10);
|
|
142
|
+
}
|
|
143
|
+
const result = await tx.execute(client);
|
|
144
|
+
await result.getReceipt(client);
|
|
145
|
+
return { txId: result.transactionId.toString(), consensusTimestamp: null };
|
|
146
|
+
}
|
|
147
|
+
export async function submitTaskMessage(message) {
|
|
148
|
+
const client = getClient();
|
|
149
|
+
const topicId = process.env.MARKET_TOPIC_ID;
|
|
150
|
+
if (!topicId)
|
|
151
|
+
throw new Error("MARKET_TOPIC_ID must be set");
|
|
152
|
+
const messageStr = JSON.stringify(message);
|
|
153
|
+
const tx = new TopicMessageSubmitTransaction()
|
|
154
|
+
.setTopicId(TopicId.fromString(topicId))
|
|
155
|
+
.setMessage(messageStr);
|
|
156
|
+
if (messageStr.length > 1024) {
|
|
157
|
+
tx.setMaxChunks(10);
|
|
158
|
+
}
|
|
159
|
+
const result = await tx.execute(client);
|
|
160
|
+
await result.getReceipt(client);
|
|
161
|
+
return { txId: result.transactionId.toString(), consensusTimestamp: null };
|
|
162
|
+
}
|
|
163
|
+
export async function prepareTopicMessageTransaction(agentAccountId, message, topicIdOverride) {
|
|
164
|
+
const client = getClient();
|
|
165
|
+
const topicId = topicIdOverride ?? process.env.MARKET_TOPIC_ID;
|
|
166
|
+
if (!topicId)
|
|
167
|
+
throw new Error("Topic ID must be set (pass topicIdOverride or set MARKET_TOPIC_ID)");
|
|
168
|
+
const messageStr = JSON.stringify(message);
|
|
169
|
+
const tx = new TopicMessageSubmitTransaction()
|
|
170
|
+
.setTopicId(TopicId.fromString(topicId))
|
|
171
|
+
.setMessage(messageStr)
|
|
172
|
+
.setTransactionId(TransactionId.generate(AccountId.fromString(agentAccountId)));
|
|
173
|
+
if (messageStr.length > 1024) {
|
|
174
|
+
tx.setMaxChunks(10);
|
|
175
|
+
}
|
|
176
|
+
tx.freezeWith(client);
|
|
177
|
+
const txBytes = Buffer.from(tx.toBytes()).toString("base64");
|
|
178
|
+
const txId = tx.transactionId?.toString() ?? "";
|
|
179
|
+
return { txBytes, txId };
|
|
180
|
+
}
|
|
181
|
+
export async function prepareA2ATopicMessage(agentAccountId, message) {
|
|
182
|
+
const topicId = process.env.A2A_TOPIC_ID;
|
|
183
|
+
if (!topicId)
|
|
184
|
+
throw new Error("A2A_TOPIC_ID must be set");
|
|
185
|
+
return prepareTopicMessageTransaction(agentAccountId, message, topicId);
|
|
186
|
+
}
|
|
187
|
+
export async function submitSignedTopicMessage(txBytesBase64, publicKeyDer, signatureBytes) {
|
|
188
|
+
const client = getClient();
|
|
189
|
+
const txBytes = Buffer.from(txBytesBase64, "base64");
|
|
190
|
+
const tx = Transaction.fromBytes(txBytes);
|
|
191
|
+
const publicKey = PublicKey.fromString(publicKeyDer);
|
|
192
|
+
tx.addSignature(publicKey, signatureBytes);
|
|
193
|
+
const result = await tx.execute(client);
|
|
194
|
+
await result.getReceipt(client);
|
|
195
|
+
return result.transactionId.toString();
|
|
196
|
+
}
|
|
197
|
+
export async function wipeNFT(tokenId, accountId, serial) {
|
|
198
|
+
const client = getClient();
|
|
199
|
+
const wipeKey = getSupplyKey();
|
|
200
|
+
const tx = await new TokenWipeTransaction()
|
|
201
|
+
.setTokenId(TokenId.fromString(tokenId))
|
|
202
|
+
.setAccountId(AccountId.fromString(accountId))
|
|
203
|
+
.setSerials([serial])
|
|
204
|
+
.freezeWith(client)
|
|
205
|
+
.sign(wipeKey);
|
|
206
|
+
await (await tx.execute(client)).getReceipt(client);
|
|
207
|
+
}
|
|
208
|
+
export async function updateNftMetadata(tokenId, serial, newUri) {
|
|
209
|
+
const client = getClient();
|
|
210
|
+
const metadataKey = getSupplyKey();
|
|
211
|
+
const metadata = new TextEncoder().encode(newUri);
|
|
212
|
+
const tx = await new TokenUpdateNftsTransaction()
|
|
213
|
+
.setTokenId(TokenId.fromString(tokenId))
|
|
214
|
+
.setSerialNumbers([Long.fromNumber(serial)])
|
|
215
|
+
.setMetadata(metadata)
|
|
216
|
+
.freezeWith(client)
|
|
217
|
+
.sign(metadataKey);
|
|
218
|
+
await (await tx.execute(client)).getReceipt(client);
|
|
219
|
+
}
|
|
220
|
+
export async function transferHbar(fromAccountId, toAccountId, amountHbar) {
|
|
221
|
+
const client = getClient();
|
|
222
|
+
const tx = new TransferTransaction()
|
|
223
|
+
.addHbarTransfer(AccountId.fromString(fromAccountId), Hbar.fromTinybars(-Math.round(amountHbar * 100_000_000)))
|
|
224
|
+
.addHbarTransfer(AccountId.fromString(toAccountId), Hbar.fromTinybars(Math.round(amountHbar * 100_000_000)));
|
|
225
|
+
const result = await tx.execute(client);
|
|
226
|
+
await result.getReceipt(client);
|
|
227
|
+
return result.transactionId.toString();
|
|
228
|
+
}
|
|
229
|
+
export async function transferHbarWithKey(fromAccountId, fromPrivateKey, toAccountId, amountHbar) {
|
|
230
|
+
const network = process.env.HEDERA_NETWORK ?? "testnet";
|
|
231
|
+
const aid = AccountId.fromString(fromAccountId);
|
|
232
|
+
const pk = normalizePrivateKey(fromPrivateKey);
|
|
233
|
+
const client = network === "mainnet" ? Client.forMainnet() : Client.forTestnet();
|
|
234
|
+
client.setOperator(aid, pk);
|
|
235
|
+
try {
|
|
236
|
+
const tx = new TransferTransaction()
|
|
237
|
+
.addHbarTransfer(aid, Hbar.fromTinybars(-Math.round(amountHbar * 100_000_000)))
|
|
238
|
+
.addHbarTransfer(AccountId.fromString(toAccountId), Hbar.fromTinybars(Math.round(amountHbar * 100_000_000)));
|
|
239
|
+
const result = await tx.execute(client);
|
|
240
|
+
await result.getReceipt(client);
|
|
241
|
+
return result.transactionId.toString();
|
|
242
|
+
}
|
|
243
|
+
finally {
|
|
244
|
+
client.close();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
export async function prepareTransferTransaction(fromAccountId, toAccountId, amountHbar) {
|
|
248
|
+
const client = getClient();
|
|
249
|
+
const tx = new TransferTransaction()
|
|
250
|
+
.addHbarTransfer(AccountId.fromString(fromAccountId), Hbar.fromTinybars(-Math.round(amountHbar * 100_000_000)))
|
|
251
|
+
.addHbarTransfer(AccountId.fromString(toAccountId), Hbar.fromTinybars(Math.round(amountHbar * 100_000_000)));
|
|
252
|
+
const frozenTx = await tx.freezeWith(client);
|
|
253
|
+
const txBytes = Buffer.from(frozenTx.toBytes()).toString("base64");
|
|
254
|
+
const txId = frozenTx.transactionId?.toString();
|
|
255
|
+
if (!txId)
|
|
256
|
+
throw new Error("Failed to generate transaction ID");
|
|
257
|
+
return { txBytes, txId };
|
|
258
|
+
}
|
|
259
|
+
export async function transferHbarWithSignature(txBytesBase64, publicKeyStr, signatureBytes) {
|
|
260
|
+
const client = getClient();
|
|
261
|
+
const txBytes = Buffer.from(txBytesBase64, "base64");
|
|
262
|
+
const tx = Transaction.fromBytes(txBytes);
|
|
263
|
+
const publicKey = PublicKey.fromString(publicKeyStr);
|
|
264
|
+
const sigArray = Array.isArray(signatureBytes) ? signatureBytes : [signatureBytes];
|
|
265
|
+
tx.addSignature(publicKey, sigArray);
|
|
266
|
+
const result = await tx.execute(client);
|
|
267
|
+
await result.getReceipt(client);
|
|
268
|
+
return result.transactionId.toString();
|
|
269
|
+
}
|
|
270
|
+
export async function createScheduledTransfer(fromAccountId, toAccountId, amountHbar, options) {
|
|
271
|
+
const client = getClient();
|
|
272
|
+
const tinybars = Math.round(amountHbar * 100_000_000);
|
|
273
|
+
const transferTx = new TransferTransaction()
|
|
274
|
+
.addHbarTransfer(AccountId.fromString(fromAccountId), Hbar.fromTinybars(-tinybars))
|
|
275
|
+
.addHbarTransfer(AccountId.fromString(toAccountId), Hbar.fromTinybars(tinybars));
|
|
276
|
+
const scheduleTx = new ScheduleCreateTransaction()
|
|
277
|
+
.setScheduledTransaction(transferTx);
|
|
278
|
+
if (options?.adminKey !== false) {
|
|
279
|
+
scheduleTx.setAdminKey(getSupplyKey().publicKey);
|
|
280
|
+
}
|
|
281
|
+
const expirationSeconds = options?.expirationSeconds ?? 86_400;
|
|
282
|
+
const expirationDate = new Date(Date.now() + expirationSeconds * 1_000);
|
|
283
|
+
scheduleTx.setExpirationTime(Timestamp.fromDate(expirationDate));
|
|
284
|
+
const memo = options?.memo ?? `escrow:${fromAccountId}:${toAccountId}:${amountHbar}`;
|
|
285
|
+
scheduleTx.setScheduleMemo(memo);
|
|
286
|
+
const result = await scheduleTx.execute(client);
|
|
287
|
+
const receipt = await result.getReceipt(client);
|
|
288
|
+
if (!receipt.scheduleId) {
|
|
289
|
+
throw new Error("Failed to create scheduled transaction: no scheduleId in receipt");
|
|
290
|
+
}
|
|
291
|
+
const scheduleId = receipt.scheduleId.toString();
|
|
292
|
+
const scheduleTxId = receipt.scheduledTransactionId?.toString() ?? result.transactionId.toString();
|
|
293
|
+
return { scheduleId, scheduleTxId };
|
|
294
|
+
}
|
|
295
|
+
export async function signScheduledTransaction(scheduleId, signerPrivateKey) {
|
|
296
|
+
if (!scheduleId || !scheduleId.trim()) {
|
|
297
|
+
throw new Error("scheduleId must be a non-empty string");
|
|
298
|
+
}
|
|
299
|
+
const client = getClient();
|
|
300
|
+
const pk = normalizePrivateKey(signerPrivateKey);
|
|
301
|
+
const tx = await new ScheduleSignTransaction()
|
|
302
|
+
.setScheduleId(ScheduleId.fromString(scheduleId))
|
|
303
|
+
.freezeWith(client)
|
|
304
|
+
.sign(pk);
|
|
305
|
+
const result = await tx.execute(client);
|
|
306
|
+
const receipt = await result.getReceipt(client);
|
|
307
|
+
const executed = receipt.status === Status.Success;
|
|
308
|
+
if (!executed) {
|
|
309
|
+
throw new Error(`ScheduleSign failed: receipt status ${receipt.status.toString()}`);
|
|
310
|
+
}
|
|
311
|
+
const paymentTxId = receipt.scheduledTransactionId?.toString() ?? result.transactionId.toString();
|
|
312
|
+
return { txId: paymentTxId, executed };
|
|
313
|
+
}
|
|
314
|
+
export async function signScheduledTransactionWithSignature(scheduleId, txBytesBase64, publicKeyStr, signatureBytes) {
|
|
315
|
+
if (!scheduleId || !scheduleId.trim()) {
|
|
316
|
+
throw new Error("scheduleId must be a non-empty string");
|
|
317
|
+
}
|
|
318
|
+
const client = getClient();
|
|
319
|
+
const txBytes = Buffer.from(txBytesBase64, "base64");
|
|
320
|
+
const tx = Transaction.fromBytes(txBytes);
|
|
321
|
+
const publicKey = PublicKey.fromString(publicKeyStr);
|
|
322
|
+
const sigArray = Array.isArray(signatureBytes) ? signatureBytes : [signatureBytes];
|
|
323
|
+
tx.addSignature(publicKey, sigArray);
|
|
324
|
+
const result = await tx.execute(client);
|
|
325
|
+
const receipt = await result.getReceipt(client);
|
|
326
|
+
const executed = receipt.status === Status.Success;
|
|
327
|
+
if (!executed) {
|
|
328
|
+
throw new Error(`ScheduleSign failed: receipt status ${receipt.status.toString()}`);
|
|
329
|
+
}
|
|
330
|
+
const paymentTxId = receipt.scheduledTransactionId?.toString() ?? result.transactionId.toString();
|
|
331
|
+
return { txId: paymentTxId, executed };
|
|
332
|
+
}
|
|
333
|
+
export async function deleteScheduledTransaction(scheduleId) {
|
|
334
|
+
if (!scheduleId || !scheduleId.trim()) {
|
|
335
|
+
throw new Error("scheduleId must be a non-empty string");
|
|
336
|
+
}
|
|
337
|
+
const client = getClient();
|
|
338
|
+
const tx = new ScheduleDeleteTransaction().setScheduleId(ScheduleId.fromString(scheduleId));
|
|
339
|
+
const result = await tx.execute(client);
|
|
340
|
+
const receipt = await result.getReceipt(client);
|
|
341
|
+
const deleted = receipt.status === Status.Success;
|
|
342
|
+
return { scheduleId, deleted };
|
|
343
|
+
}
|
|
344
|
+
const HFS_MAX_SIZE_BYTES = 1024 * 1024;
|
|
345
|
+
const HFS_CHUNK_SIZE = 4095;
|
|
346
|
+
export async function uploadFileToHFS(contents, fileMemo) {
|
|
347
|
+
if (contents.length > HFS_MAX_SIZE_BYTES) {
|
|
348
|
+
throw new Error(`File too large: ${contents.length} bytes exceeds max size of ${HFS_MAX_SIZE_BYTES} bytes (1024 KB)`);
|
|
349
|
+
}
|
|
350
|
+
const client = getClient();
|
|
351
|
+
const fileKey = getSupplyKey();
|
|
352
|
+
const firstChunk = contents.subarray(0, HFS_CHUNK_SIZE);
|
|
353
|
+
const remaining = contents.subarray(HFS_CHUNK_SIZE);
|
|
354
|
+
const createTx = await new FileCreateTransaction()
|
|
355
|
+
.setKeys([fileKey.publicKey])
|
|
356
|
+
.setContents(firstChunk)
|
|
357
|
+
.setMaxTransactionFee(new Hbar(5));
|
|
358
|
+
if (fileMemo) {
|
|
359
|
+
createTx.setFileMemo(fileMemo);
|
|
360
|
+
}
|
|
361
|
+
createTx.freezeWith(client);
|
|
362
|
+
const signedCreate = await createTx.sign(fileKey);
|
|
363
|
+
const createResult = await signedCreate.execute(client);
|
|
364
|
+
const createReceipt = await createResult.getReceipt(client);
|
|
365
|
+
if (!createReceipt.fileId) {
|
|
366
|
+
throw new Error("Failed to create file: no fileId in receipt");
|
|
367
|
+
}
|
|
368
|
+
const fileId = createReceipt.fileId.toString();
|
|
369
|
+
const txId = createResult.transactionId.toString();
|
|
370
|
+
if (remaining.length > 0) {
|
|
371
|
+
for (let offset = 0; offset < remaining.length; offset += HFS_CHUNK_SIZE) {
|
|
372
|
+
const chunk = remaining.subarray(offset, offset + HFS_CHUNK_SIZE);
|
|
373
|
+
const appendTx = await new FileAppendTransaction()
|
|
374
|
+
.setFileId(FileId.fromString(fileId))
|
|
375
|
+
.setContents(chunk)
|
|
376
|
+
.setMaxTransactionFee(new Hbar(5))
|
|
377
|
+
.freezeWith(client)
|
|
378
|
+
.sign(fileKey);
|
|
379
|
+
await (await appendTx.execute(client)).getReceipt(client);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
return { fileId, txId };
|
|
383
|
+
}
|
|
384
|
+
export async function downloadFileFromHFS(fileId) {
|
|
385
|
+
const network = process.env.HEDERA_NETWORK ?? "testnet";
|
|
386
|
+
const mirrorBases = {
|
|
387
|
+
testnet: "https://testnet.mirrornode.hedera.com/api/v1",
|
|
388
|
+
mainnet: "https://mainnet.mirrornode.hedera.com/api/v1",
|
|
389
|
+
previewnet: "https://previewnet.mirrornode.hedera.com/api/v1",
|
|
390
|
+
};
|
|
391
|
+
const base = mirrorBases[network] ?? mirrorBases.testnet;
|
|
392
|
+
const url = `${base}/files/${fileId}/content`;
|
|
393
|
+
const controller = new AbortController();
|
|
394
|
+
const timer = setTimeout(() => controller.abort(), 10_000);
|
|
395
|
+
try {
|
|
396
|
+
const res = await fetch(url, { signal: controller.signal });
|
|
397
|
+
if (!res.ok) {
|
|
398
|
+
if (res.status === 404) {
|
|
399
|
+
throw new Error(`File not found: ${fileId} (404)`);
|
|
400
|
+
}
|
|
401
|
+
throw new Error(`Mirror Node error ${res.status}: ${url}`);
|
|
402
|
+
}
|
|
403
|
+
const arrayBuffer = await res.arrayBuffer();
|
|
404
|
+
return Buffer.from(arrayBuffer);
|
|
405
|
+
}
|
|
406
|
+
catch (err) {
|
|
407
|
+
if (err instanceof DOMException && err.name === "AbortError") {
|
|
408
|
+
throw new Error(`Mirror Node timeout after 10000ms: ${url}`);
|
|
409
|
+
}
|
|
410
|
+
throw err;
|
|
411
|
+
}
|
|
412
|
+
finally {
|
|
413
|
+
clearTimeout(timer);
|
|
414
|
+
}
|
|
415
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type { ChainAdapter, ChainMetadata, ExplorerLinks, EscrowHoldResult, EscrowHoldParams, TransferTokenParams, EventQuery, ChainEvent, } from "./chain-adapter";
|
|
2
|
+
export { HederaChainAdapter } from "./hedera-chain-adapter";
|
|
3
|
+
export { hederaChainAdapter } from "./hedera-chain-adapter-instance";
|
|
4
|
+
export * from "./types";
|
|
5
|
+
export { type DirectoryMessage } from "./hedera.service";
|
|
6
|
+
export { type PaginatedMessages } from "./mirror.service";
|
|
7
|
+
export { mintPassportNFT, transferNFTToAgent, grantKyc, burnPassportNFT, submitAuditMessage, submitDirectoryMessage, submitA2AMessage, submitTaskMessage, prepareTopicMessageTransaction, prepareA2ATopicMessage, submitSignedTopicMessage, wipeNFT, updateNftMetadata, transferHbar, transferHbarWithKey, prepareTransferTransaction, transferHbarWithSignature, signTransactionBytes, type SignatureResult, getNftInfo, getNftsForToken, getNftsForAccount, getTopicMessages, getTopicMessagesPaginated, getTaskMessages, verifyA2ADid, uploadFileToHFS, downloadFileFromHFS, type HFSUploadResult, } from "./services-index";
|
|
8
|
+
export * from "./mock-hedera.service";
|
|
9
|
+
export * from "./mock-mirror.service";
|
|
10
|
+
export * from "./mock-settle";
|
|
11
|
+
export { createScheduledTransfer, signScheduledTransaction, deleteScheduledTransaction, } from "./services-index";
|
|
12
|
+
export { isValidA2ADid, extractTokenAndSerial, didToAccountId, getMessageDirection, } from "./did";
|
|
13
|
+
export { getCatalog, getLlmsTxt, type TierEntry, MCP_TOOLS_INDEX, type McpToolEntry } from "./catalog";
|
|
14
|
+
export { DataHubClient } from "./services-index";
|
|
15
|
+
export type { DataHubSearchResult, DataHubEntity, DataHubSchemaField, DataHubLineage, DataHubAssertion, DataHubAssertionResult, LineageEdge, } from "./datahub.client";
|