@pollar/core 0.4.1 → 0.4.3
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 +205 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
# @pollar/core
|
|
2
|
+
|
|
3
|
+
Core SDK for [Pollar](https://pollar.xyz) — authentication and transaction utilities for Stellar-based applications.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @pollar/core
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @pollar/core
|
|
11
|
+
# or
|
|
12
|
+
yarn add @pollar/core
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Overview
|
|
16
|
+
|
|
17
|
+
`@pollar/core` provides the `PollarClient` class and utilities to:
|
|
18
|
+
|
|
19
|
+
- Authenticate users via **Google**, **GitHub**, **Email (OTP)**, or **Stellar wallets** (Freighter, Albedo)
|
|
20
|
+
- Build and submit Stellar transactions
|
|
21
|
+
- Fetch Stellar account balances
|
|
22
|
+
- React to real-time authentication state changes
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { PollarClient } from '@pollar/core';
|
|
28
|
+
|
|
29
|
+
const client = new PollarClient({ apiKey: 'your-api-key' });
|
|
30
|
+
|
|
31
|
+
// Listen to state changes
|
|
32
|
+
client.onStateChange((entry) => {
|
|
33
|
+
console.log(entry.var, entry.code, entry.status);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Login with email
|
|
37
|
+
const { cancelLogin } = client.login({ provider: 'email', email: 'user@example.com' });
|
|
38
|
+
|
|
39
|
+
// Verify the OTP code sent to the user
|
|
40
|
+
await client.verifyEmailCode(clientSessionId, '123456');
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## API Reference
|
|
44
|
+
|
|
45
|
+
### `new PollarClient(config)`
|
|
46
|
+
|
|
47
|
+
| Option | Type | Required | Description |
|
|
48
|
+
| --------------- | -------- | -------- | -------------------------------------------------- |
|
|
49
|
+
| `apiKey` | `string` | Yes | Your Pollar API key |
|
|
50
|
+
| `baseUrl` | `string` | No | Override the default API endpoint |
|
|
51
|
+
| `stellarNetwork`| `'mainnet' \| 'testnet'` | No | Target Stellar network (default: `testnet`) |
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
### Authentication
|
|
56
|
+
|
|
57
|
+
#### `client.login(options)`
|
|
58
|
+
|
|
59
|
+
Initiates a login flow. Returns `{ cancelLogin }` to abort the flow at any point.
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
// Social providers
|
|
63
|
+
client.login({ provider: 'google' });
|
|
64
|
+
client.login({ provider: 'github' });
|
|
65
|
+
|
|
66
|
+
// Email OTP
|
|
67
|
+
client.login({ provider: 'email', email: 'user@example.com' });
|
|
68
|
+
|
|
69
|
+
// Stellar wallet
|
|
70
|
+
import { WalletType } from '@pollar/core';
|
|
71
|
+
client.login({ provider: 'wallet', type: WalletType.FREIGHTER });
|
|
72
|
+
client.login({ provider: 'wallet', type: WalletType.ALBEDO });
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
#### `client.verifyEmailCode(clientSessionId, code)`
|
|
76
|
+
|
|
77
|
+
Submits the OTP code for email authentication. Use `clientSessionId` received from the `EMAIL_AUTH_START_SUCCESS` state event.
|
|
78
|
+
|
|
79
|
+
#### `client.logout()`
|
|
80
|
+
|
|
81
|
+
Clears the current session from memory and storage.
|
|
82
|
+
|
|
83
|
+
#### `client.isAuthenticated()`
|
|
84
|
+
|
|
85
|
+
Returns `true` if a valid session with a wallet public key is present.
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
### Transactions
|
|
90
|
+
|
|
91
|
+
#### `client.buildTx(operation, params, options?)`
|
|
92
|
+
|
|
93
|
+
Builds a Stellar transaction via the Pollar API.
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
await client.buildTx('payment', {
|
|
97
|
+
destination: 'G...',
|
|
98
|
+
amount: '10',
|
|
99
|
+
asset: 'XLM',
|
|
100
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
#### `client.submitTx(signedXdr)`
|
|
104
|
+
|
|
105
|
+
Submits a signed XDR transaction to the network.
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
await client.submitTx(signedXdr);
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
### State
|
|
114
|
+
|
|
115
|
+
#### `client.onStateChange(callback)`
|
|
116
|
+
|
|
117
|
+
Subscribe to state changes. Returns an unsubscribe function.
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
const unsubscribe = client.onStateChange((entry) => {
|
|
121
|
+
// entry.var — 'authentication' | 'transaction' | 'network'
|
|
122
|
+
// entry.code — granular event code (see STATE_VAR_CODES)
|
|
123
|
+
// entry.status — 'NONE' | 'LOADING' | 'SUCCESS' | 'ERROR'
|
|
124
|
+
// entry.data — optional payload
|
|
125
|
+
// entry.level — 'info' | 'warn' | 'error'
|
|
126
|
+
// entry.ts — timestamp (ms)
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// Unsubscribe
|
|
130
|
+
unsubscribe();
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
All state codes are available via `STATE_VAR_CODES`:
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
import { STATE_VAR_CODES } from '@pollar/core';
|
|
137
|
+
|
|
138
|
+
// STATE_VAR_CODES.authentication.EMAIL_AUTH_START_SUCCESS
|
|
139
|
+
// STATE_VAR_CODES.transaction.BUILD_TRANSACTION_SUCCESS
|
|
140
|
+
// STATE_VAR_CODES.network.NETWORK_UPDATED
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
### `StellarClient`
|
|
146
|
+
|
|
147
|
+
Lightweight client to query Stellar account balances via Horizon.
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
import { StellarClient } from '@pollar/core';
|
|
151
|
+
|
|
152
|
+
const stellar = new StellarClient('testnet');
|
|
153
|
+
// or: new StellarClient({ horizonUrl: 'https://horizon.stellar.org' })
|
|
154
|
+
|
|
155
|
+
const result = await stellar.getBalances('GABC...');
|
|
156
|
+
|
|
157
|
+
if (result.success) {
|
|
158
|
+
console.log(result.balances);
|
|
159
|
+
// [{ asset: 'XLM', balance: '100.0000000' }, ...]
|
|
160
|
+
} else {
|
|
161
|
+
console.error(result.errorCode); // 'ACCOUNT_NOT_FOUND' | 'HORIZON_ERROR' | 'NETWORK_ERROR'
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
---
|
|
166
|
+
|
|
167
|
+
### Wallet Adapters
|
|
168
|
+
|
|
169
|
+
For direct wallet interaction outside the login flow:
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
import { FreighterAdapter, AlbedoAdapter } from '@pollar/core';
|
|
173
|
+
|
|
174
|
+
const adapter = new FreighterAdapter();
|
|
175
|
+
const available = await adapter.isAvailable();
|
|
176
|
+
if (available) {
|
|
177
|
+
const { publicKey } = await adapter.connect();
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## TypeScript
|
|
182
|
+
|
|
183
|
+
`@pollar/core` is written in TypeScript and ships full type declarations.
|
|
184
|
+
|
|
185
|
+
Key exported types:
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
import type {
|
|
189
|
+
PollarClientConfig,
|
|
190
|
+
PollarLoginOptions,
|
|
191
|
+
PollarState,
|
|
192
|
+
PollarStateEntry,
|
|
193
|
+
StateAuthenticationCodes,
|
|
194
|
+
StateTransactionCodes,
|
|
195
|
+
StateNetworkCodes,
|
|
196
|
+
StateVarCodes,
|
|
197
|
+
StellarNetwork,
|
|
198
|
+
StellarBalance,
|
|
199
|
+
GetBalancesResult,
|
|
200
|
+
} from '@pollar/core';
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## License
|
|
204
|
+
|
|
205
|
+
MIT
|
package/dist/index.d.mts
CHANGED
|
@@ -1355,6 +1355,7 @@ declare const STATE_VAR_CODES: {
|
|
|
1355
1355
|
readonly FETCH_SESSION_START: "FETCH_SESSION_START";
|
|
1356
1356
|
readonly FETCH_SESSION_SUCCESS: "FETCH_SESSION_SUCCESS";
|
|
1357
1357
|
readonly FETCH_SESSION_ERROR: "FETCH_SESSION_ERROR";
|
|
1358
|
+
readonly NO_RESTORED_SESSION: "NO_RESTORED_SESSION";
|
|
1358
1359
|
readonly RESTORED_SESSION_SUCCESS: "RESTORED_SESSION_SUCCESS";
|
|
1359
1360
|
readonly RESTORED_SESSION_ERROR: "RESTORED_SESSION_ERROR";
|
|
1360
1361
|
readonly SESSION_STORED: "SESSION_STORED";
|
package/dist/index.d.ts
CHANGED
|
@@ -1355,6 +1355,7 @@ declare const STATE_VAR_CODES: {
|
|
|
1355
1355
|
readonly FETCH_SESSION_START: "FETCH_SESSION_START";
|
|
1356
1356
|
readonly FETCH_SESSION_SUCCESS: "FETCH_SESSION_SUCCESS";
|
|
1357
1357
|
readonly FETCH_SESSION_ERROR: "FETCH_SESSION_ERROR";
|
|
1358
|
+
readonly NO_RESTORED_SESSION: "NO_RESTORED_SESSION";
|
|
1358
1359
|
readonly RESTORED_SESSION_SUCCESS: "RESTORED_SESSION_SUCCESS";
|
|
1359
1360
|
readonly RESTORED_SESSION_ERROR: "RESTORED_SESSION_ERROR";
|
|
1360
1361
|
readonly SESSION_STORED: "SESSION_STORED";
|
package/dist/index.js
CHANGED
|
@@ -690,6 +690,7 @@ var STATE_VAR_CODES = {
|
|
|
690
690
|
FETCH_SESSION_START: "FETCH_SESSION_START",
|
|
691
691
|
FETCH_SESSION_SUCCESS: "FETCH_SESSION_SUCCESS",
|
|
692
692
|
FETCH_SESSION_ERROR: "FETCH_SESSION_ERROR",
|
|
693
|
+
NO_RESTORED_SESSION: "NO_RESTORED_SESSION",
|
|
693
694
|
RESTORED_SESSION_SUCCESS: "RESTORED_SESSION_SUCCESS",
|
|
694
695
|
RESTORED_SESSION_ERROR: "RESTORED_SESSION_ERROR",
|
|
695
696
|
SESSION_STORED: "SESSION_STORED",
|
|
@@ -1455,7 +1456,7 @@ var PollarClient = class {
|
|
|
1455
1456
|
);
|
|
1456
1457
|
console.info("[PollarClient] Session restored from storage");
|
|
1457
1458
|
} else {
|
|
1458
|
-
this._emitState("authentication", STATE_VAR_CODES.authentication.
|
|
1459
|
+
this._emitState("authentication", STATE_VAR_CODES.authentication.NO_RESTORED_SESSION, "info", StateStatus.NONE);
|
|
1459
1460
|
console.info("[PollarClient] Session NO restored from storage");
|
|
1460
1461
|
}
|
|
1461
1462
|
}
|