nirium-pollar-adapter 0.1.0 → 0.2.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/README.md CHANGED
@@ -8,7 +8,7 @@ Both surfaces are **software-only and non-custodial**, and both are **live on St
8
8
 
9
9
  ```bash
10
10
  npm install nirium-pollar-adapter
11
- # peer: @pollar/core
11
+ # optional peer, only for createPollarSigner: @pollar/core >= 0.11
12
12
  ```
13
13
 
14
14
  ## Quickstart
@@ -34,9 +34,18 @@ const data = await res.json();
34
34
 
35
35
  The user pays **$0.02 USDC** and signs nothing else — network fees are sponsored by the x402 facilitator, so the wallet does not need XLM for gas.
36
36
 
37
- ### Status: waiting on one Pollar method
37
+ ### Requires @pollar/core 0.11+
38
38
 
39
- `createPollarSigner` needs `signAuthEntry` on `PollarClient`. Pollar already declares it on its `WalletAdapter` interface (Freighter and Albedo implement it), but it is not yet exposed on the client for embedded/passkey wallets. Until it ships, `createPollarSigner` throws an explanatory error and you can pass any other SEP-43 signer.
39
+ `createPollarSigner` calls `PollarClient.signAuthEntry`, available from `@pollar/core` 0.11. On older versions it throws an explanatory error, and you can still pass any other SEP-43 signer.
40
+
41
+ The two sides speak different dialects and the adapter translates between them: x402 expects SEP-43 (`signAuthEntry(xdr, { networkPassphrase, address }) → { signedAuthEntry }`), Pollar expects `signAuthEntry(xdr, { validUntilLedger }) → { status, … }`. The adapter reads the network's latest ledger, sets the expiry, and normalises the outcome — you do not deal with any of it.
42
+
43
+ ```ts
44
+ const signer = createPollarSigner(pollar, {
45
+ network: "stellar:pubnet",
46
+ validityLedgers: 120, // ~10 min, the default
47
+ });
48
+ ```
40
49
 
41
50
  Check at runtime before enabling a pay button:
42
51
 
package/dist/index.d.ts CHANGED
@@ -9,6 +9,17 @@ export interface ClientStellarSigner {
9
9
  signAuthEntry: SignAuthEntry;
10
10
  signTransaction?: SignTransaction;
11
11
  }
12
+ /** Lo que devuelve `PollarClient.signAuthEntry` (@pollar/core ≥ 0.11). */
13
+ export type PollarSignAuthEntryOutcome = {
14
+ status: "signed";
15
+ signedAuthEntry: string;
16
+ } | {
17
+ status: "error";
18
+ details?: string;
19
+ } | {
20
+ signedAuthEntry: string;
21
+ signerAddress?: string;
22
+ };
12
23
  /**
13
24
  * Superficie mínima que necesitamos del cliente de Pollar. Se declara por
14
25
  * duck-typing a propósito: @pollar/core sigue moviéndose y no queremos atarnos
@@ -17,24 +28,38 @@ export interface ClientStellarSigner {
17
28
  export interface PollarLikeClient {
18
29
  address?: string;
19
30
  walletAddress?: string;
20
- getAuthState?: () => {
21
- walletAddress?: string;
22
- address?: string;
23
- } | undefined;
24
- /** SEP-43. Aún no expuesto públicamente por Pollar — ver nota abajo. */
25
- signAuthEntry?: SignAuthEntry;
31
+ getAuthState?: () => any;
32
+ /**
33
+ * OJO: la firma de Pollar NO es la de SEP-43. Pide `validUntilLedger` (en vez
34
+ * de networkPassphrase/address) y responde un outcome con `status`.
35
+ */
36
+ signAuthEntry?: (entryXdr: string, options: {
37
+ validUntilLedger: number;
38
+ }) => Promise<PollarSignAuthEntryOutcome>;
26
39
  signTransaction?: SignTransaction;
27
40
  signTx?: (unsignedXdr: string) => Promise<unknown>;
28
41
  }
29
42
  export interface PollarSignerOptions {
30
43
  /** Fuerza la dirección del pagador si el cliente aún no la expone. */
31
44
  address?: string;
45
+ /** Red usada para leer el último ledger. Default: stellar:testnet. */
46
+ network?: StellarNetwork;
47
+ /** Soroban RPC para leer el último ledger. Default: el público de la red. */
48
+ rpcUrl?: string;
49
+ /**
50
+ * Ledgers que la firma sigue siendo válida. Default 120 (~10 min a 5 s por
51
+ * ledger) — con margen para la latencia del facilitador sin dejar una firma
52
+ * viva de más.
53
+ */
54
+ validityLedgers?: number;
32
55
  }
33
56
  /**
34
57
  * Envuelve un cliente de Pollar como signer SEP-43 para x402.
35
58
  *
36
- * El día que Pollar exponga `signAuthEntry` esto funciona sin cambios: la
37
- * detección es en tiempo de ejecución, no en tiempo de compilación.
59
+ * Traduce entre los dos contratos: x402 pide SEP-43
60
+ * (`signAuthEntry(xdr, { networkPassphrase, address }) → { signedAuthEntry }`),
61
+ * y Pollar pide `signAuthEntry(xdr, { validUntilLedger }) → { status, … }`.
62
+ * El `validUntilLedger` se calcula leyendo el último ledger de la red.
38
63
  */
39
64
  export declare function createPollarSigner(pollar: PollarLikeClient, opts?: PollarSignerOptions): ClientStellarSigner;
40
65
  /** ¿Este signer ya puede pagar? Útil para deshabilitar el botón en la UI. */
package/dist/index.js CHANGED
@@ -30,6 +30,7 @@
30
30
  // ───────────────────────────────────────────────────────────────────────────
31
31
  import { x402Client as X402ClientClass, wrapFetchWithPayment } from "@x402/fetch";
32
32
  import { ExactStellarScheme } from "@x402/stellar/exact/client";
33
+ import { rpc } from "@stellar/stellar-sdk";
33
34
  const DEFAULTS = {
34
35
  network: "stellar:testnet",
35
36
  testnetAgent: "https://nirium-agent.fly.dev",
@@ -39,25 +40,29 @@ const DEFAULTS = {
39
40
  pubnetRpc: "https://soroban-rpc.mainnet.stellar.gateway.fm",
40
41
  };
41
42
  const isPubnet = (n) => n === "stellar:pubnet";
42
- const MISSING_SIGN_AUTH_ENTRY = "El cliente de Pollar no expone signAuthEntry(). x402 necesita firmar un auth " +
43
- "entry de Soroban (SEP-43: signAuthEntry(authEntryXdr, { networkPassphrase, address })), " +
44
- "no una transacción completa — signTx() no sirve para esto.\n\n" +
45
- "Pollar YA firma auth entries por dentro para cuentas C (passkey); falta " +
46
- "exponerlo en la API pública, igual que hace Freighter. Pedido abierto al " +
47
- "equipo de Pollar. Mientras tanto puedes pasar tu propio signer a " +
48
- "createNiriumAdapter({ signer }).";
43
+ const MISSING_SIGN_AUTH_ENTRY = "El cliente de Pollar no expone signAuthEntry(). Está disponible desde " +
44
+ "@pollar/core 0.11 — actualiza el paquete. Mientras tanto puedes pasar tu " +
45
+ "propio signer a createNiriumAdapter({ signer }).";
49
46
  /**
50
47
  * Envuelve un cliente de Pollar como signer SEP-43 para x402.
51
48
  *
52
- * El día que Pollar exponga `signAuthEntry` esto funciona sin cambios: la
53
- * detección es en tiempo de ejecución, no en tiempo de compilación.
49
+ * Traduce entre los dos contratos: x402 pide SEP-43
50
+ * (`signAuthEntry(xdr, { networkPassphrase, address }) → { signedAuthEntry }`),
51
+ * y Pollar pide `signAuthEntry(xdr, { validUntilLedger }) → { status, … }`.
52
+ * El `validUntilLedger` se calcula leyendo el último ledger de la red.
54
53
  */
55
54
  export function createPollarSigner(pollar, opts = {}) {
55
+ const network = opts.network ?? DEFAULTS.network;
56
+ const rpcUrl = opts.rpcUrl ?? (isPubnet(network) ? DEFAULTS.pubnetRpc : DEFAULTS.testnetRpc);
57
+ const validityLedgers = opts.validityLedgers ?? 120;
58
+ let rpcServer = null;
56
59
  const resolveAddress = () => {
57
60
  const authState = pollar.getAuthState?.();
58
61
  const address = opts.address ??
59
62
  pollar.address ??
60
63
  pollar.walletAddress ??
64
+ // @pollar/core ≥ 0.11: la wallet vive en la sesión autenticada.
65
+ authState?.session?.wallet?.address ??
61
66
  authState?.walletAddress ??
62
67
  authState?.address;
63
68
  if (!address) {
@@ -66,14 +71,24 @@ export function createPollarSigner(pollar, opts = {}) {
66
71
  }
67
72
  return address;
68
73
  };
69
- const signAuthEntry = async (authEntry, signOpts) => {
74
+ const signAuthEntry = async (authEntry) => {
70
75
  if (typeof pollar.signAuthEntry !== "function") {
71
76
  throw new Error(MISSING_SIGN_AUTH_ENTRY);
72
77
  }
73
- return pollar.signAuthEntry(authEntry, {
74
- address: resolveAddress(),
75
- ...signOpts,
78
+ const signerAddress = resolveAddress();
79
+ rpcServer ??= new rpc.Server(rpcUrl);
80
+ const { sequence } = await rpcServer.getLatestLedger();
81
+ const outcome = await pollar.signAuthEntry(authEntry, {
82
+ validUntilLedger: sequence + validityLedgers,
76
83
  });
84
+ if ("status" in outcome && outcome.status === "error") {
85
+ throw new Error(`Pollar rechazó la firma del auth entry: ${outcome.details ?? "sin detalle"}`);
86
+ }
87
+ const signedAuthEntry = outcome.signedAuthEntry;
88
+ if (!signedAuthEntry) {
89
+ throw new Error("Pollar no devolvió signedAuthEntry.");
90
+ }
91
+ return { signedAuthEntry, signerAddress };
77
92
  };
78
93
  const signer = {
79
94
  get address() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nirium-pollar-adapter",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Nirium adapter for the Pollar SDK — x402 pay-per-request and IPFS audit anchoring using the wallet a user onboarded through Pollar. Signs SEP-43 auth entries; the facilitator sponsors network fees.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -17,7 +17,10 @@
17
17
  "@x402/stellar": "2.17.0"
18
18
  },
19
19
  "peerDependencies": {
20
- "@pollar/core": "*"
20
+ "@pollar/core": ">=0.11.0"
21
+ },
22
+ "peerDependenciesMeta": {
23
+ "@pollar/core": { "optional": true }
21
24
  },
22
25
  "keywords": ["nirium", "pollar", "stellar", "soroban", "x402", "adapter"],
23
26
  "license": "MIT"