# @provenanceauth/rails/ap2

@provenanceauth/rails/ap2 in the Rails: 17 methods. AP2 (Agent Payments Protocol) mapping + verification.

## Methods

### [ap2Hash](https://provenanceauth.com/docs/sdks/rails/ap2/ap2-hash.md)

```ts
ap2Hash(value: string, sdAlg?: string): string
```

Base64url(hash(ascii value)), the way AP2 and SD-JWT hash strings.

### [ap2CheckoutHash](https://provenanceauth.com/docs/sdks/rails/ap2/ap2-checkout-hash.md)

```ts
ap2CheckoutHash(checkoutJwt: string, sdAlg?: string): string
```

The `checkout_hash` (and Payment Mandate `transaction_id`) of a merchant-signed Checkout JWT.

### [decodeAp2SdJwt](https://provenanceauth.com/docs/sdks/rails/ap2/decode-ap2-sd-jwt.md)

```ts
decodeAp2SdJwt(serialized: string): Ap2SdJwt[]
```

Parse an SD-JWT, or a `~~`-joined chain of them as AP2's delegate examples print, into its parts, and resolve the disclosed content.

### [ap2MandatesOf](https://provenanceauth.com/docs/sdks/rails/ap2/ap2-mandates-of.md)

```ts
ap2MandatesOf(sdJwt: Ap2SdJwt): Array<Record<string, unknown>>
```

The mandate content an SD-JWT discloses in `delegate_payload` (AP2's delegate format).

### [provenanceMandateToAp2OpenPayment](https://provenanceauth.com/docs/sdks/rails/ap2/provenance-mandate-to-ap2-open-payment.md)

```ts
provenanceMandateToAp2OpenPayment(m: ProvenanceMandateView, opts: Ap2OpenPaymentOptions): { mandate: Ap2OpenPaymentMandate; not_mapped: string[] }
```

A Provenance mandate → open Payment Mandate CONTENT (`mandate.payment.open.1`), unsigned: the Shopping Agent hands it to its Trusted Surface to sign.

### [ap2OpenPaymentToProvenance](https://provenanceauth.com/docs/sdks/rails/ap2/ap2-open-payment-to-provenance.md)

```ts
ap2OpenPaymentToProvenance(content: Ap2OpenPaymentMandate): { constraints: ProvenanceMandateConstraints; currency: string | null; mandate_token: string | null; checkout_reference: string | null; unresolved: string[]; }
```

Open Payment Mandate content → the Provenance mandate fields it expresses.

### [ap2BindingPayload](https://provenanceauth.com/docs/sdks/rails/ap2/ap2-binding-payload.md)

```ts
ap2BindingPayload(input: { checkout_jwt?: string; checkout_hash?: string; payee: Pick<Ap2Merchant, "id"> | string; payment_amount: Ap2Amount; }): Ap2Binding
```

The exact object to pass as `action_payload` to `POST /v1/mandate/intents`, so the token binds this Checkout (by hash), the payee and the amount.

### [ap2BindingHash](https://provenanceauth.com/docs/sdks/rails/ap2/ap2-binding-hash.md)

```ts
ap2BindingHash(b: Ap2Binding): Promise<string>
```

Sha256(canonical JSON) of the binding: the value a bound token carries as `payload_hash`.

### [ap2CheckoutMandateContent](https://provenanceauth.com/docs/sdks/rails/ap2/ap2-checkout-mandate-content.md)

```ts
ap2CheckoutMandateContent(checkoutJwt: string, intentToken: string, opts?: { jwksUrl?: string; iat?: number; exp?: number }): Ap2CheckoutMandate
```

Closed Checkout Mandate CONTENT (`mandate.checkout.1`) for a merchant-signed Checkout JWT, with the intent token in.

### [ap2PaymentMandateContent](https://provenanceauth.com/docs/sdks/rails/ap2/ap2-payment-mandate-content.md)

```ts
ap2PaymentMandateContent(input: { checkout_jwt?: string; checkout_hash?: string; payee: Ap2Merchant; payment_amount: Ap2Amount; payment_instrument: Ap2PaymentInstrument; execution_date?: string; }, intentToken: string, opts?: { jwksUrl?: string; mandateToken?: string; iat?: number; exp?: number }): Ap2PaymentMandate
```

Closed Payment Mandate CONTENT (`mandate.payment.1`) for the same Checkout, with the intent token in.

### [ap2ContentForCart](https://provenanceauth.com/docs/sdks/rails/ap2/ap2-content-for-cart.md)

```ts
ap2ContentForCart(order: { total: number; currency: string; merchant_id: string; origin?: string }, opts: { checkoutJwt?: string | null; merchantName?: string; intentToken?: string; jwksUrl?: string }): { content: Ap2CartContent; reason?: undefined } | { content: null; reason: "checkout_jwt_missing" | "checkout_jwt_malformed" }
```

Goal 7: AP2 checkout content for a store's cart, for an AP2 agent that arrived without a Checkout Mandate (or with one that doesn't cover this cart).

### [verifyAp2Mandate](https://provenanceauth.com/docs/sdks/rails/ap2/verify-ap2-mandate.md)

```ts
verifyAp2Mandate(closed: Ap2ClosedMandate | Record<string, unknown>, opts: Ap2VerifyOptions): Promise<Ap2VerifyResult>
```

Verify a closed AP2 Checkout or Payment Mandate (its content, as your AP2 stack returns it after checking the SD-JWT) against the Provenance intent token it carries: 1.

### [ap2ReceiptClaims](https://provenanceauth.com/docs/sdks/rails/ap2/ap2-receipt-claims.md)

```ts
ap2ReceiptClaims(result: Ap2VerifyResult, opts: { iss: string; reference: string; iat?: number; order_id?: string; payment_id?: string; psp_confirmation_id?: string; network_confirmation_id?: string; }): Record<string, unknown>
```

The claims of the Checkout or Payment Receipt a verifier returns (AP2 `checkout_receipt.json` / `payment_receipt.json`).

### [verifyAp2AppKeyAttestation](https://provenanceauth.com/docs/sdks/rails/ap2/verify-ap2-app-key-attestation.md)

```ts
verifyAp2AppKeyAttestation(jws: string, issuers: Ap2UserMandateOptions["issuers"], now?: number): | { ok: true; issuer: string; jwk: Record<string, unknown>; sub: string | null; approval_id: string | null; exp: number | null } | { ok: false; reason: "issuer_not_trusted" | "signature_invalid" | "expired"; detail: string }
```

Verify an app-key attestation (`com.provenanceauth.app_key`) against the listed issuers: ES256, `typ` `ap2-app-key+jwt`, a key of a listed issuer, unexpired, with a P-256 `jwk`.

### [verifyAp2UserMandate](https://provenanceauth.com/docs/sdks/rails/ap2/verify-ap2-user-mandate.md)

```ts
verifyAp2UserMandate(serialized: string, opts: Ap2UserMandateOptions): Ap2UserMandateResult
```

Verify a closed AP2 0.2 Checkout or Payment Mandate SD-JWT that the person signed on their own trusted surface (a wallet), as a confirmation of the verifier's cart: 1.

### [closeAp2OpenMandate](https://provenanceauth.com/docs/sdks/rails/ap2/close-ap2-open-mandate.md)

```ts
closeAp2OpenMandate(openSdJwt: string, input: { holderKey: { kty?: string; crv?: string; x: string; y: string; d: string }; content: Record<string, unknown>; aud: string; nonce?: string; iat?: number; }): Promise<string>
```

Slice 2: the agent closes an open mandate for one transaction (AP2's Human Not Present mode): an SD-JWT signed with the agent's private key (the open mandate's `cnf.jwk`), disclosing the closed mandate, naming the verifier's origin (`aud`) and binding the open SD-JWT by `sd_hash`.

### [verifyAp2UserMandateOnline](https://provenanceauth.com/docs/sdks/rails/ap2/verify-ap2-user-mandate-online.md)

```ts
verifyAp2UserMandateOnline(serialized: string, opts: Ap2UserMandateOptions & { onlineStatus: (jti: string, token: string) => Promise<{ revoked: boolean; reason?: string }> }): Promise<Ap2UserMandateResult>
```

Then the revocation of the intent token the mandate carries (`com.provenanceauth.intent`), through your online status check (e.g.
