@heyanon-arp/sdk 0.0.2

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.
Files changed (46) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +147 -0
  3. package/dist/assets.d.ts +101 -0
  4. package/dist/attestation/attestation.d.ts +29 -0
  5. package/dist/attestation/index.d.ts +2 -0
  6. package/dist/attestation/scrypt-proof.d.ts +28 -0
  7. package/dist/canonical/canonicalize.d.ts +33 -0
  8. package/dist/canonical/index.d.ts +1 -0
  9. package/dist/challenge/challenge.d.ts +11 -0
  10. package/dist/challenge/index.d.ts +1 -0
  11. package/dist/cosignature/cosign.d.ts +35 -0
  12. package/dist/cosignature/index.d.ts +2 -0
  13. package/dist/did/document.d.ts +30 -0
  14. package/dist/did/format.d.ts +23 -0
  15. package/dist/did/index.d.ts +2 -0
  16. package/dist/envelope/index.d.ts +4 -0
  17. package/dist/envelope/sign.d.ts +28 -0
  18. package/dist/envelope/verify.d.ts +37 -0
  19. package/dist/escrow/caip19.d.ts +41 -0
  20. package/dist/escrow/condition-hash.d.ts +79 -0
  21. package/dist/escrow/create-lock.d.ts +39 -0
  22. package/dist/escrow/index.d.ts +4 -0
  23. package/dist/escrow/lock-id.d.ts +67 -0
  24. package/dist/index.d.ts +38 -0
  25. package/dist/index.js +853 -0
  26. package/dist/index.mjs +761 -0
  27. package/dist/keys/base58btc.d.ts +10 -0
  28. package/dist/keys/ed25519.d.ts +33 -0
  29. package/dist/keys/index.d.ts +3 -0
  30. package/dist/purpose.d.ts +52 -0
  31. package/dist/server-chain/chain.d.ts +52 -0
  32. package/dist/server-chain/index.d.ts +2 -0
  33. package/dist/settlement/index.d.ts +4 -0
  34. package/dist/settlement/settlement.d.ts +111 -0
  35. package/dist/settlement/token-program.d.ts +72 -0
  36. package/dist/types/body.d.ts +263 -0
  37. package/dist/types/envelope.d.ts +121 -0
  38. package/dist/types/identity.d.ts +62 -0
  39. package/dist/types/index.d.ts +5 -0
  40. package/dist/utils/index.d.ts +3 -0
  41. package/dist/utils/nonce.d.ts +9 -0
  42. package/dist/utils/timestamp.d.ts +17 -0
  43. package/dist/utils/uuid.d.ts +10 -0
  44. package/dist/webhook/index.d.ts +2 -0
  45. package/dist/webhook/webhook.d.ts +38 -0
  46. package/package.json +58 -0
@@ -0,0 +1,79 @@
1
+ import type { AssetIdentifier } from '../types';
2
+ /**
3
+ * Subset of contract terms that the on-chain `condition_hash` binds.
4
+ * Chosen per the escrow design's "condition_hash semantics" decision:
5
+ * fields that the parties must agree on at offer time AND that the
6
+ * release flow must replay against. Excludes timestamps and
7
+ * decline-reason fields (those only exist on terminal rows; including
8
+ * them would make every accepted contract's hash dependent on
9
+ * non-content metadata).
10
+ *
11
+ * The shape MUST match server-side `deriveConditionHash` byte-for-byte;
12
+ * any drift breaks E5 settlement-digest reconstruction (which reads
13
+ * `condition_hash` from the on-chain Lock and expects it to equal a
14
+ * server-side recompute).
15
+ */
16
+ export interface ContractTermsSubset {
17
+ contractId: string;
18
+ version: number;
19
+ scopeSummary: string;
20
+ pricingModel: string;
21
+ settlementModel: string;
22
+ rateAmount?: string;
23
+ rateCurrency?: AssetIdentifier;
24
+ rateUnit?: string;
25
+ allowedDelegationTags?: string[];
26
+ }
27
+ /**
28
+ * Loose input shape — the contract DTO / row / public response object
29
+ * may carry many extra fields (`state`, `relationshipId`, timestamps,
30
+ * decline reasons, etc.). Callers pass that whole object; we project
31
+ * onto `ContractTermsSubset` so extra fields can NEVER influence the
32
+ * condition_hash. This protects against silent drift if the public
33
+ * contract shape grows new fields.
34
+ *
35
+ * All fields are typed as optional here so a row pulled directly from
36
+ * Mongoose (where most contract fields are optional in the schema)
37
+ * fits the shape. The actual validity check happens inside
38
+ * `deriveConditionHash`, which throws if a required projected field
39
+ * is undefined — that surface is the right place to fail because
40
+ * `condition_hash` cannot be derived without it.
41
+ */
42
+ export interface ContractLikeInput {
43
+ contractId?: string;
44
+ version?: number;
45
+ scopeSummary?: string;
46
+ pricingModel?: string;
47
+ settlementModel?: string;
48
+ rateAmount?: string;
49
+ rateCurrency?: AssetIdentifier;
50
+ rateUnit?: string;
51
+ allowedDelegationTags?: string[];
52
+ [other: string]: unknown;
53
+ }
54
+ /**
55
+ * Compute the 32-byte sha256 condition_hash that binds an on-chain
56
+ * lock to the off-chain contract terms.
57
+ *
58
+ * The function performs an EXPLICIT FIELD PROJECTION before
59
+ * canonicalisation. Even if the caller passes a full contract row
60
+ * with `state`, `createdAt`, `declineReason`, etc., only the eight
61
+ * whitelisted fields are hashed. This is critical for digest
62
+ * stability across SDK versions: adding a field to the public
63
+ * contract shape MUST NOT change `condition_hash` values for existing
64
+ * contracts.
65
+ *
66
+ * Wire procedure:
67
+ * 1. Project the caller's input onto the whitelisted subset.
68
+ * 2. JCS-canonicalize via the SDK's `canonicalBytes`.
69
+ * 3. sha256 → 32-byte digest.
70
+ *
71
+ * The same procedure runs on the server during `delegation.offer`
72
+ * validation AND during `receipt cosign` settlement-digest
73
+ * reconstruction. Any field-set drift between offer-time and
74
+ * cosign-time produces a different hash, which fails Ed25519 verify
75
+ * on-chain.
76
+ *
77
+ * @returns 32-byte condition_hash
78
+ */
79
+ export declare function deriveConditionHash(contract: ContractLikeInput): Uint8Array;
@@ -0,0 +1,39 @@
1
+ export declare const CREATE_LOCK_DISCRIMINATOR: Uint8Array<ArrayBuffer>;
2
+ export interface CreateLockArgs {
3
+ /** 32-byte lock_id (deriveLockId output). */
4
+ lockId: Uint8Array;
5
+ /** u64 amount in base units. */
6
+ amount: bigint;
7
+ /** 32-byte condition_hash (deriveConditionHash output). */
8
+ conditionHash: Uint8Array;
9
+ /** u64 unix-seconds expiry. */
10
+ expiry: bigint;
11
+ }
12
+ /**
13
+ * Encode the create_lock instruction data: 8-byte disc +
14
+ * Borsh-encoded `CreateLockParams { lock_id, amount, condition_hash,
15
+ * expiry }`. Total 88 bytes — caller wraps in a TransactionInstruction
16
+ * with the matching 11-account meta list (payer, payee, config, lock,
17
+ * mint, payer_token_account, escrow_account, system_program,
18
+ * token_program, event_authority, program).
19
+ *
20
+ * The new contract (Anchor 0.32.1 + `#[event_cpi]`) dropped:
21
+ * - `is_native_sol` bool — native SOL is signalled by
22
+ * `mint == Pubkey::default()` (all-zero pubkey) in the mint
23
+ * slot of the account list, NOT by a flag in the args.
24
+ * - Caller-supplied bumps for `lock` / `vault` / `vault_authority`
25
+ * — Anchor reads the bumps from `ctx.bumps` itself, and the old
26
+ * `vault` + `vault_authority` PDAs are replaced by a single
27
+ * `escrow_account` PDA derived from `[b"escrow", lock_id]`.
28
+ *
29
+ * SDK callers that still pass the old fields will hit a clear TS
30
+ * compile error from the narrower `CreateLockArgs` shape — easier
31
+ * to migrate than a silently-wrong tx.
32
+ */
33
+ export declare function buildCreateLockIxData(args: CreateLockArgs): Uint8Array;
34
+ /**
35
+ * Sanity check: confirm the hard-coded discriminator matches what
36
+ * sha256("global:create_lock")[0..8] should produce. Exposed for the
37
+ * SDK test suite; not used at runtime.
38
+ */
39
+ export declare function computeCreateLockDiscriminator(): Uint8Array;
@@ -0,0 +1,4 @@
1
+ export { deriveLockId, delegationIdToBytes16, bytes16ToDelegationId } from './lock-id';
2
+ export { deriveConditionHash, type ContractTermsSubset, type ContractLikeInput } from './condition-hash';
3
+ export { parseCaip19SolanaAssetId, type ParsedSolanaAssetId } from './caip19';
4
+ export { buildCreateLockIxData, computeCreateLockDiscriminator, CREATE_LOCK_DISCRIMINATOR, type CreateLockArgs, } from './create-lock';
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Derive the on-chain `lock_id` (bytes32) from a wire-form delegation id.
3
+ *
4
+ * Decision baked into the escrow design: `lock_id = sha256("arp-lock-v1"
5
+ * || delegation_id_bytes16)`. Both parties can compute this without a
6
+ * round-trip:
7
+ *
8
+ * - Payer derives it before submitting the offer (needs it to build
9
+ * `create_lock` accounts: lock PDA, vault PDA, vault_auth PDA, all
10
+ * seeded by `[<seed_prefix>, lock_id]`).
11
+ * - Server derives it independently when validating the offer's
12
+ * `attachments.escrow_lock.lock_id` claim.
13
+ * - Payee derives it before cosigning the receipt (needs it to look up
14
+ * the lock account and to feed into the settlement digest).
15
+ *
16
+ * Versioning: `"arp-lock-v1"` is a permanent literal. A future
17
+ * `arp-lock-v2` would derive different lock_ids for the same delegation,
18
+ * which is the right behaviour if we ever need to migrate seed shapes.
19
+ *
20
+ * @param delegationId — `del_<uuid>` string form (e.g. `del_550e8400-e29b-41d4-a716-446655440000`).
21
+ * The wire-form prefix is part of the hash input
22
+ * so this function takes raw strings, not bytes.
23
+ * BUT we hash the 16-byte UUID binary, NOT the
24
+ * string — see §"ID conversion" of the spec.
25
+ * @returns 32-byte lock_id
26
+ */
27
+ export declare function deriveLockId(delegationId: string): Uint8Array;
28
+ /**
29
+ * Convert a delegation id to the 16-byte representation the Solang
30
+ * contract takes as `bytes16 delegation_id`. RFC 4122 UUIDs are
31
+ * 128 bits = 16 bytes; the string form is 36 chars (32 hex + 4 dashes).
32
+ *
33
+ * Accepts BOTH wire-form variants the protocol uses:
34
+ * - `del_<uuid>` — SDK-canonical form (envelope SDK builders, server
35
+ * `LockValidatorService`, the wallet command's
36
+ * internal id pipeline).
37
+ * - `<uuid>` bare — wire form for `body.content.delegation_id` (the
38
+ * ARP envelope canonical layout). All CLI / API
39
+ * callers pass bare here.
40
+ *
41
+ * Either way, the same 16-byte representation comes out (UUID body is
42
+ * the lock-id substrate; the `del_` prefix is a wrapper that exists
43
+ * only so the wire shape is unambiguously a delegation reference vs.
44
+ * a contract / receipt id elsewhere in the canonical JSON). Pushing
45
+ * this normalization down into the SDK gives every caller — CLI,
46
+ * server, future TypeScript clients — a single source of truth.
47
+ *
48
+ * Failure modes:
49
+ * - UUID body doesn't match the canonical lowercase regex → throws
50
+ * `Invalid delegation_id UUID` (canonical wire form is lowercase
51
+ * per RFC 4122 §3; mixing cases would map two distinct strings
52
+ * to the same on-chain `lock_id` — collision footgun).
53
+ * - Empty / nonsense input → throws `Invalid delegation_id format`.
54
+ *
55
+ * Lowercase canonicalization is INTENTIONALLY preserved (not auto-
56
+ * downcased). The wire-format validator at the server already enforces
57
+ * lowercase; the strict check here is the second line of defence so
58
+ * two distinct wire strings cannot collide on the same lock_id.
59
+ */
60
+ export declare function delegationIdToBytes16(delegationId: string): Uint8Array;
61
+ /**
62
+ * Reverse of `delegationIdToBytes16` — render a 16-byte UUID back to
63
+ * the wire form `del_<canonical lowercase uuid>`.
64
+ *
65
+ * Output dash positions (8-4-4-4-12) match RFC 4122 §3 canonical form.
66
+ */
67
+ export declare function bytes16ToDelegationId(bytes: Uint8Array): string;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * `@heyanon-arp/sdk` — Reference TypeScript implementation of the
3
+ * Agent Relationship Protocol (ARP) wire format.
4
+ *
5
+ * Module map:
6
+ * - canonical — RFC 8785 JCS, sha256 helpers, signing-input bytes
7
+ * - keys — Ed25519 generate/sign/verify, base58btc encoding
8
+ * - did — `did:arp:<...>` parse/format + DID document types
9
+ * - envelope — sign / verify envelope (steps 4-6 of protocol verification)
10
+ * - cosignature — receipt + dispute-response co-signatures
11
+ * - challenge — ARP-CHALLENGE-v1 ownership proof (registration / rotation)
12
+ * - webhook — ARP-WEBHOOK-v1 HMAC header (outbox delivery)
13
+ * - attestation — scrypt key-link + key-rotation attestations
14
+ * - server-chain — signed_message_hash, server_event_hash, audit walker
15
+ * - settlement — ARP-SOLANA-* digest stubs (V1.5)
16
+ * - purpose — domain separators (`ARP-*-v1`)
17
+ * - utils — uuid v4, sender_nonce, RFC 3339, expires_at
18
+ * - types — wire-level TypeScript types (Envelope, body union, identity)
19
+ *
20
+ * Server-side concerns (replay defence, time window, chain assignment,
21
+ * inbox policy) are NOT in this SDK — they are application-layer
22
+ * responsibilities of the backend.
23
+ */
24
+ export * from './canonical';
25
+ export * from './keys';
26
+ export * from './did';
27
+ export * from './envelope';
28
+ export * from './cosignature';
29
+ export * from './challenge';
30
+ export * from './webhook';
31
+ export * from './attestation';
32
+ export * from './server-chain';
33
+ export * from './settlement';
34
+ export * from './purpose';
35
+ export * from './utils';
36
+ export * from './types';
37
+ export * from './assets';
38
+ export * from './escrow';