trustplane-sdk 0.1.1 → 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 +20 -1
- package/index.d.ts +24 -0
- package/index.js +67 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Trustplane JS SDK (v0.
|
|
1
|
+
# Trustplane JS SDK (v0.2)
|
|
2
2
|
|
|
3
3
|
Minimal SDK to generate Trustplane proof headers.
|
|
4
4
|
|
|
@@ -55,3 +55,22 @@ const out = client.sign({
|
|
|
55
55
|
privateKey: '<private_key_b64url>'
|
|
56
56
|
});
|
|
57
57
|
```
|
|
58
|
+
|
|
59
|
+
## Blindfold verify (one call)
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
const { blindfoldVerify } = require('trustplane-sdk');
|
|
63
|
+
|
|
64
|
+
const res = await blindfoldVerify({
|
|
65
|
+
authBaseUrl: 'https://auth.trustplane.mergematter.io',
|
|
66
|
+
tenantId: 'new_tenant',
|
|
67
|
+
apiId: 'api_demo_2',
|
|
68
|
+
clientId: 'client_demo',
|
|
69
|
+
privateKey: '<private_key_b64url>',
|
|
70
|
+
method: 'GET',
|
|
71
|
+
path: '/orders',
|
|
72
|
+
body: '',
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
console.log(res.status, res.data);
|
|
76
|
+
```
|
package/index.d.ts
CHANGED
|
@@ -30,6 +30,18 @@ export type SignOutput = {
|
|
|
30
30
|
|
|
31
31
|
export function sign(input: SignInput): SignOutput;
|
|
32
32
|
export function signAsync(input: SignInput): Promise<SignOutput>;
|
|
33
|
+
export function blindfoldVerify(input: SignInput & {
|
|
34
|
+
authBaseUrl: string;
|
|
35
|
+
fetchFn?: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
36
|
+
}): Promise<{
|
|
37
|
+
step: string;
|
|
38
|
+
status: number;
|
|
39
|
+
ok: boolean;
|
|
40
|
+
data: any;
|
|
41
|
+
verifyPayload?: any;
|
|
42
|
+
transcript?: string;
|
|
43
|
+
digest?: string;
|
|
44
|
+
}>;
|
|
33
45
|
|
|
34
46
|
export function createClient(input: {
|
|
35
47
|
tenantId: string;
|
|
@@ -39,6 +51,18 @@ export function createClient(input: {
|
|
|
39
51
|
}): {
|
|
40
52
|
sign(input: Omit<SignInput, "tenantId" | "apiId" | "clientId" | "bucketSeconds">): SignOutput;
|
|
41
53
|
signAsync(input: Omit<SignInput, "tenantId" | "apiId" | "clientId" | "bucketSeconds">): Promise<SignOutput>;
|
|
54
|
+
blindfoldVerify(input: Omit<SignInput, "tenantId" | "apiId" | "clientId" | "bucketSeconds"> & {
|
|
55
|
+
authBaseUrl: string;
|
|
56
|
+
fetchFn?: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
57
|
+
}): Promise<{
|
|
58
|
+
step: string;
|
|
59
|
+
status: number;
|
|
60
|
+
ok: boolean;
|
|
61
|
+
data: any;
|
|
62
|
+
verifyPayload?: any;
|
|
63
|
+
transcript?: string;
|
|
64
|
+
digest?: string;
|
|
65
|
+
}>;
|
|
42
66
|
};
|
|
43
67
|
|
|
44
68
|
export function fromFile(path: string): ReturnType<typeof createClient>;
|
package/index.js
CHANGED
|
@@ -198,12 +198,79 @@ function createClient({ tenantId, apiId, clientId, bucketSeconds }) {
|
|
|
198
198
|
signProof({ tenantId, apiId, clientId, privateKey, method, path, body, bucketSeconds }),
|
|
199
199
|
signAsync: ({ method, path, body, privateKey }) =>
|
|
200
200
|
signProofAsync({ tenantId, apiId, clientId, privateKey, method, path, body, bucketSeconds }),
|
|
201
|
+
blindfoldVerify: async ({ authBaseUrl, method, path, body, privateKey, fetchFn }) =>
|
|
202
|
+
blindfoldVerify({ authBaseUrl, tenantId, apiId, clientId, privateKey, method, path, body, bucketSeconds, fetchFn }),
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async function httpJSON(url, payload, fetchFn) {
|
|
207
|
+
const impl = fetchFn || (typeof fetch !== 'undefined' ? fetch : null);
|
|
208
|
+
if (!impl) {
|
|
209
|
+
throw new Error('fetch is required; provide fetchFn or run in an environment with global fetch');
|
|
210
|
+
}
|
|
211
|
+
const res = await impl(url, {
|
|
212
|
+
method: 'POST',
|
|
213
|
+
headers: { 'content-type': 'application/json' },
|
|
214
|
+
body: JSON.stringify(payload),
|
|
215
|
+
});
|
|
216
|
+
const text = await res.text();
|
|
217
|
+
let data;
|
|
218
|
+
try {
|
|
219
|
+
data = JSON.parse(text);
|
|
220
|
+
} catch (err) {
|
|
221
|
+
data = { raw: text };
|
|
222
|
+
}
|
|
223
|
+
return { status: res.status, ok: res.ok, data };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async function blindfoldVerify({ authBaseUrl, tenantId, apiId, clientId, privateKey, method, path, body, bucketSeconds, fetchFn }) {
|
|
227
|
+
if (!authBaseUrl) throw new Error('authBaseUrl is required');
|
|
228
|
+
const signed = signProof({ tenantId, apiId, clientId, privateKey, method, path, body, bucketSeconds });
|
|
229
|
+
const base = String(authBaseUrl).replace(/\/+$/, '');
|
|
230
|
+
const start = await httpJSON(base + '/auth/blindfold/start', {
|
|
231
|
+
tenant_id: signed.verifyPayload.tenant_id,
|
|
232
|
+
api_id: signed.verifyPayload.api_id,
|
|
233
|
+
client_id: signed.verifyPayload.client_id,
|
|
234
|
+
method: signed.verifyPayload.method,
|
|
235
|
+
path: signed.verifyPayload.path,
|
|
236
|
+
body_hash: signed.verifyPayload.body_hash,
|
|
237
|
+
time_bucket: signed.verifyPayload.time_bucket,
|
|
238
|
+
nonce: signed.verifyPayload.nonce,
|
|
239
|
+
}, fetchFn);
|
|
240
|
+
if (!start.ok) {
|
|
241
|
+
return { step: 'start', ...start };
|
|
242
|
+
}
|
|
243
|
+
const evalRes = await httpJSON(base + '/oprf/full-evaluate', {
|
|
244
|
+
input_b64url: (start.data || {}).input_b64url,
|
|
245
|
+
}, fetchFn);
|
|
246
|
+
if (!evalRes.ok) {
|
|
247
|
+
return { step: 'evaluate', ...evalRes };
|
|
248
|
+
}
|
|
249
|
+
const finalize = await httpJSON(base + '/auth/blindfold/finalize', {
|
|
250
|
+
session_id: (start.data || {}).session_id,
|
|
251
|
+
output_b64url: (evalRes.data || {}).output_b64url,
|
|
252
|
+
}, fetchFn);
|
|
253
|
+
if (!finalize.ok) {
|
|
254
|
+
return { step: 'finalize', ...finalize };
|
|
255
|
+
}
|
|
256
|
+
const verifyPayload = Object.assign({}, signed.verifyPayload, {
|
|
257
|
+
proof_type: 'blindfold',
|
|
258
|
+
proof_payload: ((finalize.data || {}).verify_payload || {}).proof_payload || '',
|
|
259
|
+
});
|
|
260
|
+
const verify = await httpJSON(base + '/auth/verify', verifyPayload, fetchFn);
|
|
261
|
+
return {
|
|
262
|
+
step: 'verify',
|
|
263
|
+
...verify,
|
|
264
|
+
verifyPayload,
|
|
265
|
+
transcript: signed.transcript,
|
|
266
|
+
digest: signed.digest,
|
|
201
267
|
};
|
|
202
268
|
}
|
|
203
269
|
|
|
204
270
|
module.exports = {
|
|
205
271
|
sign: signProof,
|
|
206
272
|
signAsync: signProofAsync,
|
|
273
|
+
blindfoldVerify,
|
|
207
274
|
createClient,
|
|
208
275
|
fromFile,
|
|
209
276
|
};
|