@perkos/agent-sdk 0.1.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 +32 -0
- package/LICENSE +21 -0
- package/README.md +226 -0
- package/SECURITY.md +67 -0
- package/dist/builders.d.ts +19 -0
- package/dist/builders.d.ts.map +1 -0
- package/dist/builders.js +180 -0
- package/dist/builders.js.map +1 -0
- package/dist/clarity.d.ts +11 -0
- package/dist/clarity.d.ts.map +1 -0
- package/dist/clarity.js +80 -0
- package/dist/clarity.js.map +1 -0
- package/dist/client.d.ts +39 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +290 -0
- package/dist/client.js.map +1 -0
- package/dist/constants.d.ts +12 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +72 -0
- package/dist/constants.js.map +1 -0
- package/dist/errors.d.ts +14 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +11 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/policy.d.ts +14 -0
- package/dist/policy.d.ts.map +1 -0
- package/dist/policy.js +75 -0
- package/dist/policy.js.map +1 -0
- package/dist/signers.d.ts +51 -0
- package/dist/signers.d.ts.map +1 -0
- package/dist/signers.js +145 -0
- package/dist/signers.js.map +1 -0
- package/dist/tracker.d.ts +21 -0
- package/dist/tracker.d.ts.map +1 -0
- package/dist/tracker.js +175 -0
- package/dist/tracker.js.map +1 -0
- package/dist/txid.d.ts +2 -0
- package/dist/txid.d.ts.map +1 -0
- package/dist/txid.js +13 -0
- package/dist/txid.js.map +1 -0
- package/dist/types.d.ts +204 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/validation.d.ts +17 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +124 -0
- package/dist/validation.js.map +1 -0
- package/docs/ARCHITECTURE.md +111 -0
- package/examples/quickstart.ts +19 -0
- package/examples/testnet-api.ts +23 -0
- package/examples/testnet-lifecycle.ts +305 -0
- package/examples/testnet.env.example +14 -0
- package/package.json +69 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@perkos/agent-sdk` are documented here.
|
|
4
|
+
|
|
5
|
+
## 0.1.0 - 2026-08-24
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- Validated mainnet and testnet deployment configuration.
|
|
10
|
+
- Read-only client for agents, jobs, escrow balances, payment-token configuration, and reputation.
|
|
11
|
+
- Transaction-plan builders for agent registration and the complete STX/sBTC job lifecycle.
|
|
12
|
+
- Exact funding and settlement post-conditions.
|
|
13
|
+
- Pluggable signer interface with structured transaction receipts.
|
|
14
|
+
- Included headless signer with an on-demand external key provider.
|
|
15
|
+
- Included Stacks Connect signer adapter for Leather and compatible browser wallets.
|
|
16
|
+
- Normalized transaction tracking with bounded polling, cancellation, terminal statuses, block
|
|
17
|
+
metadata, and Clarity result fields.
|
|
18
|
+
- Client helpers to confirm a broadcast or execute and return a combined confirmation receipt.
|
|
19
|
+
- Fail-closed spending policy with per-transaction and per-session limits.
|
|
20
|
+
- Read-only mainnet quickstart and an opt-in transactional sBTC testnet lifecycle using distinct
|
|
21
|
+
client, provider, and evaluator roles.
|
|
22
|
+
- Safe dry-run mode for the transactional quickstart, enabled by default.
|
|
23
|
+
|
|
24
|
+
### Fixed
|
|
25
|
+
|
|
26
|
+
- Resolve the package self-reference to source during development so clean CI environments can
|
|
27
|
+
typecheck the quickstart before `dist/` exists.
|
|
28
|
+
- Reject malformed signer results instead of accepting non-transaction identifiers as broadcasts.
|
|
29
|
+
- Use the Stacks node `/v2/info` endpoint for the transactional quickstart chain height and cover
|
|
30
|
+
the request with a regression test.
|
|
31
|
+
- Hydrate terminal Clarity results from transaction detail when the v3 status response returns a
|
|
32
|
+
null result.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 PerkOS
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
# `@perkos/agent-sdk`
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for agent identity, escrow settlement, and reputation on Stacks.
|
|
4
|
+
|
|
5
|
+
PerkOS gives AI agents a programmable path to register, hire, fund work with STX or sBTC,
|
|
6
|
+
submit deliverables, settle escrow, and build job-linked reputation.
|
|
7
|
+
|
|
8
|
+
> Status: 0.1.0 developer release. Read clients, transaction builders, browser and headless signer
|
|
9
|
+
> adapters, confirmation receipts, safety policies, and a transactional testnet quickstart are
|
|
10
|
+
> implemented. x402/MCP adapters, external review, and adoption evidence remain before Milestone 2
|
|
11
|
+
> completion.
|
|
12
|
+
|
|
13
|
+
## Requirements
|
|
14
|
+
|
|
15
|
+
- Node.js 20 or newer
|
|
16
|
+
- npm 10 or newer
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @perkos/agent-sdk
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
To develop from source:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
git clone https://github.com/PerkOS-xyz/PerkOS-Agent-SDK.git
|
|
28
|
+
cd PerkOS-Agent-SDK
|
|
29
|
+
npm install
|
|
30
|
+
npm run verify
|
|
31
|
+
npm run quickstart
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
The quickstart performs public read-only calls and does not require a wallet or private key.
|
|
35
|
+
Set `PERKOS_NETWORK=testnet` to read the testnet deployment.
|
|
36
|
+
|
|
37
|
+
The transactional quickstart is also safe by default: it only prints a seven-step sBTC testnet
|
|
38
|
+
lifecycle and its funding-policy decision.
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
npm run quickstart:testnet
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Live testnet execution requires three distinct funded roles and the exact opt-in documented in
|
|
45
|
+
[`examples/testnet.env.example`](examples/testnet.env.example). It confirms every transaction
|
|
46
|
+
before moving to the next lifecycle step. The complete flow was verified on testnet with exact
|
|
47
|
+
100-satoshi escrow, provider payout, cleared escrow, and reputation update; the
|
|
48
|
+
[completion transaction](https://explorer.hiro.so/txid/0x5cf34295641a9291a2b6785d6db95a5c56b4d3b40d4281c86da194acd4c64248?chain=testnet)
|
|
49
|
+
is publicly inspectable.
|
|
50
|
+
|
|
51
|
+
## Read on-chain state
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { PerkOSClient } from "@perkos/agent-sdk";
|
|
55
|
+
|
|
56
|
+
const perkos = new PerkOSClient({ network: "mainnet" });
|
|
57
|
+
|
|
58
|
+
const agentCount = await perkos.getAgentCount();
|
|
59
|
+
const sbtcJob = await perkos.getJob("sbtc", 1n);
|
|
60
|
+
const escrow = await perkos.getEscrowBalance("sbtc", 1n);
|
|
61
|
+
const reputation = await perkos.getReputation(
|
|
62
|
+
"SP000000000000000000002Q6VF78"
|
|
63
|
+
);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Amounts are returned as `bigint`. sBTC values are satoshis and STX values are micro-STX.
|
|
67
|
+
|
|
68
|
+
## Build a transaction plan
|
|
69
|
+
|
|
70
|
+
Builders return explicit, inspectable plans. They do not sign, broadcast, or read environment
|
|
71
|
+
variables.
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
const plan = perkos.transactions.registerAgent({
|
|
75
|
+
name: "Research Agent",
|
|
76
|
+
description: "Produces cited market research.",
|
|
77
|
+
wallet: "SP...",
|
|
78
|
+
endpoints: [{ name: "mcp", url: "https://agent.example/mcp" }],
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
console.log(plan.contract, plan.functionName, plan.functionArgs);
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Funding plans require the sender and generate an exact post-condition:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
const plan = perkos.transactions.fundJob({
|
|
88
|
+
asset: "sbtc",
|
|
89
|
+
jobId: 7n,
|
|
90
|
+
amount: 25_000n,
|
|
91
|
+
sender: "SP...",
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Settlement helpers read the job and escrow balance before building exact contract-principal
|
|
96
|
+
post-conditions. This protects both wallet-originated and headless transactions from transferring
|
|
97
|
+
more than the job requires.
|
|
98
|
+
|
|
99
|
+
## Browser signer (Leather and other Stacks wallets)
|
|
100
|
+
|
|
101
|
+
`StacksConnectSigner` adapts the official Stacks Connect request API. The application supplies
|
|
102
|
+
the connected address and request function; the SDK forwards the complete plan and never receives
|
|
103
|
+
wallet key material.
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
import { request } from "@stacks/connect";
|
|
107
|
+
import { PerkOSClient, StacksConnectSigner } from "@perkos/agent-sdk";
|
|
108
|
+
|
|
109
|
+
const signer = new StacksConnectSigner({
|
|
110
|
+
network: "mainnet",
|
|
111
|
+
address: connectedStacksAddress,
|
|
112
|
+
request,
|
|
113
|
+
});
|
|
114
|
+
const perkos = new PerkOSClient({ network: "mainnet", signer });
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Install `@stacks/connect` in the browser application. Wallet discovery and account selection stay
|
|
118
|
+
at the application boundary so the SDK cannot silently choose an account.
|
|
119
|
+
|
|
120
|
+
## Headless signer
|
|
121
|
+
|
|
122
|
+
`HeadlessSigner` requests key material only when deriving the public address or signing. It caches
|
|
123
|
+
only the derived address. Use an external secret manager, KMS adapter, or another controlled key
|
|
124
|
+
provider; never hardcode production keys.
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
import { HeadlessSigner, PerkOSClient } from "@perkos/agent-sdk";
|
|
128
|
+
|
|
129
|
+
const signer = new HeadlessSigner({
|
|
130
|
+
network: "testnet",
|
|
131
|
+
privateKeyProvider: async () => loadKeyFromSecretManager(),
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const perkos = new PerkOSClient({ network: "testnet", signer });
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
The callback may return a string or `Uint8Array`. The SDK copies and zeroes its local binary-key
|
|
138
|
+
buffer after use; JavaScript strings cannot be reliably erased from memory.
|
|
139
|
+
|
|
140
|
+
## Confirmation receipts
|
|
141
|
+
|
|
142
|
+
Broadcast acceptance is not execution success. Confirm through the configured Stacks API before
|
|
143
|
+
advancing an automated workflow:
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
const broadcast = await perkos.execute(plan);
|
|
147
|
+
const confirmation = await perkos.confirm(broadcast, {
|
|
148
|
+
timeoutMs: 10 * 60_000,
|
|
149
|
+
pollIntervalMs: 5_000,
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
if (confirmation.status !== "success") {
|
|
153
|
+
throw new Error(`Transaction ended with ${confirmation.status}`);
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Confirmation status is normalized to `pending`, `success`, `abort`, `dropped`, or `timeout`, with
|
|
158
|
+
block and Clarity-result fields when the API supplies them. Polling supports cancellation through
|
|
159
|
+
an `AbortSignal`. If the v3 status response omits a terminal Clarity result, the tracker hydrates
|
|
160
|
+
that field from the transaction-detail endpoint.
|
|
161
|
+
|
|
162
|
+
## Spending policy
|
|
163
|
+
|
|
164
|
+
Funding is fail-closed. Both limits must be configured for every spendable asset:
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
const perkos = new PerkOSClient({
|
|
168
|
+
network: "mainnet",
|
|
169
|
+
signer: new WalletSigner(),
|
|
170
|
+
spendingPolicy: {
|
|
171
|
+
allowedAssets: ["sbtc"],
|
|
172
|
+
maxPerTransaction: { sbtc: 50_000n },
|
|
173
|
+
maxPerSession: { sbtc: 150_000n },
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
await perkos.fundJob({
|
|
178
|
+
asset: "sbtc",
|
|
179
|
+
jobId: 7n,
|
|
180
|
+
amount: 25_000n,
|
|
181
|
+
});
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
The policy also limits networks and contract principals. Funding attempts without explicit
|
|
185
|
+
per-transaction and per-session limits are rejected before the signer is called.
|
|
186
|
+
|
|
187
|
+
## Supported lifecycle
|
|
188
|
+
|
|
189
|
+
- Register, update, and deactivate agents.
|
|
190
|
+
- Read agent records and counts.
|
|
191
|
+
- Create STX or sBTC jobs.
|
|
192
|
+
- Set a budget and fund escrow.
|
|
193
|
+
- Assign a provider and submit a deliverable.
|
|
194
|
+
- Complete, reject, or expire a job.
|
|
195
|
+
- Rate a provider and read reputation.
|
|
196
|
+
|
|
197
|
+
## Security model
|
|
198
|
+
|
|
199
|
+
- Mainnet or testnet must be selected explicitly.
|
|
200
|
+
- Contract overrides must match the selected network.
|
|
201
|
+
- Funding uses exact sender post-conditions.
|
|
202
|
+
- Settlement uses exact escrow-contract post-conditions.
|
|
203
|
+
- Agent-controlled payments require transaction and session budgets.
|
|
204
|
+
- The core SDK never accepts arbitrary URLs or payment destinations from an LLM.
|
|
205
|
+
- `execute` refuses plans created for a different network or signer.
|
|
206
|
+
- Broadcast receipts require a valid 32-byte Stacks transaction ID.
|
|
207
|
+
- Automated workflows can wait for an explicit terminal confirmation before their next action.
|
|
208
|
+
|
|
209
|
+
This package has not yet completed the external security review required for PerkOS Milestone 2.
|
|
210
|
+
Do not treat the developer release as audited software.
|
|
211
|
+
|
|
212
|
+
See [Architecture](docs/ARCHITECTURE.md) and [Security](SECURITY.md) for the trust boundaries and
|
|
213
|
+
responsible disclosure process.
|
|
214
|
+
|
|
215
|
+
## Development
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
npm install
|
|
219
|
+
npm run typecheck
|
|
220
|
+
npm test
|
|
221
|
+
npm run build
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
## License
|
|
225
|
+
|
|
226
|
+
MIT
|
package/SECURITY.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Security
|
|
2
|
+
|
|
3
|
+
## Release status
|
|
4
|
+
|
|
5
|
+
`@perkos/agent-sdk` 0.1.0 is an M2 foundation release and has not completed the external security
|
|
6
|
+
review required for the PerkOS Stacks Endowment milestone.
|
|
7
|
+
|
|
8
|
+
Do not use this developer release to control material funds without your own review. Inspect every
|
|
9
|
+
transaction plan in the signer and configure explicit spending limits.
|
|
10
|
+
|
|
11
|
+
## Reporting a vulnerability
|
|
12
|
+
|
|
13
|
+
Do not open a public issue for a suspected vulnerability. Send a private report to
|
|
14
|
+
`security@perkos.xyz` with:
|
|
15
|
+
|
|
16
|
+
- the affected version and network;
|
|
17
|
+
- reproduction steps or a proof of concept;
|
|
18
|
+
- the potential impact;
|
|
19
|
+
- relevant contract IDs, transaction IDs, or logs;
|
|
20
|
+
- a safe way to contact the reporter.
|
|
21
|
+
|
|
22
|
+
The PerkOS team will acknowledge the report, validate it, and coordinate remediation and
|
|
23
|
+
disclosure.
|
|
24
|
+
|
|
25
|
+
## Implemented safeguards
|
|
26
|
+
|
|
27
|
+
- Explicit network selection and network-compatible contract validation.
|
|
28
|
+
- Exact post-conditions for wallet funding.
|
|
29
|
+
- Exact escrow-contract post-conditions for completion, rejection, and refunds.
|
|
30
|
+
- Contract and asset allowlists.
|
|
31
|
+
- Required per-transaction and per-session funding limits.
|
|
32
|
+
- Funding-limit checks occur before requesting a signer address.
|
|
33
|
+
- Signer-address matching for funding plans.
|
|
34
|
+
- No automatic environment-variable or private-key loading.
|
|
35
|
+
- No arbitrary URL or payment-destination execution surface in the core SDK.
|
|
36
|
+
- Strict validation and normalization of signer-returned Stacks transaction IDs.
|
|
37
|
+
- Bounded, cancellable confirmation polling with explicit abort, dropped, and timeout states.
|
|
38
|
+
- Network mismatch rejection in both browser and headless signer adapters.
|
|
39
|
+
|
|
40
|
+
## Key handling
|
|
41
|
+
|
|
42
|
+
`StacksConnectSigner` delegates signing to the wallet extension and never receives a private key.
|
|
43
|
+
The host application must select the intended account and show the wallet approval surface.
|
|
44
|
+
|
|
45
|
+
`HeadlessSigner` calls an application-owned `privateKeyProvider` only when it needs to derive the
|
|
46
|
+
public address or sign. It caches only the public address. When the provider returns a
|
|
47
|
+
`Uint8Array`, the SDK signs with a copy and zeroes that local copy after the operation. JavaScript
|
|
48
|
+
strings are immutable and cannot be reliably erased; use binary key material and an isolated
|
|
49
|
+
secret provider when the runtime permits it.
|
|
50
|
+
|
|
51
|
+
The SDK does not make an unsafe runtime safe. Restrict process access, rotate credentials, separate
|
|
52
|
+
client/provider/evaluator identities, and use an HSM, KMS, or isolated signer for material funds.
|
|
53
|
+
|
|
54
|
+
## Confirmation handling
|
|
55
|
+
|
|
56
|
+
A broadcast receipt means a node accepted the transaction, not that the contract call succeeded.
|
|
57
|
+
Automated workflows should call `confirm` or `executeAndConfirm`, require `success`, inspect the
|
|
58
|
+
Clarity result, and choose an additional confirmation threshold appropriate to the value at risk.
|
|
59
|
+
Treat transaction APIs as external dependencies and retain the transaction ID for independent
|
|
60
|
+
verification.
|
|
61
|
+
|
|
62
|
+
## Trust boundaries
|
|
63
|
+
|
|
64
|
+
The SDK prepares, validates, and submits transaction plans through an application-provided
|
|
65
|
+
signer. It does not prove that the signer implementation is safe. Applications must protect
|
|
66
|
+
private keys, verify wallet prompts, authenticate agent commands, and treat external tool or
|
|
67
|
+
model output as untrusted.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AssignProviderInput, ContractCallPlan, CreateJobInput, FundJobInput, JobAmountInput, RateProviderInput, RegisterAgentInput, ResolvedPerkOSConfig, SettleJobInput, SubmitWorkInput, UpdateAgentInput } from "./types.js";
|
|
2
|
+
export declare class PerkOSTransactionBuilder {
|
|
3
|
+
readonly config: ResolvedPerkOSConfig;
|
|
4
|
+
constructor(config: ResolvedPerkOSConfig);
|
|
5
|
+
registerAgent(input: RegisterAgentInput): ContractCallPlan;
|
|
6
|
+
updateAgent(input: UpdateAgentInput): ContractCallPlan;
|
|
7
|
+
deactivateAgent(agentIdInput: bigint | number | string): ContractCallPlan;
|
|
8
|
+
createJob(input: CreateJobInput): ContractCallPlan;
|
|
9
|
+
setBudget(input: JobAmountInput): ContractCallPlan;
|
|
10
|
+
fundJob(input: FundJobInput): ContractCallPlan;
|
|
11
|
+
assignProvider(input: AssignProviderInput): ContractCallPlan;
|
|
12
|
+
submitWork(input: SubmitWorkInput): ContractCallPlan;
|
|
13
|
+
completeJob(input: SettleJobInput): ContractCallPlan;
|
|
14
|
+
rejectJob(input: SettleJobInput): ContractCallPlan;
|
|
15
|
+
expireJob(input: SettleJobInput): ContractCallPlan;
|
|
16
|
+
rateProvider(input: RateProviderInput): ContractCallPlan;
|
|
17
|
+
private settlement;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=builders.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builders.d.ts","sourceRoot":"","sources":["../src/builders.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,mBAAmB,EACnB,gBAAgB,EAEhB,cAAc,EACd,YAAY,EACZ,cAAc,EAEd,iBAAiB,EACjB,kBAAkB,EAClB,oBAAoB,EACpB,cAAc,EACd,eAAe,EACf,gBAAgB,EACjB,MAAM,YAAY,CAAC;AA6EpB,qBAAa,wBAAwB;IACnC,QAAQ,CAAC,MAAM,EAAE,oBAAoB,CAAC;gBAE1B,MAAM,EAAE,oBAAoB;IAIxC,aAAa,CAAC,KAAK,EAAE,kBAAkB,GAAG,gBAAgB;IA8B1D,WAAW,CAAC,KAAK,EAAE,gBAAgB,GAAG,gBAAgB;IAuBtD,eAAe,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,gBAAgB;IAWzE,SAAS,CAAC,KAAK,EAAE,cAAc,GAAG,gBAAgB;IAsBlD,SAAS,CAAC,KAAK,EAAE,cAAc,GAAG,gBAAgB;IAYlD,OAAO,CAAC,KAAK,EAAE,YAAY,GAAG,gBAAgB;IA2B9C,cAAc,CAAC,KAAK,EAAE,mBAAmB,GAAG,gBAAgB;IAiB5D,UAAU,CAAC,KAAK,EAAE,eAAe,GAAG,gBAAgB;IAWpD,WAAW,CAAC,KAAK,EAAE,cAAc,GAAG,gBAAgB;IAIpD,SAAS,CAAC,KAAK,EAAE,cAAc,GAAG,gBAAgB;IAIlD,SAAS,CAAC,KAAK,EAAE,cAAc,GAAG,gBAAgB;IAIlD,YAAY,CAAC,KAAK,EAAE,iBAAiB,GAAG,gBAAgB;IAgBxD,OAAO,CAAC,UAAU;CAyBnB"}
|
package/dist/builders.js
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { Cl, Pc } from "@stacks/transactions";
|
|
2
|
+
import { PerkOSError } from "./errors.js";
|
|
3
|
+
import { assertAscii, assertPrincipal, parseContractId, toUint, } from "./validation.js";
|
|
4
|
+
function commerceContract(config, asset) {
|
|
5
|
+
return asset === "sbtc" ? config.contracts.sbtcCommerce : config.contracts.stxCommerce;
|
|
6
|
+
}
|
|
7
|
+
function plan(config, contract, functionName, functionArgs, intent, postConditions = []) {
|
|
8
|
+
return {
|
|
9
|
+
type: "contract-call",
|
|
10
|
+
network: config.network,
|
|
11
|
+
contract,
|
|
12
|
+
functionName,
|
|
13
|
+
functionArgs,
|
|
14
|
+
postConditions,
|
|
15
|
+
postConditionMode: "deny",
|
|
16
|
+
intent,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function tokenArg(config) {
|
|
20
|
+
const token = parseContractId(config.contracts.sbtcToken, "contracts.sbtcToken", config.network);
|
|
21
|
+
return Cl.contractPrincipal(token.address, token.name);
|
|
22
|
+
}
|
|
23
|
+
function escrowArgs(config, asset, baseArgs) {
|
|
24
|
+
return asset === "sbtc" ? [...baseArgs, tokenArg(config)] : baseArgs;
|
|
25
|
+
}
|
|
26
|
+
function exactTransfer(config, asset, sender, amount) {
|
|
27
|
+
if (asset === "stx") {
|
|
28
|
+
return Pc.principal(sender).willSendEq(amount).ustx();
|
|
29
|
+
}
|
|
30
|
+
return Pc.principal(sender)
|
|
31
|
+
.willSendEq(amount)
|
|
32
|
+
.ft(config.contracts.sbtcToken, config.contracts.sbtcAssetName);
|
|
33
|
+
}
|
|
34
|
+
function deliverableBuffer(value) {
|
|
35
|
+
const bytes = typeof value === "string"
|
|
36
|
+
? new TextEncoder().encode(value)
|
|
37
|
+
: Uint8Array.from(value);
|
|
38
|
+
if (bytes.length === 0 || bytes.length > 64) {
|
|
39
|
+
throw new PerkOSError("INPUT_INVALID", "deliverable must contain between 1 and 64 bytes.", { bytes: bytes.length });
|
|
40
|
+
}
|
|
41
|
+
if (typeof value === "string") {
|
|
42
|
+
assertAscii(value, "deliverable", 64);
|
|
43
|
+
}
|
|
44
|
+
return bytes;
|
|
45
|
+
}
|
|
46
|
+
export class PerkOSTransactionBuilder {
|
|
47
|
+
config;
|
|
48
|
+
constructor(config) {
|
|
49
|
+
this.config = config;
|
|
50
|
+
}
|
|
51
|
+
registerAgent(input) {
|
|
52
|
+
assertAscii(input.name, "name", 64);
|
|
53
|
+
assertAscii(input.description, "description", 256);
|
|
54
|
+
assertPrincipal(input.wallet, "wallet", this.config.network);
|
|
55
|
+
const endpoints = input.endpoints ?? [];
|
|
56
|
+
if (endpoints.length > 10) {
|
|
57
|
+
throw new PerkOSError("INPUT_INVALID", "endpoints cannot contain more than 10 entries.");
|
|
58
|
+
}
|
|
59
|
+
const endpointCVs = endpoints.map((endpoint, index) => {
|
|
60
|
+
assertAscii(endpoint.name, `endpoints[${index}].name`, 32);
|
|
61
|
+
assertAscii(endpoint.url, `endpoints[${index}].url`, 128);
|
|
62
|
+
return Cl.tuple({
|
|
63
|
+
name: Cl.stringAscii(endpoint.name),
|
|
64
|
+
url: Cl.stringAscii(endpoint.url),
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
return plan(this.config, this.config.contracts.agentRegistry, "register-agent", [
|
|
68
|
+
Cl.stringAscii(input.name),
|
|
69
|
+
Cl.stringAscii(input.description),
|
|
70
|
+
Cl.principal(input.wallet),
|
|
71
|
+
Cl.list(endpointCVs),
|
|
72
|
+
], { operation: "register-agent" });
|
|
73
|
+
}
|
|
74
|
+
updateAgent(input) {
|
|
75
|
+
const agentId = toUint(input.agentId, "agentId");
|
|
76
|
+
if (input.name !== undefined)
|
|
77
|
+
assertAscii(input.name, "name", 64);
|
|
78
|
+
if (input.description !== undefined)
|
|
79
|
+
assertAscii(input.description, "description", 256);
|
|
80
|
+
if (input.wallet !== undefined) {
|
|
81
|
+
assertPrincipal(input.wallet, "wallet", this.config.network);
|
|
82
|
+
}
|
|
83
|
+
return plan(this.config, this.config.contracts.agentRegistry, "update-agent", [
|
|
84
|
+
Cl.uint(agentId),
|
|
85
|
+
input.name === undefined ? Cl.none() : Cl.some(Cl.stringAscii(input.name)),
|
|
86
|
+
input.description === undefined
|
|
87
|
+
? Cl.none()
|
|
88
|
+
: Cl.some(Cl.stringAscii(input.description)),
|
|
89
|
+
input.wallet === undefined ? Cl.none() : Cl.some(Cl.principal(input.wallet)),
|
|
90
|
+
], { operation: "update-agent" });
|
|
91
|
+
}
|
|
92
|
+
deactivateAgent(agentIdInput) {
|
|
93
|
+
const agentId = toUint(agentIdInput, "agentId");
|
|
94
|
+
return plan(this.config, this.config.contracts.agentRegistry, "deactivate-agent", [Cl.uint(agentId)], { operation: "deactivate-agent" });
|
|
95
|
+
}
|
|
96
|
+
createJob(input) {
|
|
97
|
+
assertPrincipal(input.evaluator, "evaluator", this.config.network);
|
|
98
|
+
if (input.provider)
|
|
99
|
+
assertPrincipal(input.provider, "provider", this.config.network);
|
|
100
|
+
if (input.provider === input.evaluator) {
|
|
101
|
+
throw new PerkOSError("INPUT_INVALID", "provider and evaluator must be different.");
|
|
102
|
+
}
|
|
103
|
+
assertAscii(input.description, "description", 512);
|
|
104
|
+
const expiredAt = toUint(input.expiredAt, "expiredAt");
|
|
105
|
+
return plan(this.config, commerceContract(this.config, input.asset), "create-job", [
|
|
106
|
+
input.provider ? Cl.some(Cl.principal(input.provider)) : Cl.none(),
|
|
107
|
+
Cl.principal(input.evaluator),
|
|
108
|
+
Cl.uint(expiredAt),
|
|
109
|
+
Cl.stringAscii(input.description),
|
|
110
|
+
], { operation: "create-job", asset: input.asset });
|
|
111
|
+
}
|
|
112
|
+
setBudget(input) {
|
|
113
|
+
const jobId = toUint(input.jobId, "jobId");
|
|
114
|
+
const amount = toUint(input.amount, "amount");
|
|
115
|
+
return plan(this.config, commerceContract(this.config, input.asset), "set-budget", [Cl.uint(jobId), Cl.uint(amount)], { operation: "set-budget", asset: input.asset, amount, jobId });
|
|
116
|
+
}
|
|
117
|
+
fundJob(input) {
|
|
118
|
+
const jobId = toUint(input.jobId, "jobId");
|
|
119
|
+
const amount = toUint(input.amount, "amount");
|
|
120
|
+
if (!input.sender) {
|
|
121
|
+
throw new PerkOSError("INPUT_INVALID", "sender is required to create the exact funding post-condition.");
|
|
122
|
+
}
|
|
123
|
+
assertPrincipal(input.sender, "sender", this.config.network);
|
|
124
|
+
const contract = commerceContract(this.config, input.asset);
|
|
125
|
+
return plan(this.config, contract, "fund-job", escrowArgs(this.config, input.asset, [Cl.uint(jobId)]), {
|
|
126
|
+
operation: "fund-job",
|
|
127
|
+
asset: input.asset,
|
|
128
|
+
amount,
|
|
129
|
+
jobId,
|
|
130
|
+
sender: input.sender,
|
|
131
|
+
}, [exactTransfer(this.config, input.asset, input.sender, amount)]);
|
|
132
|
+
}
|
|
133
|
+
assignProvider(input) {
|
|
134
|
+
const jobId = toUint(input.jobId, "jobId");
|
|
135
|
+
assertPrincipal(input.provider, "provider", this.config.network);
|
|
136
|
+
return plan(this.config, commerceContract(this.config, input.asset), "assign-provider", [Cl.uint(jobId), Cl.principal(input.provider)], {
|
|
137
|
+
operation: "assign-provider",
|
|
138
|
+
asset: input.asset,
|
|
139
|
+
jobId,
|
|
140
|
+
recipient: input.provider,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
submitWork(input) {
|
|
144
|
+
const jobId = toUint(input.jobId, "jobId");
|
|
145
|
+
return plan(this.config, commerceContract(this.config, input.asset), "submit-work", [Cl.uint(jobId), Cl.buffer(deliverableBuffer(input.deliverable))], { operation: "submit-work", asset: input.asset, jobId });
|
|
146
|
+
}
|
|
147
|
+
completeJob(input) {
|
|
148
|
+
return this.settlement("complete-job", input);
|
|
149
|
+
}
|
|
150
|
+
rejectJob(input) {
|
|
151
|
+
return this.settlement("reject-job", input);
|
|
152
|
+
}
|
|
153
|
+
expireJob(input) {
|
|
154
|
+
return this.settlement("expire-job", input);
|
|
155
|
+
}
|
|
156
|
+
rateProvider(input) {
|
|
157
|
+
const jobId = toUint(input.jobId, "jobId");
|
|
158
|
+
const score = toUint(input.score, "score");
|
|
159
|
+
if (score > 5n) {
|
|
160
|
+
throw new PerkOSError("INPUT_INVALID", "score must be between 1 and 5.");
|
|
161
|
+
}
|
|
162
|
+
assertAscii(input.comment, "comment", 256, true);
|
|
163
|
+
return plan(this.config, commerceContract(this.config, input.asset), "rate-provider", [Cl.uint(jobId), Cl.uint(score), Cl.stringAscii(input.comment)], { operation: "rate-provider", asset: input.asset, jobId });
|
|
164
|
+
}
|
|
165
|
+
settlement(operation, input) {
|
|
166
|
+
const jobId = toUint(input.jobId, "jobId");
|
|
167
|
+
const amount = toUint(input.amount, "amount", true);
|
|
168
|
+
assertPrincipal(input.recipient, "recipient", this.config.network);
|
|
169
|
+
const contract = commerceContract(this.config, input.asset);
|
|
170
|
+
const postConditions = amount > 0n ? [exactTransfer(this.config, input.asset, contract, amount)] : [];
|
|
171
|
+
return plan(this.config, contract, operation, escrowArgs(this.config, input.asset, [Cl.uint(jobId)]), {
|
|
172
|
+
operation,
|
|
173
|
+
asset: input.asset,
|
|
174
|
+
amount,
|
|
175
|
+
jobId,
|
|
176
|
+
recipient: input.recipient,
|
|
177
|
+
}, postConditions);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
//# sourceMappingURL=builders.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builders.js","sourceRoot":"","sources":["../src/builders.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,sBAAsB,CAAC;AAE9C,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAgB1C,OAAO,EACL,WAAW,EACX,eAAe,EACf,eAAe,EACf,MAAM,GACP,MAAM,iBAAiB,CAAC;AAEzB,SAAS,gBAAgB,CAAC,MAA4B,EAAE,KAAmB;IACzE,OAAO,KAAK,KAAK,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC,CAA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ClB,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,KAAK,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QACjF,OAAO,IAAI,CACT,IAAI,CAAC,MAAM,EACX,QAAQ,EACR,SAAS,EACT,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EACtD;YACE,SAAS;YACT,KAAK,EAAE,KAAK,CAAC,KAAK;YAClB,MAAM;YACN,KAAK;YACL,SAAS,EAAE,KAAK,CAAC,SAAS;SAC3B,EACD,cAAc,CACf,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ClarityValue, TupleCV } from "@stacks/transactions";
|
|
2
|
+
export declare function unwrapResponse(value: ClarityValue, context: string): ClarityValue;
|
|
3
|
+
export declare function expectTuple(value: ClarityValue, context: string): TupleCV["value"];
|
|
4
|
+
export declare function expectList(value: ClarityValue, context: string): readonly ClarityValue[];
|
|
5
|
+
export declare function expectUint(value: ClarityValue | undefined, context: string): bigint;
|
|
6
|
+
export declare function expectString(value: ClarityValue | undefined, context: string): string;
|
|
7
|
+
export declare function expectPrincipal(value: ClarityValue | undefined, context: string): string;
|
|
8
|
+
export declare function expectBoolean(value: ClarityValue | undefined, context: string): boolean;
|
|
9
|
+
export declare function optionalPrincipal(value: ClarityValue | undefined, context: string): string | undefined;
|
|
10
|
+
export declare function optionalBuffer(value: ClarityValue | undefined, context: string): string | undefined;
|
|
11
|
+
//# sourceMappingURL=clarity.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clarity.d.ts","sourceRoot":"","sources":["../src/clarity.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EAKZ,OAAO,EACR,MAAM,sBAAsB,CAAC;AAQ9B,wBAAgB,cAAc,CAAC,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,GAAG,YAAY,CAsBjF;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAKlF;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,GAAG,SAAS,YAAY,EAAE,CAKxF;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,YAAY,GAAG,SAAS,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAKnF;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,YAAY,GAAG,SAAS,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAQrF;AAED,wBAAgB,eAAe,CAAC,KAAK,EAAE,YAAY,GAAG,SAAS,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CASxF;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,YAAY,GAAG,SAAS,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAKvF;AAED,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,YAAY,GAAG,SAAS,EAC/B,OAAO,EAAE,MAAM,GACd,MAAM,GAAG,SAAS,CAMpB;AAED,wBAAgB,cAAc,CAC5B,KAAK,EAAE,YAAY,GAAG,SAAS,EAC/B,OAAO,EAAE,MAAM,GACd,MAAM,GAAG,SAAS,CAUpB"}
|
package/dist/clarity.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { ClarityType } from "@stacks/transactions";
|
|
2
|
+
import { CLARITY_ERROR_MESSAGES } from "./constants.js";
|
|
3
|
+
import { PerkOSError } from "./errors.js";
|
|
4
|
+
function cvType(value) {
|
|
5
|
+
return String(value.type);
|
|
6
|
+
}
|
|
7
|
+
export function unwrapResponse(value, context) {
|
|
8
|
+
if (value.type === ClarityType.ResponseOk) {
|
|
9
|
+
return value.value;
|
|
10
|
+
}
|
|
11
|
+
if (value.type === ClarityType.ResponseErr) {
|
|
12
|
+
const errorValue = value.value;
|
|
13
|
+
const clarityCode = errorValue.type === ClarityType.UInt || errorValue.type === ClarityType.Int
|
|
14
|
+
? BigInt(errorValue.value)
|
|
15
|
+
: undefined;
|
|
16
|
+
const knownMessage = clarityCode !== undefined ? CLARITY_ERROR_MESSAGES[Number(clarityCode)] : undefined;
|
|
17
|
+
throw new PerkOSError("CONTRACT_ERROR", knownMessage ?? `${context} returned a Clarity error.`, clarityCode === undefined ? undefined : { clarityCode });
|
|
18
|
+
}
|
|
19
|
+
throw new PerkOSError("READ_FAILED", `${context} returned ${cvType(value)} instead of a Clarity response.`);
|
|
20
|
+
}
|
|
21
|
+
export function expectTuple(value, context) {
|
|
22
|
+
if (value.type !== ClarityType.Tuple) {
|
|
23
|
+
throw new PerkOSError("READ_FAILED", `${context} must be a Clarity tuple.`);
|
|
24
|
+
}
|
|
25
|
+
return value.value;
|
|
26
|
+
}
|
|
27
|
+
export function expectList(value, context) {
|
|
28
|
+
if (value.type !== ClarityType.List) {
|
|
29
|
+
throw new PerkOSError("READ_FAILED", `${context} must be a Clarity list.`);
|
|
30
|
+
}
|
|
31
|
+
return value.value;
|
|
32
|
+
}
|
|
33
|
+
export function expectUint(value, context) {
|
|
34
|
+
if (!value || value.type !== ClarityType.UInt) {
|
|
35
|
+
throw new PerkOSError("READ_FAILED", `${context} must be a Clarity uint.`);
|
|
36
|
+
}
|
|
37
|
+
return BigInt(value.value);
|
|
38
|
+
}
|
|
39
|
+
export function expectString(value, context) {
|
|
40
|
+
if (!value ||
|
|
41
|
+
(value.type !== ClarityType.StringASCII && value.type !== ClarityType.StringUTF8)) {
|
|
42
|
+
throw new PerkOSError("READ_FAILED", `${context} must be a Clarity string.`);
|
|
43
|
+
}
|
|
44
|
+
return value.value;
|
|
45
|
+
}
|
|
46
|
+
export function expectPrincipal(value, context) {
|
|
47
|
+
if (!value ||
|
|
48
|
+
(value.type !== ClarityType.PrincipalStandard &&
|
|
49
|
+
value.type !== ClarityType.PrincipalContract)) {
|
|
50
|
+
throw new PerkOSError("READ_FAILED", `${context} must be a Clarity principal.`);
|
|
51
|
+
}
|
|
52
|
+
return value.value;
|
|
53
|
+
}
|
|
54
|
+
export function expectBoolean(value, context) {
|
|
55
|
+
if (!value || (value.type !== ClarityType.BoolTrue && value.type !== ClarityType.BoolFalse)) {
|
|
56
|
+
throw new PerkOSError("READ_FAILED", `${context} must be a Clarity boolean.`);
|
|
57
|
+
}
|
|
58
|
+
return value.type === ClarityType.BoolTrue;
|
|
59
|
+
}
|
|
60
|
+
export function optionalPrincipal(value, context) {
|
|
61
|
+
if (!value || value.type === ClarityType.OptionalNone)
|
|
62
|
+
return undefined;
|
|
63
|
+
if (value.type !== ClarityType.OptionalSome) {
|
|
64
|
+
throw new PerkOSError("READ_FAILED", `${context} must be a Clarity optional.`);
|
|
65
|
+
}
|
|
66
|
+
return expectPrincipal(value.value, context);
|
|
67
|
+
}
|
|
68
|
+
export function optionalBuffer(value, context) {
|
|
69
|
+
if (!value || value.type === ClarityType.OptionalNone)
|
|
70
|
+
return undefined;
|
|
71
|
+
if (value.type !== ClarityType.OptionalSome) {
|
|
72
|
+
throw new PerkOSError("READ_FAILED", `${context} must be a Clarity optional.`);
|
|
73
|
+
}
|
|
74
|
+
const inner = value.value;
|
|
75
|
+
if (inner.type !== ClarityType.Buffer) {
|
|
76
|
+
throw new PerkOSError("READ_FAILED", `${context} must contain a Clarity buffer.`);
|
|
77
|
+
}
|
|
78
|
+
return inner.value;
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=clarity.js.map
|