@flop-labs/sdk 0.0.0-stage → 0.4.0
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/CHANGELOG.md +315 -0
- package/LICENSE +238 -0
- package/README.md +622 -2
- package/dist/a2a/agent-card.d.ts +56 -0
- package/dist/a2a/agent-card.d.ts.map +1 -0
- package/dist/a2a/agent-card.js +111 -0
- package/dist/a2a/agent-card.js.map +1 -0
- package/dist/a2a/index.d.ts +11 -0
- package/dist/a2a/index.d.ts.map +1 -0
- package/dist/a2a/index.js +11 -0
- package/dist/a2a/index.js.map +1 -0
- package/dist/a2a/task-lifecycle.d.ts +41 -0
- package/dist/a2a/task-lifecycle.d.ts.map +1 -0
- package/dist/a2a/task-lifecycle.js +60 -0
- package/dist/a2a/task-lifecycle.js.map +1 -0
- package/dist/a2a/types.d.ts +96 -0
- package/dist/a2a/types.d.ts.map +1 -0
- package/dist/a2a/types.js +23 -0
- package/dist/a2a/types.js.map +1 -0
- package/dist/a2a-cjs/agent-card.js +115 -0
- package/dist/a2a-cjs/agent-card.js.map +1 -0
- package/dist/a2a-cjs/index.js +21 -0
- package/dist/a2a-cjs/index.js.map +1 -0
- package/dist/a2a-cjs/package.json +1 -0
- package/dist/a2a-cjs/task-lifecycle.js +64 -0
- package/dist/a2a-cjs/task-lifecycle.js.map +1 -0
- package/dist/a2a-cjs/types.js +26 -0
- package/dist/a2a-cjs/types.js.map +1 -0
- package/dist/adaptor.d.ts +39 -0
- package/dist/adaptor.d.ts.map +1 -0
- package/dist/adaptor.js +130 -0
- package/dist/adaptor.js.map +1 -0
- package/dist/agent/errors.d.ts +95 -0
- package/dist/agent/errors.d.ts.map +1 -0
- package/dist/agent/errors.js +132 -0
- package/dist/agent/errors.js.map +1 -0
- package/dist/agent/task-delegation.d.ts +283 -0
- package/dist/agent/task-delegation.d.ts.map +1 -0
- package/dist/agent/task-delegation.js +444 -0
- package/dist/agent/task-delegation.js.map +1 -0
- package/dist/agent/types.d.ts +19 -0
- package/dist/agent/types.d.ts.map +1 -0
- package/dist/agent/types.js +7 -0
- package/dist/agent/types.js.map +1 -0
- package/dist/agent-wallet.d.ts +197 -0
- package/dist/agent-wallet.d.ts.map +1 -0
- package/dist/agent-wallet.js +408 -0
- package/dist/agent-wallet.js.map +1 -0
- package/dist/amhl.d.ts +61 -0
- package/dist/amhl.d.ts.map +1 -0
- package/dist/amhl.js +161 -0
- package/dist/amhl.js.map +1 -0
- package/dist/attestation.d.ts +126 -0
- package/dist/attestation.d.ts.map +1 -0
- package/dist/attestation.js +287 -0
- package/dist/attestation.js.map +1 -0
- package/dist/bip340-adaptor.d.ts +8 -0
- package/dist/bip340-adaptor.d.ts.map +1 -0
- package/dist/bip340-adaptor.js +129 -0
- package/dist/bip340-adaptor.js.map +1 -0
- package/dist/btc-relayer.d.ts +42 -0
- package/dist/btc-relayer.d.ts.map +1 -0
- package/dist/btc-relayer.js +40 -0
- package/dist/btc-relayer.js.map +1 -0
- package/dist/bundle/index.esm.js +2 -0
- package/dist/bundle/index.esm.js.map +1 -0
- package/dist/bundle/index.umd.cjs +2 -0
- package/dist/bundle/index.umd.cjs.map +1 -0
- package/dist/capability-claim-sink.d.ts +5 -0
- package/dist/capability-claim-sink.d.ts.map +1 -0
- package/dist/capability-claim-sink.js +6 -0
- package/dist/capability-claim-sink.js.map +1 -0
- package/dist/chain.d.ts +63 -0
- package/dist/chain.d.ts.map +1 -0
- package/dist/chain.js +100 -0
- package/dist/chain.js.map +1 -0
- package/dist/client.d.ts +356 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +863 -0
- package/dist/client.js.map +1 -0
- package/dist/compute-channel/adaptor-settlement.d.ts +19 -0
- package/dist/compute-channel/adaptor-settlement.d.ts.map +1 -0
- package/dist/compute-channel/adaptor-settlement.js +40 -0
- package/dist/compute-channel/adaptor-settlement.js.map +1 -0
- package/dist/compute-channel/agent-client.d.ts +141 -0
- package/dist/compute-channel/agent-client.d.ts.map +1 -0
- package/dist/compute-channel/agent-client.js +265 -0
- package/dist/compute-channel/agent-client.js.map +1 -0
- package/dist/compute-channel/agent-driver.d.ts +59 -0
- package/dist/compute-channel/agent-driver.d.ts.map +1 -0
- package/dist/compute-channel/agent-driver.js +364 -0
- package/dist/compute-channel/agent-driver.js.map +1 -0
- package/dist/compute-channel/chain.d.ts +245 -0
- package/dist/compute-channel/chain.d.ts.map +1 -0
- package/dist/compute-channel/chain.js +490 -0
- package/dist/compute-channel/chain.js.map +1 -0
- package/dist/compute-channel/chat-render.d.ts +21 -0
- package/dist/compute-channel/chat-render.d.ts.map +1 -0
- package/dist/compute-channel/chat-render.js +87 -0
- package/dist/compute-channel/chat-render.js.map +1 -0
- package/dist/compute-channel/crypto.d.ts +19 -0
- package/dist/compute-channel/crypto.d.ts.map +1 -0
- package/dist/compute-channel/crypto.js +52 -0
- package/dist/compute-channel/crypto.js.map +1 -0
- package/dist/compute-channel/da.d.ts +111 -0
- package/dist/compute-channel/da.d.ts.map +1 -0
- package/dist/compute-channel/da.js +320 -0
- package/dist/compute-channel/da.js.map +1 -0
- package/dist/compute-channel/deepseek-v41.d.ts +56 -0
- package/dist/compute-channel/deepseek-v41.d.ts.map +1 -0
- package/dist/compute-channel/deepseek-v41.js +267 -0
- package/dist/compute-channel/deepseek-v41.js.map +1 -0
- package/dist/compute-channel/direct-session-chain.d.ts +3 -0
- package/dist/compute-channel/direct-session-chain.d.ts.map +1 -0
- package/dist/compute-channel/direct-session-chain.js +245 -0
- package/dist/compute-channel/direct-session-chain.js.map +1 -0
- package/dist/compute-channel/direct-session.d.ts +78 -0
- package/dist/compute-channel/direct-session.d.ts.map +1 -0
- package/dist/compute-channel/direct-session.js +208 -0
- package/dist/compute-channel/direct-session.js.map +1 -0
- package/dist/compute-channel/dispute.d.ts +103 -0
- package/dist/compute-channel/dispute.d.ts.map +1 -0
- package/dist/compute-channel/dispute.js +314 -0
- package/dist/compute-channel/dispute.js.map +1 -0
- package/dist/compute-channel/flop-meter.d.ts +96 -0
- package/dist/compute-channel/flop-meter.d.ts.map +1 -0
- package/dist/compute-channel/flop-meter.js +210 -0
- package/dist/compute-channel/flop-meter.js.map +1 -0
- package/dist/compute-channel/flop-submitter.d.ts +40 -0
- package/dist/compute-channel/flop-submitter.d.ts.map +1 -0
- package/dist/compute-channel/flop-submitter.js +134 -0
- package/dist/compute-channel/flop-submitter.js.map +1 -0
- package/dist/compute-channel/hybrid-candidate.d.ts +22 -0
- package/dist/compute-channel/hybrid-candidate.d.ts.map +1 -0
- package/dist/compute-channel/hybrid-candidate.js +155 -0
- package/dist/compute-channel/hybrid-candidate.js.map +1 -0
- package/dist/compute-channel/hybrid-metering.d.ts +36 -0
- package/dist/compute-channel/hybrid-metering.d.ts.map +1 -0
- package/dist/compute-channel/hybrid-metering.js +69 -0
- package/dist/compute-channel/hybrid-metering.js.map +1 -0
- package/dist/compute-channel/hybrid.d.ts +16 -0
- package/dist/compute-channel/hybrid.d.ts.map +1 -0
- package/dist/compute-channel/hybrid.js +98 -0
- package/dist/compute-channel/hybrid.js.map +1 -0
- package/dist/compute-channel/index.d.ts +47 -0
- package/dist/compute-channel/index.d.ts.map +1 -0
- package/dist/compute-channel/index.js +48 -0
- package/dist/compute-channel/index.js.map +1 -0
- package/dist/compute-channel/lane-producer.d.ts +92 -0
- package/dist/compute-channel/lane-producer.d.ts.map +1 -0
- package/dist/compute-channel/lane-producer.js +265 -0
- package/dist/compute-channel/lane-producer.js.map +1 -0
- package/dist/compute-channel/layered.d.ts +77 -0
- package/dist/compute-channel/layered.d.ts.map +1 -0
- package/dist/compute-channel/layered.js +220 -0
- package/dist/compute-channel/layered.js.map +1 -0
- package/dist/compute-channel/lineage.d.ts +19 -0
- package/dist/compute-channel/lineage.d.ts.map +1 -0
- package/dist/compute-channel/lineage.js +28 -0
- package/dist/compute-channel/lineage.js.map +1 -0
- package/dist/compute-channel/miner-host.d.ts +329 -0
- package/dist/compute-channel/miner-host.d.ts.map +1 -0
- package/dist/compute-channel/miner-host.js +880 -0
- package/dist/compute-channel/miner-host.js.map +1 -0
- package/dist/compute-channel/model-config.d.ts +87 -0
- package/dist/compute-channel/model-config.d.ts.map +1 -0
- package/dist/compute-channel/model-config.js +697 -0
- package/dist/compute-channel/model-config.js.map +1 -0
- package/dist/compute-channel/offers.d.ts +57 -0
- package/dist/compute-channel/offers.d.ts.map +1 -0
- package/dist/compute-channel/offers.js +182 -0
- package/dist/compute-channel/offers.js.map +1 -0
- package/dist/compute-channel/point-claim-sink.d.ts +5 -0
- package/dist/compute-channel/point-claim-sink.d.ts.map +1 -0
- package/dist/compute-channel/point-claim-sink.js +6 -0
- package/dist/compute-channel/point-claim-sink.js.map +1 -0
- package/dist/compute-channel/protocol.d.ts +243 -0
- package/dist/compute-channel/protocol.d.ts.map +1 -0
- package/dist/compute-channel/protocol.js +249 -0
- package/dist/compute-channel/protocol.js.map +1 -0
- package/dist/compute-channel/runtime-storage.d.ts +6 -0
- package/dist/compute-channel/runtime-storage.d.ts.map +1 -0
- package/dist/compute-channel/runtime-storage.js +11 -0
- package/dist/compute-channel/runtime-storage.js.map +1 -0
- package/dist/compute-channel/sampled-audit.d.ts +24 -0
- package/dist/compute-channel/sampled-audit.d.ts.map +1 -0
- package/dist/compute-channel/sampled-audit.js +97 -0
- package/dist/compute-channel/sampled-audit.js.map +1 -0
- package/dist/compute-channel/session-auth.d.ts +6 -0
- package/dist/compute-channel/session-auth.d.ts.map +1 -0
- package/dist/compute-channel/session-auth.js +23 -0
- package/dist/compute-channel/session-auth.js.map +1 -0
- package/dist/compute-channel/session-estimate.d.ts +103 -0
- package/dist/compute-channel/session-estimate.d.ts.map +1 -0
- package/dist/compute-channel/session-estimate.js +140 -0
- package/dist/compute-channel/session-estimate.js.map +1 -0
- package/dist/compute-channel/session-prior-bank.d.ts +61 -0
- package/dist/compute-channel/session-prior-bank.d.ts.map +1 -0
- package/dist/compute-channel/session-prior-bank.js +74 -0
- package/dist/compute-channel/session-prior-bank.js.map +1 -0
- package/dist/compute-channel/session-prior.d.ts +145 -0
- package/dist/compute-channel/session-prior.d.ts.map +1 -0
- package/dist/compute-channel/session-prior.js +325 -0
- package/dist/compute-channel/session-prior.js.map +1 -0
- package/dist/compute-channel/session-store.d.ts +51 -0
- package/dist/compute-channel/session-store.d.ts.map +1 -0
- package/dist/compute-channel/session-store.js +186 -0
- package/dist/compute-channel/session-store.js.map +1 -0
- package/dist/compute-channel/session.d.ts +120 -0
- package/dist/compute-channel/session.d.ts.map +1 -0
- package/dist/compute-channel/session.js +232 -0
- package/dist/compute-channel/session.js.map +1 -0
- package/dist/compute-channel/toploc-checker.d.ts +384 -0
- package/dist/compute-channel/toploc-checker.d.ts.map +1 -0
- package/dist/compute-channel/toploc-checker.js +794 -0
- package/dist/compute-channel/toploc-checker.js.map +1 -0
- package/dist/compute-channel/toploc-completion-rpc.d.ts +26 -0
- package/dist/compute-channel/toploc-completion-rpc.d.ts.map +1 -0
- package/dist/compute-channel/toploc-completion-rpc.js +391 -0
- package/dist/compute-channel/toploc-completion-rpc.js.map +1 -0
- package/dist/compute-channel/toploc-completion.d.ts +109 -0
- package/dist/compute-channel/toploc-completion.d.ts.map +1 -0
- package/dist/compute-channel/toploc-completion.js +301 -0
- package/dist/compute-channel/toploc-completion.js.map +1 -0
- package/dist/compute-channel/toploc-evidence.d.ts +185 -0
- package/dist/compute-channel/toploc-evidence.d.ts.map +1 -0
- package/dist/compute-channel/toploc-evidence.js +299 -0
- package/dist/compute-channel/toploc-evidence.js.map +1 -0
- package/dist/compute-channel/toploc-gumbel.d.ts +149 -0
- package/dist/compute-channel/toploc-gumbel.d.ts.map +1 -0
- package/dist/compute-channel/toploc-gumbel.js +245 -0
- package/dist/compute-channel/toploc-gumbel.js.map +1 -0
- package/dist/compute-channel/toploc.d.ts +63 -0
- package/dist/compute-channel/toploc.d.ts.map +1 -0
- package/dist/compute-channel/toploc.js +197 -0
- package/dist/compute-channel/toploc.js.map +1 -0
- package/dist/compute-channel/transcript.d.ts +127 -0
- package/dist/compute-channel/transcript.d.ts.map +1 -0
- package/dist/compute-channel/transcript.js +279 -0
- package/dist/compute-channel/transcript.js.map +1 -0
- package/dist/compute-channel/transport-http.d.ts +217 -0
- package/dist/compute-channel/transport-http.d.ts.map +1 -0
- package/dist/compute-channel/transport-http.js +625 -0
- package/dist/compute-channel/transport-http.js.map +1 -0
- package/dist/coordination.d.ts +341 -0
- package/dist/coordination.d.ts.map +1 -0
- package/dist/coordination.js +481 -0
- package/dist/coordination.js.map +1 -0
- package/dist/da-client.d.ts +124 -0
- package/dist/da-client.d.ts.map +1 -0
- package/dist/da-client.js +127 -0
- package/dist/da-client.js.map +1 -0
- package/dist/discovery/capabilities.d.ts +129 -0
- package/dist/discovery/capabilities.d.ts.map +1 -0
- package/dist/discovery/capabilities.js +398 -0
- package/dist/discovery/capabilities.js.map +1 -0
- package/dist/discovery/discovery-service.d.ts +476 -0
- package/dist/discovery/discovery-service.d.ts.map +1 -0
- package/dist/discovery/discovery-service.js +1005 -0
- package/dist/discovery/discovery-service.js.map +1 -0
- package/dist/discovery/examples.d.ts +50 -0
- package/dist/discovery/examples.d.ts.map +1 -0
- package/dist/discovery/examples.js +163 -0
- package/dist/discovery/examples.js.map +1 -0
- package/dist/discovery/fee-estimation.d.ts +550 -0
- package/dist/discovery/fee-estimation.d.ts.map +1 -0
- package/dist/discovery/fee-estimation.js +921 -0
- package/dist/discovery/fee-estimation.js.map +1 -0
- package/dist/discovery/index.d.ts +38 -0
- package/dist/discovery/index.d.ts.map +1 -0
- package/dist/discovery/index.js +32 -0
- package/dist/discovery/index.js.map +1 -0
- package/dist/discovery/metered-pricing.d.ts +100 -0
- package/dist/discovery/metered-pricing.d.ts.map +1 -0
- package/dist/discovery/metered-pricing.js +150 -0
- package/dist/discovery/metered-pricing.js.map +1 -0
- package/dist/discovery/priority.d.ts +16 -0
- package/dist/discovery/priority.d.ts.map +1 -0
- package/dist/discovery/priority.js +17 -0
- package/dist/discovery/priority.js.map +1 -0
- package/dist/discovery/scoring.d.ts +231 -0
- package/dist/discovery/scoring.d.ts.map +1 -0
- package/dist/discovery/scoring.js +354 -0
- package/dist/discovery/scoring.js.map +1 -0
- package/dist/ecdsa-adaptor.d.ts +72 -0
- package/dist/ecdsa-adaptor.d.ts.map +1 -0
- package/dist/ecdsa-adaptor.js +351 -0
- package/dist/ecdsa-adaptor.js.map +1 -0
- package/dist/errors.d.ts +98 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +190 -0
- package/dist/errors.js.map +1 -0
- package/dist/escrow.d.ts +116 -0
- package/dist/escrow.d.ts.map +1 -0
- package/dist/escrow.js +172 -0
- package/dist/escrow.js.map +1 -0
- package/dist/evm-relayer.d.ts +12 -0
- package/dist/evm-relayer.d.ts.map +1 -0
- package/dist/evm-relayer.js +25 -0
- package/dist/evm-relayer.js.map +1 -0
- package/dist/experimental.d.ts +3 -0
- package/dist/experimental.d.ts.map +1 -0
- package/dist/experimental.js +3 -0
- package/dist/experimental.js.map +1 -0
- package/dist/faucet.d.ts +141 -0
- package/dist/faucet.d.ts.map +1 -0
- package/dist/faucet.js +312 -0
- package/dist/faucet.js.map +1 -0
- package/dist/finality-status.d.ts +21 -0
- package/dist/finality-status.d.ts.map +1 -0
- package/dist/finality-status.js +10 -0
- package/dist/finality-status.js.map +1 -0
- package/dist/generated-client/capabilities.d.ts +83 -0
- package/dist/generated-client/capabilities.d.ts.map +1 -0
- package/dist/generated-client/capabilities.js +87 -0
- package/dist/generated-client/capabilities.js.map +1 -0
- package/dist/generated-client/cli.d.ts +3 -0
- package/dist/generated-client/cli.d.ts.map +1 -0
- package/dist/generated-client/cli.js +33 -0
- package/dist/generated-client/cli.js.map +1 -0
- package/dist/generated-client/errors.d.ts +13 -0
- package/dist/generated-client/errors.d.ts.map +1 -0
- package/dist/generated-client/errors.js +23 -0
- package/dist/generated-client/errors.js.map +1 -0
- package/dist/generated-client/factory.d.ts +28 -0
- package/dist/generated-client/factory.d.ts.map +1 -0
- package/dist/generated-client/factory.js +182 -0
- package/dist/generated-client/factory.js.map +1 -0
- package/dist/generated-client/generator.d.ts +9 -0
- package/dist/generated-client/generator.d.ts.map +1 -0
- package/dist/generated-client/generator.js +80 -0
- package/dist/generated-client/generator.js.map +1 -0
- package/dist/generated-client/index.d.ts +11 -0
- package/dist/generated-client/index.d.ts.map +1 -0
- package/dist/generated-client/index.js +6 -0
- package/dist/generated-client/index.js.map +1 -0
- package/dist/generated-client/profile.d.ts +166 -0
- package/dist/generated-client/profile.d.ts.map +1 -0
- package/dist/generated-client/profile.js +158 -0
- package/dist/generated-client/profile.js.map +1 -0
- package/dist/generated-client/templates/agent-chat.d.ts +16 -0
- package/dist/generated-client/templates/agent-chat.d.ts.map +1 -0
- package/dist/generated-client/templates/agent-chat.js +12 -0
- package/dist/generated-client/templates/agent-chat.js.map +1 -0
- package/dist/generated-client/templates/index.d.ts +4 -0
- package/dist/generated-client/templates/index.d.ts.map +1 -0
- package/dist/generated-client/templates/index.js +4 -0
- package/dist/generated-client/templates/index.js.map +1 -0
- package/dist/generated-client/templates/miner-operator.d.ts +16 -0
- package/dist/generated-client/templates/miner-operator.d.ts.map +1 -0
- package/dist/generated-client/templates/miner-operator.js +12 -0
- package/dist/generated-client/templates/miner-operator.js.map +1 -0
- package/dist/generated-client/templates/model-publisher.d.ts +21 -0
- package/dist/generated-client/templates/model-publisher.d.ts.map +1 -0
- package/dist/generated-client/templates/model-publisher.js +13 -0
- package/dist/generated-client/templates/model-publisher.js.map +1 -0
- package/dist/hardware.d.ts +68 -0
- package/dist/hardware.d.ts.map +1 -0
- package/dist/hardware.js +222 -0
- package/dist/hardware.js.map +1 -0
- package/dist/index.d.ts +109 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +140 -0
- package/dist/index.js.map +1 -0
- package/dist/model-loader/canonical.d.ts +79 -0
- package/dist/model-loader/canonical.d.ts.map +1 -0
- package/dist/model-loader/canonical.js +116 -0
- package/dist/model-loader/canonical.js.map +1 -0
- package/dist/model-loader/dir.d.ts +26 -0
- package/dist/model-loader/dir.d.ts.map +1 -0
- package/dist/model-loader/dir.js +109 -0
- package/dist/model-loader/dir.js.map +1 -0
- package/dist/model-loader/dmverity.d.ts +78 -0
- package/dist/model-loader/dmverity.d.ts.map +1 -0
- package/dist/model-loader/dmverity.js +141 -0
- package/dist/model-loader/dmverity.js.map +1 -0
- package/dist/model-loader/download-state.d.ts +24 -0
- package/dist/model-loader/download-state.d.ts.map +1 -0
- package/dist/model-loader/download-state.js +99 -0
- package/dist/model-loader/download-state.js.map +1 -0
- package/dist/model-loader/fetch.d.ts +51 -0
- package/dist/model-loader/fetch.d.ts.map +1 -0
- package/dist/model-loader/fetch.js +410 -0
- package/dist/model-loader/fetch.js.map +1 -0
- package/dist/model-loader/file-table.d.ts +38 -0
- package/dist/model-loader/file-table.d.ts.map +1 -0
- package/dist/model-loader/file-table.js +106 -0
- package/dist/model-loader/file-table.js.map +1 -0
- package/dist/model-loader/hf-preflight.d.ts +65 -0
- package/dist/model-loader/hf-preflight.d.ts.map +1 -0
- package/dist/model-loader/hf-preflight.js +179 -0
- package/dist/model-loader/hf-preflight.js.map +1 -0
- package/dist/model-loader/index.d.ts +27 -0
- package/dist/model-loader/index.d.ts.map +1 -0
- package/dist/model-loader/index.js +21 -0
- package/dist/model-loader/index.js.map +1 -0
- package/dist/model-loader/registry.d.ts +24 -0
- package/dist/model-loader/registry.d.ts.map +1 -0
- package/dist/model-loader/registry.js +105 -0
- package/dist/model-loader/registry.js.map +1 -0
- package/dist/model-loader/types.d.ts +101 -0
- package/dist/model-loader/types.d.ts.map +1 -0
- package/dist/model-loader/types.js +13 -0
- package/dist/model-loader/types.js.map +1 -0
- package/dist/model-loader/verify.d.ts +83 -0
- package/dist/model-loader/verify.d.ts.map +1 -0
- package/dist/model-loader/verify.js +138 -0
- package/dist/model-loader/verify.js.map +1 -0
- package/dist/model-qualification.d.ts +79 -0
- package/dist/model-qualification.d.ts.map +1 -0
- package/dist/model-qualification.js +135 -0
- package/dist/model-qualification.js.map +1 -0
- package/dist/near-relayer.d.ts +30 -0
- package/dist/near-relayer.d.ts.map +1 -0
- package/dist/near-relayer.js +81 -0
- package/dist/near-relayer.js.map +1 -0
- package/dist/paid-storage/bytes.d.ts +21 -0
- package/dist/paid-storage/bytes.d.ts.map +1 -0
- package/dist/paid-storage/bytes.js +71 -0
- package/dist/paid-storage/bytes.js.map +1 -0
- package/dist/paid-storage/chain.d.ts +20 -0
- package/dist/paid-storage/chain.d.ts.map +1 -0
- package/dist/paid-storage/chain.js +182 -0
- package/dist/paid-storage/chain.js.map +1 -0
- package/dist/paid-storage/client.d.ts +161 -0
- package/dist/paid-storage/client.d.ts.map +1 -0
- package/dist/paid-storage/client.js +1038 -0
- package/dist/paid-storage/client.js.map +1 -0
- package/dist/paid-storage/cloudflare.d.ts +4 -0
- package/dist/paid-storage/cloudflare.d.ts.map +1 -0
- package/dist/paid-storage/cloudflare.js +9 -0
- package/dist/paid-storage/cloudflare.js.map +1 -0
- package/dist/paid-storage/codec.d.ts +8 -0
- package/dist/paid-storage/codec.d.ts.map +1 -0
- package/dist/paid-storage/codec.js +92 -0
- package/dist/paid-storage/codec.js.map +1 -0
- package/dist/paid-storage/crypto.d.ts +27 -0
- package/dist/paid-storage/crypto.d.ts.map +1 -0
- package/dist/paid-storage/crypto.js +91 -0
- package/dist/paid-storage/crypto.js.map +1 -0
- package/dist/paid-storage/heads.d.ts +26 -0
- package/dist/paid-storage/heads.d.ts.map +1 -0
- package/dist/paid-storage/heads.js +208 -0
- package/dist/paid-storage/heads.js.map +1 -0
- package/dist/paid-storage/index.d.ts +14 -0
- package/dist/paid-storage/index.d.ts.map +1 -0
- package/dist/paid-storage/index.js +7 -0
- package/dist/paid-storage/index.js.map +1 -0
- package/dist/paid-storage/memory-wire.d.ts +85 -0
- package/dist/paid-storage/memory-wire.d.ts.map +1 -0
- package/dist/paid-storage/memory-wire.js +147 -0
- package/dist/paid-storage/memory-wire.js.map +1 -0
- package/dist/paid-storage/node-store.d.ts +9 -0
- package/dist/paid-storage/node-store.d.ts.map +1 -0
- package/dist/paid-storage/node-store.js +34 -0
- package/dist/paid-storage/node-store.js.map +1 -0
- package/dist/paid-storage/profile.d.ts +21 -0
- package/dist/paid-storage/profile.d.ts.map +1 -0
- package/dist/paid-storage/profile.js +22 -0
- package/dist/paid-storage/profile.js.map +1 -0
- package/dist/paid-storage/store.d.ts +18 -0
- package/dist/paid-storage/store.d.ts.map +1 -0
- package/dist/paid-storage/store.js +32 -0
- package/dist/paid-storage/store.js.map +1 -0
- package/dist/paid-storage/transport.d.ts +70 -0
- package/dist/paid-storage/transport.d.ts.map +1 -0
- package/dist/paid-storage/transport.js +140 -0
- package/dist/paid-storage/transport.js.map +1 -0
- package/dist/paid-storage/vouchers.d.ts +75 -0
- package/dist/paid-storage/vouchers.d.ts.map +1 -0
- package/dist/paid-storage/vouchers.js +239 -0
- package/dist/paid-storage/vouchers.js.map +1 -0
- package/dist/paid-storage-wire.d.ts +11 -0
- package/dist/paid-storage-wire.d.ts.map +1 -0
- package/dist/paid-storage-wire.js +184 -0
- package/dist/paid-storage-wire.js.map +1 -0
- package/dist/point-claims.d.ts +40 -0
- package/dist/point-claims.d.ts.map +1 -0
- package/dist/point-claims.js +98 -0
- package/dist/point-claims.js.map +1 -0
- package/dist/quickstart.d.ts +307 -0
- package/dist/quickstart.d.ts.map +1 -0
- package/dist/quickstart.js +536 -0
- package/dist/quickstart.js.map +1 -0
- package/dist/react/context.d.ts +10 -0
- package/dist/react/context.d.ts.map +1 -0
- package/dist/react/context.js +16 -0
- package/dist/react/context.js.map +1 -0
- package/dist/react/hooks.d.ts +206 -0
- package/dist/react/hooks.d.ts.map +1 -0
- package/dist/react/hooks.js +560 -0
- package/dist/react/hooks.js.map +1 -0
- package/dist/react/index.d.ts +4 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +4 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/provider.d.ts +18 -0
- package/dist/react/provider.d.ts.map +1 -0
- package/dist/react/provider.js +17 -0
- package/dist/react/provider.js.map +1 -0
- package/dist/relayer.d.ts +122 -0
- package/dist/relayer.d.ts.map +1 -0
- package/dist/relayer.js +203 -0
- package/dist/relayer.js.map +1 -0
- package/dist/runtime-compatibility.d.ts +8 -0
- package/dist/runtime-compatibility.d.ts.map +1 -0
- package/dist/runtime-compatibility.js +20 -0
- package/dist/runtime-compatibility.js.map +1 -0
- package/dist/session-manager.d.ts +197 -0
- package/dist/session-manager.d.ts.map +1 -0
- package/dist/session-manager.js +429 -0
- package/dist/session-manager.js.map +1 -0
- package/dist/solana-ptlc.d.ts +64 -0
- package/dist/solana-ptlc.d.ts.map +1 -0
- package/dist/solana-ptlc.js +137 -0
- package/dist/solana-ptlc.js.map +1 -0
- package/dist/staking-client.d.ts +103 -0
- package/dist/staking-client.d.ts.map +1 -0
- package/dist/staking-client.js +308 -0
- package/dist/staking-client.js.map +1 -0
- package/dist/staking-types.d.ts +203 -0
- package/dist/staking-types.d.ts.map +1 -0
- package/dist/staking-types.js +16 -0
- package/dist/staking-types.js.map +1 -0
- package/dist/submit-tx.d.ts +14 -0
- package/dist/submit-tx.d.ts.map +1 -0
- package/dist/submit-tx.js +33 -0
- package/dist/submit-tx.js.map +1 -0
- package/dist/templates/index.d.ts +52 -0
- package/dist/templates/index.d.ts.map +1 -0
- package/dist/templates/index.js +123 -0
- package/dist/templates/index.js.map +1 -0
- package/dist/ton/index.d.ts +52 -0
- package/dist/ton/index.d.ts.map +1 -0
- package/dist/ton/index.js +163 -0
- package/dist/ton/index.js.map +1 -0
- package/dist/ton-relayer.d.ts +26 -0
- package/dist/ton-relayer.d.ts.map +1 -0
- package/dist/ton-relayer.js +22 -0
- package/dist/ton-relayer.js.map +1 -0
- package/dist/tracing.d.ts +48 -0
- package/dist/tracing.d.ts.map +1 -0
- package/dist/tracing.js +108 -0
- package/dist/tracing.js.map +1 -0
- package/dist/transaction-signer.d.ts +13 -0
- package/dist/transaction-signer.d.ts.map +1 -0
- package/dist/transaction-signer.js +13 -0
- package/dist/transaction-signer.js.map +1 -0
- package/dist/trust-root.d.ts +56 -0
- package/dist/trust-root.d.ts.map +1 -0
- package/dist/trust-root.js +28 -0
- package/dist/trust-root.js.map +1 -0
- package/dist/types.d.ts +201 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/validator-staking-client.d.ts +87 -0
- package/dist/validator-staking-client.d.ts.map +1 -0
- package/dist/validator-staking-client.js +226 -0
- package/dist/validator-staking-client.js.map +1 -0
- package/dist/virtuals-acp.d.ts +149 -0
- package/dist/virtuals-acp.d.ts.map +1 -0
- package/dist/virtuals-acp.js +418 -0
- package/dist/virtuals-acp.js.map +1 -0
- package/dist/wallet-manager.d.ts +199 -0
- package/dist/wallet-manager.d.ts.map +1 -0
- package/dist/wallet-manager.js +425 -0
- package/dist/wallet-manager.js.map +1 -0
- package/dist/wallets/burner.d.ts +45 -0
- package/dist/wallets/burner.d.ts.map +1 -0
- package/dist/wallets/burner.js +58 -0
- package/dist/wallets/burner.js.map +1 -0
- package/dist/wallets/index.d.ts +26 -0
- package/dist/wallets/index.d.ts.map +1 -0
- package/dist/wallets/index.js +20 -0
- package/dist/wallets/index.js.map +1 -0
- package/dist/wallets/injected.d.ts +22 -0
- package/dist/wallets/injected.d.ts.map +1 -0
- package/dist/wallets/injected.js +45 -0
- package/dist/wallets/injected.js.map +1 -0
- package/dist/wallets/ledger.d.ts +76 -0
- package/dist/wallets/ledger.d.ts.map +1 -0
- package/dist/wallets/ledger.js +106 -0
- package/dist/wallets/ledger.js.map +1 -0
- package/dist/wallets/near.d.ts +69 -0
- package/dist/wallets/near.d.ts.map +1 -0
- package/dist/wallets/near.js +101 -0
- package/dist/wallets/near.js.map +1 -0
- package/dist/wallets/types.d.ts +42 -0
- package/dist/wallets/types.d.ts.map +1 -0
- package/dist/wallets/types.js +31 -0
- package/dist/wallets/types.js.map +1 -0
- package/dist/wallets/walletconnect.d.ts +38 -0
- package/dist/wallets/walletconnect.d.ts.map +1 -0
- package/dist/wallets/walletconnect.js +94 -0
- package/dist/wallets/walletconnect.js.map +1 -0
- package/dist/weak-subjectivity.d.ts +213 -0
- package/dist/weak-subjectivity.d.ts.map +1 -0
- package/dist/weak-subjectivity.js +389 -0
- package/dist/weak-subjectivity.js.map +1 -0
- package/dist/x402-types/channel-types.d.ts +78 -0
- package/dist/x402-types/channel-types.d.ts.map +1 -0
- package/dist/x402-types/channel-types.js +5 -0
- package/dist/x402-types/channel-types.js.map +1 -0
- package/dist/x402-types/exact-flop.d.ts +29 -0
- package/dist/x402-types/exact-flop.d.ts.map +1 -0
- package/dist/x402-types/exact-flop.js +64 -0
- package/dist/x402-types/exact-flop.js.map +1 -0
- package/dist/x402-types/index.d.ts +65 -0
- package/dist/x402-types/index.d.ts.map +1 -0
- package/dist/x402-types/index.js +26 -0
- package/dist/x402-types/index.js.map +1 -0
- package/dist/x402.d.ts +111 -0
- package/dist/x402.d.ts.map +1 -0
- package/dist/x402.js +206 -0
- package/dist/x402.js.map +1 -0
- package/package.json +165 -3
package/dist/client.js
ADDED
|
@@ -0,0 +1,863 @@
|
|
|
1
|
+
import { ApiPromise, WsProvider, HttpProvider } from "@polkadot/api";
|
|
2
|
+
import { Keyring } from "@polkadot/keyring";
|
|
3
|
+
import { u8aToHex } from "@polkadot/util";
|
|
4
|
+
import { randomAsHex, sha256AsU8a } from "@polkadot/util-crypto";
|
|
5
|
+
import { HtlcTimeoutError, FlopError, CircuitBreakerTrippedError, } from "./errors.js";
|
|
6
|
+
import { trippedCircuitBreaker } from "./submit-tx.js";
|
|
7
|
+
import { fetchTrustRoot } from "./trust-root.js";
|
|
8
|
+
import { assertSupportedSpecVersion } from "./runtime-compatibility.js";
|
|
9
|
+
import { ComputeChannelChainClient } from "./compute-channel/chain.js";
|
|
10
|
+
import { ChannelSession } from "./compute-channel/session.js";
|
|
11
|
+
import { FlopChainSubmitter } from "./compute-channel/flop-submitter.js";
|
|
12
|
+
import { SessionKey, ready as cryptoReady } from "./compute-channel/crypto.js";
|
|
13
|
+
import { HttpSessionTransport, fetchMinerSessionInfo, notifyMinerOpen, } from "./compute-channel/transport-http.js";
|
|
14
|
+
import { toChainPolicy, satisfyWitnesses } from "./escrow.js";
|
|
15
|
+
import { checkSessionLimits, getSessionNonce } from "./agent-wallet.js";
|
|
16
|
+
import { signAndSend, transactionSignerAddress, } from "./transaction-signer.js";
|
|
17
|
+
function encodeHtlcTeeRequirements(requirements) {
|
|
18
|
+
const invalid = () => new FlopError("Supply a registered HTLC appraisal profile, version, model hash and measurement hash", "INVALID_TEE_REQUIREMENTS");
|
|
19
|
+
if (typeof requirements === "string") {
|
|
20
|
+
if (!/^0x[0-9a-f]{200}$/i.test(requirements)
|
|
21
|
+
|| /^0{64}$/i.test(requirements.slice(74, 138))
|
|
22
|
+
|| /^0{64}$/i.test(requirements.slice(138))) {
|
|
23
|
+
throw invalid();
|
|
24
|
+
}
|
|
25
|
+
return `0x${requirements.slice(2).toLowerCase()}`;
|
|
26
|
+
}
|
|
27
|
+
if (!requirements?.profile || !Number.isInteger(requirements.profile.version)
|
|
28
|
+
|| requirements.profile.version < 0 || requirements.profile.version > 0xffffffff) {
|
|
29
|
+
throw invalid();
|
|
30
|
+
}
|
|
31
|
+
const hashes = [requirements.profile.profile_id, requirements.model_hash, requirements.measurement_hash];
|
|
32
|
+
if (hashes.some(hash => !/^0x[0-9a-f]{64}$/i.test(hash))
|
|
33
|
+
|| hashes.slice(1).some(hash => /^0x0{64}$/i.test(hash))) {
|
|
34
|
+
throw invalid();
|
|
35
|
+
}
|
|
36
|
+
const encoded = new Uint8Array(100);
|
|
37
|
+
for (const [hash, offset] of [[hashes[0], 0], [hashes[1], 36], [hashes[2], 68]]) {
|
|
38
|
+
encoded.set(Uint8Array.from(hash.slice(2).match(/../g).map(byte => parseInt(byte, 16))), offset);
|
|
39
|
+
}
|
|
40
|
+
new DataView(encoded.buffer).setUint32(32, requirements.profile.version, true);
|
|
41
|
+
return u8aToHex(encoded);
|
|
42
|
+
}
|
|
43
|
+
const HAS_STATUS_RPC = {
|
|
44
|
+
hasStation: {
|
|
45
|
+
checkHTLCStatus: {
|
|
46
|
+
description: "Read HTLC status and its authenticated quote digest",
|
|
47
|
+
params: [{ name: "hash_lock", type: "Text" }],
|
|
48
|
+
type: "Json",
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Case-insensitive lookup helper for HAS Station pallet.
|
|
54
|
+
*/
|
|
55
|
+
function getHasPallet(api) {
|
|
56
|
+
const possibleNames = ["hasStation", "HasStation", "has_station", "Has_Station"];
|
|
57
|
+
for (const name of possibleNames) {
|
|
58
|
+
if (api.tx[name]) {
|
|
59
|
+
return {
|
|
60
|
+
tx: api.tx[name],
|
|
61
|
+
query: api.query[name],
|
|
62
|
+
name,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Case-insensitive lookup helper for PoUI pallet.
|
|
70
|
+
*/
|
|
71
|
+
function getPouiPallet(api) {
|
|
72
|
+
const possibleNames = ["flopPoui", "flopPoUI", "FlopPoUI", "FlopPoui", "flop_poui", "Flop_PoUI"];
|
|
73
|
+
for (const name of possibleNames) {
|
|
74
|
+
if (api.tx[name]) {
|
|
75
|
+
return {
|
|
76
|
+
tx: api.tx[name],
|
|
77
|
+
query: api.query[name],
|
|
78
|
+
name,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
function getSessionKeysPallet(api) {
|
|
85
|
+
for (const name of ["sessionKeys", "SessionKeys", "session_keys"]) {
|
|
86
|
+
if (api.tx[name])
|
|
87
|
+
return api.tx[name];
|
|
88
|
+
}
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* FLOP Network TypeScript SDK client.
|
|
93
|
+
*
|
|
94
|
+
* Mirrors the Python FLOPClient from `substrate_helper.py`.
|
|
95
|
+
* Manages a Substrate API connection, keypair, and provides
|
|
96
|
+
* typed methods for all HAS Station and PoUI operations.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* import { FlopClient } from "@flop-labs/sdk";
|
|
101
|
+
*
|
|
102
|
+
* const client = await FlopClient.create({ signer: "//Alice" });
|
|
103
|
+
* const { htlcId, hashLock } = await client.createHtlc({
|
|
104
|
+
* agent: "my-agent",
|
|
105
|
+
* task: { model: "llama-3-8b", prompt: "Hello" },
|
|
106
|
+
* rewardFlop: 5.0,
|
|
107
|
+
* });
|
|
108
|
+
* const status = await client.checkHtlcStatus(hashLock);
|
|
109
|
+
* await client.disconnect();
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export class FlopClient {
|
|
113
|
+
connectedApi;
|
|
114
|
+
runtimeError;
|
|
115
|
+
keyring;
|
|
116
|
+
sessionOwner;
|
|
117
|
+
transactionSigner;
|
|
118
|
+
localSigner;
|
|
119
|
+
constructor(api, keyring, signer, sessionOwner) {
|
|
120
|
+
assertSupportedSpecVersion(api.runtimeVersion?.specVersion?.toNumber());
|
|
121
|
+
this.connectedApi = api;
|
|
122
|
+
api.on("decorated", this.onRuntimeUpgrade);
|
|
123
|
+
this.keyring = keyring;
|
|
124
|
+
this.transactionSigner = signer;
|
|
125
|
+
if (signer && !("signer" in signer))
|
|
126
|
+
this.localSigner = signer;
|
|
127
|
+
this.sessionOwner = sessionOwner;
|
|
128
|
+
}
|
|
129
|
+
onRuntimeUpgrade = () => {
|
|
130
|
+
try {
|
|
131
|
+
assertSupportedSpecVersion(this.connectedApi.runtimeVersion?.specVersion?.toNumber());
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
this.runtimeError = error;
|
|
135
|
+
// Stop consumers that retained the API before the upgrade, too.
|
|
136
|
+
void this.disconnect().catch((cause) => {
|
|
137
|
+
this.runtimeError.cause = cause;
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
get api() {
|
|
142
|
+
if (this.runtimeError)
|
|
143
|
+
throw this.runtimeError;
|
|
144
|
+
assertSupportedSpecVersion(this.connectedApi.runtimeVersion?.specVersion?.toNumber());
|
|
145
|
+
return this.connectedApi;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Create and connect a new FlopClient.
|
|
149
|
+
*
|
|
150
|
+
* @param opts - Connection and signer options.
|
|
151
|
+
* @returns Connected client instance.
|
|
152
|
+
*/
|
|
153
|
+
static async create(opts = {}) {
|
|
154
|
+
const endpoint = opts.endpoint ?? "ws://127.0.0.1:9944";
|
|
155
|
+
const ss58 = opts.ss58Format ?? 42;
|
|
156
|
+
if ((opts.externalSigner === undefined) !== (opts.signerAddress === undefined)) {
|
|
157
|
+
throw new Error("externalSigner and signerAddress must be supplied together");
|
|
158
|
+
}
|
|
159
|
+
if (opts.externalSigner && opts.signer) {
|
|
160
|
+
throw new Error("signer URI and externalSigner are mutually exclusive");
|
|
161
|
+
}
|
|
162
|
+
const provider = endpoint.startsWith("ws")
|
|
163
|
+
? new WsProvider(endpoint)
|
|
164
|
+
: new HttpProvider(endpoint);
|
|
165
|
+
const api = await ApiPromise.create({ provider, noInitWarn: true, rpc: HAS_STATUS_RPC });
|
|
166
|
+
try {
|
|
167
|
+
const keyring = new Keyring({ type: "sr25519", ss58Format: ss58 });
|
|
168
|
+
const signer = opts.externalSigner
|
|
169
|
+
? { address: opts.signerAddress, signer: opts.externalSigner }
|
|
170
|
+
: keyring.addFromUri(opts.signer ?? "//Alice");
|
|
171
|
+
return new FlopClient(api, keyring, signer, opts.sessionOwner);
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
await api.disconnect();
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/** Build a client around an already-connected api + signer (no new ws connection). */
|
|
179
|
+
static fromApi(api, signer, opts = {}) {
|
|
180
|
+
return new FlopClient(api, new Keyring({ type: "sr25519" }), signer, opts.sessionOwner);
|
|
181
|
+
}
|
|
182
|
+
/** Build a query-only client without manufacturing or retaining signing material. */
|
|
183
|
+
static fromApiReadOnly(api) {
|
|
184
|
+
return new FlopClient(api, new Keyring({ type: "sr25519" }));
|
|
185
|
+
}
|
|
186
|
+
/** Build around an already-connected API and an external wallet signer. */
|
|
187
|
+
static fromApiWithSigner(api, address, signer, opts = {}) {
|
|
188
|
+
return new FlopClient(api, new Keyring({ type: "sr25519" }), { address, signer }, opts.sessionOwner);
|
|
189
|
+
}
|
|
190
|
+
/** Connect with an already-decoded keypair (e.g. an encrypted CLI keystore). */
|
|
191
|
+
static async createWithKeypair(endpoint, signer, opts = {}) {
|
|
192
|
+
const provider = endpoint.startsWith("ws")
|
|
193
|
+
? new WsProvider(endpoint)
|
|
194
|
+
: new HttpProvider(endpoint);
|
|
195
|
+
const api = await ApiPromise.create({ provider, noInitWarn: true, rpc: HAS_STATUS_RPC });
|
|
196
|
+
try {
|
|
197
|
+
return new FlopClient(api, new Keyring({ type: signer.type }), signer, opts.sessionOwner);
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
await api.disconnect();
|
|
201
|
+
throw error;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/** Connect with an extension, WalletConnect, Ledger, or other external signer. */
|
|
205
|
+
static async createWithSigner(endpoint, address, signer, opts = {}) {
|
|
206
|
+
const provider = endpoint.startsWith("ws")
|
|
207
|
+
? new WsProvider(endpoint)
|
|
208
|
+
: new HttpProvider(endpoint);
|
|
209
|
+
const api = await ApiPromise.create({ provider, noInitWarn: true, rpc: HAS_STATUS_RPC });
|
|
210
|
+
try {
|
|
211
|
+
return FlopClient.fromApiWithSigner(api, address, signer, opts);
|
|
212
|
+
}
|
|
213
|
+
catch (error) {
|
|
214
|
+
await api.disconnect();
|
|
215
|
+
throw error;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* In-process keypair for low-level helpers.
|
|
220
|
+
* External wallets should use FlopClient's high-level methods instead.
|
|
221
|
+
*/
|
|
222
|
+
get signer() {
|
|
223
|
+
void this.api;
|
|
224
|
+
if (!this.localSigner) {
|
|
225
|
+
throw new Error("external wallet has no in-process KeyringPair; use FlopClient methods");
|
|
226
|
+
}
|
|
227
|
+
return this.localSigner;
|
|
228
|
+
}
|
|
229
|
+
/** The SS58 address of the current signer. */
|
|
230
|
+
get address() {
|
|
231
|
+
return transactionSignerAddress(this.requireTransactionSigner());
|
|
232
|
+
}
|
|
233
|
+
requireTransactionSigner() {
|
|
234
|
+
void this.api;
|
|
235
|
+
if (!this.transactionSigner)
|
|
236
|
+
throw new Error("operation requires a signer");
|
|
237
|
+
return this.transactionSigner;
|
|
238
|
+
}
|
|
239
|
+
/** Whether the API is connected. */
|
|
240
|
+
get isConnected() {
|
|
241
|
+
return !this.runtimeError && this.connectedApi.isConnected;
|
|
242
|
+
}
|
|
243
|
+
/** Disconnect from the node. */
|
|
244
|
+
async disconnect() {
|
|
245
|
+
this.connectedApi.off("decorated", this.onRuntimeUpgrade);
|
|
246
|
+
await this.connectedApi.disconnect();
|
|
247
|
+
}
|
|
248
|
+
// ── Account ───────────────────────────────────────────────────────────
|
|
249
|
+
/** Query account balance and nonce. */
|
|
250
|
+
async getAccountInfo(address) {
|
|
251
|
+
const addr = address ?? this.address;
|
|
252
|
+
const info = await this.api.query.system.account(addr);
|
|
253
|
+
const data = info.data;
|
|
254
|
+
return {
|
|
255
|
+
address: addr,
|
|
256
|
+
nonce: info.nonce.toNumber(),
|
|
257
|
+
free: BigInt(data.free.toString()),
|
|
258
|
+
reserved: BigInt(data.reserved.toString()),
|
|
259
|
+
frozen: BigInt(data.frozen?.toString() ?? "0"),
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
/** Get free balance in FLOP (human-readable, 18 decimals). */
|
|
263
|
+
async getBalance(address) {
|
|
264
|
+
const info = await this.getAccountInfo(address);
|
|
265
|
+
return formatFlop(info.free);
|
|
266
|
+
}
|
|
267
|
+
/** Check node health. */
|
|
268
|
+
async health() {
|
|
269
|
+
const h = await this.api.rpc.system.health();
|
|
270
|
+
return {
|
|
271
|
+
isSyncing: h.isSyncing.isTrue,
|
|
272
|
+
peers: h.peers.toNumber(),
|
|
273
|
+
shouldHavePeers: h.shouldHavePeers.isTrue,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Canonical on-chain trust base (read-only).
|
|
278
|
+
*
|
|
279
|
+
* Fetches the indexer `/api/trust-root` view (runtime API when available).
|
|
280
|
+
* Pin `snapshotHash` to detect governance-gated trust-surface changes.
|
|
281
|
+
*
|
|
282
|
+
* @param indexerUrl - Indexer base URL. Default `http://localhost:4000`.
|
|
283
|
+
*/
|
|
284
|
+
async trustRoot(indexerUrl = "http://localhost:4000") {
|
|
285
|
+
return fetchTrustRoot(indexerUrl);
|
|
286
|
+
}
|
|
287
|
+
// ── HTLC (HAS Station) ───────────────────────────────────────────────
|
|
288
|
+
/**
|
|
289
|
+
* Generate a cryptographic pre-image / hash-lock pair.
|
|
290
|
+
*
|
|
291
|
+
* @returns `{ preImage, hashLock }` both as hex strings (no 0x prefix).
|
|
292
|
+
*/
|
|
293
|
+
static generateSecrets() {
|
|
294
|
+
const preImageBytes = randomAsHex(32);
|
|
295
|
+
const preImage = preImageBytes.slice(2); // strip 0x
|
|
296
|
+
// SHA256 of the raw bytes
|
|
297
|
+
const hashBytes = sha256AsU8a(preImageBytes);
|
|
298
|
+
const hashLock = u8aToHex(hashBytes).slice(2);
|
|
299
|
+
return { preImage, hashLock };
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Create an HTLC on-chain.
|
|
303
|
+
*
|
|
304
|
+
* @param params.agent - Agent identifier string.
|
|
305
|
+
* @param params.task - Compute task specification.
|
|
306
|
+
* @param params.rewardFlop - Reward amount in FLOP tokens (human units).
|
|
307
|
+
* @param params.timeoutBlocks - Timeout in blocks (1 block ≈ 1 second). Default: 60.
|
|
308
|
+
* @param params.hashLock - Optional pre-computed hash lock (hex). Auto-generated if omitted.
|
|
309
|
+
* @param params.work - Optional offchain-work commitments (metadata, not proof).
|
|
310
|
+
* @returns Created HTLC details including the pre-image (if auto-generated).
|
|
311
|
+
*/
|
|
312
|
+
async createHtlc(params) {
|
|
313
|
+
const task = {
|
|
314
|
+
...params.task,
|
|
315
|
+
tee_requirements: encodeHtlcTeeRequirements(params.task.tee_requirements),
|
|
316
|
+
};
|
|
317
|
+
let preImage;
|
|
318
|
+
let hashLock;
|
|
319
|
+
if (params.hashLock) {
|
|
320
|
+
hashLock = params.hashLock.replace(/^0x/, "");
|
|
321
|
+
preImage = ""; // Caller manages their own pre-image
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
const secrets = FlopClient.generateSecrets();
|
|
325
|
+
preImage = secrets.preImage;
|
|
326
|
+
hashLock = secrets.hashLock;
|
|
327
|
+
}
|
|
328
|
+
const rewardRaw = flopToRaw(params.rewardFlop);
|
|
329
|
+
const timeout = params.timeoutBlocks ?? 60;
|
|
330
|
+
const hasStation = getHasPallet(this.api);
|
|
331
|
+
if (!hasStation) {
|
|
332
|
+
throw new Error("HAS Station pallet not available");
|
|
333
|
+
}
|
|
334
|
+
const tx = params.work
|
|
335
|
+
? hasStation.tx.createHtlcForWork(params.agent, task, `0x${hashLock}`, rewardRaw, timeout, params.work.taskHash.startsWith("0x") ? params.work.taskHash : `0x${params.work.taskHash}`, params.work.outputCommitment.startsWith("0x")
|
|
336
|
+
? params.work.outputCommitment
|
|
337
|
+
: `0x${params.work.outputCommitment}`)
|
|
338
|
+
: hasStation.tx.createHtlc(params.agent, task, `0x${hashLock}`, rewardRaw, timeout);
|
|
339
|
+
let submittedTx = tx;
|
|
340
|
+
if (this.sessionOwner) {
|
|
341
|
+
const limits = await checkSessionLimits(this.api, this.sessionOwner, this.address);
|
|
342
|
+
if (!limits.valid)
|
|
343
|
+
throw new Error(`session precheck failed: ${limits.reason ?? "invalid session"}`);
|
|
344
|
+
if (rewardRaw > limits.perTxLimit || rewardRaw > limits.dailyRemaining || rewardRaw > limits.lifetimeRemaining) {
|
|
345
|
+
throw new Error("session precheck failed: inference reward exceeds remaining session limits");
|
|
346
|
+
}
|
|
347
|
+
const sessionKeys = getSessionKeysPallet(this.api);
|
|
348
|
+
if (!sessionKeys?.executeViaSession) {
|
|
349
|
+
throw new Error("sessionKeys.executeViaSession unavailable on this runtime");
|
|
350
|
+
}
|
|
351
|
+
const nonce = await getSessionNonce(this.api, this.sessionOwner, this.address);
|
|
352
|
+
submittedTx = sessionKeys.executeViaSession(this.sessionOwner, tx, rewardRaw, nonce);
|
|
353
|
+
}
|
|
354
|
+
let htlcId;
|
|
355
|
+
const txHash = await submitAndWait(submittedTx, this.requireTransactionSigner(), events => {
|
|
356
|
+
htlcId = htlcIdFromEvents(events, hashLock);
|
|
357
|
+
});
|
|
358
|
+
return {
|
|
359
|
+
htlcId,
|
|
360
|
+
hashLock,
|
|
361
|
+
preImage,
|
|
362
|
+
txHash,
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Create a cross-chain HTLC for a FLOP ↔ BTC atomic swap.
|
|
367
|
+
*
|
|
368
|
+
* Locks `rewardFlop` on FLOP behind `H = SHA256(s)` and records the counter-chain
|
|
369
|
+
* (BTC) leg. Timelock symmetry is enforced on-chain: the FLOP leg must outlive the
|
|
370
|
+
* counter-chain leg, so `timeoutBlocks` (FLOP, 1s blocks) is the *longer* leg and
|
|
371
|
+
* `btcTimelockHours` the *shorter* one. See yellowpaper §10 / docs/bridge.
|
|
372
|
+
*
|
|
373
|
+
* This is the bilateral HTLC path (BTC, NEAR-native, NEP-141 USDT on NEAR). USDT on
|
|
374
|
+
* TON/TRON settles via NEAR Intents (solver-executed, no preimage) — see
|
|
375
|
+
* docs/bridge/settlement.md.
|
|
376
|
+
*
|
|
377
|
+
* @param params.btcAddress - Counter-chain recipient address.
|
|
378
|
+
* @param params.btcAmountSats - Counter-asset amount in satoshis.
|
|
379
|
+
* @param params.timeoutBlocks - FLOP timeout; derived from live chain margins when omitted.
|
|
380
|
+
* @param params.btcTimelockHours - Counter-chain timelock in hours. Default: 1.
|
|
381
|
+
* @param params.nearTimeoutSeconds - Select the NEAR leg and its actual timeout in seconds.
|
|
382
|
+
* @param params.hashLock - Optional pre-computed hash lock (hex). Auto-generated if omitted.
|
|
383
|
+
* @returns Created HTLC details including the pre-image (if auto-generated).
|
|
384
|
+
*/
|
|
385
|
+
async createCrossChainHtlc(params) {
|
|
386
|
+
let preImage;
|
|
387
|
+
let hashLock;
|
|
388
|
+
if (params.hashLock) {
|
|
389
|
+
hashLock = params.hashLock.replace(/^0x/, "");
|
|
390
|
+
preImage = ""; // Caller manages their own pre-image
|
|
391
|
+
}
|
|
392
|
+
else {
|
|
393
|
+
const secrets = FlopClient.generateSecrets();
|
|
394
|
+
preImage = secrets.preImage;
|
|
395
|
+
hashLock = secrets.hashLock;
|
|
396
|
+
}
|
|
397
|
+
const rewardRaw = flopToRaw(params.rewardFlop);
|
|
398
|
+
const btcTimelockHours = params.btcTimelockHours ?? 1; // counter-chain 1h (shorter leg)
|
|
399
|
+
const hasStation = getHasPallet(this.api);
|
|
400
|
+
if (!hasStation) {
|
|
401
|
+
throw new Error("HAS Station pallet not available");
|
|
402
|
+
}
|
|
403
|
+
const { timeout, minimum } = await this.crossChainTimeout(params.nearTimeoutSeconds, btcTimelockHours, params.timeoutBlocks, hasStation.name);
|
|
404
|
+
const create = params.nearTimeoutSeconds === undefined
|
|
405
|
+
? hasStation.tx.createCrossChainHtlc
|
|
406
|
+
: hasStation.tx.createNearHtlc;
|
|
407
|
+
if (!create)
|
|
408
|
+
throw new Error("NEAR HTLC creation is not supported by this runtime");
|
|
409
|
+
const tx = create(params.agent, params.task, `0x${hashLock}`, rewardRaw, timeout, params.btcAddress, BigInt(params.btcAmountSats), params.nearTimeoutSeconds ?? btcTimelockHours);
|
|
410
|
+
let htlcId;
|
|
411
|
+
const txHash = await submitAndWait(tx, this.requireTransactionSigner(), events => {
|
|
412
|
+
htlcId = htlcIdFromEvents(events, hashLock);
|
|
413
|
+
});
|
|
414
|
+
return {
|
|
415
|
+
htlcId,
|
|
416
|
+
hashLock,
|
|
417
|
+
preImage,
|
|
418
|
+
txHash,
|
|
419
|
+
minimumTimeoutBlocks: minimum,
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
async crossChainTimeout(nearSeconds, btcHours, requested, palletName) {
|
|
423
|
+
try {
|
|
424
|
+
const head = await this.api.rpc.chain.getHeader();
|
|
425
|
+
const snapshot = await this.api.at(head.hash);
|
|
426
|
+
const constants = snapshot.consts[palletName];
|
|
427
|
+
const finality = await snapshot.query.aleph.lastFinalized();
|
|
428
|
+
const best = head.number.toNumber();
|
|
429
|
+
const finalized = finality.isSome ? finality.unwrap()[0].toNumber() : best;
|
|
430
|
+
const lag = Math.max(0, best - finalized);
|
|
431
|
+
const freeze = constants.finalityFreezeThreshold.toNumber();
|
|
432
|
+
if (lag >= freeze)
|
|
433
|
+
throw new Error(`finality lag ${lag} freezes new HTLCs (threshold ${freeze})`);
|
|
434
|
+
const leg = nearSeconds ?? btcHours;
|
|
435
|
+
if (!Number.isSafeInteger(leg) || leg <= 0)
|
|
436
|
+
throw new Error("counter-chain timeout must be a positive integer");
|
|
437
|
+
const duration = nearSeconds === undefined
|
|
438
|
+
? btcHours * 6 * constants.btcBlockTimeInFlopBlocks.toNumber()
|
|
439
|
+
: nearSeconds; // The runtime uses one-second FLOP blocks for the NEAR clock.
|
|
440
|
+
const minimum = duration + Math.max(Math.ceil(duration * constants.timelockSymmetrySafetyMargin.toNumber() / 100), constants.maxFinalityStall.toNumber() + lag);
|
|
441
|
+
// Leave one admission window for lag to grow between RPC observation and inclusion.
|
|
442
|
+
const timeout = requested ?? minimum + freeze;
|
|
443
|
+
if (!Number.isSafeInteger(timeout) || timeout > 0xffffffff || timeout < minimum) {
|
|
444
|
+
throw new Error(`FLOP timeout must be at least ${minimum} blocks (counter-chain duration ${duration}, finality lag ${lag})`);
|
|
445
|
+
}
|
|
446
|
+
return { timeout, minimum };
|
|
447
|
+
}
|
|
448
|
+
catch (error) {
|
|
449
|
+
throw new Error(`Cannot create cross-chain HTLC: ${error instanceof Error ? error.message : String(error)}`);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Open a conditional escrow (CRIT-01 / PTLC). Reserves `amountFlop` from the signer
|
|
454
|
+
* and pays `payee` once `policy` is satisfied before `deadlineBlocks`, otherwise the
|
|
455
|
+
* funder may refund. `policy` may include a PTLC `Point` leaf (see escrow.ts/amhl.ts);
|
|
456
|
+
* it is validated + re-encoded to the on-chain SCALE shape via `toChainPolicy`.
|
|
457
|
+
*/
|
|
458
|
+
async openEscrow(params) {
|
|
459
|
+
const hasStation = getHasPallet(this.api);
|
|
460
|
+
if (!hasStation) {
|
|
461
|
+
throw new Error("HAS Station pallet not available");
|
|
462
|
+
}
|
|
463
|
+
const tx = hasStation.tx.openEscrow(params.payee, flopToRaw(params.amountFlop), toChainPolicy(params.policy), params.deadlineBlocks);
|
|
464
|
+
return { txHash: await submitAndWait(tx, this.requireTransactionSigner()) };
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Release an open escrow by submitting witnesses — `Hash` preimages and/or PTLC
|
|
468
|
+
* scalar witnesses `y`. Permissionless on-chain, so a relayer can submit on behalf of
|
|
469
|
+
* a zero-native-balance payee.
|
|
470
|
+
*/
|
|
471
|
+
async satisfyEscrow(escrowId, secrets) {
|
|
472
|
+
const hasStation = getHasPallet(this.api);
|
|
473
|
+
if (!hasStation) {
|
|
474
|
+
throw new Error("HAS Station pallet not available");
|
|
475
|
+
}
|
|
476
|
+
// Encode each witness as a 0x-hex string, NOT a raw Uint8Array: the extrinsic arg is
|
|
477
|
+
// `Vec<Bytes>`, and polkadot-js reads a Uint8Array as *SCALE-pre-encoded* bytes (its
|
|
478
|
+
// leading byte is taken as a compact length), which silently truncates the witness to
|
|
479
|
+
// empty and the runtime rejects with `PolicyNotSatisfied`. A hex string is encoded as
|
|
480
|
+
// the value. (Caught by the on-chain e2e; the mocked path never SCALE-encodes.)
|
|
481
|
+
const witnesses = satisfyWitnesses(secrets).map((w) => u8aToHex(w));
|
|
482
|
+
const tx = hasStation.tx.satisfyEscrow(escrowId, witnesses);
|
|
483
|
+
return { txHash: await submitAndWait(tx, this.requireTransactionSigner()) };
|
|
484
|
+
}
|
|
485
|
+
/** Approve an escrow, satisfying a `Sig(caller)` leaf in its policy (idempotent). */
|
|
486
|
+
async approveEscrow(escrowId) {
|
|
487
|
+
const hasStation = getHasPallet(this.api);
|
|
488
|
+
if (!hasStation) {
|
|
489
|
+
throw new Error("HAS Station pallet not available");
|
|
490
|
+
}
|
|
491
|
+
return {
|
|
492
|
+
txHash: await submitAndWait(hasStation.tx.approveEscrow(escrowId), this.requireTransactionSigner()),
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
/** Refund an open escrow to its funder once its deadline has passed (funder-only). */
|
|
496
|
+
async refundEscrow(escrowId) {
|
|
497
|
+
const hasStation = getHasPallet(this.api);
|
|
498
|
+
if (!hasStation) {
|
|
499
|
+
throw new Error("HAS Station pallet not available");
|
|
500
|
+
}
|
|
501
|
+
return {
|
|
502
|
+
txHash: await submitAndWait(hasStation.tx.refundEscrow(escrowId), this.requireTransactionSigner()),
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Check HTLC status via custom RPC.
|
|
507
|
+
*
|
|
508
|
+
* @param hashLock - Hash lock hex string.
|
|
509
|
+
* @returns HTLC info or null if not found.
|
|
510
|
+
*/
|
|
511
|
+
async checkHtlcStatus(hashLock) {
|
|
512
|
+
void this.api;
|
|
513
|
+
const hl = hashLock.startsWith("0x") ? hashLock : `0x${hashLock}`;
|
|
514
|
+
try {
|
|
515
|
+
const result = await this.api.rpc.hasStation.checkHTLCStatus(hl);
|
|
516
|
+
if (!result || result.isNone)
|
|
517
|
+
return null;
|
|
518
|
+
const val = result.toJSON?.() ?? result;
|
|
519
|
+
return val;
|
|
520
|
+
}
|
|
521
|
+
catch {
|
|
522
|
+
// Fallback: raw RPC request
|
|
523
|
+
try {
|
|
524
|
+
const raw = await this.api.rpc.state.call("HasStationApi_check_htlc_status", hl);
|
|
525
|
+
return raw ? (raw.toJSON?.() ?? null) : null;
|
|
526
|
+
}
|
|
527
|
+
catch {
|
|
528
|
+
return null;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Get HTLC details by hash lock via custom RPC.
|
|
534
|
+
*/
|
|
535
|
+
async getHtlcByHashLock(hashLock) {
|
|
536
|
+
void this.api;
|
|
537
|
+
const hl = hashLock.startsWith("0x") ? hashLock : `0x${hashLock}`;
|
|
538
|
+
try {
|
|
539
|
+
const result = await this.api.rpc.hasStation.getHtlcByHashLock(hl);
|
|
540
|
+
if (!result || result.isNone)
|
|
541
|
+
return null;
|
|
542
|
+
return result.toJSON?.() ?? result;
|
|
543
|
+
}
|
|
544
|
+
catch {
|
|
545
|
+
return null;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Redeem an HTLC by revealing the pre-image.
|
|
550
|
+
*
|
|
551
|
+
* @param htlcId - HTLC identifier (hex).
|
|
552
|
+
* @param preImage - Pre-image that hashes to the hash-lock (hex).
|
|
553
|
+
* @param teeQuote - TEE attestation quote.
|
|
554
|
+
* @param receipt - Optional receipt metadata. A verifier ref does not mean acceptance.
|
|
555
|
+
* @returns Transaction hash.
|
|
556
|
+
*/
|
|
557
|
+
async redeemHtlc(htlcId, preImage, teeQuote, receipt) {
|
|
558
|
+
const id = htlcId.startsWith("0x") ? htlcId : `0x${htlcId}`;
|
|
559
|
+
const pi = preImage.startsWith("0x") ? preImage : `0x${preImage}`;
|
|
560
|
+
const hasStation = getHasPallet(this.api);
|
|
561
|
+
if (!hasStation) {
|
|
562
|
+
throw new Error("HAS Station pallet not available");
|
|
563
|
+
}
|
|
564
|
+
const quote = {
|
|
565
|
+
version: teeQuote.version,
|
|
566
|
+
gn_weight: teeQuote.gn_weight,
|
|
567
|
+
attestation: teeQuote.attestation.startsWith("0x")
|
|
568
|
+
? teeQuote.attestation
|
|
569
|
+
: `0x${teeQuote.attestation}`,
|
|
570
|
+
};
|
|
571
|
+
const tx = receipt
|
|
572
|
+
? hasStation.tx.redeemHtlcWithReceipt(id, pi, quote, receipt.receiptHash.startsWith("0x") ? receipt.receiptHash : `0x${receipt.receiptHash}`, receipt.verifierRef
|
|
573
|
+
? (receipt.verifierRef.startsWith("0x") ? receipt.verifierRef : `0x${receipt.verifierRef}`)
|
|
574
|
+
: null)
|
|
575
|
+
: hasStation.tx.redeemHtlc(id, pi, quote);
|
|
576
|
+
return submitAndWait(tx, this.requireTransactionSigner());
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Refund an HTLC after timeout.
|
|
580
|
+
*
|
|
581
|
+
* @param htlcId - HTLC identifier (hex).
|
|
582
|
+
* @returns Transaction hash.
|
|
583
|
+
*/
|
|
584
|
+
async refundHtlc(htlcId) {
|
|
585
|
+
const id = htlcId.startsWith("0x") ? htlcId : `0x${htlcId}`;
|
|
586
|
+
const hasStation = getHasPallet(this.api);
|
|
587
|
+
if (!hasStation) {
|
|
588
|
+
throw new Error("HAS Station pallet not available");
|
|
589
|
+
}
|
|
590
|
+
const tx = hasStation.tx.refundHtlc(id);
|
|
591
|
+
return submitAndWait(tx, this.requireTransactionSigner());
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Poll HTLC status until settled or timeout.
|
|
595
|
+
*
|
|
596
|
+
* @param hashLock - Hash lock to watch.
|
|
597
|
+
* @param timeoutMs - Max wait time in milliseconds. Default: 60000.
|
|
598
|
+
* @param pollIntervalMs - Polling interval. Default: 2000.
|
|
599
|
+
* @returns Final HTLC info or null on timeout.
|
|
600
|
+
*/
|
|
601
|
+
async waitForSettlement(hashLock, timeoutMs = 60_000, pollIntervalMs = 2_000) {
|
|
602
|
+
const deadline = Date.now() + timeoutMs;
|
|
603
|
+
while (Date.now() < deadline) {
|
|
604
|
+
const info = await this.checkHtlcStatus(hashLock);
|
|
605
|
+
if (info && (info.status === "SETTLED" || info.status === "REFUNDED")) {
|
|
606
|
+
return info;
|
|
607
|
+
}
|
|
608
|
+
await sleep(pollIntervalMs);
|
|
609
|
+
}
|
|
610
|
+
return null;
|
|
611
|
+
}
|
|
612
|
+
// ── Verified Inference ──────────────────────────────────────────────
|
|
613
|
+
/**
|
|
614
|
+
* Buy verified inference in a single call.
|
|
615
|
+
*
|
|
616
|
+
* Creates an HTLC for a compute task, waits for a miner to settle it
|
|
617
|
+
* with a TEE attestation, and returns the on-chain inference receipt.
|
|
618
|
+
*
|
|
619
|
+
* @example
|
|
620
|
+
* ```ts
|
|
621
|
+
* const receipt = await client.buyVerifiedInference({
|
|
622
|
+
* model: "llama-3-70b",
|
|
623
|
+
* prompt: "Analyze this portfolio...",
|
|
624
|
+
* maxFlop: 10,
|
|
625
|
+
* });
|
|
626
|
+
* console.log(`Verified! G_n: ${receipt.gnUsed}, block: ${receipt.blockNumber}`);
|
|
627
|
+
* ```
|
|
628
|
+
*/
|
|
629
|
+
async buyVerifiedInference(params) {
|
|
630
|
+
const { htlcId, hashLock, txHash: createTxHash } = await this.createHtlc({
|
|
631
|
+
agent: this.address,
|
|
632
|
+
task: {
|
|
633
|
+
model: params.model,
|
|
634
|
+
prompt: params.prompt,
|
|
635
|
+
tee_requirements: params.teeRequirements,
|
|
636
|
+
},
|
|
637
|
+
rewardFlop: params.maxFlop,
|
|
638
|
+
timeoutBlocks: params.timeoutBlocks ?? 120,
|
|
639
|
+
});
|
|
640
|
+
const settled = await this.waitForSettlement(hashLock, params.waitTimeoutMs ?? 120_000, params.pollIntervalMs ?? 2_000);
|
|
641
|
+
if (!settled || settled.status !== "SETTLED") {
|
|
642
|
+
throw new HtlcTimeoutError(htlcId, params.waitTimeoutMs ?? 120_000);
|
|
643
|
+
}
|
|
644
|
+
return this.extractReceipt(hashLock, settled, htlcId);
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* Get the inference receipt for a settled HTLC.
|
|
648
|
+
*
|
|
649
|
+
* @param hashLock - Hash lock of the settled HTLC.
|
|
650
|
+
* @returns Inference receipt or null if not settled.
|
|
651
|
+
*/
|
|
652
|
+
async getInferenceReceipt(hashLock) {
|
|
653
|
+
const info = await this.checkHtlcStatus(hashLock);
|
|
654
|
+
if (!info || info.status !== "SETTLED")
|
|
655
|
+
return null;
|
|
656
|
+
return this.extractReceipt(hashLock, info);
|
|
657
|
+
}
|
|
658
|
+
// ── Session Inference (Mode A: BYO-FLOP / session / direct-to-miner) ───
|
|
659
|
+
/**
|
|
660
|
+
* Open a verified compute-channel session against a miner (Mode A).
|
|
661
|
+
*
|
|
662
|
+
* The caller's own funded wallet escrows FLOP on-chain (`open_channel`),
|
|
663
|
+
* then streams inference turns **directly to the miner** off-chain via the
|
|
664
|
+
* supplied {@link SessionTransport}; each turn's receipt is verified before
|
|
665
|
+
* its output is accepted. Close + finalize settle the session on-chain from
|
|
666
|
+
* the escrow — no HTLC, no gateway, no x402.
|
|
667
|
+
*
|
|
668
|
+
* The transport (RA-TLS to a live miner) and the miner's channel host are
|
|
669
|
+
* supplied by the caller; this wires the on-chain bridge to your own
|
|
670
|
+
* `@polkadot/api` connection. Call `ready()` once before signing turns.
|
|
671
|
+
*
|
|
672
|
+
* @example
|
|
673
|
+
* ```ts
|
|
674
|
+
* const session = await client.openInferenceSession({
|
|
675
|
+
* params: { miner, modelHash, measuredRoot, precision, enclaveKey,
|
|
676
|
+
* agentKey: agentKey.publicKey, maxTurns, maxDuration,
|
|
677
|
+
* reattestEvery, escrow, nonce },
|
|
678
|
+
* transport, // RA-TLS client to the miner
|
|
679
|
+
* enclaveKey, agentKey, measuredRoot,
|
|
680
|
+
* });
|
|
681
|
+
* const out = await session.turn(promptBytes);
|
|
682
|
+
* await session.close();
|
|
683
|
+
* await session.finalize();
|
|
684
|
+
* ```
|
|
685
|
+
*/
|
|
686
|
+
async openInferenceSession(opts) {
|
|
687
|
+
const chain = opts.chain ?? new ComputeChannelChainClient(new FlopChainSubmitter(this.api, this.requireTransactionSigner()));
|
|
688
|
+
return ChannelSession.open({
|
|
689
|
+
chain,
|
|
690
|
+
transport: opts.transport,
|
|
691
|
+
enclaveKey: opts.enclaveKey,
|
|
692
|
+
agentKey: opts.agentKey,
|
|
693
|
+
measuredRoot: opts.measuredRoot,
|
|
694
|
+
modelIdentity: opts.modelIdentity,
|
|
695
|
+
}, opts.params);
|
|
696
|
+
}
|
|
697
|
+
/**
|
|
698
|
+
* Turnkey Mode A: open a session to a live miner, run one verified turn, and
|
|
699
|
+
* post the on-chain settlement from escrow — all from the caller's own funded
|
|
700
|
+
* wallet.
|
|
701
|
+
*
|
|
702
|
+
* Handshake → `open_channel` → stream the prompt directly to the miner →
|
|
703
|
+
* verify the receipt → `force_settle` (posts the settlement + opens the
|
|
704
|
+
* challenge window). Pay-out to the miner is **not** synchronous: `force_settle`
|
|
705
|
+
* only posts the claim, and `finalize` rejects with `WindowNotElapsed` until
|
|
706
|
+
* the dispute window closes — so this returns once the settlement is posted
|
|
707
|
+
* (`challengeEnd`). The miner is paid either when anyone calls
|
|
708
|
+
* `session.finalize()` after the window, or immediately when the miner submits
|
|
709
|
+
* the cooperative `settle` (it carries the agent receipt this turn produced).
|
|
710
|
+
* The miner URL points at a session-serving miner (`miner-worker --sessions`).
|
|
711
|
+
*/
|
|
712
|
+
async buyInferenceSession(opts) {
|
|
713
|
+
await cryptoReady();
|
|
714
|
+
const info = await fetchMinerSessionInfo(opts.minerUrl);
|
|
715
|
+
if (!info.isReady)
|
|
716
|
+
throw new Error("miner model is not provider-ready");
|
|
717
|
+
const agentKey = SessionKey.generate();
|
|
718
|
+
const transport = new HttpSessionTransport(opts.minerUrl);
|
|
719
|
+
const session = await this.openInferenceSession({
|
|
720
|
+
params: {
|
|
721
|
+
miner: info.minerAddress,
|
|
722
|
+
modelHash: info.modelHash,
|
|
723
|
+
measuredRoot: info.measuredRoot,
|
|
724
|
+
decodePolicyHash: info.decodePolicyHash,
|
|
725
|
+
precision: info.precision,
|
|
726
|
+
enclaveKey: info.enclaveKey,
|
|
727
|
+
agentKey: agentKey.publicKey,
|
|
728
|
+
maxTurns: opts.maxTurns ?? 16,
|
|
729
|
+
maxDuration: opts.maxDurationBlocks ?? 600n,
|
|
730
|
+
reattestEvery: 0,
|
|
731
|
+
escrow: flopToRaw(opts.escrowFlop ?? 5),
|
|
732
|
+
nonce: BigInt(Date.now()),
|
|
733
|
+
},
|
|
734
|
+
transport,
|
|
735
|
+
enclaveKey: info.enclaveKey,
|
|
736
|
+
agentKey,
|
|
737
|
+
measuredRoot: info.measuredRoot,
|
|
738
|
+
modelIdentity: info.modelIdentity,
|
|
739
|
+
chain: opts.chain,
|
|
740
|
+
});
|
|
741
|
+
// channelId is only known post-open; bind the transport + start the host.
|
|
742
|
+
transport.bind(session.channelId);
|
|
743
|
+
await notifyMinerOpen(opts.minerUrl, session.channelId, agentKey.publicKey);
|
|
744
|
+
const out = await session.turn(new TextEncoder().encode(opts.prompt));
|
|
745
|
+
// force_settle posts the verified claim + opens the challenge window; do NOT
|
|
746
|
+
// finalize here (it would revert with WindowNotElapsed before the window).
|
|
747
|
+
const posted = await session.close();
|
|
748
|
+
return {
|
|
749
|
+
status: "settlement_posted",
|
|
750
|
+
output: new TextDecoder().decode(out),
|
|
751
|
+
channelId: u8aToHex(session.channelId),
|
|
752
|
+
gnUsed: session.aggregateGn(),
|
|
753
|
+
turnsVerified: session.turnsVerified(),
|
|
754
|
+
challengeEnd: posted.challengeEnd,
|
|
755
|
+
};
|
|
756
|
+
}
|
|
757
|
+
/** Extract an InferenceReceipt from settled HTLC info. */
|
|
758
|
+
extractReceipt(hashLock, info, expectedHtlcId) {
|
|
759
|
+
if (!info.quote_digest || !/^0x[0-9a-f]{64}$/i.test(info.quote_digest)
|
|
760
|
+
|| /^0x0{64}$/i.test(info.quote_digest) || info.gn_used == null || info.gn_used <= 0) {
|
|
761
|
+
throw new FlopError("HTLC has no authenticated TEE redemption evidence", "HTLC_ATTESTATION_REQUIRED");
|
|
762
|
+
}
|
|
763
|
+
if (typeof info.htlc_id !== "string" || !/^0x[0-9a-f]{64}$/i.test(info.htlc_id)
|
|
764
|
+
|| typeof info.hash_lock !== "string" || !/^(?:0x)?[0-9a-f]{64}$/i.test(info.hash_lock)
|
|
765
|
+
|| info.hash_lock.replace(/^0x/, "").toLowerCase() !== hashLock.replace(/^0x/, "").toLowerCase()
|
|
766
|
+
|| (expectedHtlcId !== undefined && info.htlc_id.toLowerCase() !== expectedHtlcId.toLowerCase())) {
|
|
767
|
+
throw new FlopError("HTLC status has missing or mismatched identity", "HTLC_IDENTITY_REQUIRED");
|
|
768
|
+
}
|
|
769
|
+
const promptHash = sha256AsU8a(new TextEncoder().encode("prompt")).slice(0, 32);
|
|
770
|
+
return {
|
|
771
|
+
model: info.task?.model ?? "unknown",
|
|
772
|
+
promptHash: u8aToHex(promptHash).slice(2),
|
|
773
|
+
outputHash: info.pre_image ?? "",
|
|
774
|
+
gnUsed: info.gn_used ?? 0,
|
|
775
|
+
teeQuoteHash: info.quote_digest,
|
|
776
|
+
blockNumber: info.settled_block ?? 0,
|
|
777
|
+
txHash: info.htlc_id,
|
|
778
|
+
hashLock: hashLock.replace(/^0x/, ""),
|
|
779
|
+
};
|
|
780
|
+
}
|
|
781
|
+
// ── PoUI ──────────────────────────────────────────────────────────────
|
|
782
|
+
/**
|
|
783
|
+
* Submit a PoUI proof batch.
|
|
784
|
+
*
|
|
785
|
+
* @param proofs - Array of proof objects.
|
|
786
|
+
* @param gnWeight - Claimed G_n weight.
|
|
787
|
+
* @param taskHash - Unique task hash for replay protection (hex).
|
|
788
|
+
* @returns Transaction hash.
|
|
789
|
+
*/
|
|
790
|
+
async submitPoUI(proofs, gnWeight, taskHash) {
|
|
791
|
+
const th = taskHash.startsWith("0x") ? taskHash : `0x${taskHash}`;
|
|
792
|
+
const formattedProofs = proofs.map((p) => ({
|
|
793
|
+
vk_hash: p.vkHash.startsWith("0x") ? p.vkHash : `0x${p.vkHash}`,
|
|
794
|
+
proof_data: p.proofData.startsWith("0x") ? p.proofData : `0x${p.proofData}`,
|
|
795
|
+
public_inputs: p.publicInputs.startsWith("0x")
|
|
796
|
+
? p.publicInputs
|
|
797
|
+
: `0x${p.publicInputs}`,
|
|
798
|
+
}));
|
|
799
|
+
const poui = getPouiPallet(this.api);
|
|
800
|
+
if (!poui) {
|
|
801
|
+
throw new Error("PoUI pallet not available");
|
|
802
|
+
}
|
|
803
|
+
const tx = poui.tx.submitStarkBatch(formattedProofs, gnWeight, th);
|
|
804
|
+
return submitAndWait(tx, this.requireTransactionSigner());
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
// ── Helpers ─────────────────────────────────────────────────────────────
|
|
808
|
+
const FLOP_DECIMALS = 18;
|
|
809
|
+
const FLOP_UNIT = 10n ** BigInt(FLOP_DECIMALS);
|
|
810
|
+
/** Convert human-readable FLOP to raw integer. */
|
|
811
|
+
function flopToRaw(amount) {
|
|
812
|
+
// Use string multiplication to avoid floating-point issues
|
|
813
|
+
const [whole, frac = ""] = amount.toString().split(".");
|
|
814
|
+
const padded = frac.padEnd(FLOP_DECIMALS, "0").slice(0, FLOP_DECIMALS);
|
|
815
|
+
return BigInt(whole) * FLOP_UNIT + BigInt(padded);
|
|
816
|
+
}
|
|
817
|
+
/** Convert raw integer to human-readable FLOP string. */
|
|
818
|
+
function formatFlop(raw) {
|
|
819
|
+
const whole = raw / FLOP_UNIT;
|
|
820
|
+
const frac = raw % FLOP_UNIT;
|
|
821
|
+
if (frac === 0n)
|
|
822
|
+
return whole.toString();
|
|
823
|
+
const fracStr = frac.toString().padStart(FLOP_DECIMALS, "0").replace(/0+$/, "");
|
|
824
|
+
return `${whole}.${fracStr}`;
|
|
825
|
+
}
|
|
826
|
+
function htlcIdFromEvents(events, hashLock) {
|
|
827
|
+
const created = events.find(({ event }) => ["hasstation", "has_station"].includes(event.section.toLowerCase())
|
|
828
|
+
&& event.method === "HtlcCreated"
|
|
829
|
+
&& event.data[1].toHex().toLowerCase() === `0x${hashLock.toLowerCase()}`);
|
|
830
|
+
const htlcId = created?.event.data[0].toHex();
|
|
831
|
+
if (typeof htlcId !== "string" || !/^0x[0-9a-f]{64}$/i.test(htlcId)) {
|
|
832
|
+
throw new FlopError("HTLC inclusion has no matching creation identity", "HTLC_IDENTITY_REQUIRED");
|
|
833
|
+
}
|
|
834
|
+
return htlcId;
|
|
835
|
+
}
|
|
836
|
+
/** Sign, submit, and wait for extrinsic inclusion. A circuit-breaker trip rejects. */
|
|
837
|
+
async function submitAndWait(tx, signer, onIncluded) {
|
|
838
|
+
return new Promise((resolve, reject) => {
|
|
839
|
+
signAndSend(tx, signer, ({ status, txHash, dispatchError, events = [] }) => {
|
|
840
|
+
if (dispatchError) {
|
|
841
|
+
reject(new Error(`Transaction failed: ${dispatchError.toString()}`));
|
|
842
|
+
}
|
|
843
|
+
else if (status.isInBlock || status.isFinalized) {
|
|
844
|
+
if (trippedCircuitBreaker(events)) {
|
|
845
|
+
reject(new CircuitBreakerTrippedError(txHash.toHex()));
|
|
846
|
+
return;
|
|
847
|
+
}
|
|
848
|
+
try {
|
|
849
|
+
onIncluded?.(events);
|
|
850
|
+
resolve(txHash.toHex());
|
|
851
|
+
}
|
|
852
|
+
catch (error) {
|
|
853
|
+
reject(error);
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
}).catch(reject);
|
|
857
|
+
});
|
|
858
|
+
}
|
|
859
|
+
function sleep(ms) {
|
|
860
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
861
|
+
}
|
|
862
|
+
export { flopToRaw, formatFlop };
|
|
863
|
+
//# sourceMappingURL=client.js.map
|