@hardkas/sdk 0.8.5-alpha → 0.8.9-alpha

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/index.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import * as _hardkas_config from '@hardkas/config';
2
2
  import { LoadedHardkasConfig } from '@hardkas/config';
3
3
  export { defineHardkasConfig } from '@hardkas/config';
4
+ import { TxPlanArtifact, SignedTxArtifact, TxReceiptArtifact, HardkasArtifactBase, WorkflowArtifact, ExternalHardkasSigner } from '@hardkas/artifacts';
5
+ export { ARTIFACT_SCHEMAS, HARDKAS_VERSION, SignedTxArtifact, TxPlanArtifact, TxReceiptArtifact, TxTraceArtifact, createTxPlanArtifact, writeArtifact } from '@hardkas/artifacts';
4
6
  import { KaspaRpcClient } from '@hardkas/kaspa-rpc';
5
7
  import { EventEnvelope, NetworkId } from '@hardkas/core';
6
8
  export { ArtifactId, HardkasError, KaspaAddress, LineageId, NetworkId, SOMPI_PER_KAS, TxId, formatSompi, parseKasToSompi } from '@hardkas/core';
7
- import { TxPlanArtifact, SignedTxArtifact, TxReceiptArtifact, HardkasArtifactBase, WorkflowArtifact } from '@hardkas/artifacts';
8
- export { ARTIFACT_SCHEMAS, HARDKAS_VERSION, SignedTxArtifact, TxPlanArtifact, TxReceiptArtifact, TxTraceArtifact, createTxPlanArtifact, writeArtifact } from '@hardkas/artifacts';
9
9
  import { HardkasAccount } from '@hardkas/accounts';
10
10
  export { signTxPlanArtifact } from '@hardkas/accounts';
11
11
  import { L2NetworkProfile } from '@hardkas/l2';
@@ -320,6 +320,13 @@ declare class HardkasArtifactsManager {
320
320
  strict?: boolean;
321
321
  enforceMetadata?: boolean;
322
322
  }): Promise<any>;
323
+ /**
324
+ * Migrates a legacy artifact to v4 using a migration receipt.
325
+ */
326
+ migrate(target: any, migrationId: string): Promise<{
327
+ migrated: any;
328
+ receipt: any;
329
+ }>;
323
330
  }
324
331
 
325
332
  interface WorkflowRunOptions {
@@ -361,6 +368,7 @@ interface HardkasOptions {
361
368
  mode?: "developer" | "agent";
362
369
  network?: string;
363
370
  autoBootstrap?: boolean;
371
+ signer?: ExternalHardkasSigner;
364
372
  logger?: {
365
373
  info: (msg: string) => void;
366
374
  warn: (msg: string) => void;
@@ -393,6 +401,7 @@ declare class Hardkas {
393
401
  readonly replay: HardkasReplay;
394
402
  readonly lineage: HardkasLineage;
395
403
  readonly workflow: HardkasWorkflow;
404
+ readonly signer?: ExternalHardkasSigner | undefined;
396
405
  readonly mode: "developer" | "agent";
397
406
  readonly policy: Required<NonNullable<HardkasOptions["policy"]>>;
398
407
  readonly rpc: KaspaRpcClient;
package/dist/index.js CHANGED
@@ -94,15 +94,16 @@ import {
94
94
  verifySignedTxSemantics
95
95
  } from "@hardkas/tx-builder";
96
96
  import {
97
- HARDKAS_VERSION,
98
97
  ARTIFACT_SCHEMAS,
99
- ARTIFACT_VERSION,
100
98
  CURRENT_HASH_VERSION,
101
- getBroadcastableSignedTransaction,
102
- writeArtifact,
99
+ calculateContentHash,
103
100
  getDefaultReceiptPath,
101
+ writeArtifact,
102
+ createLineageTransition,
104
103
  createTxPlanArtifact,
105
- calculateContentHash
104
+ HARDKAS_VERSION,
105
+ ARTIFACT_VERSION,
106
+ getBroadcastableSignedTransaction
106
107
  } from "@hardkas/artifacts";
107
108
  import { coreEvents } from "@hardkas/core";
108
109
  import { signTxPlanArtifact } from "@hardkas/accounts";
@@ -252,14 +253,12 @@ var HardkasTx = class {
252
253
  const newHash = calculateContentHash2(basePlan, CURRENT_HASH_VERSION2);
253
254
  basePlan.contentHash = newHash;
254
255
  if (basePlan.lineage) {
255
- basePlan.lineage.artifactId = newHash;
256
+ basePlan.lineage.lineageId = newHash;
256
257
  basePlan.lineage.parentArtifactId = newHash;
257
258
  basePlan.lineage.rootArtifactId = newHash;
258
259
  const finalHash = calculateContentHash2(basePlan, CURRENT_HASH_VERSION2);
259
260
  basePlan.contentHash = finalHash;
260
261
  basePlan.lineage.artifactId = finalHash;
261
- basePlan.lineage.parentArtifactId = finalHash;
262
- basePlan.lineage.rootArtifactId = finalHash;
263
262
  }
264
263
  this.sdk.artifacts.cacheArtifact(basePlan);
265
264
  if (basePlan.policyRefs && basePlan.policyRefs.length > 0) {
@@ -274,6 +273,29 @@ var HardkasTx = class {
274
273
  if (typeof plan === "object" && plan !== null && plan.contentHash) {
275
274
  await this.sdk.artifacts.verify(plan, { throwOnInvalid: true, strict: true, enforceMetadata: false });
276
275
  }
276
+ if (this.sdk.signer && plan.schema === "hardkas.txPlan") {
277
+ const signedArtifact2 = await this.sdk.signer.signTransaction(plan);
278
+ const { absolutePath: absolutePath2 } = await this.sdk.artifacts.write(signedArtifact2);
279
+ const { coreEvents: coreEvents3 } = await import("@hardkas/core");
280
+ const signedRecord2 = signedArtifact2;
281
+ const artifactId2 = signedRecord2.artifactId || signedArtifact2.signedId || signedRecord2.contentHash;
282
+ coreEvents3.normalizeAndEmit({
283
+ kind: "artifact.created",
284
+ schema: signedArtifact2.schema,
285
+ artifactId: artifactId2,
286
+ network: signedArtifact2.networkId,
287
+ mode: signedArtifact2.mode,
288
+ path: absolutePath2
289
+ });
290
+ coreEvents3.normalizeAndEmit({
291
+ kind: "tx.signed",
292
+ txId: signedArtifact2.txId || artifactId2,
293
+ network: signedArtifact2.networkId,
294
+ mode: signedArtifact2.mode,
295
+ amountSompi: signedArtifact2.amountSompi
296
+ });
297
+ return signedArtifact2;
298
+ }
277
299
  let resolvedAccount;
278
300
  if (typeof account === "string") {
279
301
  resolvedAccount = await this.sdk.accounts.resolve(account);
@@ -348,13 +370,7 @@ var HardkasTx = class {
348
370
  signatures: newSignatures
349
371
  },
350
372
  signatureMetadata: newMeta,
351
- lineage: {
352
- artifactId: "",
353
- // To be computed from contentHash
354
- lineageId: partialTx.lineage?.lineageId || partialTx.contentHash || "0".repeat(64),
355
- parentArtifactId: partialTx.contentHash || partialTx.signedId,
356
- rootArtifactId: partialTx.lineage?.rootArtifactId || partialTx.sourcePlanId
357
- }
373
+ lineage: createLineageTransition(partialTx, "hardkas.signedTx")
358
374
  };
359
375
  if (thresholdReached) {
360
376
  draft.signedTransaction = {
@@ -432,13 +448,7 @@ var HardkasTx = class {
432
448
  signatures
433
449
  },
434
450
  signatureMetadata,
435
- lineage: {
436
- artifactId: "",
437
- // To be computed
438
- lineageId: plan.lineage?.lineageId || plan.contentHash || "0".repeat(64),
439
- parentArtifactId: plan.contentHash || plan.planId,
440
- rootArtifactId: plan.contentHash || plan.planId
441
- },
451
+ lineage: createLineageTransition(plan, "hardkas.signedTx"),
442
452
  ...plan.workflowId ? { workflowId: plan.workflowId } : {}
443
453
  };
444
454
  if (thresholdReached) {
@@ -448,10 +458,15 @@ var HardkasTx = class {
448
458
  };
449
459
  draft.txId = `simulated-${plan.planId}-tx`;
450
460
  }
451
- const hash = calculateContentHash(draft, CURRENT_HASH_VERSION2);
461
+ let hash = calculateContentHash(draft, CURRENT_HASH_VERSION2);
452
462
  draft.signedId = `signed-${hash.slice(0, 16)}`;
453
463
  draft.contentHash = hash;
454
- if (draft.lineage) draft.lineage.artifactId = hash;
464
+ if (draft.lineage) {
465
+ draft.lineage.artifactId = hash;
466
+ hash = calculateContentHash(draft, CURRENT_HASH_VERSION2);
467
+ draft.contentHash = hash;
468
+ draft.lineage.artifactId = hash;
469
+ }
455
470
  signedArtifact = draft;
456
471
  } else {
457
472
  if (resolvedAccount.address !== plan.from.address) {
@@ -694,7 +709,8 @@ var HardkasTx = class {
694
709
  );
695
710
  }
696
711
  const activeNetwork = this.sdk.config.config.defaultNetwork || "simnet";
697
- const isSimulated = activeNetwork === "simulated" || this.sdk.config.config.networks?.[activeNetwork]?.kind === "simulated";
712
+ const isExplicitRpc = typeof urlOrOptions === "string" && (urlOrOptions.startsWith("ws://") || urlOrOptions.startsWith("http://") || urlOrOptions.startsWith("wss://") || urlOrOptions.startsWith("https://"));
713
+ const isSimulated = !isExplicitRpc && (activeNetwork === "simulated" || this.sdk.config.config.networks?.[activeNetwork]?.kind === "simulated");
698
714
  if (isSimulated) {
699
715
  const persistOpt = typeof urlOrOptions === "object" ? urlOrOptions.persist : true;
700
716
  const simOpts = persistOpt !== void 0 ? { persist: persistOpt } : {};
@@ -768,14 +784,7 @@ var HardkasTx = class {
768
784
  ...url ? { rpcUrl: url } : {},
769
785
  ...signedArtifact.workflowId ? { workflowId: signedArtifact.workflowId } : {},
770
786
  tracePath: void 0,
771
- lineage: {
772
- artifactId: "",
773
- // To be computed
774
- lineageId: signedArtifact.lineage?.lineageId || signedArtifact.contentHash || "0".repeat(64),
775
- parentArtifactId: signedArtifact.contentHash || "0".repeat(64),
776
- rootArtifactId: signedArtifact.lineage?.rootArtifactId || "0".repeat(64),
777
- sequence: (signedArtifact.lineage?.sequence || 1) + 1
778
- }
787
+ lineage: createLineageTransition(signedArtifact, "hardkas.txReceipt")
779
788
  };
780
789
  realReceiptBase.contentHash = calculateContentHash(
781
790
  realReceiptBase,
@@ -1368,6 +1377,10 @@ var HardkasArtifactsManager = class {
1368
1377
  */
1369
1378
  async write(artifact, options = {}) {
1370
1379
  const record = artifact;
1380
+ if (!record.hashVersion) {
1381
+ const { CURRENT_HASH_VERSION: CURRENT_HASH_VERSION2 } = await import("@hardkas/artifacts");
1382
+ record.hashVersion = CURRENT_HASH_VERSION2;
1383
+ }
1371
1384
  const hash = record.contentHash || "unknown";
1372
1385
  if (record.planId) this.cache.set(record.planId, artifact);
1373
1386
  if (record.signedId) this.cache.set(record.signedId, artifact);
@@ -1455,17 +1468,15 @@ var HardkasArtifactsManager = class {
1455
1468
  const shortId = id.startsWith("plan-") || id.startsWith("signed-") ? id : id.slice(0, 16);
1456
1469
  for (const file of files) {
1457
1470
  if (!file.endsWith(".json")) continue;
1458
- if (file.includes(id) || file.includes(shortId) || file.includes(id.slice(0, 8))) {
1459
- const fp = path3.join(this.workspace.artifactsDir, file);
1460
- try {
1461
- const content = fs3.readFileSync(fp, "utf-8");
1462
- const obj = JSON.parse(content);
1463
- if (obj.contentHash === id || obj.artifactId === id || obj.planId === id || obj.signedId === id || obj.txId === id) {
1464
- found = file;
1465
- break;
1466
- }
1467
- } catch {
1471
+ const fp = path3.join(this.workspace.artifactsDir, file);
1472
+ try {
1473
+ const content = fs3.readFileSync(fp, "utf-8");
1474
+ const obj = JSON.parse(content);
1475
+ if (obj.contentHash === id || obj.artifactId === id || obj.planId === id || obj.signedId === id || obj.txId === id) {
1476
+ found = file;
1477
+ break;
1468
1478
  }
1479
+ } catch {
1469
1480
  }
1470
1481
  }
1471
1482
  }
@@ -1554,7 +1565,7 @@ var HardkasArtifactsManager = class {
1554
1565
  }
1555
1566
  }
1556
1567
  if (!result.ok) {
1557
- const mappedReason = result.issues[0]?.code === "HASH_MISMATCH" ? "content_hash_mismatch" : result.issues[0]?.code === "MISSING_CONTENT_HASH" ? "missing_content_hash" : result.issues[0]?.code === "MISSING_SIGNATURE" ? "missing_signature" : result.issues[0]?.code === "REFERENCE_MISSING" ? "reference_missing" : result.issues[0]?.code === "REFERENCE_HASH_MISMATCH" ? "reference_hash_mismatch" : result.issues[0]?.code === "POLICY_VIOLATION" ? "policy_violation" : result.issues[0]?.code === "PARENT_MISSING" ? "parent_missing" : "schema_invalid";
1568
+ const mappedReason = result.issues[0]?.code === "HASH_MISMATCH" ? "content_hash_mismatch" : result.issues[0]?.code === "MISSING_CONTENT_HASH" ? "missing_content_hash" : result.issues[0]?.code === "MISSING_SIGNATURE" ? "missing_signature" : result.issues[0]?.code === "REFERENCE_MISSING" ? "reference_missing" : result.issues[0]?.code === "REFERENCE_HASH_MISMATCH" ? "reference_hash_mismatch" : result.issues[0]?.code === "POLICY_VIOLATION" ? "policy_violation" : result.issues[0]?.code === "LEGACY_HASH_VERSION_UNSAFE" ? "legacy_hash_version_unsafe" : result.issues[0]?.code === "PARENT_MISSING" ? "parent_missing" : "schema_invalid";
1558
1569
  if (throwOnInvalid) {
1559
1570
  throw new Error(`Artifact ${id} corrupted or invalid: ` + JSON.stringify(result.issues, null, 2));
1560
1571
  }
@@ -1573,6 +1584,26 @@ var HardkasArtifactsManager = class {
1573
1584
  }
1574
1585
  return result;
1575
1586
  }
1587
+ /**
1588
+ * Migrates a legacy artifact to v4 using a migration receipt.
1589
+ */
1590
+ async migrate(target, migrationId) {
1591
+ let artifact;
1592
+ if (typeof target === "string") {
1593
+ artifact = await this.read(target);
1594
+ } else {
1595
+ artifact = target;
1596
+ }
1597
+ const { migrateArtifactPayload, generateMigrationReceipt } = await import("@hardkas/artifacts");
1598
+ const result = migrateArtifactPayload(artifact, void 0, { strictPolicy: false });
1599
+ if (!result.migrated) {
1600
+ throw new Error(`Artifact ${artifact.artifactId || artifact.contentHash} is already at the target version or cannot be migrated.`);
1601
+ }
1602
+ const receipt = generateMigrationReceipt(artifact, result.artifact, migrationId);
1603
+ await this.write(result.artifact);
1604
+ await this.write(receipt);
1605
+ return { migrated: result.artifact, receipt };
1606
+ }
1576
1607
  };
1577
1608
 
1578
1609
  // src/workflow.ts
@@ -1885,6 +1916,7 @@ var Hardkas = class _Hardkas {
1885
1916
  this.tx = new HardkasTx(this);
1886
1917
  this.l2 = new HardkasL2();
1887
1918
  this.query = new HardkasQuery(this);
1919
+ this.signer = options?.signer;
1888
1920
  this.localnet = new HardkasLocalnet(this);
1889
1921
  this.replay = new HardkasReplay(this);
1890
1922
  this.lineage = new HardkasLineage(this);
@@ -1901,6 +1933,7 @@ var Hardkas = class _Hardkas {
1901
1933
  replay;
1902
1934
  lineage;
1903
1935
  workflow;
1936
+ signer;
1904
1937
  mode;
1905
1938
  policy;
1906
1939
  rpc;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hardkas/sdk",
3
- "version": "0.8.5-alpha",
3
+ "version": "0.8.9-alpha",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -23,18 +23,18 @@
23
23
  }
24
24
  },
25
25
  "dependencies": {
26
- "@hardkas/accounts": "0.8.5-alpha",
27
- "@hardkas/artifacts": "0.8.5-alpha",
28
- "@hardkas/core": "0.8.5-alpha",
29
- "@hardkas/l2": "0.8.5-alpha",
30
- "@hardkas/localnet": "0.8.5-alpha",
31
- "@hardkas/kaspa-rpc": "0.8.5-alpha",
32
- "@hardkas/config": "0.8.5-alpha",
33
- "@hardkas/query": "0.8.5-alpha",
34
- "@hardkas/simulator": "0.8.5-alpha",
35
- "@hardkas/tx-builder": "0.8.5-alpha",
36
- "@hardkas/wallet-adapter": "0.8.5-alpha",
37
- "@hardkas/query-store": "0.8.5-alpha"
26
+ "@hardkas/accounts": "0.8.9-alpha",
27
+ "@hardkas/artifacts": "0.8.9-alpha",
28
+ "@hardkas/config": "0.8.9-alpha",
29
+ "@hardkas/l2": "0.8.9-alpha",
30
+ "@hardkas/core": "0.8.9-alpha",
31
+ "@hardkas/kaspa-rpc": "0.8.9-alpha",
32
+ "@hardkas/localnet": "0.8.9-alpha",
33
+ "@hardkas/query": "0.8.9-alpha",
34
+ "@hardkas/simulator": "0.8.9-alpha",
35
+ "@hardkas/tx-builder": "0.8.9-alpha",
36
+ "@hardkas/query-store": "0.8.9-alpha",
37
+ "@hardkas/wallet-adapter": "0.8.9-alpha"
38
38
  },
39
39
  "devDependencies": {
40
40
  "tsup": "^8.3.5",