@yourself_id/siwys-api-js 0.36.0 → 0.36.1-jahatch512-docs-keymaster-init.49.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 +253 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
# @yourself_id/siwys-api-js
|
|
2
|
+
|
|
3
|
+
Node.js backend SDK for **Sign In With YourSelf (SIWYS)** and the **Multi-Dimensional Identity Protocol (MDIP)**.
|
|
4
|
+
|
|
5
|
+
This package provides the tools required for server-side operations, including:
|
|
6
|
+
- Creating authentication challenges and callbacks for the SELF Mobile Wallet
|
|
7
|
+
- Verifying challenge responses submitted by users
|
|
8
|
+
- Issuing, binding, and publishing Verifiable Credentials
|
|
9
|
+
- Managing wallets and Decentralized Identifiers (DIDs)
|
|
10
|
+
- Communicating with Gatekeeper and Keymaster nodes
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @yourself_id/siwys-api-js
|
|
18
|
+
# or
|
|
19
|
+
yarn add @yourself_id/siwys-api-js
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## Two Integration Pathways
|
|
25
|
+
|
|
26
|
+
You can use this SDK in one of two modes depending on your architecture:
|
|
27
|
+
|
|
28
|
+
| Pathway | Class | Best For | Architecture |
|
|
29
|
+
|---|---|---|---|
|
|
30
|
+
| **Embedded Keymaster** | `Keymaster` | Node.js / TypeScript backends | Runs Keymaster in-process, saves encrypted wallet state via pluggable DB adapter. |
|
|
31
|
+
| **External Keymaster Client** | `KeymasterExternalClient` | Polyglot / Microservices | Connects over HTTP REST to a standalone Keymaster container (e.g. from `KeychainMDIP/kc`). |
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Pathway 1: Embedded Keymaster (`Keymaster`)
|
|
36
|
+
|
|
37
|
+
Runs the Keymaster service in-process within your Node.js application.
|
|
38
|
+
|
|
39
|
+
### 1. Configure Wallet Persistence
|
|
40
|
+
|
|
41
|
+
Keymaster requires a persistence adapter (`WalletBase`) to store the encrypted wallet. Here is an example filesystem adapter:
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
// services/wallet.ts
|
|
45
|
+
import fs from "fs";
|
|
46
|
+
import type { StoredWallet } from "@yourself_id/siwys-api-js";
|
|
47
|
+
|
|
48
|
+
const WALLET_FILE = "./data/wallet.json";
|
|
49
|
+
|
|
50
|
+
export async function saveWallet(wallet: StoredWallet, overwrite = false): Promise<boolean> {
|
|
51
|
+
if (fs.existsSync(WALLET_FILE) && !overwrite) return true;
|
|
52
|
+
fs.mkdirSync("./data", { recursive: true });
|
|
53
|
+
fs.writeFileSync(WALLET_FILE, JSON.stringify(wallet, null, 2));
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export async function loadWallet(): Promise<StoredWallet | null> {
|
|
58
|
+
if (fs.existsSync(WALLET_FILE)) {
|
|
59
|
+
return JSON.parse(fs.readFileSync(WALLET_FILE, "utf-8"));
|
|
60
|
+
}
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function updateWallet(
|
|
65
|
+
mutator: (wallet: StoredWallet) => void | Promise<void>
|
|
66
|
+
): Promise<void> {
|
|
67
|
+
const wallet = await loadWallet();
|
|
68
|
+
if (!wallet) throw new Error("No wallet found to update");
|
|
69
|
+
const before = JSON.stringify(wallet);
|
|
70
|
+
await mutator(wallet);
|
|
71
|
+
if (before !== JSON.stringify(wallet)) {
|
|
72
|
+
await saveWallet(wallet, true);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### 2. Initialize and Start Keymaster
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
// server.ts
|
|
81
|
+
import { Keymaster } from "@yourself_id/siwys-api-js";
|
|
82
|
+
import { saveWallet, loadWallet, updateWallet } from "./services/wallet.js";
|
|
83
|
+
|
|
84
|
+
async function setupKeymaster() {
|
|
85
|
+
Keymaster.initialize({
|
|
86
|
+
gatekeeperConfig: {
|
|
87
|
+
url: process.env.GATEKEEPER_URL || "http://localhost:4422",
|
|
88
|
+
waitUntilReady: true,
|
|
89
|
+
},
|
|
90
|
+
walletConfig: {
|
|
91
|
+
id: "company-node-id",
|
|
92
|
+
registry: "hyperswarm",
|
|
93
|
+
mnemonic: process.env.WALLET_MNEMONIC, // optional: recovers wallet if initializing fresh
|
|
94
|
+
},
|
|
95
|
+
walletDb: {
|
|
96
|
+
saveWallet,
|
|
97
|
+
loadWallet,
|
|
98
|
+
updateWallet,
|
|
99
|
+
},
|
|
100
|
+
passphrase: process.env.KEYMASTER_PASSPHRASE || "a-secure-passphrase",
|
|
101
|
+
didPrefix: "did:test",
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
await Keymaster.start();
|
|
105
|
+
console.log("Keymaster is ready");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
setupKeymaster();
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
## Pathway 2: External Keymaster (`KeymasterExternalClient`)
|
|
114
|
+
|
|
115
|
+
Use this when you are running a standalone Keymaster container (such as the Docker image from [KeychainMDIP/kc](https://github.com/KeychainMDIP/kc)) on port 4226:
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
import { KeymasterExternalClient } from "@yourself_id/siwys-api-js";
|
|
119
|
+
|
|
120
|
+
const keymasterClient = new KeymasterExternalClient({
|
|
121
|
+
keymasterConfig: {
|
|
122
|
+
url: process.env.KEYMASTER_URL || "http://localhost:4226",
|
|
123
|
+
waitUntilReady: true,
|
|
124
|
+
},
|
|
125
|
+
didPrefix: "did:test",
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
await keymasterClient.start();
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
`KeymasterExternalClient` provides the same core methods (`createChallenge`, `verifyResponse`, `bindCredential`, `issueCredential`, etc.) via HTTP REST.
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
## Core Use Cases
|
|
136
|
+
|
|
137
|
+
### 1. Creating Authentication Challenges
|
|
138
|
+
|
|
139
|
+
When a user visits your sign-in page, create a challenge and return the challenge data and QR code URL to the frontend:
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import { Keymaster } from "@yourself_id/siwys-api-js";
|
|
143
|
+
|
|
144
|
+
app.post("/challenges", async (req, res) => {
|
|
145
|
+
try {
|
|
146
|
+
const challenge = await Keymaster.createChallenge({
|
|
147
|
+
callback: `${process.env.BACKEND_URL}/login`,
|
|
148
|
+
// Optional: require user to present specific credentials
|
|
149
|
+
// credentials: [{ schema: "did:test:schema-id", issuers: ["did:test:issuer-did"] }]
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// Returns: { challenge: "did:test:...", challengeUrl: "https://.../login?challenge=did:test:..." }
|
|
153
|
+
res.json(challenge);
|
|
154
|
+
} catch (err) {
|
|
155
|
+
res.status(500).json({ error: "Failed to generate challenge" });
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### 2. Verifying Authentication Responses
|
|
161
|
+
|
|
162
|
+
The SELF mobile wallet signs and submits the challenge response back to your backend callback URL:
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
app.post("/login", async (req, res) => {
|
|
166
|
+
try {
|
|
167
|
+
const { response } = req.body;
|
|
168
|
+
const verify = await Keymaster.verifyResponse(response);
|
|
169
|
+
|
|
170
|
+
if (verify.match) {
|
|
171
|
+
// Authentication succeeded!
|
|
172
|
+
// verify.responder contains the user's DID
|
|
173
|
+
const userDid = verify.responder;
|
|
174
|
+
|
|
175
|
+
// Issue your application's session cookie or JWT
|
|
176
|
+
const sessionToken = createSessionForUser(userDid);
|
|
177
|
+
|
|
178
|
+
res.json({ authenticated: true, token: sessionToken });
|
|
179
|
+
} else {
|
|
180
|
+
res.status(401).json({ error: "Challenge response verification failed" });
|
|
181
|
+
}
|
|
182
|
+
} catch (err) {
|
|
183
|
+
res.status(500).json({ error: "Exception verifying response" });
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### 3. Issuing Verifiable Credentials to Users
|
|
189
|
+
|
|
190
|
+
Issue a verifiable credential to a user's DID:
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
// 1. Bind credential to user's DID against a defined schema
|
|
194
|
+
const boundCredential = await Keymaster.bindCredential(schemaDid, userDid, {
|
|
195
|
+
validFrom: new Date().toISOString(),
|
|
196
|
+
validUntil: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000).toISOString(),
|
|
197
|
+
credential: {
|
|
198
|
+
membershipTier: "Gold",
|
|
199
|
+
accountNumber: "A-987654",
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// 2. Issue the credential
|
|
204
|
+
const credentialDid = await Keymaster.issueCredential(boundCredential);
|
|
205
|
+
|
|
206
|
+
// 3. Publish so the holder or verifiers can resolve it
|
|
207
|
+
await Keymaster.publishCredential(credentialDid);
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
## API & Configuration Reference
|
|
213
|
+
|
|
214
|
+
### `KeymasterConfig`
|
|
215
|
+
|
|
216
|
+
| Field | Type | Required | Description |
|
|
217
|
+
|---|---|---|---|
|
|
218
|
+
| `gatekeeperConfig` | `SdkConfig` | Yes (embedded) | Configuration for connecting to the Gatekeeper node. |
|
|
219
|
+
| `walletConfig` | `WalletConfig` | Yes | ID and registry name for the default DID, plus optional mnemonic. |
|
|
220
|
+
| `walletDb` | `WalletBase` | Yes (embedded) | `saveWallet`, `loadWallet`, and `updateWallet` persistence hooks. |
|
|
221
|
+
| `passphrase` | `string` | Yes | Secret passphrase used to encrypt and decrypt the wallet at rest. |
|
|
222
|
+
| `didPrefix` | `string` | Optional | DID method prefix (e.g. `did:test` or `did:mdip`). |
|
|
223
|
+
|
|
224
|
+
### `WalletConfig`
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
interface WalletConfig {
|
|
228
|
+
id: string; // Identifier for this node's default DID (e.g. "my-company-id")
|
|
229
|
+
registry: "local" | "hyperswarm" | "TESS" | "TBTC" | "TFTC" | "Signet" | "Signet-Inscription" | "BTC-Inscription";
|
|
230
|
+
mnemonic?: string; // BIP39 mnemonic seed phrase for recovery
|
|
231
|
+
}
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
### `SdkConfig`
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
interface SdkConfig {
|
|
238
|
+
url: string; // Base URL of Gatekeeper or Keymaster service
|
|
239
|
+
waitUntilReady?: boolean; // Wait for upstream service readiness before completing start
|
|
240
|
+
intervalSeconds?: number; // Poll interval when waiting for ready state
|
|
241
|
+
chatty?: boolean; // Enable verbose logging during connection
|
|
242
|
+
token?: string; // Optional bearer authorization token
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
---
|
|
247
|
+
|
|
248
|
+
## Related Documentation
|
|
249
|
+
|
|
250
|
+
- [Keymaster Integration Guide](../../demo/README.md#integrating-keymaster)
|
|
251
|
+
- [React Frontend SDK (`@yourself_id/siwys-react-js`)](../react-js/README.md)
|
|
252
|
+
- [React Native SDK (`@yourself_id/siwys-react-native`)](../react-native/README.md)
|
|
253
|
+
- [Reference Node & Docker Microservices (KeychainMDIP/kc)](https://github.com/KeychainMDIP/kc)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yourself_id/siwys-api-js",
|
|
3
|
-
"version": "0.36.0",
|
|
3
|
+
"version": "0.36.1-jahatch512-docs-keymaster-init.49.0",
|
|
4
4
|
"description": "A React component library",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -46,5 +46,5 @@
|
|
|
46
46
|
"tsup": "^8.3.0",
|
|
47
47
|
"typescript": "4.9.4"
|
|
48
48
|
},
|
|
49
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "a07a6e39d3dec57ed2da137800488e3badad717c"
|
|
50
50
|
}
|